From 36ccb3408d55256dbcd8adf27e95ddfbc1a28e8d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 27 Jun 2014 11:12:45 -0700 Subject: [PATCH] command/refresh: test the -out flag --- command/refresh_test.go | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/command/refresh_test.go b/command/refresh_test.go index 33cff13c8..192f42137 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -66,3 +66,81 @@ func TestRefresh(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +func TestRefresh_outPath(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()) + + // Output path + outf, err := ioutil.TempFile("", "tf") + if err != nil { + t.Fatalf("err: %s", err) + } + outPath := outf.Name() + 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{ + TFConfig: testTFConfig(p), + Ui: ui, + } + + p.RefreshFn = nil + p.RefreshReturn = &terraform.ResourceState{ID: "yes"} + + args := []string{ + "-out", outPath, + statePath, + testFixturePath("refresh"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + f, err := os.Open(statePath) + if err != nil { + t.Fatalf("err: %s", err) + } + + newState, err := terraform.ReadState(f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(newState, state) { + t.Fatalf("bad: %#v", newState) + } + + f, err = os.Open(outPath) + if err != nil { + t.Fatalf("err: %s", err) + } + + newState, err = terraform.ReadState(f) + f.Close() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := newState.Resources["test_instance.foo"] + expected := p.RefreshReturn + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +}