terraform: ensure file version is set and serial incremented
This commit is contained in:
parent
3b7c987889
commit
ec4be66f63
|
@ -538,6 +538,12 @@ func ReadState(src io.Reader) (*State, error) {
|
||||||
|
|
||||||
// WriteState writes a state somewhere in a binary format.
|
// WriteState writes a state somewhere in a binary format.
|
||||||
func WriteState(d *State, dst io.Writer) error {
|
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)
|
enc := json.NewEncoder(dst)
|
||||||
if err := enc.Encode(d); err != nil {
|
if err := enc.Encode(d); err != nil {
|
||||||
return fmt.Errorf("Failed to write state: %v", err)
|
return fmt.Errorf("Failed to write state: %v", err)
|
||||||
|
|
|
@ -125,6 +125,7 @@ func TestReadUpgradeState(t *testing.T) {
|
||||||
|
|
||||||
func TestReadWriteState(t *testing.T) {
|
func TestReadWriteState(t *testing.T) {
|
||||||
state := &State{
|
state := &State{
|
||||||
|
Serial: 9,
|
||||||
Modules: []*ModuleState{
|
Modules: []*ModuleState{
|
||||||
&ModuleState{
|
&ModuleState{
|
||||||
Path: rootModulePath,
|
Path: rootModulePath,
|
||||||
|
@ -154,6 +155,20 @@ func TestReadWriteState(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
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
|
// Checksum after the write
|
||||||
chksumAfter := checksumStruct(t, state)
|
chksumAfter := checksumStruct(t, state)
|
||||||
if chksumAfter != chksum {
|
if chksumAfter != chksum {
|
||||||
|
@ -165,6 +180,10 @@ func TestReadWriteState(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify the changes came through
|
||||||
|
state.Version = textStateVersion
|
||||||
|
state.Serial = 10
|
||||||
|
|
||||||
// ReadState should not restore sensitive information!
|
// ReadState should not restore sensitive information!
|
||||||
mod := state.RootModule()
|
mod := state.RootModule()
|
||||||
mod.Resources["foo"].Primary.Ephemeral = EphemeralState{}
|
mod.Resources["foo"].Primary.Ephemeral = EphemeralState{}
|
||||||
|
|
Loading…
Reference in New Issue