From 74386655a5b4bcc1123a00715637b302b119f824 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Jun 2015 18:25:08 -0700 Subject: [PATCH] terraform: test for reducing count and using splats --- terraform/context_test.go | 62 +++++++++++++++++++ .../test-fixtures/apply-multi-var/main.tf | 10 +++ 2 files changed, 72 insertions(+) create mode 100644 terraform/test-fixtures/apply-multi-var/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 720ed2b2d..6275fd8d7 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4454,6 +4454,68 @@ func TestContext2Apply_multiProvider(t *testing.T) { } } +func TestContext2Apply_multiVar(t *testing.T) { + m := testModule(t, "apply-multi-var") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + // First, apply with a count of 3 + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]string{ + "count": "3", + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := state.RootModule().Outputs["output"] + expected := "bar0,bar1,bar2" + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } + + // Apply again, reduce the count to 1 + { + ctx := testContext2(t, &ContextOpts{ + Module: m, + State: state, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]string{ + "count": "1", + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := state.RootModule().Outputs["output"] + expected := "bar0" + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } + } +} + func TestContext2Apply_nilDiff(t *testing.T) { m := testModule(t, "apply-good") p := testProvider("aws") diff --git a/terraform/test-fixtures/apply-multi-var/main.tf b/terraform/test-fixtures/apply-multi-var/main.tf new file mode 100644 index 000000000..22eb1988e --- /dev/null +++ b/terraform/test-fixtures/apply-multi-var/main.tf @@ -0,0 +1,10 @@ +variable "count" {} + +resource "aws_instance" "bar" { + foo = "bar${count.index}" + count = "${var.count}" +} + +output "output" { + value = "${join(",", aws_instance.bar.*.foo)}" +}