diff --git a/command/init_test.go b/command/init_test.go index 96d853d36..a809a3814 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -17,7 +17,6 @@ import ( "github.com/mitchellh/cli" "github.com/zclconf/go-cty/cty" - version "github.com/hashicorp/go-version" "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs/configschema" @@ -25,7 +24,6 @@ import ( "github.com/hashicorp/terraform/internal/getproviders" "github.com/hashicorp/terraform/internal/providercache" "github.com/hashicorp/terraform/states" - "github.com/hashicorp/terraform/states/statefile" "github.com/hashicorp/terraform/states/statemgr" ) @@ -965,12 +963,27 @@ func TestInit_getProvider(t *testing.T) { } defer f.Close() - s := &statefile.File{ - Lineage: "", - State: states.NewState(), - TerraformVersion: version.Must(version.NewVersion("100.1.0")), + // Construct a mock state file from the far future + type FutureState struct { + Version uint `json:"version"` + Lineage string `json:"lineage"` + TerraformVersion string `json:"terraform_version"` + Outputs map[string]interface{} `json:"outputs"` + Resources []map[string]interface{} `json:"resources"` } - statefile.WriteForTest(s, f) + fs := &FutureState{ + Version: 999, + Lineage: "123-456-789", + TerraformVersion: "999.0.0", + Outputs: make(map[string]interface{}, 0), + Resources: make([]map[string]interface{}, 0), + } + src, err := json.MarshalIndent(fs, "", " ") + if err != nil { + t.Fatalf("failed to marshal future state: %s", err) + } + src = append(src, '\n') + _, err = f.Write(src) ui := new(cli.MockUi) m.Ui = ui @@ -983,7 +996,7 @@ func TestInit_getProvider(t *testing.T) { } errMsg := ui.ErrorWriter.String() - if !strings.Contains(errMsg, "which is newer than current") { + if !strings.Contains(errMsg, "Unsupported state file format") { t.Fatal("unexpected error:", errMsg) } }) diff --git a/states/statefile/read.go b/states/statefile/read.go index d691c0290..8abd3be14 100644 --- a/states/statefile/read.go +++ b/states/statefile/read.go @@ -62,15 +62,6 @@ func Read(r io.Reader) (*File, error) { panic("readState returned nil state with no errors") } - if state.TerraformVersion != nil && state.TerraformVersion.GreaterThan(tfversion.SemVer) { - return state, fmt.Errorf( - "state snapshot was created by Terraform v%s, which is newer than current v%s; upgrade to Terraform v%s or greater to work with this state", - state.TerraformVersion, - tfversion.SemVer, - state.TerraformVersion, - ) - } - return state, diags.Err() } diff --git a/states/statefile/testdata/roundtrip/v4-future.in.tfstate b/states/statefile/testdata/roundtrip/v4-future.in.tfstate new file mode 100644 index 000000000..71d759bc7 --- /dev/null +++ b/states/statefile/testdata/roundtrip/v4-future.in.tfstate @@ -0,0 +1,60 @@ +{ + "version": 4, + "serial": 0, + "lineage": "f2968801-fa14-41ab-a044-224f3a4adf04", + "terraform_version": "999.0.0", + "outputs": { + "numbers": { + "type": "string", + "value": "0,1" + } + }, + "resources": [ + { + "mode": "managed", + "type": "null_resource", + "name": "bar", + "provider": "provider[\"registry.terraform.io/-/null\"]", + "instances": [ + { + "schema_version": 0, + "attributes_flat": { + "id": "5388490630832483079", + "triggers.%": "1", + "triggers.whaaat": "0,1" + }, + "depends_on": [ + "null_resource.foo" + ] + } + ] + }, + { + "mode": "managed", + "type": "null_resource", + "name": "foo", + "provider": "provider[\"registry.terraform.io/-/null\"]", + "each": "list", + "instances": [ + { + "index_key": 0, + "schema_version": 0, + "attributes_flat": { + "id": "8212585058302700791", + "triggers.%": "1", + "triggers.what": "0" + } + }, + { + "index_key": 1, + "schema_version": 0, + "attributes_flat": { + "id": "1523897709610803586", + "triggers.%": "1", + "triggers.what": "0" + } + } + ] + } + ] +} diff --git a/states/statefile/testdata/roundtrip/v4-future.out.tfstate b/states/statefile/testdata/roundtrip/v4-future.out.tfstate new file mode 120000 index 000000000..b4037372e --- /dev/null +++ b/states/statefile/testdata/roundtrip/v4-future.out.tfstate @@ -0,0 +1 @@ +v4-future.in.tfstate \ No newline at end of file