From 622690583cfe5104cc7e152e5eaf653ca637e673 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Feb 2015 10:49:31 -0800 Subject: [PATCH] command/init: remove dependency on remote package --- command/command_test.go | 6 +++- command/init.go | 39 +++++++++++----------- command/init_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 20 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index 996178f49c..cda23cf3fa 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -178,17 +178,20 @@ func testTempDir(t *testing.T) string { // testCwdDir is used to change the current working directory // into a test directory that should be remoted after func testCwd(t *testing.T) (string, string) { - tmp, err := ioutil.TempDir("", "remote") + tmp, err := ioutil.TempDir("", "tf") if err != nil { t.Fatalf("err: %v", err) } + cwd, err := os.Getwd() if err != nil { t.Fatalf("err: %v", err) } + if err := os.Chdir(tmp); err != nil { t.Fatalf("err: %v", err) } + return tmp, cwd } @@ -197,6 +200,7 @@ func testFixCwd(t *testing.T, tmp, cwd string) { if err := os.Chdir(cwd); err != nil { t.Fatalf("err: %v", err) } + if err := os.RemoveAll(tmp); err != nil { t.Fatalf("err: %v", err) } diff --git a/command/init.go b/command/init.go index 4e99a884b6..3651284df5 100644 --- a/command/init.go +++ b/command/init.go @@ -8,7 +8,6 @@ import ( "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config/module" - "github.com/hashicorp/terraform/remote" "github.com/hashicorp/terraform/terraform" ) @@ -102,27 +101,29 @@ func (c *InitCommand) Run(args []string) int { "path": remotePath, } - // Ensure remote state is not already enabled - haveLocal, err := remote.HaveLocalState() + state, err := c.State() if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to check for local state: %v", err)) + c.Ui.Error(fmt.Sprintf("Error checking for state: %s", err)) return 1 } - if haveLocal { - c.Ui.Error("Remote state is already enabled. Aborting.") - return 1 - } - - // Check if we have the non-managed state file - haveNonManaged, err := remote.ExistsFile(DefaultStateFilename) - if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to check for state file: %v", err)) - return 1 - } - if haveNonManaged { - c.Ui.Error(fmt.Sprintf("Existing state file '%s' found. Aborting.", - DefaultStateFilename)) - return 1 + if state != nil { + s := state.State() + if !s.Empty() { + c.Ui.Error(fmt.Sprintf( + "State file already exists and is not empty! Please remove this\n" + + "state file before initializing. Note that removing the state file\n" + + "may result in a loss of information since Terraform uses this\n" + + "to track your infrastructure.")) + return 1 + } + if s.IsRemote() { + c.Ui.Error(fmt.Sprintf( + "State file already exists with remote state enabled! Please remove this\n" + + "state file before initializing. Note that removing the state file\n" + + "may result in a loss of information since Terraform uses this\n" + + "to track your infrastructure.")) + return 1 + } } // Initialize a blank state file with remote enabled diff --git a/command/init_test.go b/command/init_test.go index a528da99a8..ac1d1113d9 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -181,3 +181,76 @@ func TestInit_remoteState(t *testing.T) { t.Fatalf("missing state") } } + +func TestInit_remoteStateWithLocal(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + + statePath := filepath.Join(tmp, DefaultStateFilename) + + // Write some state + f, err := os.Create(statePath) + if err != nil { + t.Fatalf("err: %s", err) + } + err = terraform.WriteState(testState(), f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-backend", "http", + "-address", "http://google.com", + testFixturePath("init"), + } + if code := c.Run(args); code == 0 { + t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) + } +} + +func TestInit_remoteStateWithRemote(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + + statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename) + if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil { + t.Fatalf("err: %s", err) + } + + // Write some state + f, err := os.Create(statePath) + if err != nil { + t.Fatalf("err: %s", err) + } + err = terraform.WriteState(testState(), f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + ui := new(cli.MockUi) + c := &InitCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-backend", "http", + "-address", "http://google.com", + testFixturePath("init"), + } + if code := c.Run(args); code == 0 { + t.Fatalf("should have failed: \n%s", ui.OutputWriter.String()) + } +}