diff --git a/state/cache.go b/state/cache.go index 0f0f306a0..a20eb4a06 100644 --- a/state/cache.go +++ b/state/cache.go @@ -19,7 +19,7 @@ type CacheState struct { // StateReader impl. func (s *CacheState) State() *terraform.State { - return s.state + return s.state.DeepCopy() } // WriteState will write and persist the state to the cache. diff --git a/state/inmem.go b/state/inmem.go index 68c2ad0c3..ff8daab8f 100644 --- a/state/inmem.go +++ b/state/inmem.go @@ -10,7 +10,7 @@ type InmemState struct { } func (s *InmemState) State() *terraform.State { - return s.state + return s.state.DeepCopy() } func (s *InmemState) RefreshState() error { diff --git a/state/local.go b/state/local.go index 30c3093aa..02afb1ed7 100644 --- a/state/local.go +++ b/state/local.go @@ -28,7 +28,7 @@ func (s *LocalState) SetState(state *terraform.State) { // StateReader impl. func (s *LocalState) State() *terraform.State { - return s.state + return s.state.DeepCopy() } // WriteState for LocalState always persists the state as well. diff --git a/state/remote/state.go b/state/remote/state.go index c0abca40e..e679b5d73 100644 --- a/state/remote/state.go +++ b/state/remote/state.go @@ -18,7 +18,7 @@ type State struct { // StateReader impl. func (s *State) State() *terraform.State { - return s.state + return s.state.DeepCopy() } // StateWriter impl. diff --git a/state/testing.go b/state/testing.go index 7efd782d8..6a4a88ad0 100644 --- a/state/testing.go +++ b/state/testing.go @@ -28,7 +28,7 @@ func TestState(t *testing.T, s interface{}) { current := TestStateInitial() // Check that the initial state is correct - if state := reader.State(); !reflect.DeepEqual(state, current) { + if state := reader.State(); !current.Equal(state) { t.Fatalf("not initial: %#v\n\n%#v", state, current) } @@ -45,7 +45,7 @@ func TestState(t *testing.T, s interface{}) { t.Fatalf("err: %s", err) } - if actual := reader.State(); !reflect.DeepEqual(actual, current) { + if actual := reader.State(); !actual.Equal(current) { t.Fatalf("bad: %#v\n\n%#v", actual, current) } } @@ -65,7 +65,7 @@ func TestState(t *testing.T, s interface{}) { // Just set the serials the same... Then compare. actual := reader.State() - if !reflect.DeepEqual(actual, current) { + if !actual.Equal(current) { t.Fatalf("bad: %#v\n\n%#v", actual, current) } } @@ -107,6 +107,12 @@ func TestState(t *testing.T, s interface{}) { if reader.State().Serial <= serial { t.Fatalf("bad: expected %d, got %d", serial, reader.State().Serial) } + + // Check that State() returns a copy + reader.State().Serial++ + if reflect.DeepEqual(reader.State(), current) { + t.Fatal("State() should return a copy") + } } }