terraform: add tests for variables

This commit is contained in:
Mitchell Hashimoto 2016-08-17 11:28:58 -07:00
parent af7085f671
commit cc5abd0815
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 134 additions and 10 deletions

View File

@ -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")

View File

@ -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 {

View File

@ -0,0 +1,14 @@
variable "a" {
default = "foo"
type = "string"
}
variable "b" {
default = []
type = "list"
}
variable "c" {
default = {}
type = "map"
}

114
terraform/variables_test.go Normal file
View File

@ -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)
}
}()
}
}