Within this article, I aim to quickly outline and inform you of some of the different forms of testing that can be implemented on your Terraform code.

We will cover:

  • Static Testing/Analysis
  • Linters
  • Basic Security Testing
  • Unit Testing
  • Integration Testing
  • Property Testing
  • E2E Testing
  • Example code and links

Static Testing/Analysis

What is Static testing?

Static testing is a software testing technique by which we can check the defects in software without actually executing it. Often you can find small issues that you may have missed when eyeballing your code.

What are the benefits of static testing/analysis?

  • Fast.
  • Stable.
  • No need to deploy resources.
  • Very easy to use.

Hashicorp provides you with a way to perform static analysis with the command — terraform validate. This command will allow for a quick check to make sure you have not made a trivial mistake with spelling, missing a bracket, or what I believe to be more useful; checking for unused variables.

All you will need to do here is run your terraform validate command in your code directory.

_Note- _If you want to do something with your output text you can always format to JSON with the flag _-json_ .

What is a typical example of where this type of testing can help us?Duplication. To give an example:

I have followed a common Hashicorp standard of creating a_ proivider.tf _file to store my provider information:

provider "aws" {
access_key = var.AWS_ACCESS_KEY_ID
secret_key = var.AWS_SECRET_ACCESS_KEY
region = var.region
version = “~> 2.56”
}

However, I have also copied code that already has a provider block nested within it. Therefore, I now have two provider blocks within my Terraform working directory, one in my provider file and one in my main file. If I run a validate command, this becomes clear:

$terraform validate

Error: Duplicate provider configuration
on provider.tf line 1:
1: provider “aws” {
A default (non-aliased) provider configuration for "aws" was already given at main.tf:3,1–15\. If multiple configurations are required, set the "alias" argument for alternative configurations.

#devops #hashicorp-terraform #testing #terraform

A Jump Start on Terraform Testing
2.20 GEEK