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 |
---|---|
CompareValue | Compares sequential values of the specified attribute at the given managed resource, or data source, using the supplied value comparer. |
CompareValueCollection | Compares each item in the specified collection (e.g., list, set) attribute, with the second specified attribute at the given managed resources, or data sources, using the supplied value comparer. |
CompareValuePairs | Compares the specified attributes at the given managed resources, or data sources, using the supplied value comparer. |
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. |
CompareValue
State Check
The intended usage of statecheck.CompareValue(comparer)
state check is to retrieve a specific resource attribute value from state during sequential test steps, and to compare these values using the supplied value comparer.
Refer to Value Comparers for details, and examples of the available compare.ValueComparer types that can be used with the CompareValue
state check.
CompareValueCollection
State Check
The statecheck.CompareValueCollection(resourceAddressOne, collectionPath, resourceAddressTwo, attributePath, comparer)
state check retrieves a specific collection (e.g., list, set) resource attribute, and a second resource attribute from state, and compares each of the items in the collection with the second attribute using the supplied value comparer.
Refer to Value Comparers for details, and examples of the available compare.ValueComparer types that can be used with the CompareValueCollection
state check.
The following example illustrates how a CompareValue
state check can be used to determine whether an attribute value appears in a collection attribute. Note that this is for illustrative purposes only, CompareValue
should only be used for checking computed values.
The following example illustrates how a CompareValue
state check can be used to determine whether an object attribute value appears in a collection (e.g., list) attribute containing objects. Note that this is for illustrative purposes only, CompareValue
should only be used for checking computed values.
CompareValuePairs
State Check
The statecheck.CompareValuePairs(resourceAddressOne, attributePathOne, resourceAddressTwo, attributePathTwo, comparer)
state check provides a basis for retrieving a pair of attribute values, and comparing them using the supplied value comparer.
Refer to Value Comparers for details, and examples of the available compare.ValueComparer types that can be used with the CompareValuePairs
state check.
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.
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).