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:
parent
958f2ec094
commit
6c96e0f6ac
|
@ -862,6 +862,8 @@ func (c *walkContext) planWalkFn() depgraph.WalkFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !diff.Empty() {
|
if !diff.Empty() {
|
||||||
|
log.Printf("[DEBUG] %s: Diff: %#v", r.Id, diff)
|
||||||
|
|
||||||
l.Lock()
|
l.Lock()
|
||||||
md := result.Diff.ModuleByPath(c.Path)
|
md := result.Diff.ModuleByPath(c.Path)
|
||||||
if md == nil {
|
if md == nil {
|
||||||
|
@ -1259,6 +1261,9 @@ func (c *walkContext) genericWalkResource(
|
||||||
return err
|
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
|
// Go through all the nouns and run them in parallel, collecting
|
||||||
// any errors.
|
// any errors.
|
||||||
var l sync.Mutex
|
var l sync.Mutex
|
||||||
|
|
|
@ -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) {
|
func TestContextApply_module(t *testing.T) {
|
||||||
m := testModule(t, "apply-module")
|
m := testModule(t, "apply-module")
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
|
@ -1750,6 +1750,7 @@ func (n *GraphNodeResource) copyResource(id string) *Resource {
|
||||||
resource.Id = id
|
resource.Id = id
|
||||||
resource.Info = &info
|
resource.Info = &info
|
||||||
resource.Config = NewResourceConfig(n.Config.RawConfig)
|
resource.Config = NewResourceConfig(n.Config.RawConfig)
|
||||||
|
resource.Diff = nil
|
||||||
return &resource
|
return &resource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -206,6 +206,24 @@ aws_instance.foo:
|
||||||
type = aws_instance
|
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 = `
|
const testTerraformApplyMinimalStr = `
|
||||||
aws_instance.bar:
|
aws_instance.bar:
|
||||||
ID = foo
|
ID = foo
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
foo = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "bar" {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
foo = "foo"
|
||||||
|
count = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "bar" {
|
||||||
|
foo = "bar"
|
||||||
|
}
|
Loading…
Reference in New Issue