Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom terratest tests #36

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ target
report.xml
volume
terraform-provider-aws
test-bin/*
test-bin/*
.terraform.lock.hcl
**/terraform.tfstate*
.terraform/
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ venv: $(VENV_ACTIVATE) ## Create a new (empty) virtual environment

install: venv ## Install the package in editable mode
$(VENV_RUN); $(PIP_CMD) install -r requirements.txt
@terraform -v >/dev/null 2>&1 || { echo >&2 "Terraform is not installed. Please install it by following the guide at https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli"; exit 1; }

init_precommit: ## Install the pre-commit hook into your local git repository
($(VENV_RUN); pre-commit install)
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,44 @@ python -m pytest terraform-provider-aws/internal/service/<service>/<test-file>::
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
```

---

## 🕵️ **How to Add new terratest tests**

1. Go into the `terratest` folder (which contains a regular go module)
2. Go into the directory where your service of interest is (e.g. `ec2`), or create it if it doesn't exist
3. 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.
4. Create an associated directory that will host the required Terraform files (e.g. `{your-use-case-of-interest}/`), and put your `main.tf`, `variables.tf` and whatever you require for your test scenario.
5. 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):
```go
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)
}

```

## 🔢 **Default Environment Variables for Terraform Tests**

| Variable | Default Value |
Expand Down
1 change: 1 addition & 0 deletions terraform_pytest/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
TF_REPO_NAME = "terraform-provider-aws"
TF_TEST_BINARY_FOLDER = "test-bin"
TF_REPO_SERVICE_FOLDER = "internal/service"
TERRATEST_PROJECT_FOLDER = "terratest"

# absolute path to the terraform repo
TF_REPO_PATH = os.path.realpath(TF_REPO_NAME)
Expand Down
25 changes: 23 additions & 2 deletions terraform_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

import click

from terraform_pytest.constants import TF_REPO_PATH, TF_TEST_BINARY_PATH
from terraform_pytest.utils import build_test_binary, get_services, patch_repository
from terraform_pytest.constants import (
TF_REPO_PATH,
TF_TEST_BINARY_PATH,
TERRATEST_PROJECT_FOLDER,
)
from terraform_pytest.utils import (
build_test_binary,
get_services,
patch_repository,
run_terratest_tests,
)

logging.basicConfig(level=logging.INFO)

Expand Down Expand Up @@ -64,8 +73,20 @@ def clean_command():
logging.info("Done")


@click.command(name="terratest-tests", help="Run Golang Terratest tests")
def terratest_tests():
logging.info(f"Running terratest tests from {TERRATEST_PROJECT_FOLDER} directory")

try:
_, output = run_terratest_tests(TERRATEST_PROJECT_FOLDER)
print(output)
except Exception as e:
logging.error(f"Failed to execute terratest tests: {str(e)}")


if __name__ == "__main__":
cli.add_command(build_command)
cli.add_command(patch_command)
cli.add_command(clean_command)
cli.add_command(terratest_tests)
cli()
33 changes: 32 additions & 1 deletion terraform_pytest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def execute_command(

try:
process = subprocess.run(
cmd, env=env_vars, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
cmd,
env=env_vars,
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return_code = process.returncode
output = process.stdout + "\n" + process.stderr
Expand Down Expand Up @@ -155,3 +160,29 @@ def patch_repository():
else:
if stdout:
logging.info(f"{patch_file} has been patched successfully.")


def run_terratest_tests(terratest_path: str) -> Tuple[int, str]:
"""
Run the Golang Terratest tests for the specified project.

:param str terratest_path: Path to the Terratest project
:return: Tuple[int, str]
Return code and combined stdout and stderr
"""
logging.info(f"Initiating Golang Terratest in the directory: {terratest_path}")

# Command to execute Golang tests
test_cmd = ["go", "test", "./..."]

# Run the tests and capture output
return_code, output = execute_command(test_cmd, cwd=terratest_path)

# Check for errors and log appropriately
if return_code != 0:
logging.error("Golang tests failed.")
logging.error(output)
else:
logging.info("Golang tests completed successfully.")

return return_code, output
68 changes: 68 additions & 0 deletions terratest/dynamo-db-table-creation/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
provider "aws" {
region = var.region
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true

endpoints {
apigateway = "http://localhost:4566"
cloudformation = "http://localhost:4566"
cloudwatch = "http://localhost:4566"
dynamodb = "http://localhost:4566"
es = "http://localhost:4566"
firehose = "http://localhost:4566"
iam = "http://localhost:4566"
kinesis = "http://localhost:4566"
lambda = "http://localhost:4566"
route53 = "http://localhost:4566"
redshift = "http://localhost:4566"
s3 = "http://localhost:4566"
secretsmanager = "http://localhost:4566"
ses = "http://localhost:4566"
sns = "http://localhost:4566"
sqs = "http://localhost:4566"
ssm = "http://localhost:4566"
stepfunctions = "http://localhost:4566"
sts = "http://localhost:4566"
ec2 = "http://localhost:4566"
}
}

terraform {
required_version = ">= 0.12.26"
}

resource "aws_dynamodb_table" "example" {
name = var.table_name
hash_key = "userId"
range_key = "department"
billing_mode = "PAY_PER_REQUEST"

server_side_encryption {
enabled = true
}

attribute {
name = "userId"
type = "S"
}
attribute {
name = "department"
type = "S"
}

ttl {
enabled = true
attribute_name = "expires"
}

tags = {
Environment = "production"
}
}

variable "table_name" {
description = "The name to set for the dynamoDB table."
type = string
default = "terratest-example"
}
5 changes: 5 additions & 0 deletions terratest/dynamo-db-table-creation/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "region" {
description = "The AWS region to create resources in."
type = string
default = "us-east-1"
}
21 changes: 21 additions & 0 deletions terratest/dynamo_db_table_creation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package terratest

import (
"github.com/gruntwork-io/terratest/modules/terraform"
"testing"
)

// From issue: https://github.com/localstack/localstack/issues/4716

func TestDynamoDbTableCreation(t *testing.T) {
t.Parallel()
awsRegion := "us-east-1"
terraformOptions := &terraform.Options{
TerraformDir: "./dynamo-db-table-creation/",
Vars: map[string]interface{}{
"region": awsRegion,
},
}
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}
98 changes: 98 additions & 0 deletions terratest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module github.com/localstack/localstack-terraform-test/terratest

go 1.22.3

require (
github.com/aws/aws-lambda-go v1.13.3
github.com/aws/aws-sdk-go v1.44.122
github.com/gruntwork-io/terratest v0.46.13
github.com/stretchr/testify v1.9.0
)

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.28.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gruntwork-io/go-commons v0.8.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/urfave/cli v1.22.2 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/apimachinery v0.28.4 // indirect
k8s.io/client-go v0.28.4 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading
Loading