helper/schema: refactor tests for fieldreader to be common

This commit is contained in:
Mitchell Hashimoto 2015-01-10 12:42:15 -08:00
parent 9ab128899a
commit 361d00347a
5 changed files with 413 additions and 639 deletions

View File

@ -160,17 +160,17 @@ func (r *ConfigFieldReader) readPrimitive(
func (r *ConfigFieldReader) readSet( func (r *ConfigFieldReader) readSet(
address []string, schema *Schema) (FieldReadResult, map[int]int, error) { address []string, schema *Schema) (FieldReadResult, map[int]int, error) {
indexMap := make(map[int]int) indexMap := make(map[int]int)
// Create the set that will be our result
set := &Set{F: schema.Set}
raw, err := readListField(&nestedConfigFieldReader{r}, address, schema) raw, err := readListField(&nestedConfigFieldReader{r}, address, schema)
if err != nil { if err != nil {
return FieldReadResult{}, indexMap, err return FieldReadResult{}, indexMap, err
} }
if !raw.Exists { if !raw.Exists {
return FieldReadResult{}, indexMap, nil return FieldReadResult{Value: set}, indexMap, nil
} }
// Create the set that will be our result
set := &Set{F: schema.Set}
// If the list is computed, the set is necessarilly computed // If the list is computed, the set is necessarilly computed
if raw.Computed { if raw.Computed {
return FieldReadResult{ return FieldReadResult{

View File

@ -1,7 +1,6 @@
package schema package schema
import ( import (
"reflect"
"testing" "testing"
"github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config"
@ -13,40 +12,9 @@ func TestConfigFieldReader_impl(t *testing.T) {
} }
func TestConfigFieldReader(t *testing.T) { func TestConfigFieldReader(t *testing.T) {
r := &ConfigFieldReader{ testFieldReader(t, func(s map[string]*Schema) FieldReader {
Schema: map[string]*Schema{ return &ConfigFieldReader{
"bool": &Schema{Type: TypeBool}, Schema: s,
"int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString},
"list": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeString},
},
"listInt": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
},
"map": &Schema{Type: TypeMap},
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
},
Config: testConfig(t, map[string]interface{}{ Config: testConfig(t, map[string]interface{}{
"bool": true, "bool": true,
@ -63,7 +31,6 @@ func TestConfigFieldReader(t *testing.T) {
}, },
"set": []interface{}{10, 50}, "set": []interface{}{10, 50},
"setDeep": []interface{}{ "setDeep": []interface{}{
map[string]interface{}{ map[string]interface{}{
"index": 10, "index": 10,
@ -76,137 +43,7 @@ func TestConfigFieldReader(t *testing.T) {
}, },
}), }),
} }
})
cases := map[string]struct {
Addr []string
Out interface{}
OutOk bool
OutComputed bool
OutErr bool
}{
"noexist": {
[]string{"boolNOPE"},
nil,
false,
false,
false,
},
"bool": {
[]string{"bool"},
true,
true,
false,
false,
},
"int": {
[]string{"int"},
42,
true,
false,
false,
},
"string": {
[]string{"string"},
"string",
true,
false,
false,
},
"list": {
[]string{"list"},
[]interface{}{
"foo",
"bar",
},
true,
false,
false,
},
"listInt": {
[]string{"listInt"},
[]interface{}{
21,
42,
},
true,
false,
false,
},
"map": {
[]string{"map"},
map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
true,
false,
false,
},
"mapelem": {
[]string{"map", "foo"},
"bar",
true,
false,
false,
},
"set": {
[]string{"set"},
[]interface{}{10, 50},
true,
false,
false,
},
"setDeep": {
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
true,
false,
false,
},
}
for name, tc := range cases {
if name != "list" {
continue
}
out, err := r.ReadField(tc.Addr)
if (err != nil) != tc.OutErr {
t.Fatalf("%s: err: %s", name, err)
}
if out.Computed != tc.OutComputed {
t.Fatalf("%s: err: %#v", name, out.Computed)
}
if s, ok := out.Value.(*Set); ok {
// If it is a set, convert to a list so its more easily checked.
out.Value = s.List()
}
if !reflect.DeepEqual(out.Value, tc.Out) {
t.Fatalf("%s: out: %#v", name, out.Value)
}
if out.Exists != tc.OutOk {
t.Fatalf("%s: outOk: %#v", name, out.Exists)
}
}
} }
func testConfig( func testConfig(

View File

@ -11,35 +11,19 @@ func TestDiffFieldReader_impl(t *testing.T) {
var _ FieldReader = new(DiffFieldReader) var _ FieldReader = new(DiffFieldReader)
} }
func TestDiffFieldReader(t *testing.T) { func TestDiffFieldReader_extra(t *testing.T) {
schema := map[string]*Schema{ schema := map[string]*Schema{
"bool": &Schema{Type: TypeBool},
"int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString},
"stringComputed": &Schema{Type: TypeString}, "stringComputed": &Schema{Type: TypeString},
"list": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeString},
},
"listInt": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
},
"listMap": &Schema{ "listMap": &Schema{
Type: TypeList, Type: TypeList,
Elem: &Schema{ Elem: &Schema{
Type: TypeMap, Type: TypeMap,
}, },
}, },
"map": &Schema{Type: TypeMap},
"mapRemove": &Schema{Type: TypeMap}, "mapRemove": &Schema{Type: TypeMap},
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
"setChange": &Schema{ "setChange": &Schema{
Type: TypeSet, Type: TypeSet,
Optional: true, Optional: true,
@ -61,29 +45,128 @@ func TestDiffFieldReader(t *testing.T) {
return m["index"].(int) return m["index"].(int)
}, },
}, },
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
"setEmpty": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
} }
r := &DiffFieldReader{ r := &DiffFieldReader{
Schema: schema, Schema: schema,
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"stringComputed": &terraform.ResourceAttrDiff{
Old: "foo",
New: "bar",
NewComputed: true,
},
"listMap.0.bar": &terraform.ResourceAttrDiff{
NewRemoved: true,
},
"mapRemove.bar": &terraform.ResourceAttrDiff{
NewRemoved: true,
},
"setChange.10.value": &terraform.ResourceAttrDiff{
Old: "50",
New: "80",
},
},
},
Source: &MapFieldReader{
Schema: schema,
Map: BasicMapReader(map[string]string{
"listMap.#": "2",
"listMap.0.foo": "bar",
"listMap.0.bar": "baz",
"listMap.1.baz": "baz",
"mapRemove.foo": "bar",
"mapRemove.bar": "bar",
"setChange.#": "1",
"setChange.10.index": "10",
"setChange.10.value": "50",
}),
},
}
cases := map[string]struct {
Addr []string
Result FieldReadResult
Err bool
}{
"stringComputed": {
[]string{"stringComputed"},
FieldReadResult{
Value: "",
Exists: true,
Computed: true,
},
false,
},
"listMapRemoval": {
[]string{"listMap"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"foo": "bar",
},
map[string]interface{}{
"baz": "baz",
},
},
Exists: true,
},
false,
},
"mapRemove": {
[]string{"mapRemove"},
FieldReadResult{
Value: map[string]interface{}{
"foo": "bar",
},
Exists: true,
Computed: false,
},
false,
},
"setChange": {
[]string{"setChange"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"index": 10,
"value": "80",
},
},
Exists: true,
},
false,
},
}
for name, tc := range cases {
out, err := r.ReadField(tc.Addr)
if (err != nil) != tc.Err {
t.Fatalf("%s: err: %s", name, err)
}
if s, ok := out.Value.(*Set); ok {
// If it is a set, convert to a list so its more easily checked.
out.Value = s.List()
}
if !reflect.DeepEqual(tc.Result, out) {
t.Fatalf("%s: bad: %#v", name, out)
}
}
}
func TestDiffFieldReader(t *testing.T) {
testFieldReader(t, func(s map[string]*Schema) FieldReader {
return &DiffFieldReader{
Schema: s,
Diff: &terraform.InstanceDiff{ Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{
"bool": &terraform.ResourceAttrDiff{ "bool": &terraform.ResourceAttrDiff{
@ -147,10 +230,6 @@ func TestDiffFieldReader(t *testing.T) {
New: "baz", New: "baz",
}, },
"mapRemove.bar": &terraform.ResourceAttrDiff{
NewRemoved: true,
},
"set.#": &terraform.ResourceAttrDiff{ "set.#": &terraform.ResourceAttrDiff{
Old: "0", Old: "0",
New: "2", New: "2",
@ -190,232 +269,18 @@ func TestDiffFieldReader(t *testing.T) {
Old: "", Old: "",
New: "bar", New: "bar",
}, },
"listMap.0.bar": &terraform.ResourceAttrDiff{
NewRemoved: true,
},
"setChange.10.value": &terraform.ResourceAttrDiff{
Old: "50",
New: "80",
},
}, },
}, },
Source: &MapFieldReader{ Source: &MapFieldReader{
Schema: schema, Schema: s,
Map: BasicMapReader(map[string]string{ Map: BasicMapReader(map[string]string{
"listMap.#": "2", "listMap.#": "2",
"listMap.0.foo": "bar", "listMap.0.foo": "bar",
"listMap.0.bar": "baz", "listMap.0.bar": "baz",
"listMap.1.baz": "baz", "listMap.1.baz": "baz",
"mapRemove.foo": "bar",
"mapRemove.bar": "bar",
"setChange.#": "1",
"setChange.10.index": "10",
"setChange.10.value": "50",
}), }),
}, },
} }
})
cases := map[string]struct {
Addr []string
Result FieldReadResult
Err bool
}{
"noexist": {
[]string{"boolNOPE"},
FieldReadResult{
Value: nil,
Exists: false,
Computed: false,
},
false,
},
"bool": {
[]string{"bool"},
FieldReadResult{
Value: true,
Exists: true,
Computed: false,
},
false,
},
"int": {
[]string{"int"},
FieldReadResult{
Value: 42,
Exists: true,
Computed: false,
},
false,
},
"string": {
[]string{"string"},
FieldReadResult{
Value: "string",
Exists: true,
Computed: false,
},
false,
},
"stringComputed": {
[]string{"stringComputed"},
FieldReadResult{
Value: "",
Exists: true,
Computed: true,
},
false,
},
"list": {
[]string{"list"},
FieldReadResult{
Value: []interface{}{
"foo",
"bar",
},
Exists: true,
Computed: false,
},
false,
},
"listInt": {
[]string{"listInt"},
FieldReadResult{
Value: []interface{}{
21,
42,
},
Exists: true,
Computed: false,
},
false,
},
"map": {
[]string{"map"},
FieldReadResult{
Value: map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
Exists: true,
Computed: false,
},
false,
},
"mapelem": {
[]string{"map", "foo"},
FieldReadResult{
Value: "bar",
Exists: true,
Computed: false,
},
false,
},
"mapRemove": {
[]string{"mapRemove"},
FieldReadResult{
Value: map[string]interface{}{
"foo": "bar",
},
Exists: true,
Computed: false,
},
false,
},
"set": {
[]string{"set"},
FieldReadResult{
Value: []interface{}{10, 50},
Exists: true,
Computed: false,
},
false,
},
"setDeep": {
[]string{"setDeep"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
Exists: true,
Computed: false,
},
false,
},
"listMapRemoval": {
[]string{"listMap"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"foo": "bar",
},
map[string]interface{}{
"baz": "baz",
},
},
Exists: true,
},
false,
},
"setChange": {
[]string{"setChange"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"index": 10,
"value": "80",
},
},
Exists: true,
},
false,
},
"setEmpty": {
[]string{"setEmpty"},
FieldReadResult{
Value: []interface{}{},
Exists: false,
},
false,
},
}
for name, tc := range cases {
out, err := r.ReadField(tc.Addr)
if (err != nil) != tc.Err {
t.Fatalf("%s: err: %s", name, err)
}
if s, ok := out.Value.(*Set); ok {
// If it is a set, convert to a list so its more easily checked.
out.Value = s.List()
}
if !reflect.DeepEqual(tc.Result, out) {
t.Fatalf("%s: bad: %#v", name, out)
}
}
} }

