update TestState helper

In practice, States must all implement the full interface, so checking
for each method set only leaves gaps where tests could be skipped.
Change the helper to only accept a full state.State implementation.

Add some Lineage, Version, and TFVersion checks to TestState to avoid
regressions.

Compare the copy test against the immediate State returnedm rather than
our previous "current" state.

Check that the states round-trip and still marhsal identically via
MarshalEqual.
This commit is contained in:
James Bardin 2017-07-05 17:10:19 -04:00
parent 4d53eaa6df
commit fba5decae5
1 changed files with 96 additions and 95 deletions

View File

@ -10,125 +10,126 @@ import (
// TestState is a helper for testing state implementations. It is expected // TestState is a helper for testing state implementations. It is expected
// that the given implementation is pre-loaded with the TestStateInitial // that the given implementation is pre-loaded with the TestStateInitial
// state. // state.
func TestState(t *testing.T, s interface{}) { func TestState(t *testing.T, s State) {
reader, ok := s.(StateReader) if err := s.RefreshState(); err != nil {
if !ok { t.Fatalf("err: %s", err)
t.Fatalf("must at least be a StateReader")
} }
// If it implements refresh, refresh // Check that the initial state is correct.
if rs, ok := s.(StateRefresher); ok { // These do have different Lineages, but we will replace current below.
if err := rs.RefreshState(); err != nil { initial := TestStateInitial()
t.Fatalf("err: %s", err) if state := s.State(); !state.Equal(initial) {
} t.Fatalf("state does not match expected initial state:\n%#v\n\n%#v", state, initial)
}
// current will track our current state
current := TestStateInitial()
// Check that the initial state is correct
if state := reader.State(); !current.Equal(state) {
t.Fatalf("not initial:\n%#v\n\n%#v", state, current)
} }
// Now we've proven that the state we're starting with is an initial // Now we've proven that the state we're starting with is an initial
// state, we'll complete our work here with that state, since otherwise // state, we'll complete our work here with that state, since otherwise
// further writes would violate the invariant that we only try to write // further writes would violate the invariant that we only try to write
// states that share the same lineage as what was initially written. // states that share the same lineage as what was initially written.
current = reader.State() current := s.State()
// Write a new state and verify that we have it // Write a new state and verify that we have it
if ws, ok := s.(StateWriter); ok { current.AddModuleState(&terraform.ModuleState{
current.AddModuleState(&terraform.ModuleState{ Path: []string{"root"},
Path: []string{"root"}, Outputs: map[string]*terraform.OutputState{
Outputs: map[string]*terraform.OutputState{ "bar": &terraform.OutputState{
"bar": &terraform.OutputState{ Type: "string",
Type: "string", Sensitive: false,
Sensitive: false, Value: "baz",
Value: "baz",
},
}, },
}) },
})
if err := ws.WriteState(current); err != nil { if err := s.WriteState(current); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if actual := reader.State(); !actual.Equal(current) { if actual := s.State(); !actual.Equal(current) {
t.Fatalf("bad:\n%#v\n\n%#v", actual, current) t.Fatalf("bad:\n%#v\n\n%#v", actual, current)
}
} }
// Test persistence // Test persistence
if ps, ok := s.(StatePersister); ok { if err := s.PersistState(); err != nil {
if err := ps.PersistState(); err != nil { t.Fatalf("err: %s", err)
t.Fatalf("err: %s", err)
}
// Refresh if we got it
if rs, ok := s.(StateRefresher); ok {
if err := rs.RefreshState(); err != nil {
t.Fatalf("err: %s", err)
}
}
// Just set the serials the same... Then compare.
actual := reader.State()
if !actual.Equal(current) {
t.Fatalf("bad: %#v\n\n%#v", actual, current)
}
} }
// If we can write and persist then verify that the serial // Refresh if we got it
// is only incremented on change. if err := s.RefreshState(); err != nil {
writer, writeOk := s.(StateWriter) t.Fatalf("err: %s", err)
persister, persistOk := s.(StatePersister) }
if writeOk && persistOk {
// Same serial
serial := reader.State().Serial
if err := writer.WriteState(current); err != nil {
t.Fatalf("err: %s", err)
}
if err := persister.PersistState(); err != nil {
t.Fatalf("err: %s", err)
}
if reader.State().Serial != serial { if s.State().Lineage != current.Lineage {
t.Fatalf("serial changed after persisting with no changes: got %d, want %d", reader.State().Serial, serial) t.Fatalf("Lineage changed from %s to %s", s.State().Lineage, current.Lineage)
} }
// Change the serial // Just set the serials the same... Then compare.
current = current.DeepCopy() actual := s.State()
current.Modules = []*terraform.ModuleState{ if !actual.Equal(current) {
&terraform.ModuleState{ t.Fatalf("bad: %#v\n\n%#v", actual, current)
Path: []string{"root", "somewhere"}, }
Outputs: map[string]*terraform.OutputState{
"serialCheck": &terraform.OutputState{ // Same serial
Type: "string", serial := s.State().Serial
Sensitive: false, if err := s.WriteState(current); err != nil {
Value: "true", t.Fatalf("err: %s", err)
}, }
if err := s.PersistState(); err != nil {
t.Fatalf("err: %s", err)
}
if s.State().Serial != serial {
t.Fatalf("serial changed after persisting with no changes: got %d, want %d", s.State().Serial, serial)
}
// Change the serial
current = current.DeepCopy()
current.Modules = []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root", "somewhere"},
Outputs: map[string]*terraform.OutputState{
"serialCheck": &terraform.OutputState{
Type: "string",
Sensitive: false,
Value: "true",
}, },
}, },
} },
if err := writer.WriteState(current); err != nil { }
t.Fatalf("err: %s", err) if err := s.WriteState(current); err != nil {
} t.Fatalf("err: %s", err)
if err := persister.PersistState(); err != nil { }
t.Fatalf("err: %s", err) if err := s.PersistState(); err != nil {
} t.Fatalf("err: %s", err)
}
if reader.State().Serial <= serial { if s.State().Serial <= serial {
t.Fatalf("serial incorrect after persisting with changes: got %d, want > %d", reader.State().Serial, serial) t.Fatalf("serial incorrect after persisting with changes: got %d, want > %d", s.State().Serial, serial)
} }
// Check that State() returns a copy by modifying the copy and comparing if s.State().Version != current.Version {
// to the current state. t.Fatalf("Version changed from %d to %d", s.State().Version, current.Version)
stateCopy := reader.State() }
stateCopy.Serial++
if reflect.DeepEqual(stateCopy, current) { if s.State().TFVersion != current.TFVersion {
t.Fatal("State() should return a copy") t.Fatalf("TFVersion changed from %s to %s", s.State().TFVersion, current.TFVersion)
} }
// verify that Lineage doesn't change along with Serial, or during copying.
if s.State().Lineage != current.Lineage {
t.Fatalf("Lineage changed from %s to %s", s.State().Lineage, current.Lineage)
}
// Check that State() returns a copy by modifying the copy and comparing
// to the current state.
stateCopy := s.State()
stateCopy.Serial++
if reflect.DeepEqual(stateCopy, s.State()) {
t.Fatal("State() should return a copy")
}
// our current expected state should also marhsal identically to the persisted state
if current.MarshalEqual(s.State()) {
t.Fatalf("Persisted state altered unexpectedly. Expected: %#v\b Got: %#v", current, s.State())
} }
} }