From 916eb5aaf37c6d56ec374ab7ec0d942e1e7df9af Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Jul 2014 12:53:01 -0700 Subject: [PATCH] terraform: copy before modifying struct in graphAddDiff /cc @pearkes - Fixes the destroy bug --- terraform/context.go | 1 + terraform/graph.go | 5 ++++- terraform/graph_test.go | 12 ++++++++++-- terraform/terraform_test.go | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index 84aa7b460..2c2df5ee8 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -412,6 +412,7 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc { cb := func(r *Resource) error { diff := r.Diff if diff.Empty() { + log.Printf("[DEBUG] %s: Diff is empty. Will not apply.", r.Id) return nil } diff --git a/terraform/graph.go b/terraform/graph.go index ff97e5fbf..56ef94bc0 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -295,7 +295,10 @@ func graphAddDiff(g *depgraph.Graph, d *Diff) error { // Mark the old diff to not destroy since we handle that in // the dedicated node. - rd.Destroy = false + newDiff := new(ResourceDiff) + *newDiff = *rd + newDiff.Destroy = false + rd = newDiff // Add to the new noun to our dependencies so that the destroy // happens before the apply. diff --git a/terraform/graph_test.go b/terraform/graph_test.go index f8d460e1c..a83b10c47 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -195,6 +195,8 @@ func TestGraphAddDiff_destroy(t *testing.T) { }, } + diffHash := checksumStruct(t, diff) + g, err := Graph(&GraphOpts{ Config: config, Diff: diff, @@ -211,14 +213,20 @@ func TestGraphAddDiff_destroy(t *testing.T) { } // Verify that the state has been added - n := g.Noun("aws_instance.foo") + n := g.Noun("aws_instance.foo (destroy)") rn := n.Meta.(*GraphNodeResource) - expected2 := diff.Resources["aws_instance.foo"] + expected2 := &ResourceDiff{Destroy: true} actual2 := rn.Resource.Diff if !reflect.DeepEqual(actual2, expected2) { t.Fatalf("bad: %#v", actual2) } + + // Verify that our original structure has not been modified + diffHash2 := checksumStruct(t, diff) + if diffHash != diffHash2 { + t.Fatal("diff has been modified") + } } const testTerraformGraphStr = ` diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 1f5e792a1..91f1e1990 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -1,6 +1,10 @@ package terraform import ( + "bytes" + "crypto/sha1" + "encoding/gob" + "encoding/hex" "path/filepath" "sync" "testing" @@ -11,6 +15,17 @@ import ( // This is the directory where our test fixtures are. const fixtureDir = "./test-fixtures" +func checksumStruct(t *testing.T, i interface{}) string { + buf := new(bytes.Buffer) + enc := gob.NewEncoder(buf) + if err := enc.Encode(i); err != nil { + t.Fatalf("err: %s", err) + } + + sum := sha1.Sum(buf.Bytes()) + return hex.EncodeToString(sum[:]) +} + func testConfig(t *testing.T, name string) *config.Config { c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf")) if err != nil {