2016-08-17 13:28:58 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2018-05-07 18:47:24 -05:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
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) {
|
2018-05-07 18:47:24 -05:00
|
|
|
tests := map[string]struct {
|
2016-08-17 13:28:58 -05:00
|
|
|
Module string
|
2018-05-04 21:24:06 -05:00
|
|
|
Override map[string]cty.Value
|
2018-05-07 18:47:24 -05:00
|
|
|
Want InputValues
|
2016-08-17 13:28:58 -05:00
|
|
|
}{
|
|
|
|
"config only": {
|
|
|
|
"vars-basic",
|
|
|
|
nil,
|
2018-05-07 18:47:24 -05:00
|
|
|
InputValues{
|
|
|
|
"a": &InputValue{
|
|
|
|
Value: cty.StringVal("foo"),
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRange{
|
|
|
|
Filename: "test-fixtures/vars-basic/main.tf",
|
|
|
|
Start: tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
|
|
End: tfdiags.SourcePos{Line: 1, Column: 13, Byte: 12},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": &InputValue{
|
|
|
|
Value: cty.ListValEmpty(cty.DynamicPseudoType),
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRange{
|
|
|
|
Filename: "test-fixtures/vars-basic/main.tf",
|
|
|
|
Start: tfdiags.SourcePos{Line: 6, Column: 1, Byte: 58},
|
|
|
|
End: tfdiags.SourcePos{Line: 6, Column: 13, Byte: 70},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"c": &InputValue{
|
|
|
|
Value: cty.MapValEmpty(cty.DynamicPseudoType),
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRange{
|
|
|
|
Filename: "test-fixtures/vars-basic/main.tf",
|
|
|
|
Start: tfdiags.SourcePos{Line: 11, Column: 1, Byte: 111},
|
|
|
|
End: tfdiags.SourcePos{Line: 11, Column: 13, Byte: 123},
|
|
|
|
},
|
|
|
|
},
|
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"),
|
|
|
|
}),
|
|
|
|
},
|
2018-05-07 18:47:24 -05:00
|
|
|
InputValues{
|
|
|
|
"a": &InputValue{
|
|
|
|
Value: cty.StringVal("bar"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
|
|
|
"b": &InputValue{
|
|
|
|
Value: cty.ListVal([]cty.Value{
|
|
|
|
cty.StringVal("foo"),
|
|
|
|
cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
|
|
|
"c": &InputValue{
|
|
|
|
Value: cty.MapVal(map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
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-07 18:47:24 -05:00
|
|
|
InputValues{
|
|
|
|
"a": &InputValue{
|
|
|
|
Value: cty.True,
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRange{
|
|
|
|
Filename: "test-fixtures/vars-basic-bool/main.tf",
|
|
|
|
Start: tfdiags.SourcePos{Line: 4, Column: 1, Byte: 177},
|
|
|
|
End: tfdiags.SourcePos{Line: 4, Column: 13, Byte: 189},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": &InputValue{
|
|
|
|
Value: cty.False,
|
|
|
|
SourceType: ValueFromConfig,
|
|
|
|
SourceRange: tfdiags.SourceRange{
|
|
|
|
Filename: "test-fixtures/vars-basic-bool/main.tf",
|
|
|
|
Start: tfdiags.SourcePos{Line: 8, Column: 1, Byte: 214},
|
|
|
|
End: tfdiags.SourcePos{Line: 8, Column: 13, Byte: 226},
|
|
|
|
},
|
|
|
|
},
|
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-07 18:47:24 -05:00
|
|
|
InputValues{
|
|
|
|
"a": &InputValue{
|
|
|
|
Value: cty.StringVal("foo"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
|
|
|
"b": &InputValue{
|
|
|
|
Value: cty.StringVal("bar"),
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
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-07 18:47:24 -05:00
|
|
|
InputValues{
|
|
|
|
"a": &InputValue{
|
|
|
|
Value: cty.False,
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
|
|
|
"b": &InputValue{
|
|
|
|
Value: cty.True,
|
|
|
|
SourceType: ValueFromCaller,
|
|
|
|
},
|
2016-10-31 13:11:18 -05:00
|
|
|
},
|
|
|
|
},
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
|
|
|
|
2018-05-07 18:47:24 -05:00
|
|
|
for name, test := range tests {
|
2016-08-17 13:28:58 -05:00
|
|
|
// 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) {
|
2018-05-07 18:47:24 -05:00
|
|
|
m := testModule(t, test.Module)
|
2018-05-04 21:24:06 -05:00
|
|
|
fromConfig := DefaultVariableValues(m.Module.Variables)
|
2018-05-07 18:47:24 -05:00
|
|
|
overrides := InputValuesFromCaller(test.Override)
|
|
|
|
got := fromConfig.Override(overrides)
|
2016-08-17 13:28:58 -05:00
|
|
|
|
2018-05-07 18:47:24 -05:00
|
|
|
if !got.Identical(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.Want))
|
|
|
|
}
|
|
|
|
for _, problem := range deep.Equal(got, test.Want) {
|
|
|
|
t.Errorf(problem)
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
2016-10-31 13:11:18 -05:00
|
|
|
})
|
2016-08-17 13:28:58 -05:00
|
|
|
}
|
|
|
|
}
|