2015-01-03 16:32:24 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMultiLevelFieldReaderReadFieldExact(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Addr []string
|
|
|
|
Readers []FieldReader
|
|
|
|
Level string
|
|
|
|
Result FieldReadResult
|
|
|
|
}{
|
|
|
|
"specific": {
|
|
|
|
Addr: []string{"foo"},
|
|
|
|
|
|
|
|
Readers: []FieldReader{
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"foo": "bar",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"foo": "baz",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Level: "1",
|
|
|
|
Result: FieldReadResult{
|
|
|
|
Value: "baz",
|
|
|
|
Exists: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
readers := make(map[string]FieldReader)
|
|
|
|
levels := make([]string, len(tc.Readers))
|
|
|
|
for i, r := range tc.Readers {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
|
|
|
readers[is] = r
|
|
|
|
levels[i] = is
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &MultiLevelFieldReader{
|
|
|
|
Readers: readers,
|
|
|
|
Levels: levels,
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
out, err := r.ReadFieldExact(tc.Addr, tc.Level)
|
2015-01-03 16:32:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: err: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.Result, out) {
|
|
|
|
t.Fatalf("%s: bad: %#v", name, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiLevelFieldReaderReadFieldMerge(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Addr []string
|
|
|
|
Readers []FieldReader
|
|
|
|
Result FieldReadResult
|
|
|
|
}{
|
|
|
|
"stringInDiff": {
|
|
|
|
Addr: []string{"availability_zone"},
|
|
|
|
|
|
|
|
Readers: []FieldReader{
|
|
|
|
&DiffFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{Type: TypeString},
|
|
|
|
},
|
|
|
|
|
2015-01-03 16:32:24 +01:00
|
|
|
Source: &MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"availability_zone": "foo",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
RequiresNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: FieldReadResult{
|
|
|
|
Value: "bar",
|
|
|
|
Exists: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"lastLevelComputed": {
|
|
|
|
Addr: []string{"availability_zone"},
|
|
|
|
|
|
|
|
Readers: []FieldReader{
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{Type: TypeString},
|
|
|
|
},
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"availability_zone": "foo",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
&DiffFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{Type: TypeString},
|
|
|
|
},
|
|
|
|
|
2015-01-03 16:32:24 +01:00
|
|
|
Source: &MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"availability_zone": &Schema{Type: TypeString},
|
|
|
|
},
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"availability_zone": "foo",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"availability_zone": &terraform.ResourceAttrDiff{
|
|
|
|
Old: "foo",
|
|
|
|
New: "bar",
|
|
|
|
NewComputed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: FieldReadResult{
|
|
|
|
Value: "",
|
|
|
|
Exists: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"list of maps with removal in diff": {
|
|
|
|
Addr: []string{"config_vars"},
|
|
|
|
|
|
|
|
Readers: []FieldReader{
|
|
|
|
&DiffFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"config_vars": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Schema{Type: TypeMap},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-01-03 16:32:24 +01:00
|
|
|
Source: &MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"config_vars": &Schema{
|
|
|
|
Type: TypeList,
|
|
|
|
Elem: &Schema{Type: TypeMap},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"config_vars.#": "2",
|
|
|
|
"config_vars.0.foo": "bar",
|
|
|
|
"config_vars.0.bar": "bar",
|
|
|
|
"config_vars.1.bar": "baz",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Diff: &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"config_vars.0.bar": &terraform.ResourceAttrDiff{
|
|
|
|
NewRemoved: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: FieldReadResult{
|
|
|
|
Value: []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Exists: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"first level only": {
|
|
|
|
Addr: []string{"foo"},
|
|
|
|
|
|
|
|
Readers: []FieldReader{
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-03 16:32:24 +01:00
|
|
|
"foo": "bar",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
&MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
|
|
|
"foo": &Schema{Type: TypeString},
|
|
|
|
},
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{}),
|
2015-01-03 16:32:24 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Result: FieldReadResult{
|
|
|
|
Value: "bar",
|
|
|
|
Exists: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
readers := make(map[string]FieldReader)
|
|
|
|
levels := make([]string, len(tc.Readers))
|
|
|
|
for i, r := range tc.Readers {
|
|
|
|
is := strconv.FormatInt(int64(i), 10)
|
|
|
|
readers[is] = r
|
|
|
|
levels[i] = is
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &MultiLevelFieldReader{
|
|
|
|
Readers: readers,
|
|
|
|
Levels: levels,
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
out, err := r.ReadFieldMerge(tc.Addr, levels[len(levels)-1])
|
2015-01-03 16:32:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: err: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tc.Result, out) {
|
2016-06-30 17:48:52 +02:00
|
|
|
t.Fatalf("%s: bad: %#v", name, out)
|
2015-01-03 16:32:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|