Fix crash on check blocks in modules with no other changes (#34067)

pull/34084/head
Liam Cervante 3 years ago committed by GitHub
parent af441abeda
commit e1563572c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -744,6 +744,58 @@ check "error" {
}
}
func TestContextChecks_DoesNotPanicOnModuleExpansion(t *testing.T) {
// This is a bit of a special test, we're adding it to verify that
// https://github.com/hashicorp/terraform/issues/34062 is fixed.
//
// Essentially we make a check block in a child module that depends on a
// resource that has no changes. We don't care about the actual behaviour
// of the check block. We just don't want the apply operation to crash.
m := testModuleInline(t, map[string]string{
"main.tf": `
module "panic_at_the_disco" {
source = "./panic"
}
`,
"panic/main.tf": `
resource "test_object" "object" {
test_string = "Hello, world!"
}
check "check_should_not_panic" {
assert {
condition = test_object.object.test_string == "Hello, world!"
error_message = "condition violated"
}
}
`,
})
p := simpleMockProvider()
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
plan, diags := ctx.Plan(m, states.BuildState(func(state *states.SyncState) {
state.SetResourceInstanceCurrent(
mustResourceInstanceAddr("module.panic_at_the_disco.test_object.object"),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"test_string":"Hello, world!"}`),
Status: states.ObjectReady,
},
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)
}), DefaultPlanOpts)
assertNoErrors(t, diags)
_, diags = ctx.Apply(plan, m)
assertNoErrors(t, diags)
}
func validateCheckDiagnostics(t *testing.T, stage string, expectedWarning, expectedError string, actual tfdiags.Diagnostics) bool {
if expectedError != "" {
if !actual.HasErrors() {

@ -58,6 +58,7 @@ var (
_ GraphNodeModulePath = (*nodeExpandCheck)(nil)
_ GraphNodeDynamicExpandable = (*nodeExpandCheck)(nil)
_ GraphNodeReferencer = (*nodeExpandCheck)(nil)
_ graphNodeExpandsInstances = (*nodeExpandCheck)(nil)
)
// nodeExpandCheck creates child nodes that actually execute the assertions for
@ -75,6 +76,8 @@ type nodeExpandCheck struct {
makeInstance func(addrs.AbsCheck, *configs.Check) dag.Vertex
}
func (n *nodeExpandCheck) expandsInstances() {}
func (n *nodeExpandCheck) ModulePath() addrs.Module {
return n.addr.Module
}

@ -307,6 +307,13 @@ func (t *pruneUnusedNodesTransformer) Transform(g *Graph) error {
func() {
n := nodes[i]
switch n := n.(type) {
case *nodeExpandCheck:
// We always execute check blocks, and they never have
// anything referencing them. We have to explicitly list
// them here otherwise they'll be deleted.
return
case graphNodeTemporaryValue:
// root module outputs indicate they are not temporary by
// returning false here.

Loading…
Cancel
Save