From a3639b6156b3258b869f85ab158fb7348cb84360 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 26 Jul 2014 14:55:42 -0700 Subject: [PATCH] terraform: further tests around count edge cases, fix 1 => N case /cc @pearkes GH-35 --- terraform/context_test.go | 38 ++++++++++++++++++++++++++++++++++++- terraform/state.go | 7 +++---- terraform/terraform_test.go | 21 ++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/terraform/context_test.go b/terraform/context_test.go index 9e1981158..5819aae76 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1388,7 +1388,7 @@ func TestContextPlan_countDecreaseToOne(t *testing.T) { } } -func TestContextPlan_countIncreaseFromOne(t *testing.T) { +func TestContextPlan_countIncreaseFromNotSet(t *testing.T) { c := testConfig(t, "plan-count-inc") p := testProvider("aws") p.DiffFn = testDiffFn @@ -1424,6 +1424,42 @@ func TestContextPlan_countIncreaseFromOne(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.0": &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(testTerraformPlanCountIncreaseFromOneStr) + 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/state.go b/terraform/state.go index c9812abdb..1f5e90d02 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -72,10 +72,9 @@ func (s *State) Orphans(c *config.Config) []string { for _, r := range c.Resources { delete(keys, r.Id()) - // If there is only one of this instance, then we alias that - // to the ".0" version as well so that it can count - if r.Count == 1 { - delete(keys, r.Id()+".0") + // Mark all the counts as not orphans. + for i := 0; i < r.Count; i++ { + delete(keys, fmt.Sprintf("%s.%d", r.Id(), i)) } } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index ecd0b232d..384006ab0 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -364,6 +364,27 @@ aws_instance.foo: type = aws_instance ` +const testTerraformPlanCountIncreaseFromOneStr = ` +DIFF: + +CREATE: aws_instance.bar + foo: "" => "bar" + type: "" => "aws_instance" +CREATE: aws_instance.foo.1 + foo: "" => "foo" + type: "" => "aws_instance" +CREATE: aws_instance.foo.2 + foo: "" => "foo" + type: "" => "aws_instance" + +STATE: + +aws_instance.foo.0: + ID = bar + foo = foo + type = aws_instance +` + const testTerraformPlanDestroyStr = ` DIFF: