Skip to content

Commit

Permalink
Migrate tests to Localstack (#112)
Browse files Browse the repository at this point in the history
* Migrate tests to Localstack

* Fix fmt + add non OSS version

* Parallel tests
  • Loading branch information
grem11n committed Feb 13, 2024
1 parent 8c9568d commit de195dc
Show file tree
Hide file tree
Showing 37 changed files with 332 additions and 308 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/terratest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
terraform: ['1.3.7']
terraform: ['1.5.7', '1.7.3']
steps:
- uses: actions/checkout@master
- uses: hashicorp/setup-terraform@v1
Expand All @@ -36,15 +36,13 @@ jobs:
name: terratest
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
terraform: ['1.3.7']
terraform: ['1.5.7', '1.7.3']
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.19'

go-version: '1.21'
- name: Checkout
uses: actions/checkout@v1
with:
Expand All @@ -58,13 +56,12 @@ jobs:
sudo unzip -d /tmp/ /tmp/terraform.zip &&
sudo mv /tmp/terraform /usr/local/bin/
- name: Start Localstack
run: docker-compose up -d

- name: Terratest
env:
GOPATH: /home/runner/work/terraform-aws-vpc-peering/go
TF_VAR_aws_peer_access_key: ${{ secrets.TF_VAR_aws_peer_access_key }}
TF_VAR_aws_peer_secret_key: ${{ secrets.TF_VAR_aws_peer_secret_key }}
TF_VAR_aws_this_access_key: ${{ secrets.TF_VAR_aws_this_access_key }}
TF_VAR_aws_this_secret_key: ${{ secrets.TF_VAR_aws_this_secret_key }}
run: >
go mod init github.com/grem11n/terraform-aws-vpc-peering &&
go mod tidy &&
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ go.sum

# Environment variables
.env

# Localstack
volume/
16 changes: 8 additions & 8 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ data "aws_subnet" "peer" {

# this vpc main route table
data "aws_route_table" "this_main_route_table" {
provider = aws.this
provider = aws.this
vpc_id = var.this_vpc_id
filter {
name = "association.main"
values = ["true"]
}
}
}

# this subnets
data "aws_subnets" "this" {
provider = aws.this
provider = aws.this
filter {
name = "vpc-id"
values = [var.this_vpc_id]
Expand All @@ -58,7 +58,7 @@ data "aws_subnets" "this" {

# get route tables associated with subnets
data "aws_route_tables" "this_associated_route_tables" {
for_each = { for subnet in data.aws_subnets.this.ids: subnet => subnet }
for_each = { for subnet in data.aws_subnets.this.ids : subnet => subnet }
provider = aws.this
vpc_id = var.this_vpc_id
filter {
Expand All @@ -72,17 +72,17 @@ data "aws_route_tables" "this_associated_route_tables" {

# peer vpc main route table
data "aws_route_table" "peer_main_route_table" {
provider = aws.peer
provider = aws.peer
vpc_id = var.peer_vpc_id
filter {
name = "association.main"
values = ["true"]
}
}
}

# peer subnets
data "aws_subnets" "peer" {
provider = aws.peer
provider = aws.peer
filter {
name = "vpc-id"
values = [var.peer_vpc_id]
Expand All @@ -91,7 +91,7 @@ data "aws_subnets" "peer" {

# get route tables associated with subnets
data "aws_route_tables" "peer_associated_route_tables" {
for_each = { for subnet in data.aws_subnets.peer.ids: subnet => subnet }
for_each = { for subnet in data.aws_subnets.peer.ids : subnet => subnet }
provider = aws.peer
vpc_id = var.peer_vpc_id
filter {
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: "3.8"
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
16 changes: 13 additions & 3 deletions examples/associated-cidrs/provider.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
region = "eu-west-1"
access_key = var.aws_this_access_key
secret_key = var.aws_this_secret_key
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "eu-west-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
9 changes: 0 additions & 9 deletions examples/associated-cidrs/variables.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
// Variables are required to pass them via Terratest
// on fixtures creation
variable "this_vpc_id" {}

variable "peer_vpc_id" {}

variable "aws_this_access_key" {
description = "AWS Access Key for requester account"
}

variable "aws_this_secret_key" {
description = "AWS Secret Key for requester account"
}
4 changes: 3 additions & 1 deletion examples/module-depends-on/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Using depends_on With This Module
# Using `depends_on` With This Module

**Warning**: There's a bug which prevents the creation of peering with the `depends_on` directove. Thus, Terraform should be executed twice to address that.

If you're using Terraform `>=0.13`, you [can use `depends_on` meta-argument with modules as well](https://www.terraform.io/docs/language/meta-arguments/depends_on.html)

Expand Down
16 changes: 13 additions & 3 deletions examples/module-depends-on/provider.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
region = "eu-west-1"
access_key = var.aws_this_access_key
secret_key = var.aws_this_secret_key
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "eu-west-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
8 changes: 0 additions & 8 deletions examples/module-depends-on/variables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
variable "aws_this_access_key" {
description = "AWS Access Key for requester account"
}

variable "aws_this_secret_key" {
description = "AWS Secret Key for requester account"
}

variable "this_subnets" {
description = "Subnet list for _this_ VPC"
type = list(string)
Expand Down
34 changes: 26 additions & 8 deletions examples/multi-account-multi-region/provider.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
alias = "this"
region = "eu-west-1"
access_key = var.aws_this_access_key
secret_key = var.aws_this_secret_key
alias = "this"
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "eu-west-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}

provider "aws" {
alias = "peer"
region = "eu-central-1"
access_key = var.aws_peer_access_key
secret_key = var.aws_peer_secret_key
alias = "peer"
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "eu-central-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
17 changes: 0 additions & 17 deletions examples/multi-account-multi-region/variables.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
// Variables are required to pass them via Terratest
// on fixtures creation
variable "this_vpc_id" {}

variable "peer_vpc_id" {}

variable "aws_this_access_key" {
description = "AWS Access Key for requester account"
}

variable "aws_this_secret_key" {
description = "AWS Secret Key for requester account"
}

variable "aws_peer_access_key" {
description = "AWS Access Key for accepter account"
}

variable "aws_peer_secret_key" {
description = "AWS Secret Key for accepter account"
}
34 changes: 26 additions & 8 deletions examples/multi-account-single-region/provider.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
alias = "this"
region = "ap-southeast-2"
access_key = var.aws_this_access_key
secret_key = var.aws_this_secret_key
alias = "this"
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "ap-southeast-2"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}

provider "aws" {
alias = "peer"
region = "ap-southeast-2"
access_key = var.aws_peer_access_key
secret_key = var.aws_peer_secret_key
alias = "peer"
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
region = "ap-southeast-2"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}
17 changes: 0 additions & 17 deletions examples/multi-account-single-region/variables.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
// Variables are required to pass them via Terratest
// on fixtures creation
variable "this_vpc_id" {}

variable "peer_vpc_id" {}

variable "aws_this_access_key" {
description = "AWS Access Key for requester account"
}

variable "aws_this_secret_key" {
description = "AWS Secret Key for requester account"
}

variable "aws_peer_access_key" {
description = "AWS Access Key for accepter account"
}

variable "aws_peer_secret_key" {
description = "AWS Secret Key for accepter account"
}
32 changes: 22 additions & 10 deletions examples/partial-subnets/provider.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// This provider example is designed to work with Localstack.
// You need to have a real AWS provider configuration for the production usage.
provider "aws" {
alias = "this"
region = var.this_region != "" ? var.this_region : "eu-west-2"
assume_role {
role_arn = var.this_assume_role_arn != "" ? var.this_assume_role_arn : null
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
access_key = var.aws_this_access_key != "" ? var.aws_this_access_key : null
secret_key = var.aws_this_secret_key != "" ? var.aws_this_secret_key : null
region = "eu-west-2"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}

provider "aws" {
alias = "peer"
region = var.peer_region != "" ? var.peer_region : "eu-central-1"
assume_role {
role_arn = var.peer_assume_role_arn != "" ? var.peer_assume_role_arn : null
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
sts = "http://localhost:4566"
}
access_key = var.aws_peer_access_key != "" ? var.aws_peer_access_key : null
secret_key = var.aws_peer_secret_key != "" ? var.aws_peer_secret_key : null
region = "eu-central-1"
access_key = "null"
secret_key = "null"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
}

Loading

0 comments on commit de195dc

Please sign in to comment.