From 088feb933f02c73780f1a44df28a8dc4c6290caf Mon Sep 17 00:00:00 2001 From: James Nugent Date: Thu, 7 Jul 2016 19:14:33 +0100 Subject: [PATCH] terraform: Add test case reproducing #7241 The reproduction of issue #7421 involves a list of maps being passed to a module, where one or more of the maps has a value which is computed (for example, from another resource). There is a failure at the point of use (via lookup interpolation) of the computed value of the form: ``` lookup: lookup failed to find 'elb' in: ${lookup(var.services[count.index], "elb")} ``` Where 'elb' is the key of the map. --- terraform/context_plan_test.go | 40 +++++++++++++++++++ terraform/terraform_test.go | 16 ++++++++ .../plan-computed-value-in-map/main.tf | 15 +++++++ .../plan-computed-value-in-map/mod/main.tf | 8 ++++ 4 files changed, 79 insertions(+) create mode 100644 terraform/test-fixtures/plan-computed-value-in-map/main.tf create mode 100644 terraform/test-fixtures/plan-computed-value-in-map/mod/main.tf diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 9ad2d1f19..09ed1bd82 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -2325,3 +2325,43 @@ func TestContext2Plan_moduleMapLiteral(t *testing.T) { t.Fatalf("err: %s", err) } } + +func TestContext2Plan_computedValueInMap(t *testing.T) { + m := testModule(t, "plan-computed-value-in-map") + p := testProvider("aws") + p.DiffFn = func(info *InstanceInfo, state *InstanceState, c *ResourceConfig) (*InstanceDiff, error) { + switch info.Type { + case "aws_computed_source": + return &InstanceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "computed_read_only": &ResourceAttrDiff{ + NewComputed: true, + }, + }, + }, nil + } + + return testDiffFn(info, state, c) + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + plan, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanComputedValueInMap) + if actual != expected { + t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) + } +} diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index c608d3d73..333e72767 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -1355,3 +1355,19 @@ aws_instance.foo: ID = bar ami = ami-abcd1234 ` + +const testTerraformPlanComputedValueInMap = ` +DIFF: + +CREATE: aws_computed_source.intermediates + computed_read_only: "" => "" + +module.test_mod: + CREATE: aws_instance.inner2 + looked_up: "" => "" + type: "" => "aws_instance" + +STATE: + + +` diff --git a/terraform/test-fixtures/plan-computed-value-in-map/main.tf b/terraform/test-fixtures/plan-computed-value-in-map/main.tf new file mode 100644 index 000000000..b820b1705 --- /dev/null +++ b/terraform/test-fixtures/plan-computed-value-in-map/main.tf @@ -0,0 +1,15 @@ +resource "aws_computed_source" "intermediates" {} + +module "test_mod" { + source = "./mod" + + services { + "exists" = "true" + "elb" = "${aws_computed_source.intermediates.computed_read_only}" + } + + services { + "otherexists" = " true" + "elb" = "${aws_computed_source.intermediates.computed_read_only}" + } +} diff --git a/terraform/test-fixtures/plan-computed-value-in-map/mod/main.tf b/terraform/test-fixtures/plan-computed-value-in-map/mod/main.tf new file mode 100644 index 000000000..82ee1e494 --- /dev/null +++ b/terraform/test-fixtures/plan-computed-value-in-map/mod/main.tf @@ -0,0 +1,8 @@ +variable "services" { + type = "list" +} + +resource "aws_instance" "inner2" { + looked_up = "${lookup(var.services[0], "elb")}" +} +