Terratest is a popular library for testing Terraform code. Testing Infrastructure As Code (IAC) is not as widespread as it should be. The reasons are multi-fold, ranging from developer’s attitude towards testing to the difficulty of writing unit tests because of inherent side effects of IAC. Nevertheless, testing is no less important, in particular under these scenarios:
- When your module gets complicated, with medium to complex behaviour logic
- When your module makes underlying assumptions of external dependencies (such as AWS SCPs at Organization level permitting certain actions)
In this post, we will take a look at using Terratest to test Terraform code. A typical Terratest testing pattern involves:
- Deploying real infrastructure in real environment
- Asserting that the deployed resources behaves as expected
- Undeploy everything at the end of the test.
Behavior Driven Test (BDD) uses examples to describe the behavior of a system. It serves the dual purpose of testing the code and documenting it at the same time. Terratest is not a BDD testing framework, however it is possible to write BDD tests that executes Terratest code. In a later section of this post, we will see how this can be achieved using Godog which is a Go BDD testing library.