Skip to content

Commit

Permalink
ensure plans always have a stored state
Browse files Browse the repository at this point in the history
When refresh was skipped for a destroy plan, there was no state stored
in the plan.
  • Loading branch information
jbardin committed Apr 1, 2021
1 parent 1e1bb00 commit 71dbe0c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@ func (c *Context) plan() (*plans.Plan, tfdiags.Diagnostics) {

func (c *Context) destroyPlan() (*plans.Plan, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
destroyPlan := &plans.Plan{}
destroyPlan := &plans.Plan{
State: c.state.DeepCopy(),
}
c.changes = plans.NewChanges()

// A destroy plan starts by running Refresh to read any pending data
Expand Down
48 changes: 48 additions & 0 deletions terraform/context_plan2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,51 @@ resource "test_resource" "b" {
}
}
}

func TestContext2Plan_destroySkipRefresh(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_object" "a" {
}
`,
})

p := simpleMockProvider()
p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
resp.NewState = cty.NullVal(req.PriorState.Type())
return resp
}

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

0 comments on commit 71dbe0c

Please sign in to comment.