Skip to content

Commit

Permalink
Adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
relusc committed Jul 22, 2021
1 parent ae54964 commit 1425a47
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.terraform
terraform.tfstate
terraform.tfvars
terraform.tfvars.json
*.tfstate*
.terragrunt
.terragrunt-cache
Expand Down
4 changes: 2 additions & 2 deletions modules/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func CopyFolderContentsWithFilter(source string, destination string, filter func
return nil
}

// PathContainsTerraformStateOrVars returns true if the path corresponds to a Terraform state file or .tfvars file.
// PathContainsTerraformStateOrVars returns true if the path corresponds to a Terraform state file or .tfvars/.tfvars.json file.
func PathContainsTerraformStateOrVars(path string) bool {
filename := filepath.Base(path)
return filename == "terraform.tfstate" || filename == "terraform.tfstate.backup" || filename == "terraform.tfvars"
return filename == "terraform.tfstate" || filename == "terraform.tfstate.backup" || filename == "terraform.tfvars" || filename == "terraform.tfvars.json"
}

// PathContainsTerraformState returns true if the path corresponds to a Terraform state file.
Expand Down
28 changes: 28 additions & 0 deletions modules/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ func TestCopyTerragruntFolderToTemp(t *testing.T) {
requireDirectoriesEqual(t, expectedDir, tmpDir)
}

func TestPathContainsTerraformStateOrVars(t *testing.T) {
var data = []struct {
desc string
path string
contains bool
}{
{"contains tfvars", "./folder/terraform.tfvars", true},
{"contains tfvars.json", "./folder/hello/terraform.tfvars.json", true},
{"contains state", "./folder/hello/helloagain/terraform.tfstate", true},
{"contains state backup", "./folder/pewpew/terraform.tfstate.backup", true},
{"does not contain any", "./folder/pewpew/terraform.json", false},
}

for _, tt := range data {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
result := PathContainsTerraformStateOrVars(tt.path)
if result != tt.contains {
if tt.contains {
t.Errorf("Expected %s to contain Terraform related file", tt.path)
} else {
t.Errorf("Expected %s to not contain Terraform related file", tt.path)
}
}
})
}
}

// Diffing two directories to ensure they have the exact same files, contents, etc and showing exactly what's different
// takes a lot of code. Why waste time on that when this functionality is already nicely implemented in the Unix/Linux
// "diff" command? We shell out to that command at test time.
Expand Down
4 changes: 2 additions & 2 deletions modules/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func FormatTerraformPluginDirAsArgs(pluginDir string) []string {
return pluginArgs
}

// FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars"})
// returns "-var-file foo.tfvars -var-file bar.tfvars"
// FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars", "baz.tfvars.json"})
// returns "-var-file foo.tfvars -var-file bar.tfvars -var-file baz.tfvars.json"
func FormatTerraformArgs(argName string, args []string) []string {
argsList := []string{}
for _, argValue := range args {
Expand Down
236 changes: 236 additions & 0 deletions modules/terraform/var-file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,243 @@ func TestGetAllVariablesFromVarFileStructOut(t *testing.T) {
err := GetAllVariablesFromVarFileE(t, randomFileName, &region)
require.NoError(t, err)
require.Equal(t, "us-east-2", region.AwsRegion)
}

func TestGetVariablesFromVarFilesAsStringJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"number_type": 2,
"boolean_type": true,
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

stringVal := GetVariableAsStringFromVarFile(t, randomFileName, "aws_region")

boolString := GetVariableAsStringFromVarFile(t, randomFileName, "boolean_type")

numString := GetVariableAsStringFromVarFile(t, randomFileName, "number_type")

require.Equal(t, "us-east-2", stringVal)
require.Equal(t, "true", boolString)
require.Equal(t, "2", numString)

}

func TestGetVariablesFromVarFilesAsStringKeyDoesNotExistJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

_, err := GetVariableAsStringFromVarFileE(t, randomFileName, "badkey")

require.Error(t, err)
}

func TestGetVariableAsMapFromVarFileJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())
expected := make(map[string]string)
expected["foo"] = "bar"

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

val := GetVariableAsMapFromVarFile(t, randomFileName, "tags")
require.Equal(t, expected, val)
}

func TestGetVariableAsMapFromVarFileNotMapJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

_, err := GetVariableAsMapFromVarFileE(t, randomFileName, "aws_region")

require.Error(t, err)
}

func TestGetVariableAsMapFromVarFileKeyDoesNotExistJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

_, err := GetVariableAsMapFromVarFileE(t, randomFileName, "badkey")

require.Error(t, err)
}

func TestGetVariableAsListFromVarFileJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())
expected := []string{"item1"}

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

val := GetVariableAsListFromVarFile(t, randomFileName, "list")

require.Equal(t, expected, val)
}

func TestGetVariableAsListNotListJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

_, err := GetVariableAsListFromVarFileE(t, randomFileName, "tags")

require.Error(t, err)
}

func TestGetVariableAsListKeyDoesNotExistJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())

testJSON := []byte(`
{
"aws_region": "us-east-2",
"aws_account_id": "111111111111",
"tags": {
"foo": "bar"
},
"list": ["item1"]
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

_, err := GetVariableAsListFromVarFileE(t, randomFileName, "badkey")

require.Error(t, err)
}

func TestGetAllVariablesFromVarFileBadFileJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())
testJSON := []byte(`
{
thiswillnotwork
}`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

var variables map[string]interface{}
err := GetAllVariablesFromVarFileE(t, randomFileName, &variables)
require.Error(t, err)

// HCL library could change their error string, so we are only testing the error string contains what we add to it
require.Regexp(t, fmt.Sprintf("^%s:3,7-22: ", randomFileName), err.Error())
}

func TestGetAllVariablesFromVarFileJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())
testJSON := []byte(`
{
"aws_region": "us-east-2"
}
`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

var variables map[string]interface{}
err := GetAllVariablesFromVarFileE(t, randomFileName, &variables)
require.NoError(t, err)

expected := make(map[string]interface{})
expected["aws_region"] = "us-east-2"

require.Equal(t, expected, variables)
}

func TestGetAllVariablesFromVarFileStructOutJSON(t *testing.T) {
randomFileName := fmt.Sprintf("./%s.tfvars.json", random.UniqueId())
testJSON := []byte(`
{
"aws_region": "us-east-2"
}
`)

WriteFile(t, randomFileName, testJSON)
defer os.Remove(randomFileName)

var region struct {
AwsRegion string `cty:"aws_region"`
}
err := GetAllVariablesFromVarFileE(t, randomFileName, &region)
require.NoError(t, err)
require.Equal(t, "us-east-2", region.AwsRegion)
}

// Helper function to write a file to the filesystem
Expand Down

0 comments on commit 1425a47

Please sign in to comment.