ensure plans always have a stored state

When refresh was skipped for a destroy plan, there was no state stored
in the plan.
This commit is contained in:
James Bardin 2021-04-01 16:41:30 -04:00
parent 1e1bb00e5b
commit b6b7f78b49
2 changed files with 47 additions and 1 deletions

View File

@ -604,7 +604,9 @@ func (c *Context) plan() (*plans.Plan, tfdiags.Diagnostics) {
func (c *Context) destroyPlan() (*plans.Plan, tfdiags.Diagnostics) { func (c *Context) destroyPlan() (*plans.Plan, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
destroyPlan := &plans.Plan{} destroyPlan := &plans.Plan{
State: c.state.DeepCopy(),
}
c.changes = plans.NewChanges() c.changes = plans.NewChanges()
// A destroy plan starts by running Refresh to read any pending data // A destroy plan starts by running Refresh to read any pending data

View File

@ -333,3 +333,47 @@ resource "test_resource" "b" {
} }
} }
} }
func TestContext2Plan_destroySkipRefresh(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_object" "a" {
}
`,
})
p := simpleMockProvider()
addr := mustResourceInstanceAddr("test_object.a")
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(addr, &states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"test_string":"foo"}`),
Status: states.ObjectReady,
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
})
ctx := testContext2(t, &ContextOpts{
Config: m,
State: state,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
Destroy: true,
SkipRefresh: true,
})
plan, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatal(diags.Err())
}
if plan.State == nil {
t.Fatal("missing plan state")
}
for _, c := range plan.Changes.Resources {
if c.Action != plans.Delete {
t.Errorf("unexpected %s change for %s", c.Action, c.Addr)
}
}
}