always write the current state terraform_version

Any time a new state is written, the current terraform version should be
recorded.
This commit is contained in:
James Bardin 2019-05-21 18:55:53 -04:00
parent f199030739
commit 1e1dcfd95a
2 changed files with 13 additions and 1 deletions

View File

@ -11,6 +11,8 @@ import (
"testing" "testing"
"github.com/go-test/deep" "github.com/go-test/deep"
tfversion "github.com/hashicorp/terraform/version"
) )
func TestRoundtrip(t *testing.T) { func TestRoundtrip(t *testing.T) {
@ -20,6 +22,8 @@ func TestRoundtrip(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
currentVersion := tfversion.Version
for _, info := range entries { for _, info := range entries {
const inSuffix = ".in.tfstate" const inSuffix = ".in.tfstate"
const outSuffix = ".out.tfstate" const outSuffix = ".out.tfstate"
@ -56,7 +60,7 @@ func TestRoundtrip(t *testing.T) {
} }
oSrcGot := buf.Bytes() oSrcGot := buf.Bytes()
var oGot, oWant interface{} var oGot, oWant map[string]interface{}
err = json.Unmarshal(oSrcGot, &oGot) err = json.Unmarshal(oSrcGot, &oGot)
if err != nil { if err != nil {
t.Fatalf("result isn't JSON: %s", err) t.Fatalf("result isn't JSON: %s", err)
@ -66,6 +70,9 @@ func TestRoundtrip(t *testing.T) {
t.Fatalf("wanted result isn't JSON: %s", err) t.Fatalf("wanted result isn't JSON: %s", err)
} }
// A newly written state should always reflect the current terraform version.
oWant["terraform_version"] = currentVersion
problems := deep.Equal(oGot, oWant) problems := deep.Equal(oGot, oWant)
sort.Strings(problems) sort.Strings(problems)
for _, problem := range problems { for _, problem := range problems {

View File

@ -2,11 +2,16 @@ package statefile
import ( import (
"io" "io"
tfversion "github.com/hashicorp/terraform/version"
) )
// Write writes the given state to the given writer in the current state // Write writes the given state to the given writer in the current state
// serialization format. // serialization format.
func Write(s *File, w io.Writer) error { func Write(s *File, w io.Writer) error {
// Always record the current terraform version in the state.
s.TerraformVersion = tfversion.SemVer
diags := writeStateV4(s, w) diags := writeStateV4(s, w)
return diags.Err() return diags.Err()
} }