opentofu/terraform/variables_test.go

98 lines
2.0 KiB
Go
Raw Normal View History

2016-08-17 13:28:58 -05:00
package terraform
import (
"reflect"
"testing"
"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
Override map[string]cty.Value
Expected map[string]cty.Value
2016-08-17 13:28:58 -05:00
}{
"config only": {
"vars-basic",
nil,
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",
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
},
},
"bools: config only": {
"vars-basic-bool",
nil,
map[string]cty.Value{
"a": cty.StringVal("1"),
"b": cty.StringVal("0"),
},
},
"bools: override with string": {
"vars-basic-bool",
map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.StringVal("bar"),
},
map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.StringVal("bar"),
},
},
"bools: override with bool": {
"vars-basic-bool",
map[string]cty.Value{
"a": cty.False,
"b": cty.True,
},
map[string]cty.Value{
"a": cty.StringVal("0"),
"b": cty.StringVal("1"),
},
},
2016-08-17 13:28:58 -05:00
}
for name, tc := range cases {
// Wrapped in a func so we can get defers to work
t.Run(name, func(t *testing.T) {
2016-08-17 13:28:58 -05:00
m := testModule(t, tc.Module)
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) {
t.Fatalf("%s\n\nexpected: %#v\n\ngot: %#v", name, tc.Expected, actual)
2016-08-17 13:28:58 -05:00
}
})
2016-08-17 13:28:58 -05:00
}
}