Fix nested module "unknown variable" during dest (#7496)

* Fix nested module "unknown variable" during dstry

During a destroy with nested modules, accessing a variable between them
causes an "unknown variable accessed" during destroy.
This commit is contained in:
James Bardin 2016-07-06 11:22:41 -04:00 committed by GitHub
parent 86ff2ca7a9
commit 21e2173e0a
5 changed files with 98 additions and 2 deletions

View File

@ -4704,3 +4704,83 @@ aws_instance.foo:
t.Fatalf("bad: \n%s", actual) t.Fatalf("bad: \n%s", actual)
} }
} }
// https://github.com/hashicorp/terraform/issues/7378
func TestContext2Apply_destroyNestedModuleWithAttrsReferencingResource(t *testing.T) {
m := testModule(t, "apply-destroy-nested-module-with-attrs")
p := testProvider("null")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
var state *State
var err error
{
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
},
})
// First plan and apply a create operation
if _, err := ctx.Plan(); err != nil {
t.Fatalf("plan err: %s", err)
}
state, err = ctx.Apply()
if err != nil {
t.Fatalf("apply err: %s", err)
}
}
{
ctx := testContext2(t, &ContextOpts{
Destroy: true,
Module: m,
State: state,
Providers: map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
},
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("destroy plan err: %s", err)
}
var buf bytes.Buffer
if err := WritePlan(plan, &buf); err != nil {
t.Fatalf("plan write err: %s", err)
}
planFromFile, err := ReadPlan(&buf)
if err != nil {
t.Fatalf("plan read err: %s", err)
}
ctx, err = planFromFile.Context(&ContextOpts{
Providers: map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
},
})
if err != nil {
t.Fatalf("err: %s", err)
}
state, err = ctx.Apply()
if err != nil {
t.Fatalf("destroy apply err: %s", err)
}
}
//Test that things were destroyed
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(`
<no state>
module.middle:
<no state>
`)
if actual != expected {
t.Fatalf("expected: \n%s\n\nbad: \n%s", expected, actual)
}
}

View File

@ -130,6 +130,7 @@ func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex da
if vertex == nil { if vertex == nil {
vertex = opts.Vertex vertex = opts.Vertex
} }
log.Printf("[DEBUG] hasDestroyEdgeInPath: Looking for destroy edge: %s - %T", dag.VertexName(vertex), vertex) log.Printf("[DEBUG] hasDestroyEdgeInPath: Looking for destroy edge: %s - %T", dag.VertexName(vertex), vertex)
for _, v := range opts.Graph.UpEdges(vertex).List() { for _, v := range opts.Graph.UpEdges(vertex).List() {
if len(opts.Graph.UpEdges(v).List()) > 1 { if len(opts.Graph.UpEdges(v).List()) > 1 {
@ -137,10 +138,12 @@ func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex da
return true return true
} }
} }
// Here we borrow the implementation of DestroyEdgeInclude, whose logic // Here we borrow the implementation of DestroyEdgeInclude, whose logic
// and semantics are exactly what we want here. // and semantics are exactly what we want here. We add a check for the
// the root node, since we have to always depend on its existance.
if cv, ok := vertex.(*GraphNodeConfigVariableFlat); ok { if cv, ok := vertex.(*GraphNodeConfigVariableFlat); ok {
if cv.DestroyEdgeInclude(v) { if dag.VertexName(v) == rootNodeName || cv.DestroyEdgeInclude(v) {
return true return true
} }
} }

View File

@ -0,0 +1 @@
variable "bottom_param" {}

View File

@ -0,0 +1,8 @@
variable "param" {}
resource "null_resource" "n" {}
module "bottom" {
source = "./bottom"
bottom_param = "${var.param}"
}

View File

@ -0,0 +1,4 @@
module "middle" {
source = "./middle"
param = "foo"
}