terraform: Guard against future version changes

This commit is contained in:
Armon Dadgar 2014-09-18 13:43:26 -07:00
parent ec4be66f63
commit 68614aeb79
2 changed files with 27 additions and 0 deletions

View File

@ -533,6 +533,12 @@ func ReadState(src io.Reader) (*State, error) {
return nil, fmt.Errorf("Decoding state file failed: %v", err)
}
// Check the version, this to ensure we don't read a future
// version that we don't understand
if state.Version > textStateVersion {
return nil, fmt.Errorf("State version %d not supported, please update.",
state.Version)
}
return state, nil
}

View File

@ -2,7 +2,9 @@ package terraform
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/hashicorp/terraform/config"
@ -193,6 +195,25 @@ func TestReadWriteState(t *testing.T) {
}
}
func TestReadStateNewVersion(t *testing.T) {
type out struct {
Version int
}
buf, err := json.Marshal(&out{textStateVersion + 1})
if err != nil {
t.Fatalf("err: %v", err)
}
s, err := ReadState(bytes.NewReader(buf))
if s != nil {
t.Fatalf("unexpected: %#v", s)
}
if !strings.Contains(err.Error(), "not supported") {
t.Fatalf("err: %v", err)
}
}
func TestUpgradeV1State(t *testing.T) {
old := &StateV1{
Outputs: map[string]string{