From fe4799bd68eb2169cf861b316f6a1d67c2acad7e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 13 Oct 2016 11:16:03 -0400 Subject: [PATCH] Add failing test for nil IsntanceState in State A nil InstanceState within State/Modules/Resources/Deposed will panic during a deep copy. The panic needs to be fixed in copystructure, but the nil probably should have been normalized out before we got here too. --- terraform/state_test.go | 54 +++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/terraform/state_test.go b/terraform/state_test.go index 569f5ef941..cae8751f07 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -254,30 +254,64 @@ func TestStateModuleOrphans_deepNestedNilConfig(t *testing.T) { func TestStateDeepCopy(t *testing.T) { cases := []struct { - One, Two *State - F func(*State) interface{} + State *State }{ // Version { &State{Version: 5}, - &State{Version: 5}, - func(s *State) interface{} { return s.Version }, }, - // TFVersion { &State{TFVersion: "5"}, - &State{TFVersion: "5"}, - func(s *State) interface{} { return s.TFVersion }, + }, + // Modules + { + &State{ + Version: 6, + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Primary: &InstanceState{ + Meta: map[string]string{}, + }, + }, + }, + }, + }, + }, + }, + // Deposed + { + &State{ + Version: 6, + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "test_instance.foo": &ResourceState{ + Primary: &InstanceState{ + Meta: map[string]string{}, + }, + Deposed: []*InstanceState{ + {ID: "test"}, + nil, + }, + }, + }, + }, + }, + }, }, } for i, tc := range cases { t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) { - actual := tc.F(tc.One.DeepCopy()) - expected := tc.F(tc.Two) + actual := tc.State.DeepCopy() + expected := tc.State if !reflect.DeepEqual(actual, expected) { - t.Fatalf("Bad: %d\n\n%s\n\n%s", i, actual, expected) + t.Fatalf("Expected: %#v\nRecevied: %#v\n", expected, actual) } }) }