Test case for changing backend hash during aborted state migration

Pulled and updated from
https://github.com/hashicorp/terraform/pull/26260
This commit is contained in:
Łukasz Sierant 2020-09-16 12:26:46 +02:00 committed by Chris Arcand
parent 12651c0a81
commit 19cce931a8
5 changed files with 84 additions and 0 deletions

View File

@ -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) { func TestInit_backendConfigKV(t *testing.T) {
// Create a temporary working directory that is empty // Create a temporary working directory that is empty
td := tempDir(t) td := tempDir(t)

View File

@ -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": []
}
]
}

View File

@ -0,0 +1 @@
path = "hello"

View File

@ -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": []
}
]
}

View File

@ -0,0 +1,5 @@
terraform {
backend "local" {
path = "local-state.tfstate"
}
}