Terraform
Output State Checks
The terraform-plugin-testing
module provides a package statecheck
with built-in output value state checks for common use-cases:
Check | Description |
---|---|
ExpectKnownOutputValue | Asserts the output at the specified address has the specified type, and value. |
ExpectKnownOutputValueAtPath | Asserts the output at the specified address, and path has the specified type, and value. |
ExpectKnownOutputValue
State Check
The statecheck.ExpectKnownOutputValue(address, value)
state check verifies that a specific output value has a known type, and value.
Refer to Known Value Checks for details, and examples of the available knownvalue.Check types that can be used with the ExpectKnownOutputValue
state check.
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)
func TestExpectKnownOutputValue_CheckState_Bool(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_8_0),
},
// Provider definition omitted.
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::example::bool(true)
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValue(
"test",
knownvalue.Bool(true),
),
},
},
},
})
}
ExpectKnownOutputValueAtPath
State Check
The statecheck.ExpectKnownOutputValueAtPath(address, path, value)
state check verifies that a specific output value at a defined path has a known type, and value.
Refer to Known Value Checks for details, and examples of the available knownvalue.Check types that can be used with the ExpectKnownOutputValueAtPath
state check.
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)
func TestExpectKnownOutputValueAtPath_CheckState_Bool(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed boolean attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}
// Generally, it is not necessary to use an output to test a resource attribute,
// the resource attribute should be tested directly instead. This is only shown as
// an example.
// Generally, it is not necessary to use an output to test a resource attribute,
// the resource attribute should be tested directly instead, by inspecting the
// value of the resource attribute. For instance:
//
// ConfigStateChecks: []statecheck.StateCheck{
// statecheck.ExpectKnownValue(
// "test_resource.one",
// tfjsonpath.New("computed_attribute"),
// knownvalue.Bool(true),
// ),
// },
//
// This is only shown as an example.
output test_resource_one_output {
value = test_resource.one
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownOutputValueAtPath(
"test_resource_one_output",
tfjsonpath.New("computed_attribute"),
knownvalue.Bool(true),
),
},
},
},
})
}