From 58f5257678b9e071fbab53e8dd113d313c525858 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 4 May 2017 16:46:48 -0700 Subject: [PATCH] core: context test for partially-unknown splat lists This is a context test for the behavior enabled by #14135, as some insurance to decrease the chance that we break it again. --- terraform/context_plan_test.go | 101 ++++++++++++++++++ .../plan-count-splat-reference/main.tf | 9 ++ 2 files changed, 110 insertions(+) create mode 100644 terraform/test-fixtures/plan-count-splat-reference/main.tf diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 3851e406bd9d..aeabcb0217b5 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -1871,6 +1871,107 @@ func TestContext2Plan_countIncreaseFromOneCorrupted(t *testing.T) { } } +// A common pattern in TF configs is to have a set of resources with the same +// count and to use count.index to create correspondences between them: +// +// foo_id = "${foo.bar.*.id[count.index]}" +// +// This test is for the situation where some instances already exist and the +// count is increased. In that case, we should see only the create diffs +// for the new instances and not any update diffs for the existing ones. +func TestContext2Plan_countIncreaseWithSplatReference(t *testing.T) { + m := testModule(t, "plan-count-splat-reference") + p := testProvider("aws") + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo.0": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "name": "foo 0", + }, + }, + }, + "aws_instance.foo.1": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "name": "foo 1", + }, + }, + }, + "aws_instance.bar.0": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo_name": "foo 0", + }, + }, + }, + "aws_instance.bar.1": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo_name": "foo 1", + }, + }, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + plan, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(` +DIFF: + +CREATE: aws_instance.bar.2 + foo_name: "" => "foo 2" + type: "" => "aws_instance" +CREATE: aws_instance.foo.2 + name: "" => "foo 2" + type: "" => "aws_instance" + +STATE: + +aws_instance.bar.0: + ID = bar + foo_name = foo 0 +aws_instance.bar.1: + ID = bar + foo_name = foo 1 +aws_instance.foo.0: + ID = bar + name = foo 0 +aws_instance.foo.1: + ID = bar + name = foo 1 +`) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContext2Plan_destroy(t *testing.T) { m := testModule(t, "plan-destroy") p := testProvider("aws") diff --git a/terraform/test-fixtures/plan-count-splat-reference/main.tf b/terraform/test-fixtures/plan-count-splat-reference/main.tf new file mode 100644 index 000000000000..76834e2555c8 --- /dev/null +++ b/terraform/test-fixtures/plan-count-splat-reference/main.tf @@ -0,0 +1,9 @@ +resource "aws_instance" "foo" { + name = "foo ${count.index}" + count = 3 +} + +resource "aws_instance" "bar" { + foo_name = "${aws_instance.foo.*.name[count.index]}" + count = 3 +}