Write Terraform Tests
Terraform tests let you validate your module configuration without impacting your existing state file or resources. Testing is a separate operation that is not part of a plan or apply workflow, but instead builds ephemeral infrastructure and tests your assertions against in-memory state for those short-lived resources. This lets you safely verify changes to your module without affecting your infrastructure.
In this tutorial, you will review the syntax for tests and how to use helper modules to validate your configuration. The example module creates an S3 bucket and uploads files to host a static website. The tests use a helper module to generate a random bucket name as a test input for the bucket module.
After running the test workflow with the provided tests, you will write your own test and helper module. The helper module will create an http
data source, which you will use to test that the static website is running. You will then publish the module to your HCP Terraform private module registry and use the Terraform CLI locally to trigger remote testing in HCP Terraform.
Finally, you will use test mocking to allow Terraform to run your tests without creating unnecessary resources.
Prerequisites
This tutorial assumes that you are familiar with the Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.7+ installed locally
- An AWS account with local credentials configured for use with Terraform
- A GitHub account
- An HCP Terraform account
Create example repository
Navigate to the template
repository
for this tutorial. Click the Use this template button and select Create a
new repository. Choose a GitHub account to create the repository in and
name the new repository terraform-aws-s3-website-tests
. Leave the rest of
the settings at their default values.
Tip
You will publish this module to your Terraform registry, so the repository name must match the format terraform-<PROVIDER>-<NAME>
, where <NAME>
can contain extra hyphens.
Clone your example repository, replacing USER
with your own GitHub username.
Change to the repository directory.
Review example configuration
Explore the example configuration to review how to organize your configuration and test files.
The root directory's main.tf
file defines a publicly-accessible S3 bucket and resources to upload files to the bucket. The www
directory contains the source files for Terramino, a Terraform-skinned Tetris application.
Terraform tests consist of two parts:
- Test files that end with the
.tftest.hcl
file extension - Optional helper modules, which you can use to create test-specific resources and data sources outside of your main configuration
By default when you run the terraform test
command, Terraform looks for .tftest.hcl
files in both the root directory and in the tests
directory. You can tell Terraform to look in a different directory with the -test-directory
flag. In this example, the tests
directory contains all of the tests for the module. The tests/website.tftest.hcl
file contains the configuration for the tests.
You can define optional helper modules to create test-specific resources or data sources. For example, if the module that you are testing creates a compute instance, a helper module can create the required networking infrastructure. In this example, there is a helper module in the tests/setup
directory to generate a globally unique bucket name using the random
provider that it exposes as an output.
Open the tests/setup/main.tf
file to review the helper module.
Next, open the tests/website.tftest.hcl
file. This file defines the test assertions for the configuration and consists of a series of run
blocks, which Terraform executes sequentially. The first run block, named "setup_tests", runs a terraform apply
command on the setup
helper module to create the random bucket prefix. Each run block requires a unique name.
The second run block runs a terraform apply
command to create the S3 bucket. The test sets the required bucket_name
variable by referencing the output of the previous run block, run.setup_tests.bucket_prefix
. The block must reference the setup_tests
run block since that is where the state of the bucket_prefix
output exists. The run block then defines three assertions. The condition
of each assert block must evaluate to true, otherwise the test will fail and display the error_message
.
A run block may contain multiple assert
blocks, but every assert
block must evaluate to true for the run block to pass. Your decision to split multiple assert
blocks into separate run
blocks should be based on what is most clear to the module developers. Remember that every run
block performs either a terraform plan
or terraform apply
. In general, a run
block can be thought of as a step in a test, and each assert
block validates that step.
Run the tests
Initialize the Terraform configuration to install the required providers. Any time you add a new provider or module to your configuration or tests, you must run the terraform init
command.
Run terraform test
. Terraform authenticates the AWS provider defined in your tests using your provider credentials the same way it does for your regular configuration.
When you ran the tests, Terraform performed the following actions:
- Ran an apply on the
setup
helper module to create arandom_pet
resource. - Applied your main module to create the S3 bucket and upload the website files.
- Ran the three assertions to check the bucket name and the hashes of the
index.html
anderror.html
files. - Destroyed the test-specific S3 resources it created from the main configuration.
- Destroyed the helper module resources.
Create a new helper module
Now that you ran the existing tests, you will create your own test and helper module to verify that the static website is running and responding to requests. Your helper module will use the http
data source to make a request to the site and access its response.
Create a new directory for the helper module named final
.
Create a new file at tests/final/main.tf
and add the following configuration to it.
This helper module creates an http
data source and performs an HTTP GET request to the URL specified by the endpoint
variable.
Add a new test
Add the following run block to the end of the tests/website.tftest.hcl
file.
This test uses the final
helper module and references the website_endpoint
output from the main module for the endpoint
variable. It also defines one assert block to check that the HTTP GET request responds with a 200 status code, indicating that the website is running properly.
Next, run the terraform init
command to initialize your new final
module.
Next, rerun the tests.
Terraform ran the new test and verified that the website is running as expected.
As your module configuration evolves, you can use tests to confirm your assumptions and ensure predictability for your module consumers.
Push these changes to your GitHub repository. First, stage your changes.
Then, commit the changes to your local main branch.
Finally, push the changes to the remote main branch.
Publish the module
To publish your module, navigate to your organization's HCP Terraform registry, click Publish, then select Module.
Select your version control provider, then select your terraform-aws-s3-website-tests
repository.
On the Add Module screen, choose Branch for the module publish type and provide the following values.
Field | Value |
---|---|
Module Publish Type | Branch |
Branch Name | "main" |
Module Version | "1.0.0" |
Check Enable testing for Module, then click Publish module
When you publish a module using the branch-based workflow, HCP Terraform displays a Branch-Based
badge next to the module name, as well as the branch and commit SHA that your module version references.
Configure the module tests
Since the tests for this module deploy resources to AWS, you must provide credentials for the tests to use. To configure environment variables for the test, click Configure Tests.
Under Module variables, click + Add variable and add the following environment variables.
Key | Value | Sensitive |
---|---|---|
AWS_ACCESS_KEY_ID | Your AWS IAM Key ID | True |
AWS_SECRET_ACCESS_KEY | Your AWS IAM Key Secret | True |
AWS_REGION | us-east-2 | False |
Click Module overview to return to the module.
Update the module
Tests help you avoid introducing breaking changes to your modules. In this example, you will verify that it is safe to update your module's provider version.
Open the terraform.tf
file and update the AWS provider version to 5.16.0
.
Reinitialize your configuration with the -upgrade
flag to update your Terraform lock file with the new provider version.
If your module uses branch-based publishing and the module source code contains tests, HCP Terraform will automatically run them for every push to the configured branch and for any pull requests against that branch.
Commit the changes to your local main
branch.
Push the changes to the remote main
branch.
On your module's overview page, HCP Terraform reports your module's running tests a few moments after you push your changes.
Click View all tests. Here, HCP Terraform shows the history of all test runs, starting with the latest run.
Click the latest test run to view the details of the tests for your changes. Click the File: tests/website.tftest.hcl dropdown to see the status of each test step. If any of your tests fail, HCP Terraform will include the detailed output of that test so can troubleshoot the issue.
Run tests in HCP Terraform from the CLI
You can also run tests remotely in HCP Terraform from the CLI without committing your changes to source control. When you run tests remotely, HCP Terraform will use the environment variables that you have configured for your module, which helps secure your test runs and removes the need to store cloud credentials on your local machine. To test this local workflow, open the website.tftest.hcl
file and update the condition
of the website_is_running
test to check for a 404
status code rather than 200
. This will force the test to fail.
Next, run the terraform test
command with the cloud-run
flag. This flag tells Terraform to use your local configuration but execute your tests remotely. The cloud-run
flag is the path of the module in your private registry. Replace ORG
with your HCP Terraform organization.
Visit your module overview page in HCP Terraform and to confirm that your test status now shows Tests failed. Click View all tests, then click the latest test run to review the same test results that Terraform displayed in your terminal.
Mock tests
Terraform also lets you mock providers, resources, and data sources for your tests. This lets you simulate any resources and their attributes that your configuration depends on without actually creating ephemeral infrastructure for testing. When you use test mocking, Terraform automatically generates values for every computed field of your resources and data sources.
First, update your configuration in main.tf
to declare new resources.
This configuration creates an EC2 instance and RDS database to support a backend API for your application. These resources can have long provision times, which can slow down your tests. Since your tests do not test these resources directly, you can using mocking to access their attributes without having to create them.
Open the website.tftest.hcl
file and add the following override_resource
blocks.
The override_resource
blocks instruct Terraform to mock the resources at the address defined in the target
attribute. In this case, Terraform will mock the EC2 instance and RDS database instead of provisioning them in AWS.
Next, add the following run
block to your website.tftest.hcl
file.
Finally, run the tests.
Without mocking, your tests would take several minutes to complete because Terraform would need to wait for AWS to create the RDS database and EC2 instance. Since you enabled mocking using the override_resource
blocks, Terraform completed the tests quickly.
You can also use mocking for data sources. For example, your organization may have separate teams to manage networking and to deploy applications. In this scenario, the application team could use the tfe_outputs
data source to reference the infrastructure created by the networking team. For your tests, you can use the override_data
block to mock the values of the tfe_outputs
data source.
Review testing and validation
Tests differ from validation methods such as variable validation, preconditions, postconditions, and check blocks. These features focus on verifying the infrastructure deployed by your configuration, while tests validate the behavior and logic of your configuration itself.
Validation is like error checking for your Terraform configuration. When validation fails, the module consumer is responsible for resolving the issue. For example, a module author can define a precondition that prevents the user from using an invalid subnet CIDR block for a server. In this case, it is the consumers responsibility to provide the correct subnet.
Tests let module authors verify the behavior of the configuration and ensure that updates do not introduce breaking changes. They are comparable to unit and integration testing, and are a core part of the development cycle.
Next steps
In this tutorial, you learned how to write and run Terraform tests. You also learned two ways to run tests in HCP Terraform, keeping your test runs secure by centralizing your cloud credentials. Refer to the following resources to learn more about testing and validation.
- Read the Testing Terraform overview for more details about how to write tests.
- Learn more about the Terraform test syntax.
- Reference the test-integrated module documentation to learn more about running tests in HCP Terraform.
- Learn about custom Terraform conditions.
- Review the test mocks documentation.