From 0d4c7887c5c918abd1b6212fb0593676fbb89699 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Mar 2015 15:38:24 -0700 Subject: [PATCH 1/3] terraform: don't increment state if one is nil --- terraform/state.go | 9 +++++++++ terraform/state_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/terraform/state.go b/terraform/state.go index 42e9023baa..4ec772fd20 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -214,6 +214,15 @@ 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) { s.Serial++ } diff --git a/terraform/state_test.go b/terraform/state_test.go index 9dfbbbf04e..91f9170c71 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -178,6 +178,40 @@ 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, + }, + } + + 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 From f68f285f721d062e18bddade510277c6d9997c55 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Mar 2015 15:39:33 -0700 Subject: [PATCH 2/3] terraform: test case for higher S1 serial --- terraform/state_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/terraform/state_test.go b/terraform/state_test.go index 91f9170c71..e222b1b722 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -202,6 +202,16 @@ func TestStateIncrementSerialMaybe(t *testing.T) { }, 1, }, + "S1 serial is higher": { + &State{Serial: 5}, + &State{ + Serial: 3, + Modules: []*ModuleState{ + &ModuleState{Path: rootModulePath}, + }, + }, + 5, + }, } for name, tc := range cases { From 9c6e2cdc2167abba570a1a87ab492ef73a41e628 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Mar 2015 15:40:53 -0700 Subject: [PATCH 3/3] terraform: make sure our serial is always higher --- terraform/state.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/terraform/state.go b/terraform/state.go index 4ec772fd20..1a1a28bea8 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -224,6 +224,10 @@ func (s *State) IncrementSerialMaybe(other *State) { return } if !s.Equal(other) { + if other.Serial > s.Serial { + s.Serial = other.Serial + } + s.Serial++ } }