View File

@ -10,48 +10,9 @@ func TestMapFieldReader_impl(t *testing.T) {
} }
func TestMapFieldReader(t *testing.T) { func TestMapFieldReader(t *testing.T) {
r := &MapFieldReader{ testFieldReader(t, func(s map[string]*Schema) FieldReader {
Schema: map[string]*Schema{ return &MapFieldReader{
"bool": &Schema{Type: TypeBool}, Schema: s,
"int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString},
"list": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeString},
},
"listInt": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
},
"map": &Schema{Type: TypeMap},
"mapDel": &Schema{Type: TypeMap},
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
"setEmpty": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
Map: BasicMapReader(map[string]string{ Map: BasicMapReader(map[string]string{
"bool": "true", "bool": "true",
@ -69,8 +30,6 @@ func TestMapFieldReader(t *testing.T) {
"map.foo": "bar", "map.foo": "bar",
"map.bar": "baz", "map.bar": "baz",
"mapDel": "",
"set.#": "2", "set.#": "2",
"set.10": "10", "set.10": "10",
"set.50": "50", "set.50": "50",
@ -82,6 +41,19 @@ func TestMapFieldReader(t *testing.T) {
"setDeep.50.value": "bar", "setDeep.50.value": "bar",
}), }),
} }
})
}
func TestMapFieldReader_extra(t *testing.T) {
r := &MapFieldReader{
Schema: map[string]*Schema{
"mapDel": &Schema{Type: TypeMap},
},
Map: BasicMapReader(map[string]string{
"mapDel": "",
}),
}
cases := map[string]struct { cases := map[string]struct {
Addr []string Addr []string
@ -90,71 +62,6 @@ func TestMapFieldReader(t *testing.T) {
OutComputed bool OutComputed bool
OutErr bool OutErr bool
}{ }{
"noexist": {
[]string{"boolNOPE"},
nil,
false,
false,
false,
},
"bool": {
[]string{"bool"},
true,
true,
false,
false,
},
"int": {
[]string{"int"},
42,
true,
false,
false,
},
"string": {
[]string{"string"},
"string",
true,
false,
false,
},
"list": {
[]string{"list"},
[]interface{}{
"foo",
"bar",
},
true,
false,
false,
},
"listInt": {
[]string{"listInt"},
[]interface{}{
21,
42,
},
true,
false,
false,
},
"map": {
[]string{"map"},
map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
true,
false,
false,
},
"mapDel": { "mapDel": {
[]string{"mapDel"}, []string{"mapDel"},
map[string]interface{}{}, map[string]interface{}{},
@ -162,47 +69,6 @@ func TestMapFieldReader(t *testing.T) {
false, false,
false, false,
}, },
"mapelem": {
[]string{"map", "foo"},
"bar",
true,
false,
false,
},
"set": {
[]string{"set"},
[]interface{}{10, 50},
true,
false,
false,
},
"setEmpty": {
[]string{"setEmpty"},
[]interface{}{},
false,
false,
false,
},
"setDeep": {
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
true,
false,
false,
},
} }
for name, tc := range cases { for name, tc := range cases {

View File

@ -182,3 +182,209 @@ func TestAddrToSchema(t *testing.T) {
} }
} }
} }
// testFieldReader is a helper that should be used to verify that
// a FieldReader behaves properly in all the common cases.
func testFieldReader(t *testing.T, f func(map[string]*Schema) FieldReader) {
schema := map[string]*Schema{
// Primitives
"bool": &Schema{Type: TypeBool},
"int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString},
// Lists
"list": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeString},
},
"listInt": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
},
"listMap": &Schema{
Type: TypeList,
Elem: &Schema{
Type: TypeMap,
},
},
// Maps
"map": &Schema{Type: TypeMap},
// Sets
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
"setEmpty": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
}
cases := map[string]struct {
Addr []string
Result FieldReadResult
Err bool
}{
"noexist": {
[]string{"boolNOPE"},
FieldReadResult{
Value: nil,
Exists: false,
Computed: false,
},
false,
},
"bool": {
[]string{"bool"},
FieldReadResult{
Value: true,
Exists: true,
Computed: false,
},
false,
},
"int": {
[]string{"int"},
FieldReadResult{
Value: 42,
Exists: true,
Computed: false,
},
false,
},
"string": {
[]string{"string"},
FieldReadResult{
Value: "string",
Exists: true,
Computed: false,
},
false,
},
"list": {
[]string{"list"},
FieldReadResult{
Value: []interface{}{
"foo",
"bar",
},
Exists: true,
Computed: false,
},
false,
},
"listInt": {
[]string{"listInt"},
FieldReadResult{
Value: []interface{}{
21,
42,
},
Exists: true,
Computed: false,
},
false,
},
"map": {
[]string{"map"},
FieldReadResult{
Value: map[string]interface{}{
"foo": "bar",
"bar": "baz",
},
Exists: true,
Computed: false,
},
false,
},
"mapelem": {
[]string{"map", "foo"},
FieldReadResult{
Value: "bar",
Exists: true,
Computed: false,
},
false,
},
"set": {
[]string{"set"},
FieldReadResult{
Value: []interface{}{10, 50},
Exists: true,
Computed: false,
},
false,
},
"setDeep": {
[]string{"setDeep"},
FieldReadResult{
Value: []interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
Exists: true,
Computed: false,
},
false,
},
"setEmpty": {
[]string{"setEmpty"},
FieldReadResult{
Value: []interface{}{},
Exists: false,
},
false,
},
}
for name, tc := range cases {
r := f(schema)
out, err := r.ReadField(tc.Addr)
if (err != nil) != tc.Err {
t.Fatalf("%s: err: %s", name, err)
}
if s, ok := out.Value.(*Set); ok {
// If it is a set, convert to a list so its more easily checked.
out.Value = s.List()
}
if !reflect.DeepEqual(tc.Result, out) {
t.Fatalf("%s: bad: %#v", name, out)
}
}
}