mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: test that varaibles from the plan are interpolated in
This commit is contained in:
parent
92ffbf2880
commit
9e8f578c8b
@ -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 {
|
||||
|
@ -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"
|
||||
|
7
terraform/test-fixtures/apply-vars/main.tf
Normal file
7
terraform/test-fixtures/apply-vars/main.tf
Normal file
@ -0,0 +1,7 @@
|
||||
resource "aws_instance" "foo" {
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.foo}"
|
||||
}
|
Loading…
Reference in New Issue
Block a user