From 890e214c00ce70ea545457785119e515e010d30b Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 23 Nov 2015 10:32:46 +0200 Subject: [PATCH 1/2] Add failing test replicating #4013 --- command/refresh_test.go | 29 +++++++++++++++++++ .../test-fixtures/refresh-unset-var/main.tf | 7 +++++ 2 files changed, 36 insertions(+) create mode 100644 command/test-fixtures/refresh-unset-var/main.tf diff --git a/command/refresh_test.go b/command/refresh_test.go index 7f574e725b4f..df0142d28c61 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "bytes" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -413,6 +414,34 @@ func TestRefresh_varFileDefault(t *testing.T) { } } +func TestRefresh_varsUnset(t *testing.T) { + // Disable test mode so input would be asked + test = false + defer func() { test = true }() + + defaultInputReader = bytes.NewBufferString("bar\n") + + state := testState() + statePath := testStateFile(t, state) + + p := testProvider() + ui := new(cli.MockUi) + c := &RefreshCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + testFixturePath("refresh-unset-var"), + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } +} + func TestRefresh_backup(t *testing.T) { state := testState() statePath := testStateFile(t, state) diff --git a/command/test-fixtures/refresh-unset-var/main.tf b/command/test-fixtures/refresh-unset-var/main.tf new file mode 100644 index 000000000000..446cf70e2cb3 --- /dev/null +++ b/command/test-fixtures/refresh-unset-var/main.tf @@ -0,0 +1,7 @@ +variable "should_ask" {} + +provider "test" {} + +resource "test_instance" "foo" { + ami = "${var.should_ask}" +} From 50b5e7c1a58283728e789b4b858b0cd8ac550066 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 23 Nov 2015 10:33:40 +0200 Subject: [PATCH 2/2] Validate context after input of vars on refresh Fixes #4013. --- command/refresh.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/command/refresh.go b/command/refresh.go index 99190bf872ba..c64505b2ea75 100644 --- a/command/refresh.go +++ b/command/refresh.go @@ -87,14 +87,16 @@ func (c *RefreshCommand) Run(args []string) int { c.Ui.Error(err.Error()) return 1 } - if !validateContext(ctx, c.Ui) { - return 1 - } + if err := ctx.Input(c.InputMode()); err != nil { c.Ui.Error(fmt.Sprintf("Error configuring: %s", err)) return 1 } + if !validateContext(ctx, c.Ui) { + return 1 + } + newState, err := ctx.Refresh() if err != nil { c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))