terraform: Guard against future version changes
This commit is contained in:
parent
ec4be66f63
commit
68614aeb79
|
@ -533,6 +533,12 @@ func ReadState(src io.Reader) (*State, error) {
|
||||||
return nil, fmt.Errorf("Decoding state file failed: %v", err)
|
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
|
return state, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,9 @@ package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/config"
|
"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) {
|
func TestUpgradeV1State(t *testing.T) {
|
||||||
old := &StateV1{
|
old := &StateV1{
|
||||||
Outputs: map[string]string{
|
Outputs: map[string]string{
|
||||||
|
|
Loading…
Reference in New Issue