Skip to content

Commit

Permalink
Terraform testing alignment (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcurtis authored Aug 7, 2024
1 parent b0286d4 commit 0ad878d
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 101 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: check-symlinks

- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.92.0
rev: v1.92.1
hooks:
- id: terraform_fmt

Expand All @@ -29,7 +29,7 @@ repos:
- id: terraform_docs

- repo: https://github.com/bridgecrewio/checkov.git
rev: 3.2.213
rev: 3.2.219
hooks:
- id: checkov
verbose: true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ terraform test

| Name | Version |
|------|---------|
| google | 5.38.0 |
| google | 5.40.0 |

### Resources

Expand Down
2 changes: 1 addition & 1 deletion regional/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ No requirements.

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | 5.38.0 |
| <a name="provider_google"></a> [google](#provider\_google) | 5.40.0 |

## Modules

Expand Down
2 changes: 1 addition & 1 deletion regional/nat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ No requirements.

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | 5.38.0 |
| <a name="provider_google"></a> [google](#provider\_google) | 5.40.0 |

## Modules

Expand Down
30 changes: 8 additions & 22 deletions tests/default.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ mock_provider "google" {}
run "default" {
command = apply

module {
module {
source = "./tests/fixtures/default"
}

assert {
condition = module.test.name == "test-vpc"
error_message = "The VPC name does not match the expected value 'test-vpc'."
}
# Example assertion

assert {
condition = module.test.project == "test-default-tf75-sb"
error_message = "The project ID does not match the expected value 'test-default-tf75-sb'."
}

variables {
project = "test-default-tf75-sb"
condition = module.test.name == "mock-vpc"
error_message = "The VPC name does not match the expected value 'mock-vpc'."
}
}

Expand All @@ -28,10 +21,6 @@ run "dns" {
module {
source = "./tests/fixtures/default/dns"
}

variables {
project = "test-default-tf75-sb"
}
}

run "regional" {
Expand All @@ -40,10 +29,6 @@ run "regional" {
module {
source = "./tests/fixtures/default/regional"
}

variables {
project = "test-default-tf75-sb"
}
}

run "regional_nat" {
Expand All @@ -52,8 +37,9 @@ run "regional_nat" {
module {
source = "./tests/fixtures/default/regional_nat"
}
}

variables {
project = "test-default-tf75-sb"
}
variables {
project = "mock-project"
environment = "mock-environment"
}
12 changes: 12 additions & 0 deletions tests/fixtures/default/dns/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Local Values
# https://www.terraform.io/docs/language/values/locals.html

locals {
labels = {
cost-center = "mock-x001"
env = var.environment
repository = "mock-repository"
platform = "mock-platform"
team = "mock-team"
}
}
31 changes: 7 additions & 24 deletions tests/fixtures/default/dns/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Required Providers
# https://developer.hashicorp.com/terraform/language/providers/requirements

terraform {
required_providers {
google = {
Expand All @@ -12,34 +9,20 @@ terraform {
module "public_test" {
source = "../../../../dns"

dns_name = "gcp.osinfra.io."

labels = {
cost-center = "x000"
env = "sb"
repository = "terraform-google-cloud-dns"
team = "testing"
}

name = "gcp-osinfra-io"
dns_name = "mock-subdomain.mock-domain.io."
labels = local.labels
name = "mock-subdomain-mock-domain"
project = var.project
visibility = "public"
}

module "private_test" {
source = "../../../../dns"

dns_name = "gcp-priv.osinfra.io."

labels = {
cost-center = "x000"
env = "sb"
repository = "terraform-google-cloud-dns"
team = "testing"
}

name = "gcp-priv-osinfra-io"
private_visibility_config_networks = ["https://www.googleapis.com/compute/v1/projects/${var.project}/global/networks/terraform-test-vpc"]
dns_name = "mock-priv-subdomain.mock-domain."
labels = local.labels
name = "mock-priv-subdomain-mock-domain"
private_visibility_config_networks = ["https://www.googleapis.com/compute/v1/projects/${var.project}/global/networks/mock-vpc"]
project = var.project
visibility = "private"
}
7 changes: 5 additions & 2 deletions tests/fixtures/default/dns/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
variable "environment" {
type = string
}

variable "project" {
type = string
default = "test-default-tf75-sb"
type = string
}
12 changes: 12 additions & 0 deletions tests/fixtures/default/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Local Values
# https://www.terraform.io/docs/language/values/locals.html

locals {
labels = {
cost-center = "mock-x001"
env = var.environment
repository = "mock-repository"
platform = "mock-platform"
team = "mock-team"
}
}
12 changes: 3 additions & 9 deletions tests/fixtures/default/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Required Providers
# https://developer.hashicorp.com/terraform/language/providers/requirements

terraform {
required_providers {
google = {
Expand All @@ -12,7 +9,7 @@ terraform {
module "test" {
source = "../../.."

name = "test-vpc"
name = "mock-vpc"
project = var.project

# The following rules test each of the dynamic blocks in various combinations. This should
Expand All @@ -33,7 +30,7 @@ module "test" {

direction = "INGRESS"

name = "test-01"
name = "mock-rule-01"

ranges = [
"130.211.0.0/22",
Expand Down Expand Up @@ -70,8 +67,5 @@ module "test" {
}
]

# In order to test this we would need a new project created in the google-cloud-terraform-testing
# repository. The existing projects are already a shared VPC host or service projects.

shared_vpc = false
shared_vpc = true
}
12 changes: 12 additions & 0 deletions tests/fixtures/default/regional/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Local Values
# https://www.terraform.io/docs/language/values/locals.html

locals {
labels = {
cost-center = "mock-x001"
env = var.environment
repository = "mock-repository"
platform = "mock-platform"
team = "mock-team"
}
}
13 changes: 5 additions & 8 deletions tests/fixtures/default/regional/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Required Providers
# https://developer.hashicorp.com/terraform/language/providers/requirements

terraform {
required_providers {
google = {
Expand All @@ -13,15 +10,15 @@ module "test" {
source = "../../../../regional"

ip_cidr_range = "10.60.32.0/20"
name = "terraform-test-subnet-us-east1"
network = "terraform-test-vpc"
name = "mock-subnet-us-east1"
network = "mock-vpc"

# When enabled, VMs in this subnetwork without external IP addresses can access Google APIs and
# services by using Private Google Access. This is required for private Kubernetes clusters.

private_ip_google_access = true
project = var.project
region = "us-east1"
region = "mock-region"

# Secondary ranges are used to allocate IP addresses to resources in a subnetwork. In this example we create Pod IP address ranges
# and Service (ClusterIP) address ranges for a VPC-native cluster.
Expand All @@ -30,11 +27,11 @@ module "test" {

secondary_ip_ranges = [
{
range_name = "k8s-services-us-east1"
range_name = "mock-secondary-services"
ip_cidr_range = "10.61.16.0/20"
},
{
range_name = "k8s-pods-us-east1"
range_name = "mock-secondary-pods"
ip_cidr_range = "10.8.0.0/14"
}
]
Expand Down
3 changes: 0 additions & 3 deletions tests/fixtures/default/regional/outputs.tf

This file was deleted.

7 changes: 5 additions & 2 deletions tests/fixtures/default/regional/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
variable "environment" {
type = string
}

variable "project" {
type = string
default = "test-default-tf75-sb"
type = string
}
12 changes: 12 additions & 0 deletions tests/fixtures/default/regional_nat/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Local Values
# https://www.terraform.io/docs/language/values/locals.html

locals {
labels = {
cost-center = "mock-x001"
env = var.environment
repository = "mock-repository"
platform = "mock-platform"
team = "mock-team"
}
}
15 changes: 6 additions & 9 deletions tests/fixtures/default/regional_nat/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Required Providers
# https://developer.hashicorp.com/terraform/language/providers/requirements

terraform {
required_providers {
google = {
Expand All @@ -12,18 +9,18 @@ terraform {
module "all_subnets_test" {
source = "../../../../regional/nat"

network = "terraform-test-vpc"
network = "mock-vpc"
project = var.project
region = "us-east1"
region = "mock-region"
}

module "list_of_subnets_test" {
source = "../../../../regional/nat"

network = "terraform-test-vpc"
prefix = "terraform-test-vpc-subnetworks"
network = "mock-vpc"
prefix = "mock-vpc-subnetworks"
project = var.project
region = "us-east1"
region = "mock-region"
source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
subnetwork_names = ["fleet-member-us-east1"]
subnetwork_names = ["mock-subnetwork"]
}
10 changes: 0 additions & 10 deletions tests/fixtures/default/regional_nat/outputs.tf

This file was deleted.

8 changes: 4 additions & 4 deletions tests/fixtures/default/regional_nat/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Terraform Input Variables
# https://www.terraform.io/language/values/variables
variable "environment" {
type = string
}

variable "project" {
type = string
default = "test-default-tf75-sb"
type = string
}
10 changes: 7 additions & 3 deletions tests/fixtures/default/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
variable "project" {
description = "The ID of the project in which the resource belongs"
}
variable "environment" {
type = string
}

variable "project" {
type = string
}

0 comments on commit 0ad878d

Please sign in to comment.