From cc5abd0815b0b89fab9387d4fa5c54f65eb19d7f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Aug 2016 11:28:58 -0700 Subject: [PATCH] terraform: add tests for variables --- terraform/context_apply_test.go | 11 +- terraform/terraform_test.go | 5 +- terraform/test-fixtures/vars-basic/main.tf | 14 +++ terraform/variables_test.go | 114 +++++++++++++++++++++ 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 terraform/test-fixtures/vars-basic/main.tf create mode 100644 terraform/variables_test.go diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 6c2a493c58..d01174ed80 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -3,7 +3,6 @@ package terraform import ( "bytes" "fmt" - "os" "reflect" "sort" "strings" @@ -4314,13 +4313,9 @@ func TestContext2Apply_vars(t *testing.T) { func TestContext2Apply_varsEnv(t *testing.T) { // Set the env var - old_ami := tempEnv(t, "TF_VAR_ami", "baz") - old_list := tempEnv(t, "TF_VAR_list", `["Hello", "World"]`) - old_map := tempEnv(t, "TF_VAR_map", `{"Hello" = "World", "Foo" = "Bar", "Baz" = "Foo"}`) - - defer os.Setenv("TF_VAR_ami", old_ami) - defer os.Setenv("TF_VAR_list", old_list) - defer os.Setenv("TF_VAR_list", old_map) + defer tempEnv(t, "TF_VAR_ami", "baz")() + defer tempEnv(t, "TF_VAR_list", `["Hello", "World"]`)() + defer tempEnv(t, "TF_VAR_map", `{"Hello" = "World", "Foo" = "Bar", "Baz" = "Foo"}`)() m := testModule(t, "apply-vars-env") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 469d5376db..e6dc575e52 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -47,11 +47,12 @@ func tempDir(t *testing.T) string { } // tempEnv lets you temporarily set an environment variable. It returns +// a function to defer to reset the old value. // the old value that should be set via a defer. -func tempEnv(t *testing.T, k string, v string) string { +func tempEnv(t *testing.T, k string, v string) func() { old := os.Getenv(k) os.Setenv(k, v) - return old + return func() { os.Setenv(k, old) } } func testConfig(t *testing.T, name string) *config.Config { diff --git a/terraform/test-fixtures/vars-basic/main.tf b/terraform/test-fixtures/vars-basic/main.tf new file mode 100644 index 0000000000..66fa77a8be --- /dev/null +++ b/terraform/test-fixtures/vars-basic/main.tf @@ -0,0 +1,14 @@ +variable "a" { + default = "foo" + type = "string" +} + +variable "b" { + default = [] + type = "list" +} + +variable "c" { + default = {} + type = "map" +} diff --git a/terraform/variables_test.go b/terraform/variables_test.go new file mode 100644 index 0000000000..45a59240a3 --- /dev/null +++ b/terraform/variables_test.go @@ -0,0 +1,114 @@ +package terraform + +import ( + "reflect" + "testing" +) + +func TestVariables(t *testing.T) { + cases := map[string]struct { + Module string + Env map[string]string + Override map[string]interface{} + Error bool + Expected map[string]interface{} + }{ + "config only": { + "vars-basic", + nil, + nil, + false, + map[string]interface{}{ + "a": "foo", + "b": []interface{}{}, + "c": map[string]interface{}{}, + }, + }, + + "env vars": { + "vars-basic", + map[string]string{ + "TF_VAR_a": "bar", + "TF_VAR_b": `["foo", "bar"]`, + "TF_VAR_c": `{"foo" = "bar"}`, + }, + nil, + false, + map[string]interface{}{ + "a": "bar", + "b": []interface{}{"foo", "bar"}, + "c": map[string]interface{}{ + "foo": "bar", + }, + }, + }, + + "override": { + "vars-basic", + nil, + map[string]interface{}{ + "a": "bar", + "b": []interface{}{"foo", "bar"}, + "c": map[string]interface{}{ + "foo": "bar", + }, + }, + false, + map[string]interface{}{ + "a": "bar", + "b": []interface{}{"foo", "bar"}, + "c": map[string]interface{}{ + "foo": "bar", + }, + }, + }, + + "override partial map": { + "vars-basic", + map[string]string{ + "TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`, + }, + map[string]interface{}{ + "c": map[string]interface{}{ + "foo": "bar", + }, + }, + false, + map[string]interface{}{ + "a": "foo", + "b": []interface{}{}, + "c": map[string]interface{}{ + "foo": "bar", + "bar": "baz", + }, + }, + }, + } + + for name, tc := range cases { + if name != "override partial map" { + continue + } + + // Wrapped in a func so we can get defers to work + func() { + // Set the env vars + for k, v := range tc.Env { + defer tempEnv(t, k, v)() + } + + m := testModule(t, tc.Module) + actual, err := Variables(m, tc.Override) + if (err != nil) != tc.Error { + t.Fatalf("%s: err: %s", err) + } + if err != nil { + return + } + + if !reflect.DeepEqual(actual, tc.Expected) { + t.Fatalf("%s: expected: %#v\n\ngot: %#v", name, tc.Expected, actual) + } + }() + } +}