Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Validate context after input of vars on refresh #4017

Merged
merged 2 commits into from Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions command/refresh.go
Expand Up @@ -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))
Expand Down
29 changes: 29 additions & 0 deletions command/refresh_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"bytes"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions 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}"
}