From 9e8f578c8b4b6a67837be9407ab8628e4acf5e46 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Jun 2014 12:28:02 -0700 Subject: [PATCH] terraform: test that varaibles from the plan are interpolated in --- terraform/terraform.go | 16 +++++++--- terraform/terraform_test.go | 36 ++++++++++++++++++++++ terraform/test-fixtures/apply-vars/main.tf | 7 +++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 terraform/test-fixtures/apply-vars/main.tf diff --git a/terraform/terraform.go b/terraform/terraform.go index db0627fa1..2dc3c2337 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -167,7 +167,7 @@ func (t *Terraform) applyWalkFn( return vars, nil } - return t.genericWalkFn(p.State, p.Diff, cb) + return t.genericWalkFn(p.State, p.Diff, p.Vars, cb) } func (t *Terraform) planWalkFn( @@ -179,7 +179,12 @@ func (t *Terraform) planWalkFn( // Write our configuration out result.Config = t.config - result.Vars = t.variables + + // Copy the variables + result.Vars = make(map[string]string) + for k, v := range t.variables { + result.Vars[k] = v + } cb := func(r *Resource) (map[string]string, error) { // Refresh the state so we're working with the latest resource info @@ -216,19 +221,20 @@ func (t *Terraform) planWalkFn( return vars, nil } - return t.genericWalkFn(state, nil, cb) + return t.genericWalkFn(state, nil, t.variables, cb) } func (t *Terraform) genericWalkFn( state *State, diff *Diff, + invars map[string]string, cb genericWalkFunc) depgraph.WalkFunc { var l sync.Mutex // Initialize the variables for application vars := make(map[string]string) - for k, v := range t.variables { - vars[k] = v + for k, v := range invars { + vars[fmt.Sprintf("var.%s", k)] = v } return func(n *depgraph.Noun) error { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 0e250a500..6eaba3b9f 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -256,6 +256,31 @@ func TestTerraformApply_compute(t *testing.T) { } } +func TestTerraformApply_vars(t *testing.T) { + tf := testTerraform(t, "apply-vars") + tf.variables = map[string]string{"foo": "baz"} + + s := &State{} + p, err := tf.Plan(s) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Explicitly set the "foo" variable + p.Vars["foo"] = "bar" + + state, err := tf.Apply(p) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyVarsStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestTerraformPlan(t *testing.T) { tf := testTerraform(t, "plan-good") @@ -550,6 +575,17 @@ aws_instance.foo: id = computed_id ` +const testTerraformApplyVarsStr = ` +aws_instance.bar: + ID = foo + type = aws_instance + foo = bar +aws_instance.foo: + ID = foo + type = aws_instance + num = 2 +` + const testTerraformPlanStr = ` UPDATE: aws_instance.bar foo: "" => "2" diff --git a/terraform/test-fixtures/apply-vars/main.tf b/terraform/test-fixtures/apply-vars/main.tf new file mode 100644 index 000000000..f5c9c684f --- /dev/null +++ b/terraform/test-fixtures/apply-vars/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + num = "2" +} + +resource "aws_instance" "bar" { + foo = "${var.foo}" +}