terraform/helper/schema/field_reader_multi_test.go

271 lines
5.4 KiB
Go
Raw Normal View History

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{
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{
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{
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,
}
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{
Schema: map[string]*Schema{
"availability_zone": &Schema{Type: TypeString},
},
2015-01-03 16:32:24 +01:00
Source: &MapFieldReader{
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{
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{
Schema: map[string]*Schema{
"availability_zone": &Schema{Type: TypeString},
},
2015-01-03 16:32:24 +01:00
Source: &MapFieldReader{
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{
Schema: map[string]*Schema{
"config_vars": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeMap},
},
},
2015-01-03 16:32:24 +01:00
Source: &MapFieldReader{
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{
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{
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,
}
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) {
t.Fatalf("%s: bad: %#v", name, out)
}
}
}