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.
This commit is contained in:
Mitchell Hashimoto 2014-10-09 23:15:42 -07:00
parent 958f2ec094
commit 6c96e0f6ac
6 changed files with 165 additions and 0 deletions

View File

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

View File

@ -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")

View File

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

View File

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

View File

@ -0,0 +1,7 @@
resource "aws_instance" "foo" {
foo = "foo"
}
resource "aws_instance" "bar" {
foo = "bar"
}

View File

@ -0,0 +1,8 @@
resource "aws_instance" "foo" {
foo = "foo"
count = 2
}
resource "aws_instance" "bar" {
foo = "bar"
}