Skip to content

Commit

Permalink
Merge pull request #5 from coveooss/feature/SRC-3322-add-test-coverage
Browse files Browse the repository at this point in the history
Feature/src 3322 add test coverage
  • Loading branch information
Jean-Michel Provencher authored Oct 17, 2019
2 parents a410561 + 12693f9 commit e0a155d
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ terraform-provider-launchdarkly
output
.terraform
crash.log
.idea/.gitignore
.idea/misc.xml
.idea/modules.xml
.idea/terraform-provider-launchdarkly.iml
.idea/vcs.xml
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
SOURCES = $(wildcard *.go)
TEST?=./launchdarkly

.PHONY: default
default: build cross-compile

.PHONY: build
build:
go build
go test $(TEST) -timeout=30s -parallel=4

.PHONY: clean
clean:
Expand All @@ -22,7 +24,5 @@ cross-compile:
tar -C output/linux_amd64 -czf output/terraform-provider-launchdarkly_linux_amd64.tar.gz terraform-provider-launchdarkly

.PHONY: test
test: build
terraform init
terraform apply
terraform destroy
test:
go test $(TEST) -timeout=30s -parallel=4
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ For the `project` resource you only need the project key. e.g.: `import launchda

## Building the provider
Clone the repository, and run `make` at the root of the working copy.

## Testing the provider
run `make test` at the root of the working copy.

136 changes: 136 additions & 0 deletions launchdarkly/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package launchdarkly

import (
"errors"
"github.com/hashicorp/terraform/helper/schema"
"reflect"
"testing"
)

func TestParseCompositeID(t *testing.T) {
expectedErr := errors.New("error: Import composite ID requires two parts separated by colon, eg x:y")

testCases := []struct {
name string
id string
wantedP1 string
wantedP2 string
wantedErr error
}{
{
name: "expected",
id: "id:test",
wantedP1: "id",
wantedP2: "test",
wantedErr: nil,
},
{
name: "with more than one separator",
id: "id:test:id",
wantedP1: "id",
wantedP2: "test:id",
wantedErr: nil,
},
{
name: "without separator",
id: "test",
wantedP1: "",
wantedP2: "",
wantedErr: expectedErr,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
p1, p2, err := parseCompositeID(testCase.id)
testParseCompositeIDVerify(t, p1, p2, err, testCase)
})
}
}

func testReadMethod(d *schema.ResourceData, m interface{}) error { return nil }

func TestResourceImport(t *testing.T) {
resourceKey := "resource"
projectKey := "project"
dTest := new(schema.ResourceData)
dTest.SetId(projectKey + ":" + resourceKey)

dTestWithError := new(schema.ResourceData)
dTestWithError.SetId(resourceKey)

wantedResourceData := new(schema.ResourceData)
wantedResourceData.SetId(resourceKey)
wantedResourceData.Set("project_key", projectKey)
wantedResourceData.Set("key", resourceKey)

expectedErr := errors.New("error: Import composite ID requires two parts separated by colon, eg x:y")

testCases := []struct {
name string
readMethod importFunc
d *schema.ResourceData
meta interface{}
wantedResourceData []*schema.ResourceData
wantedErr error
}{
{
name: "expected",
readMethod: testReadMethod,
d: dTest,
meta: nil,
wantedResourceData: []*schema.ResourceData{wantedResourceData},
wantedErr: nil,
},
{
name: "with bad resource/project key formatting",
readMethod: testReadMethod,
d: dTestWithError,
meta: nil,
wantedResourceData: nil,
wantedErr: expectedErr,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
resourceData, err := resourceImport(testCase.readMethod, testCase.d, testCase.meta)
testResourceImportVerify(t, resourceData, err, testCase.wantedResourceData, testCase.wantedErr)
})
}
}

func testParseCompositeIDVerify(t *testing.T, p1 string, p2 string, err error, testCase struct {
name string
id string
wantedP1 string
wantedP2 string
wantedErr error
}) {
if p1 != testCase.wantedP1 {
t.Errorf("got string (%s) but want (%s)", p1, testCase.wantedP1)
}

if p2 != testCase.wantedP2 {
t.Errorf("got string (%s) but want (%s)", p2, testCase.wantedP2)
}

if testCase.wantedErr != nil {
if err.Error() != testCase.wantedErr.Error() {
t.Errorf("got error (%s) but want (%s)", err, testCase.wantedErr)
}
}
}

func testResourceImportVerify(t *testing.T, resourceData []*schema.ResourceData, err error, wantedResourceData []*schema.ResourceData, wantedErr error) {

if !reflect.DeepEqual(resourceData, wantedResourceData) {
t.Errorf("resourceData is not equal to wantedResourceData")
}

if wantedErr != nil {
if err.Error() != wantedErr.Error() {
t.Errorf("got error (%s) but want (%s)", err, wantedErr)
}
}
}
58 changes: 58 additions & 0 deletions launchdarkly/urls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package launchdarkly

import (
"testing"
)

const launchDarklyApiUrl = "https://app.launchdarkly.com/api/v2/"
const aProjectName = "my-project"

func TestGetProjectCreateUrl(t *testing.T) {
expectedUrl := launchDarklyApiUrl + "projects"
returnedUrl := getProjectCreateUrl()
if returnedUrl != expectedUrl {
t.Errorf("getProjectCreateUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}

func TestGetProjectUrl(t *testing.T) {
expectedUrl := launchDarklyApiUrl + "projects/" + aProjectName
returnedUrl := getProjectUrl(aProjectName)
if returnedUrl != expectedUrl {
t.Errorf("getProjectUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}

func TestGetFlagCreateUrl(t *testing.T) {
expectedUrl := launchDarklyApiUrl + "flags/" + aProjectName
returnedUrl := getFlagCreateUrl(aProjectName)
if returnedUrl != expectedUrl {
t.Errorf("getFlagCreateUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}

func TestGetFlagUrl(t *testing.T) {
aFlagName := "my-super-flag"
expectedUrl := launchDarklyApiUrl + "flags/" + aProjectName + "/" + aFlagName
returnedUrl := getFlagUrl(aProjectName, aFlagName)
if returnedUrl != expectedUrl {
t.Errorf("getFlagUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}

func TestGetEnvironmentCreateUrl(t *testing.T) {
expectedUrl := launchDarklyApiUrl + "projects/" + aProjectName + "/environments"
returnedUrl := getEnvironmentCreateUrl(aProjectName)
if returnedUrl != expectedUrl {
t.Errorf("getEnvironmentCreateUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}

func TestGetEnvironmentUrl(t *testing.T) {
anEnvironmentName := "my-marvelous-environment"
expectedUrl := launchDarklyApiUrl + "projects/" + aProjectName + "/environments/" + anEnvironmentName
returnedUrl := getEnvironmentUrl(aProjectName, anEnvironmentName)
if returnedUrl != expectedUrl {
t.Errorf("getEnvironmentUrl expected return value was '%s' but got '%s'", expectedUrl, returnedUrl)
}
}
2 changes: 1 addition & 1 deletion launchdarkly/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func validateColor(v interface{}, k string) ([]string, []error) {
}

if !matched {
return nil, []error{errors.New(fmt.Sprintf("%s is not a valid RGB color code: %s", k, value))}
return nil, []error{errors.New(fmt.Sprintf("%s is not a valid HEX color code: %s", k, value))}
}

return nil, nil
Expand Down
Loading

0 comments on commit e0a155d

Please sign in to comment.