mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
681d94ae20
Terraform 0.7 introduces lists and maps as first-class values for variables, in addition to string values which were previously available. However, there was previously no way to override the default value of a list or map, and the functionality for overriding specific map keys was broken. Using the environment variable method for setting variable values, there was previously no way to give a variable a value of a list or map. These now support HCL for individual values - specifying: TF_VAR_test='["Hello", "World"]' will set the variable `test` to a two-element list containing "Hello" and "World". Specifying TF_VAR_test_map='{"Hello = "World", "Foo" = "bar"}' will set the variable `test_map` to a two-element map with keys "Hello" and "Foo", and values "World" and "bar" respectively. The same logic is applied to `-var` flags, and the file parsed by `-var-files` ("autoVariables"). Note that care must be taken to not run into shell expansion for `-var-` flags and environment variables. We also merge map keys where appropriate. The override syntax has changed (to be noted in CHANGELOG as a breaking change), so several tests needed their syntax updating from the old `amis.us-east-1 = "newValue"` style to `amis = "{ "us-east-1" = "newValue"}"` style as defined in TF-002. In order to continue supporting the `-var "foo=bar"` type of variable flag (which is not valid HCL), a special case error is checked after HCL parsing fails, and the old code path runs instead.
201 lines
3.2 KiB
Go
201 lines
3.2 KiB
Go
package command
|
|
|
|
import (
|
|
"flag"
|
|
"github.com/davecgh/go-spew/spew"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestFlagStringKV_impl(t *testing.T) {
|
|
var _ flag.Value = new(FlagStringKV)
|
|
}
|
|
|
|
func TestFlagStringKV(t *testing.T) {
|
|
cases := []struct {
|
|
Input string
|
|
Output map[string]string
|
|
Error bool
|
|
}{
|
|
{
|
|
"key=value",
|
|
map[string]string{"key": "value"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key=",
|
|
map[string]string{"key": ""},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key=foo=bar",
|
|
map[string]string{"key": "foo=bar"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"map.key=foo",
|
|
map[string]string{"map.key": "foo"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key",
|
|
nil,
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
f := new(FlagStringKV)
|
|
err := f.Set(tc.Input)
|
|
if err != nil != tc.Error {
|
|
t.Fatalf("bad error. Input: %#v\n\nError: %s", tc.Input, err)
|
|
}
|
|
|
|
actual := map[string]string(*f)
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
t.Fatalf("bad: %#v", actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFlagTypedKV_impl(t *testing.T) {
|
|
var _ flag.Value = new(FlagTypedKV)
|
|
}
|
|
|
|
func TestFlagTypedKV(t *testing.T) {
|
|
cases := []struct {
|
|
Input string
|
|
Output map[string]interface{}
|
|
Error bool
|
|
}{
|
|
{
|
|
"key=value",
|
|
map[string]interface{}{"key": "value"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key=",
|
|
map[string]interface{}{"key": ""},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key=foo=bar",
|
|
map[string]interface{}{"key": "foo=bar"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"map.key=foo",
|
|
map[string]interface{}{"map.key": "foo"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
"key",
|
|
nil,
|
|
true,
|
|
},
|
|
|
|
{
|
|
`key=["hello", "world"]`,
|
|
map[string]interface{}{"key": []interface{}{"hello", "world"}},
|
|
false,
|
|
},
|
|
|
|
{
|
|
`key={"hello" = "world", "foo" = "bar"}`,
|
|
map[string]interface{}{
|
|
"key": []map[string]interface{}{
|
|
map[string]interface{}{
|
|
"hello": "world",
|
|
"foo": "bar",
|
|
},
|
|
},
|
|
},
|
|
false,
|
|
},
|
|
|
|
{
|
|
`key={"hello" = "world", "foo" = "bar"}\nkey2="invalid"`,
|
|
nil,
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
f := new(FlagTypedKV)
|
|
err := f.Set(tc.Input)
|
|
if err != nil != tc.Error {
|
|
t.Fatalf("bad error. Input: %#v\n\nError: %s", tc.Input, err)
|
|
}
|
|
|
|
actual := map[string]interface{}(*f)
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
t.Fatalf("bad:\nexpected: %s\n\n got: %s\n", spew.Sdump(tc.Output), spew.Sdump(actual))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFlagKVFile_impl(t *testing.T) {
|
|
var _ flag.Value = new(FlagKVFile)
|
|
}
|
|
|
|
func TestFlagKVFile(t *testing.T) {
|
|
inputLibucl := `
|
|
foo = "bar"
|
|
`
|
|
|
|
inputJson := `{
|
|
"foo": "bar"}`
|
|
|
|
cases := []struct {
|
|
Input string
|
|
Output map[string]interface{}
|
|
Error bool
|
|
}{
|
|
{
|
|
inputLibucl,
|
|
map[string]interface{}{"foo": "bar"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
inputJson,
|
|
map[string]interface{}{"foo": "bar"},
|
|
false,
|
|
},
|
|
|
|
{
|
|
`map.key = "foo"`,
|
|
map[string]interface{}{"map.key": "foo"},
|
|
false,
|
|
},
|
|
}
|
|
|
|
path := testTempFile(t)
|
|
|
|
for _, tc := range cases {
|
|
if err := ioutil.WriteFile(path, []byte(tc.Input), 0644); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
f := new(FlagKVFile)
|
|
err := f.Set(path)
|
|
if err != nil != tc.Error {
|
|
t.Fatalf("bad error. Input: %#v, err: %s", tc.Input, err)
|
|
}
|
|
|
|
actual := map[string]interface{}(*f)
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
t.Fatalf("bad: %#v", actual)
|
|
}
|
|
}
|
|
}
|