terraform: copy before modifying struct in graphAddDiff

/cc @pearkes - Fixes the destroy bug
This commit is contained in:
Mitchell Hashimoto 2014-07-07 12:53:01 -07:00
parent 4f2388f787
commit 916eb5aaf3
4 changed files with 30 additions and 3 deletions

View File

@ -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
}

View File

@ -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.

View File

@ -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 = `

View File

@ -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 {