Terraform
Resource State Checks
The terraform-plugin-testing
module provides a package statecheck
with built-in managed resource, and data source state checks for common use-cases:
Check | Description |
---|---|
ExpectKnownValue | Asserts the specified attribute at the given managed resource, or data source, has the specified type, and value. |
ExpectSensitiveValue | Asserts the specified attribute at the given managed resource, or data source, has a sensitive value. |
ExpectKnownValue
State Check
The statecheck.ExpectKnownValue(address, path, value)
state check provides a basis for asserting that a specific resource attribute 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 ExpectKnownValue
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 TestExpectKnownValue_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" {}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.Bool(true),
),
},
},
},
})
}
ExpectSensitiveValue
State Check
The statecheck.ExpectSensitiveValue(address, path)
state check provides a basis for asserting that a specific resource attribute is marked as sensitive.
Note: In this example, a TerraformVersionCheck is being used to prevent execution of this test prior to Terraform version 1.4.6
(refer to the release notes for Terraform v1.4.6).
package example_test
import (
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)
func Test_ExpectSensitiveValue_SensitiveStringAttribute(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_4_6), // StateResource.SensitiveValues
},
// Provider definition omitted.
Steps: []resource.TestStep{
{
Config: `
resource "test_resource" "one" {
sensitive_string_attribute = "test"
}
`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectSensitiveValue("test_resource.one",
tfjsonpath.New("sensitive_string_attribute")),
},
},
},
})
}