From 3b3c9e140ae59775091b3fb89b10d997fa94f068 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 3 Jul 2014 21:47:07 -0700 Subject: [PATCH] terraform: tests for increasing count from 1 to > 1 --- terraform/context_test.go | 38 ++++++++++++++++++- terraform/graph.go | 16 ++++++-- terraform/terraform_test.go | 21 ++++++++++ .../test-fixtures/plan-count-inc/main.tf | 8 ++++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 terraform/test-fixtures/plan-count-inc/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 25bb48aaf..c67faec6c 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -668,7 +668,7 @@ func TestContextPlan_count(t *testing.T) { } } -func TestContextPlan_countDecrease(t *testing.T) { +func TestContextPlan_countDecreaseToOne(t *testing.T) { c := testConfig(t, "plan-count-dec") p := testProvider("aws") p.DiffFn = testDiffFn @@ -712,6 +712,42 @@ func TestContextPlan_countDecrease(t *testing.T) { } } +func TestContextPlan_countIncreaseFromOne(t *testing.T) { + c := testConfig(t, "plan-count-inc") + p := testProvider("aws") + p.DiffFn = testDiffFn + s := &State{ + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + ID: "bar", + Type: "aws_instance", + Attributes: map[string]string{ + "foo": "foo", + "type": "aws_instance", + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanCountIncreaseStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextPlan_destroy(t *testing.T) { c := testConfig(t, "plan-destroy") p := testProvider("aws") diff --git a/terraform/graph.go b/terraform/graph.go index fd7a40cff..ff97e5fbf 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -172,10 +172,18 @@ func graphAddConfigResources( if s != nil { state = s.Resources[name] - // If the count is one, check the state for ".0" appended, which - // might exist if we go from count > 1 to count == 1. - if state == nil && r.Count == 1 { - state = s.Resources[r.Id()+".0"] + if state == nil { + if r.Count == 1 { + // If the count is one, check the state for ".0" + // appended, which might exist if we go from + // count > 1 to count == 1. + state = s.Resources[r.Id()+".0"] + } else if i == 0 { + // If count is greater than one, check for state + // with just the ID, which might exist if we go + // from count == 1 to count > 1 + state = s.Resources[r.Id()] + } } } if state == nil { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 84c360e16..dfb0f0e37 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -203,6 +203,27 @@ aws_instance.foo.2: ID = bar ` +const testTerraformPlanCountIncreaseStr = ` +DIFF: + +UPDATE: aws_instance.bar + foo: "" => "bar" + type: "" => "aws_instance" +UPDATE: aws_instance.foo.1 + foo: "" => "foo" + type: "" => "aws_instance" +UPDATE: aws_instance.foo.2 + foo: "" => "foo" + type: "" => "aws_instance" + +STATE: + +aws_instance.foo: + ID = bar + foo = foo + type = aws_instance +` + const testTerraformPlanDestroyStr = ` DIFF: diff --git a/terraform/test-fixtures/plan-count-inc/main.tf b/terraform/test-fixtures/plan-count-inc/main.tf new file mode 100644 index 000000000..d5a3d8434 --- /dev/null +++ b/terraform/test-fixtures/plan-count-inc/main.tf @@ -0,0 +1,8 @@ +resource "aws_instance" "foo" { + foo = "foo" + count = 3 +} + +resource "aws_instance" "bar" { + foo = "bar" +}