terraform: test that varaibles from the plan are interpolated in

This commit is contained in:
Mitchell Hashimoto 2014-06-23 12:28:02 -07:00
parent 92ffbf2880
commit 9e8f578c8b
3 changed files with 54 additions and 5 deletions

View File

@ -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 {

View File

@ -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"

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
num = "2"
}
resource "aws_instance" "bar" {
foo = "${var.foo}"
}