- 🌍 Localstack Terraform Test Runner 🚀
- 🎯 Purpose
- 🔧 Installation
- 🏃 How to Run
- 🔍 How to Run Test Cases
- 🔢 Default Environment Variables for Terraform Tests
- ⚙️ Options
- 🔐 Services
This utility serves as a test runner designed specifically for Localstack and Terraform. By leveraging it, users can execute test cases from the Hashicorp Terraform provider AWS against a Localstack Instance.
The primary objective behind this project is to segregate test cases from the Localstack repo and execute them against Localstack. This helps in obtaining parity metrics.
- 📦 Clone the repository (including submodules):
git clone git@github.com:localstack/localstack-terraform-test.git --recurse-submodules
- 🔀 Ensure you're on the latest version of the submodules:
git submodule update --init --recursive
- 🚀 Install dependencies:
make install
- 🔑 (Pro-image only) Set the
LOCALSTACK_API_KEY
environment variable. - Apply the patch to the Terraform provider AWS:
python -m terraform_pytest.main patch
- Construct a testing binary for the Golang module:
python -m terraform_pytest.main build -s s3
- Now you're all set to utilize the
python -m pytest
commands to list and execute test cases derived from Golang.
- 📋 List all test cases from a specific service:
python -m pytest terraform-provider-aws/internal/service/<service> --collect-only -q
- 🚀 Execute a particular test case:
python -m pytest terraform-provider-aws/internal/service/<service>/<test-file> -k <test-case-name> --ls-start
or
python -m pytest terraform-provider-aws/internal/service/<service>/<test-file>::<test-case-name> --ls-start
- You can prepend additional environment variables to the command. For instance:
AWS_ALTERNATE_REGION='us-west-2' python -m pytest terraform-provider-aws/internal/service/<service>/<test-file>::<test-case-name> --ls-start
To run custom terratest test cases, you can run:
python -m terraform_pytest.main terratest-tests
- Go into the
terratest
folder (which contains a regular go module) - Go into the directory where your service of interest is (e.g.
ec2
), or create it if it doesn't exist - Create a golang test file with a naming like
{your_use_case_of_interest}_test.go
replacing{your_use_case_of_interest}
with something meaningful. - Create an associated directory that will host the required Terraform files (e.g.
{your-use-case-of-interest}/
), and put yourmain.tf
,variables.tf
and whatever you require for your test scenario. - Add actual testing logic to your golang file, which can be as simple as the following (and can involve logic as complex as you wish, interacting with the components created by Terraform):
package test
import (
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/localstack/localstack-terraform-test/terratest"
"testing"
)
func TestUseCaseOfInterest(t *testing.T) {
terratest.SetUpFakeAWSCredentials(t)
awsRegion := "us-east-1"
terraformOptions := &terraform.Options{
TerraformDir: "./use-case-of-interest/",
Vars: map[string]interface{}{
"region": awsRegion,
},
}
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}
Variable | Default Value |
---|---|
TF_ACC |
1 |
AWS_ACCESS_KEY_ID |
test |
AWS_SECRET_ACCESS_KEY |
test |
AWS_DEFAULT_REGION |
us-west-1 |
AWS_ALTERNATE_ACCESS_KEY_ID |
test |
AWS_ALTERNATE_SECRET_ACCESS_KEY |
test |
AWS_ALTERNATE_REGION |
us-east-2 |
AWS_THIRD_SECRET_ACCESS_KEY |
test |
AWS_THIRD_ACCESS_KEY_ID |
test |
AWS_THIRD_REGION |
eu-west-1 |
--ls-start
: Initializes the Localstack instance before test case execution. It triggers the CLI:
localstack start -d
--gather-metrics
: Gathers raw test metrics for a specific run. But first, make sure you manually install the extension:
localstack extensions init
localstack extensions install "git+https://github.com/localstack/localstack-moto-test-coverage/#egg=collect-raw-metric-data-extension&subdirectory=collect-raw-metric-data-extension"
Remember to set the SERVICE
environment variable for naming the metric file.
Executing this test suite is a time-intensive process. To cater to this, the following mechanisms are in place:
Mechanism | Description |
---|---|
Blacklisting | Services devoid of tests are blacklisted to avoid needless execution. |
Ignored | Services might have test cases, but if they all fail leading to timeouts, they're marked as non-functional and bypassed. Refer to terraform_pytest/utils.py . |
Partitioning | Some services are extensive and get divided into partitions. Each partition holds a unique subset of tests for that particular service. |