-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Terraform Version
1.7.4Use Cases
I'm looking to add automated test cases to ensure that our variable validation blocks are working as expected, since some of the involved conditional logic is non-trivial. (So, for avoidance of doubt, I'm looking at input variables only here, rather than resources or other checkable objects - see #34700 for that.)
Whilst most of our validations are at the root module level (as many as possible), a number have been delegated to child modules too; this is primarily due to the limitation Terraform still has in cross-referencing input variables within validation blocks (see #25609), but we can also then rely on derived variables without defining them multiple times.
This balance has worked pretty well till now, and we've been comfortable to just get the validation error messages (regardless of where they appear in the hierarchy). However, in looking to add Terraform tests for the nested validation blocks, I'm not (afaict - see here) able to target the input variables in child modules and hence have no way of catching the error messages that are produced there. Omitting the expect_failures attribute produces the underlying error message from the validation block, but fails the test (which I want to be considered as a pass).
I understand various points that have been made elsewhere around encapsulation best practices, and I also appreciate that the delegated validation blocks could be tested at the child module level. That being said, treating our child modules as black boxes for a moment, I don't really need to define tests at that level as I can read their outputs just fine from the root module level for other testing. Furthermore, if we can already read the outputs from child modules it makes a certain amount of sense to support reading their inputs as well - this doesn't mean that I care about the way in which the child module resources are defined, and so the module is still fairly encapsulated.
Attempted Solutions
No obvious solutions, not able to target the checkable objects in question.
Proposal
Add the capability to access module input variables from within the expect_failures attribute of Terraform test run blocks - perhaps something like module.<module_name>.var.<input_name>, e.g. module.s3.var.bucket_name. This provides a consistent approach to modules from the outside, in the sense that one can access the module inputs as well as the outputs (as can already be done).
Failing this, modifying the attribute to expect errors without an object reference would be acceptable, since the error message already gets produced during the test. I'm not sure if this is preferable, however, as unexpected errors could produce false passes (unless we can identify errors based on the error message itself).
References
No response