From 7d2da9865ecc648620c96b2e749f3563fe272b7b Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 18 Dec 2017 10:37:15 -0500 Subject: [PATCH] inputFalse test should attempt migration and check error Make sure the init inputFalse test actually errors from missing input, since skipping input will still fail later during provider initialization. We need to make sure there are two different states that aren't a noop for migration, and reset the command struct for each run. Also verify that we don't go into an infinite loop if there is no input. --- command/init_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/command/init_test.go b/command/init_test.go index 05ad340938..b47936f057 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -13,6 +13,8 @@ import ( "github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/plugin/discovery" + "github.com/hashicorp/terraform/state" + "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -530,10 +532,48 @@ func TestInit_inputFalse(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter) } + // write different states for foo and bar + s := terraform.NewState() + s.Lineage = "foo" + if err := (&state.LocalState{Path: "foo"}).WriteState(s); err != nil { + t.Fatal(err) + } + s.Lineage = "bar" + if err := (&state.LocalState{Path: "bar"}).WriteState(s); err != nil { + t.Fatal(err) + } + + ui = new(cli.MockUi) + c = &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + args = []string{"-input=false", "-backend-config=path=bar"} if code := c.Run(args); code == 0 { t.Fatal("init should have failed", ui.OutputWriter) } + + errMsg := ui.ErrorWriter.String() + if !strings.Contains(errMsg, "input disabled") { + t.Fatal("expected input disabled error, got", errMsg) + } + + ui = new(cli.MockUi) + c = &InitCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + // A missing input=false should abort rather than loop infinitely + args = []string{"-backend-config=path=bar"} + if code := c.Run(args); code == 0 { + t.Fatal("init should have failed", ui.OutputWriter) + } } func TestInit_getProvider(t *testing.T) {