From 6c96e0f6acf997d7105cd11ad04066922a6b2f91 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 9 Oct 2014 23:15:42 -0700 Subject: [PATCH] terraform: nil out the Diff on a resource when expanding This fixes a bug where the Destroy diff was being kept around for nodes that shouldn't be destroyed. We added a test to verify this doesn't happen. --- terraform/context.go | 5 + terraform/context_test.go | 126 ++++++++++++++++++ terraform/graph.go | 1 + terraform/terraform_test.go | 18 +++ .../test-fixtures/apply-count-dec-one/main.tf | 7 + .../test-fixtures/apply-count-dec/main.tf | 8 ++ 6 files changed, 165 insertions(+) create mode 100644 terraform/test-fixtures/apply-count-dec-one/main.tf create mode 100644 terraform/test-fixtures/apply-count-dec/main.tf diff --git a/terraform/context.go b/terraform/context.go index 7f1502fa1..171237efd 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -862,6 +862,8 @@ func (c *walkContext) planWalkFn() depgraph.WalkFunc { } if !diff.Empty() { + log.Printf("[DEBUG] %s: Diff: %#v", r.Id, diff) + l.Lock() md := result.Diff.ModuleByPath(c.Path) if md == nil { @@ -1259,6 +1261,9 @@ func (c *walkContext) genericWalkResource( return err } + println(fmt.Sprintf("%s NODES: %#v", rn.Resource.Id, ns)) + println(fmt.Sprintf("%s DIFF: %#v", rn.Resource.Id, rn.Resource.Diff)) + // Go through all the nouns and run them in parallel, collecting // any errors. var l sync.Mutex diff --git a/terraform/context_test.go b/terraform/context_test.go index 857b923ab..877ff00cc 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -940,6 +940,132 @@ func TestContextApply_compute(t *testing.T) { } } +func TestContextApply_countDecrease(t *testing.T) { + m := testModule(t, "apply-count-dec") + 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{ + "foo": "foo", + "type": "aws_instance", + }, + }, + }, + "aws_instance.foo.1": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "foo", + "type": "aws_instance", + }, + }, + }, + "aws_instance.foo.2": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "foo", + "type": "aws_instance", + }, + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyCountDecStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + +func TestContextApply_countDecreaseToOne(t *testing.T) { + m := testModule(t, "apply-count-dec-one") + 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{ + "foo": "foo", + "type": "aws_instance", + }, + }, + }, + "aws_instance.foo.1": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + "aws_instance.foo.2": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyCountDecToOneStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContextApply_module(t *testing.T) { m := testModule(t, "apply-module") p := testProvider("aws") diff --git a/terraform/graph.go b/terraform/graph.go index 19b026715..489e17d0f 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1750,6 +1750,7 @@ func (n *GraphNodeResource) copyResource(id string) *Resource { resource.Id = id resource.Info = &info resource.Config = NewResourceConfig(n.Config.RawConfig) + resource.Diff = nil return &resource } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index b620d23c5..6eee2c51d 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -206,6 +206,24 @@ aws_instance.foo: type = aws_instance ` +const testTerraformApplyCountDecStr = ` +aws_instance.foo.0: + ID = bar + foo = foo + type = aws_instance +aws_instance.foo.1: + ID = bar + foo = foo + type = aws_instance +` + +const testTerraformApplyCountDecToOneStr = ` +aws_instance.foo.0: + ID = bar + foo = foo + type = aws_instance +` + const testTerraformApplyMinimalStr = ` aws_instance.bar: ID = foo diff --git a/terraform/test-fixtures/apply-count-dec-one/main.tf b/terraform/test-fixtures/apply-count-dec-one/main.tf new file mode 100644 index 000000000..7837f5865 --- /dev/null +++ b/terraform/test-fixtures/apply-count-dec-one/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" { + foo = "foo" +} + +resource "aws_instance" "bar" { + foo = "bar" +} diff --git a/terraform/test-fixtures/apply-count-dec/main.tf b/terraform/test-fixtures/apply-count-dec/main.tf new file mode 100644 index 000000000..f18748c3b --- /dev/null +++ b/terraform/test-fixtures/apply-count-dec/main.tf @@ -0,0 +1,8 @@ +resource "aws_instance" "foo" { + foo = "foo" + count = 2 +} + +resource "aws_instance" "bar" { + foo = "bar" +}