Merge pull request #12218 from hashicorp/b-state-equal

terraform: State.Equal needs to use reflect for rich types
This commit is contained in:
Mitchell Hashimoto 2017-02-23 14:09:40 -08:00 committed by GitHub
commit 89dbaad2be
2 changed files with 64 additions and 13 deletions

View File

@ -1633,13 +1633,11 @@ func (s *InstanceState) Equal(other *InstanceState) bool {
if len(s.Meta) != len(other.Meta) { if len(s.Meta) != len(other.Meta) {
return false return false
} }
for k, v := range s.Meta { if s.Meta != nil && other.Meta != nil {
otherV, ok := other.Meta[k] // We only do the deep check if both are non-nil. If one is nil
if !ok { // we treat it as equal since their lengths are both zero (check
return false // above).
} if !reflect.DeepEqual(s.Meta, other.Meta) {
if v != otherV {
return false return false
} }
} }

View File

@ -325,17 +325,20 @@ func TestStateDeepCopy(t *testing.T) {
func TestStateEqual(t *testing.T) { func TestStateEqual(t *testing.T) {
cases := []struct { cases := []struct {
Name string
Result bool Result bool
One, Two *State One, Two *State
}{ }{
// Nils // Nils
{ {
"one nil",
false, false,
nil, nil,
&State{Version: 2}, &State{Version: 2},
}, },
{ {
"both nil",
true, true,
nil, nil,
nil, nil,
@ -343,6 +346,7 @@ func TestStateEqual(t *testing.T) {
// Different versions // Different versions
{ {
"different state versions",
false, false,
&State{Version: 5}, &State{Version: 5},
&State{Version: 2}, &State{Version: 2},
@ -350,6 +354,7 @@ func TestStateEqual(t *testing.T) {
// Different modules // Different modules
{ {
"different module states",
false, false,
&State{ &State{
Modules: []*ModuleState{ Modules: []*ModuleState{
@ -362,6 +367,7 @@ func TestStateEqual(t *testing.T) {
}, },
{ {
"same module states",
true, true,
&State{ &State{
Modules: []*ModuleState{ Modules: []*ModuleState{
@ -381,6 +387,7 @@ func TestStateEqual(t *testing.T) {
// Meta differs // Meta differs
{ {
"differing meta values with primitives",
false, false,
&State{ &State{
Modules: []*ModuleState{ Modules: []*ModuleState{
@ -415,15 +422,61 @@ func TestStateEqual(t *testing.T) {
}, },
}, },
}, },
// Meta with complex types
{
"same meta with complex types",
true,
&State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"test_instance.foo": &ResourceState{
Primary: &InstanceState{
Meta: map[string]interface{}{
"timeouts": map[string]interface{}{
"create": 42,
"read": "27",
},
},
},
},
},
},
},
},
&State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"test_instance.foo": &ResourceState{
Primary: &InstanceState{
Meta: map[string]interface{}{
"timeouts": map[string]interface{}{
"create": 42,
"read": "27",
},
},
},
},
},
},
},
},
},
} }
for i, tc := range cases { for i, tc := range cases {
if tc.One.Equal(tc.Two) != tc.Result { t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, tc.One.String(), tc.Two.String()) if tc.One.Equal(tc.Two) != tc.Result {
} t.Fatalf("Bad: %d\n\n%s\n\n%s", i, tc.One.String(), tc.Two.String())
if tc.Two.Equal(tc.One) != tc.Result { }
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, tc.One.String(), tc.Two.String()) if tc.Two.Equal(tc.One) != tc.Result {
} t.Fatalf("Bad: %d\n\n%s\n\n%s", i, tc.One.String(), tc.Two.String())
}
})
} }
} }