Add tests for action invocation counts

pull/38245/head
Mutahhir Hayat 4 months ago committed by Daniel Schmidt
parent a876afb6ca
commit 0cb9a689d7

@ -0,0 +1,26 @@
terraform {
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}
}
resource "testing_resource" "this" {
id = "test"
value = "hello"
lifecycle {
action_trigger {
events = [after_create]
actions = [action.testing_action.example]
}
}
}
action "testing_action" "example" {
config {
message = "Test action invocation"
}
}

@ -0,0 +1,15 @@
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}
provider "testing" "default" {}
component "self" {
source = "./"
providers = {
testing = provider.testing.default
}
}

@ -45,6 +45,11 @@
"source": "git::https://example.com/empty.git",
"local": "empty",
"meta": {}
},
{
"source": "git::https://example.com/action_invocations.git",
"local": "action_invocations",
"meta": {}
}
]
}

@ -104,6 +104,11 @@ func ReportComponentInstance(ctx context.Context, plan *plans.Plan, h *Hooks, se
},
})
}
// Count action invocations
for range plan.Changes.ActionInvocations {
cic.ActionInvocation++
}
hookMore(ctx, seq, h.ReportComponentInstancePlanned, cic)
}

@ -113,6 +113,14 @@ var (
Nesting: configschema.NestingSingle,
},
}
TestingActionSchema = providers.ActionSchema{
ConfigSchema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"message": {Type: cty.String, Optional: true},
},
},
}
)
// MockProvider wraps the standard MockProvider with a simple in-memory
@ -223,6 +231,9 @@ func NewProviderWithData(t *testing.T, store *ResourceStore) *MockProvider {
ReturnType: cty.DynamicPseudoType,
},
},
Actions: map[string]providers.ActionSchema{
"testing_action": TestingActionSchema,
},
ServerCapabilities: providers.ServerCapabilities{
MoveResourceState: true,
},
@ -322,6 +333,29 @@ func NewProviderWithData(t *testing.T, store *ResourceStore) *MockProvider {
}),
}
},
PlanActionFn: func(request providers.PlanActionRequest) providers.PlanActionResponse {
// Simple action planning - no drift, just validation
return providers.PlanActionResponse{
Diagnostics: tfdiags.Diagnostics{},
}
},
InvokeActionFn: func(request providers.InvokeActionRequest) providers.InvokeActionResponse {
// Simple action invocation - just emit a completed event
return providers.InvokeActionResponse{
Events: func(yield func(providers.InvokeActionEvent) bool) {
yield(providers.InvokeActionEvent_Completed{
Diagnostics: tfdiags.Diagnostics{},
})
},
Diagnostics: tfdiags.Diagnostics{},
}
},
ValidateActionConfigFn: func(request providers.ValidateActionConfigRequest) providers.ValidateActionConfigResponse {
// No validation errors for testing actions
return providers.ValidateActionConfigResponse{
Diagnostics: tfdiags.Diagnostics{},
}
},
},
ResourceStore: store,
}

Loading…
Cancel
Save