GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - matthew-dresden/terraform-terratest-framework: Provider-agnostic Terratest-based testing framework for Terraform modules

BranchesTags
Open more actions menu

Latest commit

 

History

36 Commits
36 Commits

Folders and files

NameName
Last commit message
Last commit date
.devcontainer
.devcontainer
 
 
.github
.github
 
 
cmd/tftest
cmd/tftest
 
 
docs
docs
 
 
example
example
 
 
false
false
 
 
internal
internal
 
 
pkg
pkg
 
 
scripts
scripts
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.golangci-memory-profile.yml
.golangci-memory-profile.yml
 
 
.golangci.batch.yml
.golangci.batch.yml
 
 
.golangci.local.yml
.golangci.local.yml
 
 
.golangci.yml
.golangci.yml
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
.pre-commit-hooks.yaml
.pre-commit-hooks.yaml
 
 
.tool-versions
.tool-versions
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
NOTICE
NOTICE
 
 
README.md
README.md
 
 
VERSION
VERSION
 
 
go.mod
go.mod
 
 

Repository files navigation

Terraform Terratest Framework

A framework for testing Terraform modules using Terratest with a focus on functional testing and parallel execution of examples.

Overview

This framework provides a structured way to test Terraform modules by:

  1. Automatically discovering and running all examples in parallel
  2. Testing idempotency of Terraform code
  3. Providing common assertions for Terraform outputs and resources
  4. Supporting custom test functions for advanced verification
  5. Simplifying test setup and execution

Features

  • Parallel Example Testing: Run all examples in the examples/ directory in parallel
  • Flexible Parallelism Control: Control parallelism at both test fixture and individual test levels
  • Idempotency Testing: Verify that Terraform code is idempotent by running a plan after apply
  • Common Assertions: Pre-built assertions for common testing scenarios
  • Custom Tests: Support for custom test functions to verify specific resource behaviors
  • Automatic Discovery: Automatically finds and runs tests on all examples without manual configuration
  • Configurable: Easily customize test configurations for each example
  • Environment Control: Disable idempotency testing with the TERRATEST_IDEMPOTENCY=false environment variable

Development Environment Setup

Using Caylent Devcontainer (Recommended)

The easiest way to set up your development environment is using the Caylent Devcontainer:

  1. Install the Caylent Devcontainer CLI:

    pip install caylent-devcontainer-cli
  2. Set up the devcontainer in your project:

    cdevcontainer setup-devcontainer .

    Follow the interactive prompts to configure your environment.

  3. Launch VS Code with the devcontainer:

    cdevcontainer code

    Accept the prompt to reopen in container when VS Code launches.

For more detailed instructions on using the Caylent Devcontainer, run cdevcontainer --help or refer to the Caylent Devcontainer documentation.

Usage

Directory Structure

For details on the required directory structure, see the Directory Structure Documentation.

Running Tests

For details on installing and using the tftest CLI, see the CLI Usage Documentation.

Required Environment Variables

  • AWS Authentication: Tests require AWS credentials to be available

    export AWS_ACCESS_KEY_ID=your_access_key
    export AWS_SECRET_ACCESS_KEY=your_secret_key
    export AWS_REGION=us-west-2

    Or using AWS SSO:

    aws sso login --profile your-profile
    export AWS_PROFILE=your-profile
  • Test Control (optional):

    # To disable idempotency testing
    export TERRATEST_IDEMPOTENCY=false
    
    # To control parallelism of tests within fixtures
    export TERRATEST_DISABLE_PARALLEL_TESTS=true  # Disable parallel tests within fixtures

Writing Tests

For details on writing tests, see the Writing Tests Documentation.

TestCtx Package

The testctx package is the core of the framework, providing essential functionality for running and managing Terraform tests:

// Run a single example
ctx := testctx.RunSingleExample(t, "../../examples", "example1", testctx.TestConfig{
    Name: "example1-test",
    ExtraVars: map[string]interface{}{
        "region": "us-west-2",
    },
})

// Run all examples
results := testctx.RunAllExamples(t, "../../examples", nil)

// Run custom tests on all examples
testctx.RunCustomTests(t, results, verifyS3Bucket)

For detailed documentation on the testctx package, see the TestCtx Package Documentation.

Idempotency Testing

By default, the framework automatically runs idempotency tests for all Terraform examples. This ensures your Terraform code is idempotent (running it multiple times produces the same result).

The idempotency test:

  • Runs automatically when using RunExample or RunAllExamples functions
  • Verifies that running terraform plan after terraform apply shows no changes
  • Is enabled by default

To disable idempotency testing (useful when there are known issues with providers):

TERRATEST_IDEMPOTENCY=false tftest run

The test will run if:

  • TERRATEST_IDEMPOTENCY environment variable doesn't exist
  • TERRATEST_IDEMPOTENCY is set to any value other than "false"

The test will be skipped if:

  • TERRATEST_IDEMPOTENCY is set to "false"

Controlling Parallel Test Execution

You can control parallelism at two levels:

  1. Test Fixtures: Control whether different test fixtures run in parallel
  2. Tests Within Fixtures: Control whether tests within a single fixture run in parallel

By default, both levels of parallelism are disabled for maximum stability. You can enable them as needed:

# Run test fixtures in parallel
tftest run --parallel-fixtures=true

# Run tests within fixtures in parallel
tftest run --parallel-tests=true

# Enable both levels of parallelism
tftest run --parallel-fixtures=true --parallel-tests=true

