state: deep copies are required

This commit is contained in:
Mitchell Hashimoto 2015-02-23 21:36:35 -08:00
parent f3af221866
commit cc8e6b6331
5 changed files with 13 additions and 7 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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.

View File

@ -18,7 +18,7 @@ type State struct {
// StateReader impl.
func (s *State) State() *terraform.State {
return s.state
return s.state.DeepCopy()
}
// StateWriter impl.

View File

@ -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")
}
}
}