This repository contains necessary documentation to perform automated tests on Terraform scripts.
- Author: Sakthi Santhosh
- Created on: 26/12/2023
terraform test
has been introduced with Terraform V1.6 and it is used to test Terraform scripts. Unlike terraform plan
and terraform apply
, testing is done locally on an ephemeral infractructure on memory. Various assertions can be placed in testing scripts to ensure desired behaviour.
In this repository, Terraform scripts have been written to provision a static website in a S3 bucket.
- Terraform looks for testing scripts in the
./
(root) or./tests/
directory. Inside these directories, it looks for*.tftest.hcl
file and executes them. - Setup work for tests can be written as same as the provisioning scripts. These are called helper modules. For this project, the setup scripts for testing can be found at
./tests/setup/
directory. Here, we're creating a random prefix for S3 bucket as a setup process. - In the test file, there are
run
blocks which represent a step in the process of provisioning. Inside this block, we can have multipleassert
blocks that validate the correctness of the step. - The
run.create_button
block assets three conditions:- Bucket Name
- eTag of
index.html
(hash matching with local and bucket file) - eTag of
error.html
(hash matching with local and bucket file)
- This block uses the
apply
command because we cannot test the assertions without applying (provisioning) the infrastructure. - Values to variables can be assigned inside the
run
block. For example, the variable namedbucket_name
defined in./variables.tf
is assigned a value here. The default value is overidden by this. - The
run.website_is_running
block requires the./tests/final/
helper module. This helper module makes a request (withdata
block in./tests/fina/data.tf
) to the website URL created by the previous run block. This URL is accesses from therun
block which references it from theoutput
block in./outputs.tf
. - Here, the command applied is
plan
as the website is already running (from previousrun
block) andplan
command lists out the execution plans alone which is sufficient for the assertions made in this block.
This is a personal project and I discourage public contributions. The repository has been kept public only for learning purpose.