2016-08-17 13:28:58 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2016-08-17 13:28:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVariables(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Module string
|
2018-05-04 21:24:06 -05:00
|
|
|
Override map[string]cty.Value
|
|
|
|
Expected map[string]cty.Value
|
2016-08-17 13:28:58 -05:00
|
|
|
}{
|
|
|
|
"config only": {
|
|
|
|
"vars-basic",
|
|
|
|
nil,
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("foo"),
|
|
|
|
"b": cty.ListValEmpty(cty.String),
|
|
|
|
"c": cty.MapValEmpty(cty.String),
|
2016-08-17 13:28:58 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"override": {
|
|
|
|
"vars-basic",
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("bar"),
|
|
|
|
"b": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("foo"),
|
|
|
|
cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
"c": cty.MapVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("bar"),
|
|
|
|
"b": cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("foo"),
|
|
|
|
cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
"c": cty.MapVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
2016-08-17 13:28:58 -05:00
|
|
|
},
|
|
|
|
},
|
2016-10-31 13:11:18 -05:00
|
|
|
|
|
|
|
"bools: config only": {
|
|
|
|
"vars-basic-bool",
|
|
|
|
nil,
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("1"),
|
|
|
|
"b": cty.StringVal("0"),
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"bools: override with string": {
|
|
|
|
"vars-basic-bool",
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("foo"),
|
|
|
|
"b": cty.StringVal("bar"),
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("foo"),
|
|
|
|
"b": cty.StringVal("bar"),
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"bools: override with bool": {
|
|
|
|
"vars-basic-bool",
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.False,
|
|
|
|
"b": cty.True,
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
2018-05-04 21:24:06 -05:00
|
|
|
map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("0"),
|
|
|
|
"b": cty.StringVal("1"),
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
|
|
|
},
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
// Wrapped in a func so we can get defers to work
|
2016-10-31 13:11:18 -05:00
|
|
|
t.Run(name, func(t *testing.T) {
|
2016-08-17 13:28:58 -05:00
|
|
|
m := testModule(t, tc.Module)
|
2018-05-04 21:24:06 -05:00
|
|
|
fromConfig := DefaultVariableValues(m.Module.Variables)
|
|
|
|
overrides := InputValuesFromCaller(tc.Override)
|
|
|
|
actual := fromConfig.Override(overrides)
|
2016-08-17 13:28:58 -05:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, tc.Expected) {
|
2016-10-31 13:11:18 -05:00
|
|
|
t.Fatalf("%s\n\nexpected: %#v\n\ngot: %#v", name, tc.Expected, actual)
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
2016-10-31 13:11:18 -05:00
|
|
|
})
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
|
|
|
}
|