Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20251014-162659.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'helper/resource: Adds `PostApplyFunc` test step hook to run generic post-apply logic for plan/apply testing.'
time: 2025-10-14T16:26:59.267372-04:00
custom:
Issue: "566"
4 changes: 4 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ type TestStep struct {
// SkipFunc is called after PreConfig but before applying the Config.
SkipFunc func() (bool, error)

// PostApplyFunc is called after the Config is applied and after all plan/apply checks are run.
// This can be used to perform assertions against API values that are not stored in Terraform state.
PostApplyFunc func()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a specific qualm about the signature, but if you haven't already, might be worth confirming with interested provider developers if there is anything they'd expect to be in a request/response, since we can't add anything without a breaking change. For example, some of these hooks have error returns for the test framework to handle, do they need any more context/data in the request, etc.


//---------------------------------------------------------------
// ImportState testing
//---------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
}
}

if step.PostApplyFunc != nil {
logging.HelperResourceDebug(ctx, "Calling TestCase PostApplyFunc")
step.PostApplyFunc()
logging.HelperResourceDebug(ctx, "Called TestCase PostApplyFunc")
}

if refreshAfterApply && !step.Destroy && !step.PlanOnly {
if len(c.Steps) > stepIndex+1 {
// If the next step is a refresh, then we have no need to refresh here
Expand Down
64 changes: 64 additions & 0 deletions helper/resource/testing_new_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,67 @@ func Test_ConfigStateChecks_Errors(t *testing.T) {
},
})
}

func Test_PostApplyFunc_Called(t *testing.T) {
t.Parallel()

spy1 := &stateCheckSpy{}
postFuncCalled := false
UnitTest(t, TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"test_resource": {
CreateResponse: &resource.CreateResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "test"),
},
),
},
SchemaResponse: &resource.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "id",
Type: tftypes.String,
Computed: true,
},
},
},
},
},
},
},
}),
},
Steps: []TestStep{
{
Config: `resource "test_resource" "test" {}`,
ConfigStateChecks: []statecheck.StateCheck{
spy1,
},
PostApplyFunc: func() {
postFuncCalled = true
},
},
},
})

if !postFuncCalled {
t.Error("expected PostApplyFunc to be called at least once")
}

if !spy1.called {
t.Error("expected ConfigStateChecks spy1 to be called at least once")
}
}