Terraform
Output Plan Checks
The terraform-plugin-testing
module provides a package plancheck
with built-in output value plan checks for common use-cases:
Check | Description |
---|---|
plancheck.ExpectNullOutputValue(address) | Asserts the output at the specified address has a null value. |
plancheck.ExpectNullOutputValueAtPath(address, path) | Asserts the output at the specified address, and path has a null value. |
plancheck.ExpectUnknownOutputValue(address) | Asserts the output at the specified address has an unknown value. |
plancheck.ExpectUnknownOutputValueAtPath(address, path) | Asserts the output at the specified address, and path has an unknown value. |
Example using plancheck.ExpectUnknownOutputValue
One of the built-in plan checks, plancheck.ExpectUnknownOutputValue
, determines whether an output value is unknown, for example, prior to the terraform apply
phase.
The following uses the time_offset resource from the time provider, to illustrate usage of the plancheck.ExpectUnknownOutputValue
, and verifies that day
is unknown.
func Test_Time_Unknown(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
}
output day {
value = time_offset.one.day
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownOutputValue("day"),
},
},
},
},
})
}
Example using plancheck.ExpectUnknownOutputValueAtPath
Output values can contain objects or collections as well as primitive (e.g., string) values. Output value plan checks provide two forms for the plan checks, for example ExpectUnknownOutputValue()
, and ExpectUnknownOutputValueAtPath()
. The Expect<...>OutputValueAtPath()
form is used to access a value contained within an object or collection, as illustrated in the following example.
func Test_Time_Unknown(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
}
output time_offset_one {
value = time_offset.one
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownOutputValueAtPath("time_offset_one", tfjsonpath.New("day")),
},
},
},
},
})
}