diff --git a/command/init_test.go b/command/init_test.go index 7aadb0f0f..62bd484fb 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -1,6 +1,7 @@ package command import ( + "encoding/json" "fmt" "io/ioutil" "log" @@ -410,6 +411,51 @@ func TestInit_backendConfigKV(t *testing.T) { } } +func TestInit_backendConfigKVReInit(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + copy.CopyDir(testFixturePath("init-backend-config-kv"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + args := []string{"-backend-config", "path=test"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + ui = new(cli.MockUi) + c = &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + // a second init should require no changes, nor should it change the backend. + args = []string{"-input=false"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + // make sure the backend is configured how we expect + configState := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) + cfg := map[string]interface{}{} + if err := json.Unmarshal(configState.Backend.ConfigRaw, &cfg); err != nil { + t.Fatal(err) + } + if cfg["path"] != "test" { + t.Fatalf(`expected backend path="test", got path="%v"`, cfg["path"]) + } +} + func TestInit_targetSubdir(t *testing.T) { // Create a temporary working directory that is empty td := tempDir(t) diff --git a/command/meta_backend.go b/command/meta_backend.go index bfaa9b44e..c649c3336 100644 --- a/command/meta_backend.go +++ b/command/meta_backend.go @@ -823,6 +823,8 @@ func (m *Meta) backend_C_r_S_changed(c *configs.Backend, cHash int, sMgr *state. return nil, diags } + fmt.Println("HERE") + // Perform the migration err := m.backendMigrateState(&backendMigrateOpts{ OneType: s.Backend.Type, diff --git a/command/meta_backend_migrate.go b/command/meta_backend_migrate.go index 9227844a1..8d020f90a 100644 --- a/command/meta_backend_migrate.go +++ b/command/meta_backend_migrate.go @@ -98,6 +98,7 @@ func (m *Meta) backendMigrateState(opts *backendMigrateOpts) error { // Multi-state to multi-state. We merge the states together (migrating // each from the source to the destination one by one). case !oneSingle && !twoSingle: + fmt.Printf("STATES: %q\n", oneStates) // If the source only has one state and it is the default, // treat it as if it doesn't support multi-state. if len(oneStates) == 1 && oneStates[0] == backend.DefaultStateName {