Merge pull request #1306 from hashicorp/b-state-serial-inc

terraform: don't increment state if one is nil
This commit is contained in:
Mitchell Hashimoto 2015-03-25 15:55:58 -07:00
commit 1bbfb07b00
2 changed files with 57 additions and 0 deletions

View File

@ -214,7 +214,20 @@ func (s *State) DeepCopy() *State {
// IncrementSerialMaybe increments the serial number of this state
// if it different from the other state.
func (s *State) IncrementSerialMaybe(other *State) {
if s == nil {
return
}
if other == nil {
return
}
if s.Serial > other.Serial {
return
}
if !s.Equal(other) {
if other.Serial > s.Serial {
s.Serial = other.Serial
}
s.Serial++
}
}

View File

@ -178,6 +178,50 @@ func TestStateEqual(t *testing.T) {
}
}
func TestStateIncrementSerialMaybe(t *testing.T) {
cases := map[string]struct {
S1, S2 *State
Serial int64
}{
"S2 is nil": {
&State{},
nil,
0,
},
"S2 is identical": {
&State{},
&State{},
0,
},
"S2 is different": {
&State{},
&State{
Modules: []*ModuleState{
&ModuleState{Path: rootModulePath},
},
},
1,
},
"S1 serial is higher": {
&State{Serial: 5},
&State{
Serial: 3,
Modules: []*ModuleState{
&ModuleState{Path: rootModulePath},
},
},
5,
},
}
for name, tc := range cases {
tc.S1.IncrementSerialMaybe(tc.S2)
if tc.S1.Serial != tc.Serial {
t.Fatalf("Bad: %s\nGot: %d", name, tc.S1.Serial)
}
}
}
func TestResourceStateEqual(t *testing.T) {
cases := []struct {
Result bool