From 1e1dcfd95a80f2863f473d3719f0d45b41995c6b Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 21 May 2019 18:55:53 -0400 Subject: [PATCH] always write the current state terraform_version Any time a new state is written, the current terraform version should be recorded. --- states/statefile/roundtrip_test.go | 9 ++++++++- states/statefile/write.go | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/states/statefile/roundtrip_test.go b/states/statefile/roundtrip_test.go index e9d6bdd0e..81158aa3f 100644 --- a/states/statefile/roundtrip_test.go +++ b/states/statefile/roundtrip_test.go @@ -11,6 +11,8 @@ import ( "testing" "github.com/go-test/deep" + + tfversion "github.com/hashicorp/terraform/version" ) func TestRoundtrip(t *testing.T) { @@ -20,6 +22,8 @@ func TestRoundtrip(t *testing.T) { t.Fatal(err) } + currentVersion := tfversion.Version + for _, info := range entries { const inSuffix = ".in.tfstate" const outSuffix = ".out.tfstate" @@ -56,7 +60,7 @@ func TestRoundtrip(t *testing.T) { } oSrcGot := buf.Bytes() - var oGot, oWant interface{} + var oGot, oWant map[string]interface{} err = json.Unmarshal(oSrcGot, &oGot) if err != nil { 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) } + // A newly written state should always reflect the current terraform version. + oWant["terraform_version"] = currentVersion + problems := deep.Equal(oGot, oWant) sort.Strings(problems) for _, problem := range problems { diff --git a/states/statefile/write.go b/states/statefile/write.go index cff3742d1..548ba8a8b 100644 --- a/states/statefile/write.go +++ b/states/statefile/write.go @@ -2,11 +2,16 @@ package statefile import ( "io" + + tfversion "github.com/hashicorp/terraform/version" ) // Write writes the given state to the given writer in the current state // serialization format. 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) return diags.Err() }