From 19cce931a8067c714fbb5a096c2eec79a070ecd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sierant?= Date: Wed, 16 Sep 2020 12:26:46 +0200 Subject: [PATCH] Test case for changing backend hash during aborted state migration Pulled and updated from https://github.com/hashicorp/terraform/pull/26260 --- internal/command/init_test.go | 35 +++++++++++++++++++ .../.terraform/terraform.tfstate | 22 ++++++++++++ .../input.config | 1 + .../local-state.tfstate | 21 +++++++++++ .../main.tf | 5 +++ 5 files changed, 84 insertions(+) create mode 100644 internal/command/testdata/init-backend-config-file-change-migrate-existing/.terraform/terraform.tfstate create mode 100644 internal/command/testdata/init-backend-config-file-change-migrate-existing/input.config create mode 100644 internal/command/testdata/init-backend-config-file-change-migrate-existing/local-state.tfstate create mode 100644 internal/command/testdata/init-backend-config-file-change-migrate-existing/main.tf diff --git a/internal/command/init_test.go b/internal/command/init_test.go index 56bc1f911..8f5008765 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -616,6 +616,41 @@ func TestInit_backendMigrateWhileLocked(t *testing.T) { } } +func TestInit_backendConfigFileChangeWithExistingState(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + testCopyDir(t, testFixturePath("init-backend-config-file-change-migrate-existing"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + oldState := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) + + // we deliberately do not provide the answer for backend-migrate-copy-to-empty to trigger error + args := []string{"-migrate-state", "-backend-config", "input.config", "-input=true"} + if code := c.Run(args); code == 0 { + t.Fatal("expected error") + } + + // Read our backend config and verify new settings are not saved + state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"local-state.tfstate"}`; got != want { + t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) + } + + // without changing config, hash should not change + if oldState.Backend.Hash != state.Backend.Hash { + t.Errorf("backend hash should not have changed\ngot: %d\nwant: %d", state.Backend.Hash, oldState.Backend.Hash) + } +} + func TestInit_backendConfigKV(t *testing.T) { // Create a temporary working directory that is empty td := tempDir(t) diff --git a/internal/command/testdata/init-backend-config-file-change-migrate-existing/.terraform/terraform.tfstate b/internal/command/testdata/init-backend-config-file-change-migrate-existing/.terraform/terraform.tfstate new file mode 100644 index 000000000..073bd7a82 --- /dev/null +++ b/internal/command/testdata/init-backend-config-file-change-migrate-existing/.terraform/terraform.tfstate @@ -0,0 +1,22 @@ +{ + "version": 3, + "serial": 0, + "lineage": "666f9301-7e65-4b19-ae23-71184bb19b03", + "backend": { + "type": "local", + "config": { + "path": "local-state.tfstate" + }, + "hash": 9073424445967744180 + }, + "modules": [ + { + "path": [ + "root" + ], + "outputs": {}, + "resources": {}, + "depends_on": [] + } + ] +} diff --git a/internal/command/testdata/init-backend-config-file-change-migrate-existing/input.config b/internal/command/testdata/init-backend-config-file-change-migrate-existing/input.config new file mode 100644 index 000000000..6cd14f4a3 --- /dev/null +++ b/internal/command/testdata/init-backend-config-file-change-migrate-existing/input.config @@ -0,0 +1 @@ +path = "hello" diff --git a/internal/command/testdata/init-backend-config-file-change-migrate-existing/local-state.tfstate b/internal/command/testdata/init-backend-config-file-change-migrate-existing/local-state.tfstate new file mode 100644 index 000000000..ce8d954f4 --- /dev/null +++ b/internal/command/testdata/init-backend-config-file-change-migrate-existing/local-state.tfstate @@ -0,0 +1,21 @@ +{ + "version": 3, + "terraform_version": "0.8.2", + "serial": 8, + "lineage": "local", + "modules": [ + { + "path": [ + "root" + ], + "outputs": { + "foo": { + "type": "string", + "value": "bar" + } + }, + "resources": {}, + "depends_on": [] + } + ] +} diff --git a/internal/command/testdata/init-backend-config-file-change-migrate-existing/main.tf b/internal/command/testdata/init-backend-config-file-change-migrate-existing/main.tf new file mode 100644 index 000000000..ca1bd3921 --- /dev/null +++ b/internal/command/testdata/init-backend-config-file-change-migrate-existing/main.tf @@ -0,0 +1,5 @@ +terraform { + backend "local" { + path = "local-state.tfstate" + } +}