From 35b6715ae37e6ecae3a888bc48d24df77f6afb59 Mon Sep 17 00:00:00 2001 From: Raz Ben Simon Date: Mon, 7 Jun 2021 16:52:57 +0300 Subject: [PATCH] Chore: UT: data - Configuration Variable - tests (#116) Co-authored-by: Hever Farber --- env0/data_configuration_variable_test.go | 106 +++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 env0/data_configuration_variable_test.go diff --git a/env0/data_configuration_variable_test.go b/env0/data_configuration_variable_test.go new file mode 100644 index 00000000..9d68d738 --- /dev/null +++ b/env0/data_configuration_variable_test.go @@ -0,0 +1,106 @@ +package env0 + +import ( + "errors" + + "github.com/env0/terraform-provider-env0/client" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "regexp" + "strconv" + "testing" +) + +func TestUnitConfigurationVariableData(t *testing.T) { + resourceType := "env0_configuration_variable" + resourceName := "test" + accessor := dataSourceAccessor(resourceType, resourceName) + configurationVariable := client.ConfigurationVariable{ + Id: "id0", + Name: "name0", + ScopeId: "scope0", + Value: "value0", + OrganizationId: "organization0", + UserId: "user0", + IsSensitive: false, + Scope: client.ScopeEnvironment, + Type: client.ConfigurationVariableTypeEnvironment, + Schema: client.ConfigurationVariableSchema{Type: "string"}, + } + + checkResources := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(accessor, "id", configurationVariable.Id), + resource.TestCheckResourceAttr(accessor, "name", configurationVariable.Name), + resource.TestCheckResourceAttr(accessor, "type", "environment"), + resource.TestCheckResourceAttr(accessor, "value", configurationVariable.Value), + resource.TestCheckResourceAttr(accessor, "scope", string(configurationVariable.Scope)), + resource.TestCheckResourceAttr(accessor, "is_sensitive", strconv.FormatBool(configurationVariable.IsSensitive)), + ) + + t.Run("ScopeGlobal", func(t *testing.T) { + runUnitTest(t, + resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"id": configurationVariable.Id}), + Check: checkResources, + }, + { + Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": configurationVariable.Name}), + Check: checkResources, + }, + }, + }, + func(mock *client.MockApiClientInterface) { + mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes(). + Return([]client.ConfigurationVariable{configurationVariable}, nil) + }) + }) + + t.Run("ScopeTemplate", func(t *testing.T) { + runUnitTest(t, + resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"id": configurationVariable.Id, "template_id": "template_id"}), + Check: checkResources, + }, + }, + }, + func(mock *client.MockApiClientInterface) { + mock.EXPECT().ConfigurationVariables(client.ScopeTemplate, "template_id").AnyTimes(). + Return([]client.ConfigurationVariable{configurationVariable}, nil) + }) + }) + + t.Run("configuration variable not exists in the server", func(t *testing.T) { + runUnitTest(t, + resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "invalid"}), + ExpectError: regexp.MustCompile("Could not query variables"), + }, + }, + }, + func(mock *client.MockApiClientInterface) { + mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes(). + Return(nil, errors.New("not found")) + }) + }) + + t.Run("configuration variable not match to name", func(t *testing.T) { + runUnitTest(t, + resource.TestCase{ + Steps: []resource.TestStep{ + { + Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "invalid"}), + ExpectError: regexp.MustCompile("Could not find variable"), + }, + }, + }, + func(mock *client.MockApiClientInterface) { + mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes(). + Return([]client.ConfigurationVariable{configurationVariable}, nil) + }) + }) +}