Skip to content

Commit

Permalink
Add data template UT (#95)
Browse files Browse the repository at this point in the history
* Add data template UT

* Add data template UT wip

* wip

* minimal test pass

* fixed data template test

* fmt

* wip need to fix project_ids and ssh_keys

* added project ids

* removed sshKeys

* removed sshKeys from data template

* clean code

* added terraform version check

* changed test structure

* fixed merged changes

* fixed compilation error due to api changes
  • Loading branch information
eyalatzmon authored Jun 7, 2021
1 parent dc9b2b2 commit d9646ae
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
11 changes: 0 additions & 11 deletions env0/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ func dataTemplate() *schema.Resource {
Description: "env0_project.id for each project",
},
},
"ssh_keys": {
Type: schema.TypeList,
Description: "which ssh keys are used for accessing git over ssh",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeMap,
Description: "a map of env0_ssh_key.id and env0_ssh_key.name for each project",
},
},
"retries_on_deploy": {
Type: schema.TypeInt,
Description: "number of times to retry when deploying an environment based on this template",
Expand Down Expand Up @@ -121,7 +112,6 @@ func dataTemplateRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.Set("type", template.Type)
d.Set("project_ids", template.ProjectIds)
d.Set("terraform_version", template.TerraformVersion)
d.Set("ssh_keys", template.SshKeys)
if (template.Retry.OnDeploy != client.TemplateRetryOn{}) {
d.Set("retries_on_deploy", template.Retry.OnDeploy.Times)
d.Set("retry_on_deploy_only_when_matches_regex", template.Retry.OnDeploy.ErrorRegex)
Expand All @@ -141,7 +131,6 @@ func dataTemplateRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.Set("github_installation_id", template.GithubInstallationId)
}

//TODO: sshkeys
return nil
}

Expand Down
82 changes: 82 additions & 0 deletions env0/data_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package env0

import (
"strconv"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestUnitTemplateData(t *testing.T) {
resourceType := "env0_template"
resourceName := "test"
resourceFullName := dataSourceAccessor(resourceType, resourceName)
onDeploy := client.TemplateRetryOn{
Times: 2,
ErrorRegex: "error retry on deploy",
}
onDestroy := client.TemplateRetryOn{
Times: 3,
ErrorRegex: "error retry on destroy",
}
templateRetry := client.TemplateRetry{
OnDeploy: onDeploy,
OnDestroy: onDestroy,
}

template := client.Template{
Id: "id0",
Name: "name0",
Repository: "repository",
Path: "path",
Revision: "revision",
Type: "terraform",
TerraformVersion: "0.15.1",
Retry: templateRetry,
ProjectIds: []string{"pId1", "pId2"},
GithubInstallationId: 123,
}

getValidTestCase := func(input map[string]interface{}) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, input),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", template.Id),
resource.TestCheckResourceAttr(resourceFullName, "name", template.Name),
resource.TestCheckResourceAttr(resourceFullName, "repository", template.Repository),
resource.TestCheckResourceAttr(resourceFullName, "path", template.Path),
resource.TestCheckResourceAttr(resourceFullName, "revision", template.Revision),
resource.TestCheckResourceAttr(resourceFullName, "type", template.Type),
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", template.TerraformVersion),
resource.TestCheckResourceAttr(resourceFullName, "retries_on_deploy", strconv.Itoa(template.Retry.OnDeploy.Times)),
resource.TestCheckResourceAttr(resourceFullName, "retry_on_deploy_only_when_matches_regex", template.Retry.OnDeploy.ErrorRegex),
resource.TestCheckResourceAttr(resourceFullName, "retries_on_destroy", strconv.Itoa(template.Retry.OnDestroy.Times)),
resource.TestCheckResourceAttr(resourceFullName, "retry_on_destroy_only_when_matches_regex", template.Retry.OnDestroy.ErrorRegex),
resource.TestCheckResourceAttr(resourceFullName, "github_installation_id", strconv.Itoa(template.GithubInstallationId)),
resource.TestCheckResourceAttr(resourceFullName, "project_ids.0", template.ProjectIds[0]),
resource.TestCheckResourceAttr(resourceFullName, "project_ids.1", template.ProjectIds[1]),
),
},
},
}
}

t.Run("Template By ID", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(map[string]interface{}{"id": template.Id}),
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Template(template.Id).AnyTimes().Return(template, nil)
})
})

t.Run("Template By Name", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(map[string]interface{}{"name": template.Name}),
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Templates().AnyTimes().Return([]client.Template{template}, nil)
})
})
}
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env0

import (
"context"

"github.com/env0/terraform-provider-env0/client"
"github.com/env0/terraform-provider-env0/client/http"
"github.com/go-resty/resty/v2"
Expand Down

0 comments on commit d9646ae

Please sign in to comment.