From d3d45ca064b535911271bfe6fc8cc9282dea3781 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Jul 2014 21:12:12 -0700 Subject: [PATCH] command: fix some issues with refresh, tests passing --- command/plan_test.go | 8 ++++++-- command/refresh.go | 1 + command/refresh_test.go | 44 +++++++++++++++-------------------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/command/plan_test.go b/command/plan_test.go index 948cd73d9..84963d318 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -40,6 +40,10 @@ func TestPlan_destroy(t *testing.T) { t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } + if !p.RefreshCalled { + t.Fatal("refresh should be called") + } + plan := testReadPlan(t, outPath) for _, r := range plan.Diff.Resources { if !r.Destroy { @@ -63,8 +67,8 @@ func TestPlan_noState(t *testing.T) { } // Verify that refresh was called - if !p.RefreshCalled { - t.Fatal("refresh should be called") + if p.RefreshCalled { + t.Fatal("refresh should not be called") } // Verify that the provider was called with the existing state diff --git a/command/refresh.go b/command/refresh.go index f1447b781..a1229c9b7 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -67,6 +67,7 @@ func (c *RefreshCommand) Run(args []string) int { } c.ContextOpts.Config = b + c.ContextOpts.State = state c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui}) ctx := terraform.NewContext(c.ContextOpts) if !validateContext(ctx, c.Ui) { diff --git a/command/refresh_test.go b/command/refresh_test.go index 9f65e92c1..f6332c5ad 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -11,21 +11,15 @@ import ( ) func TestRefresh(t *testing.T) { - // Write out some prior state - tf, err := ioutil.TempFile("", "tf") - if err != nil { - t.Fatalf("err: %s", err) - } - statePath := tf.Name() - defer os.Remove(tf.Name()) - - state := &terraform.State{} - - err = terraform.WriteState(state, tf) - tf.Close() - if err != nil { - t.Fatalf("err: %s", err) + state := &terraform.State{ + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + ID: "bar", + Type: "test_instance", + }, + }, } + statePath := testStateFile(t, state) p := testProvider() ui := new(cli.MockUi) @@ -68,13 +62,15 @@ func TestRefresh(t *testing.T) { } func TestRefresh_outPath(t *testing.T) { - // Write out some prior state - tf, err := ioutil.TempFile("", "tf") - if err != nil { - t.Fatalf("err: %s", err) + state := &terraform.State{ + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + ID: "bar", + Type: "test_instance", + }, + }, } - statePath := tf.Name() - defer os.Remove(tf.Name()) + statePath := testStateFile(t, state) // Output path outf, err := ioutil.TempFile("", "tf") @@ -85,14 +81,6 @@ func TestRefresh_outPath(t *testing.T) { outf.Close() os.Remove(outPath) - state := &terraform.State{} - - err = terraform.WriteState(state, tf) - tf.Close() - if err != nil { - t.Fatalf("err: %s", err) - } - p := testProvider() ui := new(cli.MockUi) c := &RefreshCommand{