Terraform test preconditions or postconditions

Hi All, is there a good example out there of exercising pre and post conditions under terraform test?

Although the documentation says its possible with expect_failures referencing a resource with a precondition, I have never been able to get a working example the same way I can for expect_failures referencing an input variable

Hi @rob.borg,

I don’t have a ready example of it, but I would have expected it to be quite similar to input variables.

One detail to watch out for is that Terraform distinguishes between a check failure and an error while evaluating the condition. A check only “fails” if its condition expression evaluates successfully and returns false.

If the condition expression itself is invalid then Terraform treats that as an error rather than a failure, and so expect_failures would not be applicable.

I’m not sure if that’s what’s going on for you, though. It might help if you can share an example of the problem that I can execute myself to understand what’s happening, and then I might be able to explain how to make it work. (Or open a bug report, if we discover that Terraform is not working as intended here.)

Than you @apparentlymart . I see. A basic test works without issue.

When errors are thrown or a precondition of a different resource in the same module is triggered before it gets to the precondition I was targeting, the test is halted and it never gets to throw the expected failure.

Also worth noting that mocking out the first resource to throw the precondition failure doesn’t seem to halt the precondition check being thrown either, so I guess preconditions are thrown before mocks are called?

If the resource you have mocked has a failing postcondition then the configuration for that resource will be validated but Terraform will not perform its planning step, because the precondition blocks it.