Terraform
Resource Plan Checks
The terraform-plugin-testing
module provides a package plancheck
with built-in managed resource, and data source plan checks for common use-cases:
Check | Description |
---|---|
plancheck.ExpectResourceAction(address, operation) | Asserts the given managed resource, or data source, has the specified operation for apply. |
plancheck.ExpectUnknownValue(address, path) | Asserts the specified attribute at the given managed resource, or data source, has an unknown value. |
plancheck.ExpectSensitiveValue(address, path) | Asserts the specified attribute at the given managed resource, or data source, has a sensitive value. |
Examples using plancheck.ExpectResourceAction
One of the built-in plan checks, plancheck.ExpectResourceAction
, is useful for determining the exact action type a resource will under-go during, say, the terraform apply
phase.
Given the following example with the random provider, we have written a test that asserts that random_string.one
will be destroyed and re-created when the length
attribute is changed:
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)
func Test_Random_ForcesRecreate(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
ExternalProviders: map[string]resource.ExternalProvider{
"random": {
Source: "registry.terraform.io/hashicorp/random",
},
},
Steps: []resource.TestStep{
{
Config: `resource "random_string" "one" {
length = 16
}`,
},
{
Config: `resource "random_string" "one" {
length = 15
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("random_string.one", plancheck.ResourceActionDestroyBeforeCreate),
},
},
},
},
})
}
Another example with the time provider asserts that time_offset.one
will be updated in-place when the offset_days
attribute is changed:
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)
func Test_Time_UpdateInPlace(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
ExternalProviders: map[string]resource.ExternalProvider{
"time": {
Source: "registry.terraform.io/hashicorp/time",
},
},
Steps: []resource.TestStep{
{
Config: `resource "time_offset" "one" {
offset_days = 1
}`,
},
{
Config: `resource "time_offset" "one" {
offset_days = 2
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("time_offset.one", plancheck.ResourceActionUpdate),
},
},
},
},
})
}
Multiple plan checks can be combined if you want to assert multiple resource actions:
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)
func Test_Time_UpdateInPlace_and_NoOp(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
ExternalProviders: map[string]resource.ExternalProvider{
"time": {
Source: "registry.terraform.io/hashicorp/time",
},
},
Steps: []resource.TestStep{
{
Config: `resource "time_offset" "one" {
offset_days = 1
}
resource "time_offset" "two" {
offset_days = 1
}`,
},
{
Config: `resource "time_offset" "one" {
offset_days = 2
}
resource "time_offset" "two" {
offset_days = 1
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("time_offset.one", plancheck.ResourceActionUpdate),
plancheck.ExpectResourceAction("time_offset.two", plancheck.ResourceActionNoop),
},
},
},
},
})
}