TDD: where is information on how to do TDD for terraform?

Hello:
work has a mandate that we must do TDD. Google seems to have a couple of frameworks that look to becoming the de-facto standard. I am hoping that people can chime in and say

I watched the Terraform TDD info located here

after watching the video i think that " I don’t have enough to write a TDD test and run it"

can you recommend some place for learning how to write TDD for Terraform

Hi @pcooke2002!

I typically understand test-driven development as a principle for what order you work in rather than what you produce: you were presumably going to test your module in any case, but test-driven development suggests that you write your test first, see it fail, and then make it pass. This is as opposed to writing your module first, and then writing tests for it.

So really I think this question comes down to “how can I test a Terraform module?”, which is a fair question regardless of at what point in the process you are doing that testing.

Because a Terraform module’s only purpose is to describe a desired infrastructure state, testing that module typically means applying it and then verifying that the infrastructure is configured correctly. In other words, a typical test of a Terraform module is really a test of the infrastructure it built, and not so much the module itself.

For example, if your Terraform module’s purpose is to describe a virtual network topology, then testing that module would likely involve applying it in an isolated test environment and then inspecting the network objects it created to verify that they meet expectations. The testing part could therefore be written in any language and any testing framework where there’s an available client to interrogate the remote system. You can then use terraform destroy to delete that testing infrastructure once you’ve verified it.

To apply test-driven development to that, you’d presumably write a program which accesses the test environment and expects to find a valid set of network objects, but that fails because no such objects exist yet. Then you can write the Terraform configuration to declare those objects and apply it before you re-run your tests. Your test program should then find the expected network objects, and thus succeed.

Some module developers optimize this process slightly by writing a test configuration which calls the module multiple times with different arguments in order to describe a suite of possible permutations that the test program will then check for. That means you can test various possibilities all at once, as long as the module and its underlying system can support multiple instances of itself at once. You can also, if you wish, make your test suite also run terraform apply and terraform destroy as its preparation and teardown steps respectively, so that you can in principle run your tests with a single command.

With all of that said, it’s worth noting that infrastructure objects in remote systems are typically considerably slower to manipulate than local logic would be, and so the test run feedback loop will inevitably be longer than you might be accustomed to for testing software running locally.

1 Like