Controlling parallelism is particularly useful when:

  • Tests interact with shared state files
  • Tests modify the same AWS resources
  • You're experiencing intermittent failures due to race conditions

You can also control test-level parallelism using an environment variable:

# Disable parallel tests within fixtures
export TERRATEST_DISABLE_PARALLEL_TESTS=true

Available Assertions

Basic Assertions

  • AssertIdempotent: Verifies that a Terraform plan shows no changes after apply
  • AssertOutputEquals: Checks if a specified Terraform output matches an expected value
  • AssertOutputContains: Checks if a specified Terraform output contains an expected substring
  • AssertOutputMatches: Checks if a specified Terraform output matches a regular expression
  • AssertOutputNotEmpty: Checks if a specified Terraform output is not empty
  • AssertOutputEmpty: Checks if a specified Terraform output is empty

File Assertions

  • AssertFileExists: Checks if a file exists at the path specified by the output_file_path Terraform output
  • AssertFileContent: Checks if the output_content Terraform output matches the expected value

Collection Assertions

  • AssertOutputMapContainsKey: Checks if a Terraform map output contains a specific key
  • AssertOutputMapKeyEquals: Checks if a key in a Terraform map output equals an expected value
  • AssertOutputListContains: Checks if a Terraform list output contains an expected value
  • AssertOutputListLength: Checks if a Terraform list output has the expected length

JSON Assertions

  • AssertOutputJSONContains: Checks if a JSON string output contains an expected key-value pair

Resource Assertions

  • AssertResourceExists: Checks if a specific resource exists in the Terraform state
  • AssertResourceCount: Checks if the number of resources of a specific type matches the expected count
  • AssertNoResourcesOfType: Checks that no resources of a specific type exist in the Terraform state

Environment Assertions

  • AssertTerraformVersion: Checks if the Terraform version meets the minimum required version

For detailed documentation on all assertions, including usage examples and requirements, see the Assertions Documentation.

Documentation

  • Directory Structure - Required directory structure for using the framework
  • CLI Usage - How to use the TFTest CLI
  • Writing Tests - Guide to writing tests with the framework
  • TestCtx Package - Documentation for the core testctx package
  • Standard Tests - Standard tests to run on every Terraform module
  • Assertions Documentation - Detailed guide to all built-in assertions
  • Testing Guide - Comprehensive guide to writing and organizing tests
  • Error Handling - Error handling strategy for the framework
  • Logging - Logging framework for the framework
  • Benchmarking - Benchmarking and performance optimization
  • CI/CD Pipeline - Overview of the CI/CD pipeline and release process
  • Contributing Guide - Guidelines for contributing to the project

Examples

For a complete working example of how to use this framework in a real Terraform module, see the Example Directory. This example demonstrates best practices for structuring and testing Terraform modules using this framework.

For simple test fixtures used by the framework's own tests, see the tests/terraform-module-test-fixtures directory.

Installation

To install the framework as a dependency in your Terraform module project:

go get github.com/caylent-solutions/terraform-terratest-framework

This adds the framework to your module's go.mod file, allowing you to use its testing capabilities in your Go test files.

To install the CLI tool:

go install github.com/caylent-solutions/terraform-terratest-framework/cmd/tftest

Development

Prerequisites

  • Go 1.23 or later - Required to run the Go tests
  • Terraform CLI - Required to execute the terraform commands used by the framework
  • AWS credentials (if testing AWS resources) - Required for deploying and testing AWS resources

Setup

make install-tools
make install

Running Tests

# Run all tests (unit tests and functional tests)
make test

# Run all tests including example tests
make test-all

# List all functional tests with descriptions
make list-functional-tests

# Run a specific functional test
FUNCTIONAL_TEST=TestName make run-specific-functional-test

The list-functional-tests command will show all available functional tests in the project. This is useful when you need to run a specific test or understand what tests are available.

The run-specific-functional-test command allows you to run a single functional test by name. You must set the FUNCTIONAL_TEST environment variable to the name of the test you want to run. For example:

FUNCTIONAL_TEST=TestCliVersion make run-specific-functional-test

Releasing

Releases are typically managed through the GitHub Actions pipeline. However, for manual releases outside of the pipeline, you can use the make release-manual command:

# Create a manual release based on commit messages
make release-manual

# Explicitly specify the version bump type
make release-manual TYPE=major
make release-manual TYPE=minor
make release-manual TYPE=patch

Note: This manual release process should only be used in exceptional circumstances when the automated pipeline cannot be used.

The release process automatically determines the version bump type based on conventional commit messages:

  • Major version bump is triggered by:
    • BREAKING CHANGE: prefix
    • Any prefix with an exclamation mark (!): feat!:, fix!:, refactor!:, etc.
  • Minor version bump is triggered by:
    • feat: or feature: prefix
  • Patch version bump is triggered by all other prefixes:
    • fix: - Bug fixes
    • docs: - Documentation changes
    • style: - Code style changes
    • refactor: - Code refactoring
    • test: - Test changes
    • chore: - Routine tasks
    • ci: - CI/CD changes
    • build: - Build system changes
    • perf: - Performance improvements

Contributing

We welcome contributions from both external contributors and Caylent team members. Please see our Contributing Guide for details on how to contribute to this project.

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

About

Provider-agnostic Terratest-based testing framework for Terraform modules

Resources

Readme
Apache-2.0 license

Contributing

Contributing
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.