@@ -1077,3 +1077,75 @@ got: %#v`,
10771077 t .Fatal ("failed to configure provider during destroy plan" )
10781078 }
10791079}
1080+
1081+ // check that a provider can verify a planned destroy
1082+ func TestContext2Apply_plannedDestroy (t * testing.T ) {
1083+ m := testModuleInline (t , map [string ]string {
1084+ "main.tf" : `
1085+ resource "test_object" "x" {
1086+ test_string = "ok"
1087+ }` ,
1088+ })
1089+
1090+ p := simpleMockProvider ()
1091+ p .PlanResourceChangeFn = func (req providers.PlanResourceChangeRequest ) (resp providers.PlanResourceChangeResponse ) {
1092+ if ! req .ProposedNewState .IsNull () {
1093+ // we should only be destroying in this test
1094+ resp .Diagnostics = resp .Diagnostics .Append (fmt .Errorf ("unexpected plan with %#v" , req .ProposedNewState ))
1095+ return resp
1096+ }
1097+
1098+ resp .PlannedState = req .ProposedNewState
1099+ // we're going to verify the destroy plan by inserting private data required for destroy
1100+ resp .PlannedPrivate = append (resp .PlannedPrivate , []byte ("planned" )... )
1101+ return resp
1102+ }
1103+
1104+ p .ApplyResourceChangeFn = func (req providers.ApplyResourceChangeRequest ) (resp providers.ApplyResourceChangeResponse ) {
1105+ // if the value is nil, we return that directly to correspond to a delete
1106+ if ! req .PlannedState .IsNull () {
1107+ resp .Diagnostics = resp .Diagnostics .Append (fmt .Errorf ("unexpected apply with %#v" , req .PlannedState ))
1108+ return resp
1109+ }
1110+
1111+ resp .NewState = req .PlannedState
1112+
1113+ // make sure we get our private data from the plan
1114+ private := string (req .PlannedPrivate )
1115+ if private != "planned" {
1116+ resp .Diagnostics = resp .Diagnostics .Append (fmt .Errorf ("missing private data from plan, got %q" , private ))
1117+ }
1118+ return resp
1119+ }
1120+
1121+ state := states .NewState ()
1122+ root := state .EnsureModule (addrs .RootModuleInstance )
1123+ root .SetResourceInstanceCurrent (
1124+ mustResourceInstanceAddr ("test_object.x" ).Resource ,
1125+ & states.ResourceInstanceObjectSrc {
1126+ Status : states .ObjectReady ,
1127+ AttrsJSON : []byte (`{"test_string":"ok"}` ),
1128+ },
1129+ mustProviderConfig (`provider["registry.terraform.io/hashicorp/test"]` ),
1130+ )
1131+
1132+ ctx := testContext2 (t , & ContextOpts {
1133+ Providers : map [addrs.Provider ]providers.Factory {
1134+ addrs .NewDefaultProvider ("test" ): testProviderFuncFixed (p ),
1135+ },
1136+ })
1137+
1138+ plan , diags := ctx .Plan (m , state , & PlanOpts {
1139+ Mode : plans .DestroyMode ,
1140+ // we don't want to refresh, because that actually runs a normal plan
1141+ SkipRefresh : true ,
1142+ })
1143+ if diags .HasErrors () {
1144+ t .Fatalf ("plan: %s" , diags .Err ())
1145+ }
1146+
1147+ _ , diags = ctx .Apply (plan , m )
1148+ if diags .HasErrors () {
1149+ t .Fatalf ("apply: %s" , diags .Err ())
1150+ }
1151+ }
0 commit comments