From 6bf543cb07e1950dcfc5508bc2ea15a1c1b30b72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Jul 2014 21:03:56 -0700 Subject: [PATCH] command/plan: default state path --- command/command.go | 3 +++ command/command_test.go | 12 ++++++++- command/plan.go | 13 +++++++-- command/plan_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/command/command.go b/command/command.go index fd316f4b1..6ca862d83 100644 --- a/command/command.go +++ b/command/command.go @@ -9,6 +9,9 @@ import ( "github.com/mitchellh/cli" ) +// DefaultStateFilename is the default filename used for the state file. +const DefaultStateFilename = "terraform.tfstate" + func ContextArg( path string, statePath string, diff --git a/command/command_test.go b/command/command_test.go index e2165bafa..0f231ba90 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -10,7 +10,17 @@ import ( ) // This is the directory where our test fixtures are. -const fixtureDir = "./test-fixtures" +var fixtureDir = "./test-fixtures" + +func init() { + // Expand the fixture dir on init because we change the working + // directory in some tests. + var err error + fixtureDir, err = filepath.Abs(fixtureDir) + if err != nil { + panic(err) + } +} func testFixturePath(name string) string { return filepath.Join(fixtureDir, name) diff --git a/command/plan.go b/command/plan.go index e41466087..d1c24482f 100644 --- a/command/plan.go +++ b/command/plan.go @@ -27,7 +27,7 @@ func (c *PlanCommand) Run(args []string) int { cmdFlags.BoolVar(&destroy, "destroy", false, "destroy") cmdFlags.BoolVar(&refresh, "refresh", true, "refresh") cmdFlags.StringVar(&outPath, "out", "", "path") - cmdFlags.StringVar(&statePath, "state", "", "path") + cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { return 1 @@ -51,6 +51,15 @@ func (c *PlanCommand) Run(args []string) int { } } + // If the default state path doesn't exist, ignore it. + if statePath != "" { + if _, err := os.Stat(statePath); err != nil { + if os.IsNotExist(err) && statePath == DefaultStateFilename { + statePath = "" + } + } + } + // Load up the state var state *terraform.State if statePath != "" { @@ -70,7 +79,7 @@ func (c *PlanCommand) Run(args []string) int { b, err := config.LoadDir(path) if err != nil { - c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err)) + c.Ui.Error(fmt.Sprintf("Error loading config: %s", err)) return 1 } diff --git a/command/plan_test.go b/command/plan_test.go index 61c22b8a6..f31c82356 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -3,6 +3,7 @@ package command import ( "io/ioutil" "os" + "path/filepath" "reflect" "testing" @@ -207,3 +208,62 @@ func TestPlan_state(t *testing.T) { t.Fatalf("bad: %#v", p.DiffState) } } + +func TestPlan_stateDefault(t *testing.T) { + originalState := &terraform.State{ + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + ID: "bar", + Type: "test_instance", + }, + }, + } + + // Write the state file in a temporary directory with the + // default filename. + td, err := ioutil.TempDir("", "tf") + if err != nil { + t.Fatalf("err: %s", err) + } + statePath := filepath.Join(td, DefaultStateFilename) + + f, err := os.Create(statePath) + if err != nil { + t.Fatalf("err: %s", err) + } + err = terraform.WriteState(originalState, f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + // Change to that directory + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(filepath.Dir(statePath)); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + p := testProvider() + ui := new(cli.MockUi) + c := &PlanCommand{ + ContextOpts: testCtxConfig(p), + Ui: ui, + } + + args := []string{ + testFixturePath("plan"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + // Verify that the provider was called with the existing state + expectedState := originalState.Resources["test_instance.foo"] + if !reflect.DeepEqual(p.DiffState, expectedState) { + t.Fatalf("bad: %#v", p.DiffState) + } +}