From 0eb7cfd0d94050492e3f7dbaad430d470fd9ef18 Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Mon, 26 Aug 2019 13:27:33 -0400 Subject: [PATCH] Check for wholly known for forEach evaluation, add some tests --- terraform/context_apply_test.go | 35 +++++++++++++++++++ terraform/context_plan_test.go | 35 +++++++++++++++++++ terraform/eval_for_each.go | 2 +- terraform/node_data_refresh.go | 1 + terraform/terraform_test.go | 17 ++++++++- .../testdata/plan-for-each-expression/main.tf | 11 ++++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 terraform/testdata/plan-for-each-expression/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 20c6212c6..4d0c216e0 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -2562,6 +2562,41 @@ func TestContext2Apply_provisionerInterpCount(t *testing.T) { } } +func TestContext2Apply_foreachVariable(t *testing.T) { + m := testModule(t, "plan-for-each-expression") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: providers.ResolverFixed( + map[string]providers.Factory{ + "aws": testProviderFuncFixed(p), + }, + ), + Variables: InputValues{ + "foo": &InputValue{ + Value: cty.StringVal("hello"), + }, + }, + }) + + if _, diags := ctx.Plan(); diags.HasErrors() { + t.Fatalf("plan errors: %s", diags.Err()) + } + + state, diags := ctx.Apply() + if diags.HasErrors() { + t.Fatalf("diags: %s", diags.Err()) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyForEachVariableStr) + if actual != expected { + t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actual, expected) + } +} + func TestContext2Apply_moduleBasic(t *testing.T) { m := testModule(t, "apply-module") p := testProvider("aws") diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index d1e3c9ed2..4b16ceb73 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -3385,7 +3385,42 @@ func TestContext2Plan_forEach(t *testing.T) { t.Fatal(err) } } +} +func TestContext2Plan_forEachExpression(t *testing.T) { + m := testModule(t, "plan-for-each-expression") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Config: m, + ProviderResolver: providers.ResolverFixed( + map[string]providers.Factory{ + "aws": testProviderFuncFixed(p), + }, + ), + }) + + plan, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatalf("unexpected errors: %s", diags.Err()) + } + + schema := p.GetSchemaReturn.ResourceTypes["aws_instance"] + ty := schema.ImpliedType() + + if len(plan.Changes.Resources) != 3 { + t.Fatal("expected 3 changes, got", len(plan.Changes.Resources)) + } + + for _, res := range plan.Changes.Resources { + if res.Action != plans.Create { + t.Fatalf("expected resource creation, got %s", res.Action) + } + _, err := res.Decode(ty) + if err != nil { + t.Fatal(err) + } + } } func TestContext2Plan_destroy(t *testing.T) { diff --git a/terraform/eval_for_each.go b/terraform/eval_for_each.go index 333f65e26..86160e765 100644 --- a/terraform/eval_for_each.go +++ b/terraform/eval_for_each.go @@ -50,7 +50,7 @@ func evaluateResourceForEachExpressionKnown(expr hcl.Expression, ctx EvalContext Subject: expr.Range().Ptr(), }) return nil, true, diags - case !forEachVal.IsKnown(): + case !forEachVal.IsWhollyKnown(): return map[string]cty.Value{}, false, diags } diff --git a/terraform/node_data_refresh.go b/terraform/node_data_refresh.go index dd9286648..12f8bbce5 100644 --- a/terraform/node_data_refresh.go +++ b/terraform/node_data_refresh.go @@ -39,6 +39,7 @@ func (n *NodeRefreshableDataResource) DynamicExpand(ctx EvalContext) (*Graph, er } forEachMap, forEachKnown, forEachDiags := evaluateResourceForEachExpressionKnown(n.Config.ForEach, ctx) + diags = diags.Append(forEachDiags) if forEachDiags.HasErrors() { return nil, diags.Err() } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 9f0a63529..3a9d88c0b 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -538,7 +538,22 @@ aws_instance.foo.1: ID = foo provider = provider.aws ` - +const testTerraformApplyForEachVariableStr = ` +aws_instance.foo["b15c6d616d6143248c575900dff57325eb1de498"]: + ID = foo + provider = provider.aws + foo = foo + type = aws_instance +aws_instance.foo["c3de47d34b0a9f13918dd705c141d579dd6555fd"]: + ID = foo + provider = provider.aws + foo = foo + type = aws_instance +aws_instance.foo["e30a7edcc42a846684f2a4eea5f3cd261d33c46d"]: + ID = foo + provider = provider.aws + foo = foo + type = aws_instance` const testTerraformApplyMinimalStr = ` aws_instance.bar: ID = foo diff --git a/terraform/testdata/plan-for-each-expression/main.tf b/terraform/testdata/plan-for-each-expression/main.tf new file mode 100644 index 000000000..2b26dbd1f --- /dev/null +++ b/terraform/testdata/plan-for-each-expression/main.tf @@ -0,0 +1,11 @@ +# expressions with variable reference +variable "foo" { + type = string +} + +resource "aws_instance" "foo" { + for_each = toset( + [for i in range(0,3) : sha1("${i}${var.foo}")] + ) + foo = "foo" +}