Commit 9487cfba authored by James Bardin's avatar James Bardin
Browse files

add test for planned private data in destroy

parent 96c72051
liamcervante/structured-run-output add-cont-valid-callout b-check-output-multi-expand b-check-resource-multi-expand b-flatten-panic b-yamldecode-emptydoc-null backport/add-cont-valid-callout/luckily-golden-wildcat backport/alisdair/pre-convert-optional-defaults/virtually-able-mouse backport/b-check-resource-multi-expand/extremely-key-sheep backport/clarify-backend-state-storage/ultimately-causal-ladybird backport/cstella84/patch-add-hyperlink-for-referenced-argument backport/doc-s3-fix/utterly-close-rabbit backport/docs-for-each-list-toset/basically-still-zebra backport/docs/unknwon-value/completely-musical-lionfish backport/f-build-go1.19.3/largely-peaceful-grouper backport/fix-backends-link/vertically-noble-hornet backport/fix-internals-overview/globally-allowed-kid backport/fix-links-devdot/strictly-notable-sparrow backport/kevin/rewrite-internal-redirects/quietly-helped-pelican backport/mg_no_code_prov_followup/marginally-relevant-eagle backport/mktg-tf-76ef54dc3c574e032725e0341be8e1d2/friendly-evident-grouse backport/optional-type-attributes-note/inherently-dear-goat backport/patch-1/nationally-working-kite backport/patch-1/noticeably-comic-manatee backport/patch-1/vaguely-deciding-beagle backport/patch-1/virtually-more-rhino backport/patch-1/wholly-verified-racer backport/patch-1/willingly-usable-husky backport/patch-2/finally-amazed-catfish backport/patch-2/openly-clean-tick backport/remove-provisioners/readily-correct-ferret backport/startsswith-to-startswith/highly-gorgeous-katydid backport/workspaces-confusion-fixes/secondly-huge-titmouse brandonc/changelog_nested_sensitive brandonc/changelog_sensitive_diff_fixes brandonc/nested_attr_sensitive brandonc/providers-estimate build-workflow-dev/liamcervante/equivalence-test-action bump-gcp-storage-dependency bump-gcp-storage-dependency-2 dev-portal-updates-docs dividers-devdot-fixes doc-unicode-hcl doc-yamlencode-stable docs-readme-updates-versioned-docs f-addrs-static-checkable f-build-go1.19.3 f-cli-hide-fast-refresh f-cmd-web f-dynamic-provider-assignment f-expand-root-outputs f-jsonstate-2 f-moduletest-2 f-output-value-types f-partial-plan-on-error f-persistent-checks-old fix-dividers-for-devdot fix-future-facing-language fix-future-lang-2 fix-internals-overview fix-links-devdot gcs-backend-add-kms gcs-backend-add-private-connect-support gcs-refactor-credential-handling gs/add-pre-plan-run-tasks jbardin/1.3-destroy-perf jbardin/data-source-destroy-edges jbardin/output-perf jbardin/plan-orphan-deleted jbardin/remove-deprecated-backends jbardin/resolved-provided-by jbardin/terraform-data jbardin/terraform-null kevin/remove-guides-docs liamcervante/cicd-go-vet v1.4.0-alpha20221109 v1.3.5 v1.3.4 v1.3.3 v1.3.2 v1.3.1 v1.3.0 v1.3.0-rc1 v1.3.0-beta1 v1.3.0-alpha20220817 v1.3.0-alpha20220803 v1.3.0-alpha20220706
No related merge requests found
Showing with 72 additions and 0 deletions
+72 -0
......@@ -1077,3 +1077,75 @@ got: %#v`,
t.Fatal("failed to configure provider during destroy plan")
}
}
// check that a provider can verify a planned destroy
func TestContext2Apply_plannedDestroy(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_object" "x" {
test_string = "ok"
}`,
})
p := simpleMockProvider()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
if !req.ProposedNewState.IsNull() {
// we should only be destroying in this test
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unexpected plan with %#v", req.ProposedNewState))
return resp
}
resp.PlannedState = req.ProposedNewState
// we're going to verify the destroy plan by inserting private data required for destroy
resp.PlannedPrivate = append(resp.PlannedPrivate, []byte("planned")...)
return resp
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
// if the value is nil, we return that directly to correspond to a delete
if !req.PlannedState.IsNull() {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unexpected apply with %#v", req.PlannedState))
return resp
}
resp.NewState = req.PlannedState
// make sure we get our private data from the plan
private := string(req.PlannedPrivate)
if private != "planned" {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("missing private data from plan, got %q", private))
}
return resp
}
state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.x").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"test_string":"ok"}`),
},
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
plan, diags := ctx.Plan(m, state, &PlanOpts{
Mode: plans.DestroyMode,
// we don't want to refresh, because that actually runs a normal plan
SkipRefresh: true,
})
if diags.HasErrors() {
t.Fatalf("plan: %s", diags.Err())
}
_, diags = ctx.Apply(plan, m)
if diags.HasErrors() {
t.Fatalf("apply: %s", diags.Err())
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment