Skip to content

Commit

Permalink
Chore: UT: data - Configuration Variable - tests (#116)
Browse files Browse the repository at this point in the history
Co-authored-by: Hever Farber <[email protected]>
  • Loading branch information
razbensimon and HeverFarber committed Jun 7, 2021
1 parent d9646ae commit 35b6715
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
}

0 comments on commit 35b6715

Please sign in to comment.