You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are certain patterns of provider acceptance testing which involving checking/comparing multiple values, typically computed resource attribute values from multiple TestStep states, including:
Value Should Change: For example, ensuring that a computed attribute value is different after resource replacement, to assert that the resource replacement occurred as expected.
Value Should Not Change: For example, ensuring that a computed attribute value is the same after resource update.
Integer Value Should Increment: For example, ensuring that a computed attribute value, such as an API-handled resource-wide version number, increments after resource update/replacement. (Although now that plan checks exist for known values, this could be considered moot since checking that the resource generated the incremented known value during planning is just as valid/correct since Terraform will error if there is a mismatch after apply.)
When provider developers encounter this situation, there is not really a native/prescriptive way to handle the situation. It would be nice if at all possible to encourage developers towards more supported/prescriptive testing patterns. Having those patterns be baked into the provider acceptance testing API and extensible would better position us to document what developers should do or try in these situations, rather than potentially point at example code that may differ between providers and require extra explanation/re-familiarization each time.
Attempted solutions
Overload the abstraction of the StateCheckFunc-based checking (TestStep.Checks) to extract API object types or resource attribute state values and potentially another function to implement value comparisons. Since there is no a canonical way to extract values out of API/state during each test step, developers are left to figure out this solution on their own. The additional "problem" with this setup is that it then is no longer a "check", even though much of the internal logic is the same as required for a state check to get to a resource attribute value.
From the hashicorp/aws provider, this example extracts API objects for comparison (which should instead be possible via state value comparison):
An idea could be terraform-plugin-testing offering multiple value checking helpers that can work like the following:
Before the test case starts, the developer initializes the multiple value check
In each step, have a method on the multiple value check that implements the state check interface and enables developers to point at a resource address and attribute path. That logic could extract the attribute value to include it in the multiple value check.
(Optional) After the test case is ended, the developer calls a method on the multiple value check that returns an error or not.
By pseudo-code example (naming, interfaces, etc. debatable!):
The post test case logic feels optional as this check could be written so the checking logic runs each time a value is "added" (raising an error immediately, on the failing test step, if the added value doesn't conform to the check). The benefit being less logic that needs to be written each time, but the tradeoff being that it could have less implementation error handling (e.g. returning an error if only one value is ever provided) unless the additional method is provided to do so after the acceptance testing should have ran.
The developer is declaring the check as a single unit and leaving the resource address and path information within each step, likely near other declarative checks doing the same, so the interface should feel familiar. The check can save step numbers, resource addresses, attribute paths, etc. associated with each value for error messaging.
It may be possible to further enhance this by having a generic value comparison interface:
// ValueComparer defines an interface that is implemented to run comparison logic on multiple values. Individual// implementations determine how the comparison is performed (e.g., values differ, values equal).typeValueComparerinterface {
// CompareValues should assert the given known values against any expectations.// Values are always ordered in the order they were added. Use the error// return to signal unexpected values or implementation errors.CompareValues(values...any) error// String should return a string representation of the comparison.String() string
}
// example implementationtypeValuesDifferstruct {}
func (dValuesDiffer) CompareValues(values...any) error {
// logic to ensure all values differ
}
And provider developers could implement custom value comparison logic. Its not clear if making the check itself an interface would be beneficial or unnecessary abstraction, but that could likely be teased out during further design/implementation.
For what its worth, the proposed ValueComparer interface could also be something that helps with a re-implementation of TestCheckResourceAttrPair() for state checks as mentioned in #282.
terraform-plugin-testing version
Use cases
There are certain patterns of provider acceptance testing which involving checking/comparing multiple values, typically computed resource attribute values from multiple
TestStep
states, including:When provider developers encounter this situation, there is not really a native/prescriptive way to handle the situation. It would be nice if at all possible to encourage developers towards more supported/prescriptive testing patterns. Having those patterns be baked into the provider acceptance testing API and extensible would better position us to document what developers should do or try in these situations, rather than potentially point at example code that may differ between providers and require extra explanation/re-familiarization each time.
Attempted solutions
Overload the abstraction of the
StateCheckFunc
-based checking (TestStep.Checks
) to extract API object types or resource attribute state values and potentially another function to implement value comparisons. Since there is no a canonical way to extract values out of API/state during each test step, developers are left to figure out this solution on their own. The additional "problem" with this setup is that it then is no longer a "check", even though much of the internal logic is the same as required for a state check to get to a resource attribute value.From the hashicorp/aws provider, this example extracts API objects for comparison (which should instead be possible via state value comparison):
From the hashicorp/time provider, for example:
Proposal
An idea could be terraform-plugin-testing offering multiple value checking helpers that can work like the following:
By pseudo-code example (naming, interfaces, etc. debatable!):
The post test case logic feels optional as this check could be written so the checking logic runs each time a value is "added" (raising an error immediately, on the failing test step, if the added value doesn't conform to the check). The benefit being less logic that needs to be written each time, but the tradeoff being that it could have less implementation error handling (e.g. returning an error if only one value is ever provided) unless the additional method is provided to do so after the acceptance testing should have ran.
The developer is declaring the check as a single unit and leaving the resource address and path information within each step, likely near other declarative checks doing the same, so the interface should feel familiar. The check can save step numbers, resource addresses, attribute paths, etc. associated with each value for error messaging.
It may be possible to further enhance this by having a generic value comparison interface:
So implementations could look like:
And provider developers could implement custom value comparison logic. Its not clear if making the check itself an interface would be beneficial or unnecessary abstraction, but that could likely be teased out during further design/implementation.
References
rfc3339
values terraform-provider-time#295The text was updated successfully, but these errors were encountered: