9 Testing and refactoring

This chapter covers

  • The hows and whys of testing
  • Challenges of testing infrastructure as code
  • Terratest
  • The Terraform testing framework
  • Copilot
  • Module refactoring
  • Managing and breaking backward compatibility

The ability to run automated tests is by far one of the most powerful benefits of using infrastructure as code (IaC). While writing tests does require some level of upfront effort, the payout is more than worth it. Automated testing grants three immediate benefits that will exist for the life of the project:

9.1 The theory of IaC testing

9.1.1 What to test (and what not to test)

9.1.2 How IaC testing differs from software testing

9.1.3 Terraform testing frameworks

9.1.4 Unit testing vs. integration testing

9.2 Testing IaC in practice

9.2.1 Simple testing flow

9.2.2 Starting with examples

9.2.3 Concurrency and automated testing

9.2.4 Timeouts

9.2.5 Automatic cleanup

9.2.6 Authentication and secrets

9.2.7 Testing as code

9.3 Terratest

9.3.1 Getting started with Go

9.3.2 Terratest Hello World

9.3.3 Building on examples

9.3.4 Terratest helpers

9.3.5 Updating our makefile and template

9.3.6 Testing with CI

9.3.7 Terratest and Copilot

9.4 Terraform testing framework

9.4.1 Hello World