Skip to content

Commit

Permalink
Refactor tests and add shared helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcurtis committed Nov 3, 2024
1 parent b1a7da7 commit 21e6474
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 158 deletions.
Submodule v0.1.0 added at 97964a
4 changes: 2 additions & 2 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ changelog:
labels:
- dependencies

- title: 🔩 Dependencies
- title: 🔩 Dependencies
labels:
- dependencies

# This file is managed by the osinfra-io/github-organization-management repository and should not be edited directly.
# This file is managed by the osinfra-io/github-organization-management repository and should not be edited directly.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ crash.log
# be included in version control.
local.tfvars

# Provider.tf is used for local development of modules and shouldn't be added to repos.
provider.tf

# Ignore override files as they are usually used to override ressources locally
override.tf
override.tf.json
Expand Down
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.96.1
rev: v1.96.2
hooks:
- id: terraform_fmt

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

- repo: https://github.com/bridgecrewio/checkov.git
rev: 3.2.257
rev: 3.2.277
hooks:
- id: checkov
verbose: true
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_helm"></a> [helm](#provider\_helm) | 2.16.0 |
| <a name="provider_helm"></a> [helm](#provider\_helm) | 2.16.1 |

## Modules

Expand Down
6 changes: 3 additions & 3 deletions regional/helm/cert-manager.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
crds:
enabled: true

global:
commonLabels:
tags.datadoghq.com/source: cert-manager

crds:
enabled: true
1 change: 1 addition & 0 deletions regional/helpers.tf
2 changes: 1 addition & 1 deletion regional/istio-csr/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_helm"></a> [helm](#provider\_helm) | 2.16.0 |
| <a name="provider_helm"></a> [helm](#provider\_helm) | 2.16.1 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.33.0 |

## Modules
Expand Down
1 change: 1 addition & 0 deletions regional/istio-csr/helpers.tf
30 changes: 0 additions & 30 deletions regional/istio-csr/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@

locals {
cluster_id = local.zone != null ? "${var.cluster_prefix}-${local.region}-${local.zone}-${local.env}" : "${var.cluster_prefix}-${local.region}-${local.env}"
env = lookup(local.env_map, local.environment, "none")

environment = (
terraform.workspace == "default" ?
"mock-environment" :
regex(".*-(?P<environment>[^-]+)$", terraform.workspace)["environment"]
)

env_map = {
"non-production" = "nonprod"
"production" = "prod"
"sandbox" = "sb"
}

helm_values = {
"app.server.clusterID" = local.cluster_id
Expand All @@ -26,21 +13,4 @@ locals {
"resources.requests.cpu" = var.resources_requests_cpu
"resources.requests.memory" = var.resources_requests_memory
}

region = (
terraform.workspace == "default" ?
"mock-region" :
regex("^(?P<region>[^-]+-[^-]+)", terraform.workspace)["region"]
)


zone = (
terraform.workspace == "default" ?
"mock-zone" :
(
regex("^(?P<region>[^-]+-[^-]+)(?:-(?P<zone>[^-]+))?-.*$", terraform.workspace)["zone"] != "" ?
regex("^(?P<region>[^-]+-[^-]+)(?:-(?P<zone>[^-]+))?-.*$", terraform.workspace)["zone"] :
null
)
)
}
14 changes: 0 additions & 14 deletions regional/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@
# https://www.terraform.io/docs/language/values/locals.html

locals {
env = lookup(local.env_map, local.environment, "none")

environment = (
terraform.workspace == "default" ?
"mock-environment" :
regex(".*-(?P<environment>[^-]+)$", terraform.workspace)["environment"]
)

env_map = {
"non-production" = "nonprod"
"production" = "prod"
"sandbox" = "sb"
}

helm_values = {
"cainjector.podLabels.tags\\.datadoghq\\.com/service" = "cert-manager-cainjector"
"cainjector.resources.limits.cpu" = var.cain_injector_resources_limits_cpu
Expand Down
33 changes: 33 additions & 0 deletions shared/helpers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Terraform Core Child Module Helpers

locals {
env = lookup(local.env_map, local.environment, "none")

environment = (
terraform.workspace == "default" ?
"mock-environment" :
regex(".*-(?P<environment>[^-]+)$", terraform.workspace)["environment"]
)

env_map = {
"non-production" = "nonprod"
"production" = "prod"
"sandbox" = "sb"
}

region = (
terraform.workspace == "default" ?
"mock-region" :
regex("^(?P<region>[^-]+-[^-]+)", terraform.workspace)["region"]
)

zone = (
terraform.workspace == "default" ?
"mock-zone" :
(
regex("^(?P<region>[^-]+-[^-]+)(?:-(?P<zone>[^-]+))?-.*$", terraform.workspace)["zone"] != "" ?
regex("^(?P<region>[^-]+-[^-]+)(?:-(?P<zone>[^-]+))?-.*$", terraform.workspace)["zone"] :
null
)
)
}
6 changes: 0 additions & 6 deletions tests/fixtures/default/regional/istio-csr/locals.tf

This file was deleted.

45 changes: 0 additions & 45 deletions tests/fixtures/default/regional/istio-csr/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,6 @@ terraform {
}
}

# Helm Provider
# https://registry.terraform.io/providers/hashicorp/helm/latest

provider "helm" {
kubernetes {

cluster_ca_certificate = base64decode(
local.regional.cluster_ca_certificate
)

host = local.regional.cluster_endpoint
token = data.google_client_config.current.access_token
}
}

# Kubernetes Provider
# https://registry.terraform.io/providers/hashicorp/kubernetes/latest

provider "kubernetes" {
cluster_ca_certificate = base64decode(
local.regional.cluster_ca_certificate
)

host = "https://${local.regional.cluster_endpoint}"
token = data.google_client_config.current.access_token
}

# Google Client Config Data Source
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_config

data "google_client_config" "current" {
}

# Remote State Data Source
# https://www.terraform.io/language/state/remote-state-data

data "terraform_remote_state" "regional" {
backend = "gcs"
workspace = "mock-workspace"

config = {
bucket = "mock-bucket"
}
}

module "test" {
source = "../../../../../regional/istio-csr"

Expand Down
6 changes: 0 additions & 6 deletions tests/fixtures/default/regional/locals.tf

This file was deleted.

45 changes: 0 additions & 45 deletions tests/fixtures/default/regional/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,6 @@ terraform {
}
}

# Helm Provider
# https://registry.terraform.io/providers/hashicorp/helm/latest

provider "helm" {
kubernetes {

cluster_ca_certificate = base64decode(
local.regional.cluster_ca_certificate
)

host = local.regional.cluster_endpoint
token = data.google_client_config.current.access_token
}
}

# Kubernetes Provider
# https://registry.terraform.io/providers/hashicorp/kubernetes/latest

provider "kubernetes" {
cluster_ca_certificate = base64decode(
local.regional.cluster_ca_certificate
)

host = "https://${local.regional.cluster_endpoint}"
token = data.google_client_config.current.access_token
}

# Google Client Config Data Source
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_config

data "google_client_config" "current" {
}

# Remote State Data Source
# https://www.terraform.io/language/state/remote-state-data

data "terraform_remote_state" "regional" {
backend = "gcs"
workspace = "mock-workspace"

config = {
bucket = "mock-bucket"
}
}

module "test" {
source = "../../../../regional"

Expand Down

0 comments on commit 21e6474

Please sign in to comment.