Terraform
Testing actions
For testing an action, use the plugin testing Go module to implement real world testing with Terraform configuration and commands.
Actions are executed when invoked directly via the CLI
or with a lifecycle.action_trigger configuration block
during terraform apply.
Using a lifecycle.action_trigger block with config mode in your
acceptance test is currently the recommended way to test an action. If there isn't a managed resource that fits to include in your acceptance test
of an action, the built-in terraform_data resource can be used as a replacement.
The PostApplyFunc method on TestStep
can be used to write assertions against the remote API after an action has been invoked, as shown in the example below.
func TestExampleAction(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
// Actions are only available in 1.14 and later
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_14_0),
},
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `
resource "terraform_data" "test" {
lifecycle {
action_trigger {
events = [after_create]
actions = [action.examplecloud_action.do_thing]
}
}
}
action "examplecloud_action" "do_thing" {
config {
attr_one = "hello"
attr_two = "world!"
}
}`,
PostApplyFunc: func() {
// Assertion logic can be written here to confirm the action
// has successfully run. Actions cannot write to plan or state,
// so any assertions are likely to be made against the remote
// API directly.
},
},
},
})
}