terraform: ensure file version is set and serial incremented

This commit is contained in:
Armon Dadgar 2014-09-18 13:38:36 -07:00
parent 3b7c987889
commit ec4be66f63
2 changed files with 25 additions and 0 deletions

View File

@ -538,6 +538,12 @@ func ReadState(src io.Reader) (*State, error) {
// WriteState writes a state somewhere in a binary format.
func WriteState(d *State, dst io.Writer) error {
// Ensure the version is set
d.Version = textStateVersion
// Always increment the serial number
d.Serial++
enc := json.NewEncoder(dst)
if err := enc.Encode(d); err != nil {
return fmt.Errorf("Failed to write state: %v", err)

View File

@ -125,6 +125,7 @@ func TestReadUpgradeState(t *testing.T) {
func TestReadWriteState(t *testing.T) {
state := &State{
Serial: 9,
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
@ -154,6 +155,20 @@ func TestReadWriteState(t *testing.T) {
t.Fatalf("err: %s", err)
}
// Verify that the version and serial are set
if state.Version != textStateVersion {
t.Fatalf("bad version number: %d", state.Version)
}
// Verify the serial number is incremented
if state.Serial != 10 {
t.Fatalf("bad serial: %d", state.Serial)
}
// Remove the changes or the checksum will fail
state.Version = 0
state.Serial = 9
// Checksum after the write
chksumAfter := checksumStruct(t, state)
if chksumAfter != chksum {
@ -165,6 +180,10 @@ func TestReadWriteState(t *testing.T) {
t.Fatalf("err: %s", err)
}
// Verify the changes came through
state.Version = textStateVersion
state.Serial = 10
// ReadState should not restore sensitive information!
mod := state.RootModule()
mod.Resources["foo"].Primary.Ephemeral = EphemeralState{}