From aacbe1d14be9d9422941192e840c84bec640b985 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 12 Nov 2018 18:30:01 -0800 Subject: [PATCH] command: Fix TestMetaBackend_configureNewWithState This test was initially failing because its fixture had a state which our new state models consider to be "empty", and thus it was not migrated. After fixing that (by adding an output to the fixture), this revealed a bug that the lineage was not being persisted through the migration. This is fixed by using the statemgr.Migrate method instead of writing via the normal Writer interface, which allows two cooperating state managers to properly transfer the lineage and serial along with the state snapshot. --- command/meta_backend_migrate.go | 6 ++++-- command/meta_backend_test.go | 11 +++++------ .../backend-new-migrate/terraform.tfstate | 7 ++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/command/meta_backend_migrate.go b/command/meta_backend_migrate.go index 91c24a9c6..eceb3032d 100644 --- a/command/meta_backend_migrate.go +++ b/command/meta_backend_migrate.go @@ -382,8 +382,10 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error { } } - // Confirmed! Write. - if err := stateTwo.WriteState(one); err != nil { + // Confirmed! We'll have the statemgr package handle the migration, which + // includes preserving any lineage/serial information where possible, if + // both managers support such metadata. + if err := statemgr.Migrate(stateTwo, stateOne); err != nil { return fmt.Errorf(strings.TrimSpace(errBackendStateCopy), opts.OneType, opts.TwoType, err) } diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index fc6ba51fd..f0c951094 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -326,24 +326,23 @@ func TestMetaBackend_configureNewWithState(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %s", err) } - if err := s.RefreshState(); err != nil { + state, err := statemgr.RefreshAndRead(s) + if err != nil { t.Fatalf("unexpected error: %s", err) } - state := s.State() if state == nil { t.Fatal("state is nil") } - if testStateMgrCurrentLineage(s) != "backend-new-migrate" { - t.Fatalf("bad: %#v", state) + if got, want := testStateMgrCurrentLineage(s), "backend-new-migrate"; got != want { + t.Fatalf("lineage changed during migration\nnow: %s\nwas: %s", got, want) } // Write some state state = states.NewState() mark := markStateForMatching(state, "changing") - s.WriteState(state) - if err := s.PersistState(); err != nil { + if err := statemgr.WriteAndPersist(s, state); err != nil { t.Fatalf("unexpected error: %s", err) } diff --git a/command/test-fixtures/backend-new-migrate/terraform.tfstate b/command/test-fixtures/backend-new-migrate/terraform.tfstate index b1b1415d0..f1d8b968b 100644 --- a/command/test-fixtures/backend-new-migrate/terraform.tfstate +++ b/command/test-fixtures/backend-new-migrate/terraform.tfstate @@ -8,7 +8,12 @@ "path": [ "root" ], - "outputs": {}, + "outputs": { + "foo": { + "type": "string", + "value": "bar" + } + }, "resources": {}, "depends_on": [] }