From edda4152843eaba4dc71a08b2d0ed864007a0d03 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 9 Jan 2026 09:58:01 -0300 Subject: [PATCH 01/40] First static frontend version --- frontend/deployment/build_context | 69 ++++++ frontend/deployment/compose_modules | 72 ++++++ .../distribution/amplify/modules/main.tf | 205 ++++++++++++++++++ .../deployment/distribution/amplify/setup | 23 ++ .../distribution/blob-cdn/modules/main.tf | 163 ++++++++++++++ .../deployment/distribution/blob-cdn/setup | 19 ++ .../distribution/cloudfront/modules/data.tf | 3 + .../distribution/cloudfront/modules/locals.tf | 14 ++ .../distribution/cloudfront/modules/main.tf | 88 ++++++++ .../cloudfront/modules/outputs.tf | 44 ++++ .../cloudfront/modules/variables.tf | 26 +++ .../deployment/distribution/cloudfront/setup | 111 ++++++++++ .../distribution/firebase/modules/main.tf | 87 ++++++++ .../deployment/distribution/firebase/setup | 27 +++ .../distribution/gcs-cdn/modules/main.tf | 193 +++++++++++++++++ .../deployment/distribution/gcs-cdn/setup | 30 +++ .../static-web-apps/modules/main.tf | 107 +++++++++ .../distribution/static-web-apps/setup | 19 ++ frontend/deployment/do_tofu | 42 ++++ .../network/azure_dns/modules/main.tf | 79 +++++++ frontend/deployment/network/azure_dns/setup | 43 ++++ .../network/cloud_dns/modules/main.tf | 105 +++++++++ frontend/deployment/network/cloud_dns/setup | 43 ++++ .../network/route_53/modules/locals.tf | 4 + .../network/route_53/modules/main.tf | 23 ++ .../network/route_53/modules/outputs.tf | 14 ++ .../network/route_53/modules/variables.tf | 15 ++ frontend/deployment/network/route_53/setup | 118 ++++++++++ .../tofu_state/aws/modules/provider.tf | 20 ++ .../tofu_state/aws/modules/variables.tf | 14 ++ frontend/deployment/tofu_state/aws/setup | 35 +++ .../tofu_state/azure/modules/provider.tf | 17 ++ .../tofu_state/azure/modules/variables.tf | 9 + frontend/deployment/tofu_state/azure/setup | 46 ++++ .../tofu_state/gcp/modules/provider.tf | 26 +++ .../tofu_state/gcp/modules/variables.tf | 8 + frontend/deployment/tofu_state/gcp/setup | 37 ++++ frontend/deployment/workflows/blue_green.yaml | 2 + frontend/deployment/workflows/delete.yaml | 8 + frontend/deployment/workflows/finalize.yaml | 4 + frontend/deployment/workflows/initial.yaml | 29 +++ frontend/deployment/workflows/rollback.yaml | 4 + frontend/instance/workflows/list.yaml | 0 frontend/log/workflows/log.yaml | 0 frontend/metric/workflows/list.yaml | 0 frontend/metric/workflows/metric.yaml | 0 frontend/no_op | 3 + frontend/scope/workflows/create.yaml | 4 + frontend/scope/workflows/delete.yaml | 2 + frontend/scope/workflows/update.yaml | 2 + frontend/specs/actions/create-scope.json.tpl | 29 +++ .../specs/actions/delete-deployment.json.tpl | 33 +++ frontend/specs/actions/delete-scope.json.tpl | 29 +++ .../actions/finalize-blue-green.json.tpl | 33 +++ .../actions/rollback-deployment.json.tpl | 33 +++ .../specs/actions/start-blue-green.json.tpl | 33 +++ frontend/specs/actions/start-initial.json.tpl | 32 +++ frontend/specs/notification-channel.json.tpl | 34 +++ frontend/specs/scope-type-definition.json.tpl | 9 + frontend/specs/service-spec.json.tpl | 34 +++ 60 files changed, 2355 insertions(+) create mode 100644 frontend/deployment/build_context create mode 100755 frontend/deployment/compose_modules create mode 100644 frontend/deployment/distribution/amplify/modules/main.tf create mode 100755 frontend/deployment/distribution/amplify/setup create mode 100644 frontend/deployment/distribution/blob-cdn/modules/main.tf create mode 100755 frontend/deployment/distribution/blob-cdn/setup create mode 100644 frontend/deployment/distribution/cloudfront/modules/data.tf create mode 100644 frontend/deployment/distribution/cloudfront/modules/locals.tf create mode 100644 frontend/deployment/distribution/cloudfront/modules/main.tf create mode 100644 frontend/deployment/distribution/cloudfront/modules/outputs.tf create mode 100644 frontend/deployment/distribution/cloudfront/modules/variables.tf create mode 100755 frontend/deployment/distribution/cloudfront/setup create mode 100644 frontend/deployment/distribution/firebase/modules/main.tf create mode 100755 frontend/deployment/distribution/firebase/setup create mode 100644 frontend/deployment/distribution/gcs-cdn/modules/main.tf create mode 100755 frontend/deployment/distribution/gcs-cdn/setup create mode 100644 frontend/deployment/distribution/static-web-apps/modules/main.tf create mode 100755 frontend/deployment/distribution/static-web-apps/setup create mode 100644 frontend/deployment/do_tofu create mode 100644 frontend/deployment/network/azure_dns/modules/main.tf create mode 100755 frontend/deployment/network/azure_dns/setup create mode 100644 frontend/deployment/network/cloud_dns/modules/main.tf create mode 100755 frontend/deployment/network/cloud_dns/setup create mode 100644 frontend/deployment/network/route_53/modules/locals.tf create mode 100644 frontend/deployment/network/route_53/modules/main.tf create mode 100644 frontend/deployment/network/route_53/modules/outputs.tf create mode 100644 frontend/deployment/network/route_53/modules/variables.tf create mode 100755 frontend/deployment/network/route_53/setup create mode 100644 frontend/deployment/tofu_state/aws/modules/provider.tf create mode 100644 frontend/deployment/tofu_state/aws/modules/variables.tf create mode 100755 frontend/deployment/tofu_state/aws/setup create mode 100644 frontend/deployment/tofu_state/azure/modules/provider.tf create mode 100644 frontend/deployment/tofu_state/azure/modules/variables.tf create mode 100755 frontend/deployment/tofu_state/azure/setup create mode 100644 frontend/deployment/tofu_state/gcp/modules/provider.tf create mode 100644 frontend/deployment/tofu_state/gcp/modules/variables.tf create mode 100755 frontend/deployment/tofu_state/gcp/setup create mode 100644 frontend/deployment/workflows/blue_green.yaml create mode 100644 frontend/deployment/workflows/delete.yaml create mode 100644 frontend/deployment/workflows/finalize.yaml create mode 100644 frontend/deployment/workflows/initial.yaml create mode 100644 frontend/deployment/workflows/rollback.yaml create mode 100644 frontend/instance/workflows/list.yaml create mode 100644 frontend/log/workflows/log.yaml create mode 100644 frontend/metric/workflows/list.yaml create mode 100644 frontend/metric/workflows/metric.yaml create mode 100644 frontend/no_op create mode 100644 frontend/scope/workflows/create.yaml create mode 100644 frontend/scope/workflows/delete.yaml create mode 100644 frontend/scope/workflows/update.yaml create mode 100644 frontend/specs/actions/create-scope.json.tpl create mode 100644 frontend/specs/actions/delete-deployment.json.tpl create mode 100644 frontend/specs/actions/delete-scope.json.tpl create mode 100644 frontend/specs/actions/finalize-blue-green.json.tpl create mode 100644 frontend/specs/actions/rollback-deployment.json.tpl create mode 100644 frontend/specs/actions/start-blue-green.json.tpl create mode 100644 frontend/specs/actions/start-initial.json.tpl create mode 100644 frontend/specs/notification-channel.json.tpl create mode 100644 frontend/specs/scope-type-definition.json.tpl create mode 100644 frontend/specs/service-spec.json.tpl diff --git a/frontend/deployment/build_context b/frontend/deployment/build_context new file mode 100644 index 00000000..b7100019 --- /dev/null +++ b/frontend/deployment/build_context @@ -0,0 +1,69 @@ +#!/bin/bash + +# ============================================================================= +# Build Context - Initializes TOFU_VARIABLES and prepares module composition +# ============================================================================= + +application_slug=$(echo "$CONTEXT" | jq -r .application.slug) +scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +scope_id=$(echo "$CONTEXT" | jq -r .scope.id) +repository_url=$(echo "$CONTEXT" | jq -r .application.repository_url) +application_version="$(echo "$CONTEXT" | jq -r .release.semver)" +env_vars_json=$(echo "$CONTEXT" | jq '.parameters.results | map({(.variable): .values[0].value}) | add') +resource_tags_json=$(echo "$CONTEXT" | jq \ + '{ + nullplatform: "true", + account: .account.slug, + account_id: .account.id, + namespace: .namespace.slug, + namespace_id: .namespace.id, + application: .application.slug, + application_id: .application.id, + scope: .scope.slug, + scope_id: .scope.id, + deployment_id: .deployment.id + }') + +TOFU_VARIABLES=$(jq -n \ + --arg application_slug "$application_slug" \ + --arg scope_slug "$scope_slug" \ + --arg scope_id "$scope_id" \ + --arg repository_url "$repository_url" \ + --arg application_version "$application_version" \ + --argjson env_vars_json "$env_vars_json" \ + --argjson resource_tags_json "$resource_tags_json" \ + '{ + application_slug: $application_slug, + scope_slug: $scope_slug, + scope_id: $scope_id, + repository_url: $repository_url, + application_version: $application_version, + env_vars_json: $env_vars_json, + resource_tags_json: $resource_tags_json + }') + +tf_state_key="amplify/$application_slug/$scope_slug-$scope_id" + +TOFU_INIT_VARIABLES="-backend-config=\"key=$tf_state_key\"" + +TOFU_MODULE_DIR="$SERVICE_PATH/output/$scope_id" +#if [ -n "${NP_OUTPUT_DIR:-}" ]; then +# TOFU_MODULE_DIR="$NP_OUTPUT_DIR/output/$SCOPE_ID" +#fi + +mkdir -p "$TOFU_MODULE_DIR" + +# ============================================================================= +# Modules to compose (comma-separated list) +# Initialized from CUSTOM_MODULES, extended by setup scripts +# Available modules: +# - state/aws : AWS S3 backend for terraform state +# - network/route_53 : AWS Route53 DNS configuration +# - hosting/amplify : AWS Amplify hosting (coming soon) +# ============================================================================= +MODULES_TO_USE="${CUSTOM_TOFU_MODULES:-}" + +export TOFU_VARIABLES +export TOFU_INIT_VARIABLES +export TOFU_MODULE_DIR +export MODULES_TO_USE \ No newline at end of file diff --git a/frontend/deployment/compose_modules b/frontend/deployment/compose_modules new file mode 100755 index 00000000..b2361546 --- /dev/null +++ b/frontend/deployment/compose_modules @@ -0,0 +1,72 @@ +#!/bin/bash + +# Compose Terraform modules dynamically +# Usage: source compose_modules +# +# Required environment variables: +# MODULES_TO_USE - Comma-separated list of modules (e.g., "state/aws,network/route_53,hosting/amplify") +# TOFU_MODULE_DIR - Target directory where .tf files will be copied +# +# Each module can have: +# - *.tf files: Copied to TOFU_MODULE_DIR (Terraform auto-merges all .tf files) +# - setup script: Sourced to configure TOFU_VARIABLES and TOFU_INIT_VARIABLES + +script_dir="$(dirname "${BASH_SOURCE[0]}")" +modules_dir="$script_dir" + +if [ -z "${MODULES_TO_USE:-}" ]; then + echo "✗ MODULES_TO_USE is not set" + exit 1 +fi + +if [ -z "${TOFU_MODULE_DIR:-}" ]; then + echo "✗ TOFU_MODULE_DIR is not set" + exit 1 +fi + +mkdir -p "$TOFU_MODULE_DIR" + +echo "Composing modules: $MODULES_TO_USE" +echo "Target directory: $TOFU_MODULE_DIR" +echo "" + +IFS=',' read -ra modules <<< "$MODULES_TO_USE" +for module in "${modules[@]}"; do + module=$(echo "$module" | xargs) # trim whitespace + + echo $module + + ls $module + if [ ! -d "$module" ]; then + echo "✗ Module not found: $module" + exit 1 + fi + + # Copy .tf files if they exist (with module prefix to avoid conflicts) + if ls "$module"/*.tf 1> /dev/null 2>&1; then + # Extract last two path components for prefix (e.g., "/path/to/state/aws" -> "state_aws_") + parent=$(basename "$(dirname "$module")") + leaf=$(basename "$module") + prefix="${parent}_${leaf}_" + for tf_file in "$module"/*.tf; do + filename=$(basename "$tf_file") + cp "$tf_file" "$TOFU_MODULE_DIR/${prefix}${filename}" + done + echo "✓ Copied modules from: $module (prefix: $prefix)" + fi + + # Source setup script if it exists + if [ -f "$module/setup" ]; then + echo " Running setup for: $module" + source "$module/setup" + if [ $? -ne 0 ]; then + echo "✗ Setup failed for module: $module" + exit 1 + fi + echo "✓ Setup completed for: $module" + fi + + echo "" +done + +echo "✓ All modules composed successfully" \ No newline at end of file diff --git a/frontend/deployment/distribution/amplify/modules/main.tf b/frontend/deployment/distribution/amplify/modules/main.tf new file mode 100644 index 00000000..0157e7f2 --- /dev/null +++ b/frontend/deployment/distribution/amplify/modules/main.tf @@ -0,0 +1,205 @@ +# AWS Amplify Hosting +# Resources for AWS Amplify static frontend hosting + +variable "hosting_app_name" { + description = "Application name" + type = string +} + +variable "hosting_environment" { + description = "Environment (dev, staging, prod)" + type = string + default = "prod" +} + +variable "hosting_repository_url" { + description = "Git repository URL" + type = string +} + +variable "hosting_branch_name" { + description = "Branch to deploy" + type = string + default = "main" +} + +variable "hosting_github_access_token" { + description = "GitHub access token" + type = string + sensitive = true + default = null +} + +variable "hosting_custom_domain" { + description = "Custom domain (e.g., app.example.com)" + type = string + default = null +} + +variable "hosting_environment_variables" { + description = "Environment variables for the application" + type = map(string) + default = {} +} + +variable "hosting_build_spec" { + description = "Build specification in YAML format" + type = string + default = <<-EOT + version: 1 + frontend: + phases: + preBuild: + commands: + - npm ci + build: + commands: + - npm run build + artifacts: + baseDirectory: dist + files: + - '**/*' + cache: + paths: + - node_modules/**/* + EOT +} + +variable "hosting_framework" { + description = "Application framework (React, Vue, Angular, etc.)" + type = string + default = "React" +} + +variable "hosting_resource_tags_json" { + description = "Resource tags as JSON object" + type = map(string) + default = {} +} + +locals { + hosting_default_tags = merge(var.hosting_resource_tags_json, { + Application = var.hosting_app_name + Environment = var.hosting_environment + ManagedBy = "terraform" + Module = "hosting/amplify" + }) + + hosting_env_vars = merge({ + ENVIRONMENT = var.hosting_environment + APP_NAME = var.hosting_app_name + }, var.hosting_environment_variables) +} + +resource "aws_amplify_app" "main" { + name = "${var.hosting_app_name}-${var.hosting_environment}" + repository = var.hosting_repository_url + + access_token = var.hosting_github_access_token + build_spec = var.hosting_build_spec + environment_variables = local.hosting_env_vars + + custom_rule { + source = "" + status = "200" + target = "/index.html" + } + + enable_auto_branch_creation = false + enable_branch_auto_build = true + enable_branch_auto_deletion = false + platform = "WEB" + + tags = local.hosting_default_tags +} + +resource "aws_amplify_branch" "main" { + app_id = aws_amplify_app.main.id + branch_name = var.hosting_branch_name + + framework = var.hosting_framework + stage = var.hosting_environment == "prod" ? "PRODUCTION" : "DEVELOPMENT" + enable_auto_build = true + + environment_variables = { + BRANCH = var.hosting_branch_name + } + + tags = local.hosting_default_tags +} + +resource "aws_amplify_domain_association" "main" { + count = var.hosting_custom_domain != null ? 1 : 0 + + app_id = aws_amplify_app.main.id + domain_name = var.hosting_custom_domain + + sub_domain { + branch_name = aws_amplify_branch.main.branch_name + prefix = "" + } + + sub_domain { + branch_name = aws_amplify_branch.main.branch_name + prefix = "www" + } + + wait_for_verification = false +} + +resource "aws_amplify_webhook" "main" { + app_id = aws_amplify_app.main.id + branch_name = aws_amplify_branch.main.branch_name + description = "Webhook for manual build triggers" +} + +# Locals for cross-module references (consumed by network/route53) +locals { + # Amplify default domain for DNS pointing + hosting_target_domain = "${aws_amplify_branch.main.branch_name}.${aws_amplify_app.main.id}.amplifyapp.com" + # Amplify uses CNAME records, not alias - so no hosted zone ID needed + hosting_target_zone_id = null + # Amplify requires CNAME records + hosting_record_type = "CNAME" +} + +output "hosting_app_id" { + description = "Amplify application ID" + value = aws_amplify_app.main.id +} + +output "hosting_app_arn" { + description = "Amplify application ARN" + value = aws_amplify_app.main.arn +} + +output "hosting_default_domain" { + description = "Amplify default domain" + value = "https://${local.hosting_target_domain}" +} + +output "hosting_target_domain" { + description = "Target domain for DNS records" + value = local.hosting_target_domain +} + +output "hosting_target_zone_id" { + description = "Hosted zone ID for alias records (null for Amplify/CNAME)" + value = local.hosting_target_zone_id +} + +output "hosting_record_type" { + description = "DNS record type to use (CNAME for Amplify)" + value = local.hosting_record_type +} + +output "hosting_website_url" { + description = "Website URL" + value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${local.hosting_target_domain}" +} + +output "hosting_webhook_url" { + description = "Webhook URL for manual triggers" + value = aws_amplify_webhook.main.url + sensitive = true +} diff --git a/frontend/deployment/distribution/amplify/setup b/frontend/deployment/distribution/amplify/setup new file mode 100755 index 00000000..0e7be107 --- /dev/null +++ b/frontend/deployment/distribution/amplify/setup @@ -0,0 +1,23 @@ +#!/bin/bash + +# Amplify Hosting Setup +# Adds amplify-specific variables to TOFU_VARIABLES + +hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +hosting_repository_url=$(echo "$TOFU_VARIABLES" | jq -r '.repository_url') +hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg app_name "$hosting_app_name" \ + --arg repository_url "$hosting_repository_url" \ + --arg environment "$hosting_environment" \ + '. + { + hosting_app_name: $app_name, + hosting_repository_url: $repository_url, + hosting_environment: $environment + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/distribution/blob-cdn/modules/main.tf b/frontend/deployment/distribution/blob-cdn/modules/main.tf new file mode 100644 index 00000000..a425583f --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/main.tf @@ -0,0 +1,163 @@ +# Azure Blob Storage + CDN Hosting +# Resources for Azure static hosting with CDN + +variable "hosting_app_name" { + description = "Application name" + type = string +} + +variable "hosting_environment" { + description = "Environment (dev, staging, prod)" + type = string + default = "prod" +} + +variable "hosting_location" { + description = "Azure region" + type = string + default = "eastus2" +} + +variable "hosting_custom_domain" { + description = "Custom domain (e.g., app.example.com)" + type = string + default = null +} + +variable "hosting_cdn_sku" { + description = "CDN Profile SKU" + type = string + default = "Standard_Microsoft" +} + +variable "hosting_tags" { + description = "Resource tags" + type = map(string) + default = {} +} + +locals { + hosting_storage_account_name = lower(replace("${var.hosting_app_name}${var.hosting_environment}static", "-", "")) + + hosting_default_tags = merge(var.hosting_tags, { + Application = var.hosting_app_name + Environment = var.hosting_environment + ManagedBy = "terraform" + }) +} + +resource "azurerm_resource_group" "main" { + name = "rg-${var.hosting_app_name}-${var.hosting_environment}" + location = var.hosting_location + tags = local.hosting_default_tags +} + +resource "azurerm_storage_account" "static" { + name = substr(local.hosting_storage_account_name, 0, 24) + resource_group_name = azurerm_resource_group.main.name + location = azurerm_resource_group.main.location + account_tier = "Standard" + account_replication_type = "LRS" + account_kind = "StorageV2" + + static_website { + index_document = "index.html" + error_404_document = "index.html" + } + + min_tls_version = "TLS1_2" + enable_https_traffic_only = true + allow_nested_items_to_be_public = false + + blob_properties { + versioning_enabled = true + + cors_rule { + allowed_headers = ["*"] + allowed_methods = ["GET", "HEAD", "OPTIONS"] + allowed_origins = ["*"] + exposed_headers = ["*"] + max_age_in_seconds = 3600 + } + } + + tags = local.hosting_default_tags +} + +resource "azurerm_cdn_profile" "main" { + name = "cdn-${var.hosting_app_name}-${var.hosting_environment}" + location = "global" + resource_group_name = azurerm_resource_group.main.name + sku = var.hosting_cdn_sku + tags = local.hosting_default_tags +} + +resource "azurerm_cdn_endpoint" "static" { + name = "${var.hosting_app_name}-${var.hosting_environment}" + profile_name = azurerm_cdn_profile.main.name + location = "global" + resource_group_name = azurerm_resource_group.main.name + + origin { + name = "static-website" + host_name = azurerm_storage_account.static.primary_web_host + } + + origin_host_header = azurerm_storage_account.static.primary_web_host + + is_compression_enabled = true + content_types_to_compress = [ + "application/javascript", + "application/json", + "application/xml", + "text/css", + "text/html", + "text/javascript", + "text/plain", + "text/xml", + "image/svg+xml" + ] + + querystring_caching_behaviour = "IgnoreQueryString" + + tags = local.hosting_default_tags +} + +resource "azurerm_cdn_endpoint_custom_domain" "main" { + count = var.hosting_custom_domain != null ? 1 : 0 + + name = replace(var.hosting_custom_domain, ".", "-") + cdn_endpoint_id = azurerm_cdn_endpoint.static.id + host_name = var.hosting_custom_domain + + cdn_managed_https { + certificate_type = "Dedicated" + protocol_type = "ServerNameIndication" + tls_version = "TLS12" + } +} + +output "hosting_resource_group_name" { + description = "Resource Group name" + value = azurerm_resource_group.main.name +} + +output "hosting_storage_account_name" { + description = "Storage Account name" + value = azurerm_storage_account.static.name +} + +output "hosting_cdn_endpoint_hostname" { + description = "CDN Endpoint hostname" + value = azurerm_cdn_endpoint.static.fqdn +} + +output "hosting_website_url" { + description = "Website URL" + value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${azurerm_cdn_endpoint.static.fqdn}" +} + +output "hosting_upload_command" { + description = "Command to upload files" + value = "az storage blob upload-batch --account-name ${azurerm_storage_account.static.name} --destination '$web' --source ./dist" +} diff --git a/frontend/deployment/distribution/blob-cdn/setup b/frontend/deployment/distribution/blob-cdn/setup new file mode 100755 index 00000000..0a3a9f4d --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/setup @@ -0,0 +1,19 @@ +#!/bin/bash + +# Azure Blob + CDN Hosting Setup + +hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg app_name "$hosting_app_name" \ + --arg environment "$hosting_environment" \ + '. + { + hosting_app_name: $app_name, + hosting_environment: $environment + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/distribution/cloudfront/modules/data.tf b/frontend/deployment/distribution/cloudfront/modules/data.tf new file mode 100644 index 00000000..9d4b0475 --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/data.tf @@ -0,0 +1,3 @@ +data "aws_s3_bucket" "static" { + bucket = var.distribution_bucket_name +} diff --git a/frontend/deployment/distribution/cloudfront/modules/locals.tf b/frontend/deployment/distribution/cloudfront/modules/locals.tf new file mode 100644 index 00000000..abac0de8 --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/locals.tf @@ -0,0 +1,14 @@ +locals { + distribution_origin_id = "S3-${var.distribution_bucket_name}" + distribution_aliases = var.distribution_custom_domain != null ? [var.distribution_custom_domain] : [] + + distribution_default_tags = merge(var.distribution_resource_tags_json, { + ManagedBy = "terraform" + Module = "distribution/cloudfront" + }) + + # Cross-module references (consumed by network/route53) + distribution_target_domain = aws_cloudfront_distribution.static.domain_name + distribution_target_zone_id = aws_cloudfront_distribution.static.hosted_zone_id + distribution_record_type = "A" +} diff --git a/frontend/deployment/distribution/cloudfront/modules/main.tf b/frontend/deployment/distribution/cloudfront/modules/main.tf new file mode 100644 index 00000000..6bc652e9 --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/main.tf @@ -0,0 +1,88 @@ +resource "aws_cloudfront_origin_access_control" "static" { + name = "${var.distribution_app_name}-oac" + description = "OAC for ${var.distribution_app_name}" + origin_access_control_origin_type = "s3" + signing_behavior = "always" + signing_protocol = "sigv4" +} + +resource "aws_cloudfront_distribution" "static" { + enabled = true + is_ipv6_enabled = true + default_root_object = "index.html" + aliases = local.distribution_aliases + price_class = "PriceClass_100" + comment = "Distribution for ${var.distribution_app_name}" + + origin { + domain_name = data.aws_s3_bucket.static.bucket_regional_domain_name + origin_id = local.distribution_origin_id + origin_access_control_id = aws_cloudfront_origin_access_control.static.id + + origin_path = var.distribution_s3_prefix != "" ? "/${var.distribution_s3_prefix}" : "" + } + + default_cache_behavior { + allowed_methods = ["GET", "HEAD", "OPTIONS"] + cached_methods = ["GET", "HEAD"] + target_origin_id = local.distribution_origin_id + + forwarded_values { + query_string = false + cookies { + forward = "none" + } + } + + viewer_protocol_policy = "redirect-to-https" + min_ttl = 0 + default_ttl = 3600 + max_ttl = 86400 + compress = true + } + + ordered_cache_behavior { + path_pattern = "/static/*" + allowed_methods = ["GET", "HEAD"] + cached_methods = ["GET", "HEAD"] + target_origin_id = local.distribution_origin_id + + forwarded_values { + query_string = false + cookies { + forward = "none" + } + } + + viewer_protocol_policy = "redirect-to-https" + min_ttl = 86400 + default_ttl = 604800 + max_ttl = 31536000 + compress = true + } + + custom_error_response { + error_code = 404 + response_code = 200 + response_page_path = "/index.html" + } + + custom_error_response { + error_code = 403 + response_code = 200 + response_page_path = "/index.html" + } + + restrictions { + geo_restriction { + restriction_type = "none" + } + } + + viewer_certificate { + cloudfront_default_certificate = true + minimum_protocol_version = "TLSv1.2_2021" + } + + tags = local.distribution_default_tags +} diff --git a/frontend/deployment/distribution/cloudfront/modules/outputs.tf b/frontend/deployment/distribution/cloudfront/modules/outputs.tf new file mode 100644 index 00000000..fe503cdb --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/outputs.tf @@ -0,0 +1,44 @@ +output "hosting_bucket_name" { + description = "S3 bucket name" + value = data.aws_s3_bucket.static.id +} + +output "hosting_bucket_arn" { + description = "S3 bucket ARN" + value = data.aws_s3_bucket.static.arn +} + +output "hosting_s3_prefix" { + description = "S3 prefix path for this scope" + value = var.hosting_s3_prefix +} + +output "hosting_cloudfront_distribution_id" { + description = "CloudFront distribution ID" + value = aws_cloudfront_distribution.static.id +} + +output "hosting_cloudfront_domain_name" { + description = "CloudFront domain name" + value = aws_cloudfront_distribution.static.domain_name +} + +output "hosting_target_domain" { + description = "Target domain for DNS records (CloudFront domain)" + value = local.hosting_target_domain +} + +output "hosting_target_zone_id" { + description = "Hosted zone ID for Route 53 alias records" + value = local.hosting_target_zone_id +} + +output "hosting_record_type" { + description = "DNS record type (A for CloudFront alias)" + value = local.hosting_record_type +} + +output "hosting_website_url" { + description = "Website URL" + value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" +} diff --git a/frontend/deployment/distribution/cloudfront/modules/variables.tf b/frontend/deployment/distribution/cloudfront/modules/variables.tf new file mode 100644 index 00000000..a9ef4cbc --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/variables.tf @@ -0,0 +1,26 @@ +variable "distribution_bucket_name" { + description = "Existing S3 bucket name for static website distribution" + type = string +} + +variable "distribution_s3_prefix" { + description = "S3 prefix/path for this scope's files (e.g., 'app-name/scope-id')" + type = string +} + +variable "distribution_app_name" { + description = "Application name (used for resource naming)" + type = string +} + +variable "distribution_custom_domain" { + description = "Custom domain for CloudFront (optional)" + type = string + default = null +} + +variable "distribution_resource_tags_json" { + description = "Resource tags as JSON object" + type = map(string) + default = {} +} diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup new file mode 100755 index 00000000..b852595d --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/setup @@ -0,0 +1,111 @@ +#!/bin/bash + +# S3 + CloudFront Hosting Setup + +application_slug=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +scope_slug=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug') +scope_id=$(echo "$TOFU_VARIABLES" | jq -r '.scope_id') + +hosting_app_name="$application_slug-$scope_slug-$scope_id" + +# Fetch bucket name from assets-repository provider +echo "🔍 Fetching assets-repository provider..." + +nrn=$(echo "$CONTEXT" | jq -r .scope.nrn) + +asset_repository=$(np provider list --nrn "$nrn" --categories assets-repository --format json 2>&1) +np_exit_code=$? + +if [ $np_exit_code -ne 0 ]; then + echo "" + echo "❌ Failed to fetch assets-repository provider" + echo "" + + if echo "$asset_repository" | grep -q "not found\|no providers"; then + echo " 🔎 Error: No assets-repository provider found" + echo "" + echo " 💡 Possible causes:" + echo " • No assets-repository provider is configured for this scope" + echo " • The provider category 'assets-repository' does not exist" + echo "" + echo " 🔧 How to fix:" + echo " 1. Create an assets-repository provider in nullplatform" + echo " 2. Ensure it's linked to the scope with NRN: $nrn" + + elif echo "$asset_repository" | grep -q "unauthorized\|forbidden\|401\|403"; then + echo " 🔒 Error: Permission denied" + echo "" + echo " 💡 Possible causes:" + echo " • The API token doesn't have permission to list providers" + echo " • The token has expired" + echo "" + echo " 🔧 How to fix:" + echo " 1. Check your NP_API_KEY is set and valid" + echo " 2. Ensure you have permissions to access providers" + + elif echo "$asset_repository" | grep -q "command not found"; then + echo " ⚠️ Error: 'np' CLI not found" + echo "" + echo " 🔧 How to fix:" + echo " Install the nullplatform CLI: npm install -g @nullplatform/cli" + + else + echo " 📋 Error details:" + echo "$asset_repository" | sed 's/^/ /' + fi + + echo "" + exit 1 +fi + +hosting_bucket_name=$(echo "$asset_repository" | jq -r ' + [.results[] | select(.attributes.bucket.name != null)] | first | .attributes.bucket.name // empty +') + +if [ -z "$hosting_bucket_name" ] || [ "$hosting_bucket_name" = "null" ]; then + echo "" + echo "❌ No S3 bucket found in assets-repository providers" + echo "" + echo " 🤔 Found $(echo "$asset_repository" | jq '.results | length') provider(s), but none contain bucket information" + echo "" + echo " 📋 Providers found:" + echo "$asset_repository" | jq -r '.results[] | " • \(.name // .id // "unnamed") (type: \(.type // "unknown"))"' 2>/dev/null || echo " (could not parse providers)" + echo "" + echo " 💡 Expected one provider with: attributes.bucket.name" + echo "" + echo " 🔧 How to fix:" + echo " 1. Ensure an S3 bucket provider is configured in assets-repository category" + echo " 2. Verify the provider has the bucket_name attribute populated" + echo "" + exit 1 +fi + +echo "✅ Bucket name: $hosting_bucket_name" + +# S3 prefix for multi-scope bucket support +# TODO: Replace with your prefix variable +hosting_s3_prefix="/app" + +echo "📁 S3 prefix: ${hosting_s3_prefix:-"(root)"}" + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg bucket_name "$hosting_bucket_name" \ + --arg app_name "$hosting_app_name" \ + --arg s3_prefix "$hosting_s3_prefix" \ + '. + { + hosting_bucket_name: $bucket_name, + hosting_app_name: $app_name, + hosting_s3_prefix: $s3_prefix + }') + +echo "✅ S3 + CloudFront hosting configured" + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi diff --git a/frontend/deployment/distribution/firebase/modules/main.tf b/frontend/deployment/distribution/firebase/modules/main.tf new file mode 100644 index 00000000..f4243380 --- /dev/null +++ b/frontend/deployment/distribution/firebase/modules/main.tf @@ -0,0 +1,87 @@ +# Firebase Hosting +# Resources for Firebase static hosting + +variable "hosting_project_id" { + description = "GCP/Firebase project ID" + type = string +} + +variable "hosting_app_name" { + description = "Application name" + type = string +} + +variable "hosting_environment" { + description = "Environment (dev, staging, prod)" + type = string + default = "prod" +} + +variable "hosting_custom_domains" { + description = "List of custom domains" + type = list(string) + default = [] +} + +variable "hosting_labels" { + description = "Resource labels" + type = map(string) + default = {} +} + +locals { + hosting_site_id = "${var.hosting_app_name}-${var.hosting_environment}" + + hosting_default_labels = merge(var.hosting_labels, { + application = replace(var.hosting_app_name, "-", "_") + environment = var.hosting_environment + managed_by = "terraform" + }) +} + +resource "google_firebase_project" "default" { + provider = google-beta + project = var.hosting_project_id +} + +resource "google_firebase_hosting_site" "default" { + provider = google-beta + project = google_firebase_project.default.project + site_id = local.hosting_site_id +} + +resource "google_firebase_hosting_custom_domain" "domains" { + for_each = toset(var.hosting_custom_domains) + + provider = google-beta + project = google_firebase_project.default.project + site_id = google_firebase_hosting_site.default.site_id + custom_domain = each.value + + wait_dns_verification = false +} + +output "hosting_project_id" { + description = "Firebase project ID" + value = google_firebase_project.default.project +} + +output "hosting_site_id" { + description = "Firebase Hosting site ID" + value = google_firebase_hosting_site.default.site_id +} + +output "hosting_default_url" { + description = "Firebase Hosting default URL" + value = "https://${google_firebase_hosting_site.default.site_id}.web.app" +} + +output "hosting_firebaseapp_url" { + description = "Firebase alternative URL" + value = "https://${google_firebase_hosting_site.default.site_id}.firebaseapp.com" +} + +output "hosting_website_url" { + description = "Website URL" + value = length(var.hosting_custom_domains) > 0 ? "https://${var.hosting_custom_domains[0]}" : "https://${google_firebase_hosting_site.default.site_id}.web.app" +} diff --git a/frontend/deployment/distribution/firebase/setup b/frontend/deployment/distribution/firebase/setup new file mode 100755 index 00000000..9b6892ff --- /dev/null +++ b/frontend/deployment/distribution/firebase/setup @@ -0,0 +1,27 @@ +#!/bin/bash + +# Firebase Hosting Setup + +hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') +hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') + +if [ -z "$hosting_project_id" ]; then + echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + exit 1 +fi + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg app_name "$hosting_app_name" \ + --arg environment "$hosting_environment" \ + --arg project_id "$hosting_project_id" \ + '. + { + hosting_app_name: $app_name, + hosting_environment: $environment, + hosting_project_id: $project_id + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/distribution/gcs-cdn/modules/main.tf b/frontend/deployment/distribution/gcs-cdn/modules/main.tf new file mode 100644 index 00000000..4615aabf --- /dev/null +++ b/frontend/deployment/distribution/gcs-cdn/modules/main.tf @@ -0,0 +1,193 @@ +# GCP Cloud Storage + Cloud CDN Hosting +# Resources for GCS static hosting with Cloud CDN + +variable "hosting_project_id" { + description = "GCP project ID" + type = string +} + +variable "hosting_app_name" { + description = "Application name" + type = string +} + +variable "hosting_environment" { + description = "Environment (dev, staging, prod)" + type = string + default = "prod" +} + +variable "hosting_region" { + description = "GCP region" + type = string + default = "us-central1" +} + +variable "hosting_custom_domain" { + description = "Custom domain (e.g., app.example.com)" + type = string + default = null +} + +variable "hosting_labels" { + description = "Resource labels" + type = map(string) + default = {} +} + +locals { + hosting_bucket_name = "${var.hosting_app_name}-${var.hosting_environment}-static-${var.hosting_project_id}" + + hosting_default_labels = merge(var.hosting_labels, { + application = var.hosting_app_name + environment = var.hosting_environment + managed_by = "terraform" + }) +} + +resource "google_storage_bucket" "static" { + name = local.hosting_bucket_name + project = var.hosting_project_id + location = var.hosting_region + force_destroy = false + + website { + main_page_suffix = "index.html" + not_found_page = "index.html" + } + + versioning { + enabled = true + } + + cors { + origin = ["*"] + method = ["GET", "HEAD", "OPTIONS"] + response_header = ["*"] + max_age_seconds = 3600 + } + + uniform_bucket_level_access = true + + labels = local.hosting_default_labels +} + +resource "google_storage_bucket_iam_member" "public_read" { + bucket = google_storage_bucket.static.name + role = "roles/storage.objectViewer" + member = "allUsers" +} + +resource "google_compute_backend_bucket" "static" { + name = "${var.hosting_app_name}-${var.hosting_environment}-backend" + project = var.hosting_project_id + bucket_name = google_storage_bucket.static.name + + enable_cdn = true + + cdn_policy { + cache_mode = "CACHE_ALL_STATIC" + client_ttl = 3600 + default_ttl = 3600 + max_ttl = 86400 + negative_caching = true + serve_while_stale = 86400 + + negative_caching_policy { + code = 404 + ttl = 60 + } + } +} + +resource "google_compute_url_map" "static" { + name = "${var.hosting_app_name}-${var.hosting_environment}-urlmap" + project = var.hosting_project_id + default_service = google_compute_backend_bucket.static.id +} + +resource "google_compute_managed_ssl_certificate" "static" { + count = var.hosting_custom_domain != null ? 1 : 0 + name = "${var.hosting_app_name}-${var.hosting_environment}-cert" + project = var.hosting_project_id + + managed { + domains = [var.hosting_custom_domain] + } +} + +resource "google_compute_target_https_proxy" "static" { + count = var.hosting_custom_domain != null ? 1 : 0 + name = "${var.hosting_app_name}-${var.hosting_environment}-https-proxy" + project = var.hosting_project_id + url_map = google_compute_url_map.static.id + ssl_certificates = [google_compute_managed_ssl_certificate.static[0].id] +} + +resource "google_compute_target_http_proxy" "static" { + name = "${var.hosting_app_name}-${var.hosting_environment}-http-proxy" + project = var.hosting_project_id + url_map = google_compute_url_map.http_redirect.id +} + +resource "google_compute_url_map" "http_redirect" { + name = "${var.hosting_app_name}-${var.hosting_environment}-http-redirect" + project = var.hosting_project_id + + default_url_redirect { + https_redirect = true + redirect_response_code = "MOVED_PERMANENTLY_DEFAULT" + strip_query = false + } +} + +resource "google_compute_global_address" "static" { + name = "${var.hosting_app_name}-${var.hosting_environment}-ip" + project = var.hosting_project_id +} + +resource "google_compute_global_forwarding_rule" "https" { + count = var.hosting_custom_domain != null ? 1 : 0 + name = "${var.hosting_app_name}-${var.hosting_environment}-https-rule" + project = var.hosting_project_id + ip_address = google_compute_global_address.static.address + ip_protocol = "TCP" + port_range = "443" + target = google_compute_target_https_proxy.static[0].id + load_balancing_scheme = "EXTERNAL_MANAGED" +} + +resource "google_compute_global_forwarding_rule" "http" { + name = "${var.hosting_app_name}-${var.hosting_environment}-http-rule" + project = var.hosting_project_id + ip_address = google_compute_global_address.static.address + ip_protocol = "TCP" + port_range = "80" + target = google_compute_target_http_proxy.static.id + load_balancing_scheme = "EXTERNAL_MANAGED" +} + +output "hosting_bucket_name" { + description = "GCS bucket name" + value = google_storage_bucket.static.name +} + +output "hosting_bucket_url" { + description = "GCS bucket URL" + value = google_storage_bucket.static.url +} + +output "hosting_load_balancer_ip" { + description = "Load Balancer IP" + value = google_compute_global_address.static.address +} + +output "hosting_website_url" { + description = "Website URL" + value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "http://${google_compute_global_address.static.address}" +} + +output "hosting_upload_command" { + description = "Command to upload files" + value = "gsutil -m rsync -r ./dist gs://${google_storage_bucket.static.name}" +} diff --git a/frontend/deployment/distribution/gcs-cdn/setup b/frontend/deployment/distribution/gcs-cdn/setup new file mode 100755 index 00000000..df40afb7 --- /dev/null +++ b/frontend/deployment/distribution/gcs-cdn/setup @@ -0,0 +1,30 @@ +#!/bin/bash + +# GCS + Cloud CDN Hosting Setup + +hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') +hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') +hosting_region=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.region // "us-central1"') + +if [ -z "$hosting_project_id" ]; then + echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + exit 1 +fi + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg app_name "$hosting_app_name" \ + --arg environment "$hosting_environment" \ + --arg project_id "$hosting_project_id" \ + --arg region "$hosting_region" \ + '. + { + hosting_app_name: $app_name, + hosting_environment: $environment, + hosting_project_id: $project_id, + hosting_region: $region + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/distribution/static-web-apps/modules/main.tf b/frontend/deployment/distribution/static-web-apps/modules/main.tf new file mode 100644 index 00000000..d010e384 --- /dev/null +++ b/frontend/deployment/distribution/static-web-apps/modules/main.tf @@ -0,0 +1,107 @@ +# Azure Static Web Apps Hosting +# Resources for Azure Static Web Apps + +variable "hosting_app_name" { + description = "Application name" + type = string +} + +variable "hosting_environment" { + description = "Environment (dev, staging, prod)" + type = string + default = "prod" +} + +variable "hosting_location" { + description = "Azure region" + type = string + default = "eastus2" +} + +variable "hosting_sku_tier" { + description = "SKU tier (Free or Standard)" + type = string + default = "Free" +} + +variable "hosting_sku_size" { + description = "SKU size" + type = string + default = "Free" +} + +variable "hosting_custom_domains" { + description = "List of custom domains" + type = list(string) + default = [] +} + +variable "hosting_tags" { + description = "Resource tags" + type = map(string) + default = {} +} + +locals { + hosting_default_tags = merge(var.hosting_tags, { + Application = var.hosting_app_name + Environment = var.hosting_environment + ManagedBy = "terraform" + }) +} + +resource "azurerm_resource_group" "main" { + name = "rg-${var.hosting_app_name}-${var.hosting_environment}" + location = var.hosting_location + tags = local.hosting_default_tags +} + +resource "azurerm_static_web_app" "main" { + name = "swa-${var.hosting_app_name}-${var.hosting_environment}" + resource_group_name = azurerm_resource_group.main.name + location = var.hosting_location + + sku_tier = var.hosting_sku_tier + sku_size = var.hosting_sku_size + + tags = local.hosting_default_tags +} + +resource "azurerm_static_web_app_custom_domain" "main" { + for_each = toset(var.hosting_custom_domains) + + static_web_app_id = azurerm_static_web_app.main.id + domain_name = each.value + validation_type = "cname-delegation" +} + +output "hosting_resource_group_name" { + description = "Resource Group name" + value = azurerm_resource_group.main.name +} + +output "hosting_static_web_app_id" { + description = "Static Web App ID" + value = azurerm_static_web_app.main.id +} + +output "hosting_static_web_app_name" { + description = "Static Web App name" + value = azurerm_static_web_app.main.name +} + +output "hosting_default_hostname" { + description = "Default hostname" + value = azurerm_static_web_app.main.default_host_name +} + +output "hosting_website_url" { + description = "Website URL" + value = length(var.hosting_custom_domains) > 0 ? "https://${var.hosting_custom_domains[0]}" : "https://${azurerm_static_web_app.main.default_host_name}" +} + +output "hosting_api_key" { + description = "API key for deployments" + value = azurerm_static_web_app.main.api_key + sensitive = true +} diff --git a/frontend/deployment/distribution/static-web-apps/setup b/frontend/deployment/distribution/static-web-apps/setup new file mode 100755 index 00000000..dd8f1c59 --- /dev/null +++ b/frontend/deployment/distribution/static-web-apps/setup @@ -0,0 +1,19 @@ +#!/bin/bash + +# Azure Static Web Apps Hosting Setup + +hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') +hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg app_name "$hosting_app_name" \ + --arg environment "$hosting_environment" \ + '. + { + hosting_app_name: $app_name, + hosting_environment: $environment + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/do_tofu b/frontend/deployment/do_tofu new file mode 100644 index 00000000..85b4f303 --- /dev/null +++ b/frontend/deployment/do_tofu @@ -0,0 +1,42 @@ +#!/bin/bash + +#echo $TOFU_VARIABLES | jq . + +echo "$TOFU_INIT_VARIABLES" +echo "$TOFU_VARIABLES" +echo "$MODULES_TO_USE" +#set -eou pipefail +# +#CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") +# +#cd "$CURRENT_DIR" +# +#AWS_REGION="${AWS_REGION:-us-east-1}" +#TF_STATE_BUCKET="test-static-null2" +#TF_LOCK_TABLE="service-provisioning-terraform-state-lock" +## You need to export the GITHUB_TOKEN as an env var in the agent +##GITHUB_TOKEN="" +# +#HOSTED_PUBLIC_ZONE_ID=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id') +# +#DOMAIN=$(aws route53 get-hosted-zone --id "$HOSTED_PUBLIC_ZONE_ID" --query 'HostedZone.Name' --output text | sed 's/\.$//') +#SUBDOMAIN="$APPLICATION_SLUG-$SCOPE_SLUG" +# +#np scope patch --id "$SCOPE_ID" --body "{\"domain\":\"$SUBDOMAIN.$DOMAIN\"}" +# +#tofu init \ +# -backend-config="bucket=${TF_STATE_BUCKET}" \ +# -backend-config="key=amplify/$APPLICATION_SLUG/$SCOPE_SLUG-$SCOPE_ID" \ +# -backend-config="region=${AWS_REGION}" \ +# -backend-config="dynamodb_table=${TF_LOCK_TABLE}" +# +#tofu $ACTION -auto-approve \ +# -var="aws_region=${AWS_REGION}" \ +# -var="github_token=${GITHUB_TOKEN}" \ +# -var="application_name=${APPLICATION_SLUG}" \ +# -var="repository_url=${REPOSITORY_URL}" \ +# -var="application_version=${APPLICATION_VERSION}" \ +# -var="env_vars_json=${ENV_VARS_JSON}" \ +# -var="resource_tags_json=${RESOURCE_TAGS_JSON}" \ +# -var="domain=${DOMAIN}" \ +# -var="subdomain=${SUBDOMAIN}" \ No newline at end of file diff --git a/frontend/deployment/network/azure_dns/modules/main.tf b/frontend/deployment/network/azure_dns/modules/main.tf new file mode 100644 index 00000000..01252949 --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/main.tf @@ -0,0 +1,79 @@ +# Azure DNS Configuration +# Creates DNS records pointing to hosting resources (CDN, Static Web Apps, etc.) + +variable "network_resource_group" { + description = "Resource group containing the DNS zone" + type = string +} + +variable "network_zone_name" { + description = "Azure DNS zone name" + type = string +} + +variable "network_domain" { + description = "Domain/subdomain for the application" + type = string +} + +variable "network_target_domain" { + description = "Target domain (for CNAME records)" + type = string +} + +variable "network_ttl" { + description = "DNS record TTL in seconds" + type = number + default = 300 +} + +variable "network_create_www" { + description = "Create www subdomain record as well" + type = bool + default = true +} + +variable "network_tags" { + description = "Resource tags" + type = map(string) + default = {} +} + +# CNAME record for main domain +resource "azurerm_dns_cname_record" "main" { + name = var.network_domain == var.network_zone_name ? "@" : replace(var.network_domain, ".${var.network_zone_name}", "") + zone_name = var.network_zone_name + resource_group_name = var.network_resource_group + ttl = var.network_ttl + record = var.network_target_domain + + tags = var.network_tags +} + +# WWW subdomain +resource "azurerm_dns_cname_record" "www" { + count = var.network_create_www ? 1 : 0 + + name = "www" + zone_name = var.network_zone_name + resource_group_name = var.network_resource_group + ttl = var.network_ttl + record = var.network_target_domain + + tags = var.network_tags +} + +output "network_domain" { + description = "Configured domain" + value = var.network_domain +} + +output "network_fqdn" { + description = "Fully qualified domain name" + value = azurerm_dns_cname_record.main.fqdn +} + +output "network_website_url" { + description = "Website URL" + value = "https://${var.network_domain}" +} diff --git a/frontend/deployment/network/azure_dns/setup b/frontend/deployment/network/azure_dns/setup new file mode 100755 index 00000000..a039d808 --- /dev/null +++ b/frontend/deployment/network/azure_dns/setup @@ -0,0 +1,43 @@ +#!/bin/bash + +# Azure DNS Setup +# Configures DNS variables based on hosting output + +network_resource_group=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.dns_resource_group // empty') +network_zone_name=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.dns_zone_name // empty') + +if [ -z "$network_resource_group" ]; then + echo "✗ dns_resource_group is not set in context" + exit 1 +fi + +if [ -z "$network_zone_name" ]; then + echo "✗ dns_zone_name is not set in context" + exit 1 +fi + +# Get domain from scope or context +network_domain=$(echo "$TOFU_VARIABLES" | jq -r '.scope_domain // empty') +if [ -z "$network_domain" ]; then + network_domain=$(echo "$CONTEXT" | jq -r '.scope.domain // empty') +fi + +# Get target from hosting outputs (CDN endpoint, Static Web App hostname, etc.) +network_target_domain=$(echo "$TOFU_VARIABLES" | jq -r '.hosting_cdn_endpoint_hostname // .hosting_default_hostname // empty') + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg resource_group "$network_resource_group" \ + --arg zone_name "$network_zone_name" \ + --arg domain "$network_domain" \ + --arg target_domain "$network_target_domain" \ + '. + { + network_resource_group: $resource_group, + network_zone_name: $zone_name, + network_domain: $domain, + network_target_domain: $target_domain + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/network/cloud_dns/modules/main.tf b/frontend/deployment/network/cloud_dns/modules/main.tf new file mode 100644 index 00000000..897803e9 --- /dev/null +++ b/frontend/deployment/network/cloud_dns/modules/main.tf @@ -0,0 +1,105 @@ +# GCP Cloud DNS Configuration +# Creates DNS records pointing to hosting resources (Load Balancer, Firebase, etc.) + +variable "network_project_id" { + description = "GCP project ID" + type = string +} + +variable "network_managed_zone" { + description = "Cloud DNS managed zone name" + type = string +} + +variable "network_domain" { + description = "Domain name for the application" + type = string +} + +variable "network_target_ip" { + description = "Target IP address (for A records)" + type = string + default = null +} + +variable "network_target_domain" { + description = "Target domain (for CNAME records)" + type = string + default = null +} + +variable "network_record_type" { + description = "DNS record type (A or CNAME)" + type = string + default = "A" +} + +variable "network_ttl" { + description = "DNS record TTL in seconds" + type = number + default = 300 +} + +variable "network_create_www" { + description = "Create www subdomain record as well" + type = bool + default = true +} + +# A record +resource "google_dns_record_set" "main_a" { + count = var.network_record_type == "A" && var.network_target_ip != null ? 1 : 0 + + name = "${var.network_domain}." + project = var.network_project_id + type = "A" + ttl = var.network_ttl + managed_zone = var.network_managed_zone + rrdatas = [var.network_target_ip] +} + +# CNAME record +resource "google_dns_record_set" "main_cname" { + count = var.network_record_type == "CNAME" && var.network_target_domain != null ? 1 : 0 + + name = "${var.network_domain}." + project = var.network_project_id + type = "CNAME" + ttl = var.network_ttl + managed_zone = var.network_managed_zone + rrdatas = ["${var.network_target_domain}."] +} + +# WWW subdomain (A record) +resource "google_dns_record_set" "www_a" { + count = var.network_create_www && var.network_record_type == "A" && var.network_target_ip != null ? 1 : 0 + + name = "www.${var.network_domain}." + project = var.network_project_id + type = "A" + ttl = var.network_ttl + managed_zone = var.network_managed_zone + rrdatas = [var.network_target_ip] +} + +# WWW subdomain (CNAME record) +resource "google_dns_record_set" "www_cname" { + count = var.network_create_www && var.network_record_type == "CNAME" && var.network_target_domain != null ? 1 : 0 + + name = "www.${var.network_domain}." + project = var.network_project_id + type = "CNAME" + ttl = var.network_ttl + managed_zone = var.network_managed_zone + rrdatas = ["${var.network_target_domain}."] +} + +output "network_domain" { + description = "Configured domain" + value = var.network_domain +} + +output "network_website_url" { + description = "Website URL" + value = "https://${var.network_domain}" +} diff --git a/frontend/deployment/network/cloud_dns/setup b/frontend/deployment/network/cloud_dns/setup new file mode 100755 index 00000000..e491ebfb --- /dev/null +++ b/frontend/deployment/network/cloud_dns/setup @@ -0,0 +1,43 @@ +#!/bin/bash + +# Cloud DNS Setup +# Configures DNS variables based on hosting output + +network_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') + +if [ -z "$network_project_id" ]; then + echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + exit 1 +fi + +managed_zone=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.managed_zone // empty') +if [ -z "$managed_zone" ]; then + echo "✗ managed_zone is not set in context" + exit 1 +fi + +# Get domain from scope or context +network_domain=$(echo "$TOFU_VARIABLES" | jq -r '.scope_domain // empty') +if [ -z "$network_domain" ]; then + network_domain=$(echo "$CONTEXT" | jq -r '.scope.domain // empty') +fi + +# Get target from hosting outputs (Load Balancer IP, etc.) +network_target_ip=$(echo "$TOFU_VARIABLES" | jq -r '.hosting_load_balancer_ip // empty') + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg project_id "$network_project_id" \ + --arg managed_zone "$managed_zone" \ + --arg domain "$network_domain" \ + --arg target_ip "$network_target_ip" \ + '. + { + network_project_id: $project_id, + network_managed_zone: $managed_zone, + network_domain: $domain, + network_target_ip: $target_ip + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir#*deployment/}" +MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" diff --git a/frontend/deployment/network/route_53/modules/locals.tf b/frontend/deployment/network/route_53/modules/locals.tf new file mode 100644 index 00000000..d9357d81 --- /dev/null +++ b/frontend/deployment/network/route_53/modules/locals.tf @@ -0,0 +1,4 @@ +locals { + # Compute full domain from domain + subdomain + network_full_domain = var.network_subdomain != "" ? "${var.network_subdomain}.${var.network_domain}" : var.network_domain +} \ No newline at end of file diff --git a/frontend/deployment/network/route_53/modules/main.tf b/frontend/deployment/network/route_53/modules/main.tf new file mode 100644 index 00000000..7cd8f0f7 --- /dev/null +++ b/frontend/deployment/network/route_53/modules/main.tf @@ -0,0 +1,23 @@ +resource "aws_route53_record" "main_alias" { + count = local.hosting_record_type == "A" ? 1 : 0 + + zone_id = var.network_hosted_zone_id + name = local.network_full_domain + type = "A" + + alias { + name = local.hosting_target_domain + zone_id = local.hosting_target_zone_id + evaluate_target_health = false + } +} + +resource "aws_route53_record" "main_cname" { + count = local.hosting_record_type == "CNAME" ? 1 : 0 + + zone_id = var.network_hosted_zone_id + name = local.network_full_domain + type = "CNAME" + ttl = 300 + records = [local.hosting_target_domain] +} diff --git a/frontend/deployment/network/route_53/modules/outputs.tf b/frontend/deployment/network/route_53/modules/outputs.tf new file mode 100644 index 00000000..6fa188e5 --- /dev/null +++ b/frontend/deployment/network/route_53/modules/outputs.tf @@ -0,0 +1,14 @@ +output "network_full_domain" { + description = "Full domain name (subdomain.domain or just domain)" + value = local.network_full_domain +} + +output "network_fqdn" { + description = "Fully qualified domain name" + value = local.hosting_record_type == "A" ? aws_route53_record.main_alias[0].fqdn : aws_route53_record.main_cname[0].fqdn +} + +output "network_website_url" { + description = "Website URL" + value = "https://${local.network_full_domain}" +} \ No newline at end of file diff --git a/frontend/deployment/network/route_53/modules/variables.tf b/frontend/deployment/network/route_53/modules/variables.tf new file mode 100644 index 00000000..9f67cf99 --- /dev/null +++ b/frontend/deployment/network/route_53/modules/variables.tf @@ -0,0 +1,15 @@ +variable "network_hosted_zone_id" { + description = "Route53 hosted zone ID" + type = string +} + +variable "network_domain" { + description = "Root domain name (e.g., example.com)" + type = string +} + +variable "network_subdomain" { + description = "Subdomain prefix (e.g., 'app' for app.example.com, empty string for apex)" + type = string + default = "" +} \ No newline at end of file diff --git a/frontend/deployment/network/route_53/setup b/frontend/deployment/network/route_53/setup new file mode 100755 index 00000000..aaf1fa93 --- /dev/null +++ b/frontend/deployment/network/route_53/setup @@ -0,0 +1,118 @@ +#!/bin/bash + +hosted_zone_id=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id // empty') + +if [ -z "$hosted_zone_id" ]; then + echo "❌ hosted_public_zone_id is not set in context" + exit 1 +fi + +application_slug=$(echo "$CONTEXT" | jq -r .application.slug) +scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) + +# Fetch the domain name from Route 53 hosted zone +echo "🔍 Fetching domain from Route 53 hosted zone: $hosted_zone_id" + +aws_output=$(aws route53 get-hosted-zone --id "$hosted_zone_id" 2>&1) +aws_exit_code=$? + +if [ $aws_exit_code -ne 0 ]; then + echo "" + echo "❌ Failed to fetch Route 53 hosted zone information" + echo "" + + if echo "$aws_output" | grep -q "NoSuchHostedZone"; then + echo " 🔎 Error: Hosted zone '$hosted_zone_id' does not exist" + echo "" + echo " 💡 Possible causes:" + echo " • The hosted zone ID is incorrect or has a typo" + echo " • The hosted zone was deleted" + echo " • The hosted zone ID format is wrong (should be like 'Z1234567890ABC' or '/hostedzone/Z1234567890ABC')" + echo "" + echo " 🔧 How to fix:" + echo " 1. Verify the hosted zone exists: aws route53 list-hosted-zones" + echo " 2. Update 'hosted_public_zone_id' in your cloud provider configuration" + + elif echo "$aws_output" | grep -q "AccessDenied\|not authorized"; then + echo " 🔒 Error: Permission denied when accessing Route 53" + echo "" + echo " 💡 Possible causes:" + echo " • The AWS credentials don't have Route 53 read permissions" + echo " • The IAM role/user is missing the 'route53:GetHostedZone' permission" + echo "" + echo " 🔧 How to fix:" + echo " 1. Check your AWS credentials are configured correctly" + echo " 2. Ensure your IAM policy includes:" + echo " {" + echo " \"Effect\": \"Allow\"," + echo " \"Action\": \"route53:GetHostedZone\"," + echo " \"Resource\": \"arn:aws:route53:::hostedzone/$hosted_zone_id\"" + echo " }" + + elif echo "$aws_output" | grep -q "InvalidInput"; then + echo " ⚠️ Error: Invalid hosted zone ID format" + echo "" + echo " The hosted zone ID '$hosted_zone_id' is not valid." + echo "" + echo " 🔧 How to fix:" + echo " • Use the format 'Z1234567890ABC' or '/hostedzone/Z1234567890ABC'" + echo " • Find valid zone IDs with: aws route53 list-hosted-zones" + + elif echo "$aws_output" | grep -q "Unable to locate credentials\|ExpiredToken\|InvalidClientTokenId"; then + echo " 🔑 Error: AWS credentials issue" + echo "" + echo " 💡 Possible causes:" + echo " • AWS credentials are not configured" + echo " • AWS credentials have expired" + echo " • AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY environment variables are missing" + echo "" + echo " 🔧 How to fix:" + echo " 1. Run 'aws configure' to set up credentials" + echo " 2. Or set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables" + echo " 3. If using temporary credentials, refresh your session token" + + else + echo " 📋 Error details:" + echo "$aws_output" | sed 's/^/ /' + fi + + echo "" + exit 1 +fi + +network_domain=$(echo "$aws_output" | jq -r '.HostedZone.Name' | sed 's/\.$//') + +if [ -z "$network_domain" ] || [ "$network_domain" = "null" ]; then + echo "" + echo "❌ Failed to extract domain name from hosted zone response" + echo "" + echo " 🤔 The AWS API returned successfully but the domain name could not be parsed." + echo " This is unexpected - please check the hosted zone configuration." + echo "" + exit 1 +fi + +echo "✅ Domain resolved: $network_domain" + +network_subdomain="$application_slug-$scope_slug" +echo "✅ Subdomain: $network_subdomain" + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg hosted_zone_id "$hosted_zone_id" \ + --arg domain "$network_domain" \ + --arg subdomain "$network_subdomain" \ + '. + { + network_hosted_zone_id: $hosted_zone_id, + network_domain: $domain, + network_subdomain: $subdomain + }') + +# Add module to composition list +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi diff --git a/frontend/deployment/tofu_state/aws/modules/provider.tf b/frontend/deployment/tofu_state/aws/modules/provider.tf new file mode 100644 index 00000000..c6ef3b81 --- /dev/null +++ b/frontend/deployment/tofu_state/aws/modules/provider.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">= 1.4.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } + + backend "s3" {} +} + +provider "aws" { + region = var.aws_provider.region + + default_tags { + tags = var.provider_resource_tags_json + } +} \ No newline at end of file diff --git a/frontend/deployment/tofu_state/aws/modules/variables.tf b/frontend/deployment/tofu_state/aws/modules/variables.tf new file mode 100644 index 00000000..27d2535b --- /dev/null +++ b/frontend/deployment/tofu_state/aws/modules/variables.tf @@ -0,0 +1,14 @@ +variable "aws_provider" { + description = "AWS provider configuration" + type = object({ + region = string + state_bucket = string + lock_table = string + }) +} + +variable "provider_resource_tags_json" { + description = "Resource tags as JSON object - applied as default tags to all AWS resources" + type = map(string) + default = {} +} \ No newline at end of file diff --git a/frontend/deployment/tofu_state/aws/setup b/frontend/deployment/tofu_state/aws/setup new file mode 100755 index 00000000..c12d6280 --- /dev/null +++ b/frontend/deployment/tofu_state/aws/setup @@ -0,0 +1,35 @@ +#!/bin/bash + +if [ -z "${AWS_REGION:-}" ]; then + echo "✗ AWS_REGION is not set" + exit 1 +fi + +if [ -z "${TOFU_STATE_BUCKET:-}" ]; then + echo "✗ TOFU_STATE_BUCKET is not set" + exit 1 +fi + +if [ -z "${TOFU_LOCK_TABLE:-}" ]; then + echo "✗ TOFU_LOCK_TABLE is not set" + exit 1 +fi + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg aws_region "$AWS_REGION" \ + --arg tf_state_bucket "$TOFU_STATE_BUCKET" \ + --arg tf_lock_table "$TOFU_LOCK_TABLE" \ + '. + {aws_provider: {region: $aws_region, state_bucket: $tf_state_bucket, lock_table: $tf_lock_table}}') + +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_STATE_BUCKET\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"region=$AWS_REGION\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"dynamodb_table=$TOFU_LOCK_TABLE\"" + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi \ No newline at end of file diff --git a/frontend/deployment/tofu_state/azure/modules/provider.tf b/frontend/deployment/tofu_state/azure/modules/provider.tf new file mode 100644 index 00000000..285e92ea --- /dev/null +++ b/frontend/deployment/tofu_state/azure/modules/provider.tf @@ -0,0 +1,17 @@ +terraform { + required_version = ">= 1.4.0" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.0" + } + } + + backend "azurerm" {} +} + +provider "azurerm" { + features {} + subscription_id = var.azure_provider.subscription_id +} diff --git a/frontend/deployment/tofu_state/azure/modules/variables.tf b/frontend/deployment/tofu_state/azure/modules/variables.tf new file mode 100644 index 00000000..843cad7a --- /dev/null +++ b/frontend/deployment/tofu_state/azure/modules/variables.tf @@ -0,0 +1,9 @@ +variable "azure_provider" { + description = "Azure provider configuration" + type = object({ + subscription_id = string + resource_group_name = string + storage_account_name = string + container_name = string + }) +} \ No newline at end of file diff --git a/frontend/deployment/tofu_state/azure/setup b/frontend/deployment/tofu_state/azure/setup new file mode 100755 index 00000000..bff5aa97 --- /dev/null +++ b/frontend/deployment/tofu_state/azure/setup @@ -0,0 +1,46 @@ +#!/bin/bash + +if [ -z "${AZURE_SUBSCRIPTION_ID:-}" ]; then + echo "✗ AZURE_SUBSCRIPTION_ID is not set" + exit 1 +fi + +if [ -z "${TOFU_STATE_RESOURCE_GROUP:-}" ]; then + echo "✗ TOFU_STATE_RESOURCE_GROUP is not set" + exit 1 +fi + +if [ -z "${TOFU_STATE_STORAGE_ACCOUNT:-}" ]; then + echo "✗ TOFU_STATE_STORAGE_ACCOUNT is not set" + exit 1 +fi + +if [ -z "${TOFU_STATE_CONTAINER:-}" ]; then + echo "✗ TOFU_STATE_CONTAINER is not set" + exit 1 +fi + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg subscription_id "$AZURE_SUBSCRIPTION_ID" \ + --arg resource_group "$TOFU_STATE_RESOURCE_GROUP" \ + --arg storage_account "$TOFU_STATE_STORAGE_ACCOUNT" \ + --arg container "$TOFU_STATE_CONTAINER" \ + '. + {azure_provider: { + subscription_id: $subscription_id, + resource_group_name: $resource_group, + storage_account_name: $storage_account, + container_name: $container + }}') + +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"resource_group_name=$TOFU_STATE_RESOURCE_GROUP\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"storage_account_name=$TOFU_STATE_STORAGE_ACCOUNT\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"container_name=$TOFU_STATE_CONTAINER\"" + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi diff --git a/frontend/deployment/tofu_state/gcp/modules/provider.tf b/frontend/deployment/tofu_state/gcp/modules/provider.tf new file mode 100644 index 00000000..25db4e39 --- /dev/null +++ b/frontend/deployment/tofu_state/gcp/modules/provider.tf @@ -0,0 +1,26 @@ +terraform { + required_version = ">= 1.4.0" + + required_providers { + google = { + source = "hashicorp/google" + version = "~> 5.0" + } + google-beta = { + source = "hashicorp/google-beta" + version = "~> 5.0" + } + } + + backend "gcs" {} +} + +provider "google" { + project = var.gcp_provider.project + region = var.gcp_provider.region +} + +provider "google-beta" { + project = var.gcp_provider.project + region = var.gcp_provider.region +} diff --git a/frontend/deployment/tofu_state/gcp/modules/variables.tf b/frontend/deployment/tofu_state/gcp/modules/variables.tf new file mode 100644 index 00000000..12a6b20d --- /dev/null +++ b/frontend/deployment/tofu_state/gcp/modules/variables.tf @@ -0,0 +1,8 @@ +variable "gcp_provider" { + description = "GCP provider configuration" + type = object({ + project = string + region = string + bucket = string + }) +} diff --git a/frontend/deployment/tofu_state/gcp/setup b/frontend/deployment/tofu_state/gcp/setup new file mode 100755 index 00000000..3d021672 --- /dev/null +++ b/frontend/deployment/tofu_state/gcp/setup @@ -0,0 +1,37 @@ +#!/bin/bash + +if [ -z "${GCP_PROJECT:-}" ]; then + echo "✗ GCP_PROJECT is not set" + exit 1 +fi + +if [ -z "${GCP_REGION:-}" ]; then + echo "✗ GCP_REGION is not set" + exit 1 +fi + +if [ -z "${TOFU_STATE_BUCKET:-}" ]; then + echo "✗ TOFU_STATE_BUCKET is not set" + exit 1 +fi + +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg project "$GCP_PROJECT" \ + --arg region "$GCP_REGION" \ + --arg bucket "$TOFU_STATE_BUCKET" \ + '. + {gcp_provider: { + project: $project, + region: $region, + bucket: $bucket + }}') + +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_STATE_BUCKET\"" + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$module_name" +else + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +fi \ No newline at end of file diff --git a/frontend/deployment/workflows/blue_green.yaml b/frontend/deployment/workflows/blue_green.yaml new file mode 100644 index 00000000..c5dcb0c6 --- /dev/null +++ b/frontend/deployment/workflows/blue_green.yaml @@ -0,0 +1,2 @@ +include: + - "$SERVICE_PATH/deployment/workflows/initial.yaml" \ No newline at end of file diff --git a/frontend/deployment/workflows/delete.yaml b/frontend/deployment/workflows/delete.yaml new file mode 100644 index 00000000..41962566 --- /dev/null +++ b/frontend/deployment/workflows/delete.yaml @@ -0,0 +1,8 @@ +provider_categories: + - cloud-providers +steps: + - name: tofu + type: script + file: "$SERVICE_PATH/deployment/module/provision" + configuration: + ACTION: "destroy" \ No newline at end of file diff --git a/frontend/deployment/workflows/finalize.yaml b/frontend/deployment/workflows/finalize.yaml new file mode 100644 index 00000000..7297d551 --- /dev/null +++ b/frontend/deployment/workflows/finalize.yaml @@ -0,0 +1,4 @@ +steps: + - name: no_op + type: command + command: echo "No action needed to create scope" \ No newline at end of file diff --git a/frontend/deployment/workflows/initial.yaml b/frontend/deployment/workflows/initial.yaml new file mode 100644 index 00000000..f48c9cc8 --- /dev/null +++ b/frontend/deployment/workflows/initial.yaml @@ -0,0 +1,29 @@ +provider_categories: + - cloud-providers +steps: + - name: build_context + type: script + file: "$SERVICE_PATH/deployment/build_context" + output: + - name: TOFU_VARIABLES + type: environment + - name: setup_tofu_state_layer + type: script + file: "$SERVICE_PATH/deployment/tofu_state/$TOFU_STATE_PROVIDER/setup" + - name: setup_network_layer + type: script + file: "$SERVICE_PATH/deployment/network/$NETWORK_LAYER/setup" + - name: setup_distribution_layer + type: script + file: "$SERVICE_PATH/deployment/distribution/$DISTRIBUTION_LAYER/setup" +# - name: setup_data_layer +# type: script +# file: "$SERVICE_PATH/deployment/data/$DATA_LAYER/setup" + - name: build_modules + type: script + file: "$SERVICE_PATH/deployment/compose_modules" + - name: tofu + type: script + file: "$SERVICE_PATH/deployment/do_tofu" + configuration: + ACTION: "apply" \ No newline at end of file diff --git a/frontend/deployment/workflows/rollback.yaml b/frontend/deployment/workflows/rollback.yaml new file mode 100644 index 00000000..7297d551 --- /dev/null +++ b/frontend/deployment/workflows/rollback.yaml @@ -0,0 +1,4 @@ +steps: + - name: no_op + type: command + command: echo "No action needed to create scope" \ No newline at end of file diff --git a/frontend/instance/workflows/list.yaml b/frontend/instance/workflows/list.yaml new file mode 100644 index 00000000..e69de29b diff --git a/frontend/log/workflows/log.yaml b/frontend/log/workflows/log.yaml new file mode 100644 index 00000000..e69de29b diff --git a/frontend/metric/workflows/list.yaml b/frontend/metric/workflows/list.yaml new file mode 100644 index 00000000..e69de29b diff --git a/frontend/metric/workflows/metric.yaml b/frontend/metric/workflows/metric.yaml new file mode 100644 index 00000000..e69de29b diff --git a/frontend/no_op b/frontend/no_op new file mode 100644 index 00000000..c6918a62 --- /dev/null +++ b/frontend/no_op @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "No action needed on this scope" \ No newline at end of file diff --git a/frontend/scope/workflows/create.yaml b/frontend/scope/workflows/create.yaml new file mode 100644 index 00000000..1353c84c --- /dev/null +++ b/frontend/scope/workflows/create.yaml @@ -0,0 +1,4 @@ +steps: + - name: no_op + type: command + command: "$SERVICE_PATH/scope/no_op" \ No newline at end of file diff --git a/frontend/scope/workflows/delete.yaml b/frontend/scope/workflows/delete.yaml new file mode 100644 index 00000000..5cd04cd7 --- /dev/null +++ b/frontend/scope/workflows/delete.yaml @@ -0,0 +1,2 @@ +include: + - "$SERVICE_PATH/scope/workflows/create.yaml" \ No newline at end of file diff --git a/frontend/scope/workflows/update.yaml b/frontend/scope/workflows/update.yaml new file mode 100644 index 00000000..5cd04cd7 --- /dev/null +++ b/frontend/scope/workflows/update.yaml @@ -0,0 +1,2 @@ +include: + - "$SERVICE_PATH/scope/workflows/create.yaml" \ No newline at end of file diff --git a/frontend/specs/actions/create-scope.json.tpl b/frontend/specs/actions/create-scope.json.tpl new file mode 100644 index 00000000..4c0fb3a6 --- /dev/null +++ b/frontend/specs/actions/create-scope.json.tpl @@ -0,0 +1,29 @@ +{ + "name": "create-scope", + "slug": "create-scope", + "type": "create", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id" + ], + "properties": { + "scope_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/delete-deployment.json.tpl b/frontend/specs/actions/delete-deployment.json.tpl new file mode 100644 index 00000000..0334b10c --- /dev/null +++ b/frontend/specs/actions/delete-deployment.json.tpl @@ -0,0 +1,33 @@ +{ + "name": "delete-deployment", + "slug": "delete-deployment", + "type": "custom", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id", + "deployment_id" + ], + "properties": { + "scope_id": { + "type": "string" + }, + "deployment_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/delete-scope.json.tpl b/frontend/specs/actions/delete-scope.json.tpl new file mode 100644 index 00000000..16d9b992 --- /dev/null +++ b/frontend/specs/actions/delete-scope.json.tpl @@ -0,0 +1,29 @@ +{ + "name": "delete-scope", + "slug": "delete-scope", + "type": "custom", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id" + ], + "properties": { + "scope_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/finalize-blue-green.json.tpl b/frontend/specs/actions/finalize-blue-green.json.tpl new file mode 100644 index 00000000..4dc780cb --- /dev/null +++ b/frontend/specs/actions/finalize-blue-green.json.tpl @@ -0,0 +1,33 @@ +{ + "name": "finalize-blue-green", + "slug": "finalize-blue-green", + "type": "custom", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id", + "deployment_id" + ], + "properties": { + "scope_id": { + "type": "string" + }, + "deployment_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/rollback-deployment.json.tpl b/frontend/specs/actions/rollback-deployment.json.tpl new file mode 100644 index 00000000..dcbf4cd1 --- /dev/null +++ b/frontend/specs/actions/rollback-deployment.json.tpl @@ -0,0 +1,33 @@ +{ + "name": "rollback-deployment", + "slug": "rollback-deployment", + "type": "custom", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id", + "deployment_id" + ], + "properties": { + "scope_id": { + "type": "string" + }, + "deployment_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/start-blue-green.json.tpl b/frontend/specs/actions/start-blue-green.json.tpl new file mode 100644 index 00000000..a5e387b5 --- /dev/null +++ b/frontend/specs/actions/start-blue-green.json.tpl @@ -0,0 +1,33 @@ +{ + "name": "start-blue-green", + "slug": "start-blue-green", + "type": "custom", + "retryable": false, + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id", + "deployment_id" + ], + "properties": { + "scope_id": { + "type": "string" + }, + "deployment_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/actions/start-initial.json.tpl b/frontend/specs/actions/start-initial.json.tpl new file mode 100644 index 00000000..b00708e0 --- /dev/null +++ b/frontend/specs/actions/start-initial.json.tpl @@ -0,0 +1,32 @@ +{ + "name": "start-initial", + "slug": "start-initial", + "type": "custom", + "service_specification_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "parameters": { + "schema": { + "type": "object", + "required": [ + "scope_id", + "deployment_id" + ], + "properties": { + "scope_id": { + "type": "string" + }, + "deployment_id": { + "type": "string" + } + } + }, + "values": {} + }, + "results": { + "schema": { + "type": "object", + "required": [], + "properties": {} + }, + "values": {} + } +} \ No newline at end of file diff --git a/frontend/specs/notification-channel.json.tpl b/frontend/specs/notification-channel.json.tpl new file mode 100644 index 00000000..ee3c7986 --- /dev/null +++ b/frontend/specs/notification-channel.json.tpl @@ -0,0 +1,34 @@ +{ + "nrn": "{{ env.Getenv "NRN" }}", + "status": "active", + "type": "agent", + "source": [ + "telemetry", + "service" + ], + "configuration": { + "api_key": "{{ env.Getenv "NP_API_KEY" }}", + "command": { + "data": { + "cmdline": "{{ env.Getenv "REPO_PATH" }}/entrypoint --service-path={{ env.Getenv "REPO_PATH" }}/{{ env.Getenv "SERVICE_PATH" }}", + "environment": { + "NP_ACTION_CONTEXT": "'${NOTIFICATION_CONTEXT}'" + } + }, + "type": "exec" + }, + "selector": { + "environment": "{{ env.Getenv "ENVIRONMENT" }}" + } + }, + "filters": { + "$or": [ + { + "service.specification.slug": "{{ env.Getenv "SERVICE_SLUG" }}" + }, + { + "arguments.scope_provider": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}" + } + ] + } +} \ No newline at end of file diff --git a/frontend/specs/scope-type-definition.json.tpl b/frontend/specs/scope-type-definition.json.tpl new file mode 100644 index 00000000..2d3be235 --- /dev/null +++ b/frontend/specs/scope-type-definition.json.tpl @@ -0,0 +1,9 @@ +{ + "description": "Allows you to deploy frontend applications", + "name": "Frontends", + "nrn": "{{ env.Getenv "NRN" }}", + "provider_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", + "provider_type": "service", + "status": "active", + "type": "custom" +} \ No newline at end of file diff --git a/frontend/specs/service-spec.json.tpl b/frontend/specs/service-spec.json.tpl new file mode 100644 index 00000000..6d542df1 --- /dev/null +++ b/frontend/specs/service-spec.json.tpl @@ -0,0 +1,34 @@ +{ + "assignable_to": "any", + "attributes": { + "schema": { + "properties": { + "asset_type": { + "default": "bundle", + "export": false, + "type": "string" + } + }, + "required": [], + "uiSchema": { + "elements": [], + "type": "VerticalLayout" + } + }, + "values": {} + }, + "dimensions": {}, + "name": "Frontend", + "scopes": {}, + "selectors": { + "category": "Scope", + "imported": false, + "provider": "AWS", + "sub_category": "Frontend" + }, + "type": "scope", + "use_default_actions": false, + "visible_to": [ + "{{ env.Getenv "NRN" }}" + ] +} \ No newline at end of file From 795e718449c3fa9e1053443174bfed9838186842 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 9 Jan 2026 10:56:46 -0300 Subject: [PATCH 02/40] Add first tests --- frontend/deployment/build_context | 7 +- .../{route_53 => route53}/modules/locals.tf | 0 .../{route_53 => route53}/modules/main.tf | 0 .../{route_53 => route53}/modules/outputs.tf | 0 .../modules/variables.tf | 0 .../network/{route_53 => route53}/setup | 0 .../deployment/tests/build_context_test.bats | 180 +++++++++++++++++ .../deployment/tests/resources/context.json | 183 ++++++++++++++++++ .../output/1051892149/aws_modules_provider.tf | 20 ++ .../1051892149/aws_modules_variables.tf | 14 ++ .../1051892149/cloudfront_modules_data.tf | 3 + .../1051892149/cloudfront_modules_locals.tf | 14 ++ .../1051892149/cloudfront_modules_main.tf | 88 +++++++++ .../1051892149/cloudfront_modules_outputs.tf | 44 +++++ .../cloudfront_modules_variables.tf | 26 +++ .../1051892149/route53_modules_locals.tf | 4 + .../output/1051892149/route53_modules_main.tf | 23 +++ .../1051892149/route53_modules_outputs.tf | 14 ++ .../1051892149/route53_modules_variables.tf | 15 ++ 19 files changed, 630 insertions(+), 5 deletions(-) rename frontend/deployment/network/{route_53 => route53}/modules/locals.tf (100%) rename frontend/deployment/network/{route_53 => route53}/modules/main.tf (100%) rename frontend/deployment/network/{route_53 => route53}/modules/outputs.tf (100%) rename frontend/deployment/network/{route_53 => route53}/modules/variables.tf (100%) rename frontend/deployment/network/{route_53 => route53}/setup (100%) create mode 100644 frontend/deployment/tests/build_context_test.bats create mode 100644 frontend/deployment/tests/resources/context.json create mode 100644 frontend/output/1051892149/aws_modules_provider.tf create mode 100644 frontend/output/1051892149/aws_modules_variables.tf create mode 100644 frontend/output/1051892149/cloudfront_modules_data.tf create mode 100644 frontend/output/1051892149/cloudfront_modules_locals.tf create mode 100644 frontend/output/1051892149/cloudfront_modules_main.tf create mode 100644 frontend/output/1051892149/cloudfront_modules_outputs.tf create mode 100644 frontend/output/1051892149/cloudfront_modules_variables.tf create mode 100644 frontend/output/1051892149/route53_modules_locals.tf create mode 100644 frontend/output/1051892149/route53_modules_main.tf create mode 100644 frontend/output/1051892149/route53_modules_outputs.tf create mode 100644 frontend/output/1051892149/route53_modules_variables.tf diff --git a/frontend/deployment/build_context b/frontend/deployment/build_context index b7100019..6144dd5b 100644 --- a/frontend/deployment/build_context +++ b/frontend/deployment/build_context @@ -1,11 +1,8 @@ #!/bin/bash -# ============================================================================= -# Build Context - Initializes TOFU_VARIABLES and prepares module composition -# ============================================================================= - application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +namespace_slug=$(echo "$CONTEXT" | jq -r .namespace.slug) scope_id=$(echo "$CONTEXT" | jq -r .scope.id) repository_url=$(echo "$CONTEXT" | jq -r .application.repository_url) application_version="$(echo "$CONTEXT" | jq -r .release.semver)" @@ -42,7 +39,7 @@ TOFU_VARIABLES=$(jq -n \ resource_tags_json: $resource_tags_json }') -tf_state_key="amplify/$application_slug/$scope_slug-$scope_id" +tf_state_key="frontend/$namespace_slug/$application_slug/$scope_slug-$scope_id" TOFU_INIT_VARIABLES="-backend-config=\"key=$tf_state_key\"" diff --git a/frontend/deployment/network/route_53/modules/locals.tf b/frontend/deployment/network/route53/modules/locals.tf similarity index 100% rename from frontend/deployment/network/route_53/modules/locals.tf rename to frontend/deployment/network/route53/modules/locals.tf diff --git a/frontend/deployment/network/route_53/modules/main.tf b/frontend/deployment/network/route53/modules/main.tf similarity index 100% rename from frontend/deployment/network/route_53/modules/main.tf rename to frontend/deployment/network/route53/modules/main.tf diff --git a/frontend/deployment/network/route_53/modules/outputs.tf b/frontend/deployment/network/route53/modules/outputs.tf similarity index 100% rename from frontend/deployment/network/route_53/modules/outputs.tf rename to frontend/deployment/network/route53/modules/outputs.tf diff --git a/frontend/deployment/network/route_53/modules/variables.tf b/frontend/deployment/network/route53/modules/variables.tf similarity index 100% rename from frontend/deployment/network/route_53/modules/variables.tf rename to frontend/deployment/network/route53/modules/variables.tf diff --git a/frontend/deployment/network/route_53/setup b/frontend/deployment/network/route53/setup similarity index 100% rename from frontend/deployment/network/route_53/setup rename to frontend/deployment/network/route53/setup diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/build_context_test.bats new file mode 100644 index 00000000..06beb17e --- /dev/null +++ b/frontend/deployment/tests/build_context_test.bats @@ -0,0 +1,180 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for build_context script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/build_context_test.bats +# +# Or run all tests: +# bats tests/*.bats +# ============================================================================= + +scope_id=7 + +# Setup - runs before each test +setup() { + # Get the directory of the test file + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/.." && pwd)" + + CONTEXT=$(cat "$TEST_DIR/resources/context.json") + SERVICE_PATH="$PROJECT_DIR" + TEST_OUTPUT_DIR=$(mktemp -d) + + export CONTEXT SERVICE_PATH TEST_OUTPUT_DIR +} + +# Teardown - runs after each test +teardown() { + # Clean up temp directory + if [ -d "$TEST_OUTPUT_DIR" ]; then + rm -rf "$TEST_OUTPUT_DIR" + fi +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_build_context() { + # Source the build_context script + source "$PROJECT_DIR/build_context" +} + +assert_equal() { + local actual="$1" + local expected="$2" + if [ "$actual" != "$expected" ]; then + echo "Expected: '$expected'" + echo "Actual: '$actual'" + return 1 + fi +} + +assert_contains() { + local haystack="$1" + local needle="$2" + if [[ "$haystack" != *"$needle"* ]]; then + echo "Expected string to contain: '$needle'" + echo "Actual: '$haystack'" + return 1 + fi +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "TOFU_VARIABLES matches expected structure" { + run_build_context + + # Expected JSON - update this when adding new fields + local expected='{ + "application_slug": "automation", + "application_version": "v1.0.0", + "env_vars_json": { + "CLUSTER_NAME": "development-cluster", + "TEST": "testing-tools" + }, + "repository_url": "https://github.com/playground-repos/tools-automation", + "resource_tags_json": { + "account": "playground", + "account_id": 2, + "application": "automation", + "application_id": 4, + "deployment_id": 8, + "namespace": "tools", + "namespace_id": 3, + "nullplatform": "true", + "scope": "development-tools", + "scope_id": 7 + }, + "scope_id": "7", + "scope_slug": "development-tools" +}' + + # Sort keys for consistent comparison + local actual_sorted=$(echo "$TOFU_VARIABLES" | jq -S .) + local expected_sorted=$(echo "$expected" | jq -S .) + + if [ "$actual_sorted" != "$expected_sorted" ]; then + echo "TOFU_VARIABLES does not match expected structure" + echo "" + echo "Expected:" + echo "$expected_sorted" + echo "" + echo "Actual:" + echo "$actual_sorted" + echo "" + echo "Diff:" + diff <(echo "$expected_sorted") <(echo "$actual_sorted") || true + return 1 + fi +} + +# ============================================================================= +# Test: TOFU_INIT_VARIABLES +# ============================================================================= +@test "generates correct tf_state_key format" { + run_build_context + + # Should contain the expected backend-config key + + assert_contains "$TOFU_INIT_VARIABLES" "key=frontend/tools/automation/development-tools-$scope_id" +} + +# ============================================================================= +# Test: TOFU_MODULE_DIR +# ============================================================================= +@test "creates TOFU_MODULE_DIR with scope_id" { + run_build_context + + # Should end with the scope_id + assert_contains "$TOFU_MODULE_DIR" "$SERVICE_PATH/output/$scope_id" +} + +@test "TOFU_MODULE_DIR is created as directory" { + run_build_context + + [ -d "$TOFU_MODULE_DIR" ] +} + +# ============================================================================= +# Test: MODULES_TO_USE initialization +# ============================================================================= +@test "MODULES_TO_USE is empty by default" { + unset CUSTOM_TOFU_MODULES + run_build_context + + [ -z "$MODULES_TO_USE" ] +} + +@test "MODULES_TO_USE inherits from CUSTOM_TOFU_MODULES" { + export CUSTOM_TOFU_MODULES="custom/module1,custom/module2" + run_build_context + + assert_equal "$MODULES_TO_USE" "custom/module1,custom/module2" +} + +# ============================================================================= +# Test: exports are set +# ============================================================================= +@test "exports TOFU_VARIABLES" { + run_build_context + + [ -n "$TOFU_VARIABLES" ] +} + +@test "exports TOFU_INIT_VARIABLES" { + run_build_context + + [ -n "$TOFU_INIT_VARIABLES" ] +} + +@test "exports TOFU_MODULE_DIR" { + run_build_context + + [ -n "$TOFU_MODULE_DIR" ] +} diff --git a/frontend/deployment/tests/resources/context.json b/frontend/deployment/tests/resources/context.json new file mode 100644 index 00000000..91b6ac31 --- /dev/null +++ b/frontend/deployment/tests/resources/context.json @@ -0,0 +1,183 @@ +{ + "account": { + "created_at": "2023-01-31T21:53:32.597Z", + "id": 2, + "metadata": {}, + "name": "Playground", + "nrn": "organization=1:account=2", + "organization_id": 1, + "repository_prefix": "playground-repos", + "repository_provider": "github", + "settings": {}, + "slug": "playground", + "status": "active", + "updated_at": "2023-01-31T21:53:32.597Z" + }, + "application": { + "auto_deploy_on_creation": false, + "created_at": "2025-10-07T03:22:21.385Z", + "id": 4, + "is_mono_repo": false, + "messages": [], + "metadata": {}, + "name": "Automation", + "namespace_id": 3, + "nrn": "organization=1:account=2:namespace=3:application=4", + "repository_app_path": null, + "repository_url": "https://github.com/playground-repos/tools-automation", + "settings": {}, + "slug": "automation", + "status": "active", + "tags": {}, + "template_id": 1037172878, + "updated_at": "2025-10-07T03:22:30.695Z" + }, + "asset": { + "id": 6, + "build_id": 612605537, + "name": "main", + "type": "bundle", + "url": "s3://my-asset-bucket/tools/automation/v1.0.0", + "platform": "x86_64", + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:build=5:asset=6" + }, + "deployment": { + "created_at": "2025-12-22T18:27:54.701Z", + "created_by": 123456789, + "deployment_group_id": null, + "deployment_token": "dep-token", + "expires_at": null, + "external_strategy_id": 10, + "id": 8, + "messages": [], + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:scope=7:deployment=8", + "parameters": [], + "release_id": 9, + "scope_id": 7, + "status": "creating", + "status_in_scope": "inactive", + "status_started_at": { + "creating": "2025-12-22T18:27:54.629Z" + }, + "strategy": "initial", + "strategy_data": { + "parameters": { + "metrics": { + "enabled": false, + "rules": [] + }, + "traffic": { + "enable_auto_switch": false, + "interval": 10, + "step": "0.1" + } + } + }, + "updated_at": "2025-12-23T13:22:06.345Z", + "updated_by": null + }, + "namespace": { + "account_id": 2, + "created_at": "2025-05-15T21:34:40.725Z", + "id": 3, + "metadata": {}, + "name": "Tools", + "nrn": "organization=1:account=2:namespace=3", + "slug": "tools", + "status": "active", + "updated_at": "2025-05-15T21:34:40.725Z" + }, + "parameters": { + "results": [ + { + "destination_path": null, + "id": 10, + "name": "TEST", + "type": "environment", + "values": [ + { + "id": "11", + "value": "testing-tools" + } + ], + "variable": "TEST", + "version_id": 12 + }, + { + "destination_path": null, + "id": 13, + "name": "CLUSTER_NAME", + "type": "environment", + "values": [ + { + "id": "14", + "value": "development-cluster" + } + ], + "variable": "CLUSTER_NAME", + "version_id": 15 + } + ] + }, + "providers": { + "cloud-providers": { + "account": { + "id": "aws-account-id", + "region": "us-east-2" + }, + "iam": { + "scope_workflow_role": "" + }, + "networking": { + "application_domain": true, + "hosted_public_zone_id": "public-zone-id", + "hosted_zone_id": "private-zone-id" + } + } + }, + "release": { + "application_id": 4, + "build_id": 5, + "created_at": "2025-12-12T13:07:27.435Z", + "id": 9, + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:release=9", + "semver": "v1.0.0", + "status": "active", + "updated_at": "2025-12-12T13:07:27.702Z" + }, + "scope": { + "application_id": 4, + "asset_name": "main", + "capabilities": {}, + "created_at": "2025-12-22T18:27:04.949Z", + "dimensions": { + "country": "argentina", + "environment": "development" + }, + "domain": "", + "domains": [], + "external_created": false, + "id": 7, + "instance_id": "some-instance-id", + "messages": [], + "metadata": {}, + "name": "Development tools", + "nrn": "organization=1:account=2:namespace=3:application=4:scope=7", + "profiles": [ + "environment_development", + "environment_development_country_argentina" + ], + "provider": "scope-type-id", + "requested_spec": {}, + "runtime_configurations": [], + "slug": "development-tools", + "status": "active", + "tags": [], + "tier": "important", + "type": "custom", + "updated_at": "2025-12-29T18:25:55.908Z" + } +} diff --git a/frontend/output/1051892149/aws_modules_provider.tf b/frontend/output/1051892149/aws_modules_provider.tf new file mode 100644 index 00000000..c6ef3b81 --- /dev/null +++ b/frontend/output/1051892149/aws_modules_provider.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">= 1.4.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } + + backend "s3" {} +} + +provider "aws" { + region = var.aws_provider.region + + default_tags { + tags = var.provider_resource_tags_json + } +} \ No newline at end of file diff --git a/frontend/output/1051892149/aws_modules_variables.tf b/frontend/output/1051892149/aws_modules_variables.tf new file mode 100644 index 00000000..27d2535b --- /dev/null +++ b/frontend/output/1051892149/aws_modules_variables.tf @@ -0,0 +1,14 @@ +variable "aws_provider" { + description = "AWS provider configuration" + type = object({ + region = string + state_bucket = string + lock_table = string + }) +} + +variable "provider_resource_tags_json" { + description = "Resource tags as JSON object - applied as default tags to all AWS resources" + type = map(string) + default = {} +} \ No newline at end of file diff --git a/frontend/output/1051892149/cloudfront_modules_data.tf b/frontend/output/1051892149/cloudfront_modules_data.tf new file mode 100644 index 00000000..9d4b0475 --- /dev/null +++ b/frontend/output/1051892149/cloudfront_modules_data.tf @@ -0,0 +1,3 @@ +data "aws_s3_bucket" "static" { + bucket = var.distribution_bucket_name +} diff --git a/frontend/output/1051892149/cloudfront_modules_locals.tf b/frontend/output/1051892149/cloudfront_modules_locals.tf new file mode 100644 index 00000000..abac0de8 --- /dev/null +++ b/frontend/output/1051892149/cloudfront_modules_locals.tf @@ -0,0 +1,14 @@ +locals { + distribution_origin_id = "S3-${var.distribution_bucket_name}" + distribution_aliases = var.distribution_custom_domain != null ? [var.distribution_custom_domain] : [] + + distribution_default_tags = merge(var.distribution_resource_tags_json, { + ManagedBy = "terraform" + Module = "distribution/cloudfront" + }) + + # Cross-module references (consumed by network/route53) + distribution_target_domain = aws_cloudfront_distribution.static.domain_name + distribution_target_zone_id = aws_cloudfront_distribution.static.hosted_zone_id + distribution_record_type = "A" +} diff --git a/frontend/output/1051892149/cloudfront_modules_main.tf b/frontend/output/1051892149/cloudfront_modules_main.tf new file mode 100644 index 00000000..6bc652e9 --- /dev/null +++ b/frontend/output/1051892149/cloudfront_modules_main.tf @@ -0,0 +1,88 @@ +resource "aws_cloudfront_origin_access_control" "static" { + name = "${var.distribution_app_name}-oac" + description = "OAC for ${var.distribution_app_name}" + origin_access_control_origin_type = "s3" + signing_behavior = "always" + signing_protocol = "sigv4" +} + +resource "aws_cloudfront_distribution" "static" { + enabled = true + is_ipv6_enabled = true + default_root_object = "index.html" + aliases = local.distribution_aliases + price_class = "PriceClass_100" + comment = "Distribution for ${var.distribution_app_name}" + + origin { + domain_name = data.aws_s3_bucket.static.bucket_regional_domain_name + origin_id = local.distribution_origin_id + origin_access_control_id = aws_cloudfront_origin_access_control.static.id + + origin_path = var.distribution_s3_prefix != "" ? "/${var.distribution_s3_prefix}" : "" + } + + default_cache_behavior { + allowed_methods = ["GET", "HEAD", "OPTIONS"] + cached_methods = ["GET", "HEAD"] + target_origin_id = local.distribution_origin_id + + forwarded_values { + query_string = false + cookies { + forward = "none" + } + } + + viewer_protocol_policy = "redirect-to-https" + min_ttl = 0 + default_ttl = 3600 + max_ttl = 86400 + compress = true + } + + ordered_cache_behavior { + path_pattern = "/static/*" + allowed_methods = ["GET", "HEAD"] + cached_methods = ["GET", "HEAD"] + target_origin_id = local.distribution_origin_id + + forwarded_values { + query_string = false + cookies { + forward = "none" + } + } + + viewer_protocol_policy = "redirect-to-https" + min_ttl = 86400 + default_ttl = 604800 + max_ttl = 31536000 + compress = true + } + + custom_error_response { + error_code = 404 + response_code = 200 + response_page_path = "/index.html" + } + + custom_error_response { + error_code = 403 + response_code = 200 + response_page_path = "/index.html" + } + + restrictions { + geo_restriction { + restriction_type = "none" + } + } + + viewer_certificate { + cloudfront_default_certificate = true + minimum_protocol_version = "TLSv1.2_2021" + } + + tags = local.distribution_default_tags +} diff --git a/frontend/output/1051892149/cloudfront_modules_outputs.tf b/frontend/output/1051892149/cloudfront_modules_outputs.tf new file mode 100644 index 00000000..fe503cdb --- /dev/null +++ b/frontend/output/1051892149/cloudfront_modules_outputs.tf @@ -0,0 +1,44 @@ +output "hosting_bucket_name" { + description = "S3 bucket name" + value = data.aws_s3_bucket.static.id +} + +output "hosting_bucket_arn" { + description = "S3 bucket ARN" + value = data.aws_s3_bucket.static.arn +} + +output "hosting_s3_prefix" { + description = "S3 prefix path for this scope" + value = var.hosting_s3_prefix +} + +output "hosting_cloudfront_distribution_id" { + description = "CloudFront distribution ID" + value = aws_cloudfront_distribution.static.id +} + +output "hosting_cloudfront_domain_name" { + description = "CloudFront domain name" + value = aws_cloudfront_distribution.static.domain_name +} + +output "hosting_target_domain" { + description = "Target domain for DNS records (CloudFront domain)" + value = local.hosting_target_domain +} + +output "hosting_target_zone_id" { + description = "Hosted zone ID for Route 53 alias records" + value = local.hosting_target_zone_id +} + +output "hosting_record_type" { + description = "DNS record type (A for CloudFront alias)" + value = local.hosting_record_type +} + +output "hosting_website_url" { + description = "Website URL" + value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" +} diff --git a/frontend/output/1051892149/cloudfront_modules_variables.tf b/frontend/output/1051892149/cloudfront_modules_variables.tf new file mode 100644 index 00000000..a9ef4cbc --- /dev/null +++ b/frontend/output/1051892149/cloudfront_modules_variables.tf @@ -0,0 +1,26 @@ +variable "distribution_bucket_name" { + description = "Existing S3 bucket name for static website distribution" + type = string +} + +variable "distribution_s3_prefix" { + description = "S3 prefix/path for this scope's files (e.g., 'app-name/scope-id')" + type = string +} + +variable "distribution_app_name" { + description = "Application name (used for resource naming)" + type = string +} + +variable "distribution_custom_domain" { + description = "Custom domain for CloudFront (optional)" + type = string + default = null +} + +variable "distribution_resource_tags_json" { + description = "Resource tags as JSON object" + type = map(string) + default = {} +} diff --git a/frontend/output/1051892149/route53_modules_locals.tf b/frontend/output/1051892149/route53_modules_locals.tf new file mode 100644 index 00000000..d9357d81 --- /dev/null +++ b/frontend/output/1051892149/route53_modules_locals.tf @@ -0,0 +1,4 @@ +locals { + # Compute full domain from domain + subdomain + network_full_domain = var.network_subdomain != "" ? "${var.network_subdomain}.${var.network_domain}" : var.network_domain +} \ No newline at end of file diff --git a/frontend/output/1051892149/route53_modules_main.tf b/frontend/output/1051892149/route53_modules_main.tf new file mode 100644 index 00000000..7cd8f0f7 --- /dev/null +++ b/frontend/output/1051892149/route53_modules_main.tf @@ -0,0 +1,23 @@ +resource "aws_route53_record" "main_alias" { + count = local.hosting_record_type == "A" ? 1 : 0 + + zone_id = var.network_hosted_zone_id + name = local.network_full_domain + type = "A" + + alias { + name = local.hosting_target_domain + zone_id = local.hosting_target_zone_id + evaluate_target_health = false + } +} + +resource "aws_route53_record" "main_cname" { + count = local.hosting_record_type == "CNAME" ? 1 : 0 + + zone_id = var.network_hosted_zone_id + name = local.network_full_domain + type = "CNAME" + ttl = 300 + records = [local.hosting_target_domain] +} diff --git a/frontend/output/1051892149/route53_modules_outputs.tf b/frontend/output/1051892149/route53_modules_outputs.tf new file mode 100644 index 00000000..6fa188e5 --- /dev/null +++ b/frontend/output/1051892149/route53_modules_outputs.tf @@ -0,0 +1,14 @@ +output "network_full_domain" { + description = "Full domain name (subdomain.domain or just domain)" + value = local.network_full_domain +} + +output "network_fqdn" { + description = "Fully qualified domain name" + value = local.hosting_record_type == "A" ? aws_route53_record.main_alias[0].fqdn : aws_route53_record.main_cname[0].fqdn +} + +output "network_website_url" { + description = "Website URL" + value = "https://${local.network_full_domain}" +} \ No newline at end of file diff --git a/frontend/output/1051892149/route53_modules_variables.tf b/frontend/output/1051892149/route53_modules_variables.tf new file mode 100644 index 00000000..9f67cf99 --- /dev/null +++ b/frontend/output/1051892149/route53_modules_variables.tf @@ -0,0 +1,15 @@ +variable "network_hosted_zone_id" { + description = "Route53 hosted zone ID" + type = string +} + +variable "network_domain" { + description = "Root domain name (e.g., example.com)" + type = string +} + +variable "network_subdomain" { + description = "Subdomain prefix (e.g., 'app' for app.example.com, empty string for apex)" + type = string + default = "" +} \ No newline at end of file From da1bc1a87d4d43962ff3a0b356f2f3a70020445f Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 9 Jan 2026 11:03:49 -0300 Subject: [PATCH 03/40] Improve asserts --- .../deployment/tests/build_context_test.bats | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/build_context_test.bats index 06beb17e..81ad7ee6 100644 --- a/frontend/deployment/tests/build_context_test.bats +++ b/frontend/deployment/tests/build_context_test.bats @@ -64,6 +64,33 @@ assert_contains() { fi } +assert_not_empty() { + local value="$1" + local name="${2:-value}" + if [ -z "$value" ]; then + echo "Expected $name to be non-empty, but it was empty" + return 1 + fi +} + +assert_empty() { + local value="$1" + local name="${2:-value}" + if [ -n "$value" ]; then + echo "Expected $name to be empty" + echo "Actual: '$value'" + return 1 + fi +} + +assert_directory_exists() { + local dir="$1" + if [ ! -d "$dir" ]; then + echo "Expected directory to exist: '$dir'" + return 1 + fi +} + # ============================================================================= # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= @@ -138,7 +165,7 @@ assert_contains() { @test "TOFU_MODULE_DIR is created as directory" { run_build_context - [ -d "$TOFU_MODULE_DIR" ] + assert_directory_exists "$TOFU_MODULE_DIR" } # ============================================================================= @@ -148,7 +175,7 @@ assert_contains() { unset CUSTOM_TOFU_MODULES run_build_context - [ -z "$MODULES_TO_USE" ] + assert_empty "$MODULES_TO_USE" "MODULES_TO_USE" } @test "MODULES_TO_USE inherits from CUSTOM_TOFU_MODULES" { @@ -164,17 +191,17 @@ assert_contains() { @test "exports TOFU_VARIABLES" { run_build_context - [ -n "$TOFU_VARIABLES" ] + assert_not_empty "$TOFU_VARIABLES" "TOFU_VARIABLES" } @test "exports TOFU_INIT_VARIABLES" { run_build_context - [ -n "$TOFU_INIT_VARIABLES" ] + assert_not_empty "$TOFU_INIT_VARIABLES" "TOFU_INIT_VARIABLES" } @test "exports TOFU_MODULE_DIR" { run_build_context - [ -n "$TOFU_MODULE_DIR" ] + assert_not_empty "$TOFU_MODULE_DIR" "TOFU_MODULE_DIR" } From 652a30a1f3eba89f7cc60ac37e3c957e5a72b810 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 9 Jan 2026 12:07:35 -0300 Subject: [PATCH 04/40] Add bash tests --- .../deployment/distribution/firebase/setup | 2 +- .../deployment/distribution/gcs-cdn/setup | 2 +- frontend/deployment/network/cloud_dns/setup | 2 +- .../aws/modules/provider.tf | 0 .../aws/modules/variables.tf | 0 .../{tofu_state => provider}/aws/setup | 8 +- .../azure/modules/provider.tf | 0 .../azure/modules/variables.tf | 0 .../{tofu_state => provider}/azure/setup | 24 +- .../gcp/modules/provider.tf | 0 .../gcp/modules/variables.tf | 0 .../{tofu_state => provider}/gcp/setup | 8 +- .../deployment/tests/build_context_test.bats | 68 +----- .../distribution/cloudfront/setup_test.bats | 154 +++++++++++++ .../tests/network/route53/setup_test.bats | 215 ++++++++++++++++++ .../tests/provider/aws/setup_test.bats | 168 ++++++++++++++ .../deployment/tests/resources/aws_mocks/aws | 18 ++ .../aws_mocks/route53/access_denied.json | 1 + .../aws_mocks/route53/credentials_error.json | 1 + .../aws_mocks/route53/empty_domain.json | 10 + .../aws_mocks/route53/invalid_input.json | 1 + .../aws_mocks/route53/no_such_zone.json | 1 + .../resources/aws_mocks/route53/success.json | 20 ++ .../np_mocks/asset_repository/auth_error.json | 3 + .../asset_repository/no_bucket_data.json | 27 +++ .../np_mocks/asset_repository/no_data.json | 3 + .../np_mocks/asset_repository/success.json | 49 ++++ .../deployment/tests/resources/np_mocks/np | 18 ++ frontend/deployment/tests/run_tests.sh | 65 ++++++ frontend/deployment/tests/test_utils.bash | 90 ++++++++ frontend/deployment/workflows/initial.yaml | 4 +- 31 files changed, 873 insertions(+), 89 deletions(-) rename frontend/deployment/{tofu_state => provider}/aws/modules/provider.tf (100%) rename frontend/deployment/{tofu_state => provider}/aws/modules/variables.tf (100%) rename frontend/deployment/{tofu_state => provider}/aws/setup (84%) rename frontend/deployment/{tofu_state => provider}/azure/modules/provider.tf (100%) rename frontend/deployment/{tofu_state => provider}/azure/modules/variables.tf (100%) rename frontend/deployment/{tofu_state => provider}/azure/setup (58%) rename frontend/deployment/{tofu_state => provider}/gcp/modules/provider.tf (100%) rename frontend/deployment/{tofu_state => provider}/gcp/modules/variables.tf (100%) rename frontend/deployment/{tofu_state => provider}/gcp/setup (81%) create mode 100644 frontend/deployment/tests/distribution/cloudfront/setup_test.bats create mode 100644 frontend/deployment/tests/network/route53/setup_test.bats create mode 100644 frontend/deployment/tests/provider/aws/setup_test.bats create mode 100755 frontend/deployment/tests/resources/aws_mocks/aws create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/access_denied.json create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/credentials_error.json create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/empty_domain.json create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/invalid_input.json create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/no_such_zone.json create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/success.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository/auth_error.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository/no_bucket_data.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository/no_data.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository/success.json create mode 100755 frontend/deployment/tests/resources/np_mocks/np create mode 100755 frontend/deployment/tests/run_tests.sh create mode 100644 frontend/deployment/tests/test_utils.bash diff --git a/frontend/deployment/distribution/firebase/setup b/frontend/deployment/distribution/firebase/setup index 9b6892ff..e2883c6a 100755 --- a/frontend/deployment/distribution/firebase/setup +++ b/frontend/deployment/distribution/firebase/setup @@ -7,7 +7,7 @@ hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') if [ -z "$hosting_project_id" ]; then - echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + echo "✗ GCP project not configured. Run provider/gcp setup first." exit 1 fi diff --git a/frontend/deployment/distribution/gcs-cdn/setup b/frontend/deployment/distribution/gcs-cdn/setup index df40afb7..024ab570 100755 --- a/frontend/deployment/distribution/gcs-cdn/setup +++ b/frontend/deployment/distribution/gcs-cdn/setup @@ -8,7 +8,7 @@ hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // em hosting_region=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.region // "us-central1"') if [ -z "$hosting_project_id" ]; then - echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + echo "✗ GCP project not configured. Run provider/gcp setup first." exit 1 fi diff --git a/frontend/deployment/network/cloud_dns/setup b/frontend/deployment/network/cloud_dns/setup index e491ebfb..23a3d0bc 100755 --- a/frontend/deployment/network/cloud_dns/setup +++ b/frontend/deployment/network/cloud_dns/setup @@ -6,7 +6,7 @@ network_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') if [ -z "$network_project_id" ]; then - echo "✗ GCP project not configured. Run tofu_state/gcp setup first." + echo "✗ GCP project not configured. Run provider/gcp setup first." exit 1 fi diff --git a/frontend/deployment/tofu_state/aws/modules/provider.tf b/frontend/deployment/provider/aws/modules/provider.tf similarity index 100% rename from frontend/deployment/tofu_state/aws/modules/provider.tf rename to frontend/deployment/provider/aws/modules/provider.tf diff --git a/frontend/deployment/tofu_state/aws/modules/variables.tf b/frontend/deployment/provider/aws/modules/variables.tf similarity index 100% rename from frontend/deployment/tofu_state/aws/modules/variables.tf rename to frontend/deployment/provider/aws/modules/variables.tf diff --git a/frontend/deployment/tofu_state/aws/setup b/frontend/deployment/provider/aws/setup similarity index 84% rename from frontend/deployment/tofu_state/aws/setup rename to frontend/deployment/provider/aws/setup index c12d6280..f2e168e4 100755 --- a/frontend/deployment/tofu_state/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -5,8 +5,8 @@ if [ -z "${AWS_REGION:-}" ]; then exit 1 fi -if [ -z "${TOFU_STATE_BUCKET:-}" ]; then - echo "✗ TOFU_STATE_BUCKET is not set" +if [ -z "${TOFU_PROVIDER_BUCKET:-}" ]; then + echo "✗ TOFU_PROVIDER_BUCKET is not set" exit 1 fi @@ -17,11 +17,11 @@ fi TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg aws_region "$AWS_REGION" \ - --arg tf_state_bucket "$TOFU_STATE_BUCKET" \ + --arg tf_state_bucket "$TOFU_PROVIDER_BUCKET" \ --arg tf_lock_table "$TOFU_LOCK_TABLE" \ '. + {aws_provider: {region: $aws_region, state_bucket: $tf_state_bucket, lock_table: $tf_lock_table}}') -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_STATE_BUCKET\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_PROVIDER_BUCKET\"" TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"region=$AWS_REGION\"" TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"dynamodb_table=$TOFU_LOCK_TABLE\"" diff --git a/frontend/deployment/tofu_state/azure/modules/provider.tf b/frontend/deployment/provider/azure/modules/provider.tf similarity index 100% rename from frontend/deployment/tofu_state/azure/modules/provider.tf rename to frontend/deployment/provider/azure/modules/provider.tf diff --git a/frontend/deployment/tofu_state/azure/modules/variables.tf b/frontend/deployment/provider/azure/modules/variables.tf similarity index 100% rename from frontend/deployment/tofu_state/azure/modules/variables.tf rename to frontend/deployment/provider/azure/modules/variables.tf diff --git a/frontend/deployment/tofu_state/azure/setup b/frontend/deployment/provider/azure/setup similarity index 58% rename from frontend/deployment/tofu_state/azure/setup rename to frontend/deployment/provider/azure/setup index bff5aa97..ff2216d8 100755 --- a/frontend/deployment/tofu_state/azure/setup +++ b/frontend/deployment/provider/azure/setup @@ -5,26 +5,26 @@ if [ -z "${AZURE_SUBSCRIPTION_ID:-}" ]; then exit 1 fi -if [ -z "${TOFU_STATE_RESOURCE_GROUP:-}" ]; then - echo "✗ TOFU_STATE_RESOURCE_GROUP is not set" +if [ -z "${TOFU_PROVIDER_RESOURCE_GROUP:-}" ]; then + echo "✗ TOFU_PROVIDER_RESOURCE_GROUP is not set" exit 1 fi -if [ -z "${TOFU_STATE_STORAGE_ACCOUNT:-}" ]; then - echo "✗ TOFU_STATE_STORAGE_ACCOUNT is not set" +if [ -z "${TOFU_PROVIDER_STORAGE_ACCOUNT:-}" ]; then + echo "✗ TOFU_PROVIDER_STORAGE_ACCOUNT is not set" exit 1 fi -if [ -z "${TOFU_STATE_CONTAINER:-}" ]; then - echo "✗ TOFU_STATE_CONTAINER is not set" +if [ -z "${TOFU_PROVIDER_CONTAINER:-}" ]; then + echo "✗ TOFU_PROVIDER_CONTAINER is not set" exit 1 fi TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg subscription_id "$AZURE_SUBSCRIPTION_ID" \ - --arg resource_group "$TOFU_STATE_RESOURCE_GROUP" \ - --arg storage_account "$TOFU_STATE_STORAGE_ACCOUNT" \ - --arg container "$TOFU_STATE_CONTAINER" \ + --arg resource_group "$TOFU_PROVIDER_RESOURCE_GROUP" \ + --arg storage_account "$TOFU_PROVIDER_STORAGE_ACCOUNT" \ + --arg container "$TOFU_PROVIDER_CONTAINER" \ '. + {azure_provider: { subscription_id: $subscription_id, resource_group_name: $resource_group, @@ -32,9 +32,9 @@ TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ container_name: $container }}') -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"resource_group_name=$TOFU_STATE_RESOURCE_GROUP\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"storage_account_name=$TOFU_STATE_STORAGE_ACCOUNT\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"container_name=$TOFU_STATE_CONTAINER\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"resource_group_name=$TOFU_PROVIDER_RESOURCE_GROUP\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"storage_account_name=$TOFU_PROVIDER_STORAGE_ACCOUNT\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"container_name=$TOFU_PROVIDER_CONTAINER\"" script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/tofu_state/gcp/modules/provider.tf b/frontend/deployment/provider/gcp/modules/provider.tf similarity index 100% rename from frontend/deployment/tofu_state/gcp/modules/provider.tf rename to frontend/deployment/provider/gcp/modules/provider.tf diff --git a/frontend/deployment/tofu_state/gcp/modules/variables.tf b/frontend/deployment/provider/gcp/modules/variables.tf similarity index 100% rename from frontend/deployment/tofu_state/gcp/modules/variables.tf rename to frontend/deployment/provider/gcp/modules/variables.tf diff --git a/frontend/deployment/tofu_state/gcp/setup b/frontend/deployment/provider/gcp/setup similarity index 81% rename from frontend/deployment/tofu_state/gcp/setup rename to frontend/deployment/provider/gcp/setup index 3d021672..a91cd7c6 100755 --- a/frontend/deployment/tofu_state/gcp/setup +++ b/frontend/deployment/provider/gcp/setup @@ -10,22 +10,22 @@ if [ -z "${GCP_REGION:-}" ]; then exit 1 fi -if [ -z "${TOFU_STATE_BUCKET:-}" ]; then - echo "✗ TOFU_STATE_BUCKET is not set" +if [ -z "${TOFU_PROVIDER_BUCKET:-}" ]; then + echo "✗ TOFU_PROVIDER_BUCKET is not set" exit 1 fi TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg project "$GCP_PROJECT" \ --arg region "$GCP_REGION" \ - --arg bucket "$TOFU_STATE_BUCKET" \ + --arg bucket "$TOFU_PROVIDER_BUCKET" \ '. + {gcp_provider: { project: $project, region: $region, bucket: $bucket }}') -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_STATE_BUCKET\"" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_PROVIDER_BUCKET\"" script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/build_context_test.bats index 81ad7ee6..7d93ff7a 100644 --- a/frontend/deployment/tests/build_context_test.bats +++ b/frontend/deployment/tests/build_context_test.bats @@ -21,6 +21,9 @@ setup() { TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/.." && pwd)" + # Load shared test utilities + source "$TEST_DIR/test_utils.bash" + CONTEXT=$(cat "$TEST_DIR/resources/context.json") SERVICE_PATH="$PROJECT_DIR" TEST_OUTPUT_DIR=$(mktemp -d) @@ -44,53 +47,6 @@ run_build_context() { source "$PROJECT_DIR/build_context" } -assert_equal() { - local actual="$1" - local expected="$2" - if [ "$actual" != "$expected" ]; then - echo "Expected: '$expected'" - echo "Actual: '$actual'" - return 1 - fi -} - -assert_contains() { - local haystack="$1" - local needle="$2" - if [[ "$haystack" != *"$needle"* ]]; then - echo "Expected string to contain: '$needle'" - echo "Actual: '$haystack'" - return 1 - fi -} - -assert_not_empty() { - local value="$1" - local name="${2:-value}" - if [ -z "$value" ]; then - echo "Expected $name to be non-empty, but it was empty" - return 1 - fi -} - -assert_empty() { - local value="$1" - local name="${2:-value}" - if [ -n "$value" ]; then - echo "Expected $name to be empty" - echo "Actual: '$value'" - return 1 - fi -} - -assert_directory_exists() { - local dir="$1" - if [ ! -d "$dir" ]; then - echo "Expected directory to exist: '$dir'" - return 1 - fi -} - # ============================================================================= # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= @@ -122,23 +78,7 @@ assert_directory_exists() { "scope_slug": "development-tools" }' - # Sort keys for consistent comparison - local actual_sorted=$(echo "$TOFU_VARIABLES" | jq -S .) - local expected_sorted=$(echo "$expected" | jq -S .) - - if [ "$actual_sorted" != "$expected_sorted" ]; then - echo "TOFU_VARIABLES does not match expected structure" - echo "" - echo "Expected:" - echo "$expected_sorted" - echo "" - echo "Actual:" - echo "$actual_sorted" - echo "" - echo "Diff:" - diff <(echo "$expected_sorted") <(echo "$actual_sorted") || true - return 1 - fi + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } # ============================================================================= diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats new file mode 100644 index 00000000..49849f32 --- /dev/null +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -0,0 +1,154 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for distribution/cloudfront/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/distribution/cloudfront/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/distribution/cloudfront/setup" + RESOURCES_DIR="$PROJECT_DIR/tests/resources" + MOCKS_DIR="$RESOURCES_DIR/np_mocks" + + # Load shared test utilities + source "$PROJECT_DIR/tests/test_utils.bash" + + # Add mock np to PATH (must be first) + export PATH="$MOCKS_DIR:$PATH" + + # Load context + export CONTEXT=$(cat "$RESOURCES_DIR/context.json") + + # Initialize TOFU_VARIABLES with required fields + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export MODULES_TO_USE="" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_cloudfront_setup() { + source "$SCRIPT_PATH" +} + +set_np_mock() { + local mock_file="$1" + local exit_code="${2:-0}" + export NP_MOCK_RESPONSE="$MOCKS_DIR/asset_repository/$mock_file" + export NP_MOCK_EXIT_CODE="$exit_code" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "TOFU_VARIABLES matches expected structure on success" { + set_np_mock "success.json" + + run_cloudfront_setup + + # Expected JSON - update this when adding new fields + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "hosting_bucket_name": "assets-kwik-e-mart-main", + "hosting_app_name": "automation-development-tools-7", + "hosting_s3_prefix": "/app" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "adds module to MODULES_TO_USE when empty" { + set_np_mock "success.json" + + run_cloudfront_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/distribution/cloudfront/modules" +} + +@test "appends module to existing MODULES_TO_USE" { + set_np_mock "success.json" + export MODULES_TO_USE="existing/module" + + run_cloudfront_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/distribution/cloudfront/modules" +} + +# ============================================================================= +# Test: Auth error case +# ============================================================================= +@test "fails with auth error" { + set_np_mock "auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to fetch assets-repository provider" +} + +@test "shows permission denied message for 403 error" { + set_np_mock "auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Permission denied" +} + +# ============================================================================= +# Test: No providers found case +# ============================================================================= +@test "fails when no bucket data in providers" { + set_np_mock "no_bucket_data.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "No S3 bucket found in assets-repository providers" +} + +@test "shows provider count when no bucket found" { + set_np_mock "no_bucket_data.json" + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Found 1 provider(s)" +} + +# ============================================================================= +# Test: Empty results case +# ============================================================================= +@test "fails when no providers returned" { + set_np_mock "no_data.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "No S3 bucket found in assets-repository providers" +} + +@test "shows zero providers found" { + set_np_mock "no_data.json" + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Found 0 provider(s)" +} + diff --git a/frontend/deployment/tests/network/route53/setup_test.bats b/frontend/deployment/tests/network/route53/setup_test.bats new file mode 100644 index 00000000..70c4fcd8 --- /dev/null +++ b/frontend/deployment/tests/network/route53/setup_test.bats @@ -0,0 +1,215 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for network/route53/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/network/route53/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/network/route53/setup" + RESOURCES_DIR="$PROJECT_DIR/tests/resources" + MOCKS_DIR="$RESOURCES_DIR/aws_mocks" + + # Load shared test utilities + source "$PROJECT_DIR/tests/test_utils.bash" + + # Add mock aws to PATH (must be first) + export PATH="$MOCKS_DIR:$PATH" + + # Load context with hosted_public_zone_id + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools"}, + "providers": { + "cloud-providers": { + "networking": { + "hosted_public_zone_id": "Z1234567890ABC" + } + } + } + }' + + # Initialize TOFU_VARIABLES with existing keys to verify script merges (not replaces) + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export MODULES_TO_USE="" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_route53_setup() { + source "$SCRIPT_PATH" +} + +set_aws_mock() { + local mock_file="$1" + local exit_code="${2:-0}" + export AWS_MOCK_RESPONSE="$MOCKS_DIR/route53/$mock_file" + export AWS_MOCK_EXIT_CODE="$exit_code" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "TOFU_VARIABLES matches expected structure on success" { + set_aws_mock "success.json" + + run_route53_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "network_hosted_zone_id": "Z1234567890ABC", + "network_domain": "example.com", + "network_subdomain": "automation-development-tools" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "adds module to MODULES_TO_USE when empty" { + set_aws_mock "success.json" + + run_route53_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/network/route53/modules" +} + +@test "appends module to existing MODULES_TO_USE" { + set_aws_mock "success.json" + export MODULES_TO_USE="existing/module" + + run_route53_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/network/route53/modules" +} + +# ============================================================================= +# Test: Missing hosted_zone_id in context +# ============================================================================= +@test "fails when hosted_public_zone_id is missing from context" { + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools"}, + "providers": { + "cloud-providers": { + "networking": {} + } + } + }' + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "hosted_public_zone_id is not set in context" +} + +# ============================================================================= +# Test: NoSuchHostedZone error +# ============================================================================= +@test "fails when hosted zone does not exist" { + set_aws_mock "no_such_zone.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to fetch Route 53 hosted zone information" +} + +@test "shows helpful message for NoSuchHostedZone error" { + set_aws_mock "no_such_zone.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Hosted zone" + assert_contains "$output" "does not exist" +} + +# ============================================================================= +# Test: AccessDenied error +# ============================================================================= +@test "fails when access is denied" { + set_aws_mock "access_denied.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to fetch Route 53 hosted zone information" +} + +@test "shows permission denied message for AccessDenied error" { + set_aws_mock "access_denied.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Permission denied" +} + +# ============================================================================= +# Test: InvalidInput error +# ============================================================================= +@test "fails when hosted zone ID is invalid" { + set_aws_mock "invalid_input.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to fetch Route 53 hosted zone information" +} + +@test "shows invalid format message for InvalidInput error" { + set_aws_mock "invalid_input.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Invalid hosted zone ID format" +} + +# ============================================================================= +# Test: Credentials error +# ============================================================================= +@test "fails when AWS credentials are missing" { + set_aws_mock "credentials_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to fetch Route 53 hosted zone information" +} + +@test "shows credentials message for credentials error" { + set_aws_mock "credentials_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "AWS credentials issue" +} + +# ============================================================================= +# Test: Empty domain in response +# ============================================================================= +@test "fails when domain cannot be extracted from response" { + set_aws_mock "empty_domain.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to extract domain name from hosted zone response" +} diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats new file mode 100644 index 00000000..fbc85714 --- /dev/null +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -0,0 +1,168 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for provider/aws/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/aws/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/provider/aws/setup" + + # Load shared test utilities + source "$PROJECT_DIR/tests/test_utils.bash" + + # Initialize variables that the script expects to exist + export TOFU_VARIABLES='{}' + export TOFU_INIT_VARIABLES="" + export MODULES_TO_USE="" + + # Set required AWS variables + export AWS_REGION="us-east-1" + export TOFU_PROVIDER_BUCKET="my-state-bucket" + export TOFU_LOCK_TABLE="my-lock-table" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_aws_setup() { + source "$SCRIPT_PATH" +} + +# ============================================================================= +# Test: Required environment variables +# ============================================================================= +@test "fails when AWS_REGION is not set" { + unset AWS_REGION + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "AWS_REGION is not set" +} + +@test "fails when TOFU_PROVIDER_BUCKET is not set" { + unset TOFU_PROVIDER_BUCKET + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "TOFU_PROVIDER_BUCKET is not set" +} + +@test "fails when TOFU_LOCK_TABLE is not set" { + unset TOFU_LOCK_TABLE + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "TOFU_LOCK_TABLE is not set" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - aws_provider object +# ============================================================================= +@test "adds aws_provider to TOFU_VARIABLES" { + run_aws_setup + + local aws_provider=$(echo "$TOFU_VARIABLES" | jq '.aws_provider') + assert_not_empty "$aws_provider" "aws_provider" +} + +@test "aws_provider contains correct region" { + run_aws_setup + + local region=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.region') + assert_equal "$region" "us-east-1" +} + +@test "aws_provider contains correct state_bucket" { + run_aws_setup + + local bucket=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.state_bucket') + assert_equal "$bucket" "my-state-bucket" +} + +@test "aws_provider contains correct lock_table" { + run_aws_setup + + local table=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.lock_table') + assert_equal "$table" "my-lock-table" +} + +@test "TOFU_VARIABLES aws_provider matches expected structure" { + run_aws_setup + + local expected='{ + "region": "us-east-1", + "state_bucket": "my-state-bucket", + "lock_table": "my-lock-table" +}' + + local actual=$(echo "$TOFU_VARIABLES" | jq -S '.aws_provider') + local expected_sorted=$(echo "$expected" | jq -S .) + + if [ "$actual" != "$expected_sorted" ]; then + echo "aws_provider does not match expected structure" + echo "" + echo "Expected:" + echo "$expected_sorted" + echo "" + echo "Actual:" + echo "$actual" + return 1 + fi +} + +# ============================================================================= +# Test: TOFU_INIT_VARIABLES - backend config +# ============================================================================= +@test "TOFU_INIT_VARIABLES contains bucket backend-config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="bucket=my-state-bucket"' +} + +@test "TOFU_INIT_VARIABLES contains region backend-config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="region=us-east-1"' +} + +@test "TOFU_INIT_VARIABLES contains dynamodb_table backend-config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="dynamodb_table=my-lock-table"' +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "MODULES_TO_USE contains aws modules path" { + run_aws_setup + + assert_contains "$MODULES_TO_USE" "provider/aws/modules" +} + +@test "MODULES_TO_USE appends to existing modules" { + export MODULES_TO_USE="existing/module" + run_aws_setup + + assert_contains "$MODULES_TO_USE" "existing/module" + assert_contains "$MODULES_TO_USE" "provider/aws/modules" +} + +@test "MODULES_TO_USE uses comma separator when appending" { + export MODULES_TO_USE="existing/module" + run_aws_setup + + assert_contains "$MODULES_TO_USE" "existing/module," +} diff --git a/frontend/deployment/tests/resources/aws_mocks/aws b/frontend/deployment/tests/resources/aws_mocks/aws new file mode 100755 index 00000000..0ab580f3 --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/aws @@ -0,0 +1,18 @@ +#!/bin/bash +# Mock aws CLI for testing +# Set AWS_MOCK_RESPONSE to the path of the mock file to return +# Set AWS_MOCK_EXIT_CODE to the exit code (default: 0) + +if [ -z "$AWS_MOCK_RESPONSE" ]; then + echo "AWS_MOCK_RESPONSE not set" >&2 + exit 1 +fi + +if [ -f "$AWS_MOCK_RESPONSE" ]; then + cat "$AWS_MOCK_RESPONSE" +else + echo "Mock file not found: $AWS_MOCK_RESPONSE" >&2 + exit 1 +fi + +exit "${AWS_MOCK_EXIT_CODE:-0}" diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/access_denied.json b/frontend/deployment/tests/resources/aws_mocks/route53/access_denied.json new file mode 100644 index 00000000..13ad79c6 --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/access_denied.json @@ -0,0 +1 @@ +An error occurred (AccessDenied) when calling the GetHostedZone operation: User: arn:aws:iam::123456789012:user/testuser is not authorized to perform: route53:GetHostedZone on resource: arn:aws:route53:::hostedzone/Z1234567890ABC diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/credentials_error.json b/frontend/deployment/tests/resources/aws_mocks/route53/credentials_error.json new file mode 100644 index 00000000..81dcfbb7 --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/credentials_error.json @@ -0,0 +1 @@ +Unable to locate credentials. You can configure credentials by running "aws configure". diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/empty_domain.json b/frontend/deployment/tests/resources/aws_mocks/route53/empty_domain.json new file mode 100644 index 00000000..73956353 --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/empty_domain.json @@ -0,0 +1,10 @@ +{ + "HostedZone": { + "Id": "/hostedzone/Z1234567890ABC", + "Name": null, + "CallerReference": "2024-01-15T10:30:00Z", + "Config": { + "PrivateZone": false + } + } +} diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/invalid_input.json b/frontend/deployment/tests/resources/aws_mocks/route53/invalid_input.json new file mode 100644 index 00000000..e3a344ca --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/invalid_input.json @@ -0,0 +1 @@ +An error occurred (InvalidInput) when calling the GetHostedZone operation: Invalid id: not-a-valid-zone-id diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/no_such_zone.json b/frontend/deployment/tests/resources/aws_mocks/route53/no_such_zone.json new file mode 100644 index 00000000..142d504e --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/no_such_zone.json @@ -0,0 +1 @@ +An error occurred (NoSuchHostedZone) when calling the GetHostedZone operation: No hosted zone found with ID: Z9999999999XXX diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/success.json b/frontend/deployment/tests/resources/aws_mocks/route53/success.json new file mode 100644 index 00000000..56d84baa --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/success.json @@ -0,0 +1,20 @@ +{ + "HostedZone": { + "Id": "/hostedzone/Z1234567890ABC", + "Name": "example.com.", + "CallerReference": "2024-01-15T10:30:00Z", + "Config": { + "Comment": "Production hosted zone", + "PrivateZone": false + }, + "ResourceRecordSetCount": 42 + }, + "DelegationSet": { + "NameServers": [ + "ns-1234.awsdns-12.org", + "ns-567.awsdns-34.com", + "ns-890.awsdns-56.co.uk", + "ns-123.awsdns-78.net" + ] + } +} diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/auth_error.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/auth_error.json new file mode 100644 index 00000000..f3a69bc4 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/auth_error.json @@ -0,0 +1,3 @@ +{ + "error": "provider specification fetch error: request failed with status 403: {\"statusCode\":403,\"code\":\"FST_ERR_AUTHORIZATION\",\"error\":\"Forbidden\",\"message\":\"Authorization error, insufficient permissions to access the requested resource. Insufficient permissions to access the requested resource\"}" +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/no_bucket_data.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/no_bucket_data.json new file mode 100644 index 00000000..5315ddda --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/no_bucket_data.json @@ -0,0 +1,27 @@ +{ + "results": [ + { + "attributes": { + "ci": { + "access_key": "", + "region": "us-east-1", + "secret_key": "" + }, + "setup": { + "naming_rule": "\"\\(.namespace.slug)/\\(.application.slug)\"", + "region": "us-east-1", + "role_arn": "arn:aws:iam::688720756067:role/null-application-manager" + } + }, + "category": "assets-repository", + "created_at": "2024-10-25T12:47:52.552Z", + "dimensions": {}, + "groups": [], + "id": "d397e46b-89b8-419d-ac14-2b483ace511c", + "nrn": "organization=1255165411:account=95118862", + "specification_id": "a6cf5ddf-ee3e-4960-b56a-ee9a13f8b5d2", + "tags": [], + "updated_at": "2025-08-21T01:58:19.844Z" + } + ] +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/no_data.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/no_data.json new file mode 100644 index 00000000..914332ed --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/no_data.json @@ -0,0 +1,3 @@ +{ + "results": [] +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json new file mode 100644 index 00000000..8fd527a1 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json @@ -0,0 +1,49 @@ +{ + "results": [ + { + "attributes": { + "bucket": { + "name": "assets-kwik-e-mart-main" + } + }, + "category": "assets-repository", + "created_at": "2026-01-07T16:28:17.036Z", + "dimensions": {}, + "groups": [], + "id": "4a7be073-92ee-4f66-91be-02d115bc3e7c", + "nrn": "organization=1255165411:account=95118862", + "specification_id": "85e164dc-3149-40c6-b85d-28bddf6e21e8", + "tags": [ + { + "id": "ceb2021b-714e-4fa5-9202-6965c744ffd9", + "key": "bucket.name", + "value": "assets-kwik-e-mart-main" + } + ], + "updated_at": "2026-01-07T16:28:17.036Z" + }, + { + "attributes": { + "ci": { + "access_key": "", + "region": "us-east-1", + "secret_key": "" + }, + "setup": { + "naming_rule": "\"\\(.namespace.slug)/\\(.application.slug)\"", + "region": "us-east-1", + "role_arn": "arn:aws:iam::688720756067:role/null-application-manager" + } + }, + "category": "assets-repository", + "created_at": "2024-10-25T12:47:52.552Z", + "dimensions": {}, + "groups": [], + "id": "d397e46b-89b8-419d-ac14-2b483ace511c", + "nrn": "organization=1255165411:account=95118862", + "specification_id": "a6cf5ddf-ee3e-4960-b56a-ee9a13f8b5d2", + "tags": [], + "updated_at": "2025-08-21T01:58:19.844Z" + } + ] +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/np b/frontend/deployment/tests/resources/np_mocks/np new file mode 100755 index 00000000..195c6220 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/np @@ -0,0 +1,18 @@ +#!/bin/bash +# Mock np CLI for testing +# Set NP_MOCK_RESPONSE to the path of the mock file to return +# Set NP_MOCK_EXIT_CODE to the exit code (default: 0) + +if [ -z "$NP_MOCK_RESPONSE" ]; then + echo "NP_MOCK_RESPONSE not set" >&2 + exit 1 +fi + +if [ -f "$NP_MOCK_RESPONSE" ]; then + cat "$NP_MOCK_RESPONSE" +else + echo "Mock file not found: $NP_MOCK_RESPONSE" >&2 + exit 1 +fi + +exit "${NP_MOCK_EXIT_CODE:-0}" diff --git a/frontend/deployment/tests/run_tests.sh b/frontend/deployment/tests/run_tests.sh new file mode 100755 index 00000000..2ffa4062 --- /dev/null +++ b/frontend/deployment/tests/run_tests.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# ============================================================================= +# Test runner for all BATS tests +# +# Usage: +# ./tests/run_tests.sh # Run all tests +# ./tests/run_tests.sh aws # Run tests in aws/ directory +# ./tests/run_tests.sh build_context # Run specific test file +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + +# Check if bats is installed +if ! command -v bats &> /dev/null; then + echo -e "${RED}bats-core is not installed${NC}" + echo "" + echo "Install with:" + echo " brew install bats-core # macOS" + echo " apt install bats # Ubuntu/Debian" + exit 1 +fi + +# Check if jq is installed +if ! command -v jq &> /dev/null; then + echo -e "${RED}jq is not installed${NC}" + echo "" + echo "Install with:" + echo " brew install jq # macOS" + echo " apt install jq # Ubuntu/Debian" + exit 1 +fi + +# Run specific test or all tests +if [ -n "$1" ]; then + # Check if it's a directory + if [ -d "$1" ]; then + echo "Running tests in $1/" + bats "$1"/*.bats + # Check if it's a file (with or without .bats extension) + elif [ -f "${1}_test.bats" ]; then + echo "Running ${1}_test.bats" + bats "${1}_test.bats" + elif [ -f "$1" ]; then + echo "Running $1" + bats "$1" + else + echo -e "${RED}Test not found: $1${NC}" + exit 1 + fi +else + echo "Running all tests..." + echo "" + bats ./*.bats ./**/*.bats +fi + +echo "" +echo -e "${GREEN}All tests passed!${NC}" diff --git a/frontend/deployment/tests/test_utils.bash b/frontend/deployment/tests/test_utils.bash new file mode 100644 index 00000000..46102a38 --- /dev/null +++ b/frontend/deployment/tests/test_utils.bash @@ -0,0 +1,90 @@ +# ============================================================================= +# Shared test utilities for BATS tests +# +# Usage: Add this line at the top of your .bats file's setup() function: +# source "$TEST_DIR/test_utils.bash" +# # or if in a subdirectory: +# source "$TEST_DIR/../test_utils.bash" +# ============================================================================= + +# ============================================================================= +# Assertion functions +# ============================================================================= + +assert_equal() { + local actual="$1" + local expected="$2" + if [ "$actual" != "$expected" ]; then + echo "Expected: '$expected'" + echo "Actual: '$actual'" + return 1 + fi +} + +assert_contains() { + local haystack="$1" + local needle="$2" + if [[ "$haystack" != *"$needle"* ]]; then + echo "Expected string to contain: '$needle'" + echo "Actual: '$haystack'" + return 1 + fi +} + +assert_not_empty() { + local value="$1" + local name="${2:-value}" + if [ -z "$value" ]; then + echo "Expected $name to be non-empty, but it was empty" + return 1 + fi +} + +assert_empty() { + local value="$1" + local name="${2:-value}" + if [ -n "$value" ]; then + echo "Expected $name to be empty" + echo "Actual: '$value'" + return 1 + fi +} + +assert_directory_exists() { + local dir="$1" + if [ ! -d "$dir" ]; then + echo "Expected directory to exist: '$dir'" + return 1 + fi +} + +assert_file_exists() { + local file="$1" + if [ ! -f "$file" ]; then + echo "Expected file to exist: '$file'" + return 1 + fi +} + +assert_json_equal() { + local actual="$1" + local expected="$2" + local name="${3:-JSON}" + + local actual_sorted=$(echo "$actual" | jq -S .) + local expected_sorted=$(echo "$expected" | jq -S .) + + if [ "$actual_sorted" != "$expected_sorted" ]; then + echo "$name does not match expected structure" + echo "" + echo "Expected:" + echo "$expected_sorted" + echo "" + echo "Actual:" + echo "$actual_sorted" + echo "" + echo "Diff:" + diff <(echo "$expected_sorted") <(echo "$actual_sorted") || true + return 1 + fi +} diff --git a/frontend/deployment/workflows/initial.yaml b/frontend/deployment/workflows/initial.yaml index f48c9cc8..28c95540 100644 --- a/frontend/deployment/workflows/initial.yaml +++ b/frontend/deployment/workflows/initial.yaml @@ -7,9 +7,9 @@ steps: output: - name: TOFU_VARIABLES type: environment - - name: setup_tofu_state_layer + - name: setup_provider_layer type: script - file: "$SERVICE_PATH/deployment/tofu_state/$TOFU_STATE_PROVIDER/setup" + file: "$SERVICE_PATH/deployment/provider/$TOFU_PROVIDER/setup" - name: setup_network_layer type: script file: "$SERVICE_PATH/deployment/network/$NETWORK_LAYER/setup" From 5f50aeba6aff8e4a7c2c36ac032513504511e02c Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 9 Jan 2026 13:13:13 -0300 Subject: [PATCH 05/40] Add terra tests --- frontend/deployment/.gitignore | 16 ++ frontend/deployment/compose_modules | 4 + .../cloudfront/modules/cloudfront.tftest.hcl | 256 ++++++++++++++++++ .../cloudfront/modules/outputs.tf | 28 +- .../network/route53/modules/main.tf | 10 +- .../network/route53/modules/outputs.tf | 2 +- .../route53/modules/route53.tftest.hcl | 157 +++++++++++ .../provider/aws/modules/provider.tftest.hcl | 82 ++++++ .../tests/provider/aws/setup_test.bats | 168 ------------ frontend/deployment/tests/run_tests.sh | 65 ----- .../output/1051892149/aws_modules_provider.tf | 20 -- .../1051892149/aws_modules_variables.tf | 14 - .../1051892149/cloudfront_modules_data.tf | 3 - .../1051892149/cloudfront_modules_locals.tf | 14 - .../1051892149/cloudfront_modules_main.tf | 88 ------ .../1051892149/cloudfront_modules_outputs.tf | 44 --- .../cloudfront_modules_variables.tf | 26 -- .../1051892149/route53_modules_locals.tf | 4 - .../output/1051892149/route53_modules_main.tf | 23 -- .../1051892149/route53_modules_outputs.tf | 14 - .../1051892149/route53_modules_variables.tf | 15 - run_all_tests.sh | 72 +++++ run_tests.sh | 115 ++++++++ run_tofu_tests.sh | 129 +++++++++ 24 files changed, 851 insertions(+), 518 deletions(-) create mode 100644 frontend/deployment/.gitignore create mode 100644 frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl create mode 100644 frontend/deployment/network/route53/modules/route53.tftest.hcl create mode 100644 frontend/deployment/provider/aws/modules/provider.tftest.hcl delete mode 100644 frontend/deployment/tests/provider/aws/setup_test.bats delete mode 100755 frontend/deployment/tests/run_tests.sh delete mode 100644 frontend/output/1051892149/aws_modules_provider.tf delete mode 100644 frontend/output/1051892149/aws_modules_variables.tf delete mode 100644 frontend/output/1051892149/cloudfront_modules_data.tf delete mode 100644 frontend/output/1051892149/cloudfront_modules_locals.tf delete mode 100644 frontend/output/1051892149/cloudfront_modules_main.tf delete mode 100644 frontend/output/1051892149/cloudfront_modules_outputs.tf delete mode 100644 frontend/output/1051892149/cloudfront_modules_variables.tf delete mode 100644 frontend/output/1051892149/route53_modules_locals.tf delete mode 100644 frontend/output/1051892149/route53_modules_main.tf delete mode 100644 frontend/output/1051892149/route53_modules_outputs.tf delete mode 100644 frontend/output/1051892149/route53_modules_variables.tf create mode 100755 run_all_tests.sh create mode 100755 run_tests.sh create mode 100755 run_tofu_tests.sh diff --git a/frontend/deployment/.gitignore b/frontend/deployment/.gitignore new file mode 100644 index 00000000..45d70888 --- /dev/null +++ b/frontend/deployment/.gitignore @@ -0,0 +1,16 @@ +# Terraform/OpenTofu +.terraform/ +.terraform.lock.hcl +*.tfstate +*.tfstate.* +crash.log +crash.*.log +override.tf +override.tf.json +*_override.tf +*_override.tf.json +.terraformrc +terraform.rc + +# Test-only terraform files +**/test_*.tf diff --git a/frontend/deployment/compose_modules b/frontend/deployment/compose_modules index b2361546..510232f4 100755 --- a/frontend/deployment/compose_modules +++ b/frontend/deployment/compose_modules @@ -50,6 +50,10 @@ for module in "${modules[@]}"; do prefix="${parent}_${leaf}_" for tf_file in "$module"/*.tf; do filename=$(basename "$tf_file") + # Skip test-only files (test_*.tf) + if [[ "$filename" == test_*.tf ]]; then + continue + fi cp "$tf_file" "$TOFU_MODULE_DIR/${prefix}${filename}" done echo "✓ Copied modules from: $module (prefix: $prefix)" diff --git a/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl b/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl new file mode 100644 index 00000000..1d86902e --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl @@ -0,0 +1,256 @@ +# ============================================================================= +# Unit tests for distribution/cloudfront module +# +# Run: tofu test +# ============================================================================= + +mock_provider "aws" { + mock_data "aws_s3_bucket" { + defaults = { + id = "my-static-bucket" + arn = "arn:aws:s3:::my-static-bucket" + bucket_regional_domain_name = "my-static-bucket.s3.us-east-1.amazonaws.com" + } + } +} + +variables { + distribution_bucket_name = "my-static-bucket" + distribution_s3_prefix = "app/scope-1" + distribution_app_name = "my-app-prod" + distribution_custom_domain = null + distribution_resource_tags_json = { + Environment = "production" + Application = "my-app" + } +} + +# ============================================================================= +# Test: Origin Access Control is created +# ============================================================================= +run "creates_origin_access_control" { + command = plan + + assert { + condition = aws_cloudfront_origin_access_control.static.name == "my-app-prod-oac" + error_message = "OAC name should be 'my-app-prod-oac'" + } + + assert { + condition = aws_cloudfront_origin_access_control.static.origin_access_control_origin_type == "s3" + error_message = "OAC origin type should be 's3'" + } + + assert { + condition = aws_cloudfront_origin_access_control.static.signing_behavior == "always" + error_message = "OAC signing behavior should be 'always'" + } +} + +# ============================================================================= +# Test: CloudFront distribution basic configuration +# ============================================================================= +run "distribution_basic_configuration" { + command = plan + + assert { + condition = aws_cloudfront_distribution.static.enabled == true + error_message = "Distribution should be enabled" + } + + assert { + condition = aws_cloudfront_distribution.static.is_ipv6_enabled == true + error_message = "IPv6 should be enabled" + } + + assert { + condition = aws_cloudfront_distribution.static.default_root_object == "index.html" + error_message = "Default root object should be 'index.html'" + } + + assert { + condition = aws_cloudfront_distribution.static.price_class == "PriceClass_100" + error_message = "Price class should be 'PriceClass_100'" + } +} + +# ============================================================================= +# Test: Distribution has no aliases when custom domain is null +# ============================================================================= +run "no_aliases_without_custom_domain" { + command = plan + + assert { + condition = length(local.distribution_aliases) == 0 + error_message = "Should have no aliases when custom_domain is null" + } +} + +# ============================================================================= +# Test: Distribution has alias when custom domain is set +# ============================================================================= +run "has_alias_with_custom_domain" { + command = plan + + variables { + distribution_custom_domain = "cdn.example.com" + } + + assert { + condition = length(local.distribution_aliases) == 1 + error_message = "Should have one alias when custom_domain is set" + } + + assert { + condition = local.distribution_aliases[0] == "cdn.example.com" + error_message = "Alias should be 'cdn.example.com'" + } +} + +# ============================================================================= +# Test: Origin ID is computed correctly +# ============================================================================= +run "origin_id_format" { + command = plan + + assert { + condition = local.distribution_origin_id == "S3-my-static-bucket" + error_message = "Origin ID should be 'S3-my-static-bucket'" + } +} + +# ============================================================================= +# Test: Default tags include module tag +# ============================================================================= +run "default_tags_include_module" { + command = plan + + assert { + condition = local.distribution_default_tags["ManagedBy"] == "terraform" + error_message = "Tags should include ManagedBy=terraform" + } + + assert { + condition = local.distribution_default_tags["Module"] == "distribution/cloudfront" + error_message = "Tags should include Module=distribution/cloudfront" + } + + assert { + condition = local.distribution_default_tags["Environment"] == "production" + error_message = "Tags should preserve input Environment tag" + } +} + +# ============================================================================= +# Test: Cross-module locals for DNS integration +# ============================================================================= +run "cross_module_locals_for_dns" { + command = plan + + assert { + condition = local.distribution_record_type == "A" + error_message = "Record type should be 'A' for CloudFront alias records" + } +} + +# ============================================================================= +# Test: Cache behaviors +# ============================================================================= +run "cache_behaviors_configured" { + command = plan + + # Default cache behavior + assert { + condition = aws_cloudfront_distribution.static.default_cache_behavior[0].viewer_protocol_policy == "redirect-to-https" + error_message = "Default cache should redirect to HTTPS" + } + + assert { + condition = aws_cloudfront_distribution.static.default_cache_behavior[0].compress == true + error_message = "Default cache should enable compression" + } +} + +# ============================================================================= +# Test: Custom error responses for SPA +# ============================================================================= +run "spa_error_responses" { + command = plan + + # Check that 404 and 403 errors redirect to index.html (SPA behavior) + assert { + condition = length(aws_cloudfront_distribution.static.custom_error_response) == 2 + error_message = "Should have 2 custom error responses" + } +} + +# ============================================================================= +# Test: Outputs from data source +# ============================================================================= +run "outputs_from_data_source" { + command = plan + + assert { + condition = output.distribution_bucket_name == "my-static-bucket" + error_message = "distribution_bucket_name should be 'my-static-bucket'" + } + + assert { + condition = output.distribution_bucket_arn == "arn:aws:s3:::my-static-bucket" + error_message = "distribution_bucket_arn should be 'arn:aws:s3:::my-static-bucket'" + } +} + +# ============================================================================= +# Test: Outputs from variables +# ============================================================================= +run "outputs_from_variables" { + command = plan + + assert { + condition = output.distribution_s3_prefix == "app/scope-1" + error_message = "distribution_s3_prefix should be 'app/scope-1'" + } +} + +# ============================================================================= +# Test: DNS-related outputs +# ============================================================================= +run "dns_related_outputs" { + command = plan + + assert { + condition = output.distribution_record_type == "A" + error_message = "distribution_record_type should be 'A'" + } +} + +# ============================================================================= +# Test: Website URL without custom domain +# ============================================================================= +run "website_url_without_custom_domain" { + command = plan + + # Without custom domain, URL should use CloudFront domain (known after apply) + # We can only check it starts with https:// + assert { + condition = startswith(output.distribution_website_url, "https://") + error_message = "distribution_website_url should start with 'https://'" + } +} + +# ============================================================================= +# Test: Website URL with custom domain +# ============================================================================= +run "website_url_with_custom_domain" { + command = plan + + variables { + distribution_custom_domain = "cdn.example.com" + } + + assert { + condition = output.distribution_website_url == "https://cdn.example.com" + error_message = "distribution_website_url should be 'https://cdn.example.com'" + } +} diff --git a/frontend/deployment/distribution/cloudfront/modules/outputs.tf b/frontend/deployment/distribution/cloudfront/modules/outputs.tf index fe503cdb..3942d91f 100644 --- a/frontend/deployment/distribution/cloudfront/modules/outputs.tf +++ b/frontend/deployment/distribution/cloudfront/modules/outputs.tf @@ -1,44 +1,44 @@ -output "hosting_bucket_name" { +output "distribution_bucket_name" { description = "S3 bucket name" value = data.aws_s3_bucket.static.id } -output "hosting_bucket_arn" { +output "distribution_bucket_arn" { description = "S3 bucket ARN" value = data.aws_s3_bucket.static.arn } -output "hosting_s3_prefix" { +output "distribution_s3_prefix" { description = "S3 prefix path for this scope" - value = var.hosting_s3_prefix + value = var.distribution_s3_prefix } -output "hosting_cloudfront_distribution_id" { +output "distribution_cloudfront_distribution_id" { description = "CloudFront distribution ID" value = aws_cloudfront_distribution.static.id } -output "hosting_cloudfront_domain_name" { +output "distribution_cloudfront_domain_name" { description = "CloudFront domain name" value = aws_cloudfront_distribution.static.domain_name } -output "hosting_target_domain" { +output "distribution_target_domain" { description = "Target domain for DNS records (CloudFront domain)" - value = local.hosting_target_domain + value = local.distribution_target_domain } -output "hosting_target_zone_id" { +output "distribution_target_zone_id" { description = "Hosted zone ID for Route 53 alias records" - value = local.hosting_target_zone_id + value = local.distribution_target_zone_id } -output "hosting_record_type" { +output "distribution_record_type" { description = "DNS record type (A for CloudFront alias)" - value = local.hosting_record_type + value = local.distribution_record_type } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" + value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" } diff --git a/frontend/deployment/network/route53/modules/main.tf b/frontend/deployment/network/route53/modules/main.tf index 7cd8f0f7..70ed2180 100644 --- a/frontend/deployment/network/route53/modules/main.tf +++ b/frontend/deployment/network/route53/modules/main.tf @@ -1,23 +1,23 @@ resource "aws_route53_record" "main_alias" { - count = local.hosting_record_type == "A" ? 1 : 0 + count = local.distribution_record_type == "A" ? 1 : 0 zone_id = var.network_hosted_zone_id name = local.network_full_domain type = "A" alias { - name = local.hosting_target_domain - zone_id = local.hosting_target_zone_id + name = local.distribution_target_domain + zone_id = local.distribution_target_zone_id evaluate_target_health = false } } resource "aws_route53_record" "main_cname" { - count = local.hosting_record_type == "CNAME" ? 1 : 0 + count = local.distribution_record_type == "CNAME" ? 1 : 0 zone_id = var.network_hosted_zone_id name = local.network_full_domain type = "CNAME" ttl = 300 - records = [local.hosting_target_domain] + records = [local.distribution_target_domain] } diff --git a/frontend/deployment/network/route53/modules/outputs.tf b/frontend/deployment/network/route53/modules/outputs.tf index 6fa188e5..385a1f19 100644 --- a/frontend/deployment/network/route53/modules/outputs.tf +++ b/frontend/deployment/network/route53/modules/outputs.tf @@ -5,7 +5,7 @@ output "network_full_domain" { output "network_fqdn" { description = "Fully qualified domain name" - value = local.hosting_record_type == "A" ? aws_route53_record.main_alias[0].fqdn : aws_route53_record.main_cname[0].fqdn + value = local.distribution_record_type == "A" ? aws_route53_record.main_alias[0].fqdn : aws_route53_record.main_cname[0].fqdn } output "network_website_url" { diff --git a/frontend/deployment/network/route53/modules/route53.tftest.hcl b/frontend/deployment/network/route53/modules/route53.tftest.hcl new file mode 100644 index 00000000..b43cd7eb --- /dev/null +++ b/frontend/deployment/network/route53/modules/route53.tftest.hcl @@ -0,0 +1,157 @@ +# ============================================================================= +# Unit tests for network/route53 module +# +# Run: tofu test +# ============================================================================= + +mock_provider "aws" {} + +variables { + network_hosted_zone_id = "Z1234567890ABC" + network_domain = "example.com" + network_subdomain = "app" + + # These come from the distribution module (e.g., cloudfront) + distribution_target_domain = "d1234567890.cloudfront.net" + distribution_target_zone_id = "Z2FDTNDATAQYW2" + distribution_record_type = "A" +} + +# ============================================================================= +# Test: Full domain is computed correctly with subdomain +# ============================================================================= +run "full_domain_with_subdomain" { + command = plan + + assert { + condition = local.network_full_domain == "app.example.com" + error_message = "Full domain should be 'app.example.com', got '${local.network_full_domain}'" + } +} + +# ============================================================================= +# Test: Full domain is computed correctly without subdomain (apex) +# ============================================================================= +run "full_domain_apex" { + command = plan + + variables { + network_subdomain = "" + } + + assert { + condition = local.network_full_domain == "example.com" + error_message = "Full domain should be 'example.com' for apex, got '${local.network_full_domain}'" + } +} + +# ============================================================================= +# Test: A record is created for alias type +# ============================================================================= +run "creates_alias_record_for_type_a" { + command = plan + + variables { + distribution_record_type = "A" + } + + assert { + condition = length(aws_route53_record.main_alias) == 1 + error_message = "Should create one A alias record" + } + + assert { + condition = length(aws_route53_record.main_cname) == 0 + error_message = "Should not create CNAME record when type is A" + } +} + +# ============================================================================= +# Test: CNAME record is created for CNAME type +# ============================================================================= +run "creates_cname_record_for_type_cname" { + command = plan + + variables { + distribution_record_type = "CNAME" + } + + assert { + condition = length(aws_route53_record.main_cname) == 1 + error_message = "Should create one CNAME record" + } + + assert { + condition = length(aws_route53_record.main_alias) == 0 + error_message = "Should not create A alias record when type is CNAME" + } +} + +# ============================================================================= +# Test: A record configuration +# ============================================================================= +run "alias_record_configuration" { + command = plan + + variables { + distribution_record_type = "A" + } + + assert { + condition = aws_route53_record.main_alias[0].zone_id == "Z1234567890ABC" + error_message = "Record should use the correct hosted zone ID" + } + + assert { + condition = aws_route53_record.main_alias[0].type == "A" + error_message = "Record type should be A" + } + + assert { + condition = aws_route53_record.main_alias[0].name == "app.example.com" + error_message = "Record name should be the full domain" + } +} + +# ============================================================================= +# Test: CNAME record configuration +# ============================================================================= +run "cname_record_configuration" { + command = plan + + variables { + distribution_record_type = "CNAME" + } + + assert { + condition = aws_route53_record.main_cname[0].zone_id == "Z1234567890ABC" + error_message = "Record should use the correct hosted zone ID" + } + + assert { + condition = aws_route53_record.main_cname[0].type == "CNAME" + error_message = "Record type should be CNAME" + } + + assert { + condition = aws_route53_record.main_cname[0].ttl == 300 + error_message = "CNAME TTL should be 300" + } +} + +# ============================================================================= +# Test: Outputs +# ============================================================================= +run "outputs_are_correct" { + command = plan + + assert { + condition = output.network_full_domain == "app.example.com" + error_message = "network_full_domain output should be 'app.example.com'" + } + + assert { + condition = output.network_website_url == "https://app.example.com" + error_message = "network_website_url output should be 'https://app.example.com'" + } +} diff --git a/frontend/deployment/provider/aws/modules/provider.tftest.hcl b/frontend/deployment/provider/aws/modules/provider.tftest.hcl new file mode 100644 index 00000000..ff6d8b68 --- /dev/null +++ b/frontend/deployment/provider/aws/modules/provider.tftest.hcl @@ -0,0 +1,82 @@ +# ============================================================================= +# Unit tests for provider/aws module +# +# Run: tofu test +# ============================================================================= + +mock_provider "aws" {} + +variables { + aws_provider = { + region = "us-east-1" + state_bucket = "my-terraform-state" + lock_table = "terraform-locks" + } + + provider_resource_tags_json = { + Environment = "test" + Project = "frontend" + ManagedBy = "terraform" + } +} + +# ============================================================================= +# Test: Provider configuration is valid +# ============================================================================= +run "provider_configuration_is_valid" { + command = plan + + assert { + condition = var.aws_provider.region == "us-east-1" + error_message = "AWS region should be us-east-1" + } + + assert { + condition = var.aws_provider.state_bucket == "my-terraform-state" + error_message = "State bucket should be my-terraform-state" + } + + assert { + condition = var.aws_provider.lock_table == "terraform-locks" + error_message = "Lock table should be terraform-locks" + } +} + +# ============================================================================= +# Test: Default tags are configured +# ============================================================================= +run "default_tags_are_configured" { + command = plan + + assert { + condition = var.provider_resource_tags_json["Environment"] == "test" + error_message = "Environment tag should be 'test'" + } + + assert { + condition = var.provider_resource_tags_json["ManagedBy"] == "terraform" + error_message = "ManagedBy tag should be 'terraform'" + } +} + +# ============================================================================= +# Test: Required variables validation +# ============================================================================= +run "aws_provider_requires_region" { + command = plan + + variables { + aws_provider = { + region = "" + state_bucket = "bucket" + lock_table = "table" + } + } + + # Empty region should still be syntactically valid but semantically wrong + # This tests that the variable structure is enforced + assert { + condition = var.aws_provider.region == "" + error_message = "Empty region should be accepted by variable type" + } +} diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats deleted file mode 100644 index fbc85714..00000000 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ /dev/null @@ -1,168 +0,0 @@ -#!/usr/bin/env bats -# ============================================================================= -# Unit tests for provider/aws/setup script -# -# Requirements: -# - bats-core: brew install bats-core -# - jq: brew install jq -# -# Run tests: -# bats tests/aws/setup_test.bats -# ============================================================================= - -# Setup - runs before each test -setup() { - TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" - PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" - SCRIPT_PATH="$PROJECT_DIR/provider/aws/setup" - - # Load shared test utilities - source "$PROJECT_DIR/tests/test_utils.bash" - - # Initialize variables that the script expects to exist - export TOFU_VARIABLES='{}' - export TOFU_INIT_VARIABLES="" - export MODULES_TO_USE="" - - # Set required AWS variables - export AWS_REGION="us-east-1" - export TOFU_PROVIDER_BUCKET="my-state-bucket" - export TOFU_LOCK_TABLE="my-lock-table" -} - -# ============================================================================= -# Helper functions -# ============================================================================= -run_aws_setup() { - source "$SCRIPT_PATH" -} - -# ============================================================================= -# Test: Required environment variables -# ============================================================================= -@test "fails when AWS_REGION is not set" { - unset AWS_REGION - - run source "$SCRIPT_PATH" - - assert_equal "$status" "1" - assert_contains "$output" "AWS_REGION is not set" -} - -@test "fails when TOFU_PROVIDER_BUCKET is not set" { - unset TOFU_PROVIDER_BUCKET - - run source "$SCRIPT_PATH" - - assert_equal "$status" "1" - assert_contains "$output" "TOFU_PROVIDER_BUCKET is not set" -} - -@test "fails when TOFU_LOCK_TABLE is not set" { - unset TOFU_LOCK_TABLE - - run source "$SCRIPT_PATH" - - assert_equal "$status" "1" - assert_contains "$output" "TOFU_LOCK_TABLE is not set" -} - -# ============================================================================= -# Test: TOFU_VARIABLES - aws_provider object -# ============================================================================= -@test "adds aws_provider to TOFU_VARIABLES" { - run_aws_setup - - local aws_provider=$(echo "$TOFU_VARIABLES" | jq '.aws_provider') - assert_not_empty "$aws_provider" "aws_provider" -} - -@test "aws_provider contains correct region" { - run_aws_setup - - local region=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.region') - assert_equal "$region" "us-east-1" -} - -@test "aws_provider contains correct state_bucket" { - run_aws_setup - - local bucket=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.state_bucket') - assert_equal "$bucket" "my-state-bucket" -} - -@test "aws_provider contains correct lock_table" { - run_aws_setup - - local table=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.lock_table') - assert_equal "$table" "my-lock-table" -} - -@test "TOFU_VARIABLES aws_provider matches expected structure" { - run_aws_setup - - local expected='{ - "region": "us-east-1", - "state_bucket": "my-state-bucket", - "lock_table": "my-lock-table" -}' - - local actual=$(echo "$TOFU_VARIABLES" | jq -S '.aws_provider') - local expected_sorted=$(echo "$expected" | jq -S .) - - if [ "$actual" != "$expected_sorted" ]; then - echo "aws_provider does not match expected structure" - echo "" - echo "Expected:" - echo "$expected_sorted" - echo "" - echo "Actual:" - echo "$actual" - return 1 - fi -} - -# ============================================================================= -# Test: TOFU_INIT_VARIABLES - backend config -# ============================================================================= -@test "TOFU_INIT_VARIABLES contains bucket backend-config" { - run_aws_setup - - assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="bucket=my-state-bucket"' -} - -@test "TOFU_INIT_VARIABLES contains region backend-config" { - run_aws_setup - - assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="region=us-east-1"' -} - -@test "TOFU_INIT_VARIABLES contains dynamodb_table backend-config" { - run_aws_setup - - assert_contains "$TOFU_INIT_VARIABLES" '-backend-config="dynamodb_table=my-lock-table"' -} - -# ============================================================================= -# Test: MODULES_TO_USE -# ============================================================================= -@test "MODULES_TO_USE contains aws modules path" { - run_aws_setup - - assert_contains "$MODULES_TO_USE" "provider/aws/modules" -} - -@test "MODULES_TO_USE appends to existing modules" { - export MODULES_TO_USE="existing/module" - run_aws_setup - - assert_contains "$MODULES_TO_USE" "existing/module" - assert_contains "$MODULES_TO_USE" "provider/aws/modules" -} - -@test "MODULES_TO_USE uses comma separator when appending" { - export MODULES_TO_USE="existing/module" - run_aws_setup - - assert_contains "$MODULES_TO_USE" "existing/module," -} diff --git a/frontend/deployment/tests/run_tests.sh b/frontend/deployment/tests/run_tests.sh deleted file mode 100755 index 2ffa4062..00000000 --- a/frontend/deployment/tests/run_tests.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Test runner for all BATS tests -# -# Usage: -# ./tests/run_tests.sh # Run all tests -# ./tests/run_tests.sh aws # Run tests in aws/ directory -# ./tests/run_tests.sh build_context # Run specific test file -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -NC='\033[0m' - -# Check if bats is installed -if ! command -v bats &> /dev/null; then - echo -e "${RED}bats-core is not installed${NC}" - echo "" - echo "Install with:" - echo " brew install bats-core # macOS" - echo " apt install bats # Ubuntu/Debian" - exit 1 -fi - -# Check if jq is installed -if ! command -v jq &> /dev/null; then - echo -e "${RED}jq is not installed${NC}" - echo "" - echo "Install with:" - echo " brew install jq # macOS" - echo " apt install jq # Ubuntu/Debian" - exit 1 -fi - -# Run specific test or all tests -if [ -n "$1" ]; then - # Check if it's a directory - if [ -d "$1" ]; then - echo "Running tests in $1/" - bats "$1"/*.bats - # Check if it's a file (with or without .bats extension) - elif [ -f "${1}_test.bats" ]; then - echo "Running ${1}_test.bats" - bats "${1}_test.bats" - elif [ -f "$1" ]; then - echo "Running $1" - bats "$1" - else - echo -e "${RED}Test not found: $1${NC}" - exit 1 - fi -else - echo "Running all tests..." - echo "" - bats ./*.bats ./**/*.bats -fi - -echo "" -echo -e "${GREEN}All tests passed!${NC}" diff --git a/frontend/output/1051892149/aws_modules_provider.tf b/frontend/output/1051892149/aws_modules_provider.tf deleted file mode 100644 index c6ef3b81..00000000 --- a/frontend/output/1051892149/aws_modules_provider.tf +++ /dev/null @@ -1,20 +0,0 @@ -terraform { - required_version = ">= 1.4.0" - - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 5.0" - } - } - - backend "s3" {} -} - -provider "aws" { - region = var.aws_provider.region - - default_tags { - tags = var.provider_resource_tags_json - } -} \ No newline at end of file diff --git a/frontend/output/1051892149/aws_modules_variables.tf b/frontend/output/1051892149/aws_modules_variables.tf deleted file mode 100644 index 27d2535b..00000000 --- a/frontend/output/1051892149/aws_modules_variables.tf +++ /dev/null @@ -1,14 +0,0 @@ -variable "aws_provider" { - description = "AWS provider configuration" - type = object({ - region = string - state_bucket = string - lock_table = string - }) -} - -variable "provider_resource_tags_json" { - description = "Resource tags as JSON object - applied as default tags to all AWS resources" - type = map(string) - default = {} -} \ No newline at end of file diff --git a/frontend/output/1051892149/cloudfront_modules_data.tf b/frontend/output/1051892149/cloudfront_modules_data.tf deleted file mode 100644 index 9d4b0475..00000000 --- a/frontend/output/1051892149/cloudfront_modules_data.tf +++ /dev/null @@ -1,3 +0,0 @@ -data "aws_s3_bucket" "static" { - bucket = var.distribution_bucket_name -} diff --git a/frontend/output/1051892149/cloudfront_modules_locals.tf b/frontend/output/1051892149/cloudfront_modules_locals.tf deleted file mode 100644 index abac0de8..00000000 --- a/frontend/output/1051892149/cloudfront_modules_locals.tf +++ /dev/null @@ -1,14 +0,0 @@ -locals { - distribution_origin_id = "S3-${var.distribution_bucket_name}" - distribution_aliases = var.distribution_custom_domain != null ? [var.distribution_custom_domain] : [] - - distribution_default_tags = merge(var.distribution_resource_tags_json, { - ManagedBy = "terraform" - Module = "distribution/cloudfront" - }) - - # Cross-module references (consumed by network/route53) - distribution_target_domain = aws_cloudfront_distribution.static.domain_name - distribution_target_zone_id = aws_cloudfront_distribution.static.hosted_zone_id - distribution_record_type = "A" -} diff --git a/frontend/output/1051892149/cloudfront_modules_main.tf b/frontend/output/1051892149/cloudfront_modules_main.tf deleted file mode 100644 index 6bc652e9..00000000 --- a/frontend/output/1051892149/cloudfront_modules_main.tf +++ /dev/null @@ -1,88 +0,0 @@ -resource "aws_cloudfront_origin_access_control" "static" { - name = "${var.distribution_app_name}-oac" - description = "OAC for ${var.distribution_app_name}" - origin_access_control_origin_type = "s3" - signing_behavior = "always" - signing_protocol = "sigv4" -} - -resource "aws_cloudfront_distribution" "static" { - enabled = true - is_ipv6_enabled = true - default_root_object = "index.html" - aliases = local.distribution_aliases - price_class = "PriceClass_100" - comment = "Distribution for ${var.distribution_app_name}" - - origin { - domain_name = data.aws_s3_bucket.static.bucket_regional_domain_name - origin_id = local.distribution_origin_id - origin_access_control_id = aws_cloudfront_origin_access_control.static.id - - origin_path = var.distribution_s3_prefix != "" ? "/${var.distribution_s3_prefix}" : "" - } - - default_cache_behavior { - allowed_methods = ["GET", "HEAD", "OPTIONS"] - cached_methods = ["GET", "HEAD"] - target_origin_id = local.distribution_origin_id - - forwarded_values { - query_string = false - cookies { - forward = "none" - } - } - - viewer_protocol_policy = "redirect-to-https" - min_ttl = 0 - default_ttl = 3600 - max_ttl = 86400 - compress = true - } - - ordered_cache_behavior { - path_pattern = "/static/*" - allowed_methods = ["GET", "HEAD"] - cached_methods = ["GET", "HEAD"] - target_origin_id = local.distribution_origin_id - - forwarded_values { - query_string = false - cookies { - forward = "none" - } - } - - viewer_protocol_policy = "redirect-to-https" - min_ttl = 86400 - default_ttl = 604800 - max_ttl = 31536000 - compress = true - } - - custom_error_response { - error_code = 404 - response_code = 200 - response_page_path = "/index.html" - } - - custom_error_response { - error_code = 403 - response_code = 200 - response_page_path = "/index.html" - } - - restrictions { - geo_restriction { - restriction_type = "none" - } - } - - viewer_certificate { - cloudfront_default_certificate = true - minimum_protocol_version = "TLSv1.2_2021" - } - - tags = local.distribution_default_tags -} diff --git a/frontend/output/1051892149/cloudfront_modules_outputs.tf b/frontend/output/1051892149/cloudfront_modules_outputs.tf deleted file mode 100644 index fe503cdb..00000000 --- a/frontend/output/1051892149/cloudfront_modules_outputs.tf +++ /dev/null @@ -1,44 +0,0 @@ -output "hosting_bucket_name" { - description = "S3 bucket name" - value = data.aws_s3_bucket.static.id -} - -output "hosting_bucket_arn" { - description = "S3 bucket ARN" - value = data.aws_s3_bucket.static.arn -} - -output "hosting_s3_prefix" { - description = "S3 prefix path for this scope" - value = var.hosting_s3_prefix -} - -output "hosting_cloudfront_distribution_id" { - description = "CloudFront distribution ID" - value = aws_cloudfront_distribution.static.id -} - -output "hosting_cloudfront_domain_name" { - description = "CloudFront domain name" - value = aws_cloudfront_distribution.static.domain_name -} - -output "hosting_target_domain" { - description = "Target domain for DNS records (CloudFront domain)" - value = local.hosting_target_domain -} - -output "hosting_target_zone_id" { - description = "Hosted zone ID for Route 53 alias records" - value = local.hosting_target_zone_id -} - -output "hosting_record_type" { - description = "DNS record type (A for CloudFront alias)" - value = local.hosting_record_type -} - -output "hosting_website_url" { - description = "Website URL" - value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" -} diff --git a/frontend/output/1051892149/cloudfront_modules_variables.tf b/frontend/output/1051892149/cloudfront_modules_variables.tf deleted file mode 100644 index a9ef4cbc..00000000 --- a/frontend/output/1051892149/cloudfront_modules_variables.tf +++ /dev/null @@ -1,26 +0,0 @@ -variable "distribution_bucket_name" { - description = "Existing S3 bucket name for static website distribution" - type = string -} - -variable "distribution_s3_prefix" { - description = "S3 prefix/path for this scope's files (e.g., 'app-name/scope-id')" - type = string -} - -variable "distribution_app_name" { - description = "Application name (used for resource naming)" - type = string -} - -variable "distribution_custom_domain" { - description = "Custom domain for CloudFront (optional)" - type = string - default = null -} - -variable "distribution_resource_tags_json" { - description = "Resource tags as JSON object" - type = map(string) - default = {} -} diff --git a/frontend/output/1051892149/route53_modules_locals.tf b/frontend/output/1051892149/route53_modules_locals.tf deleted file mode 100644 index d9357d81..00000000 --- a/frontend/output/1051892149/route53_modules_locals.tf +++ /dev/null @@ -1,4 +0,0 @@ -locals { - # Compute full domain from domain + subdomain - network_full_domain = var.network_subdomain != "" ? "${var.network_subdomain}.${var.network_domain}" : var.network_domain -} \ No newline at end of file diff --git a/frontend/output/1051892149/route53_modules_main.tf b/frontend/output/1051892149/route53_modules_main.tf deleted file mode 100644 index 7cd8f0f7..00000000 --- a/frontend/output/1051892149/route53_modules_main.tf +++ /dev/null @@ -1,23 +0,0 @@ -resource "aws_route53_record" "main_alias" { - count = local.hosting_record_type == "A" ? 1 : 0 - - zone_id = var.network_hosted_zone_id - name = local.network_full_domain - type = "A" - - alias { - name = local.hosting_target_domain - zone_id = local.hosting_target_zone_id - evaluate_target_health = false - } -} - -resource "aws_route53_record" "main_cname" { - count = local.hosting_record_type == "CNAME" ? 1 : 0 - - zone_id = var.network_hosted_zone_id - name = local.network_full_domain - type = "CNAME" - ttl = 300 - records = [local.hosting_target_domain] -} diff --git a/frontend/output/1051892149/route53_modules_outputs.tf b/frontend/output/1051892149/route53_modules_outputs.tf deleted file mode 100644 index 6fa188e5..00000000 --- a/frontend/output/1051892149/route53_modules_outputs.tf +++ /dev/null @@ -1,14 +0,0 @@ -output "network_full_domain" { - description = "Full domain name (subdomain.domain or just domain)" - value = local.network_full_domain -} - -output "network_fqdn" { - description = "Fully qualified domain name" - value = local.hosting_record_type == "A" ? aws_route53_record.main_alias[0].fqdn : aws_route53_record.main_cname[0].fqdn -} - -output "network_website_url" { - description = "Website URL" - value = "https://${local.network_full_domain}" -} \ No newline at end of file diff --git a/frontend/output/1051892149/route53_modules_variables.tf b/frontend/output/1051892149/route53_modules_variables.tf deleted file mode 100644 index 9f67cf99..00000000 --- a/frontend/output/1051892149/route53_modules_variables.tf +++ /dev/null @@ -1,15 +0,0 @@ -variable "network_hosted_zone_id" { - description = "Route53 hosted zone ID" - type = string -} - -variable "network_domain" { - description = "Root domain name (e.g., example.com)" - type = string -} - -variable "network_subdomain" { - description = "Subdomain prefix (e.g., 'app' for app.example.com, empty string for apex)" - type = string - default = "" -} \ No newline at end of file diff --git a/run_all_tests.sh b/run_all_tests.sh new file mode 100755 index 00000000..9ad5686e --- /dev/null +++ b/run_all_tests.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# ============================================================================= +# Test runner for all tests (BATS + OpenTofu) +# +# Usage: +# ./run_all_tests.sh # Run all tests +# ./run_all_tests.sh frontend # Run tests for frontend module only +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +CYAN='\033[0;36m' +NC='\033[0m' + +MODULE="${1:-}" + +echo "" +echo "========================================" +echo " Running All Tests" +echo "========================================" +echo "" + +# Track failures +BATS_FAILED=0 +TOFU_FAILED=0 + +# Run BATS tests +echo -e "${CYAN}[BATS]${NC} Running bash tests..." +echo "" +if ./run_tests.sh $MODULE; then + echo -e "${GREEN}[BATS] All bash tests passed${NC}" +else + BATS_FAILED=1 + echo -e "${RED}[BATS] Some bash tests failed${NC}" +fi + +echo "" +echo "----------------------------------------" +echo "" + +# Run OpenTofu tests +echo -e "${CYAN}[TOFU]${NC} Running OpenTofu tests..." +echo "" +if ./run_tofu_tests.sh $MODULE; then + echo -e "${GREEN}[TOFU] All OpenTofu tests passed${NC}" +else + TOFU_FAILED=1 + echo -e "${RED}[TOFU] Some OpenTofu tests failed${NC}" +fi + +echo "" +echo "========================================" +echo " Summary" +echo "========================================" +echo "" + +if [ $BATS_FAILED -eq 0 ] && [ $TOFU_FAILED -eq 0 ]; then + echo -e "${GREEN}All tests passed!${NC}" + exit 0 +else + [ $BATS_FAILED -eq 1 ] && echo -e "${RED}BATS tests: FAILED${NC}" + [ $BATS_FAILED -eq 0 ] && echo -e "${GREEN}BATS tests: PASSED${NC}" + [ $TOFU_FAILED -eq 1 ] && echo -e "${RED}OpenTofu tests: FAILED${NC}" + [ $TOFU_FAILED -eq 0 ] && echo -e "${GREEN}OpenTofu tests: PASSED${NC}" + exit 1 +fi diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 00000000..285f7b37 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,115 @@ +#!/bin/bash +# ============================================================================= +# Test runner for all BATS tests across all modules +# +# Usage: +# ./run_tests.sh # Run all tests +# ./run_tests.sh frontend # Run tests for frontend module only +# ./run_tests.sh frontend/deployment/tests/aws # Run specific test directory +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +NC='\033[0m' + +# Check if bats is installed +if ! command -v bats &> /dev/null; then + echo -e "${RED}bats-core is not installed${NC}" + echo "" + echo "Install with:" + echo " brew install bats-core # macOS" + echo " apt install bats # Ubuntu/Debian" + exit 1 +fi + +# Check if jq is installed +if ! command -v jq &> /dev/null; then + echo -e "${RED}jq is not installed${NC}" + echo "" + echo "Install with:" + echo " brew install jq # macOS" + echo " apt install jq # Ubuntu/Debian" + exit 1 +fi + +# Find all test directories +find_test_dirs() { + find . -type d -name "tests" -path "*/deployment/*" 2>/dev/null | sort +} + +# Get module name from test path +get_module_name() { + local path="$1" + echo "$path" | sed 's|^\./||' | cut -d'/' -f1 +} + +# Run tests for a specific directory +run_tests_in_dir() { + local test_dir="$1" + local module_name=$(get_module_name "$test_dir") + + # Find all .bats files recursively + local bats_files=$(find "$test_dir" -name "*.bats" 2>/dev/null) + + if [ -z "$bats_files" ]; then + return 0 + fi + + echo -e "${CYAN}[$module_name]${NC} Running BATS tests in $test_dir" + echo "" + + ( + cd "$test_dir" + # Use script to force TTY for colored output + script -q /dev/null bats --formatter pretty $(find . -name "*.bats" | sort) + ) + + echo "" +} + +echo "" +echo "========================================" +echo " BATS Tests" +echo "========================================" +echo "" + +if [ -n "$1" ]; then + # Run tests for specific module or directory + if [ -d "$1" ]; then + # Direct directory path + run_tests_in_dir "$1" + elif [ -d "$1/deployment/tests" ]; then + # Module name (e.g., "frontend") + run_tests_in_dir "$1/deployment/tests" + else + echo -e "${RED}Test directory not found: $1${NC}" + echo "" + echo "Available modules with tests:" + for dir in $(find_test_dirs); do + echo " - $(get_module_name "$dir")" + done + exit 1 + fi +else + # Run all tests + test_dirs=$(find_test_dirs) + + if [ -z "$test_dirs" ]; then + echo -e "${YELLOW}No test directories found${NC}" + exit 0 + fi + + for test_dir in $test_dirs; do + run_tests_in_dir "$test_dir" + done +fi + +echo -e "${GREEN}All BATS tests passed!${NC}" diff --git a/run_tofu_tests.sh b/run_tofu_tests.sh new file mode 100755 index 00000000..7ac606b8 --- /dev/null +++ b/run_tofu_tests.sh @@ -0,0 +1,129 @@ +#!/bin/bash +# ============================================================================= +# Test runner for all OpenTofu/Terraform tests across all modules +# +# Usage: +# ./run_tofu_tests.sh # Run all tofu tests +# ./run_tofu_tests.sh frontend # Run tests for frontend module +# ./run_tofu_tests.sh frontend/provider/aws # Run specific module tests +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +NC='\033[0m' + +# Check if tofu is installed +if ! command -v tofu &> /dev/null; then + echo -e "${RED}OpenTofu is not installed${NC}" + echo "" + echo "Install with:" + echo " brew install opentofu # macOS" + echo " See https://opentofu.org/docs/intro/install/" + exit 1 +fi + +# Find all directories with .tftest.hcl files +find_tofu_test_dirs() { + find . -name "*.tftest.hcl" -path "*/deployment/*" 2>/dev/null | xargs -I{} dirname {} | sort -u +} + +# Get module name from path +get_module_name() { + local path="$1" + echo "$path" | sed 's|^\./||' | cut -d'/' -f1 +} + +# Get relative module path (e.g., provider/aws/modules) +get_relative_path() { + local path="$1" + echo "$path" | sed 's|^\./[^/]*/deployment/||' +} + +# Run tests for a specific directory +run_tofu_tests_in_dir() { + local test_dir="$1" + local module_name=$(get_module_name "$test_dir") + local relative_path=$(get_relative_path "$test_dir") + + echo -e "${CYAN}[$module_name]${NC} ${relative_path}" + + ( + cd "$test_dir" + + # Initialize if needed (without backend) + if [ ! -d ".terraform" ]; then + tofu init -backend=false -input=false >/dev/null 2>&1 || true + fi + + # Run tests + tofu test + ) + + echo "" +} + +echo "" +echo "========================================" +echo " OpenTofu Tests" +echo "========================================" +echo "" + +if [ -n "$1" ]; then + # Run tests for specific module or directory + if [ -d "$1" ] && ls "$1"/*.tftest.hcl &>/dev/null; then + # Direct directory with test files + run_tofu_tests_in_dir "$1" + elif [ -d "$1/deployment" ]; then + # Module name (e.g., "frontend") + module_dirs=$(find "$1/deployment" -name "*.tftest.hcl" 2>/dev/null | xargs -I{} dirname {} | sort -u) + if [ -z "$module_dirs" ]; then + echo -e "${YELLOW}No tofu test files found in $1${NC}" + exit 0 + fi + for dir in $module_dirs; do + run_tofu_tests_in_dir "$dir" + done + elif [ -d "$1/modules" ] && ls "$1/modules"/*.tftest.hcl &>/dev/null; then + # Path like "frontend/provider/aws" -> check frontend/deployment/provider/aws/modules + run_tofu_tests_in_dir "$1/modules" + else + # Try to find it under deployment + for base in */deployment; do + if [ -d "$base/$1/modules" ] && ls "$base/$1/modules"/*.tftest.hcl &>/dev/null 2>&1; then + run_tofu_tests_in_dir "$base/$1/modules" + exit 0 + fi + done + echo -e "${RED}No tofu test files found for: $1${NC}" + echo "" + echo "Available modules with tofu tests:" + for dir in $(find_tofu_test_dirs); do + local module=$(get_module_name "$dir") + local rel=$(get_relative_path "$dir") + echo " - $module: $rel" + done + exit 1 + fi +else + # Run all tests + test_dirs=$(find_tofu_test_dirs) + + if [ -z "$test_dirs" ]; then + echo -e "${YELLOW}No tofu test files found${NC}" + exit 0 + fi + + for test_dir in $test_dirs; do + run_tofu_tests_in_dir "$test_dir" + done +fi + +echo -e "${GREEN}All OpenTofu tests passed!${NC}" From aca1aae387c6a001cd5cd91d2b999590f7a55db0 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Mon, 12 Jan 2026 16:35:19 -0300 Subject: [PATCH 06/40] First integration tests --- frontend/deployment/build_context | 35 +- .../distribution/amplify/modules/main.tf | 92 ++-- .../deployment/distribution/amplify/setup | 18 +- .../distribution/blob-cdn/modules/main.tf | 58 +-- .../deployment/distribution/blob-cdn/setup | 13 +- .../deployment/distribution/cloudfront/setup | 38 +- .../distribution/firebase/modules/main.tf | 48 +- .../deployment/distribution/firebase/setup | 20 +- .../distribution/gcs-cdn/modules/main.tf | 84 +-- .../deployment/distribution/gcs-cdn/setup | 26 +- .../static-web-apps/modules/main.tf | 52 +- .../distribution/static-web-apps/setup | 12 +- frontend/deployment/do_tofu | 42 +- frontend/deployment/network/azure_dns/setup | 2 +- frontend/deployment/network/cloud_dns/setup | 4 +- frontend/deployment/provider/aws/setup | 20 +- .../deployment/tests/build_context_test.bats | 47 +- .../distribution/cloudfront/setup_test.bats | 29 +- .../integration/cloudfront_lifecycle_test.sh | 78 +++ .../configs/example_create_and_destroy.json | 97 ++++ .../tests/integration/docker-compose.yml | 37 ++ .../deployment/tests/integration/mocks/np | 123 +++++ .../responses/asset_repository_success.json | 26 + .../mocks/responses/scope_success.json | 6 + .../integration/run_integration_tests.sh | 187 +++++++ .../integration/scripts/disable_cloudfront.sh | 59 +++ .../integration/volume/cache/machine.json | 1 + .../integration/volume/cache/server.test.pem | 168 ++++++ .../volume/cache/server.test.pem.crt | 140 +++++ .../volume/cache/server.test.pem.key | 28 + .../tests/integration_test_utils.sh | 492 ++++++++++++++++++ .../tests/provider/aws/setup_test.bats | 226 ++++++++ .../np_mocks/asset_repository/success.json | 4 +- frontend/deployment/workflows/delete.yaml | 22 +- frontend/deployment/workflows/initial.yaml | 3 - run_all_tests.sh | 122 ++++- run_integration_tests.sh | 102 ++++ 37 files changed, 2212 insertions(+), 349 deletions(-) create mode 100755 frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh create mode 100644 frontend/deployment/tests/integration/configs/example_create_and_destroy.json create mode 100644 frontend/deployment/tests/integration/docker-compose.yml create mode 100755 frontend/deployment/tests/integration/mocks/np create mode 100644 frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json create mode 100644 frontend/deployment/tests/integration/mocks/responses/scope_success.json create mode 100755 frontend/deployment/tests/integration/run_integration_tests.sh create mode 100755 frontend/deployment/tests/integration/scripts/disable_cloudfront.sh create mode 100644 frontend/deployment/tests/integration/volume/cache/machine.json create mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem create mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem.crt create mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem.key create mode 100755 frontend/deployment/tests/integration_test_utils.sh create mode 100644 frontend/deployment/tests/provider/aws/setup_test.bats create mode 100755 run_integration_tests.sh diff --git a/frontend/deployment/build_context b/frontend/deployment/build_context index 6144dd5b..0a9aec3f 100644 --- a/frontend/deployment/build_context +++ b/frontend/deployment/build_context @@ -4,10 +4,10 @@ application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) namespace_slug=$(echo "$CONTEXT" | jq -r .namespace.slug) scope_id=$(echo "$CONTEXT" | jq -r .scope.id) -repository_url=$(echo "$CONTEXT" | jq -r .application.repository_url) application_version="$(echo "$CONTEXT" | jq -r .release.semver)" -env_vars_json=$(echo "$CONTEXT" | jq '.parameters.results | map({(.variable): .values[0].value}) | add') -resource_tags_json=$(echo "$CONTEXT" | jq \ +env_vars_json=$(echo "$CONTEXT" | jq '(.parameters.results // []) | map({(.variable): .values[0].value}) | add // {}') + +RESOURCE_TAGS_JSON=$(echo "$CONTEXT" | jq \ '{ nullplatform: "true", account: .account.slug, @@ -21,32 +21,16 @@ resource_tags_json=$(echo "$CONTEXT" | jq \ deployment_id: .deployment.id }') -TOFU_VARIABLES=$(jq -n \ - --arg application_slug "$application_slug" \ - --arg scope_slug "$scope_slug" \ - --arg scope_id "$scope_id" \ - --arg repository_url "$repository_url" \ - --arg application_version "$application_version" \ - --argjson env_vars_json "$env_vars_json" \ - --argjson resource_tags_json "$resource_tags_json" \ - '{ - application_slug: $application_slug, - scope_slug: $scope_slug, - scope_id: $scope_id, - repository_url: $repository_url, - application_version: $application_version, - env_vars_json: $env_vars_json, - resource_tags_json: $resource_tags_json - }') +TOFU_VARIABLES={} tf_state_key="frontend/$namespace_slug/$application_slug/$scope_slug-$scope_id" -TOFU_INIT_VARIABLES="-backend-config=\"key=$tf_state_key\"" +TOFU_INIT_VARIABLES="-backend-config=key=$tf_state_key" TOFU_MODULE_DIR="$SERVICE_PATH/output/$scope_id" -#if [ -n "${NP_OUTPUT_DIR:-}" ]; then -# TOFU_MODULE_DIR="$NP_OUTPUT_DIR/output/$SCOPE_ID" -#fi +if [ -n "${NP_OUTPUT_DIR:-}" ]; then + TOFU_MODULE_DIR="$NP_OUTPUT_DIR/output/$scope_id" +fi mkdir -p "$TOFU_MODULE_DIR" @@ -63,4 +47,5 @@ MODULES_TO_USE="${CUSTOM_TOFU_MODULES:-}" export TOFU_VARIABLES export TOFU_INIT_VARIABLES export TOFU_MODULE_DIR -export MODULES_TO_USE \ No newline at end of file +export MODULES_TO_USE +export RESOURCE_TAGS_JSON \ No newline at end of file diff --git a/frontend/deployment/distribution/amplify/modules/main.tf b/frontend/deployment/distribution/amplify/modules/main.tf index 0157e7f2..6a45c61f 100644 --- a/frontend/deployment/distribution/amplify/modules/main.tf +++ b/frontend/deployment/distribution/amplify/modules/main.tf @@ -1,48 +1,48 @@ # AWS Amplify Hosting # Resources for AWS Amplify static frontend hosting -variable "hosting_app_name" { +variable "distribution_app_name" { description = "Application name" type = string } -variable "hosting_environment" { +variable "distribution_environment" { description = "Environment (dev, staging, prod)" type = string default = "prod" } -variable "hosting_repository_url" { +variable "distribution_repository_url" { description = "Git repository URL" type = string } -variable "hosting_branch_name" { +variable "distribution_branch_name" { description = "Branch to deploy" type = string default = "main" } -variable "hosting_github_access_token" { +variable "distribution_github_access_token" { description = "GitHub access token" type = string sensitive = true default = null } -variable "hosting_custom_domain" { +variable "distribution_custom_domain" { description = "Custom domain (e.g., app.example.com)" type = string default = null } -variable "hosting_environment_variables" { +variable "distribution_environment_variables" { description = "Environment variables for the application" type = map(string) default = {} } -variable "hosting_build_spec" { +variable "distribution_build_spec" { description = "Build specification in YAML format" type = string default = <<-EOT @@ -65,39 +65,39 @@ variable "hosting_build_spec" { EOT } -variable "hosting_framework" { +variable "distribution_framework" { description = "Application framework (React, Vue, Angular, etc.)" type = string default = "React" } -variable "hosting_resource_tags_json" { +variable "distribution_resource_tags_json" { description = "Resource tags as JSON object" type = map(string) default = {} } locals { - hosting_default_tags = merge(var.hosting_resource_tags_json, { - Application = var.hosting_app_name - Environment = var.hosting_environment + distribution_default_tags = merge(var.distribution_resource_tags_json, { + Application = var.distribution_app_name + Environment = var.distribution_environment ManagedBy = "terraform" Module = "hosting/amplify" }) - hosting_env_vars = merge({ - ENVIRONMENT = var.hosting_environment - APP_NAME = var.hosting_app_name - }, var.hosting_environment_variables) + distribution_env_vars = merge({ + ENVIRONMENT = var.distribution_environment + APP_NAME = var.distribution_app_name + }, var.distribution_environment_variables) } resource "aws_amplify_app" "main" { - name = "${var.hosting_app_name}-${var.hosting_environment}" - repository = var.hosting_repository_url + name = "${var.distribution_app_name}-${var.distribution_environment}" + repository = var.distribution_repository_url - access_token = var.hosting_github_access_token - build_spec = var.hosting_build_spec - environment_variables = local.hosting_env_vars + access_token = var.distribution_github_access_token + build_spec = var.distribution_build_spec + environment_variables = local.distribution_env_vars custom_rule { source = "" @@ -110,29 +110,29 @@ resource "aws_amplify_app" "main" { enable_branch_auto_deletion = false platform = "WEB" - tags = local.hosting_default_tags + tags = local.distribution_default_tags } resource "aws_amplify_branch" "main" { app_id = aws_amplify_app.main.id - branch_name = var.hosting_branch_name + branch_name = var.distribution_branch_name - framework = var.hosting_framework - stage = var.hosting_environment == "prod" ? "PRODUCTION" : "DEVELOPMENT" + framework = var.distribution_framework + stage = var.distribution_environment == "prod" ? "PRODUCTION" : "DEVELOPMENT" enable_auto_build = true environment_variables = { - BRANCH = var.hosting_branch_name + BRANCH = var.distribution_branch_name } - tags = local.hosting_default_tags + tags = local.distribution_default_tags } resource "aws_amplify_domain_association" "main" { - count = var.hosting_custom_domain != null ? 1 : 0 + count = var.distribution_custom_domain != null ? 1 : 0 app_id = aws_amplify_app.main.id - domain_name = var.hosting_custom_domain + domain_name = var.distribution_custom_domain sub_domain { branch_name = aws_amplify_branch.main.branch_name @@ -156,49 +156,49 @@ resource "aws_amplify_webhook" "main" { # Locals for cross-module references (consumed by network/route53) locals { # Amplify default domain for DNS pointing - hosting_target_domain = "${aws_amplify_branch.main.branch_name}.${aws_amplify_app.main.id}.amplifyapp.com" + distribution_target_domain = "${aws_amplify_branch.main.branch_name}.${aws_amplify_app.main.id}.amplifyapp.com" # Amplify uses CNAME records, not alias - so no hosted zone ID needed - hosting_target_zone_id = null + distribution_target_zone_id = null # Amplify requires CNAME records - hosting_record_type = "CNAME" + distribution_record_type = "CNAME" } -output "hosting_app_id" { +output "distribution_app_id" { description = "Amplify application ID" value = aws_amplify_app.main.id } -output "hosting_app_arn" { +output "distribution_app_arn" { description = "Amplify application ARN" value = aws_amplify_app.main.arn } -output "hosting_default_domain" { +output "distribution_default_domain" { description = "Amplify default domain" - value = "https://${local.hosting_target_domain}" + value = "https://${local.distribution_target_domain}" } -output "hosting_target_domain" { +output "distribution_target_domain" { description = "Target domain for DNS records" - value = local.hosting_target_domain + value = local.distribution_target_domain } -output "hosting_target_zone_id" { +output "distribution_target_zone_id" { description = "Hosted zone ID for alias records (null for Amplify/CNAME)" - value = local.hosting_target_zone_id + value = local.distribution_target_zone_id } -output "hosting_record_type" { +output "distribution_record_type" { description = "DNS record type to use (CNAME for Amplify)" - value = local.hosting_record_type + value = local.distribution_record_type } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${local.hosting_target_domain}" + value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "https://${local.distribution_target_domain}" } -output "hosting_webhook_url" { +output "distribution_webhook_url" { description = "Webhook URL for manual triggers" value = aws_amplify_webhook.main.url sensitive = true diff --git a/frontend/deployment/distribution/amplify/setup b/frontend/deployment/distribution/amplify/setup index 0e7be107..90a3fc2f 100755 --- a/frontend/deployment/distribution/amplify/setup +++ b/frontend/deployment/distribution/amplify/setup @@ -3,18 +3,18 @@ # Amplify Hosting Setup # Adds amplify-specific variables to TOFU_VARIABLES -hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -hosting_repository_url=$(echo "$TOFU_VARIABLES" | jq -r '.repository_url') -hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') +distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) +distribution_repository_url=$(echo "$CONTEXT" | jq -r .application.repository_url) +distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg app_name "$hosting_app_name" \ - --arg repository_url "$hosting_repository_url" \ - --arg environment "$hosting_environment" \ + --arg app_name "$distribution_app_name" \ + --arg repository_url "$distribution_repository_url" \ + --arg environment "$distribution_environment" \ '. + { - hosting_app_name: $app_name, - hosting_repository_url: $repository_url, - hosting_environment: $environment + distribution_app_name: $app_name, + distribution_repository_url: $repository_url, + distribution_environment: $environment }') # Add module to composition list diff --git a/frontend/deployment/distribution/blob-cdn/modules/main.tf b/frontend/deployment/distribution/blob-cdn/modules/main.tf index a425583f..152c1c22 100644 --- a/frontend/deployment/distribution/blob-cdn/modules/main.tf +++ b/frontend/deployment/distribution/blob-cdn/modules/main.tf @@ -1,59 +1,59 @@ # Azure Blob Storage + CDN Hosting # Resources for Azure static hosting with CDN -variable "hosting_app_name" { +variable "distribution_app_name" { description = "Application name" type = string } -variable "hosting_environment" { +variable "distribution_environment" { description = "Environment (dev, staging, prod)" type = string default = "prod" } -variable "hosting_location" { +variable "distribution_location" { description = "Azure region" type = string default = "eastus2" } -variable "hosting_custom_domain" { +variable "distribution_custom_domain" { description = "Custom domain (e.g., app.example.com)" type = string default = null } -variable "hosting_cdn_sku" { +variable "distribution_cdn_sku" { description = "CDN Profile SKU" type = string default = "Standard_Microsoft" } -variable "hosting_tags" { +variable "distribution_tags" { description = "Resource tags" type = map(string) default = {} } locals { - hosting_storage_account_name = lower(replace("${var.hosting_app_name}${var.hosting_environment}static", "-", "")) + distribution_storage_account_name = lower(replace("${var.distribution_app_name}${var.distribution_environment}static", "-", "")) - hosting_default_tags = merge(var.hosting_tags, { - Application = var.hosting_app_name - Environment = var.hosting_environment + distribution_default_tags = merge(var.distribution_tags, { + Application = var.distribution_app_name + Environment = var.distribution_environment ManagedBy = "terraform" }) } resource "azurerm_resource_group" "main" { - name = "rg-${var.hosting_app_name}-${var.hosting_environment}" - location = var.hosting_location - tags = local.hosting_default_tags + name = "rg-${var.distribution_app_name}-${var.distribution_environment}" + location = var.distribution_location + tags = local.distribution_default_tags } resource "azurerm_storage_account" "static" { - name = substr(local.hosting_storage_account_name, 0, 24) + name = substr(local.distribution_storage_account_name, 0, 24) resource_group_name = azurerm_resource_group.main.name location = azurerm_resource_group.main.location account_tier = "Standard" @@ -81,19 +81,19 @@ resource "azurerm_storage_account" "static" { } } - tags = local.hosting_default_tags + tags = local.distribution_default_tags } resource "azurerm_cdn_profile" "main" { - name = "cdn-${var.hosting_app_name}-${var.hosting_environment}" + name = "cdn-${var.distribution_app_name}-${var.distribution_environment}" location = "global" resource_group_name = azurerm_resource_group.main.name - sku = var.hosting_cdn_sku - tags = local.hosting_default_tags + sku = var.distribution_cdn_sku + tags = local.distribution_default_tags } resource "azurerm_cdn_endpoint" "static" { - name = "${var.hosting_app_name}-${var.hosting_environment}" + name = "${var.distribution_app_name}-${var.distribution_environment}" profile_name = azurerm_cdn_profile.main.name location = "global" resource_group_name = azurerm_resource_group.main.name @@ -120,15 +120,15 @@ resource "azurerm_cdn_endpoint" "static" { querystring_caching_behaviour = "IgnoreQueryString" - tags = local.hosting_default_tags + tags = local.distribution_default_tags } resource "azurerm_cdn_endpoint_custom_domain" "main" { - count = var.hosting_custom_domain != null ? 1 : 0 + count = var.distribution_custom_domain != null ? 1 : 0 - name = replace(var.hosting_custom_domain, ".", "-") + name = replace(var.distribution_custom_domain, ".", "-") cdn_endpoint_id = azurerm_cdn_endpoint.static.id - host_name = var.hosting_custom_domain + host_name = var.distribution_custom_domain cdn_managed_https { certificate_type = "Dedicated" @@ -137,27 +137,27 @@ resource "azurerm_cdn_endpoint_custom_domain" "main" { } } -output "hosting_resource_group_name" { +output "distribution_resource_group_name" { description = "Resource Group name" value = azurerm_resource_group.main.name } -output "hosting_storage_account_name" { +output "distribution_storage_account_name" { description = "Storage Account name" value = azurerm_storage_account.static.name } -output "hosting_cdn_endpoint_hostname" { +output "distribution_cdn_endpoint_hostname" { description = "CDN Endpoint hostname" value = azurerm_cdn_endpoint.static.fqdn } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "https://${azurerm_cdn_endpoint.static.fqdn}" + value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "https://${azurerm_cdn_endpoint.static.fqdn}" } -output "hosting_upload_command" { +output "distribution_upload_command" { description = "Command to upload files" value = "az storage blob upload-batch --account-name ${azurerm_storage_account.static.name} --destination '$web' --source ./dist" } diff --git a/frontend/deployment/distribution/blob-cdn/setup b/frontend/deployment/distribution/blob-cdn/setup index 0a3a9f4d..76eecc7a 100755 --- a/frontend/deployment/distribution/blob-cdn/setup +++ b/frontend/deployment/distribution/blob-cdn/setup @@ -1,16 +1,15 @@ #!/bin/bash # Azure Blob + CDN Hosting Setup - -hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') +distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) +distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg app_name "$hosting_app_name" \ - --arg environment "$hosting_environment" \ + --arg app_name "$distribution_app_name" \ + --arg environment "$distribution_environment" \ '. + { - hosting_app_name: $app_name, - hosting_environment: $environment + distribution_app_name: $app_name, + distribution_environment: $environment }') # Add module to composition list diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index b852595d..64fba1ac 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -2,11 +2,11 @@ # S3 + CloudFront Hosting Setup -application_slug=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -scope_slug=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug') -scope_id=$(echo "$TOFU_VARIABLES" | jq -r '.scope_id') +application_slug=$(echo "$CONTEXT" | jq -r .application.slug) +scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +scope_id=$(echo "$CONTEXT" | jq -r .scope.id) -hosting_app_name="$application_slug-$scope_slug-$scope_id" +distribution_app_name="$application_slug-$scope_slug-$scope_id" # Fetch bucket name from assets-repository provider echo "🔍 Fetching assets-repository provider..." @@ -26,7 +26,7 @@ if [ $np_exit_code -ne 0 ]; then echo "" echo " 💡 Possible causes:" echo " • No assets-repository provider is configured for this scope" - echo " • The provider category 'assets-repository' does not exist" + echo " • The provider category 'assets-repository' fes not exist" echo "" echo " 🔧 How to fix:" echo " 1. Create an assets-repository provider in nullplatform" @@ -58,11 +58,11 @@ if [ $np_exit_code -ne 0 ]; then exit 1 fi -hosting_bucket_name=$(echo "$asset_repository" | jq -r ' +distribution_bucket_name=$(echo "$asset_repository" | jq -r ' [.results[] | select(.attributes.bucket.name != null)] | first | .attributes.bucket.name // empty ') -if [ -z "$hosting_bucket_name" ] || [ "$hosting_bucket_name" = "null" ]; then +if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" ]; then echo "" echo "❌ No S3 bucket found in assets-repository providers" echo "" @@ -80,25 +80,29 @@ if [ -z "$hosting_bucket_name" ] || [ "$hosting_bucket_name" = "null" ]; then exit 1 fi -echo "✅ Bucket name: $hosting_bucket_name" +echo "✅ Bucket name: $distribution_bucket_name" # S3 prefix for multi-scope bucket support # TODO: Replace with your prefix variable -hosting_s3_prefix="/app" +distribution_s3_prefix="/app" -echo "📁 S3 prefix: ${hosting_s3_prefix:-"(root)"}" +echo "📁 S3 prefix: ${distribution_s3_prefix:-"(root)"}" + +RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg bucket_name "$hosting_bucket_name" \ - --arg app_name "$hosting_app_name" \ - --arg s3_prefix "$hosting_s3_prefix" \ + --arg bucket_name "$distribution_bucket_name" \ + --arg app_name "$distribution_app_name" \ + --argjson resource_tags_json "$RESOURCE_TAGS_JSON" \ + --arg s3_prefix "$distribution_s3_prefix" \ '. + { - hosting_bucket_name: $bucket_name, - hosting_app_name: $app_name, - hosting_s3_prefix: $s3_prefix + distribution_bucket_name: $bucket_name, + distribution_app_name: $app_name, + distribution_s3_prefix: $s3_prefix, + distribution_resource_tags_json: $resource_tags_json }') -echo "✅ S3 + CloudFront hosting configured" +echo "✅ S3 + CloudFront distribution configured" # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" diff --git a/frontend/deployment/distribution/firebase/modules/main.tf b/frontend/deployment/distribution/firebase/modules/main.tf index f4243380..c99c1a0b 100644 --- a/frontend/deployment/distribution/firebase/modules/main.tf +++ b/frontend/deployment/distribution/firebase/modules/main.tf @@ -1,87 +1,87 @@ # Firebase Hosting # Resources for Firebase static hosting -variable "hosting_project_id" { +variable "distribution_project_id" { description = "GCP/Firebase project ID" type = string } -variable "hosting_app_name" { +variable "distribution_app_name" { description = "Application name" type = string } -variable "hosting_environment" { +variable "distribution_environment" { description = "Environment (dev, staging, prod)" type = string default = "prod" } -variable "hosting_custom_domains" { +variable "distribution_custom_domains" { description = "List of custom domains" type = list(string) default = [] } -variable "hosting_labels" { +variable "distribution_labels" { description = "Resource labels" type = map(string) default = {} } locals { - hosting_site_id = "${var.hosting_app_name}-${var.hosting_environment}" + distribution_site_id = "${var.distribution_app_name}-${var.distribution_environment}" - hosting_default_labels = merge(var.hosting_labels, { - application = replace(var.hosting_app_name, "-", "_") - environment = var.hosting_environment + distribution_default_labels = merge(var.distribution_labels, { + application = replace(var.distribution_app_name, "-", "_") + environment = var.distribution_environment managed_by = "terraform" }) } resource "google_firebase_project" "default" { provider = google-beta - project = var.hosting_project_id + project = var.distribution_project_id } -resource "google_firebase_hosting_site" "default" { +resource "google_firebase_distribution_site" "default" { provider = google-beta project = google_firebase_project.default.project - site_id = local.hosting_site_id + site_id = local.distribution_site_id } -resource "google_firebase_hosting_custom_domain" "domains" { - for_each = toset(var.hosting_custom_domains) +resource "google_firebase_distribution_custom_domain" "domains" { + for_each = toset(var.distribution_custom_domains) provider = google-beta project = google_firebase_project.default.project - site_id = google_firebase_hosting_site.default.site_id + site_id = google_firebase_distribution_site.default.site_id custom_domain = each.value wait_dns_verification = false } -output "hosting_project_id" { +output "distribution_project_id" { description = "Firebase project ID" value = google_firebase_project.default.project } -output "hosting_site_id" { +output "distribution_site_id" { description = "Firebase Hosting site ID" - value = google_firebase_hosting_site.default.site_id + value = google_firebase_distribution_site.default.site_id } -output "hosting_default_url" { +output "distribution_default_url" { description = "Firebase Hosting default URL" - value = "https://${google_firebase_hosting_site.default.site_id}.web.app" + value = "https://${google_firebase_distribution_site.default.site_id}.web.app" } -output "hosting_firebaseapp_url" { +output "distribution_firebaseapp_url" { description = "Firebase alternative URL" - value = "https://${google_firebase_hosting_site.default.site_id}.firebaseapp.com" + value = "https://${google_firebase_distribution_site.default.site_id}.firebaseapp.com" } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = length(var.hosting_custom_domains) > 0 ? "https://${var.hosting_custom_domains[0]}" : "https://${google_firebase_hosting_site.default.site_id}.web.app" + value = length(var.distribution_custom_domains) > 0 ? "https://${var.distribution_custom_domains[0]}" : "https://${google_firebase_distribution_site.default.site_id}.web.app" } diff --git a/frontend/deployment/distribution/firebase/setup b/frontend/deployment/distribution/firebase/setup index e2883c6a..cfe2181a 100755 --- a/frontend/deployment/distribution/firebase/setup +++ b/frontend/deployment/distribution/firebase/setup @@ -2,23 +2,23 @@ # Firebase Hosting Setup -hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') -hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') +distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) +distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) +distribution_project_id="must fill this value" -if [ -z "$hosting_project_id" ]; then +if [ -z "$distribution_project_id" ]; then echo "✗ GCP project not configured. Run provider/gcp setup first." exit 1 fi TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg app_name "$hosting_app_name" \ - --arg environment "$hosting_environment" \ - --arg project_id "$hosting_project_id" \ + --arg app_name "$distribution_app_name" \ + --arg environment "$distribution_environment" \ + --arg project_id "$distribution_project_id" \ '. + { - hosting_app_name: $app_name, - hosting_environment: $environment, - hosting_project_id: $project_id + distribution_app_name: $app_name, + distribution_environment: $environment, + distribution_project_id: $project_id }') # Add module to composition list diff --git a/frontend/deployment/distribution/gcs-cdn/modules/main.tf b/frontend/deployment/distribution/gcs-cdn/modules/main.tf index 4615aabf..5182dd48 100644 --- a/frontend/deployment/distribution/gcs-cdn/modules/main.tf +++ b/frontend/deployment/distribution/gcs-cdn/modules/main.tf @@ -1,54 +1,54 @@ # GCP Cloud Storage + Cloud CDN Hosting # Resources for GCS static hosting with Cloud CDN -variable "hosting_project_id" { +variable "distribution_project_id" { description = "GCP project ID" type = string } -variable "hosting_app_name" { +variable "distribution_app_name" { description = "Application name" type = string } -variable "hosting_environment" { +variable "distribution_environment" { description = "Environment (dev, staging, prod)" type = string default = "prod" } -variable "hosting_region" { +variable "distribution_region" { description = "GCP region" type = string default = "us-central1" } -variable "hosting_custom_domain" { +variable "distribution_custom_domain" { description = "Custom domain (e.g., app.example.com)" type = string default = null } -variable "hosting_labels" { +variable "distribution_labels" { description = "Resource labels" type = map(string) default = {} } locals { - hosting_bucket_name = "${var.hosting_app_name}-${var.hosting_environment}-static-${var.hosting_project_id}" + distribution_bucket_name = "${var.distribution_app_name}-${var.distribution_environment}-static-${var.distribution_project_id}" - hosting_default_labels = merge(var.hosting_labels, { - application = var.hosting_app_name - environment = var.hosting_environment + distribution_default_labels = merge(var.distribution_labels, { + application = var.distribution_app_name + environment = var.distribution_environment managed_by = "terraform" }) } resource "google_storage_bucket" "static" { - name = local.hosting_bucket_name - project = var.hosting_project_id - location = var.hosting_region + name = local.distribution_bucket_name + project = var.distribution_project_id + location = var.distribution_region force_destroy = false website { @@ -69,7 +69,7 @@ resource "google_storage_bucket" "static" { uniform_bucket_level_access = true - labels = local.hosting_default_labels + labels = local.distribution_default_labels } resource "google_storage_bucket_iam_member" "public_read" { @@ -79,8 +79,8 @@ resource "google_storage_bucket_iam_member" "public_read" { } resource "google_compute_backend_bucket" "static" { - name = "${var.hosting_app_name}-${var.hosting_environment}-backend" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-backend" + project = var.distribution_project_id bucket_name = google_storage_bucket.static.name enable_cdn = true @@ -101,38 +101,38 @@ resource "google_compute_backend_bucket" "static" { } resource "google_compute_url_map" "static" { - name = "${var.hosting_app_name}-${var.hosting_environment}-urlmap" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-urlmap" + project = var.distribution_project_id default_service = google_compute_backend_bucket.static.id } resource "google_compute_managed_ssl_certificate" "static" { - count = var.hosting_custom_domain != null ? 1 : 0 - name = "${var.hosting_app_name}-${var.hosting_environment}-cert" - project = var.hosting_project_id + count = var.distribution_custom_domain != null ? 1 : 0 + name = "${var.distribution_app_name}-${var.distribution_environment}-cert" + project = var.distribution_project_id managed { - domains = [var.hosting_custom_domain] + domains = [var.distribution_custom_domain] } } resource "google_compute_target_https_proxy" "static" { - count = var.hosting_custom_domain != null ? 1 : 0 - name = "${var.hosting_app_name}-${var.hosting_environment}-https-proxy" - project = var.hosting_project_id + count = var.distribution_custom_domain != null ? 1 : 0 + name = "${var.distribution_app_name}-${var.distribution_environment}-https-proxy" + project = var.distribution_project_id url_map = google_compute_url_map.static.id ssl_certificates = [google_compute_managed_ssl_certificate.static[0].id] } resource "google_compute_target_http_proxy" "static" { - name = "${var.hosting_app_name}-${var.hosting_environment}-http-proxy" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-http-proxy" + project = var.distribution_project_id url_map = google_compute_url_map.http_redirect.id } resource "google_compute_url_map" "http_redirect" { - name = "${var.hosting_app_name}-${var.hosting_environment}-http-redirect" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-http-redirect" + project = var.distribution_project_id default_url_redirect { https_redirect = true @@ -142,14 +142,14 @@ resource "google_compute_url_map" "http_redirect" { } resource "google_compute_global_address" "static" { - name = "${var.hosting_app_name}-${var.hosting_environment}-ip" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-ip" + project = var.distribution_project_id } resource "google_compute_global_forwarding_rule" "https" { - count = var.hosting_custom_domain != null ? 1 : 0 - name = "${var.hosting_app_name}-${var.hosting_environment}-https-rule" - project = var.hosting_project_id + count = var.distribution_custom_domain != null ? 1 : 0 + name = "${var.distribution_app_name}-${var.distribution_environment}-https-rule" + project = var.distribution_project_id ip_address = google_compute_global_address.static.address ip_protocol = "TCP" port_range = "443" @@ -158,8 +158,8 @@ resource "google_compute_global_forwarding_rule" "https" { } resource "google_compute_global_forwarding_rule" "http" { - name = "${var.hosting_app_name}-${var.hosting_environment}-http-rule" - project = var.hosting_project_id + name = "${var.distribution_app_name}-${var.distribution_environment}-http-rule" + project = var.distribution_project_id ip_address = google_compute_global_address.static.address ip_protocol = "TCP" port_range = "80" @@ -167,27 +167,27 @@ resource "google_compute_global_forwarding_rule" "http" { load_balancing_scheme = "EXTERNAL_MANAGED" } -output "hosting_bucket_name" { +output "distribution_bucket_name" { description = "GCS bucket name" value = google_storage_bucket.static.name } -output "hosting_bucket_url" { +output "distribution_bucket_url" { description = "GCS bucket URL" value = google_storage_bucket.static.url } -output "hosting_load_balancer_ip" { +output "distribution_load_balancer_ip" { description = "Load Balancer IP" value = google_compute_global_address.static.address } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = var.hosting_custom_domain != null ? "https://${var.hosting_custom_domain}" : "http://${google_compute_global_address.static.address}" + value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "http://${google_compute_global_address.static.address}" } -output "hosting_upload_command" { +output "distribution_upload_command" { description = "Command to upload files" value = "gsutil -m rsync -r ./dist gs://${google_storage_bucket.static.name}" } diff --git a/frontend/deployment/distribution/gcs-cdn/setup b/frontend/deployment/distribution/gcs-cdn/setup index 024ab570..5637f51d 100755 --- a/frontend/deployment/distribution/gcs-cdn/setup +++ b/frontend/deployment/distribution/gcs-cdn/setup @@ -2,26 +2,26 @@ # GCS + Cloud CDN Hosting Setup -hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') -hosting_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') -hosting_region=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.region // "us-central1"') +distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) +distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) +distribution_project_id="must fill this value" +distribution_region="must fill this value" -if [ -z "$hosting_project_id" ]; then +if [ -z "$distribution_project_id" ]; then echo "✗ GCP project not configured. Run provider/gcp setup first." exit 1 fi TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg app_name "$hosting_app_name" \ - --arg environment "$hosting_environment" \ - --arg project_id "$hosting_project_id" \ - --arg region "$hosting_region" \ + --arg app_name "$distribution_app_name" \ + --arg environment "$distribution_environment" \ + --arg project_id "$distribution_project_id" \ + --arg region "$distribution_region" \ '. + { - hosting_app_name: $app_name, - hosting_environment: $environment, - hosting_project_id: $project_id, - hosting_region: $region + distribution_app_name: $app_name, + distribution_environment: $environment, + distribution_project_id: $project_id, + distribution_region: $region }') # Add module to composition list diff --git a/frontend/deployment/distribution/static-web-apps/modules/main.tf b/frontend/deployment/distribution/static-web-apps/modules/main.tf index d010e384..a0ac20f0 100644 --- a/frontend/deployment/distribution/static-web-apps/modules/main.tf +++ b/frontend/deployment/distribution/static-web-apps/modules/main.tf @@ -1,106 +1,106 @@ # Azure Static Web Apps Hosting # Resources for Azure Static Web Apps -variable "hosting_app_name" { +variable "distribution_app_name" { description = "Application name" type = string } -variable "hosting_environment" { +variable "distribution_environment" { description = "Environment (dev, staging, prod)" type = string default = "prod" } -variable "hosting_location" { +variable "distribution_location" { description = "Azure region" type = string default = "eastus2" } -variable "hosting_sku_tier" { +variable "distribution_sku_tier" { description = "SKU tier (Free or Standard)" type = string default = "Free" } -variable "hosting_sku_size" { +variable "distribution_sku_size" { description = "SKU size" type = string default = "Free" } -variable "hosting_custom_domains" { +variable "distribution_custom_domains" { description = "List of custom domains" type = list(string) default = [] } -variable "hosting_tags" { +variable "distribution_tags" { description = "Resource tags" type = map(string) default = {} } locals { - hosting_default_tags = merge(var.hosting_tags, { - Application = var.hosting_app_name - Environment = var.hosting_environment + distribution_default_tags = merge(var.distribution_tags, { + Application = var.distribution_app_name + Environment = var.distribution_environment ManagedBy = "terraform" }) } resource "azurerm_resource_group" "main" { - name = "rg-${var.hosting_app_name}-${var.hosting_environment}" - location = var.hosting_location - tags = local.hosting_default_tags + name = "rg-${var.distribution_app_name}-${var.distribution_environment}" + location = var.distribution_location + tags = local.distribution_default_tags } resource "azurerm_static_web_app" "main" { - name = "swa-${var.hosting_app_name}-${var.hosting_environment}" + name = "swa-${var.distribution_app_name}-${var.distribution_environment}" resource_group_name = azurerm_resource_group.main.name - location = var.hosting_location + location = var.distribution_location - sku_tier = var.hosting_sku_tier - sku_size = var.hosting_sku_size + sku_tier = var.distribution_sku_tier + sku_size = var.distribution_sku_size - tags = local.hosting_default_tags + tags = local.distribution_default_tags } resource "azurerm_static_web_app_custom_domain" "main" { - for_each = toset(var.hosting_custom_domains) + for_each = toset(var.distribution_custom_domains) static_web_app_id = azurerm_static_web_app.main.id domain_name = each.value validation_type = "cname-delegation" } -output "hosting_resource_group_name" { +output "distribution_resource_group_name" { description = "Resource Group name" value = azurerm_resource_group.main.name } -output "hosting_static_web_app_id" { +output "distribution_static_web_app_id" { description = "Static Web App ID" value = azurerm_static_web_app.main.id } -output "hosting_static_web_app_name" { +output "distribution_static_web_app_name" { description = "Static Web App name" value = azurerm_static_web_app.main.name } -output "hosting_default_hostname" { +output "distribution_default_hostname" { description = "Default hostname" value = azurerm_static_web_app.main.default_host_name } -output "hosting_website_url" { +output "distribution_website_url" { description = "Website URL" - value = length(var.hosting_custom_domains) > 0 ? "https://${var.hosting_custom_domains[0]}" : "https://${azurerm_static_web_app.main.default_host_name}" + value = length(var.distribution_custom_domains) > 0 ? "https://${var.distribution_custom_domains[0]}" : "https://${azurerm_static_web_app.main.default_host_name}" } -output "hosting_api_key" { +output "distribution_api_key" { description = "API key for deployments" value = azurerm_static_web_app.main.api_key sensitive = true diff --git a/frontend/deployment/distribution/static-web-apps/setup b/frontend/deployment/distribution/static-web-apps/setup index dd8f1c59..c51d9f6f 100755 --- a/frontend/deployment/distribution/static-web-apps/setup +++ b/frontend/deployment/distribution/static-web-apps/setup @@ -2,15 +2,15 @@ # Azure Static Web Apps Hosting Setup -hosting_app_name=$(echo "$TOFU_VARIABLES" | jq -r '.application_slug') -hosting_environment=$(echo "$TOFU_VARIABLES" | jq -r '.scope_slug // "prod"') +distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) +distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg app_name "$hosting_app_name" \ - --arg environment "$hosting_environment" \ + --arg app_name "$distribution_app_name" \ + --arg environment "$distribution_environment" \ '. + { - hosting_app_name: $app_name, - hosting_environment: $environment + distribution_app_name: $app_name, + distribution_environment: $environment }') # Add module to composition list diff --git a/frontend/deployment/do_tofu b/frontend/deployment/do_tofu index 85b4f303..bd91c906 100644 --- a/frontend/deployment/do_tofu +++ b/frontend/deployment/do_tofu @@ -1,35 +1,23 @@ #!/bin/bash -#echo $TOFU_VARIABLES | jq . +set -eou pipefail -echo "$TOFU_INIT_VARIABLES" -echo "$TOFU_VARIABLES" -echo "$MODULES_TO_USE" -#set -eou pipefail -# -#CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") -# -#cd "$CURRENT_DIR" -# -#AWS_REGION="${AWS_REGION:-us-east-1}" -#TF_STATE_BUCKET="test-static-null2" -#TF_LOCK_TABLE="service-provisioning-terraform-state-lock" -## You need to export the GITHUB_TOKEN as an env var in the agent -##GITHUB_TOKEN="" -# -#HOSTED_PUBLIC_ZONE_ID=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id') -# -#DOMAIN=$(aws route53 get-hosted-zone --id "$HOSTED_PUBLIC_ZONE_ID" --query 'HostedZone.Name' --output text | sed 's/\.$//') -#SUBDOMAIN="$APPLICATION_SLUG-$SCOPE_SLUG" -# -#np scope patch --id "$SCOPE_ID" --body "{\"domain\":\"$SUBDOMAIN.$DOMAIN\"}" -# -#tofu init \ +CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") + +cd "$CURRENT_DIR" + +#np scope patchtofu init \ # -backend-config="bucket=${TF_STATE_BUCKET}" \ # -backend-config="key=amplify/$APPLICATION_SLUG/$SCOPE_SLUG-$SCOPE_ID" \ # -backend-config="region=${AWS_REGION}" \ -# -backend-config="dynamodb_table=${TF_LOCK_TABLE}" -# +# -backend-config="dynamodb_table=${TF_LOCK_TABLE}" --id "$SCOPE_ID" --body "{\"domain\":\"$SUBDOMAIN.$DOMAIN\"}" +# Write variables to a temp file for tofu +TOFU_VAR_FILE="$TOFU_MODULE_DIR/.tfvars.json" +echo "$TOFU_VARIABLES" > "$TOFU_VAR_FILE" + +tofu -chdir="$TOFU_MODULE_DIR" init -input=false $TOFU_INIT_VARIABLES +tofu -chdir="$TOFU_MODULE_DIR" "$ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" + #tofu $ACTION -auto-approve \ # -var="aws_region=${AWS_REGION}" \ # -var="github_token=${GITHUB_TOKEN}" \ @@ -39,4 +27,4 @@ echo "$MODULES_TO_USE" # -var="env_vars_json=${ENV_VARS_JSON}" \ # -var="resource_tags_json=${RESOURCE_TAGS_JSON}" \ # -var="domain=${DOMAIN}" \ -# -var="subdomain=${SUBDOMAIN}" \ No newline at end of file +# -var="subdomain=${SUBDOMAIN}" diff --git a/frontend/deployment/network/azure_dns/setup b/frontend/deployment/network/azure_dns/setup index a039d808..224600cb 100755 --- a/frontend/deployment/network/azure_dns/setup +++ b/frontend/deployment/network/azure_dns/setup @@ -23,7 +23,7 @@ if [ -z "$network_domain" ]; then fi # Get target from hosting outputs (CDN endpoint, Static Web App hostname, etc.) -network_target_domain=$(echo "$TOFU_VARIABLES" | jq -r '.hosting_cdn_endpoint_hostname // .hosting_default_hostname // empty') +network_target_domain=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_cdn_endpoint_hostname // .distribution_default_hostname // empty') TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg resource_group "$network_resource_group" \ diff --git a/frontend/deployment/network/cloud_dns/setup b/frontend/deployment/network/cloud_dns/setup index 23a3d0bc..9ae7b088 100755 --- a/frontend/deployment/network/cloud_dns/setup +++ b/frontend/deployment/network/cloud_dns/setup @@ -3,7 +3,7 @@ # Cloud DNS Setup # Configures DNS variables based on hosting output -network_project_id=$(echo "$TOFU_VARIABLES" | jq -r '.gcp_provider.project // empty') +network_project_id="must fill this value" if [ -z "$network_project_id" ]; then echo "✗ GCP project not configured. Run provider/gcp setup first." @@ -23,7 +23,7 @@ if [ -z "$network_domain" ]; then fi # Get target from hosting outputs (Load Balancer IP, etc.) -network_target_ip=$(echo "$TOFU_VARIABLES" | jq -r '.hosting_load_balancer_ip // empty') +network_target_ip=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_load_balancer_ip // empty') TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg project_id "$network_project_id" \ diff --git a/frontend/deployment/provider/aws/setup b/frontend/deployment/provider/aws/setup index f2e168e4..92c63bc3 100755 --- a/frontend/deployment/provider/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -15,15 +15,27 @@ if [ -z "${TOFU_LOCK_TABLE:-}" ]; then exit 1 fi +RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} + TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg aws_region "$AWS_REGION" \ --arg tf_state_bucket "$TOFU_PROVIDER_BUCKET" \ + --argjson resource_tags_json "$RESOURCE_TAGS_JSON" \ --arg tf_lock_table "$TOFU_LOCK_TABLE" \ - '. + {aws_provider: {region: $aws_region, state_bucket: $tf_state_bucket, lock_table: $tf_lock_table}}') + '. + {aws_provider: {region: $aws_region, state_bucket: $tf_state_bucket, lock_table: $tf_lock_table}, provider_resource_tags_json: $resource_tags_json}') + +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=bucket=$TOFU_PROVIDER_BUCKET" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=region=$AWS_REGION" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=dynamodb_table=$TOFU_LOCK_TABLE" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"bucket=$TOFU_PROVIDER_BUCKET\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"region=$AWS_REGION\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"dynamodb_table=$TOFU_LOCK_TABLE\"" +# TODO(federico.maleh) this is necessary for the integration tests, tests should not make us change the production code +if [ -n "${AWS_ENDPOINT_URL:-}" ]; then + TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=force_path_style=true" + TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_credentials_validation=true" + TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_metadata_api_check=true" + TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_region_validation=true" + TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=endpoints={s3=\"$AWS_ENDPOINT_URL\",dynamodb=\"$AWS_ENDPOINT_URL\"}" +fi script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/build_context_test.bats index 7d93ff7a..84d0c3ac 100644 --- a/frontend/deployment/tests/build_context_test.bats +++ b/frontend/deployment/tests/build_context_test.bats @@ -54,29 +54,7 @@ run_build_context() { run_build_context # Expected JSON - update this when adding new fields - local expected='{ - "application_slug": "automation", - "application_version": "v1.0.0", - "env_vars_json": { - "CLUSTER_NAME": "development-cluster", - "TEST": "testing-tools" - }, - "repository_url": "https://github.com/playground-repos/tools-automation", - "resource_tags_json": { - "account": "playground", - "account_id": 2, - "application": "automation", - "application_id": 4, - "deployment_id": 8, - "namespace": "tools", - "namespace_id": 3, - "nullplatform": "true", - "scope": "development-tools", - "scope_id": 7 - }, - "scope_id": "7", - "scope_slug": "development-tools" -}' + local expected='{}' assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } @@ -145,3 +123,26 @@ run_build_context() { assert_not_empty "$TOFU_MODULE_DIR" "TOFU_MODULE_DIR" } + +# ============================================================================= +# Test: RESOURCE_TAGS_JSON - verifies the entire JSON structure +# ============================================================================= +@test "RESOURCE_TAGS_JSON matches expected structure" { + run_build_context + + # Expected JSON - update this when adding new fields + local expected='{ + "account": "playground", + "account_id": 2, + "application": "automation", + "application_id": 4, + "deployment_id": 8, + "namespace": "tools", + "namespace_id": 3, + "nullplatform": "true", + "scope": "development-tools", + "scope_id": 7 + }' + + assert_json_equal "$RESOURCE_TAGS_JSON" "$expected" "RESOURCE_TAGS_JSON" +} \ No newline at end of file diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index 49849f32..3e9c8d4f 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -64,9 +64,10 @@ set_np_mock() { "application_slug": "automation", "scope_slug": "development-tools", "scope_id": "7", - "hosting_bucket_name": "assets-kwik-e-mart-main", - "hosting_app_name": "automation-development-tools-7", - "hosting_s3_prefix": "/app" + "distribution_bucket_name": "assets-bucket", + "distribution_app_name": "automation-development-tools-7", + "distribution_resource_tags_json": {}, + "distribution_s3_prefix": "/app" }' assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" @@ -152,3 +153,25 @@ set_np_mock() { assert_contains "$output" "Found 0 provider(s)" } +# ============================================================================= +# Test: Custom resource tags +# ============================================================================= +@test "TOFU_VARIABLES includes custom resource tags" { + set_np_mock "success.json" + export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' + + run_cloudfront_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "distribution_bucket_name": "assets-bucket", + "distribution_app_name": "automation-development-tools-7", + "distribution_resource_tags_json": {"Environment": "production", "Team": "platform"}, + "distribution_s3_prefix": "/app" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh new file mode 100755 index 00000000..cf0d760d --- /dev/null +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh @@ -0,0 +1,78 @@ +#!/bin/bash +# ============================================================================= +# Integration test: CloudFront distribution lifecycle +# +# Tests the full lifecycle of creating and destroying CloudFront infrastructure +# using shunit2 test framework. +# +# Run: ./run_integration_tests.sh cloudfront_lifecycle_test.sh +# ============================================================================= + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Set integration test directory before sourcing utilities +export INTEGRATION_TEST_DIR="$SCRIPT_DIR" + +# Source test utilities from shared location +. "$SCRIPT_DIR/../integration_test_utils.sh" + +# ============================================================================= +# Test Setup/Teardown +# ============================================================================= + +oneTimeSetUp() { + # Start LocalStack once for all tests in this file + localstack_start +} + +oneTimeTearDown() { + # Stop LocalStack after all tests complete + localstack_stop +} + +setUp() { + # Reset LocalStack state before each test + localstack_reset +} + +tearDown() { + # Cleanup after each test if needed + : +} + +# ============================================================================= +# Tests +# ============================================================================= + +test_create_and_destroy_cloudfront_distribution() { + # Load test configuration + load_test_config "$SCRIPT_DIR/configs/example_create_and_destroy.json" + + # Execute all steps defined in the config + execute_all_steps + + # If we get here without failures, the test passed + assertTrue "All steps completed successfully" true +} + +# ============================================================================= +# Load shunit2 +# ============================================================================= + +# Find shunit2 - check common locations +if [ -f "/usr/local/bin/shunit2" ]; then + . /usr/local/bin/shunit2 +elif [ -f "/usr/share/shunit2/shunit2" ]; then + . /usr/share/shunit2/shunit2 +elif [ -f "/opt/homebrew/bin/shunit2" ]; then + . /opt/homebrew/bin/shunit2 +elif command -v shunit2 &> /dev/null; then + . "$(command -v shunit2)" +else + echo "Error: shunit2 not found" + echo "" + echo "Install with:" + echo " brew install shunit2 # macOS" + echo " apt install shunit2 # Ubuntu/Debian" + exit 1 +fi diff --git a/frontend/deployment/tests/integration/configs/example_create_and_destroy.json b/frontend/deployment/tests/integration/configs/example_create_and_destroy.json new file mode 100644 index 00000000..bc8a88b4 --- /dev/null +++ b/frontend/deployment/tests/integration/configs/example_create_and_destroy.json @@ -0,0 +1,97 @@ +{ + "name": "Create and destroy CloudFront distribution", + "description": "Tests the full lifecycle: create infrastructure, verify it exists, then destroy it", + "context_file": "resources/context.json", + "setup": [ + "aws s3api create-bucket --bucket assets-bucket", + "aws s3api create-bucket --bucket tofu-state-bucket", + "aws dynamodb create-table --table-name tofu-locks --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --billing-mode PAY_PER_REQUEST", + "aws route53 create-hosted-zone --name frontend.publicdomain.com --caller-reference public-zone-id" + ], + "context_overrides": { + "providers.cloud-providers.networking.hosted_public_zone_id": "$(aws route53 list-hosted-zones --query 'HostedZones[0].Id' --output text | sed 's|/hostedzone/||')" + }, + "steps": [ + { + "name": "create_infrastructure", + "workflow": "$PROJECT_DIR/workflows/initial.yaml", + "env": { + "CUSTOM_TOFU_MODULES": "$INTEGRATION_TEST_DIR/localstack", + "SERVICE_PATH": "$PROJECT_DIR/..", + "NETWORK_LAYER": "route53", + "DISTRIBUTION_LAYER": "cloudfront", + "TOFU_PROVIDER": "aws", + "TOFU_PROVIDER_BUCKET": "tofu-state-bucket", + "TOFU_LOCK_TABLE": "tofu-locks", + "AWS_REGION": "us-east-1" + }, + "np_mocks": { + "np provider list": { + "response_file": "asset_repository_success.json", + "exit_code": 0 + }, + "np scope get": { + "response_file": "scope_success.json", + "exit_code": 0 + } + }, + "assertions": [ + { + "type": "s3_bucket_exists", + "bucket": "assets-bucket" + }, + { + "type": "cloudfront_distribution_exists", + "comment": "Distribution for automation-development-tools-7" + }, + { + "type": "route53_record_exists", + "name": "automation-development-tools.frontend.publicdomain.com", + "record_type": "A" + } + ] + }, + { + "name": "destroy_infrastructure", + "before": [ + "$INTEGRATION_TEST_DIR/scripts/disable_cloudfront.sh 'Distribution for automation-development-tools-7'" + ], + "workflow": "$PROJECT_DIR/workflows/delete.yaml", + "env": { + "CUSTOM_TOFU_MODULES": "$INTEGRATION_TEST_DIR/localstack", + "SERVICE_PATH": "$PROJECT_DIR/..", + "NETWORK_LAYER": "route53", + "DISTRIBUTION_LAYER": "cloudfront", + "TOFU_PROVIDER": "aws", + "TOFU_PROVIDER_BUCKET": "tofu-state-bucket", + "TOFU_LOCK_TABLE": "tofu-locks", + "AWS_REGION": "us-east-1" + }, + "np_mocks": { + "np provider list": { + "response_file": "asset_repository_success.json", + "exit_code": 0 + }, + "np scope get": { + "response_file": "scope_success.json", + "exit_code": 0 + } + }, + "assertions": [ + { + "type": "s3_bucket_exists", + "bucket": "assets-bucket" + }, + { + "type": "cloudfront_distribution_not_exists", + "comment": "Distribution for automation-development-tools-7" + }, + { + "type": "route53_record_not_exists", + "name": "automation-development-tools.frontend.publicdomain.com", + "record_type": "A" + } + ] + } + ] +} diff --git a/frontend/deployment/tests/integration/docker-compose.yml b/frontend/deployment/tests/integration/docker-compose.yml new file mode 100644 index 00000000..ddda2fab --- /dev/null +++ b/frontend/deployment/tests/integration/docker-compose.yml @@ -0,0 +1,37 @@ +services: + localstack: + image: localstack/localstack:latest + container_name: localstack-integration-tests + ports: + - "4566:4566" # LocalStack Gateway + - "4510-4559:4510-4559" # External services port range + environment: + - DEBUG=0 + - SERVICES=s3,route53,sts,iam,dynamodb,acm + - DEFAULT_REGION=us-east-1 + - AWS_DEFAULT_REGION=us-east-1 + - AWS_ACCESS_KEY_ID=test + - AWS_SECRET_ACCESS_KEY=test + - PERSISTENCE=0 + - EAGER_SERVICE_LOADING=1 + volumes: + - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" + - "/var/run/docker.sock:/var/run/docker.sock" + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:4566/_localstack/health"] + interval: 5s + timeout: 5s + retries: 10 + + moto: + image: motoserver/moto:latest + container_name: moto-cloudfront + ports: + - "5555:5000" + environment: + - MOTO_PORT=5000 + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:5000/moto-api/"] + interval: 5s + timeout: 5s + retries: 10 diff --git a/frontend/deployment/tests/integration/mocks/np b/frontend/deployment/tests/integration/mocks/np new file mode 100755 index 00000000..bc1d1be4 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/np @@ -0,0 +1,123 @@ +#!/bin/bash +# ============================================================================= +# np CLI mock for integration tests +# +# This mock intercepts all np commands EXCEPT 'np service workflow exec' +# which is passed through to the real np CLI. +# +# Mock responses are configured via environment variables: +# NP_MOCK_DIR - Directory containing mock response files +# NP_MOCK_CONFIG - JSON file with mock configuration for current test step +# NP_REAL_CLI - Path to the real np CLI (default: uses which np from original PATH) +# ============================================================================= + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# Build the command key from arguments (e.g., "np provider list", "np scope get") +# This extracts only the subcommands (no flags) for matching +build_command_key() { + local key="np" + local skip_next=false + for arg in "$@"; do + # Skip flag values (argument after a flag) + if [ "$skip_next" = true ]; then + skip_next=false + continue + fi + # Skip flags and mark to skip their values + if [[ "$arg" == --* ]]; then + skip_next=true + continue + fi + if [[ "$arg" == -* ]]; then + skip_next=true + continue + fi + key="$key $arg" + done + echo "$key" +} + +# Build full command string with flags for error messages +build_full_command() { + local cmd="np" + local i=1 + while [ $i -le $# ]; do + local arg="${!i}" + cmd="$cmd $arg" + i=$((i + 1)) + done + echo "$cmd" +} + +# Check if this is a workflow exec command +is_workflow_exec() { + local args="$*" + if [[ "$args" == *"service workflow exec"* ]] || [[ "$args" == *"service"*"workflow"*"exec"* ]]; then + return 0 + fi + return 1 +} + +# If this is 'np service workflow exec', pass through to real CLI +if is_workflow_exec "$@"; then + if [ -n "$NP_REAL_CLI" ] && [ -x "$NP_REAL_CLI" ]; then + exec "$NP_REAL_CLI" "$@" + else + # Try to find np in original PATH (before mock was added) + if [ -n "$NP_ORIGINAL_PATH" ]; then + REAL_NP=$(PATH="$NP_ORIGINAL_PATH" which np 2>/dev/null) + if [ -n "$REAL_NP" ] && [ -x "$REAL_NP" ]; then + exec "$REAL_NP" "$@" + fi + fi + echo "Error: Cannot find real np CLI for workflow exec" >&2 + echo "Set NP_REAL_CLI or NP_ORIGINAL_PATH environment variable" >&2 + exit 1 + fi +fi + +# For all other commands, use mock responses +if [ -z "$NP_MOCK_CONFIG" ]; then + echo "Error: NP_MOCK_CONFIG not set" >&2 + exit 1 +fi + +if [ ! -f "$NP_MOCK_CONFIG" ]; then + echo "Error: Mock config file not found: $NP_MOCK_CONFIG" >&2 + exit 1 +fi + +# Build command key (for matching) and full command (for error messages) +CMD_KEY=$(build_command_key "$@") +FULL_CMD=$(build_full_command "$@") + +# Get the step index (np_mocks are defined per step) +STEP_INDEX="${NP_MOCK_STEP_INDEX:-0}" + +# Look up mock configuration for this command (mocks are in .steps[N].np_mocks) +MOCK_RESPONSE_FILE=$(jq -r --arg key "$CMD_KEY" --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks[$key].response_file // empty' "$NP_MOCK_CONFIG") +MOCK_EXIT_CODE=$(jq -r --arg key "$CMD_KEY" --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks[$key].exit_code // 0' "$NP_MOCK_CONFIG") + +if [ -z "$MOCK_RESPONSE_FILE" ]; then + echo "Error: No mock configured for command" >&2 + echo "" >&2 + echo " Full command: $FULL_CMD" >&2 + echo " Match key: $CMD_KEY" >&2 + echo " Step index: $STEP_INDEX" >&2 + echo "" >&2 + echo "Available mocks for this step:" >&2 + jq -r --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks // {} | keys[]' "$NP_MOCK_CONFIG" | sed 's/^/ - /' >&2 + exit 1 +fi + +# Output mock response +MOCK_FILE_PATH="$NP_MOCK_DIR/$MOCK_RESPONSE_FILE" +if [ -f "$MOCK_FILE_PATH" ]; then + cat "$MOCK_FILE_PATH" +else + echo "Error: Mock response file not found: $MOCK_FILE_PATH" >&2 + exit 1 +fi + +exit "${MOCK_EXIT_CODE:-0}" diff --git a/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json b/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json new file mode 100644 index 00000000..9db8b393 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json @@ -0,0 +1,26 @@ +{ + "results": [ + { + "attributes": { + "bucket": { + "name": "assets-bucket" + } + }, + "category": "assets-repository", + "created_at": "2026-01-07T16:28:17.036Z", + "dimensions": {}, + "groups": [], + "id": "4a7be073-92ee-4f66-91be-02d115bc3e7c", + "nrn": "organization=1255165411:account=95118862", + "specification_id": "85e164dc-3149-40c6-b85d-28bddf6e21e8", + "tags": [ + { + "id": "ceb2021b-714e-4fa5-9202-6965c744ffd9", + "key": "bucket.name", + "value": "assets-bucket" + } + ], + "updated_at": "2026-01-07T16:28:17.036Z" + } + ] +} \ No newline at end of file diff --git a/frontend/deployment/tests/integration/mocks/responses/scope_success.json b/frontend/deployment/tests/integration/mocks/responses/scope_success.json new file mode 100644 index 00000000..cf0bd9e2 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/responses/scope_success.json @@ -0,0 +1,6 @@ +{ + "id": "456", + "slug": "production", + "name": "Production", + "application_id": "123" +} diff --git a/frontend/deployment/tests/integration/run_integration_tests.sh b/frontend/deployment/tests/integration/run_integration_tests.sh new file mode 100755 index 00000000..269a208f --- /dev/null +++ b/frontend/deployment/tests/integration/run_integration_tests.sh @@ -0,0 +1,187 @@ +#!/bin/bash +# ============================================================================= +# Integration test runner for shunit2 tests +# +# Usage: +# ./run_integration_tests.sh # Run all integration tests +# ./run_integration_tests.sh test_file.sh # Run specific test file +# ./run_integration_tests.sh --no-localstack # Skip LocalStack management +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +NC='\033[0m' + +# Parse arguments +MANAGE_LOCALSTACK=true +SPECIFIC_TEST="" + +for arg in "$@"; do + case $arg in + --no-localstack) + MANAGE_LOCALSTACK=false + ;; + *.sh) + SPECIFIC_TEST="$arg" + ;; + esac +done + +# Check dependencies +check_dependencies() { + local missing=() + + if ! command -v docker &> /dev/null; then + missing+=("docker") + fi + + if ! command -v docker compose &> /dev/null && ! command -v docker-compose &> /dev/null; then + missing+=("docker-compose") + fi + + if ! command -v jq &> /dev/null; then + missing+=("jq") + fi + + if ! command -v aws &> /dev/null; then + missing+=("aws-cli") + fi + + # Check for shunit2 + if ! command -v shunit2 &> /dev/null && \ + [ ! -f "/usr/local/bin/shunit2" ] && \ + [ ! -f "/usr/share/shunit2/shunit2" ] && \ + [ ! -f "/opt/homebrew/bin/shunit2" ]; then + missing+=("shunit2") + fi + + if [ ${#missing[@]} -gt 0 ]; then + echo -e "${RED}Missing dependencies:${NC}" + for dep in "${missing[@]}"; do + echo " - $dep" + done + echo "" + echo "Install with:" + echo " brew install docker jq awscli shunit2 # macOS" + exit 1 + fi +} + +# Start LocalStack +start_localstack() { + echo -e "${CYAN}Starting LocalStack...${NC}" + docker compose -f "$SCRIPT_DIR/docker-compose.yml" up -d + + echo "Waiting for LocalStack to be ready..." + local max_attempts=30 + local attempt=0 + + while [ $attempt -lt $max_attempts ]; do + if curl -s "http://localhost:4566/_localstack/health" | jq -e '.services.s3 == "running"' > /dev/null 2>&1; then + echo -e "${GREEN}LocalStack is ready${NC}" + return 0 + fi + attempt=$((attempt + 1)) + sleep 2 + echo -n "." + done + + echo "" + echo -e "${RED}LocalStack failed to start${NC}" + return 1 +} + +# Stop LocalStack +stop_localstack() { + echo -e "${CYAN}Stopping LocalStack...${NC}" + docker compose -f "$SCRIPT_DIR/docker-compose.yml" down -v +} + +# Run a single test file +run_test_file() { + local test_file="$1" + local test_name=$(basename "$test_file" .sh) + + echo "" + echo -e "${CYAN}Running: $test_name${NC}" + echo "========================================" + + if bash "$test_file"; then + echo -e "${GREEN}PASSED: $test_name${NC}" + return 0 + else + echo -e "${RED}FAILED: $test_name${NC}" + return 1 + fi +} + +# Main +echo "" +echo "========================================" +echo " Integration Tests (shunit2)" +echo "========================================" +echo "" + +check_dependencies + +# Manage LocalStack if requested +if [ "$MANAGE_LOCALSTACK" = true ]; then + # Ensure LocalStack is stopped on exit + trap stop_localstack EXIT + start_localstack +fi + +# Find and run tests +FAILED=0 +PASSED=0 + +if [ -n "$SPECIFIC_TEST" ]; then + # Run specific test + if [ -f "$SPECIFIC_TEST" ]; then + if run_test_file "$SPECIFIC_TEST"; then + PASSED=$((PASSED + 1)) + else + FAILED=$((FAILED + 1)) + fi + else + echo -e "${RED}Test file not found: $SPECIFIC_TEST${NC}" + exit 1 + fi +else + # Run all test files + for test_file in "$SCRIPT_DIR"/*_test.sh; do + if [ -f "$test_file" ]; then + if run_test_file "$test_file"; then + PASSED=$((PASSED + 1)) + else + FAILED=$((FAILED + 1)) + fi + fi + done +fi + +# Summary +echo "" +echo "========================================" +echo " Summary" +echo "========================================" +echo -e "Passed: ${GREEN}$PASSED${NC}" +echo -e "Failed: ${RED}$FAILED${NC}" + +if [ $FAILED -gt 0 ]; then + echo "" + echo -e "${RED}Some integration tests failed${NC}" + exit 1 +else + echo "" + echo -e "${GREEN}All integration tests passed!${NC}" + exit 0 +fi diff --git a/frontend/deployment/tests/integration/scripts/disable_cloudfront.sh b/frontend/deployment/tests/integration/scripts/disable_cloudfront.sh new file mode 100755 index 00000000..be45014f --- /dev/null +++ b/frontend/deployment/tests/integration/scripts/disable_cloudfront.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Disable a CloudFront distribution by comment +# Usage: disable_cloudfront.sh "Distribution comment" + +set -e + +COMMENT="$1" +MOTO_ENDPOINT="${MOTO_ENDPOINT:-http://localhost:5555}" + +if [ -z "$COMMENT" ]; then + echo "Usage: $0 " + exit 1 +fi + +echo "Looking for CloudFront distribution with comment: $COMMENT" + +# Get distribution ID by comment +DIST_ID=$(aws --endpoint-url="$MOTO_ENDPOINT" cloudfront list-distributions \ + --query "DistributionList.Items[?Comment=='$COMMENT'].Id" \ + --output text 2>/dev/null) + +if [ -z "$DIST_ID" ] || [ "$DIST_ID" = "None" ]; then + echo "No distribution found with comment: $COMMENT" + exit 0 +fi + +echo "Found distribution: $DIST_ID" + +# Get current ETag +ETAG=$(aws --endpoint-url="$MOTO_ENDPOINT" cloudfront get-distribution \ + --id "$DIST_ID" \ + --query "ETag" \ + --output text) + +echo "Current ETag: $ETAG" + +# Get current config and save to temp file +TEMP_CONFIG=$(mktemp) +aws --endpoint-url="$MOTO_ENDPOINT" cloudfront get-distribution-config \ + --id "$DIST_ID" \ + --query "DistributionConfig" \ + > "$TEMP_CONFIG" + +# Set Enabled to false +jq '.Enabled = false' "$TEMP_CONFIG" > "${TEMP_CONFIG}.new" +mv "${TEMP_CONFIG}.new" "$TEMP_CONFIG" + +echo "Disabling distribution..." + +# Update distribution to disabled +aws --endpoint-url="$MOTO_ENDPOINT" cloudfront update-distribution \ + --id "$DIST_ID" \ + --distribution-config "file://$TEMP_CONFIG" \ + --if-match "$ETAG" \ + > /dev/null + +rm -f "$TEMP_CONFIG" + +echo "Distribution $DIST_ID disabled successfully" diff --git a/frontend/deployment/tests/integration/volume/cache/machine.json b/frontend/deployment/tests/integration/volume/cache/machine.json new file mode 100644 index 00000000..a76a7199 --- /dev/null +++ b/frontend/deployment/tests/integration/volume/cache/machine.json @@ -0,0 +1 @@ +{"machine_id": "dkr_6984afd086e1"} \ No newline at end of file diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem b/frontend/deployment/tests/integration/volume/cache/server.test.pem new file mode 100644 index 00000000..d3a80887 --- /dev/null +++ b/frontend/deployment/tests/integration/volume/cache/server.test.pem @@ -0,0 +1,168 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCB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v10Yy+xUGUJ +5lhCLkMaTLTwJUdZ+gQek9QmRkpQgbLevni3/GcV4clXhB4PY9bpYrrWX1Uu6lzG +KAgEJTm4Diup8kyXHAc/DVL17e8vgg8CAwEAAaOB8jCB7zAfBgNVHSMEGDAWgBSg +EQojPpbxB+zirynvgqV/0DCktDAdBgNVHQ4EFgQUU3m/WqorSs9UgOHYm8Cd8rID +ZsswDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQFMAMBAf8wEQYDVR0gBAowCDAG +BgRVHSAAMEMGA1UdHwQ8MDowOKA2oDSGMmh0dHA6Ly9jcmwuY29tb2RvY2EuY29t +L0FBQUNlcnRpZmljYXRlU2VydmljZXMuY3JsMDQGCCsGAQUFBwEBBCgwJjAkBggr +BgEFBQcwAYYYaHR0cDovL29jc3AuY29tb2RvY2EuY29tMA0GCSqGSIb3DQEBDAUA +A4IBAQAYh1HcdCE9nIrgJ7cz0C7M7PDmy14R3iJvm3WOnnL+5Nb+qh+cli3vA0p+ +rvSNb3I8QzvAP+u431yqqcau8vzY7qN7Q/aGNnwU4M309z/+3ri0ivCRlv79Q2R+ +/czSAaF9ffgZGclCKxO/WIu6pKJmBHaIkU4MiRTOok3JMrO66BQavHHxW/BBC5gA +CiIDEOUMsfnNkjcZ7Tvx5Dq2+UUTJnWvu6rvP3t3O9LEApE9GQDTF1w52z97GA1F +zZOFli9d31kWTz9RvdVFGD/tSo7oBmF0Ixa1DVBzJ0RHfxBdiSprhTEUxOipakyA +vGp4z7h/jnZymQyd/teRCBaho1+V +-----END CERTIFICATE----- diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt b/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt new file mode 100644 index 00000000..626af51b --- /dev/null +++ b/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt @@ -0,0 +1,140 @@ +-----BEGIN CERTIFICATE----- +MIIMhDCCCmygAwIBAgIRAO/rUlOy5ZeSoNzstjyL90EwDQYJKoZIhvcNAQEMBQ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C5gA +CiIDEOUMsfnNkjcZ7Tvx5Dq2+UUTJnWvu6rvP3t3O9LEApE9GQDTF1w52z97GA1F +zZOFli9d31kWTz9RvdVFGD/tSo7oBmF0Ixa1DVBzJ0RHfxBdiSprhTEUxOipakyA +vGp4z7h/jnZymQyd/teRCBaho1+V +-----END CERTIFICATE----- \ No newline at end of file diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem.key b/frontend/deployment/tests/integration/volume/cache/server.test.pem.key new file mode 100644 index 00000000..0e505e61 --- /dev/null +++ b/frontend/deployment/tests/integration/volume/cache/server.test.pem.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFbI7+EBqzmxtI +8GDIk5XOwXtw0c7+kmwc2aPXmVQD+LqQ3NwwUSSWkH2psjwkT13ZtLI7KRrj8IQn +RiR58ZW1gldlho7b2dvOwdnl/bSfrzoocceUKcGdnrEJ1O4MVKnL+ffw9GjpyzBp +jcRexzOOxJCRGORBaomiPm3YpLZJLX3ODyiOZSa/spdEpYeaDS9raB1d/iFg03RF +0L7GyzDo51dNI3SRXGoAFbAhPajtzrgCNLtTxUFkodqUgRoid7VrZWa4IlU6CzJK +eWmC6Y/Z120I8y0Fm/xCh+RPJ5yoyJ6GOqM2AWzB+t9Ew1T+KyHbUj1PQ/6y1Eg/ +1Z5UTaWJAgMBAAECggEACBR1i7rNJPY6y25YP7HXwdK4Xfl5aqVoMXnLrsXgWb6w +pJtI3JPyKQumIPih0xHAxaBu9PcI7Flu2XoRgWUBJKDQp01tllxO38aeR79bNnfd +h0PLtOnfJ8nvGa6yVyS16FFzDx2XYMUHeyJytzcrd/MCiayPoBsxxiKerG0dU+ji +OVQwzzAKEsFoMjWxG4O+dnokqYhP6AkZ/w0iCppxlTYubKOBzJjU9vjbDkpi9vQY +IaFZ5BjzJQBYKWDcgRNgQwTlG3Vynr6QpbbY0P+T0dJjKrerS4dfISzBMNsQRbVO +sRHV7LXSd4DYJ9Ci+cBoI5Db/FQV/GA2niSQTU3gUwKBgQDszHLoCUkThFPqwsOk +Gr5gPmtf/q2cjGjuuo9bwVdDF3bVhtbMwAdZAL1qJq+o/ZYMDTLdLfrZUwghmyeD +DbDjC+sJGt8VXe+HHkV9l58RG12xU/CHER5Mqdb4CdBN4hm9Oc5iEs5tCIoUCoAD +Z8Ol6lbHeEGyS9t0gse8/qkxKwKBgQDVbsIYiK7LbiQOQfWkeKJYUFUIOyMWNiuD +Iu/bBNx0ufLj6GEbWVcnSCF+MBVjqjN7fcCVFWx0DvMXAWtojTelDBtTcW2sY1V6 +EwwT6TxHGm+hhK1SD85Vj759HcheaqJFR9GKgH3+ayBzB4U+EPxDh65SyZCoFFN+ +BEPaG2liGwKBgDIHR8eKHqxG6svQdjD3jX0b8ueHEPrgF1NIiv0hreP40xxtrnf3 +ohXFuD7zCW20lbzaFQLxseu0RSWEeCaR/+sYG4IC8Vq8S9zKInhUTkD4/SR3zXtb +vIEJ6Obie+XYfQOjcNz7iC00/qcZSM5vX8Bv8AGYgJjAug61iql9TBWdAoGBALHX +KEPpIDzB+aknrNbu7ddImJHTNNk9KeSLJ/EHi+p3RrxA1SlEuCozIDVVO31gRKWR +kvamc0gBbOyuciEcClGsVNiimxAZdQ/S7y1oGqHklT+wnfrS0Mrai48VUe/aSnwP +67nMdy+Xc+JlUdD1tj1OwSKacb6bsTY/t4n1bUohAoGAO7C/PpSoku5ptn2WkfV8 +o6zNPlVNBlaIWmB5puRUyOrIm5MEnB4JZ4euG8TNx6cxKPqet5OLB9SHESb7rWLx +n1KohBwHn1Eccb2m1axCNw09sIrqAWueITWm39U3nDwkLNAwTWRX+BgoGcxDqV+X +UxHXh8CQGDKSwopZsJLZiUw= +-----END PRIVATE KEY----- \ No newline at end of file diff --git a/frontend/deployment/tests/integration_test_utils.sh b/frontend/deployment/tests/integration_test_utils.sh new file mode 100755 index 00000000..440d3018 --- /dev/null +++ b/frontend/deployment/tests/integration_test_utils.sh @@ -0,0 +1,492 @@ +#!/bin/bash +# ============================================================================= +# Integration test utilities for shunit2 +# +# Provides helper functions for: +# - LocalStack management +# - AWS resource assertions +# - Test configuration loading +# - Workflow execution +# +# Usage: +# export INTEGRATION_TEST_DIR="/path/to/integration/test/dir" +# source "/path/to/tests/integration_test_utils.sh" +# ============================================================================= + +# Validate INTEGRATION_TEST_DIR is set +if [ -z "${INTEGRATION_TEST_DIR:-}" ]; then + echo "Error: INTEGRATION_TEST_DIR must be set before sourcing integration_test_utils.sh" + exit 1 +fi + +export INTEGRATION_TEST_DIR +export PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" + +# LocalStack configuration (S3, Route53, DynamoDB, IAM, STS, ACM) +export LOCALSTACK_ENDPOINT="${LOCALSTACK_ENDPOINT:-http://localhost:4566}" +# Moto configuration (CloudFront) +export MOTO_ENDPOINT="${MOTO_ENDPOINT:-http://localhost:5555}" +export AWS_ENDPOINT_URL="$LOCALSTACK_ENDPOINT" +export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-test}" +export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-test}" +export AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-east-1}" +export AWS_PAGER="" + +# Save original PATH before adding mock +export NP_ORIGINAL_PATH="$PATH" + +# Add mock np to PATH +export PATH="$INTEGRATION_TEST_DIR/mocks:$PATH" + +# ============================================================================= +# LocalStack Management +# ============================================================================= + +localstack_start() { + echo "Starting LocalStack..." + docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" up -d + localstack_wait_ready +} + +localstack_stop() { + echo "Stopping LocalStack..." + docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" down -v +} + +localstack_wait_ready() { + echo "Waiting for LocalStack to be ready..." + local max_attempts=30 + local attempt=0 + + while [ $attempt -lt $max_attempts ]; do + if curl -s "$LOCALSTACK_ENDPOINT/_localstack/health" | jq -e '.services.s3 == "running"' > /dev/null 2>&1; then + echo "LocalStack is ready" + return 0 + fi + attempt=$((attempt + 1)) + sleep 2 + done + + echo "LocalStack failed to start" + return 1 +} + +localstack_reset() { + echo "Resetting LocalStack state..." + # Reset by restarting the container + docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" restart localstack + localstack_wait_ready +} + +# ============================================================================= +# Test Configuration +# ============================================================================= + +load_test_config() { + local config_file="$1" + + if [ ! -f "$config_file" ]; then + echo "Error: Config file not found: $config_file" + return 1 + fi + + export CURRENT_TEST_CONFIG="$config_file" + export CURRENT_TEST_NAME=$(jq -r '.name' "$config_file") + export CURRENT_TEST_STEPS=$(jq -r '.steps | length' "$config_file") + + # Setup prerequisites + setup_prerequisites +} + +# ============================================================================= +# Prerequisites Setup (S3 buckets, Route53 zones, etc.) +# ============================================================================= + +setup_prerequisites() { + echo "" + echo "==========================================" + echo "Setting up prerequisites" + echo "==========================================" + + # Get setup commands array + local setup_commands=$(jq -r '.setup // []' "$CURRENT_TEST_CONFIG") + local cmd_count=$(echo "$setup_commands" | jq -r 'length') + + if [ "$cmd_count" -eq 0 ]; then + echo "No setup commands defined" + echo "" + return 0 + fi + + echo "Running $cmd_count setup command(s)..." + echo "" + + for i in $(seq 0 $((cmd_count - 1))); do + local cmd=$(echo "$setup_commands" | jq -r ".[$i]") + echo " $ $cmd" + + # Execute the command + eval "$cmd" /dev/null || true + done + + echo "" + echo "Prerequisites setup complete" + echo "" +} + +get_step_config() { + local step_index="$1" + jq -r ".steps[$step_index]" "$CURRENT_TEST_CONFIG" +} + +get_step_env() { + local step_index="$1" + jq -r ".steps[$step_index].env // {}" "$CURRENT_TEST_CONFIG" +} + +get_step_workflow() { + local step_index="$1" + jq -r ".steps[$step_index].workflow" "$CURRENT_TEST_CONFIG" +} + +get_step_assertions() { + local step_index="$1" + jq -r ".steps[$step_index].assertions // []" "$CURRENT_TEST_CONFIG" +} + +# ============================================================================= +# Workflow Execution +# ============================================================================= + +setup_test_environment() { + local step_index="$1" + + # Set mock configuration + export NP_MOCK_CONFIG="$CURRENT_TEST_CONFIG" + export NP_MOCK_DIR="$INTEGRATION_TEST_DIR/mocks/responses" + + # Set AWS endpoint for LocalStack + export AWS_ENDPOINT_URL="$LOCALSTACK_ENDPOINT" + + # Load CONTEXT from file if specified + local context_file=$(jq -r ".steps[$step_index].context_file // .context_file // empty" "$CURRENT_TEST_CONFIG") + if [ -n "$context_file" ]; then + # Resolve relative paths from the tests directory + if [[ "$context_file" != /* ]]; then + context_file="$PROJECT_DIR/tests/$context_file" + fi + if [ -f "$context_file" ]; then + echo " Loading CONTEXT from: $context_file" + export CONTEXT=$(cat "$context_file") + else + echo " Warning: Context file not found: $context_file" + fi + fi + + # Apply context_overrides - allows dynamic values using shell commands + local overrides=$(jq -r ".steps[$step_index].context_overrides // .context_overrides // {}" "$CURRENT_TEST_CONFIG") + if [ "$overrides" != "{}" ] && [ -n "$CONTEXT" ]; then + echo " Applying context overrides..." + local override_keys=$(echo "$overrides" | jq -r 'keys[]') + for key in $override_keys; do + local value_expr=$(echo "$overrides" | jq -r --arg k "$key" '.[$k]') + # Evaluate shell commands in the value (e.g., $(aws ...)) + local value=$(eval "echo \"$value_expr\"") + echo " $key = $value" + # Use jq to set nested keys (supports dot notation like "providers.cloud-providers.networking.hosted_public_zone_id") + CONTEXT=$(echo "$CONTEXT" | jq --arg k "$key" --arg v "$value" 'setpath($k | split("."); $v)') + done + export CONTEXT + fi + + # Load step-specific environment variables (with variable expansion) + local env_json=$(get_step_env "$step_index") + while IFS="=" read -r key value; do + if [ -n "$key" ]; then + # Expand environment variables in the value + local expanded_value=$(eval "echo \"$value\"") + export "$key=$expanded_value" + fi + done < <(echo "$env_json" | jq -r 'to_entries[] | "\(.key)=\(.value)"') +} + +run_workflow_step() { + local step_index="$1" + local workflow + workflow=$(get_step_workflow "$step_index") + # Expand environment variables in workflow path + workflow=$(eval "echo \"$workflow\"") + local step_name + step_name=$(jq -r ".steps[$step_index].name" "$CURRENT_TEST_CONFIG") + + echo "Running step: $step_name" + echo "Workflow: $workflow" + echo "" + + # Update mock config to point to this step's mocks + export NP_MOCK_STEP_INDEX="$step_index" + + # Execute the workflow using real np CLI + # The mock will pass through 'np service workflow exec' to the real CLI + np service workflow exec --workflow "$workflow" +} + +# ============================================================================= +# AWS Resource Assertions (against LocalStack) +# ============================================================================= + +aws_local() { + aws --endpoint-url="$LOCALSTACK_ENDPOINT" --no-cli-pager --no-cli-auto-prompt "$@" +} + +assert_s3_bucket_exists() { + local bucket="$1" + + echo -n " Checking S3 bucket '$bucket' exists... " + if aws_local s3api head-bucket --bucket "$bucket" 2>/dev/null; then + echo "✓" + return 0 + else + echo "✗" + fail "S3 bucket does not exist: $bucket" + return 1 + fi +} + +assert_s3_bucket_not_exists() { + local bucket="$1" + + echo -n " Checking S3 bucket '$bucket' does not exist... " + if aws_local s3api head-bucket --bucket "$bucket" 2>/dev/null; then + echo "✗" + fail "S3 bucket should not exist: $bucket" + return 1 + else + echo "✓" + return 0 + fi +} + +assert_cloudfront_distribution_exists() { + local comment="$1" + + echo -n " Checking CloudFront distribution with comment '$comment' exists... " + # CloudFront uses Moto endpoint, not LocalStack + local distribution + distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ + --query "DistributionList.Items[?Comment=='$comment'].Id" \ + --output text 2>/dev/null) + + if [ -n "$distribution" ] && [ "$distribution" != "None" ]; then + echo "✓" + return 0 + else + echo "✗" + fail "CloudFront distribution does not exist with comment: $comment" + return 1 + fi +} + +assert_cloudfront_distribution_not_exists() { + local comment="$1" + + echo -n " Checking CloudFront distribution with comment '$comment' does not exist... " + # CloudFront uses Moto endpoint, not LocalStack + local distribution + distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ + --query "DistributionList.Items[?Comment=='$comment'].Id" \ + --output text 2>/dev/null) + + if [ -z "$distribution" ] || [ "$distribution" == "None" ]; then + echo "✓" + return 0 + else + echo "✗" + fail "CloudFront distribution should not exist with comment: $comment" + return 1 + fi +} + +assert_route53_record_exists() { + local record_name="$1" + local record_type="$2" + + echo -n " Checking Route53 record '$record_name' ($record_type) exists... " + + # Ensure record name ends with a dot + [[ "$record_name" != *. ]] && record_name="$record_name." + + # Get the first hosted zone + local zone_id + zone_id=$(aws_local route53 list-hosted-zones \ + --query "HostedZones[0].Id" \ + --output text 2>/dev/null | sed 's|/hostedzone/||') + + if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then + echo "✗" + fail "No Route53 hosted zones found" + return 1 + fi + + local record + record=$(aws_local route53 list-resource-record-sets \ + --hosted-zone-id "$zone_id" \ + --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ + --output text 2>/dev/null) + + if [ -n "$record" ] && [ "$record" != "None" ]; then + echo "✓" + return 0 + else + echo "✗" + fail "Route53 record does not exist: $record_name ($record_type) in zone $zone_id" + return 1 + fi +} + +assert_route53_record_not_exists() { + local record_name="$1" + local record_type="$2" + + echo -n " Checking Route53 record '$record_name' ($record_type) does not exist... " + + # Ensure record name ends with a dot + [[ "$record_name" != *. ]] && record_name="$record_name." + + # Get the first hosted zone + local zone_id + zone_id=$(aws_local route53 list-hosted-zones \ + --query "HostedZones[0].Id" \ + --output text 2>/dev/null | sed 's|/hostedzone/||') + + if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then + # No zones means no records, so assertion passes + echo "✓" + return 0 + fi + + local record + record=$(aws_local route53 list-resource-record-sets \ + --hosted-zone-id "$zone_id" \ + --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ + --output text 2>/dev/null) + + if [ -z "$record" ] || [ "$record" == "None" ]; then + echo "✓" + return 0 + else + echo "✗" + fail "Route53 record should not exist: $record_name ($record_type) in zone $zone_id" + return 1 + fi +} + +# ============================================================================= +# Assertion Runner +# ============================================================================= + +run_assertions() { + local step_index="$1" + local assertions=$(get_step_assertions "$step_index") + local assertion_count=$(echo "$assertions" | jq -r 'length') + + echo "Running $assertion_count assertions..." + + for i in $(seq 0 $((assertion_count - 1))); do + local assertion=$(echo "$assertions" | jq -r ".[$i]") + local type=$(echo "$assertion" | jq -r '.type') + + case "$type" in + s3_bucket_exists) + local bucket=$(echo "$assertion" | jq -r '.bucket') + assert_s3_bucket_exists "$bucket" + ;; + s3_bucket_not_exists) + local bucket=$(echo "$assertion" | jq -r '.bucket') + assert_s3_bucket_not_exists "$bucket" + ;; + cloudfront_distribution_exists) + local comment=$(echo "$assertion" | jq -r '.comment') + assert_cloudfront_distribution_exists "$comment" + ;; + cloudfront_distribution_not_exists) + local comment=$(echo "$assertion" | jq -r '.comment') + assert_cloudfront_distribution_not_exists "$comment" + ;; + route53_record_exists) + local name=$(echo "$assertion" | jq -r '.name') + local record_type=$(echo "$assertion" | jq -r '.record_type') + assert_route53_record_exists "$name" "$record_type" + ;; + route53_record_not_exists) + local name=$(echo "$assertion" | jq -r '.name') + local record_type=$(echo "$assertion" | jq -r '.record_type') + assert_route53_record_not_exists "$name" "$record_type" + ;; + *) + fail "Unknown assertion type: $type" + ;; + esac + done +} + +# ============================================================================= +# Full Test Step Execution +# ============================================================================= + +run_before_commands() { + local step_index="$1" + local before_commands + before_commands=$(jq -r ".steps[$step_index].before // []" "$CURRENT_TEST_CONFIG") + local cmd_count + cmd_count=$(echo "$before_commands" | jq -r 'length') + + if [ "$cmd_count" -eq 0 ]; then + return 0 + fi + + echo "Running $cmd_count before command(s)..." + echo "" + + for i in $(seq 0 $((cmd_count - 1))); do + local cmd + cmd=$(echo "$before_commands" | jq -r ".[$i]") + echo " $ $cmd" + eval "$cmd" || true + done + + echo "" +} + +execute_test_step() { + local step_index="$1" + local step_name + step_name=$(jq -r ".steps[$step_index].name" "$CURRENT_TEST_CONFIG") + + echo "" + echo "==========================================" + echo "Step $((step_index + 1)): $step_name" + echo "==========================================" + + # Setup environment for this step + setup_test_environment "$step_index" + + # Run before commands (if any) + run_before_commands "$step_index" + + # Run the workflow + run_workflow_step "$step_index" + + # Run assertions + run_assertions "$step_index" + + echo "Step $step_name completed successfully" +} + +execute_all_steps() { + local step_count=$(jq -r '.steps | length' "$CURRENT_TEST_CONFIG") + + for i in $(seq 0 $((step_count - 1))); do + execute_test_step "$i" + done +} diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats new file mode 100644 index 00000000..ca816075 --- /dev/null +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -0,0 +1,226 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for provider/aws/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/provider/aws/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/provider/aws/setup" + RESOURCES_DIR="$PROJECT_DIR/tests/resources" + + # Load shared test utilities + source "$PROJECT_DIR/tests/test_utils.bash" + + # Initialize required environment variables + export AWS_REGION="us-east-1" + export TOFU_PROVIDER_BUCKET="my-terraform-state-bucket" + export TOFU_LOCK_TABLE="terraform-locks" + + # Initialize TOFU_VARIABLES with existing keys to verify script merges (not replaces) + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export TOFU_INIT_VARIABLES="" + export MODULES_TO_USE="" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_aws_setup() { + source "$SCRIPT_PATH" +} + +# ============================================================================= +# Test: Required environment variables +# ============================================================================= +@test "fails when AWS_REGION is not set" { + unset AWS_REGION + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "AWS_REGION is not set" +} + +@test "fails when TOFU_PROVIDER_BUCKET is not set" { + unset TOFU_PROVIDER_BUCKET + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "TOFU_PROVIDER_BUCKET is not set" +} + +@test "fails when TOFU_LOCK_TABLE is not set" { + unset TOFU_LOCK_TABLE + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "TOFU_LOCK_TABLE is not set" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "TOFU_VARIABLES matches expected structure on success" { + run_aws_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "aws_provider": { + "region": "us-east-1", + "state_bucket": "my-terraform-state-bucket", + "lock_table": "terraform-locks" + }, + "provider_resource_tags_json": {} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +@test "TOFU_VARIABLES includes custom resource tags" { + export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' + + run_aws_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "aws_provider": { + "region": "us-east-1", + "state_bucket": "my-terraform-state-bucket", + "lock_table": "terraform-locks" + }, + "provider_resource_tags_json": {"Environment": "production", "Team": "platform"} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +@test "TOFU_VARIABLES uses different region" { + export AWS_REGION="eu-west-1" + + run_aws_setup + + local region=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.region') + assert_equal "$region" "eu-west-1" +} + +# ============================================================================= +# Test: TOFU_INIT_VARIABLES - backend configuration +# ============================================================================= +@test "TOFU_INIT_VARIABLES includes bucket backend config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=bucket=my-terraform-state-bucket" +} + +@test "TOFU_INIT_VARIABLES includes region backend config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=region=us-east-1" +} + +@test "TOFU_INIT_VARIABLES includes dynamodb_table backend config" { + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=dynamodb_table=terraform-locks" +} + +@test "TOFU_INIT_VARIABLES appends to existing variables" { + export TOFU_INIT_VARIABLES="-var=existing=value" + + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-var=existing=value" + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=bucket=my-terraform-state-bucket" +} + +# ============================================================================= +# Test: LocalStack/AWS_ENDPOINT_URL configuration +# ============================================================================= +@test "adds LocalStack backend config when AWS_ENDPOINT_URL is set" { + export AWS_ENDPOINT_URL="http://localhost:4566" + + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=force_path_style=true" + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_credentials_validation=true" + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_metadata_api_check=true" + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_region_validation=true" +} + +@test "includes endpoint config with LocalStack URL" { + export AWS_ENDPOINT_URL="http://localhost:4566" + + run_aws_setup + + assert_contains "$TOFU_INIT_VARIABLES" '-backend-config=endpoints={s3="http://localhost:4566",dynamodb="http://localhost:4566"}' +} + +@test "does not add LocalStack config when AWS_ENDPOINT_URL is not set" { + unset AWS_ENDPOINT_URL + + run_aws_setup + + # Should not contain LocalStack-specific configs + if [[ "$TOFU_INIT_VARIABLES" == *"force_path_style"* ]]; then + echo "TOFU_INIT_VARIABLES should not contain force_path_style when AWS_ENDPOINT_URL is not set" + echo "Actual: $TOFU_INIT_VARIABLES" + return 1 + fi +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "adds module to MODULES_TO_USE when empty" { + run_aws_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/provider/aws/modules" +} + +@test "appends module to existing MODULES_TO_USE" { + export MODULES_TO_USE="existing/module" + + run_aws_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/provider/aws/modules" +} + +@test "preserves multiple existing modules in MODULES_TO_USE" { + export MODULES_TO_USE="first/module,second/module" + + run_aws_setup + + assert_equal "$MODULES_TO_USE" "first/module,second/module,$PROJECT_DIR/provider/aws/modules" +} + +# ============================================================================= +# Test: Default values +# ============================================================================= +@test "uses empty object for RESOURCE_TAGS_JSON when not set" { + unset RESOURCE_TAGS_JSON + + run_aws_setup + + local tags=$(echo "$TOFU_VARIABLES" | jq -r '.provider_resource_tags_json') + assert_equal "$tags" "{}" +} diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json index 8fd527a1..2d5c6f4a 100644 --- a/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/success.json @@ -3,7 +3,7 @@ { "attributes": { "bucket": { - "name": "assets-kwik-e-mart-main" + "name": "assets-bucket" } }, "category": "assets-repository", @@ -17,7 +17,7 @@ { "id": "ceb2021b-714e-4fa5-9202-6965c744ffd9", "key": "bucket.name", - "value": "assets-kwik-e-mart-main" + "value": "assets-bucket" } ], "updated_at": "2026-01-07T16:28:17.036Z" diff --git a/frontend/deployment/workflows/delete.yaml b/frontend/deployment/workflows/delete.yaml index 41962566..05f33bcb 100644 --- a/frontend/deployment/workflows/delete.yaml +++ b/frontend/deployment/workflows/delete.yaml @@ -1,8 +1,26 @@ provider_categories: - cloud-providers steps: + - name: build_context + type: script + file: "$SERVICE_PATH/deployment/build_context" + output: + - name: TOFU_VARIABLES + type: environment + - name: setup_provider_layer + type: script + file: "$SERVICE_PATH/deployment/provider/$TOFU_PROVIDER/setup" + - name: setup_network_layer + type: script + file: "$SERVICE_PATH/deployment/network/$NETWORK_LAYER/setup" + - name: setup_distribution_layer + type: script + file: "$SERVICE_PATH/deployment/distribution/$DISTRIBUTION_LAYER/setup" + - name: build_modules + type: script + file: "$SERVICE_PATH/deployment/compose_modules" - name: tofu type: script - file: "$SERVICE_PATH/deployment/module/provision" + file: "$SERVICE_PATH/deployment/do_tofu" configuration: - ACTION: "destroy" \ No newline at end of file + ACTION: "destroy" diff --git a/frontend/deployment/workflows/initial.yaml b/frontend/deployment/workflows/initial.yaml index 28c95540..bca07c63 100644 --- a/frontend/deployment/workflows/initial.yaml +++ b/frontend/deployment/workflows/initial.yaml @@ -16,9 +16,6 @@ steps: - name: setup_distribution_layer type: script file: "$SERVICE_PATH/deployment/distribution/$DISTRIBUTION_LAYER/setup" -# - name: setup_data_layer -# type: script -# file: "$SERVICE_PATH/deployment/data/$DATA_LAYER/setup" - name: build_modules type: script file: "$SERVICE_PATH/deployment/compose_modules" diff --git a/run_all_tests.sh b/run_all_tests.sh index 9ad5686e..3559547d 100755 --- a/run_all_tests.sh +++ b/run_all_tests.sh @@ -1,10 +1,12 @@ #!/bin/bash # ============================================================================= -# Test runner for all tests (BATS + OpenTofu) +# Test runner for all tests (BATS + OpenTofu + Integration) # # Usage: -# ./run_all_tests.sh # Run all tests -# ./run_all_tests.sh frontend # Run tests for frontend module only +# ./run_all_tests.sh # Run all tests +# ./run_all_tests.sh frontend # Run tests for frontend module only +# ./run_all_tests.sh --skip-integration # Skip integration tests +# ./run_all_tests.sh --only-integration # Run only integration tests # ============================================================================= set -e @@ -18,7 +20,24 @@ GREEN='\033[0;32m' CYAN='\033[0;36m' NC='\033[0m' -MODULE="${1:-}" +# Parse arguments +MODULE="" +SKIP_INTEGRATION=false +ONLY_INTEGRATION=false + +for arg in "$@"; do + case $arg in + --skip-integration) + SKIP_INTEGRATION=true + ;; + --only-integration) + ONLY_INTEGRATION=true + ;; + *) + MODULE="$arg" + ;; + esac +done echo "" echo "========================================" @@ -29,44 +48,91 @@ echo "" # Track failures BATS_FAILED=0 TOFU_FAILED=0 +INTEGRATION_FAILED=0 -# Run BATS tests -echo -e "${CYAN}[BATS]${NC} Running bash tests..." -echo "" -if ./run_tests.sh $MODULE; then - echo -e "${GREEN}[BATS] All bash tests passed${NC}" -else - BATS_FAILED=1 - echo -e "${RED}[BATS] Some bash tests failed${NC}" +# Run unit tests unless only-integration is specified +if [ "$ONLY_INTEGRATION" = false ]; then + # Run BATS tests + echo -e "${CYAN}[BATS]${NC} Running bash tests..." + echo "" + if ./run_tests.sh $MODULE; then + echo -e "${GREEN}[BATS] All bash tests passed${NC}" + else + BATS_FAILED=1 + echo -e "${RED}[BATS] Some bash tests failed${NC}" + fi + + echo "" + echo "----------------------------------------" + echo "" + + # Run OpenTofu tests + echo -e "${CYAN}[TOFU]${NC} Running OpenTofu tests..." + echo "" + if ./run_tofu_tests.sh $MODULE; then + echo -e "${GREEN}[TOFU] All OpenTofu tests passed${NC}" + else + TOFU_FAILED=1 + echo -e "${RED}[TOFU] Some OpenTofu tests failed${NC}" + fi fi -echo "" -echo "----------------------------------------" -echo "" +# Run integration tests unless skip-integration is specified +if [ "$SKIP_INTEGRATION" = false ]; then + echo "" + echo "----------------------------------------" + echo "" -# Run OpenTofu tests -echo -e "${CYAN}[TOFU]${NC} Running OpenTofu tests..." -echo "" -if ./run_tofu_tests.sh $MODULE; then - echo -e "${GREEN}[TOFU] All OpenTofu tests passed${NC}" -else - TOFU_FAILED=1 - echo -e "${RED}[TOFU] Some OpenTofu tests failed${NC}" + echo -e "${CYAN}[INTEGRATION]${NC} Running integration tests..." + echo "" + if ./run_integration_tests.sh $MODULE; then + echo -e "${GREEN}[INTEGRATION] All integration tests passed${NC}" + else + INTEGRATION_FAILED=1 + echo -e "${RED}[INTEGRATION] Some integration tests failed${NC}" + fi fi +# Summary echo "" echo "========================================" echo " Summary" echo "========================================" echo "" -if [ $BATS_FAILED -eq 0 ] && [ $TOFU_FAILED -eq 0 ]; then +ALL_PASSED=true + +if [ "$ONLY_INTEGRATION" = false ]; then + if [ $BATS_FAILED -eq 0 ]; then + echo -e "${GREEN}BATS tests: PASSED${NC}" + else + echo -e "${RED}BATS tests: FAILED${NC}" + ALL_PASSED=false + fi + + if [ $TOFU_FAILED -eq 0 ]; then + echo -e "${GREEN}OpenTofu tests: PASSED${NC}" + else + echo -e "${RED}OpenTofu tests: FAILED${NC}" + ALL_PASSED=false + fi +fi + +if [ "$SKIP_INTEGRATION" = false ]; then + if [ $INTEGRATION_FAILED -eq 0 ]; then + echo -e "${GREEN}Integration tests: PASSED${NC}" + else + echo -e "${RED}Integration tests: FAILED${NC}" + ALL_PASSED=false + fi +fi + +echo "" + +if [ "$ALL_PASSED" = true ]; then echo -e "${GREEN}All tests passed!${NC}" exit 0 else - [ $BATS_FAILED -eq 1 ] && echo -e "${RED}BATS tests: FAILED${NC}" - [ $BATS_FAILED -eq 0 ] && echo -e "${GREEN}BATS tests: PASSED${NC}" - [ $TOFU_FAILED -eq 1 ] && echo -e "${RED}OpenTofu tests: FAILED${NC}" - [ $TOFU_FAILED -eq 0 ] && echo -e "${GREEN}OpenTofu tests: PASSED${NC}" + echo -e "${RED}Some tests failed${NC}" exit 1 fi diff --git a/run_integration_tests.sh b/run_integration_tests.sh new file mode 100755 index 00000000..b91a305f --- /dev/null +++ b/run_integration_tests.sh @@ -0,0 +1,102 @@ +#!/bin/bash +# ============================================================================= +# Integration test runner for all modules +# +# Usage: +# ./run_integration_tests.sh # Run all integration tests +# ./run_integration_tests.sh frontend # Run tests for frontend module +# ./run_integration_tests.sh --no-localstack # Skip LocalStack management +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +NC='\033[0m' + +# Parse arguments +EXTRA_ARGS="" +MODULE="" + +for arg in "$@"; do + case $arg in + --no-localstack) + EXTRA_ARGS="$EXTRA_ARGS --no-localstack" + ;; + *) + MODULE="$arg" + ;; + esac +done + +# Find all integration test directories +find_integration_dirs() { + find . -type d -name "integration" -path "*/deployment/tests/*" 2>/dev/null | sort +} + +# Get module name from test path +get_module_name() { + local path="$1" + echo "$path" | sed 's|^\./||' | cut -d'/' -f1 +} + +# Run integration tests for a specific directory +run_integration_in_dir() { + local test_dir="$1" + local module_name=$(get_module_name "$test_dir") + + if [ ! -f "$test_dir/run_integration_tests.sh" ]; then + return 0 + fi + + echo -e "${CYAN}[$module_name]${NC} Running integration tests in $test_dir" + echo "" + + ( + cd "$test_dir" + ./run_integration_tests.sh $EXTRA_ARGS + ) + + echo "" +} + +echo "" +echo "========================================" +echo " Integration Tests" +echo "========================================" +echo "" + +if [ -n "$MODULE" ]; then + # Run tests for specific module + if [ -d "$MODULE/deployment/tests/integration" ]; then + run_integration_in_dir "$MODULE/deployment/tests/integration" + else + echo -e "${RED}Integration test directory not found for: $MODULE${NC}" + echo "" + echo "Available modules with integration tests:" + for dir in $(find_integration_dirs); do + echo " - $(get_module_name "$dir")" + done + exit 1 + fi +else + # Run all integration tests + integration_dirs=$(find_integration_dirs) + + if [ -z "$integration_dirs" ]; then + echo -e "${YELLOW}No integration test directories found${NC}" + exit 0 + fi + + for test_dir in $integration_dirs; do + run_integration_in_dir "$test_dir" + done +fi + +echo -e "${GREEN}All integration tests passed!${NC}" From 4a21284a253bb01d0389e98a3becba6b66cbab57 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 13 Jan 2026 13:31:58 -0300 Subject: [PATCH 07/40] Add makefile for unit and tofu tests --- Makefile | 3 + .../deployment/tests/build_context_test.bats | 5 +- .../distribution/cloudfront/setup_test.bats | 4 +- .../tests/network/route53/setup_test.bats | 3 +- .../tests/provider/aws/setup_test.bats | 6 +- frontend/deployment/tests/test_utils.bash | 90 ------------ run_tests.sh | 115 ---------------- run_tofu_tests.sh | 129 ------------------ testing/assertions.sh | 2 +- testing/run_bats_tests.sh | 2 +- 10 files changed, 17 insertions(+), 342 deletions(-) delete mode 100644 frontend/deployment/tests/test_utils.bash delete mode 100755 run_tests.sh delete mode 100755 run_tofu_tests.sh diff --git a/Makefile b/Makefile index e091370b..ee5e039c 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,9 @@ ifdef MODULE else @./testing/run_integration_tests.sh $(if $(VERBOSE),-v) endif +# Run integration tests (placeholder) +test-integration: + @./run_integration_tests.sh # Help help: diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/build_context_test.bats index 84d0c3ac..210c29b7 100644 --- a/frontend/deployment/tests/build_context_test.bats +++ b/frontend/deployment/tests/build_context_test.bats @@ -20,9 +20,10 @@ setup() { # Get the directory of the test file TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/.." && pwd)" + PROJECT_ROOT="$(cd "$TEST_DIR/../../.." && pwd)" # Load shared test utilities - source "$TEST_DIR/test_utils.bash" + source "$PROJECT_ROOT/testing/assertions.sh" CONTEXT=$(cat "$TEST_DIR/resources/context.json") SERVICE_PATH="$PROJECT_DIR" @@ -145,4 +146,4 @@ run_build_context() { }' assert_json_equal "$RESOURCE_TAGS_JSON" "$expected" "RESOURCE_TAGS_JSON" -} \ No newline at end of file +} diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index 3e9c8d4f..7855cb24 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -14,12 +14,13 @@ setup() { TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" SCRIPT_PATH="$PROJECT_DIR/distribution/cloudfront/setup" RESOURCES_DIR="$PROJECT_DIR/tests/resources" MOCKS_DIR="$RESOURCES_DIR/np_mocks" # Load shared test utilities - source "$PROJECT_DIR/tests/test_utils.bash" + source "$PROJECT_ROOT/testing/assertions.sh" # Add mock np to PATH (must be first) export PATH="$MOCKS_DIR:$PATH" @@ -174,4 +175,3 @@ set_np_mock() { assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } - diff --git a/frontend/deployment/tests/network/route53/setup_test.bats b/frontend/deployment/tests/network/route53/setup_test.bats index 70c4fcd8..b8e46f15 100644 --- a/frontend/deployment/tests/network/route53/setup_test.bats +++ b/frontend/deployment/tests/network/route53/setup_test.bats @@ -14,12 +14,13 @@ setup() { TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" SCRIPT_PATH="$PROJECT_DIR/network/route53/setup" RESOURCES_DIR="$PROJECT_DIR/tests/resources" MOCKS_DIR="$RESOURCES_DIR/aws_mocks" # Load shared test utilities - source "$PROJECT_DIR/tests/test_utils.bash" + source "$PROJECT_ROOT/testing/assertions.sh" # Add mock aws to PATH (must be first) export PATH="$MOCKS_DIR:$PATH" diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats index ca816075..2cd3ff28 100644 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -14,11 +14,12 @@ setup() { TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" SCRIPT_PATH="$PROJECT_DIR/provider/aws/setup" RESOURCES_DIR="$PROJECT_DIR/tests/resources" # Load shared test utilities - source "$PROJECT_DIR/tests/test_utils.bash" + source "$PROJECT_ROOT/testing/assertions.sh" # Initialize required environment variables export AWS_REGION="us-east-1" @@ -34,6 +35,9 @@ setup() { export TOFU_INIT_VARIABLES="" export MODULES_TO_USE="" + + # Clear LocalStack endpoint + unset AWS_ENDPOINT_URL } # ============================================================================= diff --git a/frontend/deployment/tests/test_utils.bash b/frontend/deployment/tests/test_utils.bash deleted file mode 100644 index 46102a38..00000000 --- a/frontend/deployment/tests/test_utils.bash +++ /dev/null @@ -1,90 +0,0 @@ -# ============================================================================= -# Shared test utilities for BATS tests -# -# Usage: Add this line at the top of your .bats file's setup() function: -# source "$TEST_DIR/test_utils.bash" -# # or if in a subdirectory: -# source "$TEST_DIR/../test_utils.bash" -# ============================================================================= - -# ============================================================================= -# Assertion functions -# ============================================================================= - -assert_equal() { - local actual="$1" - local expected="$2" - if [ "$actual" != "$expected" ]; then - echo "Expected: '$expected'" - echo "Actual: '$actual'" - return 1 - fi -} - -assert_contains() { - local haystack="$1" - local needle="$2" - if [[ "$haystack" != *"$needle"* ]]; then - echo "Expected string to contain: '$needle'" - echo "Actual: '$haystack'" - return 1 - fi -} - -assert_not_empty() { - local value="$1" - local name="${2:-value}" - if [ -z "$value" ]; then - echo "Expected $name to be non-empty, but it was empty" - return 1 - fi -} - -assert_empty() { - local value="$1" - local name="${2:-value}" - if [ -n "$value" ]; then - echo "Expected $name to be empty" - echo "Actual: '$value'" - return 1 - fi -} - -assert_directory_exists() { - local dir="$1" - if [ ! -d "$dir" ]; then - echo "Expected directory to exist: '$dir'" - return 1 - fi -} - -assert_file_exists() { - local file="$1" - if [ ! -f "$file" ]; then - echo "Expected file to exist: '$file'" - return 1 - fi -} - -assert_json_equal() { - local actual="$1" - local expected="$2" - local name="${3:-JSON}" - - local actual_sorted=$(echo "$actual" | jq -S .) - local expected_sorted=$(echo "$expected" | jq -S .) - - if [ "$actual_sorted" != "$expected_sorted" ]; then - echo "$name does not match expected structure" - echo "" - echo "Expected:" - echo "$expected_sorted" - echo "" - echo "Actual:" - echo "$actual_sorted" - echo "" - echo "Diff:" - diff <(echo "$expected_sorted") <(echo "$actual_sorted") || true - return 1 - fi -} diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index 285f7b37..00000000 --- a/run_tests.sh +++ /dev/null @@ -1,115 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Test runner for all BATS tests across all modules -# -# Usage: -# ./run_tests.sh # Run all tests -# ./run_tests.sh frontend # Run tests for frontend module only -# ./run_tests.sh frontend/deployment/tests/aws # Run specific test directory -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -CYAN='\033[0;36m' -NC='\033[0m' - -# Check if bats is installed -if ! command -v bats &> /dev/null; then - echo -e "${RED}bats-core is not installed${NC}" - echo "" - echo "Install with:" - echo " brew install bats-core # macOS" - echo " apt install bats # Ubuntu/Debian" - exit 1 -fi - -# Check if jq is installed -if ! command -v jq &> /dev/null; then - echo -e "${RED}jq is not installed${NC}" - echo "" - echo "Install with:" - echo " brew install jq # macOS" - echo " apt install jq # Ubuntu/Debian" - exit 1 -fi - -# Find all test directories -find_test_dirs() { - find . -type d -name "tests" -path "*/deployment/*" 2>/dev/null | sort -} - -# Get module name from test path -get_module_name() { - local path="$1" - echo "$path" | sed 's|^\./||' | cut -d'/' -f1 -} - -# Run tests for a specific directory -run_tests_in_dir() { - local test_dir="$1" - local module_name=$(get_module_name "$test_dir") - - # Find all .bats files recursively - local bats_files=$(find "$test_dir" -name "*.bats" 2>/dev/null) - - if [ -z "$bats_files" ]; then - return 0 - fi - - echo -e "${CYAN}[$module_name]${NC} Running BATS tests in $test_dir" - echo "" - - ( - cd "$test_dir" - # Use script to force TTY for colored output - script -q /dev/null bats --formatter pretty $(find . -name "*.bats" | sort) - ) - - echo "" -} - -echo "" -echo "========================================" -echo " BATS Tests" -echo "========================================" -echo "" - -if [ -n "$1" ]; then - # Run tests for specific module or directory - if [ -d "$1" ]; then - # Direct directory path - run_tests_in_dir "$1" - elif [ -d "$1/deployment/tests" ]; then - # Module name (e.g., "frontend") - run_tests_in_dir "$1/deployment/tests" - else - echo -e "${RED}Test directory not found: $1${NC}" - echo "" - echo "Available modules with tests:" - for dir in $(find_test_dirs); do - echo " - $(get_module_name "$dir")" - done - exit 1 - fi -else - # Run all tests - test_dirs=$(find_test_dirs) - - if [ -z "$test_dirs" ]; then - echo -e "${YELLOW}No test directories found${NC}" - exit 0 - fi - - for test_dir in $test_dirs; do - run_tests_in_dir "$test_dir" - done -fi - -echo -e "${GREEN}All BATS tests passed!${NC}" diff --git a/run_tofu_tests.sh b/run_tofu_tests.sh deleted file mode 100755 index 7ac606b8..00000000 --- a/run_tofu_tests.sh +++ /dev/null @@ -1,129 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Test runner for all OpenTofu/Terraform tests across all modules -# -# Usage: -# ./run_tofu_tests.sh # Run all tofu tests -# ./run_tofu_tests.sh frontend # Run tests for frontend module -# ./run_tofu_tests.sh frontend/provider/aws # Run specific module tests -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -CYAN='\033[0;36m' -NC='\033[0m' - -# Check if tofu is installed -if ! command -v tofu &> /dev/null; then - echo -e "${RED}OpenTofu is not installed${NC}" - echo "" - echo "Install with:" - echo " brew install opentofu # macOS" - echo " See https://opentofu.org/docs/intro/install/" - exit 1 -fi - -# Find all directories with .tftest.hcl files -find_tofu_test_dirs() { - find . -name "*.tftest.hcl" -path "*/deployment/*" 2>/dev/null | xargs -I{} dirname {} | sort -u -} - -# Get module name from path -get_module_name() { - local path="$1" - echo "$path" | sed 's|^\./||' | cut -d'/' -f1 -} - -# Get relative module path (e.g., provider/aws/modules) -get_relative_path() { - local path="$1" - echo "$path" | sed 's|^\./[^/]*/deployment/||' -} - -# Run tests for a specific directory -run_tofu_tests_in_dir() { - local test_dir="$1" - local module_name=$(get_module_name "$test_dir") - local relative_path=$(get_relative_path "$test_dir") - - echo -e "${CYAN}[$module_name]${NC} ${relative_path}" - - ( - cd "$test_dir" - - # Initialize if needed (without backend) - if [ ! -d ".terraform" ]; then - tofu init -backend=false -input=false >/dev/null 2>&1 || true - fi - - # Run tests - tofu test - ) - - echo "" -} - -echo "" -echo "========================================" -echo " OpenTofu Tests" -echo "========================================" -echo "" - -if [ -n "$1" ]; then - # Run tests for specific module or directory - if [ -d "$1" ] && ls "$1"/*.tftest.hcl &>/dev/null; then - # Direct directory with test files - run_tofu_tests_in_dir "$1" - elif [ -d "$1/deployment" ]; then - # Module name (e.g., "frontend") - module_dirs=$(find "$1/deployment" -name "*.tftest.hcl" 2>/dev/null | xargs -I{} dirname {} | sort -u) - if [ -z "$module_dirs" ]; then - echo -e "${YELLOW}No tofu test files found in $1${NC}" - exit 0 - fi - for dir in $module_dirs; do - run_tofu_tests_in_dir "$dir" - done - elif [ -d "$1/modules" ] && ls "$1/modules"/*.tftest.hcl &>/dev/null; then - # Path like "frontend/provider/aws" -> check frontend/deployment/provider/aws/modules - run_tofu_tests_in_dir "$1/modules" - else - # Try to find it under deployment - for base in */deployment; do - if [ -d "$base/$1/modules" ] && ls "$base/$1/modules"/*.tftest.hcl &>/dev/null 2>&1; then - run_tofu_tests_in_dir "$base/$1/modules" - exit 0 - fi - done - echo -e "${RED}No tofu test files found for: $1${NC}" - echo "" - echo "Available modules with tofu tests:" - for dir in $(find_tofu_test_dirs); do - local module=$(get_module_name "$dir") - local rel=$(get_relative_path "$dir") - echo " - $module: $rel" - done - exit 1 - fi -else - # Run all tests - test_dirs=$(find_tofu_test_dirs) - - if [ -z "$test_dirs" ]; then - echo -e "${YELLOW}No tofu test files found${NC}" - exit 0 - fi - - for test_dir in $test_dirs; do - run_tofu_tests_in_dir "$test_dir" - done -fi - -echo -e "${GREEN}All OpenTofu tests passed!${NC}" diff --git a/testing/assertions.sh b/testing/assertions.sh index ab36c582..cd2abc44 100644 --- a/testing/assertions.sh +++ b/testing/assertions.sh @@ -321,4 +321,4 @@ USAGE IN TESTS ================================================================================ EOF -} +} \ No newline at end of file diff --git a/testing/run_bats_tests.sh b/testing/run_bats_tests.sh index d17384e6..36d72173 100755 --- a/testing/run_bats_tests.sh +++ b/testing/run_bats_tests.sh @@ -191,4 +191,4 @@ if [ ${#FAILED_TESTS[@]} -gt 0 ]; then exit 1 fi -echo -e "${GREEN}All BATS tests passed!${NC}" +echo -e "${GREEN}All BATS tests passed!${NC}" \ No newline at end of file From d6f404b5812ea1125985b1fa6bedc50c812fa01f Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 13 Jan 2026 13:41:22 -0300 Subject: [PATCH 08/40] base integration test framework --- Makefile | 5 +- .../integration/run_integration_tests.sh | 187 ------------------ .../tests/integration_test_utils.sh | 62 +++--- run_integration_tests.sh | 102 ---------- testing/run_integration_tests.sh | 20 +- 5 files changed, 50 insertions(+), 326 deletions(-) delete mode 100755 frontend/deployment/tests/integration/run_integration_tests.sh delete mode 100755 run_integration_tests.sh diff --git a/Makefile b/Makefile index ee5e039c..5c471b8e 100644 --- a/Makefile +++ b/Makefile @@ -39,9 +39,6 @@ ifdef MODULE else @./testing/run_integration_tests.sh $(if $(VERBOSE),-v) endif -# Run integration tests (placeholder) -test-integration: - @./run_integration_tests.sh # Help help: @@ -54,4 +51,4 @@ help: @echo "" @echo "Options:" @echo " MODULE= Run tests for specific module (e.g., MODULE=frontend)" - @echo " VERBOSE=1 Show output of passing tests (integration tests only)" + @echo " VERBOSE=1 Show output of passing tests (integration tests only)" \ No newline at end of file diff --git a/frontend/deployment/tests/integration/run_integration_tests.sh b/frontend/deployment/tests/integration/run_integration_tests.sh deleted file mode 100755 index 269a208f..00000000 --- a/frontend/deployment/tests/integration/run_integration_tests.sh +++ /dev/null @@ -1,187 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Integration test runner for shunit2 tests -# -# Usage: -# ./run_integration_tests.sh # Run all integration tests -# ./run_integration_tests.sh test_file.sh # Run specific test file -# ./run_integration_tests.sh --no-localstack # Skip LocalStack management -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -CYAN='\033[0;36m' -NC='\033[0m' - -# Parse arguments -MANAGE_LOCALSTACK=true -SPECIFIC_TEST="" - -for arg in "$@"; do - case $arg in - --no-localstack) - MANAGE_LOCALSTACK=false - ;; - *.sh) - SPECIFIC_TEST="$arg" - ;; - esac -done - -# Check dependencies -check_dependencies() { - local missing=() - - if ! command -v docker &> /dev/null; then - missing+=("docker") - fi - - if ! command -v docker compose &> /dev/null && ! command -v docker-compose &> /dev/null; then - missing+=("docker-compose") - fi - - if ! command -v jq &> /dev/null; then - missing+=("jq") - fi - - if ! command -v aws &> /dev/null; then - missing+=("aws-cli") - fi - - # Check for shunit2 - if ! command -v shunit2 &> /dev/null && \ - [ ! -f "/usr/local/bin/shunit2" ] && \ - [ ! -f "/usr/share/shunit2/shunit2" ] && \ - [ ! -f "/opt/homebrew/bin/shunit2" ]; then - missing+=("shunit2") - fi - - if [ ${#missing[@]} -gt 0 ]; then - echo -e "${RED}Missing dependencies:${NC}" - for dep in "${missing[@]}"; do - echo " - $dep" - done - echo "" - echo "Install with:" - echo " brew install docker jq awscli shunit2 # macOS" - exit 1 - fi -} - -# Start LocalStack -start_localstack() { - echo -e "${CYAN}Starting LocalStack...${NC}" - docker compose -f "$SCRIPT_DIR/docker-compose.yml" up -d - - echo "Waiting for LocalStack to be ready..." - local max_attempts=30 - local attempt=0 - - while [ $attempt -lt $max_attempts ]; do - if curl -s "http://localhost:4566/_localstack/health" | jq -e '.services.s3 == "running"' > /dev/null 2>&1; then - echo -e "${GREEN}LocalStack is ready${NC}" - return 0 - fi - attempt=$((attempt + 1)) - sleep 2 - echo -n "." - done - - echo "" - echo -e "${RED}LocalStack failed to start${NC}" - return 1 -} - -# Stop LocalStack -stop_localstack() { - echo -e "${CYAN}Stopping LocalStack...${NC}" - docker compose -f "$SCRIPT_DIR/docker-compose.yml" down -v -} - -# Run a single test file -run_test_file() { - local test_file="$1" - local test_name=$(basename "$test_file" .sh) - - echo "" - echo -e "${CYAN}Running: $test_name${NC}" - echo "========================================" - - if bash "$test_file"; then - echo -e "${GREEN}PASSED: $test_name${NC}" - return 0 - else - echo -e "${RED}FAILED: $test_name${NC}" - return 1 - fi -} - -# Main -echo "" -echo "========================================" -echo " Integration Tests (shunit2)" -echo "========================================" -echo "" - -check_dependencies - -# Manage LocalStack if requested -if [ "$MANAGE_LOCALSTACK" = true ]; then - # Ensure LocalStack is stopped on exit - trap stop_localstack EXIT - start_localstack -fi - -# Find and run tests -FAILED=0 -PASSED=0 - -if [ -n "$SPECIFIC_TEST" ]; then - # Run specific test - if [ -f "$SPECIFIC_TEST" ]; then - if run_test_file "$SPECIFIC_TEST"; then - PASSED=$((PASSED + 1)) - else - FAILED=$((FAILED + 1)) - fi - else - echo -e "${RED}Test file not found: $SPECIFIC_TEST${NC}" - exit 1 - fi -else - # Run all test files - for test_file in "$SCRIPT_DIR"/*_test.sh; do - if [ -f "$test_file" ]; then - if run_test_file "$test_file"; then - PASSED=$((PASSED + 1)) - else - FAILED=$((FAILED + 1)) - fi - fi - done -fi - -# Summary -echo "" -echo "========================================" -echo " Summary" -echo "========================================" -echo -e "Passed: ${GREEN}$PASSED${NC}" -echo -e "Failed: ${RED}$FAILED${NC}" - -if [ $FAILED -gt 0 ]; then - echo "" - echo -e "${RED}Some integration tests failed${NC}" - exit 1 -else - echo "" - echo -e "${GREEN}All integration tests passed!${NC}" - exit 0 -fi diff --git a/frontend/deployment/tests/integration_test_utils.sh b/frontend/deployment/tests/integration_test_utils.sh index 440d3018..3fc4328c 100755 --- a/frontend/deployment/tests/integration_test_utils.sh +++ b/frontend/deployment/tests/integration_test_utils.sh @@ -235,19 +235,33 @@ run_workflow_step() { # AWS Resource Assertions (against LocalStack) # ============================================================================= +# Colors for assertions +ASSERT_GREEN='\033[0;32m' +ASSERT_RED='\033[0;31m' +ASSERT_CYAN='\033[0;36m' +ASSERT_NC='\033[0m' + aws_local() { aws --endpoint-url="$LOCALSTACK_ENDPOINT" --no-cli-pager --no-cli-auto-prompt "$@" } +assert_pass() { + echo -e "${ASSERT_GREEN}PASS${ASSERT_NC}" +} + +assert_fail() { + echo -e "${ASSERT_RED}FAIL${ASSERT_NC}" +} + assert_s3_bucket_exists() { local bucket="$1" - echo -n " Checking S3 bucket '$bucket' exists... " - if aws_local s3api head-bucket --bucket "$bucket" 2>/dev/null; then - echo "✓" + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} S3 bucket '${bucket}' exists ... " + if aws_local s3api head-bucket --bucket "$bucket" >/dev/null 2>&1; then + assert_pass return 0 else - echo "✗" + assert_fail fail "S3 bucket does not exist: $bucket" return 1 fi @@ -256,13 +270,13 @@ assert_s3_bucket_exists() { assert_s3_bucket_not_exists() { local bucket="$1" - echo -n " Checking S3 bucket '$bucket' does not exist... " - if aws_local s3api head-bucket --bucket "$bucket" 2>/dev/null; then - echo "✗" + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} S3 bucket '${bucket}' does not exist ... " + if aws_local s3api head-bucket --bucket "$bucket" >/dev/null 2>&1; then + assert_fail fail "S3 bucket should not exist: $bucket" return 1 else - echo "✓" + assert_pass return 0 fi } @@ -270,7 +284,7 @@ assert_s3_bucket_not_exists() { assert_cloudfront_distribution_exists() { local comment="$1" - echo -n " Checking CloudFront distribution with comment '$comment' exists... " + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} CloudFront distribution '${comment}' exists ... " # CloudFront uses Moto endpoint, not LocalStack local distribution distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ @@ -278,10 +292,10 @@ assert_cloudfront_distribution_exists() { --output text 2>/dev/null) if [ -n "$distribution" ] && [ "$distribution" != "None" ]; then - echo "✓" + assert_pass return 0 else - echo "✗" + assert_fail fail "CloudFront distribution does not exist with comment: $comment" return 1 fi @@ -290,7 +304,7 @@ assert_cloudfront_distribution_exists() { assert_cloudfront_distribution_not_exists() { local comment="$1" - echo -n " Checking CloudFront distribution with comment '$comment' does not exist... " + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} CloudFront distribution '${comment}' does not exist ... " # CloudFront uses Moto endpoint, not LocalStack local distribution distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ @@ -298,10 +312,10 @@ assert_cloudfront_distribution_not_exists() { --output text 2>/dev/null) if [ -z "$distribution" ] || [ "$distribution" == "None" ]; then - echo "✓" + assert_pass return 0 else - echo "✗" + assert_fail fail "CloudFront distribution should not exist with comment: $comment" return 1 fi @@ -311,7 +325,7 @@ assert_route53_record_exists() { local record_name="$1" local record_type="$2" - echo -n " Checking Route53 record '$record_name' ($record_type) exists... " + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} Route53 ${record_type} record '${record_name}' exists ... " # Ensure record name ends with a dot [[ "$record_name" != *. ]] && record_name="$record_name." @@ -323,7 +337,7 @@ assert_route53_record_exists() { --output text 2>/dev/null | sed 's|/hostedzone/||') if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then - echo "✗" + assert_fail fail "No Route53 hosted zones found" return 1 fi @@ -335,10 +349,10 @@ assert_route53_record_exists() { --output text 2>/dev/null) if [ -n "$record" ] && [ "$record" != "None" ]; then - echo "✓" + assert_pass return 0 else - echo "✗" + assert_fail fail "Route53 record does not exist: $record_name ($record_type) in zone $zone_id" return 1 fi @@ -348,7 +362,7 @@ assert_route53_record_not_exists() { local record_name="$1" local record_type="$2" - echo -n " Checking Route53 record '$record_name' ($record_type) does not exist... " + echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} Route53 ${record_type} record '${record_name}' does not exist ... " # Ensure record name ends with a dot [[ "$record_name" != *. ]] && record_name="$record_name." @@ -361,7 +375,7 @@ assert_route53_record_not_exists() { if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then # No zones means no records, so assertion passes - echo "✓" + assert_pass return 0 fi @@ -372,10 +386,10 @@ assert_route53_record_not_exists() { --output text 2>/dev/null) if [ -z "$record" ] || [ "$record" == "None" ]; then - echo "✓" + assert_pass return 0 else - echo "✗" + assert_fail fail "Route53 record should not exist: $record_name ($record_type) in zone $zone_id" return 1 fi @@ -390,7 +404,9 @@ run_assertions() { local assertions=$(get_step_assertions "$step_index") local assertion_count=$(echo "$assertions" | jq -r 'length') - echo "Running $assertion_count assertions..." + echo "" + echo -e "${ASSERT_CYAN}Running ${assertion_count} assertion(s)${ASSERT_NC}" + echo "" for i in $(seq 0 $((assertion_count - 1))); do local assertion=$(echo "$assertions" | jq -r ".[$i]") diff --git a/run_integration_tests.sh b/run_integration_tests.sh deleted file mode 100755 index b91a305f..00000000 --- a/run_integration_tests.sh +++ /dev/null @@ -1,102 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Integration test runner for all modules -# -# Usage: -# ./run_integration_tests.sh # Run all integration tests -# ./run_integration_tests.sh frontend # Run tests for frontend module -# ./run_integration_tests.sh --no-localstack # Skip LocalStack management -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -CYAN='\033[0;36m' -NC='\033[0m' - -# Parse arguments -EXTRA_ARGS="" -MODULE="" - -for arg in "$@"; do - case $arg in - --no-localstack) - EXTRA_ARGS="$EXTRA_ARGS --no-localstack" - ;; - *) - MODULE="$arg" - ;; - esac -done - -# Find all integration test directories -find_integration_dirs() { - find . -type d -name "integration" -path "*/deployment/tests/*" 2>/dev/null | sort -} - -# Get module name from test path -get_module_name() { - local path="$1" - echo "$path" | sed 's|^\./||' | cut -d'/' -f1 -} - -# Run integration tests for a specific directory -run_integration_in_dir() { - local test_dir="$1" - local module_name=$(get_module_name "$test_dir") - - if [ ! -f "$test_dir/run_integration_tests.sh" ]; then - return 0 - fi - - echo -e "${CYAN}[$module_name]${NC} Running integration tests in $test_dir" - echo "" - - ( - cd "$test_dir" - ./run_integration_tests.sh $EXTRA_ARGS - ) - - echo "" -} - -echo "" -echo "========================================" -echo " Integration Tests" -echo "========================================" -echo "" - -if [ -n "$MODULE" ]; then - # Run tests for specific module - if [ -d "$MODULE/deployment/tests/integration" ]; then - run_integration_in_dir "$MODULE/deployment/tests/integration" - else - echo -e "${RED}Integration test directory not found for: $MODULE${NC}" - echo "" - echo "Available modules with integration tests:" - for dir in $(find_integration_dirs); do - echo " - $(get_module_name "$dir")" - done - exit 1 - fi -else - # Run all integration tests - integration_dirs=$(find_integration_dirs) - - if [ -z "$integration_dirs" ]; then - echo -e "${YELLOW}No integration test directories found${NC}" - exit 0 - fi - - for test_dir in $integration_dirs; do - run_integration_in_dir "$test_dir" - done -fi - -echo -e "${GREEN}All integration tests passed!${NC}" diff --git a/testing/run_integration_tests.sh b/testing/run_integration_tests.sh index 0a020f60..97af739f 100755 --- a/testing/run_integration_tests.sh +++ b/testing/run_integration_tests.sh @@ -34,15 +34,15 @@ VERBOSE="" for arg in "$@"; do case $arg in - --build) - BUILD_FLAG="--build" - ;; - -v|--verbose) - VERBOSE="--show-output-of-passing-tests" - ;; - *) - MODULE="$arg" - ;; + --build) + BUILD_FLAG="--build" + ;; + -v|--verbose) + VERBOSE="--show-output-of-passing-tests" + ;; + *) + MODULE="$arg" + ;; esac done @@ -220,4 +220,4 @@ if [ $TOTAL_FAILED -gt 0 ]; then exit 1 else echo -e "${GREEN}All integration tests passed!${NC}" -fi +fi \ No newline at end of file From 3d1927e0394542f923986e48fbac4981c8ade50a Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 13 Jan 2026 16:33:25 -0300 Subject: [PATCH 09/40] Testing improvements --- .../cloudfront_lifecycle_test.bats | 116 ++++ .../integration/cloudfront_lifecycle_test.sh | 78 --- .../configs/example_create_and_destroy.json | 97 ---- .../tests/integration/docker-compose.yml | 37 -- .../mocks/asset_repository/category.json | 12 + .../mocks/asset_repository/get_provider.json | 13 + .../mocks/asset_repository/list_provider.json | 18 + .../asset_repository/list_provider_spec.json | 14 + .../deployment/tests/integration/mocks/np | 123 ----- .../responses/asset_repository_success.json | 26 - .../mocks/responses/scope_success.json | 6 - .../tests/integration_test_utils.sh | 508 ------------------ testing/docker/Dockerfile.test-runner | 2 +- testing/docker/certs/cert.pem | 18 + testing/docker/certs/key.pem | 27 + testing/docker/docker-compose.integration.yml | 2 +- testing/docker/nginx.conf | 2 +- testing/integration_helpers.sh | 72 +-- 18 files changed, 257 insertions(+), 914 deletions(-) create mode 100644 frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats delete mode 100755 frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh delete mode 100644 frontend/deployment/tests/integration/configs/example_create_and_destroy.json delete mode 100644 frontend/deployment/tests/integration/docker-compose.yml create mode 100644 frontend/deployment/tests/integration/mocks/asset_repository/category.json create mode 100644 frontend/deployment/tests/integration/mocks/asset_repository/get_provider.json create mode 100644 frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json create mode 100644 frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json delete mode 100755 frontend/deployment/tests/integration/mocks/np delete mode 100644 frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json delete mode 100644 frontend/deployment/tests/integration/mocks/responses/scope_success.json delete mode 100755 frontend/deployment/tests/integration_test_utils.sh create mode 100644 testing/docker/certs/cert.pem create mode 100644 testing/docker/certs/key.pem diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats new file mode 100644 index 00000000..dbb3d400 --- /dev/null +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats @@ -0,0 +1,116 @@ +#!/usr/bin/env bats +# ============================================================================= +# Integration test: CloudFront Distribution Lifecycle +# +# Tests the full lifecycle: create infrastructure, verify it exists, +# then destroy it and verify cleanup. +# ============================================================================= + +setup_file() { + # Load integration helpers + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + + # Clear any existing mocks + clear_mocks + + # Create AWS prerequisites + echo "Creating test prerequisites..." + aws_local s3api create-bucket --bucket assets-bucket >/dev/null 2>&1 || true + aws_local s3api create-bucket --bucket tofu-state-bucket >/dev/null 2>&1 || true + aws_local dynamodb create-table \ + --table-name tofu-locks \ + --attribute-definitions AttributeName=LockID,AttributeType=S \ + --key-schema AttributeName=LockID,KeyType=HASH \ + --billing-mode PAY_PER_REQUEST >/dev/null 2>&1 || true + aws_local route53 create-hosted-zone \ + --name frontend.publicdomain.com \ + --caller-reference "test-$(date +%s)" >/dev/null 2>&1 || true + + # Get hosted zone ID for context override + HOSTED_ZONE_ID=$(aws_local route53 list-hosted-zones --query 'HostedZones[0].Id' --output text | sed 's|/hostedzone/||') + + export HOSTED_ZONE_ID +} + +teardown_file() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + clear_mocks +} + +# Setup runs before each test +setup() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + + # Clear mocks before each test + clear_mocks + + # Load context + load_context "frontend/deployment/tests/resources/context.json" + + # Override hosted zone ID with the one created in setup_file + override_context "providers.cloud-providers.networking.hosted_public_zone_id" "$HOSTED_ZONE_ID" + + # Export common environment variables + export NETWORK_LAYER="route53" + export DISTRIBUTION_LAYER="cloudfront" + export TOFU_PROVIDER="aws" + export TOFU_PROVIDER_BUCKET="tofu-state-bucket" + export TOFU_LOCK_TABLE="tofu-locks" + export AWS_REGION="us-east-1" + export SERVICE_PATH="$INTEGRATION_MODULE_ROOT/frontend" + + # Point to LocalStack-compatible modules + export CUSTOM_TOFU_MODULES="$BATS_TEST_DIRNAME/localstack" +} + +# ============================================================================= +# Test: Create Infrastructure +# ============================================================================= + +@test "create infrastructure deploys S3, CloudFront, and Route53 resources" { + # Setup API mocks for np CLI calls + # Note: /token is automatically mocked by clear_mocks() + local mocks_dir="frontend/deployment/tests/integration/mocks/asset_repository" + + # Mock the np CLI internal API calls + mock_request "GET" "/category" "$mocks_dir/category.json" + mock_request "GET" "/provider_specification" "$mocks_dir/list_provider_spec.json" + mock_request "GET" "/provider" "$mocks_dir/list_provider.json" + mock_request "GET" "/provider/s3-asset-repository-id" "$mocks_dir/get_provider.json" + + # Run the initial workflow + run_workflow "frontend/deployment/workflows/initial.yaml" + + # Verify resources were created + assert_s3_bucket_exists "assets-bucket" + assert_cloudfront_exists "Distribution for automation-development-tools-7" + assert_route53_record_exists "automation-development-tools.frontend.publicdomain.com" "A" +} + +# ============================================================================= +# Test: Destroy Infrastructure +# ============================================================================= + +#@test "destroy infrastructure removes CloudFront and Route53 resources" { +# # Setup API mocks +# mock_request "GET" "/provider" "frontend/deployment/tests/integration/mocks/asset_repository/success.json" +# +# mock_request "GET" "/scope/7" 200 '{ +# "id": 7, +# "name": "development-tools", +# "slug": "development-tools" +# }' +# +# # Disable CloudFront before deletion (required by AWS) +# if [[ -f "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" ]]; then +# "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" "Distribution for automation-development-tools-7" +# fi +# +# # Run the delete workflow +# run_workflow "frontend/deployment/workflows/delete.yaml" +# +# # Verify resources were removed (S3 bucket should remain) +# assert_s3_bucket_exists "assets-bucket" +# assert_cloudfront_not_exists "Distribution for automation-development-tools-7" +# assert_route53_record_not_exists "automation-development-tools.frontend.publicdomain.com" "A" +#} diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh deleted file mode 100755 index cf0d760d..00000000 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Integration test: CloudFront distribution lifecycle -# -# Tests the full lifecycle of creating and destroying CloudFront infrastructure -# using shunit2 test framework. -# -# Run: ./run_integration_tests.sh cloudfront_lifecycle_test.sh -# ============================================================================= - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -# Set integration test directory before sourcing utilities -export INTEGRATION_TEST_DIR="$SCRIPT_DIR" - -# Source test utilities from shared location -. "$SCRIPT_DIR/../integration_test_utils.sh" - -# ============================================================================= -# Test Setup/Teardown -# ============================================================================= - -oneTimeSetUp() { - # Start LocalStack once for all tests in this file - localstack_start -} - -oneTimeTearDown() { - # Stop LocalStack after all tests complete - localstack_stop -} - -setUp() { - # Reset LocalStack state before each test - localstack_reset -} - -tearDown() { - # Cleanup after each test if needed - : -} - -# ============================================================================= -# Tests -# ============================================================================= - -test_create_and_destroy_cloudfront_distribution() { - # Load test configuration - load_test_config "$SCRIPT_DIR/configs/example_create_and_destroy.json" - - # Execute all steps defined in the config - execute_all_steps - - # If we get here without failures, the test passed - assertTrue "All steps completed successfully" true -} - -# ============================================================================= -# Load shunit2 -# ============================================================================= - -# Find shunit2 - check common locations -if [ -f "/usr/local/bin/shunit2" ]; then - . /usr/local/bin/shunit2 -elif [ -f "/usr/share/shunit2/shunit2" ]; then - . /usr/share/shunit2/shunit2 -elif [ -f "/opt/homebrew/bin/shunit2" ]; then - . /opt/homebrew/bin/shunit2 -elif command -v shunit2 &> /dev/null; then - . "$(command -v shunit2)" -else - echo "Error: shunit2 not found" - echo "" - echo "Install with:" - echo " brew install shunit2 # macOS" - echo " apt install shunit2 # Ubuntu/Debian" - exit 1 -fi diff --git a/frontend/deployment/tests/integration/configs/example_create_and_destroy.json b/frontend/deployment/tests/integration/configs/example_create_and_destroy.json deleted file mode 100644 index bc8a88b4..00000000 --- a/frontend/deployment/tests/integration/configs/example_create_and_destroy.json +++ /dev/null @@ -1,97 +0,0 @@ -{ - "name": "Create and destroy CloudFront distribution", - "description": "Tests the full lifecycle: create infrastructure, verify it exists, then destroy it", - "context_file": "resources/context.json", - "setup": [ - "aws s3api create-bucket --bucket assets-bucket", - "aws s3api create-bucket --bucket tofu-state-bucket", - "aws dynamodb create-table --table-name tofu-locks --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --billing-mode PAY_PER_REQUEST", - "aws route53 create-hosted-zone --name frontend.publicdomain.com --caller-reference public-zone-id" - ], - "context_overrides": { - "providers.cloud-providers.networking.hosted_public_zone_id": "$(aws route53 list-hosted-zones --query 'HostedZones[0].Id' --output text | sed 's|/hostedzone/||')" - }, - "steps": [ - { - "name": "create_infrastructure", - "workflow": "$PROJECT_DIR/workflows/initial.yaml", - "env": { - "CUSTOM_TOFU_MODULES": "$INTEGRATION_TEST_DIR/localstack", - "SERVICE_PATH": "$PROJECT_DIR/..", - "NETWORK_LAYER": "route53", - "DISTRIBUTION_LAYER": "cloudfront", - "TOFU_PROVIDER": "aws", - "TOFU_PROVIDER_BUCKET": "tofu-state-bucket", - "TOFU_LOCK_TABLE": "tofu-locks", - "AWS_REGION": "us-east-1" - }, - "np_mocks": { - "np provider list": { - "response_file": "asset_repository_success.json", - "exit_code": 0 - }, - "np scope get": { - "response_file": "scope_success.json", - "exit_code": 0 - } - }, - "assertions": [ - { - "type": "s3_bucket_exists", - "bucket": "assets-bucket" - }, - { - "type": "cloudfront_distribution_exists", - "comment": "Distribution for automation-development-tools-7" - }, - { - "type": "route53_record_exists", - "name": "automation-development-tools.frontend.publicdomain.com", - "record_type": "A" - } - ] - }, - { - "name": "destroy_infrastructure", - "before": [ - "$INTEGRATION_TEST_DIR/scripts/disable_cloudfront.sh 'Distribution for automation-development-tools-7'" - ], - "workflow": "$PROJECT_DIR/workflows/delete.yaml", - "env": { - "CUSTOM_TOFU_MODULES": "$INTEGRATION_TEST_DIR/localstack", - "SERVICE_PATH": "$PROJECT_DIR/..", - "NETWORK_LAYER": "route53", - "DISTRIBUTION_LAYER": "cloudfront", - "TOFU_PROVIDER": "aws", - "TOFU_PROVIDER_BUCKET": "tofu-state-bucket", - "TOFU_LOCK_TABLE": "tofu-locks", - "AWS_REGION": "us-east-1" - }, - "np_mocks": { - "np provider list": { - "response_file": "asset_repository_success.json", - "exit_code": 0 - }, - "np scope get": { - "response_file": "scope_success.json", - "exit_code": 0 - } - }, - "assertions": [ - { - "type": "s3_bucket_exists", - "bucket": "assets-bucket" - }, - { - "type": "cloudfront_distribution_not_exists", - "comment": "Distribution for automation-development-tools-7" - }, - { - "type": "route53_record_not_exists", - "name": "automation-development-tools.frontend.publicdomain.com", - "record_type": "A" - } - ] - } - ] -} diff --git a/frontend/deployment/tests/integration/docker-compose.yml b/frontend/deployment/tests/integration/docker-compose.yml deleted file mode 100644 index ddda2fab..00000000 --- a/frontend/deployment/tests/integration/docker-compose.yml +++ /dev/null @@ -1,37 +0,0 @@ -services: - localstack: - image: localstack/localstack:latest - container_name: localstack-integration-tests - ports: - - "4566:4566" # LocalStack Gateway - - "4510-4559:4510-4559" # External services port range - environment: - - DEBUG=0 - - SERVICES=s3,route53,sts,iam,dynamodb,acm - - DEFAULT_REGION=us-east-1 - - AWS_DEFAULT_REGION=us-east-1 - - AWS_ACCESS_KEY_ID=test - - AWS_SECRET_ACCESS_KEY=test - - PERSISTENCE=0 - - EAGER_SERVICE_LOADING=1 - volumes: - - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" - - "/var/run/docker.sock:/var/run/docker.sock" - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:4566/_localstack/health"] - interval: 5s - timeout: 5s - retries: 10 - - moto: - image: motoserver/moto:latest - container_name: moto-cloudfront - ports: - - "5555:5000" - environment: - - MOTO_PORT=5000 - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:5000/moto-api/"] - interval: 5s - timeout: 5s - retries: 10 diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/category.json b/frontend/deployment/tests/integration/mocks/asset_repository/category.json new file mode 100644 index 00000000..3dd986f4 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/asset_repository/category.json @@ -0,0 +1,12 @@ +{ + "status": 200, + "body": { + "results": [ + { + "id": "assets-repository-id", + "slug": "assets-repository", + "name": "Assets Repository" + } + ] + } +} diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/get_provider.json b/frontend/deployment/tests/integration/mocks/asset_repository/get_provider.json new file mode 100644 index 00000000..6cacf827 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/asset_repository/get_provider.json @@ -0,0 +1,13 @@ +{ + "status": 200, + "body": { + "id": "s3-asset-repository-id", + "specification_id": "s3-asset-repository-spec-id", + "category": "assets-repository-id", + "attributes": { + "bucket": { + "name": "assets-bucket" + } + } + } +} diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json new file mode 100644 index 00000000..e7b3e9e7 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json @@ -0,0 +1,18 @@ +{ + "status": 200, + "body": { + "results": [ + { + "category": "assets-repository-id", + "created_at": "2026-01-07T16:28:17.036Z", + "dimensions": {}, + "groups": [], + "id": "s3-asset-repository-id", + "nrn": "organization=1255165411:account=95118862", + "specification_id": "s3-asset-repository-spec-id", + "tags": [], + "updated_at": "2026-01-07T16:28:17.036Z" + } + ] + } +} \ No newline at end of file diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json new file mode 100644 index 00000000..c621bf02 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json @@ -0,0 +1,14 @@ +{ + "status": 200, + "body": { + "results": [ + { + "id": "s3-asset-repository-spec-id", + "slug": "s3-assets", + "categories": [ + {"slug": "assets-repository"} + ] + } + ] + } +} diff --git a/frontend/deployment/tests/integration/mocks/np b/frontend/deployment/tests/integration/mocks/np deleted file mode 100755 index bc1d1be4..00000000 --- a/frontend/deployment/tests/integration/mocks/np +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash -# ============================================================================= -# np CLI mock for integration tests -# -# This mock intercepts all np commands EXCEPT 'np service workflow exec' -# which is passed through to the real np CLI. -# -# Mock responses are configured via environment variables: -# NP_MOCK_DIR - Directory containing mock response files -# NP_MOCK_CONFIG - JSON file with mock configuration for current test step -# NP_REAL_CLI - Path to the real np CLI (default: uses which np from original PATH) -# ============================================================================= - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -# Build the command key from arguments (e.g., "np provider list", "np scope get") -# This extracts only the subcommands (no flags) for matching -build_command_key() { - local key="np" - local skip_next=false - for arg in "$@"; do - # Skip flag values (argument after a flag) - if [ "$skip_next" = true ]; then - skip_next=false - continue - fi - # Skip flags and mark to skip their values - if [[ "$arg" == --* ]]; then - skip_next=true - continue - fi - if [[ "$arg" == -* ]]; then - skip_next=true - continue - fi - key="$key $arg" - done - echo "$key" -} - -# Build full command string with flags for error messages -build_full_command() { - local cmd="np" - local i=1 - while [ $i -le $# ]; do - local arg="${!i}" - cmd="$cmd $arg" - i=$((i + 1)) - done - echo "$cmd" -} - -# Check if this is a workflow exec command -is_workflow_exec() { - local args="$*" - if [[ "$args" == *"service workflow exec"* ]] || [[ "$args" == *"service"*"workflow"*"exec"* ]]; then - return 0 - fi - return 1 -} - -# If this is 'np service workflow exec', pass through to real CLI -if is_workflow_exec "$@"; then - if [ -n "$NP_REAL_CLI" ] && [ -x "$NP_REAL_CLI" ]; then - exec "$NP_REAL_CLI" "$@" - else - # Try to find np in original PATH (before mock was added) - if [ -n "$NP_ORIGINAL_PATH" ]; then - REAL_NP=$(PATH="$NP_ORIGINAL_PATH" which np 2>/dev/null) - if [ -n "$REAL_NP" ] && [ -x "$REAL_NP" ]; then - exec "$REAL_NP" "$@" - fi - fi - echo "Error: Cannot find real np CLI for workflow exec" >&2 - echo "Set NP_REAL_CLI or NP_ORIGINAL_PATH environment variable" >&2 - exit 1 - fi -fi - -# For all other commands, use mock responses -if [ -z "$NP_MOCK_CONFIG" ]; then - echo "Error: NP_MOCK_CONFIG not set" >&2 - exit 1 -fi - -if [ ! -f "$NP_MOCK_CONFIG" ]; then - echo "Error: Mock config file not found: $NP_MOCK_CONFIG" >&2 - exit 1 -fi - -# Build command key (for matching) and full command (for error messages) -CMD_KEY=$(build_command_key "$@") -FULL_CMD=$(build_full_command "$@") - -# Get the step index (np_mocks are defined per step) -STEP_INDEX="${NP_MOCK_STEP_INDEX:-0}" - -# Look up mock configuration for this command (mocks are in .steps[N].np_mocks) -MOCK_RESPONSE_FILE=$(jq -r --arg key "$CMD_KEY" --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks[$key].response_file // empty' "$NP_MOCK_CONFIG") -MOCK_EXIT_CODE=$(jq -r --arg key "$CMD_KEY" --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks[$key].exit_code // 0' "$NP_MOCK_CONFIG") - -if [ -z "$MOCK_RESPONSE_FILE" ]; then - echo "Error: No mock configured for command" >&2 - echo "" >&2 - echo " Full command: $FULL_CMD" >&2 - echo " Match key: $CMD_KEY" >&2 - echo " Step index: $STEP_INDEX" >&2 - echo "" >&2 - echo "Available mocks for this step:" >&2 - jq -r --argjson idx "$STEP_INDEX" '.steps[$idx].np_mocks // {} | keys[]' "$NP_MOCK_CONFIG" | sed 's/^/ - /' >&2 - exit 1 -fi - -# Output mock response -MOCK_FILE_PATH="$NP_MOCK_DIR/$MOCK_RESPONSE_FILE" -if [ -f "$MOCK_FILE_PATH" ]; then - cat "$MOCK_FILE_PATH" -else - echo "Error: Mock response file not found: $MOCK_FILE_PATH" >&2 - exit 1 -fi - -exit "${MOCK_EXIT_CODE:-0}" diff --git a/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json b/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json deleted file mode 100644 index 9db8b393..00000000 --- a/frontend/deployment/tests/integration/mocks/responses/asset_repository_success.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "results": [ - { - "attributes": { - "bucket": { - "name": "assets-bucket" - } - }, - "category": "assets-repository", - "created_at": "2026-01-07T16:28:17.036Z", - "dimensions": {}, - "groups": [], - "id": "4a7be073-92ee-4f66-91be-02d115bc3e7c", - "nrn": "organization=1255165411:account=95118862", - "specification_id": "85e164dc-3149-40c6-b85d-28bddf6e21e8", - "tags": [ - { - "id": "ceb2021b-714e-4fa5-9202-6965c744ffd9", - "key": "bucket.name", - "value": "assets-bucket" - } - ], - "updated_at": "2026-01-07T16:28:17.036Z" - } - ] -} \ No newline at end of file diff --git a/frontend/deployment/tests/integration/mocks/responses/scope_success.json b/frontend/deployment/tests/integration/mocks/responses/scope_success.json deleted file mode 100644 index cf0bd9e2..00000000 --- a/frontend/deployment/tests/integration/mocks/responses/scope_success.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "id": "456", - "slug": "production", - "name": "Production", - "application_id": "123" -} diff --git a/frontend/deployment/tests/integration_test_utils.sh b/frontend/deployment/tests/integration_test_utils.sh deleted file mode 100755 index 3fc4328c..00000000 --- a/frontend/deployment/tests/integration_test_utils.sh +++ /dev/null @@ -1,508 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Integration test utilities for shunit2 -# -# Provides helper functions for: -# - LocalStack management -# - AWS resource assertions -# - Test configuration loading -# - Workflow execution -# -# Usage: -# export INTEGRATION_TEST_DIR="/path/to/integration/test/dir" -# source "/path/to/tests/integration_test_utils.sh" -# ============================================================================= - -# Validate INTEGRATION_TEST_DIR is set -if [ -z "${INTEGRATION_TEST_DIR:-}" ]; then - echo "Error: INTEGRATION_TEST_DIR must be set before sourcing integration_test_utils.sh" - exit 1 -fi - -export INTEGRATION_TEST_DIR -export PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" - -# LocalStack configuration (S3, Route53, DynamoDB, IAM, STS, ACM) -export LOCALSTACK_ENDPOINT="${LOCALSTACK_ENDPOINT:-http://localhost:4566}" -# Moto configuration (CloudFront) -export MOTO_ENDPOINT="${MOTO_ENDPOINT:-http://localhost:5555}" -export AWS_ENDPOINT_URL="$LOCALSTACK_ENDPOINT" -export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-test}" -export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-test}" -export AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-us-east-1}" -export AWS_PAGER="" - -# Save original PATH before adding mock -export NP_ORIGINAL_PATH="$PATH" - -# Add mock np to PATH -export PATH="$INTEGRATION_TEST_DIR/mocks:$PATH" - -# ============================================================================= -# LocalStack Management -# ============================================================================= - -localstack_start() { - echo "Starting LocalStack..." - docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" up -d - localstack_wait_ready -} - -localstack_stop() { - echo "Stopping LocalStack..." - docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" down -v -} - -localstack_wait_ready() { - echo "Waiting for LocalStack to be ready..." - local max_attempts=30 - local attempt=0 - - while [ $attempt -lt $max_attempts ]; do - if curl -s "$LOCALSTACK_ENDPOINT/_localstack/health" | jq -e '.services.s3 == "running"' > /dev/null 2>&1; then - echo "LocalStack is ready" - return 0 - fi - attempt=$((attempt + 1)) - sleep 2 - done - - echo "LocalStack failed to start" - return 1 -} - -localstack_reset() { - echo "Resetting LocalStack state..." - # Reset by restarting the container - docker compose -f "$INTEGRATION_TEST_DIR/docker-compose.yml" restart localstack - localstack_wait_ready -} - -# ============================================================================= -# Test Configuration -# ============================================================================= - -load_test_config() { - local config_file="$1" - - if [ ! -f "$config_file" ]; then - echo "Error: Config file not found: $config_file" - return 1 - fi - - export CURRENT_TEST_CONFIG="$config_file" - export CURRENT_TEST_NAME=$(jq -r '.name' "$config_file") - export CURRENT_TEST_STEPS=$(jq -r '.steps | length' "$config_file") - - # Setup prerequisites - setup_prerequisites -} - -# ============================================================================= -# Prerequisites Setup (S3 buckets, Route53 zones, etc.) -# ============================================================================= - -setup_prerequisites() { - echo "" - echo "==========================================" - echo "Setting up prerequisites" - echo "==========================================" - - # Get setup commands array - local setup_commands=$(jq -r '.setup // []' "$CURRENT_TEST_CONFIG") - local cmd_count=$(echo "$setup_commands" | jq -r 'length') - - if [ "$cmd_count" -eq 0 ]; then - echo "No setup commands defined" - echo "" - return 0 - fi - - echo "Running $cmd_count setup command(s)..." - echo "" - - for i in $(seq 0 $((cmd_count - 1))); do - local cmd=$(echo "$setup_commands" | jq -r ".[$i]") - echo " $ $cmd" - - # Execute the command - eval "$cmd" /dev/null || true - done - - echo "" - echo "Prerequisites setup complete" - echo "" -} - -get_step_config() { - local step_index="$1" - jq -r ".steps[$step_index]" "$CURRENT_TEST_CONFIG" -} - -get_step_env() { - local step_index="$1" - jq -r ".steps[$step_index].env // {}" "$CURRENT_TEST_CONFIG" -} - -get_step_workflow() { - local step_index="$1" - jq -r ".steps[$step_index].workflow" "$CURRENT_TEST_CONFIG" -} - -get_step_assertions() { - local step_index="$1" - jq -r ".steps[$step_index].assertions // []" "$CURRENT_TEST_CONFIG" -} - -# ============================================================================= -# Workflow Execution -# ============================================================================= - -setup_test_environment() { - local step_index="$1" - - # Set mock configuration - export NP_MOCK_CONFIG="$CURRENT_TEST_CONFIG" - export NP_MOCK_DIR="$INTEGRATION_TEST_DIR/mocks/responses" - - # Set AWS endpoint for LocalStack - export AWS_ENDPOINT_URL="$LOCALSTACK_ENDPOINT" - - # Load CONTEXT from file if specified - local context_file=$(jq -r ".steps[$step_index].context_file // .context_file // empty" "$CURRENT_TEST_CONFIG") - if [ -n "$context_file" ]; then - # Resolve relative paths from the tests directory - if [[ "$context_file" != /* ]]; then - context_file="$PROJECT_DIR/tests/$context_file" - fi - if [ -f "$context_file" ]; then - echo " Loading CONTEXT from: $context_file" - export CONTEXT=$(cat "$context_file") - else - echo " Warning: Context file not found: $context_file" - fi - fi - - # Apply context_overrides - allows dynamic values using shell commands - local overrides=$(jq -r ".steps[$step_index].context_overrides // .context_overrides // {}" "$CURRENT_TEST_CONFIG") - if [ "$overrides" != "{}" ] && [ -n "$CONTEXT" ]; then - echo " Applying context overrides..." - local override_keys=$(echo "$overrides" | jq -r 'keys[]') - for key in $override_keys; do - local value_expr=$(echo "$overrides" | jq -r --arg k "$key" '.[$k]') - # Evaluate shell commands in the value (e.g., $(aws ...)) - local value=$(eval "echo \"$value_expr\"") - echo " $key = $value" - # Use jq to set nested keys (supports dot notation like "providers.cloud-providers.networking.hosted_public_zone_id") - CONTEXT=$(echo "$CONTEXT" | jq --arg k "$key" --arg v "$value" 'setpath($k | split("."); $v)') - done - export CONTEXT - fi - - # Load step-specific environment variables (with variable expansion) - local env_json=$(get_step_env "$step_index") - while IFS="=" read -r key value; do - if [ -n "$key" ]; then - # Expand environment variables in the value - local expanded_value=$(eval "echo \"$value\"") - export "$key=$expanded_value" - fi - done < <(echo "$env_json" | jq -r 'to_entries[] | "\(.key)=\(.value)"') -} - -run_workflow_step() { - local step_index="$1" - local workflow - workflow=$(get_step_workflow "$step_index") - # Expand environment variables in workflow path - workflow=$(eval "echo \"$workflow\"") - local step_name - step_name=$(jq -r ".steps[$step_index].name" "$CURRENT_TEST_CONFIG") - - echo "Running step: $step_name" - echo "Workflow: $workflow" - echo "" - - # Update mock config to point to this step's mocks - export NP_MOCK_STEP_INDEX="$step_index" - - # Execute the workflow using real np CLI - # The mock will pass through 'np service workflow exec' to the real CLI - np service workflow exec --workflow "$workflow" -} - -# ============================================================================= -# AWS Resource Assertions (against LocalStack) -# ============================================================================= - -# Colors for assertions -ASSERT_GREEN='\033[0;32m' -ASSERT_RED='\033[0;31m' -ASSERT_CYAN='\033[0;36m' -ASSERT_NC='\033[0m' - -aws_local() { - aws --endpoint-url="$LOCALSTACK_ENDPOINT" --no-cli-pager --no-cli-auto-prompt "$@" -} - -assert_pass() { - echo -e "${ASSERT_GREEN}PASS${ASSERT_NC}" -} - -assert_fail() { - echo -e "${ASSERT_RED}FAIL${ASSERT_NC}" -} - -assert_s3_bucket_exists() { - local bucket="$1" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} S3 bucket '${bucket}' exists ... " - if aws_local s3api head-bucket --bucket "$bucket" >/dev/null 2>&1; then - assert_pass - return 0 - else - assert_fail - fail "S3 bucket does not exist: $bucket" - return 1 - fi -} - -assert_s3_bucket_not_exists() { - local bucket="$1" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} S3 bucket '${bucket}' does not exist ... " - if aws_local s3api head-bucket --bucket "$bucket" >/dev/null 2>&1; then - assert_fail - fail "S3 bucket should not exist: $bucket" - return 1 - else - assert_pass - return 0 - fi -} - -assert_cloudfront_distribution_exists() { - local comment="$1" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} CloudFront distribution '${comment}' exists ... " - # CloudFront uses Moto endpoint, not LocalStack - local distribution - distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ - --query "DistributionList.Items[?Comment=='$comment'].Id" \ - --output text 2>/dev/null) - - if [ -n "$distribution" ] && [ "$distribution" != "None" ]; then - assert_pass - return 0 - else - assert_fail - fail "CloudFront distribution does not exist with comment: $comment" - return 1 - fi -} - -assert_cloudfront_distribution_not_exists() { - local comment="$1" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} CloudFront distribution '${comment}' does not exist ... " - # CloudFront uses Moto endpoint, not LocalStack - local distribution - distribution=$(aws --endpoint-url="$MOTO_ENDPOINT" --no-cli-pager cloudfront list-distributions \ - --query "DistributionList.Items[?Comment=='$comment'].Id" \ - --output text 2>/dev/null) - - if [ -z "$distribution" ] || [ "$distribution" == "None" ]; then - assert_pass - return 0 - else - assert_fail - fail "CloudFront distribution should not exist with comment: $comment" - return 1 - fi -} - -assert_route53_record_exists() { - local record_name="$1" - local record_type="$2" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} Route53 ${record_type} record '${record_name}' exists ... " - - # Ensure record name ends with a dot - [[ "$record_name" != *. ]] && record_name="$record_name." - - # Get the first hosted zone - local zone_id - zone_id=$(aws_local route53 list-hosted-zones \ - --query "HostedZones[0].Id" \ - --output text 2>/dev/null | sed 's|/hostedzone/||') - - if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then - assert_fail - fail "No Route53 hosted zones found" - return 1 - fi - - local record - record=$(aws_local route53 list-resource-record-sets \ - --hosted-zone-id "$zone_id" \ - --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ - --output text 2>/dev/null) - - if [ -n "$record" ] && [ "$record" != "None" ]; then - assert_pass - return 0 - else - assert_fail - fail "Route53 record does not exist: $record_name ($record_type) in zone $zone_id" - return 1 - fi -} - -assert_route53_record_not_exists() { - local record_name="$1" - local record_type="$2" - - echo -ne " ${ASSERT_CYAN}Assert:${ASSERT_NC} Route53 ${record_type} record '${record_name}' does not exist ... " - - # Ensure record name ends with a dot - [[ "$record_name" != *. ]] && record_name="$record_name." - - # Get the first hosted zone - local zone_id - zone_id=$(aws_local route53 list-hosted-zones \ - --query "HostedZones[0].Id" \ - --output text 2>/dev/null | sed 's|/hostedzone/||') - - if [ -z "$zone_id" ] || [ "$zone_id" == "None" ]; then - # No zones means no records, so assertion passes - assert_pass - return 0 - fi - - local record - record=$(aws_local route53 list-resource-record-sets \ - --hosted-zone-id "$zone_id" \ - --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ - --output text 2>/dev/null) - - if [ -z "$record" ] || [ "$record" == "None" ]; then - assert_pass - return 0 - else - assert_fail - fail "Route53 record should not exist: $record_name ($record_type) in zone $zone_id" - return 1 - fi -} - -# ============================================================================= -# Assertion Runner -# ============================================================================= - -run_assertions() { - local step_index="$1" - local assertions=$(get_step_assertions "$step_index") - local assertion_count=$(echo "$assertions" | jq -r 'length') - - echo "" - echo -e "${ASSERT_CYAN}Running ${assertion_count} assertion(s)${ASSERT_NC}" - echo "" - - for i in $(seq 0 $((assertion_count - 1))); do - local assertion=$(echo "$assertions" | jq -r ".[$i]") - local type=$(echo "$assertion" | jq -r '.type') - - case "$type" in - s3_bucket_exists) - local bucket=$(echo "$assertion" | jq -r '.bucket') - assert_s3_bucket_exists "$bucket" - ;; - s3_bucket_not_exists) - local bucket=$(echo "$assertion" | jq -r '.bucket') - assert_s3_bucket_not_exists "$bucket" - ;; - cloudfront_distribution_exists) - local comment=$(echo "$assertion" | jq -r '.comment') - assert_cloudfront_distribution_exists "$comment" - ;; - cloudfront_distribution_not_exists) - local comment=$(echo "$assertion" | jq -r '.comment') - assert_cloudfront_distribution_not_exists "$comment" - ;; - route53_record_exists) - local name=$(echo "$assertion" | jq -r '.name') - local record_type=$(echo "$assertion" | jq -r '.record_type') - assert_route53_record_exists "$name" "$record_type" - ;; - route53_record_not_exists) - local name=$(echo "$assertion" | jq -r '.name') - local record_type=$(echo "$assertion" | jq -r '.record_type') - assert_route53_record_not_exists "$name" "$record_type" - ;; - *) - fail "Unknown assertion type: $type" - ;; - esac - done -} - -# ============================================================================= -# Full Test Step Execution -# ============================================================================= - -run_before_commands() { - local step_index="$1" - local before_commands - before_commands=$(jq -r ".steps[$step_index].before // []" "$CURRENT_TEST_CONFIG") - local cmd_count - cmd_count=$(echo "$before_commands" | jq -r 'length') - - if [ "$cmd_count" -eq 0 ]; then - return 0 - fi - - echo "Running $cmd_count before command(s)..." - echo "" - - for i in $(seq 0 $((cmd_count - 1))); do - local cmd - cmd=$(echo "$before_commands" | jq -r ".[$i]") - echo " $ $cmd" - eval "$cmd" || true - done - - echo "" -} - -execute_test_step() { - local step_index="$1" - local step_name - step_name=$(jq -r ".steps[$step_index].name" "$CURRENT_TEST_CONFIG") - - echo "" - echo "==========================================" - echo "Step $((step_index + 1)): $step_name" - echo "==========================================" - - # Setup environment for this step - setup_test_environment "$step_index" - - # Run before commands (if any) - run_before_commands "$step_index" - - # Run the workflow - run_workflow_step "$step_index" - - # Run assertions - run_assertions "$step_index" - - echo "Step $step_name completed successfully" -} - -execute_all_steps() { - local step_count=$(jq -r '.steps | length' "$CURRENT_TEST_CONFIG") - - for i in $(seq 0 $((step_count - 1))); do - execute_test_step "$i" - done -} diff --git a/testing/docker/Dockerfile.test-runner b/testing/docker/Dockerfile.test-runner index 4323fbdb..66a6fcae 100644 --- a/testing/docker/Dockerfile.test-runner +++ b/testing/docker/Dockerfile.test-runner @@ -44,4 +44,4 @@ ENV PATH="/root/.local/bin:${PATH}" WORKDIR /workspace # Default command - run bats tests -ENTRYPOINT ["/bin/bash"] +ENTRYPOINT ["/bin/bash"] \ No newline at end of file diff --git a/testing/docker/certs/cert.pem b/testing/docker/certs/cert.pem new file mode 100644 index 00000000..4fb8ae7a --- /dev/null +++ b/testing/docker/certs/cert.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC7zCCAdegAwIBAgIJAOZGAGxa+MH3MA0GCSqGSIb3DQEBCwUAMB8xHTAbBgNV +BAMMFGFwaS5udWxscGxhdGZvcm0uY29tMB4XDTI2MDExMzE4MDUzNVoXDTI3MDEx +MzE4MDUzNVowHzEdMBsGA1UEAwwUYXBpLm51bGxwbGF0Zm9ybS5jb20wggEiMA0G +CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDFbtqvyx8oYOIUXIUIv5RzTPQXa4df +xVg1YNq1/hTv+dZaO/I5ufgJAtp9VC8kHppBV1yYUQ27c8aKVgTsl870RXjZW6Rq +nJUXnH5VGLRvnV0X8wjlBSFi3UZNM4AUB/BnILyt5XMWZaV0cOtujvbZ7Wqjt/As +Q8rDqYdeCZkIzA8SG2JiDBy1zModx/Fy2gKrC56JPO0+DnIz9NMHcefD/vPoFklf +JjuEw0z9B15Cy10tWEKQY8WKypdXIKProif9PO7VoRCML0LqTZDDBlmepsNpVQ2m +7Pbo+XnMD99DC6fRkgeoO41xzEusFRpUOOoQ0zREW8s1f0uKItUoC6fLAgMBAAGj +LjAsMCoGA1UdEQQjMCGCFGFwaS5udWxscGxhdGZvcm0uY29tgglsb2NhbGhvc3Qw +DQYJKoZIhvcNAQELBQADggEBAIvip4e/SRGUpoMQFJX3X++TaKKlC8XhDQS71ejS +Vi7X86PC0HsXpo+9pKeEJebLwXITiN55MoFqS4n07kLqTcXipFFArIqItE1B7jxb +hRKQKLNf4ASgg6CrI//QN8ELcjZ4o8j7jK/7qhcpeEluavhOEc0OmJIUHXxYrTKE +V+eMmgNGVbhwb/uL8ulazfOCGPMFU5NCKFZExllEJ8jIsJ+iJzBam8TIl7Pwj/VF +xcs+oaJlEkLg9YPif69tGQGosqF2+IfXBx2ckIVC3//a2/2ZVG3oUCEsxyiEz7DY +m0wi8yBCC3R1yoQj4A+/gWOeRJoinyNkOQbgciV6fJGnuWs= +-----END CERTIFICATE----- diff --git a/testing/docker/certs/key.pem b/testing/docker/certs/key.pem new file mode 100644 index 00000000..4e8cdc37 --- /dev/null +++ b/testing/docker/certs/key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAxW7ar8sfKGDiFFyFCL+Uc0z0F2uHX8VYNWDatf4U7/nWWjvy +Obn4CQLafVQvJB6aQVdcmFENu3PGilYE7JfO9EV42VukapyVF5x+VRi0b51dF/MI +5QUhYt1GTTOAFAfwZyC8reVzFmWldHDrbo722e1qo7fwLEPKw6mHXgmZCMwPEhti +YgwctczKHcfxctoCqwueiTztPg5yM/TTB3Hnw/7z6BZJXyY7hMNM/QdeQstdLVhC +kGPFisqXVyCj66In/Tzu1aEQjC9C6k2QwwZZnqbDaVUNpuz26Pl5zA/fQwun0ZIH +qDuNccxLrBUaVDjqENM0RFvLNX9LiiLVKAunywIDAQABAoIBAQCqUGm4UnoLZCBD +PljI5jOSj2TVuz3pwRzJ10Z0zr2TEqv15Vacs6+jXmHK0c9k0j1xdoJ7Jxi4hzRT +a8cNrhuqny+UGbko+vCmkKpukEQ/MLn5Cf+3SQi293lI6c9n+D1LSXNac+AnBwKH +A04lbpDGCEgA+ZDnLogA6ALNfWwVqCWoA6MXTPfhG7pAt20gz2hJdHBBiMDvI4fL +itSzuPxqNY/BT+/ReNj4rjzLSkICdxwvvxj812FTCjjF3wN/ixku64MtQL1NxL8R +aJJzz/h9wXpstUbNHidsIlAnSrV9mD3D+3DwjrV2cX54aCr1sKO0VxtFm3PKARG7 +YPDMtGaBAoGBAPKxlMj+5RokQYqMrPqwYTRLpffHXpE6dowxYEJwkAA5EA+rHOkk +rmeDI4te5X1J98sADw/7+gWNToJM4jzjDF1EuooVXl2YYAGQ4wMgRnGJfAvMr4ov +A1Tl+Esy90bkEjPQ+a8dCIHaLU92Xu9PfK8SwzFNqq2ePbpSj555QU/BAoGBANBC +AARaG/8okbaAbfYev7+e2NXyCgk4s788K9hj2fT3xI7QmlVlKUIFNHM95RraLwz2 +nt8pfzCppdCL6KjxapOG3Sfdx7F/HSrs0sUKaTxuG/orrLO0iAs6GZw1awphPV6X +lgApt/S7vzi715RpieS+l5GOlNp+siugJ5Lea9qLAoGAXft81ZU33Ta7Fs9BUVcq +XVkKLXjMW0sbi1C3qSLz8NIoMRkfef2VeEplYlxeXHVfewJL0vHOMYY2J+lkFxYJ +vLiX0E9UGsSeMR0NfDFsdh06p0sdk1J/ZMolq+FRtuctUVmUx0Zj+/0kXMhHHT9X +1mhapGYiOVe1Kck9Cq6EJ0ECgYBJ64NSCCHz2ZSO8NsXBycMfvd8SSMizsuOT29I +qXiNqPxNrOF6+iNA2dQaK5gMhfE5arhNgc8xmKXQdjio7rfjkXUiV2gwKmxR9imB +4wYjevnXPHVae/Pl+ENq3NMXphhAYRHPEP/IkHN5UcNdXHCjjrsB4VgYjevz7C7f +xK8HrQKBgC6Xi7NW5OjmyHpx6S+afdynEa25JVZjnXOjgoWOCwSqol6TQkoI/g/p ++NB4BkhtvP3FLChNi55FFvYNIpZw66C1mOYU5DDrF8FasnthGaQSGHxmI89ffyuI +im8d6jlvA49Ttx2Dhyx8g4RtiWolE+vEcQvFbVnHxLRLR1JVecBl +-----END RSA PRIVATE KEY----- diff --git a/testing/docker/docker-compose.integration.yml b/testing/docker/docker-compose.integration.yml index 0faeb76c..57638837 100644 --- a/testing/docker/docker-compose.integration.yml +++ b/testing/docker/docker-compose.integration.yml @@ -179,4 +179,4 @@ networks: - subnet: 172.28.0.0/16 volumes: - localstack-data: + localstack-data: \ No newline at end of file diff --git a/testing/docker/nginx.conf b/testing/docker/nginx.conf index f3940af1..a0f6e687 100644 --- a/testing/docker/nginx.conf +++ b/testing/docker/nginx.conf @@ -80,4 +80,4 @@ http { proxy_set_header X-Forwarded-Proto $scheme; } } -} +} \ No newline at end of file diff --git a/testing/integration_helpers.sh b/testing/integration_helpers.sh index c8d620e3..aae40326 100755 --- a/testing/integration_helpers.sh +++ b/testing/integration_helpers.sh @@ -69,14 +69,14 @@ integration_setup() { # Parse arguments while [[ $# -gt 0 ]]; do case $1 in - --cloud-provider) - cloud_provider="$2" - shift 2 - ;; - *) - echo -e "${INTEGRATION_RED}Unknown argument: $1${INTEGRATION_NC}" - return 1 - ;; + --cloud-provider) + cloud_provider="$2" + shift 2 + ;; + *) + echo -e "${INTEGRATION_RED}Unknown argument: $1${INTEGRATION_NC}" + return 1 + ;; esac done @@ -88,14 +88,14 @@ integration_setup() { fi case "$cloud_provider" in - aws|azure|gcp) - INTEGRATION_CLOUD_PROVIDER="$cloud_provider" - ;; - *) - echo -e "${INTEGRATION_RED}Error: Unsupported cloud provider: $cloud_provider${INTEGRATION_NC}" - echo "Supported providers: aws, azure, gcp" - return 1 - ;; + aws|azure|gcp) + INTEGRATION_CLOUD_PROVIDER="$cloud_provider" + ;; + *) + echo -e "${INTEGRATION_RED}Error: Unsupported cloud provider: $cloud_provider${INTEGRATION_NC}" + echo "Supported providers: aws, azure, gcp" + return 1 + ;; esac export INTEGRATION_CLOUD_PROVIDER @@ -111,15 +111,15 @@ integration_setup() { # Call provider-specific setup case "$INTEGRATION_CLOUD_PROVIDER" in - aws) - _setup_aws - ;; - azure) - _setup_azure - ;; - gcp) - _setup_gcp - ;; + aws) + _setup_aws + ;; + azure) + _setup_azure + ;; + gcp) + _setup_gcp + ;; esac } @@ -129,15 +129,15 @@ integration_teardown() { # Call provider-specific teardown case "$INTEGRATION_CLOUD_PROVIDER" in - aws) - _teardown_aws - ;; - azure) - _teardown_azure - ;; - gcp) - _teardown_gcp - ;; + aws) + _teardown_aws + ;; + azure) + _teardown_azure + ;; + gcp) + _teardown_gcp + ;; esac } @@ -607,7 +607,7 @@ _setup_default_mocks() { } }] EOF -) + ) curl -s -X POST "${SMOCKER_HOST}/mocks" \ -H "Content-Type: application/json" \ -d "$token_mock" >/dev/null 2>&1 @@ -921,4 +921,4 @@ ENVIRONMENT VARIABLES ================================================================================ EOF -} +} \ No newline at end of file From 36fe39cd3edde926344af9cf7e5a13c0b2c220c8 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 10:30:13 -0300 Subject: [PATCH 10/40] Add logs and better integration test setup --- frontend/deployment/build_context | 2 +- .../deployment/distribution/cloudfront/setup | 19 ++++++++------- frontend/deployment/network/route53/setup | 21 +++++++++++------ frontend/deployment/provider/aws/setup | 23 +++++++++---------- .../cloudfront_lifecycle_test.bats | 4 +++- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/frontend/deployment/build_context b/frontend/deployment/build_context index 0a9aec3f..09fbecc6 100644 --- a/frontend/deployment/build_context +++ b/frontend/deployment/build_context @@ -25,7 +25,7 @@ TOFU_VARIABLES={} tf_state_key="frontend/$namespace_slug/$application_slug/$scope_slug-$scope_id" -TOFU_INIT_VARIABLES="-backend-config=key=$tf_state_key" +TOFU_INIT_VARIABLES="${TOFU_INIT_VARIABLES:-} -backend-config=key=$tf_state_key" TOFU_MODULE_DIR="$SERVICE_PATH/output/$scope_id" if [ -n "${NP_OUTPUT_DIR:-}" ]; then diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index 64fba1ac..f6d1445d 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -1,15 +1,16 @@ #!/bin/bash -# S3 + CloudFront Hosting Setup +echo "🔍 Validating CloudFront distribution configuration..." application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) scope_id=$(echo "$CONTEXT" | jq -r .scope.id) distribution_app_name="$application_slug-$scope_slug-$scope_id" +echo " ✅ app_name=$distribution_app_name" -# Fetch bucket name from assets-repository provider -echo "🔍 Fetching assets-repository provider..." +echo "" +echo " 📡 Fetching assets-repository provider..." nrn=$(echo "$CONTEXT" | jq -r .scope.nrn) @@ -18,7 +19,7 @@ np_exit_code=$? if [ $np_exit_code -ne 0 ]; then echo "" - echo "❌ Failed to fetch assets-repository provider" + echo " ❌ Failed to fetch assets-repository provider" echo "" if echo "$asset_repository" | grep -q "not found\|no providers"; then @@ -64,7 +65,7 @@ distribution_bucket_name=$(echo "$asset_repository" | jq -r ' if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" ]; then echo "" - echo "❌ No S3 bucket found in assets-repository providers" + echo " ❌ No S3 bucket found in assets-repository providers" echo "" echo " 🤔 Found $(echo "$asset_repository" | jq '.results | length') provider(s), but none contain bucket information" echo "" @@ -80,13 +81,13 @@ if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" exit 1 fi -echo "✅ Bucket name: $distribution_bucket_name" +echo " ✅ bucket_name=$distribution_bucket_name" # S3 prefix for multi-scope bucket support # TODO: Replace with your prefix variable distribution_s3_prefix="/app" -echo "📁 S3 prefix: ${distribution_s3_prefix:-"(root)"}" +echo " ✅ s3_prefix=${distribution_s3_prefix:-"(root)"}" RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} @@ -102,7 +103,9 @@ TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ distribution_resource_tags_json: $resource_tags_json }') -echo "✅ S3 + CloudFront distribution configured" +echo "" +echo "✨ CloudFront distribution configured successfully" +echo "" # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" diff --git a/frontend/deployment/network/route53/setup b/frontend/deployment/network/route53/setup index aaf1fa93..f2436dfd 100755 --- a/frontend/deployment/network/route53/setup +++ b/frontend/deployment/network/route53/setup @@ -1,24 +1,27 @@ #!/bin/bash +echo "🔍 Validating Route53 network configuration..." + hosted_zone_id=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id // empty') if [ -z "$hosted_zone_id" ]; then - echo "❌ hosted_public_zone_id is not set in context" + echo " ❌ hosted_public_zone_id is not set in context" exit 1 fi +echo " ✅ hosted_zone_id=$hosted_zone_id" application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) -# Fetch the domain name from Route 53 hosted zone -echo "🔍 Fetching domain from Route 53 hosted zone: $hosted_zone_id" +echo "" +echo " 📡 Fetching domain from Route 53 hosted zone..." aws_output=$(aws route53 get-hosted-zone --id "$hosted_zone_id" 2>&1) aws_exit_code=$? if [ $aws_exit_code -ne 0 ]; then echo "" - echo "❌ Failed to fetch Route 53 hosted zone information" + echo " ❌ Failed to fetch Route 53 hosted zone information" echo "" if echo "$aws_output" | grep -q "NoSuchHostedZone"; then @@ -84,7 +87,7 @@ network_domain=$(echo "$aws_output" | jq -r '.HostedZone.Name' | sed 's/\.$//') if [ -z "$network_domain" ] || [ "$network_domain" = "null" ]; then echo "" - echo "❌ Failed to extract domain name from hosted zone response" + echo " ❌ Failed to extract domain name from hosted zone response" echo "" echo " 🤔 The AWS API returned successfully but the domain name could not be parsed." echo " This is unexpected - please check the hosted zone configuration." @@ -92,10 +95,14 @@ if [ -z "$network_domain" ] || [ "$network_domain" = "null" ]; then exit 1 fi -echo "✅ Domain resolved: $network_domain" +echo " ✅ domain=$network_domain" network_subdomain="$application_slug-$scope_slug" -echo "✅ Subdomain: $network_subdomain" +echo " ✅ subdomain=$network_subdomain" + +echo "" +echo "✨ Route53 network configured successfully" +echo "" TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg hosted_zone_id "$hosted_zone_id" \ diff --git a/frontend/deployment/provider/aws/setup b/frontend/deployment/provider/aws/setup index 92c63bc3..c2f96acc 100755 --- a/frontend/deployment/provider/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -1,19 +1,27 @@ #!/bin/bash +echo "🔍 Validating AWS provider configuration..." + if [ -z "${AWS_REGION:-}" ]; then - echo "✗ AWS_REGION is not set" + echo " ❌ AWS_REGION is not set" exit 1 fi +echo " ✅ AWS_REGION=$AWS_REGION" if [ -z "${TOFU_PROVIDER_BUCKET:-}" ]; then - echo "✗ TOFU_PROVIDER_BUCKET is not set" + echo " ❌ TOFU_PROVIDER_BUCKET is not set" exit 1 fi +echo " ✅ TOFU_PROVIDER_BUCKET=$TOFU_PROVIDER_BUCKET" if [ -z "${TOFU_LOCK_TABLE:-}" ]; then - echo "✗ TOFU_LOCK_TABLE is not set" + echo " ❌ TOFU_LOCK_TABLE is not set" exit 1 fi +echo " ✅ TOFU_LOCK_TABLE=$TOFU_LOCK_TABLE" + +echo "✨ AWS provider configured successfully" +echo "" RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} @@ -28,15 +36,6 @@ TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=bucket=$TOFU_PROVIDER_ TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=region=$AWS_REGION" TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=dynamodb_table=$TOFU_LOCK_TABLE" -# TODO(federico.maleh) this is necessary for the integration tests, tests should not make us change the production code -if [ -n "${AWS_ENDPOINT_URL:-}" ]; then - TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=force_path_style=true" - TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_credentials_validation=true" - TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_metadata_api_check=true" - TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=skip_region_validation=true" - TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=endpoints={s3=\"$AWS_ENDPOINT_URL\",dynamodb=\"$AWS_ENDPOINT_URL\"}" -fi - script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats index dbb3d400..b26293bb 100644 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats @@ -7,8 +7,9 @@ # ============================================================================= setup_file() { - # Load integration helpers + # Load integration helpers and initialize AWS/LocalStack configuration source "${PROJECT_ROOT}/testing/integration_helpers.sh" + integration_setup --cloud-provider aws # Clear any existing mocks clear_mocks @@ -35,6 +36,7 @@ setup_file() { teardown_file() { source "${PROJECT_ROOT}/testing/integration_helpers.sh" clear_mocks + integration_teardown } # Setup runs before each test From 06a70ec037c38b47ceacabd8b915f238df1c0557 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 12:21:23 -0300 Subject: [PATCH 11/40] fix tests + add missing acm + add missing route53 to cloudfront integration --- .../cloudfront/modules/cloudfront.tftest.hcl | 238 ++++++++++++++++-- .../distribution/cloudfront/modules/data.tf | 14 ++ .../distribution/cloudfront/modules/locals.tf | 13 +- .../distribution/cloudfront/modules/main.tf | 45 +++- .../cloudfront/modules/outputs.tf | 2 +- .../cloudfront/modules/variables.tf | 6 - frontend/deployment/do_tofu | 23 +- .../network/route53/modules/locals.tf | 3 + .../cloudfront_lifecycle_test.bats | 5 + .../tests/provider/aws/setup_test.bats | 38 --- 10 files changed, 303 insertions(+), 84 deletions(-) diff --git a/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl b/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl index 1d86902e..491d7c6f 100644 --- a/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl +++ b/frontend/deployment/distribution/cloudfront/modules/cloudfront.tftest.hcl @@ -12,13 +12,29 @@ mock_provider "aws" { bucket_regional_domain_name = "my-static-bucket.s3.us-east-1.amazonaws.com" } } + + mock_data "aws_caller_identity" { + defaults = { + account_id = "123456789012" + arn = "arn:aws:iam::123456789012:root" + user_id = "123456789012" + } + } + + mock_data "aws_acm_certificate" { + defaults = { + arn = "arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012" + id = "arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012" + } + } } variables { distribution_bucket_name = "my-static-bucket" distribution_s3_prefix = "app/scope-1" distribution_app_name = "my-app-prod" - distribution_custom_domain = null + network_full_domain = "" + network_domain = "" distribution_resource_tags_json = { Environment = "production" Application = "my-app" @@ -75,30 +91,30 @@ run "distribution_basic_configuration" { } # ============================================================================= -# Test: Distribution has no aliases when custom domain is null +# Test: Distribution has no aliases when network_full_domain is empty # ============================================================================= -run "no_aliases_without_custom_domain" { +run "no_aliases_without_network_domain" { command = plan assert { condition = length(local.distribution_aliases) == 0 - error_message = "Should have no aliases when custom_domain is null" + error_message = "Should have no aliases when network_full_domain is empty" } } # ============================================================================= -# Test: Distribution has alias when custom domain is set +# Test: Distribution has alias when network_full_domain is set # ============================================================================= -run "has_alias_with_custom_domain" { +run "has_alias_with_network_domain" { command = plan variables { - distribution_custom_domain = "cdn.example.com" + network_full_domain = "cdn.example.com" } assert { condition = length(local.distribution_aliases) == 1 - error_message = "Should have one alias when custom_domain is set" + error_message = "Should have one alias when network_full_domain is set" } assert { @@ -119,6 +135,70 @@ run "origin_id_format" { } } +# ============================================================================= +# Test: Origin path normalization - removes double slashes +# ============================================================================= +run "origin_path_normalizes_leading_slash" { + command = plan + + variables { + distribution_s3_prefix = "/app" + } + + assert { + condition = local.distribution_origin_path == "/app" + error_message = "Origin path should be '/app' not '//app'" + } +} + +# ============================================================================= +# Test: Origin path normalization - adds leading slash if missing +# ============================================================================= +run "origin_path_adds_leading_slash" { + command = plan + + variables { + distribution_s3_prefix = "app" + } + + assert { + condition = local.distribution_origin_path == "/app" + error_message = "Origin path should add leading slash" + } +} + +# ============================================================================= +# Test: Origin path normalization - handles empty prefix +# ============================================================================= +run "origin_path_handles_empty" { + command = plan + + variables { + distribution_s3_prefix = "" + } + + assert { + condition = local.distribution_origin_path == "" + error_message = "Origin path should be empty when prefix is empty" + } +} + +# ============================================================================= +# Test: Origin path normalization - trims trailing slashes +# ============================================================================= +run "origin_path_trims_trailing_slash" { + command = plan + + variables { + distribution_s3_prefix = "/app/subfolder/" + } + + assert { + condition = local.distribution_origin_path == "/app/subfolder" + error_message = "Origin path should trim trailing slashes" + } +} + # ============================================================================= # Test: Default tags include module tag # ============================================================================= @@ -226,12 +306,12 @@ run "dns_related_outputs" { } # ============================================================================= -# Test: Website URL without custom domain +# Test: Website URL without network domain # ============================================================================= -run "website_url_without_custom_domain" { +run "website_url_without_network_domain" { command = plan - # Without custom domain, URL should use CloudFront domain (known after apply) + # Without network domain, URL should use CloudFront domain (known after apply) # We can only check it starts with https:// assert { condition = startswith(output.distribution_website_url, "https://") @@ -240,13 +320,13 @@ run "website_url_without_custom_domain" { } # ============================================================================= -# Test: Website URL with custom domain +# Test: Website URL with network domain # ============================================================================= -run "website_url_with_custom_domain" { +run "website_url_with_network_domain" { command = plan variables { - distribution_custom_domain = "cdn.example.com" + network_full_domain = "cdn.example.com" } assert { @@ -254,3 +334,133 @@ run "website_url_with_custom_domain" { error_message = "distribution_website_url should be 'https://cdn.example.com'" } } + +# ============================================================================= +# Test: S3 bucket policy is created for CloudFront OAC +# ============================================================================= +run "creates_s3_bucket_policy" { + command = plan + + assert { + condition = aws_s3_bucket_policy.static.bucket == "my-static-bucket" + error_message = "Bucket policy should be attached to 'my-static-bucket'" + } +} + +# ============================================================================= +# Test: S3 bucket policy allows CloudFront service principal +# ============================================================================= +run "bucket_policy_allows_cloudfront" { + command = plan + + assert { + condition = can(jsondecode(aws_s3_bucket_policy.static.policy)) + error_message = "Bucket policy should be valid JSON" + } + + assert { + condition = jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Principal.Service == "cloudfront.amazonaws.com" + error_message = "Bucket policy should allow cloudfront.amazonaws.com service principal" + } + + assert { + condition = jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Action == "s3:GetObject" + error_message = "Bucket policy should allow s3:GetObject action" + } + + assert { + condition = jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Effect == "Allow" + error_message = "Bucket policy should have Allow effect" + } +} + +# ============================================================================= +# Test: S3 bucket policy resource scope +# ============================================================================= +run "bucket_policy_resource_scope" { + command = plan + + assert { + condition = jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Resource == "arn:aws:s3:::my-static-bucket/*" + error_message = "Bucket policy resource should be 'arn:aws:s3:::my-static-bucket/*'" + } +} + +# ============================================================================= +# Test: S3 bucket policy has distribution condition +# ============================================================================= +run "bucket_policy_has_distribution_condition" { + command = plan + + assert { + condition = can(jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Condition.StringEquals["AWS:SourceArn"]) + error_message = "Bucket policy should have AWS:SourceArn condition" + } + + assert { + condition = startswith(jsondecode(aws_s3_bucket_policy.static.policy).Statement[0].Condition.StringEquals["AWS:SourceArn"], "arn:aws:cloudfront::123456789012:distribution/") + error_message = "Bucket policy condition should reference the CloudFront distribution ARN with account 123456789012" + } +} + +# ============================================================================= +# Test: ACM certificate domain derivation +# ============================================================================= +run "acm_certificate_domain_derived_from_network_domain" { + command = plan + + variables { + network_domain = "example.com" + } + + assert { + condition = local.distribution_acm_certificate_domain == "*.example.com" + error_message = "ACM certificate domain should be '*.example.com'" + } +} + +# ============================================================================= +# Test: No ACM certificate lookup when network_domain is empty +# ============================================================================= +run "no_acm_lookup_without_network_domain" { + command = plan + + assert { + condition = local.distribution_acm_certificate_domain == "" + error_message = "ACM certificate domain should be empty when network_domain is empty" + } + + assert { + condition = local.distribution_has_acm_certificate == false + error_message = "Should not have ACM certificate when network_domain is empty" + } +} + +# ============================================================================= +# Test: Uses ACM certificate when network_domain is set +# ============================================================================= +run "uses_acm_certificate_with_network_domain" { + command = plan + + variables { + network_domain = "example.com" + network_full_domain = "app.example.com" + } + + assert { + condition = local.distribution_has_acm_certificate == true + error_message = "Should have ACM certificate when network_domain is set" + } +} + +# ============================================================================= +# Test: Uses default certificate without network_domain +# ============================================================================= +run "uses_default_certificate_without_network_domain" { + command = plan + + assert { + condition = local.distribution_has_acm_certificate == false + error_message = "Should use default certificate when network_domain is empty" + } +} diff --git a/frontend/deployment/distribution/cloudfront/modules/data.tf b/frontend/deployment/distribution/cloudfront/modules/data.tf index 9d4b0475..075a9425 100644 --- a/frontend/deployment/distribution/cloudfront/modules/data.tf +++ b/frontend/deployment/distribution/cloudfront/modules/data.tf @@ -1,3 +1,17 @@ data "aws_s3_bucket" "static" { bucket = var.distribution_bucket_name } + +data "aws_caller_identity" "current" {} + +# Look up ACM certificate for custom domain (must be in us-east-1 for CloudFront) +# Uses wildcard pattern: *.parent-domain.tld +# Note: PENDING_VALIDATION is included for LocalStack compatibility in integration tests +data "aws_acm_certificate" "custom_domain" { + count = local.distribution_acm_certificate_domain != "" ? 1 : 0 + + provider = aws + domain = local.distribution_acm_certificate_domain + statuses = ["ISSUED", "PENDING_VALIDATION"] + most_recent = true +} diff --git a/frontend/deployment/distribution/cloudfront/modules/locals.tf b/frontend/deployment/distribution/cloudfront/modules/locals.tf index abac0de8..2a908144 100644 --- a/frontend/deployment/distribution/cloudfront/modules/locals.tf +++ b/frontend/deployment/distribution/cloudfront/modules/locals.tf @@ -1,6 +1,17 @@ locals { distribution_origin_id = "S3-${var.distribution_bucket_name}" - distribution_aliases = var.distribution_custom_domain != null ? [var.distribution_custom_domain] : [] + + # Use network_full_domain from network layer (provided via cross-module locals when composed) + distribution_aliases = local.network_full_domain != "" ? [local.network_full_domain] : [] + + # Normalize s3_prefix: trim leading/trailing slashes, then add single leading slash if non-empty + distribution_s3_prefix_trimmed = trim(var.distribution_s3_prefix, "/") + distribution_origin_path = local.distribution_s3_prefix_trimmed != "" ? "/${local.distribution_s3_prefix_trimmed}" : "" + + # ACM certificate domain: derive wildcard from network_domain + # e.g., "example.com" -> "*.example.com" + distribution_acm_certificate_domain = local.network_domain != "" ? "*.${local.network_domain}" : "" + distribution_has_acm_certificate = length(data.aws_acm_certificate.custom_domain) > 0 distribution_default_tags = merge(var.distribution_resource_tags_json, { ManagedBy = "terraform" diff --git a/frontend/deployment/distribution/cloudfront/modules/main.tf b/frontend/deployment/distribution/cloudfront/modules/main.tf index 6bc652e9..feb3b9a1 100644 --- a/frontend/deployment/distribution/cloudfront/modules/main.tf +++ b/frontend/deployment/distribution/cloudfront/modules/main.tf @@ -6,6 +6,30 @@ resource "aws_cloudfront_origin_access_control" "static" { signing_protocol = "sigv4" } +resource "aws_s3_bucket_policy" "static" { + bucket = data.aws_s3_bucket.static.id + + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Sid = "AllowCloudFrontServicePrincipalReadOnly" + Effect = "Allow" + Principal = { + Service = "cloudfront.amazonaws.com" + } + Action = "s3:GetObject" + Resource = "${data.aws_s3_bucket.static.arn}/*" + Condition = { + StringEquals = { + "AWS:SourceArn" = "arn:aws:cloudfront::${data.aws_caller_identity.current.account_id}:distribution/${aws_cloudfront_distribution.static.id}" + } + } + } + ] + }) +} + resource "aws_cloudfront_distribution" "static" { enabled = true is_ipv6_enabled = true @@ -19,7 +43,7 @@ resource "aws_cloudfront_distribution" "static" { origin_id = local.distribution_origin_id origin_access_control_id = aws_cloudfront_origin_access_control.static.id - origin_path = var.distribution_s3_prefix != "" ? "/${var.distribution_s3_prefix}" : "" + origin_path = local.distribution_origin_path } default_cache_behavior { @@ -79,9 +103,22 @@ resource "aws_cloudfront_distribution" "static" { } } - viewer_certificate { - cloudfront_default_certificate = true - minimum_protocol_version = "TLSv1.2_2021" + # Use ACM certificate if available for custom domain, otherwise use default CloudFront certificate + dynamic "viewer_certificate" { + for_each = local.distribution_has_acm_certificate ? [1] : [] + content { + acm_certificate_arn = data.aws_acm_certificate.custom_domain[0].arn + ssl_support_method = "sni-only" + minimum_protocol_version = "TLSv1.2_2021" + } + } + + dynamic "viewer_certificate" { + for_each = local.distribution_has_acm_certificate ? [] : [1] + content { + cloudfront_default_certificate = true + minimum_protocol_version = "TLSv1.2_2021" + } } tags = local.distribution_default_tags diff --git a/frontend/deployment/distribution/cloudfront/modules/outputs.tf b/frontend/deployment/distribution/cloudfront/modules/outputs.tf index 3942d91f..dd5cbba9 100644 --- a/frontend/deployment/distribution/cloudfront/modules/outputs.tf +++ b/frontend/deployment/distribution/cloudfront/modules/outputs.tf @@ -40,5 +40,5 @@ output "distribution_record_type" { output "distribution_website_url" { description = "Website URL" - value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" + value = local.network_full_domain != "" ? "https://${local.network_full_domain}" : "https://${aws_cloudfront_distribution.static.domain_name}" } diff --git a/frontend/deployment/distribution/cloudfront/modules/variables.tf b/frontend/deployment/distribution/cloudfront/modules/variables.tf index a9ef4cbc..b118bbcd 100644 --- a/frontend/deployment/distribution/cloudfront/modules/variables.tf +++ b/frontend/deployment/distribution/cloudfront/modules/variables.tf @@ -13,12 +13,6 @@ variable "distribution_app_name" { type = string } -variable "distribution_custom_domain" { - description = "Custom domain for CloudFront (optional)" - type = string - default = null -} - variable "distribution_resource_tags_json" { description = "Resource tags as JSON object" type = map(string) diff --git a/frontend/deployment/do_tofu b/frontend/deployment/do_tofu index bd91c906..6ecc3cfc 100644 --- a/frontend/deployment/do_tofu +++ b/frontend/deployment/do_tofu @@ -2,29 +2,12 @@ set -eou pipefail +TOFU_VAR_FILE="$TOFU_MODULE_DIR/.tfvars.json" +echo "$TOFU_VARIABLES" > "$TOFU_VAR_FILE" + CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") cd "$CURRENT_DIR" -#np scope patchtofu init \ -# -backend-config="bucket=${TF_STATE_BUCKET}" \ -# -backend-config="key=amplify/$APPLICATION_SLUG/$SCOPE_SLUG-$SCOPE_ID" \ -# -backend-config="region=${AWS_REGION}" \ -# -backend-config="dynamodb_table=${TF_LOCK_TABLE}" --id "$SCOPE_ID" --body "{\"domain\":\"$SUBDOMAIN.$DOMAIN\"}" -# Write variables to a temp file for tofu -TOFU_VAR_FILE="$TOFU_MODULE_DIR/.tfvars.json" -echo "$TOFU_VARIABLES" > "$TOFU_VAR_FILE" - tofu -chdir="$TOFU_MODULE_DIR" init -input=false $TOFU_INIT_VARIABLES tofu -chdir="$TOFU_MODULE_DIR" "$ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" - -#tofu $ACTION -auto-approve \ -# -var="aws_region=${AWS_REGION}" \ -# -var="github_token=${GITHUB_TOKEN}" \ -# -var="application_name=${APPLICATION_SLUG}" \ -# -var="repository_url=${REPOSITORY_URL}" \ -# -var="application_version=${APPLICATION_VERSION}" \ -# -var="env_vars_json=${ENV_VARS_JSON}" \ -# -var="resource_tags_json=${RESOURCE_TAGS_JSON}" \ -# -var="domain=${DOMAIN}" \ -# -var="subdomain=${SUBDOMAIN}" diff --git a/frontend/deployment/network/route53/modules/locals.tf b/frontend/deployment/network/route53/modules/locals.tf index d9357d81..daf83ed9 100644 --- a/frontend/deployment/network/route53/modules/locals.tf +++ b/frontend/deployment/network/route53/modules/locals.tf @@ -1,4 +1,7 @@ locals { + # Expose network_domain for cross-module use (e.g., ACM certificate lookup) + network_domain = var.network_domain + # Compute full domain from domain + subdomain network_full_domain = var.network_subdomain != "" ? "${var.network_subdomain}.${var.network_domain}" : var.network_domain } \ No newline at end of file diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats index b26293bb..4fea4541 100644 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats @@ -27,6 +27,11 @@ setup_file() { --name frontend.publicdomain.com \ --caller-reference "test-$(date +%s)" >/dev/null 2>&1 || true + # Create ACM certificate for the test domain (required for CloudFront custom domain SSL) + aws_local acm request-certificate \ + --domain-name "*.frontend.publicdomain.com" \ + --validation-method DNS >/dev/null 2>&1 || true + # Get hosted zone ID for context override HOSTED_ZONE_ID=$(aws_local route53 list-hosted-zones --query 'HostedZones[0].Id' --output text | sed 's|/hostedzone/||') diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats index 2cd3ff28..07673dc1 100644 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -35,9 +35,6 @@ setup() { export TOFU_INIT_VARIABLES="" export MODULES_TO_USE="" - - # Clear LocalStack endpoint - unset AWS_ENDPOINT_URL } # ============================================================================= @@ -157,41 +154,6 @@ run_aws_setup() { assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=bucket=my-terraform-state-bucket" } -# ============================================================================= -# Test: LocalStack/AWS_ENDPOINT_URL configuration -# ============================================================================= -@test "adds LocalStack backend config when AWS_ENDPOINT_URL is set" { - export AWS_ENDPOINT_URL="http://localhost:4566" - - run_aws_setup - - assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=force_path_style=true" - assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_credentials_validation=true" - assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_metadata_api_check=true" - assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=skip_region_validation=true" -} - -@test "includes endpoint config with LocalStack URL" { - export AWS_ENDPOINT_URL="http://localhost:4566" - - run_aws_setup - - assert_contains "$TOFU_INIT_VARIABLES" '-backend-config=endpoints={s3="http://localhost:4566",dynamodb="http://localhost:4566"}' -} - -@test "does not add LocalStack config when AWS_ENDPOINT_URL is not set" { - unset AWS_ENDPOINT_URL - - run_aws_setup - - # Should not contain LocalStack-specific configs - if [[ "$TOFU_INIT_VARIABLES" == *"force_path_style"* ]]; then - echo "TOFU_INIT_VARIABLES should not contain force_path_style when AWS_ENDPOINT_URL is not set" - echo "Actual: $TOFU_INIT_VARIABLES" - return 1 - fi -} - # ============================================================================= # Test: MODULES_TO_USE # ============================================================================= From 0b3670beb9c43823cdffe44c3f0f94ebf15dc654 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 12:22:16 -0300 Subject: [PATCH 12/40] Commit the tofu test_local files --- frontend/deployment/.gitignore | 3 +- .../cloudfront/modules/test_locals.tf | 29 +++++++++++++++ .../network/route53/modules/test_locals.tf | 36 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 frontend/deployment/distribution/cloudfront/modules/test_locals.tf create mode 100644 frontend/deployment/network/route53/modules/test_locals.tf diff --git a/frontend/deployment/.gitignore b/frontend/deployment/.gitignore index 45d70888..3a63847b 100644 --- a/frontend/deployment/.gitignore +++ b/frontend/deployment/.gitignore @@ -12,5 +12,4 @@ override.tf.json .terraformrc terraform.rc -# Test-only terraform files -**/test_*.tf + diff --git a/frontend/deployment/distribution/cloudfront/modules/test_locals.tf b/frontend/deployment/distribution/cloudfront/modules/test_locals.tf new file mode 100644 index 00000000..652d45a8 --- /dev/null +++ b/frontend/deployment/distribution/cloudfront/modules/test_locals.tf @@ -0,0 +1,29 @@ +# ============================================================================= +# Test-only locals +# +# This file provides the network_* locals that are normally defined by the +# network layer (Route53, etc.) when modules are composed. +# This file is only used for running isolated unit tests. +# +# NOTE: Files matching test_*.tf are skipped by compose_modules +# ============================================================================= + +# Test-only variables to allow tests to control the network values +variable "network_full_domain" { + description = "Test-only: Full domain from network layer (e.g., app.example.com)" + type = string + default = "" +} + +variable "network_domain" { + description = "Test-only: Root domain from network layer (e.g., example.com)" + type = string + default = "" +} + +locals { + # These locals are normally provided by network modules (e.g., Route53) + # For testing, we bridge from variables to locals + network_full_domain = var.network_full_domain + network_domain = var.network_domain +} diff --git a/frontend/deployment/network/route53/modules/test_locals.tf b/frontend/deployment/network/route53/modules/test_locals.tf new file mode 100644 index 00000000..5120b86e --- /dev/null +++ b/frontend/deployment/network/route53/modules/test_locals.tf @@ -0,0 +1,36 @@ +# ============================================================================= +# Test-only locals +# +# This file provides the distribution_* locals that are normally defined by the +# distribution layer (CloudFront, Amplify, etc.) when modules are composed. +# This file is only used for running isolated unit tests. +# +# NOTE: Files matching test_*.tf are skipped by compose_modules +# ============================================================================= + +# Test-only variables to allow tests to control the hosting values +variable "distribution_target_domain" { + description = "Test-only: Target domain from hosting provider" + type = string + default = "d1234567890.cloudfront.net" +} + +variable "distribution_target_zone_id" { + description = "Test-only: Hosted zone ID from hosting provider" + type = string + default = "Z2FDTNDATAQYW2" +} + +variable "distribution_record_type" { + description = "Test-only: DNS record type (A or CNAME)" + type = string + default = "A" +} + +locals { + # These locals are normally provided by distribution modules (e.g., CloudFront) + # For testing, we bridge from variables to locals + distribution_target_domain = var.distribution_target_domain + distribution_target_zone_id = var.distribution_target_zone_id + distribution_record_type = var.distribution_record_type +} From e9b62bbe967311c747af2d560c12af8f9fa08cc0 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 12:29:41 -0300 Subject: [PATCH 13/40] Add descriptions to channels --- frontend/specs/notification-channel.json.tpl | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/specs/notification-channel.json.tpl b/frontend/specs/notification-channel.json.tpl index ee3c7986..9b05e149 100644 --- a/frontend/specs/notification-channel.json.tpl +++ b/frontend/specs/notification-channel.json.tpl @@ -1,6 +1,7 @@ { "nrn": "{{ env.Getenv "NRN" }}", "status": "active", + "description": "Channel to handle frontend scopes", "type": "agent", "source": [ "telemetry", From b550a95e05ff2761ec1a421440ce5ed549e872f6 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 12:31:18 -0300 Subject: [PATCH 14/40] Improve scope selectors --- frontend/specs/notification-channel.json.tpl | 2 +- frontend/specs/scope-type-definition.json.tpl | 4 ++-- frontend/specs/service-spec.json.tpl | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/frontend/specs/notification-channel.json.tpl b/frontend/specs/notification-channel.json.tpl index 9b05e149..53f217f2 100644 --- a/frontend/specs/notification-channel.json.tpl +++ b/frontend/specs/notification-channel.json.tpl @@ -1,7 +1,7 @@ { "nrn": "{{ env.Getenv "NRN" }}", "status": "active", - "description": "Channel to handle frontend scopes", + "description": "Channel to handle static files scopes", "type": "agent", "source": [ "telemetry", diff --git a/frontend/specs/scope-type-definition.json.tpl b/frontend/specs/scope-type-definition.json.tpl index 2d3be235..122fff1d 100644 --- a/frontend/specs/scope-type-definition.json.tpl +++ b/frontend/specs/scope-type-definition.json.tpl @@ -1,6 +1,6 @@ { - "description": "Allows you to deploy frontend applications", - "name": "Frontends", + "description": "Allows you to deploy static files applications", + "name": "Static files", "nrn": "{{ env.Getenv "NRN" }}", "provider_id": "{{ env.Getenv "SERVICE_SPECIFICATION_ID" }}", "provider_type": "service", diff --git a/frontend/specs/service-spec.json.tpl b/frontend/specs/service-spec.json.tpl index 6d542df1..5e8f0a68 100644 --- a/frontend/specs/service-spec.json.tpl +++ b/frontend/specs/service-spec.json.tpl @@ -18,13 +18,13 @@ "values": {} }, "dimensions": {}, - "name": "Frontend", + "name": "Static files", "scopes": {}, "selectors": { "category": "Scope", "imported": false, - "provider": "AWS", - "sub_category": "Frontend" + "provider": "Agent", + "sub_category": "Static files" }, "type": "scope", "use_default_actions": false, From 5f4cc96a039a2de47bd9344957d8671b73a8e98a Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 13:00:20 -0300 Subject: [PATCH 15/40] Use s3 path from asset --- delete-scopes.sh | 135 ++++++++++++++++++ .../deployment/distribution/cloudfront/setup | 6 +- .../distribution/cloudfront/setup_test.bats | 38 ++++- 3 files changed, 175 insertions(+), 4 deletions(-) create mode 100755 delete-scopes.sh diff --git a/delete-scopes.sh b/delete-scopes.sh new file mode 100755 index 00000000..5d7c4cf2 --- /dev/null +++ b/delete-scopes.sh @@ -0,0 +1,135 @@ +#!/bin/bash +account_id="" +scope_type_name="" +dry_run=false + +while [[ $# -gt 0 ]]; do + case $1 in + --account-id) account_id="$2"; shift ;; + --scope-type-name) scope_type_name="$2"; shift ;; + --dry-run) dry_run=true ;; + esac + shift +done + +# Color codes +BLUE='\033[0;34m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +GRAY='\033[0;90m' +BOLD='\033[1m' +NC='\033[0m' # No Color + +echo -e "\n${BOLD}═══════════════════════════════════════════════════════════${NC}" +echo -e "${BOLD} Scope Deletion Tool${NC}" +echo -e "${BOLD}═══════════════════════════════════════════════════════════${NC}\n" + +# Validate account_id +if [[ -z "$account_id" ]]; then + echo -e "${RED}✗ Missing --account-id flag${NC}" + exit 1 +else + echo -e "${GREEN}✓${NC} Found account-id: ${BOLD}$account_id${NC}" +fi + +# Validate scope_type_name +if [[ -z "$scope_type_name" ]]; then + echo -e "${RED}✗ Missing --scope-type-name flag${NC}" + echo " You must use the name it appears on the ui (it is not a slug)" + exit 1 +else + echo -e "${GREEN}✓${NC} Found scope-type-name: ${BOLD}$scope_type_name${NC}" +fi + +# Show dry-run mode if enabled +if [[ "$dry_run" == true ]]; then + echo -e "${YELLOW}⚠${NC} Running in ${BOLD}DRY RUN${NC} mode - no changes will be made" +fi + +echo -e "\n${BLUE}→${NC} Deleting all '${BOLD}$scope_type_name${NC}' scopes in account '${BOLD}$account_id${NC}'" + +account_nrn=$(np account read --id "$account_id" --format json | jq -r .nrn) +echo -e "${GRAY} Looking for scope type in nrn=$account_nrn${NC}" + +scope_type=$(np scope type list --nrn "$account_nrn" --name "$scope_type_name" --format json | jq ".results[0]") +if [[ $scope_type == "null" ]]; then + echo -e "${RED}✗ No scope type with name=$scope_type_name and nrn=$account_nrn${NC}" + exit 1 +fi + +scope_type_id=$(echo $scope_type | jq -r .id) +service_id=$(echo $scope_type | jq -r .provider_id) +echo -e "${GREEN}✓${NC} Found scope type ${GRAY}[id=$scope_type_id, provider_id=$service_id]${NC}\n" + +namespaces=$(np namespace list --account_id "$account_id" --format json) + +# Counter for dry-run +total_scopes=0 + +# Iterate through namespaces using process substitution instead of pipe +while IFS='|' read -r namespace_id namespace_name; do + echo -e "\n${BLUE}📦 Namespace:${NC} ${BOLD}$namespace_name${NC} ${GRAY}(id=$namespace_id)${NC}" + + # Get applications for this namespace + applications=$(np application list --namespace_id "$namespace_id" --format json) + + # Check if there are any applications + app_count=$(echo "$applications" | jq -r '.results | length') + if [[ $app_count -eq 0 ]]; then + echo -e " ${GRAY}└─ No applications in this namespace${NC}" + continue + fi + + # Iterate through applications + while IFS='|' read -r application_id application_name; do + echo -e " ${BLUE}├─ Application:${NC} ${BOLD}$application_name${NC} ${GRAY}(id=$application_id)${NC}" + + # Get scopes for this application + scopes=$(np scope list --application_id "$application_id" --format json) + + # Check if there are any matching scopes + matching_scopes=$(echo "$scopes" | jq -r --arg service_id "$service_id" \ + '.results[] | select(.status != "deleted" and .provider == $service_id) | "\(.id)|\(.name)"') + + if [[ -z "$matching_scopes" ]]; then + echo -e " ${GRAY}│ └─ No matching scopes${NC}" + continue + fi + + # Filter and iterate through scopes + while IFS='|' read -r scope_id scope_name; do + echo -e " ${BLUE}│ ├─ Scope:${NC} ${BOLD}$scope_name${NC} ${GRAY}(id=$scope_id)${NC}" + + if [[ "$dry_run" == true ]]; then + echo -e " ${YELLOW}│ └─ ⚠ Would be processed (dry run)${NC}" + ((total_scopes++)) + else + # Execute all three commands independently + echo -e " ${GRAY}│ │ ├─ Setting status to 'deleting'...${NC}" + np scope patch --id "$scope_id" --body '{"status": "deleting"}' > /dev/null 2>&1 + + echo -e " ${GRAY}│ │ ├─ Setting status to 'failed'...${NC}" + np scope patch --id "$scope_id" --body '{"status": "failed"}' > /dev/null 2>&1 + + echo -e " ${GRAY}│ │ └─ Force deleting scope...${NC}" + np scope delete --id "$scope_id" --force > /dev/null 2>&1 + + if [[ $? -eq 0 ]]; then + echo -e " ${GREEN}│ └─ ✓ Successfully processed${NC}" + ((total_scopes++)) + else + echo -e " ${RED}│ └─ ✗ Failed to process${NC}" + fi + fi + done < <(echo "$matching_scopes") + done < <(echo "$applications" | jq -r '.results[] | "\(.id)|\(.name)"') +done < <(echo "$namespaces" | jq -r '.results[] | "\(.id)|\(.name)"') + +echo -e "\n${BOLD}═══════════════════════════════════════════════════════════${NC}" +if [[ "$dry_run" == true ]]; then + echo -e "${YELLOW}⚠${NC} Dry run completed - found ${BOLD}$total_scopes${NC} scope(s) to delete - no changes were made" +else + echo -e "${GREEN}✓${NC} Process completed - ${BOLD}$total_scopes${NC} scope(s) processed" +fi +echo -e "${BOLD}═══════════════════════════════════════════════════════════${NC}\n" diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index f6d1445d..eab3966c 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -5,6 +5,7 @@ echo "🔍 Validating CloudFront distribution configuration..." application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) scope_id=$(echo "$CONTEXT" | jq -r .scope.id) +asset_url=$(echo "$CONTEXT" | jq -r .asset.url) distribution_app_name="$application_slug-$scope_slug-$scope_id" echo " ✅ app_name=$distribution_app_name" @@ -84,8 +85,9 @@ fi echo " ✅ bucket_name=$distribution_bucket_name" # S3 prefix for multi-scope bucket support -# TODO: Replace with your prefix variable -distribution_s3_prefix="/app" +# Extract path from asset_url (format: s3://bucket-name/path/to/assets) +# Removes "s3://bucket-name" prefix, keeps the path with leading slash +distribution_s3_prefix="/${asset_url#s3://*/}" echo " ✅ s3_prefix=${distribution_s3_prefix:-"(root)"}" diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index 7855cb24..77320a89 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -68,7 +68,7 @@ set_np_mock() { "distribution_bucket_name": "assets-bucket", "distribution_app_name": "automation-development-tools-7", "distribution_resource_tags_json": {}, - "distribution_s3_prefix": "/app" + "distribution_s3_prefix": "/tools/automation/v1.0.0" }' assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" @@ -170,8 +170,42 @@ set_np_mock() { "distribution_bucket_name": "assets-bucket", "distribution_app_name": "automation-development-tools-7", "distribution_resource_tags_json": {"Environment": "production", "Team": "platform"}, - "distribution_s3_prefix": "/app" + "distribution_s3_prefix": "/tools/automation/v1.0.0" }' assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } + +# ============================================================================= +# Test: S3 prefix extraction from asset URL +# ============================================================================= +@test "extracts s3_prefix from asset.url" { + set_np_mock "success.json" + + run_cloudfront_setup + + local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') + assert_equal "$s3_prefix" "/tools/automation/v1.0.0" +} + +@test "extracts s3_prefix correctly for different asset URL paths" { + set_np_mock "success.json" + # Override asset.url in context + export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "s3://other-bucket/app/builds/latest"') + + run_cloudfront_setup + + local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') + assert_equal "$s3_prefix" "/app/builds/latest" +} + +@test "extracts s3_prefix with single path segment" { + set_np_mock "success.json" + # Override asset.url in context + export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "s3://bucket/assets"') + + run_cloudfront_setup + + local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') + assert_equal "$s3_prefix" "/assets" +} From 14273d1d0c3434a00659596029f9cdc403e935bb Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 13:29:10 -0300 Subject: [PATCH 16/40] Fix no op operation --- frontend/deployment/workflows/finalize.yaml | 2 +- frontend/deployment/workflows/rollback.yaml | 2 +- frontend/scope/workflows/create.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/deployment/workflows/finalize.yaml b/frontend/deployment/workflows/finalize.yaml index 7297d551..3e1ffbdb 100644 --- a/frontend/deployment/workflows/finalize.yaml +++ b/frontend/deployment/workflows/finalize.yaml @@ -1,4 +1,4 @@ steps: - name: no_op type: command - command: echo "No action needed to create scope" \ No newline at end of file + command: "$SERVICE_PATH/no_op" \ No newline at end of file diff --git a/frontend/deployment/workflows/rollback.yaml b/frontend/deployment/workflows/rollback.yaml index 7297d551..3e1ffbdb 100644 --- a/frontend/deployment/workflows/rollback.yaml +++ b/frontend/deployment/workflows/rollback.yaml @@ -1,4 +1,4 @@ steps: - name: no_op type: command - command: echo "No action needed to create scope" \ No newline at end of file + command: "$SERVICE_PATH/no_op" \ No newline at end of file diff --git a/frontend/scope/workflows/create.yaml b/frontend/scope/workflows/create.yaml index 1353c84c..3e1ffbdb 100644 --- a/frontend/scope/workflows/create.yaml +++ b/frontend/scope/workflows/create.yaml @@ -1,4 +1,4 @@ steps: - name: no_op type: command - command: "$SERVICE_PATH/scope/no_op" \ No newline at end of file + command: "$SERVICE_PATH/no_op" \ No newline at end of file From 00e69816f98c71b809788701cb48a389a5598747 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 13:29:56 -0300 Subject: [PATCH 17/40] Fix no op operation --- frontend/no_op | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 frontend/no_op diff --git a/frontend/no_op b/frontend/no_op old mode 100644 new mode 100755 From 0e4921f514fc11a04da83bd406027df0b70d459d Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 13:44:58 -0300 Subject: [PATCH 18/40] Handle s3 urls through http --- frontend/deployment/distribution/cloudfront/setup | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index eab3966c..c880ad8e 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -85,9 +85,13 @@ fi echo " ✅ bucket_name=$distribution_bucket_name" # S3 prefix for multi-scope bucket support -# Extract path from asset_url (format: s3://bucket-name/path/to/assets) -# Removes "s3://bucket-name" prefix, keeps the path with leading slash -distribution_s3_prefix="/${asset_url#s3://*/}" +# Extract path from asset_url (supports both s3:// and https:// formats) +# Removes protocol and bucket/domain prefix, keeps the path with leading slash +if [[ "$asset_url" == s3://* ]]; then + distribution_s3_prefix="/${asset_url#s3://*/}" +else + distribution_s3_prefix="/${asset_url#https://*/}" +fi echo " ✅ s3_prefix=${distribution_s3_prefix:-"(root)"}" From 2ce28fedb894c0473174cceaf62cca61dcffaa2a Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 14:17:15 -0300 Subject: [PATCH 19/40] Set domain in scope --- frontend/deployment/network/route53/setup | 35 ++++++++++ .../cloudfront_lifecycle_test.bats | 11 ++-- .../tests/integration/mocks/scope/patch.json | 3 + .../tests/network/route53/setup_test.bats | 66 +++++++++++++++++-- .../deployment/tests/resources/np_mocks/np | 31 ++++++--- .../np_mocks/scope/patch/auth_error.json | 3 + .../np_mocks/scope/patch/success.json | 3 + 7 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 frontend/deployment/tests/integration/mocks/scope/patch.json create mode 100644 frontend/deployment/tests/resources/np_mocks/scope/patch/auth_error.json create mode 100644 frontend/deployment/tests/resources/np_mocks/scope/patch/success.json diff --git a/frontend/deployment/network/route53/setup b/frontend/deployment/network/route53/setup index f2436dfd..3a57275a 100755 --- a/frontend/deployment/network/route53/setup +++ b/frontend/deployment/network/route53/setup @@ -12,6 +12,7 @@ echo " ✅ hosted_zone_id=$hosted_zone_id" application_slug=$(echo "$CONTEXT" | jq -r .application.slug) scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +scope_id=$(echo "$CONTEXT" | jq -r .scope.id) echo "" echo " 📡 Fetching domain from Route 53 hosted zone..." @@ -114,6 +115,40 @@ TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ network_subdomain: $subdomain }') +scope_domain="$network_subdomain.$network_domain" + +echo " 📝 Setting scope domain to '$scope_domain'..." + +np_output=$(np scope patch --id "$scope_id" --body "{\"domain\":\"$scope_domain\"}" --format json 2>&1) +np_exit_code=$? + +if [ $np_exit_code -ne 0 ]; then + echo "" + echo " ❌ Failed to update scope domain" + echo "" + + if echo "$np_output" | grep -q "unauthorized\|forbidden\|401\|403"; then + echo " 🔒 Error: Permission denied" + echo "" + echo " 💡 Possible causes:" + echo " • The API token doesn't have permission to update scopes" + echo " • The token has expired" + echo "" + echo " 🔧 How to fix:" + echo " 1. Check your NP_API_KEY is set and valid" + echo " 2. Ensure you have permissions to update scopes" + + else + echo " 📋 Error details:" + echo "$np_output" | sed 's/^/ /' + fi + + echo "" + exit 1 +fi + +echo " ✅ Scope domain set successfully" + # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats index 4fea4541..d9f37107 100644 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats @@ -77,13 +77,14 @@ setup() { @test "create infrastructure deploys S3, CloudFront, and Route53 resources" { # Setup API mocks for np CLI calls # Note: /token is automatically mocked by clear_mocks() - local mocks_dir="frontend/deployment/tests/integration/mocks/asset_repository" + local mocks_dir="frontend/deployment/tests/integration/mocks/" # Mock the np CLI internal API calls - mock_request "GET" "/category" "$mocks_dir/category.json" - mock_request "GET" "/provider_specification" "$mocks_dir/list_provider_spec.json" - mock_request "GET" "/provider" "$mocks_dir/list_provider.json" - mock_request "GET" "/provider/s3-asset-repository-id" "$mocks_dir/get_provider.json" + mock_request "GET" "/category" "$mocks_dir/asset_repository/category.json" + mock_request "GET" "/provider_specification" "$mocks_dir/asset_repository/list_provider_spec.json" + mock_request "GET" "/provider" "$mocks_dir/asset_repository/list_provider.json" + mock_request "GET" "/provider/s3-asset-repository-id" "$mocks_dir/asset_repository/get_provider.json" + mock_request "PATCH" "/scope/7" "$mocks_dir/scope/patch.json" # Run the initial workflow run_workflow "frontend/deployment/workflows/initial.yaml" diff --git a/frontend/deployment/tests/integration/mocks/scope/patch.json b/frontend/deployment/tests/integration/mocks/scope/patch.json new file mode 100644 index 00000000..28e7be11 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/scope/patch.json @@ -0,0 +1,3 @@ +{ + "success": true +} \ No newline at end of file diff --git a/frontend/deployment/tests/network/route53/setup_test.bats b/frontend/deployment/tests/network/route53/setup_test.bats index b8e46f15..715d7e83 100644 --- a/frontend/deployment/tests/network/route53/setup_test.bats +++ b/frontend/deployment/tests/network/route53/setup_test.bats @@ -17,18 +17,19 @@ setup() { PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" SCRIPT_PATH="$PROJECT_DIR/network/route53/setup" RESOURCES_DIR="$PROJECT_DIR/tests/resources" - MOCKS_DIR="$RESOURCES_DIR/aws_mocks" + AWS_MOCKS_DIR="$RESOURCES_DIR/aws_mocks" + NP_MOCKS_DIR="$RESOURCES_DIR/np_mocks" # Load shared test utilities source "$PROJECT_ROOT/testing/assertions.sh" - # Add mock aws to PATH (must be first) - export PATH="$MOCKS_DIR:$PATH" + # Add mock aws and np to PATH (must be first) + export PATH="$AWS_MOCKS_DIR:$NP_MOCKS_DIR:$PATH" # Load context with hosted_public_zone_id export CONTEXT='{ "application": {"slug": "automation"}, - "scope": {"slug": "development-tools"}, + "scope": {"slug": "development-tools", "id": "7"}, "providers": { "cloud-providers": { "networking": { @@ -46,6 +47,10 @@ setup() { }' export MODULES_TO_USE="" + + # Set default np scope patch mock (success) + export NP_MOCK_SCOPE_PATCH_RESPONSE="$NP_MOCKS_DIR/scope/patch/success.json" + export NP_MOCK_SCOPE_PATCH_EXIT_CODE="0" } # ============================================================================= @@ -58,10 +63,17 @@ run_route53_setup() { set_aws_mock() { local mock_file="$1" local exit_code="${2:-0}" - export AWS_MOCK_RESPONSE="$MOCKS_DIR/route53/$mock_file" + export AWS_MOCK_RESPONSE="$AWS_MOCKS_DIR/route53/$mock_file" export AWS_MOCK_EXIT_CODE="$exit_code" } +set_np_scope_patch_mock() { + local mock_file="$1" + local exit_code="${2:-0}" + export NP_MOCK_SCOPE_PATCH_RESPONSE="$NP_MOCKS_DIR/scope/patch/$mock_file" + export NP_MOCK_SCOPE_PATCH_EXIT_CODE="$exit_code" +} + # ============================================================================= # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= @@ -214,3 +226,47 @@ set_aws_mock() { assert_equal "$status" "1" assert_contains "$output" "Failed to extract domain name from hosted zone response" } + +# ============================================================================= +# Test: Scope patch success +# ============================================================================= +@test "shows setting scope domain message" { + set_aws_mock "success.json" + set_np_scope_patch_mock "success.json" + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Setting scope domain" +} + +@test "shows scope domain set successfully message" { + set_aws_mock "success.json" + set_np_scope_patch_mock "success.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "Scope domain set successfully" +} + +# ============================================================================= +# Test: Scope patch auth error +# ============================================================================= +@test "fails when scope patch returns auth error" { + set_aws_mock "success.json" + set_np_scope_patch_mock "auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "Failed to update scope domain" +} + +@test "shows permission denied message for scope patch auth error" { + set_aws_mock "success.json" + set_np_scope_patch_mock "auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" "Permission denied" +} diff --git a/frontend/deployment/tests/resources/np_mocks/np b/frontend/deployment/tests/resources/np_mocks/np index 195c6220..24bfd747 100755 --- a/frontend/deployment/tests/resources/np_mocks/np +++ b/frontend/deployment/tests/resources/np_mocks/np @@ -1,18 +1,33 @@ #!/bin/bash # Mock np CLI for testing -# Set NP_MOCK_RESPONSE to the path of the mock file to return -# Set NP_MOCK_EXIT_CODE to the exit code (default: 0) +# +# Supports command-specific mocks: +# - NP_MOCK_SCOPE_PATCH_RESPONSE / NP_MOCK_SCOPE_PATCH_EXIT_CODE for "np scope patch" +# - NP_MOCK_PROVIDER_LIST_RESPONSE / NP_MOCK_PROVIDER_LIST_EXIT_CODE for "np provider list" +# - NP_MOCK_RESPONSE / NP_MOCK_EXIT_CODE as fallback for any command -if [ -z "$NP_MOCK_RESPONSE" ]; then - echo "NP_MOCK_RESPONSE not set" >&2 +# Detect which command is being called +if [[ "$1" == "scope" && "$2" == "patch" ]]; then + MOCK_RESPONSE="${NP_MOCK_SCOPE_PATCH_RESPONSE:-$NP_MOCK_RESPONSE}" + MOCK_EXIT_CODE="${NP_MOCK_SCOPE_PATCH_EXIT_CODE:-${NP_MOCK_EXIT_CODE:-0}}" +elif [[ "$1" == "provider" && "$2" == "list" ]]; then + MOCK_RESPONSE="${NP_MOCK_PROVIDER_LIST_RESPONSE:-$NP_MOCK_RESPONSE}" + MOCK_EXIT_CODE="${NP_MOCK_PROVIDER_LIST_EXIT_CODE:-${NP_MOCK_EXIT_CODE:-0}}" +else + MOCK_RESPONSE="$NP_MOCK_RESPONSE" + MOCK_EXIT_CODE="${NP_MOCK_EXIT_CODE:-0}" +fi + +if [ -z "$MOCK_RESPONSE" ]; then + echo "No mock response configured for: np $*" >&2 exit 1 fi -if [ -f "$NP_MOCK_RESPONSE" ]; then - cat "$NP_MOCK_RESPONSE" +if [ -f "$MOCK_RESPONSE" ]; then + cat "$MOCK_RESPONSE" else - echo "Mock file not found: $NP_MOCK_RESPONSE" >&2 + echo "Mock file not found: $MOCK_RESPONSE" >&2 exit 1 fi -exit "${NP_MOCK_EXIT_CODE:-0}" +exit "$MOCK_EXIT_CODE" diff --git a/frontend/deployment/tests/resources/np_mocks/scope/patch/auth_error.json b/frontend/deployment/tests/resources/np_mocks/scope/patch/auth_error.json new file mode 100644 index 00000000..1935b96c --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/scope/patch/auth_error.json @@ -0,0 +1,3 @@ +{ + "error": "scope write error: request failed with status 403: {\"statusCode\":403,\"code\":\"FST_ERR_AUTHORIZATION\",\"error\":\"Forbidden\",\"message\":\"Authorization error, insufficient permissions to access the requested resource. Insufficient permissions to access the requested resource\"}" +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/scope/patch/success.json b/frontend/deployment/tests/resources/np_mocks/scope/patch/success.json new file mode 100644 index 00000000..28e7be11 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/scope/patch/success.json @@ -0,0 +1,3 @@ +{ + "success": true +} \ No newline at end of file From 70dc750f171fca3e6e7bfe5047fc090a6319f2d3 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 14 Jan 2026 15:13:45 -0300 Subject: [PATCH 20/40] Add cloudfront cache invalidation --- frontend/deployment/.gitignore | 4 -- .../distribution/cloudfront/modules/locals.tf | 3 +- .../distribution/cloudfront/modules/main.tf | 14 +++++++ .../cloudfront/modules/variables.tf | 6 +++ .../localstack/provider_override.tf | 38 +++++++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 frontend/deployment/tests/integration/localstack/provider_override.tf diff --git a/frontend/deployment/.gitignore b/frontend/deployment/.gitignore index 3a63847b..343012d3 100644 --- a/frontend/deployment/.gitignore +++ b/frontend/deployment/.gitignore @@ -5,10 +5,6 @@ *.tfstate.* crash.log crash.*.log -override.tf -override.tf.json -*_override.tf -*_override.tf.json .terraformrc terraform.rc diff --git a/frontend/deployment/distribution/cloudfront/modules/locals.tf b/frontend/deployment/distribution/cloudfront/modules/locals.tf index 2a908144..efcadf36 100644 --- a/frontend/deployment/distribution/cloudfront/modules/locals.tf +++ b/frontend/deployment/distribution/cloudfront/modules/locals.tf @@ -1,5 +1,6 @@ locals { - distribution_origin_id = "S3-${var.distribution_bucket_name}" + distribution_origin_id = "S3-${var.distribution_bucket_name}" + distribution_aws_endpoint_url_param = var.distribution_cloudfront_endpoint_url != "" ? "--endpoint-url ${var.distribution_cloudfront_endpoint_url}" : "" # Use network_full_domain from network layer (provided via cross-module locals when composed) distribution_aliases = local.network_full_domain != "" ? [local.network_full_domain] : [] diff --git a/frontend/deployment/distribution/cloudfront/modules/main.tf b/frontend/deployment/distribution/cloudfront/modules/main.tf index feb3b9a1..53b6a67d 100644 --- a/frontend/deployment/distribution/cloudfront/modules/main.tf +++ b/frontend/deployment/distribution/cloudfront/modules/main.tf @@ -123,3 +123,17 @@ resource "aws_cloudfront_distribution" "static" { tags = local.distribution_default_tags } + +# Invalidate CloudFront cache on every deployment (when origin path changes) +resource "terraform_data" "cloudfront_invalidation" { + # Trigger invalidation whenever the origin path changes + triggers_replace = [ + local.distribution_origin_path + ] + + provisioner "local-exec" { + command = "aws cloudfront create-invalidation ${local.distribution_aws_endpoint_url_param} --distribution-id ${aws_cloudfront_distribution.static.id} --paths '/*'" + } + + depends_on = [aws_cloudfront_distribution.static] +} diff --git a/frontend/deployment/distribution/cloudfront/modules/variables.tf b/frontend/deployment/distribution/cloudfront/modules/variables.tf index b118bbcd..a55c81d3 100644 --- a/frontend/deployment/distribution/cloudfront/modules/variables.tf +++ b/frontend/deployment/distribution/cloudfront/modules/variables.tf @@ -18,3 +18,9 @@ variable "distribution_resource_tags_json" { type = map(string) default = {} } + +variable "distribution_cloudfront_endpoint_url" { + description = "Custom CloudFront endpoint URL for AWS CLI (used for testing with moto)" + type = string + default = "" +} diff --git a/frontend/deployment/tests/integration/localstack/provider_override.tf b/frontend/deployment/tests/integration/localstack/provider_override.tf new file mode 100644 index 00000000..587982c2 --- /dev/null +++ b/frontend/deployment/tests/integration/localstack/provider_override.tf @@ -0,0 +1,38 @@ +# Override file for LocalStack + Moto testing +# This file is copied into the module directory during integration tests +# to configure the AWS provider to use mock endpoints +# +# LocalStack (port 4566): S3, Route53, STS, IAM, DynamoDB, ACM +# Moto (port 5000): CloudFront + +# Set CloudFront endpoint for AWS CLI commands (used by cache invalidation) +variable "distribution_cloudfront_endpoint_url" { + default = "http://moto:5000" +} + +provider "aws" { + region = var.aws_provider.region + access_key = "test" + secret_key = "test" + skip_credentials_validation = true + skip_metadata_api_check = true + skip_requesting_account_id = true + + endpoints { + # LocalStack services (using Docker service name) + s3 = "http://localstack:4566" + route53 = "http://localstack:4566" + sts = "http://localstack:4566" + iam = "http://localstack:4566" + dynamodb = "http://localstack:4566" + acm = "http://localstack:4566" + # Moto services (CloudFront not in LocalStack free tier) + cloudfront = "http://moto:5000" + } + + default_tags { + tags = var.provider_resource_tags_json + } + + s3_use_path_style = true +} From 41a204e5bb0b37fbf8ac48e6a3b293e595bb5fae Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 14:56:09 -0300 Subject: [PATCH 21/40] More integration tests --- .../cloudfront_lifecycle_test.bats | 51 ++++++++----------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats index d9f37107..ef7ade71 100644 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats +++ b/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats @@ -68,13 +68,7 @@ setup() { # Point to LocalStack-compatible modules export CUSTOM_TOFU_MODULES="$BATS_TEST_DIRNAME/localstack" -} - -# ============================================================================= -# Test: Create Infrastructure -# ============================================================================= -@test "create infrastructure deploys S3, CloudFront, and Route53 resources" { # Setup API mocks for np CLI calls # Note: /token is automatically mocked by clear_mocks() local mocks_dir="frontend/deployment/tests/integration/mocks/" @@ -86,6 +80,14 @@ setup() { mock_request "GET" "/provider/s3-asset-repository-id" "$mocks_dir/asset_repository/get_provider.json" mock_request "PATCH" "/scope/7" "$mocks_dir/scope/patch.json" +} + +# ============================================================================= +# Test: Create Infrastructure +# ============================================================================= + +@test "create infrastructure deploys S3, CloudFront, and Route53 resources" { + # Run the initial workflow run_workflow "frontend/deployment/workflows/initial.yaml" @@ -99,26 +101,17 @@ setup() { # Test: Destroy Infrastructure # ============================================================================= -#@test "destroy infrastructure removes CloudFront and Route53 resources" { -# # Setup API mocks -# mock_request "GET" "/provider" "frontend/deployment/tests/integration/mocks/asset_repository/success.json" -# -# mock_request "GET" "/scope/7" 200 '{ -# "id": 7, -# "name": "development-tools", -# "slug": "development-tools" -# }' -# -# # Disable CloudFront before deletion (required by AWS) -# if [[ -f "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" ]]; then -# "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" "Distribution for automation-development-tools-7" -# fi -# -# # Run the delete workflow -# run_workflow "frontend/deployment/workflows/delete.yaml" -# -# # Verify resources were removed (S3 bucket should remain) -# assert_s3_bucket_exists "assets-bucket" -# assert_cloudfront_not_exists "Distribution for automation-development-tools-7" -# assert_route53_record_not_exists "automation-development-tools.frontend.publicdomain.com" "A" -#} +@test "destroy infrastructure removes CloudFront and Route53 resources" { + # Disable CloudFront before deletion (required by AWS) + if [[ -f "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" ]]; then + "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" "Distribution for automation-development-tools-7" + fi + + # Run the delete workflow + run_workflow "frontend/deployment/workflows/delete.yaml" + + # Verify resources were removed (S3 bucket should remain) + assert_s3_bucket_exists "assets-bucket" + assert_cloudfront_not_exists "Distribution for automation-development-tools-7" + assert_route53_record_not_exists "automation-development-tools.frontend.publicdomain.com" "A" +} From 5b6b68fac782a3fe1623fd33feddbb5b4309c425 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 15:13:51 -0300 Subject: [PATCH 22/40] Improve aws setup tests --- frontend/deployment/provider/aws/setup | 8 ++- .../tests/provider/aws/setup_test.bats | 66 +++++-------------- 2 files changed, 21 insertions(+), 53 deletions(-) diff --git a/frontend/deployment/provider/aws/setup b/frontend/deployment/provider/aws/setup index c2f96acc..2879d914 100755 --- a/frontend/deployment/provider/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -2,20 +2,22 @@ echo "🔍 Validating AWS provider configuration..." +set_env_var_error_message="You must set it as environment variables in you nullplatform agent installation." + if [ -z "${AWS_REGION:-}" ]; then - echo " ❌ AWS_REGION is not set" + echo " ❌ AWS_REGION is missing. $set_env_var_error_message" exit 1 fi echo " ✅ AWS_REGION=$AWS_REGION" if [ -z "${TOFU_PROVIDER_BUCKET:-}" ]; then - echo " ❌ TOFU_PROVIDER_BUCKET is not set" + echo " ❌ TOFU_PROVIDER_BUCKET is missing. $set_env_var_error_message" exit 1 fi echo " ✅ TOFU_PROVIDER_BUCKET=$TOFU_PROVIDER_BUCKET" if [ -z "${TOFU_LOCK_TABLE:-}" ]; then - echo " ❌ TOFU_LOCK_TABLE is not set" + echo " ❌ TOFU_LOCK_TABLE is missing. $set_env_var_error_message" exit 1 fi echo " ✅ TOFU_LOCK_TABLE=$TOFU_LOCK_TABLE" diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats index 07673dc1..4dfff952 100644 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -10,23 +10,19 @@ # bats tests/provider/aws/setup_test.bats # ============================================================================= -# Setup - runs before each test setup() { TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" SCRIPT_PATH="$PROJECT_DIR/provider/aws/setup" - RESOURCES_DIR="$PROJECT_DIR/tests/resources" - # Load shared test utilities source "$PROJECT_ROOT/testing/assertions.sh" - # Initialize required environment variables export AWS_REGION="us-east-1" export TOFU_PROVIDER_BUCKET="my-terraform-state-bucket" export TOFU_LOCK_TABLE="terraform-locks" - # Initialize TOFU_VARIABLES with existing keys to verify script merges (not replaces) + # Base tofu variables export TOFU_VARIABLES='{ "application_slug": "automation", "scope_slug": "development-tools", @@ -47,37 +43,37 @@ run_aws_setup() { # ============================================================================= # Test: Required environment variables # ============================================================================= -@test "fails when AWS_REGION is not set" { +@test "Should fail when AWS_REGION is not set" { unset AWS_REGION run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "AWS_REGION is not set" + assert_contains "$output" " ❌ AWS_REGION is missing. You must set it as environment variables in you nullplatform agent installation." } -@test "fails when TOFU_PROVIDER_BUCKET is not set" { +@test "Should fail when TOFU_PROVIDER_BUCKET is not set" { unset TOFU_PROVIDER_BUCKET run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "TOFU_PROVIDER_BUCKET is not set" + assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing. You must set it as environment variables in you nullplatform agent installation." } -@test "fails when TOFU_LOCK_TABLE is not set" { +@test "Should fail when TOFU_LOCK_TABLE is not set" { unset TOFU_LOCK_TABLE run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "TOFU_LOCK_TABLE is not set" + assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing. You must set it as environment variables in you nullplatform agent installation." } # ============================================================================= # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= -@test "TOFU_VARIABLES matches expected structure on success" { +@test "Should add aws_provider field to TOFU_VARIABLES" { run_aws_setup local expected='{ @@ -95,7 +91,7 @@ run_aws_setup() { assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } -@test "TOFU_VARIABLES includes custom resource tags" { +@test "Should add provider_resource_tags_json to TOFU_VARIABLES" { export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' run_aws_setup @@ -115,78 +111,48 @@ run_aws_setup() { assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } -@test "TOFU_VARIABLES uses different region" { - export AWS_REGION="eu-west-1" - - run_aws_setup - - local region=$(echo "$TOFU_VARIABLES" | jq -r '.aws_provider.region') - assert_equal "$region" "eu-west-1" -} - # ============================================================================= # Test: TOFU_INIT_VARIABLES - backend configuration # ============================================================================= -@test "TOFU_INIT_VARIABLES includes bucket backend config" { +@test "Should add S3 bucket configuration to TOFU_INIT_VARIABLES" { run_aws_setup assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=bucket=my-terraform-state-bucket" } -@test "TOFU_INIT_VARIABLES includes region backend config" { +@test "Should add AWS region configuration to TOFU_INIT_VARIABLES" { run_aws_setup assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=region=us-east-1" } -@test "TOFU_INIT_VARIABLES includes dynamodb_table backend config" { +@test "Should add Dynamo table configuration to TOFU_INIT_VARIABLES" { run_aws_setup assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=dynamodb_table=terraform-locks" } -@test "TOFU_INIT_VARIABLES appends to existing variables" { +@test "Should append to TOFU_INIT_VARIABLES when it previous settings are present" { export TOFU_INIT_VARIABLES="-var=existing=value" run_aws_setup - assert_contains "$TOFU_INIT_VARIABLES" "-var=existing=value" - assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=bucket=my-terraform-state-bucket" + assert_equal "$TOFU_INIT_VARIABLES" "-var=existing=value -backend-config=bucket=my-terraform-state-bucket -backend-config=region=us-east-1 -backend-config=dynamodb_table=terraform-locks" } # ============================================================================= # Test: MODULES_TO_USE # ============================================================================= -@test "adds module to MODULES_TO_USE when empty" { +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { run_aws_setup assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/provider/aws/modules" } -@test "appends module to existing MODULES_TO_USE" { +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { export MODULES_TO_USE="existing/module" run_aws_setup assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/provider/aws/modules" } - -@test "preserves multiple existing modules in MODULES_TO_USE" { - export MODULES_TO_USE="first/module,second/module" - - run_aws_setup - - assert_equal "$MODULES_TO_USE" "first/module,second/module,$PROJECT_DIR/provider/aws/modules" -} - -# ============================================================================= -# Test: Default values -# ============================================================================= -@test "uses empty object for RESOURCE_TAGS_JSON when not set" { - unset RESOURCE_TAGS_JSON - - run_aws_setup - - local tags=$(echo "$TOFU_VARIABLES" | jq -r '.provider_resource_tags_json') - assert_equal "$tags" "{}" -} From 92e77f668c0b3b1f7680b2fb2fe431c234ca5274 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 15:36:55 -0300 Subject: [PATCH 23/40] Improve cloudfront setup tests --- .../deployment/distribution/cloudfront/setup | 21 +-- .../distribution/cloudfront/setup_test.bats | 150 ++++++++---------- .../asset_repository/unknown_error.json | 3 + 3 files changed, 72 insertions(+), 102 deletions(-) create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository/unknown_error.json diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index c880ad8e..adfe46d3 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -23,18 +23,7 @@ if [ $np_exit_code -ne 0 ]; then echo " ❌ Failed to fetch assets-repository provider" echo "" - if echo "$asset_repository" | grep -q "not found\|no providers"; then - echo " 🔎 Error: No assets-repository provider found" - echo "" - echo " 💡 Possible causes:" - echo " • No assets-repository provider is configured for this scope" - echo " • The provider category 'assets-repository' fes not exist" - echo "" - echo " 🔧 How to fix:" - echo " 1. Create an assets-repository provider in nullplatform" - echo " 2. Ensure it's linked to the scope with NRN: $nrn" - - elif echo "$asset_repository" | grep -q "unauthorized\|forbidden\|401\|403"; then + if echo "$asset_repository" | grep -q "unauthorized\|forbidden\|401\|403"; then echo " 🔒 Error: Permission denied" echo "" echo " 💡 Possible causes:" @@ -45,12 +34,6 @@ if [ $np_exit_code -ne 0 ]; then echo " 1. Check your NP_API_KEY is set and valid" echo " 2. Ensure you have permissions to access providers" - elif echo "$asset_repository" | grep -q "command not found"; then - echo " ⚠️ Error: 'np' CLI not found" - echo "" - echo " 🔧 How to fix:" - echo " Install the nullplatform CLI: npm install -g @nullplatform/cli" - else echo " 📋 Error details:" echo "$asset_repository" | sed 's/^/ /' @@ -71,7 +54,7 @@ if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" echo " 🤔 Found $(echo "$asset_repository" | jq '.results | length') provider(s), but none contain bucket information" echo "" echo " 📋 Providers found:" - echo "$asset_repository" | jq -r '.results[] | " • \(.name // .id // "unnamed") (type: \(.type // "unknown"))"' 2>/dev/null || echo " (could not parse providers)" + echo "$asset_repository" | jq -r '.results[] | " • np provider read --id \(.id) --format json"' 2>/dev/null echo "" echo " 💡 Expected one provider with: attributes.bucket.name" echo "" diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index 77320a89..bfb0944c 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -45,6 +45,7 @@ run_cloudfront_setup() { source "$SCRIPT_PATH" } +# TODO(federico.maleh) move this to the assertions.sh and document set_np_mock() { local mock_file="$1" local exit_code="${2:-0}" @@ -53,113 +54,91 @@ set_np_mock() { } # ============================================================================= -# Test: TOFU_VARIABLES - verifies the entire JSON structure +# Test: Auth error case # ============================================================================= -@test "TOFU_VARIABLES matches expected structure on success" { - set_np_mock "success.json" - - run_cloudfront_setup +@test "Should handle permission denied error fetching the asset-repository-provider" { + set_np_mock "auth_error.json" 1 - # Expected JSON - update this when adding new fields - local expected='{ - "application_slug": "automation", - "scope_slug": "development-tools", - "scope_id": "7", - "distribution_bucket_name": "assets-bucket", - "distribution_app_name": "automation-development-tools-7", - "distribution_resource_tags_json": {}, - "distribution_s3_prefix": "/tools/automation/v1.0.0" -}' + run source "$SCRIPT_PATH" - assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to fetch assets-repository provider" + assert_contains "$output" " 🔒 Error: Permission denied" } # ============================================================================= -# Test: MODULES_TO_USE +# Test: Unknown error case # ============================================================================= -@test "adds module to MODULES_TO_USE when empty" { - set_np_mock "success.json" - - run_cloudfront_setup +@test "Should handle unknown error fetching the asset-repository-provider" { + set_np_mock "unknown_error.json" 1 - assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/distribution/cloudfront/modules" -} - -@test "appends module to existing MODULES_TO_USE" { - set_np_mock "success.json" - export MODULES_TO_USE="existing/module" - - run_cloudfront_setup + run source "$SCRIPT_PATH" - assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/distribution/cloudfront/modules" + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to fetch assets-repository provider" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error fetching provider" } # ============================================================================= -# Test: Auth error case +# Test: Empty results case # ============================================================================= -@test "fails with auth error" { - set_np_mock "auth_error.json" 1 +@test "Should fail if no asset-repository found" { + set_np_mock "no_data.json" run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to fetch assets-repository provider" -} - -@test "shows permission denied message for 403 error" { - set_np_mock "auth_error.json" 1 - - run source "$SCRIPT_PATH" - - assert_contains "$output" "Permission denied" + assert_contains "$output" " ❌ No S3 bucket found in assets-repository providers" + assert_contains "$output" " 🤔 Found 0 provider(s), but none contain bucket information" } # ============================================================================= # Test: No providers found case # ============================================================================= -@test "fails when no bucket data in providers" { +@test "Should fail when no asset provider is of type s3" { set_np_mock "no_bucket_data.json" run source "$SCRIPT_PATH" + # TODO(federico.maleh) improve the how to fix instructions for these. + # It should say: 1. you need an asset provider of type s3. assert_equal "$status" "1" - assert_contains "$output" "No S3 bucket found in assets-repository providers" -} - -@test "shows provider count when no bucket found" { - set_np_mock "no_bucket_data.json" - - run source "$SCRIPT_PATH" - - assert_contains "$output" "Found 1 provider(s)" + assert_contains "$output" " ❌ No S3 bucket found in assets-repository providers" + assert_contains "$output" " 🤔 Found 1 provider(s), but none contain bucket information" + assert_contains "$output" " 📋 Providers found:" + assert_contains "$output" " • np provider read --id d397e46b-89b8-419d-ac14-2b483ace511c --format json" } # ============================================================================= -# Test: Empty results case +# Test: S3 prefix extraction from asset URL # ============================================================================= -@test "fails when no providers returned" { - set_np_mock "no_data.json" +@test "Should extracts s3_prefix from asset.url with s3 format" { + set_np_mock "success.json" - run source "$SCRIPT_PATH" + run_cloudfront_setup - assert_equal "$status" "1" - assert_contains "$output" "No S3 bucket found in assets-repository providers" + local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') + assert_equal "$s3_prefix" "/tools/automation/v1.0.0" } -@test "shows zero providers found" { - set_np_mock "no_data.json" +@test "Should extracts s3_prefix from asset.url with http format" { + set_np_mock "success.json" - run source "$SCRIPT_PATH" + # Override asset.url in context with https format + export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "https://my-asset-bucket/tools/automation/v1.0.0"') + + run_cloudfront_setup - assert_contains "$output" "Found 0 provider(s)" + local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') + assert_equal "$s3_prefix" "/tools/automation/v1.0.0" } # ============================================================================= -# Test: Custom resource tags +# Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= -@test "TOFU_VARIABLES includes custom resource tags" { +@test "Should add distribution variables to TOFU_VARIABLES" { set_np_mock "success.json" - export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' run_cloudfront_setup @@ -169,43 +148,48 @@ set_np_mock() { "scope_id": "7", "distribution_bucket_name": "assets-bucket", "distribution_app_name": "automation-development-tools-7", - "distribution_resource_tags_json": {"Environment": "production", "Team": "platform"}, + "distribution_resource_tags_json": {}, "distribution_s3_prefix": "/tools/automation/v1.0.0" }' assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } -# ============================================================================= -# Test: S3 prefix extraction from asset URL -# ============================================================================= -@test "extracts s3_prefix from asset.url" { +@test "Should add distribution_resource_tags_json to TOFU_VARIABLES" { set_np_mock "success.json" + export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' run_cloudfront_setup - local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') - assert_equal "$s3_prefix" "/tools/automation/v1.0.0" + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "distribution_bucket_name": "assets-bucket", + "distribution_app_name": "automation-development-tools-7", + "distribution_resource_tags_json": {"Environment": "production", "Team": "platform"}, + "distribution_s3_prefix": "/tools/automation/v1.0.0" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } -@test "extracts s3_prefix correctly for different asset URL paths" { +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { set_np_mock "success.json" - # Override asset.url in context - export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "s3://other-bucket/app/builds/latest"') run_cloudfront_setup - local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') - assert_equal "$s3_prefix" "/app/builds/latest" + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/distribution/cloudfront/modules" } -@test "extracts s3_prefix with single path segment" { +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { set_np_mock "success.json" - # Override asset.url in context - export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "s3://bucket/assets"') + export MODULES_TO_USE="existing/module" run_cloudfront_setup - local s3_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_s3_prefix') - assert_equal "$s3_prefix" "/assets" -} + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/distribution/cloudfront/modules" +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository/unknown_error.json b/frontend/deployment/tests/resources/np_mocks/asset_repository/unknown_error.json new file mode 100644 index 00000000..185c8263 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository/unknown_error.json @@ -0,0 +1,3 @@ +{ + "error": "Unknown error fetching provider: {\"statusCode\":500,\"code\":\"FST_INTERNAL_SERVER_ERROR\",\"error\":\"Internal Server Error\",\"message\":\"Error processing the request\"}" +} \ No newline at end of file From 05b24508081dd2ef2c9ca14028857d985277f1e3 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 15:54:37 -0300 Subject: [PATCH 24/40] Improve route53 setup tests --- frontend/deployment/network/route53/setup | 4 +- .../tests/network/route53/setup_test.bats | 163 +++++++----------- .../aws_mocks/route53/unknown_error.json | 1 + .../np_mocks/scope/patch/unknown_error.json | 3 + 4 files changed, 70 insertions(+), 101 deletions(-) create mode 100644 frontend/deployment/tests/resources/aws_mocks/route53/unknown_error.json create mode 100644 frontend/deployment/tests/resources/np_mocks/scope/patch/unknown_error.json diff --git a/frontend/deployment/network/route53/setup b/frontend/deployment/network/route53/setup index 3a57275a..f5637fde 100755 --- a/frontend/deployment/network/route53/setup +++ b/frontend/deployment/network/route53/setup @@ -5,7 +5,7 @@ echo "🔍 Validating Route53 network configuration..." hosted_zone_id=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id // empty') if [ -z "$hosted_zone_id" ]; then - echo " ❌ hosted_public_zone_id is not set in context" + echo " ❌ hosted_public_zone_id is not set in context. You must create a 'Cloud provider' configuration and then try again." exit 1 fi echo " ✅ hosted_zone_id=$hosted_zone_id" @@ -35,7 +35,7 @@ if [ $aws_exit_code -ne 0 ]; then echo "" echo " 🔧 How to fix:" echo " 1. Verify the hosted zone exists: aws route53 list-hosted-zones" - echo " 2. Update 'hosted_public_zone_id' in your cloud provider configuration" + echo " 2. Update 'hosted_public_zone_id' in your 'Cloud provider' configuration" elif echo "$aws_output" | grep -q "AccessDenied\|not authorized"; then echo " 🔒 Error: Permission denied when accessing Route 53" diff --git a/frontend/deployment/tests/network/route53/setup_test.bats b/frontend/deployment/tests/network/route53/setup_test.bats index 715d7e83..b6f153e4 100644 --- a/frontend/deployment/tests/network/route53/setup_test.bats +++ b/frontend/deployment/tests/network/route53/setup_test.bats @@ -75,49 +75,9 @@ set_np_scope_patch_mock() { } # ============================================================================= -# Test: TOFU_VARIABLES - verifies the entire JSON structure -# ============================================================================= -@test "TOFU_VARIABLES matches expected structure on success" { - set_aws_mock "success.json" - - run_route53_setup - - local expected='{ - "application_slug": "automation", - "scope_slug": "development-tools", - "scope_id": "7", - "network_hosted_zone_id": "Z1234567890ABC", - "network_domain": "example.com", - "network_subdomain": "automation-development-tools" -}' - - assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" -} - -# ============================================================================= -# Test: MODULES_TO_USE -# ============================================================================= -@test "adds module to MODULES_TO_USE when empty" { - set_aws_mock "success.json" - - run_route53_setup - - assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/network/route53/modules" -} - -@test "appends module to existing MODULES_TO_USE" { - set_aws_mock "success.json" - export MODULES_TO_USE="existing/module" - - run_route53_setup - - assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/network/route53/modules" -} - -# ============================================================================= -# Test: Missing hosted_zone_id in context +# Test: Required environment variables # ============================================================================= -@test "fails when hosted_public_zone_id is missing from context" { +@test "Should fail when hosted_public_zone_id is not present in context" { export CONTEXT='{ "application": {"slug": "automation"}, "scope": {"slug": "development-tools"}, @@ -131,142 +91,147 @@ set_np_scope_patch_mock() { run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "hosted_public_zone_id is not set in context" + assert_contains "$output" " ❌ hosted_public_zone_id is not set in context. You must create a 'Cloud provider' configuration and then try again." } # ============================================================================= # Test: NoSuchHostedZone error # ============================================================================= -@test "fails when hosted zone does not exist" { +@test "Should fail if hosted zone does not exist" { set_aws_mock "no_such_zone.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to fetch Route 53 hosted zone information" -} - -@test "shows helpful message for NoSuchHostedZone error" { - set_aws_mock "no_such_zone.json" 1 - - run source "$SCRIPT_PATH" - - assert_contains "$output" "Hosted zone" - assert_contains "$output" "does not exist" + assert_contains "$output" " ❌ Failed to fetch Route 53 hosted zone information" + assert_contains "$output" " 🔎 Error: Hosted zone 'Z1234567890ABC' does not exist" } # ============================================================================= # Test: AccessDenied error # ============================================================================= -@test "fails when access is denied" { +@test "Should fail if lacking permissions to read hosted zones" { set_aws_mock "access_denied.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to fetch Route 53 hosted zone information" -} - -@test "shows permission denied message for AccessDenied error" { - set_aws_mock "access_denied.json" 1 - - run source "$SCRIPT_PATH" - - assert_contains "$output" "Permission denied" + assert_contains "$output" " 🔒 Error: Permission denied when accessing Route 53" } # ============================================================================= # Test: InvalidInput error # ============================================================================= -@test "fails when hosted zone ID is invalid" { +@test "Should fail if hosted zone id is not valid" { set_aws_mock "invalid_input.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to fetch Route 53 hosted zone information" -} - -@test "shows invalid format message for InvalidInput error" { - set_aws_mock "invalid_input.json" 1 - - run source "$SCRIPT_PATH" - - assert_contains "$output" "Invalid hosted zone ID format" + assert_contains "$output" " ⚠️ Error: Invalid hosted zone ID format" + assert_contains "$output" " The hosted zone ID 'Z1234567890ABC' is not valid." } # ============================================================================= # Test: Credentials error # ============================================================================= -@test "fails when AWS credentials are missing" { +@test "Should fail if AWS credentials are missing" { set_aws_mock "credentials_error.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to fetch Route 53 hosted zone information" + assert_contains "$output" " 🔑 Error: AWS credentials issue" } -@test "shows credentials message for credentials error" { - set_aws_mock "credentials_error.json" 1 +# ============================================================================= +# Test: Unknown Route53 error +# ============================================================================= +@test "Should handle unknown error getting the route53 hosted zone" { + set_aws_mock "unknown_error.json" 1 run source "$SCRIPT_PATH" - assert_contains "$output" "AWS credentials issue" + assert_equal "$status" "1" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error getting route53 hosted zone." + } # ============================================================================= # Test: Empty domain in response # ============================================================================= -@test "fails when domain cannot be extracted from response" { +@test "Should handle missing hosted zone name from response" { set_aws_mock "empty_domain.json" run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" "Failed to extract domain name from hosted zone response" + assert_contains "$output" " ❌ Failed to extract domain name from hosted zone response" } # ============================================================================= -# Test: Scope patch success +# Test: Scope patch error # ============================================================================= -@test "shows setting scope domain message" { +@test "Should handle auth error updating scope domain" { set_aws_mock "success.json" - set_np_scope_patch_mock "success.json" + set_np_scope_patch_mock "auth_error.json" 1 run source "$SCRIPT_PATH" - assert_contains "$output" "Setting scope domain" + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to update scope domain" + assert_contains "$output" " 🔒 Error: Permission denied" } -@test "shows scope domain set successfully message" { +@test "Should handle unknown error updating scope domain" { set_aws_mock "success.json" - set_np_scope_patch_mock "success.json" + set_np_scope_patch_mock "unknown_error.json" 1 run source "$SCRIPT_PATH" - assert_equal "$status" "0" - assert_contains "$output" "Scope domain set successfully" + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to update scope domain" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error updating scope" } # ============================================================================= -# Test: Scope patch auth error +# Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= -@test "fails when scope patch returns auth error" { +@test "Should add network variables to TOFU_VARIABLES" { set_aws_mock "success.json" - set_np_scope_patch_mock "auth_error.json" 1 - run source "$SCRIPT_PATH" + run_route53_setup - assert_equal "$status" "1" - assert_contains "$output" "Failed to update scope domain" + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "network_hosted_zone_id": "Z1234567890ABC", + "network_domain": "example.com", + "network_subdomain": "automation-development-tools" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" } -@test "shows permission denied message for scope patch auth error" { +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { set_aws_mock "success.json" - set_np_scope_patch_mock "auth_error.json" 1 - run source "$SCRIPT_PATH" + run_route53_setup - assert_contains "$output" "Permission denied" + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/network/route53/modules" } + +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { + set_aws_mock "success.json" + export MODULES_TO_USE="existing/module" + + run_route53_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/network/route53/modules" +} \ No newline at end of file diff --git a/frontend/deployment/tests/resources/aws_mocks/route53/unknown_error.json b/frontend/deployment/tests/resources/aws_mocks/route53/unknown_error.json new file mode 100644 index 00000000..ceee2bb5 --- /dev/null +++ b/frontend/deployment/tests/resources/aws_mocks/route53/unknown_error.json @@ -0,0 +1 @@ +Unknown error getting route53 hosted zone. diff --git a/frontend/deployment/tests/resources/np_mocks/scope/patch/unknown_error.json b/frontend/deployment/tests/resources/np_mocks/scope/patch/unknown_error.json new file mode 100644 index 00000000..613c648b --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/scope/patch/unknown_error.json @@ -0,0 +1,3 @@ +{ + "error": "Unknown error updating scope: {\"statusCode\":500,\"code\":\"FST_INTERNAL_SERVER_ERROR\",\"error\":\"Internal Server Error\",\"message\":\"Error processing the request\"}" +} \ No newline at end of file From 49537c11864384b5b96455b8976359948e442fda Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 16:36:19 -0300 Subject: [PATCH 25/40] Improve aws provider logging --- delete-scopes.sh | 135 ------------------ frontend/deployment/provider/aws/setup | 43 +++--- .../tests/provider/aws/setup_test.bats | 33 ++++- 3 files changed, 56 insertions(+), 155 deletions(-) delete mode 100755 delete-scopes.sh diff --git a/delete-scopes.sh b/delete-scopes.sh deleted file mode 100755 index 5d7c4cf2..00000000 --- a/delete-scopes.sh +++ /dev/null @@ -1,135 +0,0 @@ -#!/bin/bash -account_id="" -scope_type_name="" -dry_run=false - -while [[ $# -gt 0 ]]; do - case $1 in - --account-id) account_id="$2"; shift ;; - --scope-type-name) scope_type_name="$2"; shift ;; - --dry-run) dry_run=true ;; - esac - shift -done - -# Color codes -BLUE='\033[0;34m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -RED='\033[0;31m' -GRAY='\033[0;90m' -BOLD='\033[1m' -NC='\033[0m' # No Color - -echo -e "\n${BOLD}═══════════════════════════════════════════════════════════${NC}" -echo -e "${BOLD} Scope Deletion Tool${NC}" -echo -e "${BOLD}═══════════════════════════════════════════════════════════${NC}\n" - -# Validate account_id -if [[ -z "$account_id" ]]; then - echo -e "${RED}✗ Missing --account-id flag${NC}" - exit 1 -else - echo -e "${GREEN}✓${NC} Found account-id: ${BOLD}$account_id${NC}" -fi - -# Validate scope_type_name -if [[ -z "$scope_type_name" ]]; then - echo -e "${RED}✗ Missing --scope-type-name flag${NC}" - echo " You must use the name it appears on the ui (it is not a slug)" - exit 1 -else - echo -e "${GREEN}✓${NC} Found scope-type-name: ${BOLD}$scope_type_name${NC}" -fi - -# Show dry-run mode if enabled -if [[ "$dry_run" == true ]]; then - echo -e "${YELLOW}⚠${NC} Running in ${BOLD}DRY RUN${NC} mode - no changes will be made" -fi - -echo -e "\n${BLUE}→${NC} Deleting all '${BOLD}$scope_type_name${NC}' scopes in account '${BOLD}$account_id${NC}'" - -account_nrn=$(np account read --id "$account_id" --format json | jq -r .nrn) -echo -e "${GRAY} Looking for scope type in nrn=$account_nrn${NC}" - -scope_type=$(np scope type list --nrn "$account_nrn" --name "$scope_type_name" --format json | jq ".results[0]") -if [[ $scope_type == "null" ]]; then - echo -e "${RED}✗ No scope type with name=$scope_type_name and nrn=$account_nrn${NC}" - exit 1 -fi - -scope_type_id=$(echo $scope_type | jq -r .id) -service_id=$(echo $scope_type | jq -r .provider_id) -echo -e "${GREEN}✓${NC} Found scope type ${GRAY}[id=$scope_type_id, provider_id=$service_id]${NC}\n" - -namespaces=$(np namespace list --account_id "$account_id" --format json) - -# Counter for dry-run -total_scopes=0 - -# Iterate through namespaces using process substitution instead of pipe -while IFS='|' read -r namespace_id namespace_name; do - echo -e "\n${BLUE}📦 Namespace:${NC} ${BOLD}$namespace_name${NC} ${GRAY}(id=$namespace_id)${NC}" - - # Get applications for this namespace - applications=$(np application list --namespace_id "$namespace_id" --format json) - - # Check if there are any applications - app_count=$(echo "$applications" | jq -r '.results | length') - if [[ $app_count -eq 0 ]]; then - echo -e " ${GRAY}└─ No applications in this namespace${NC}" - continue - fi - - # Iterate through applications - while IFS='|' read -r application_id application_name; do - echo -e " ${BLUE}├─ Application:${NC} ${BOLD}$application_name${NC} ${GRAY}(id=$application_id)${NC}" - - # Get scopes for this application - scopes=$(np scope list --application_id "$application_id" --format json) - - # Check if there are any matching scopes - matching_scopes=$(echo "$scopes" | jq -r --arg service_id "$service_id" \ - '.results[] | select(.status != "deleted" and .provider == $service_id) | "\(.id)|\(.name)"') - - if [[ -z "$matching_scopes" ]]; then - echo -e " ${GRAY}│ └─ No matching scopes${NC}" - continue - fi - - # Filter and iterate through scopes - while IFS='|' read -r scope_id scope_name; do - echo -e " ${BLUE}│ ├─ Scope:${NC} ${BOLD}$scope_name${NC} ${GRAY}(id=$scope_id)${NC}" - - if [[ "$dry_run" == true ]]; then - echo -e " ${YELLOW}│ └─ ⚠ Would be processed (dry run)${NC}" - ((total_scopes++)) - else - # Execute all three commands independently - echo -e " ${GRAY}│ │ ├─ Setting status to 'deleting'...${NC}" - np scope patch --id "$scope_id" --body '{"status": "deleting"}' > /dev/null 2>&1 - - echo -e " ${GRAY}│ │ ├─ Setting status to 'failed'...${NC}" - np scope patch --id "$scope_id" --body '{"status": "failed"}' > /dev/null 2>&1 - - echo -e " ${GRAY}│ │ └─ Force deleting scope...${NC}" - np scope delete --id "$scope_id" --force > /dev/null 2>&1 - - if [[ $? -eq 0 ]]; then - echo -e " ${GREEN}│ └─ ✓ Successfully processed${NC}" - ((total_scopes++)) - else - echo -e " ${RED}│ └─ ✗ Failed to process${NC}" - fi - fi - done < <(echo "$matching_scopes") - done < <(echo "$applications" | jq -r '.results[] | "\(.id)|\(.name)"') -done < <(echo "$namespaces" | jq -r '.results[] | "\(.id)|\(.name)"') - -echo -e "\n${BOLD}═══════════════════════════════════════════════════════════${NC}" -if [[ "$dry_run" == true ]]; then - echo -e "${YELLOW}⚠${NC} Dry run completed - found ${BOLD}$total_scopes${NC} scope(s) to delete - no changes were made" -else - echo -e "${GREEN}✓${NC} Process completed - ${BOLD}$total_scopes${NC} scope(s) processed" -fi -echo -e "${BOLD}═══════════════════════════════════════════════════════════${NC}\n" diff --git a/frontend/deployment/provider/aws/setup b/frontend/deployment/provider/aws/setup index 2879d914..8cfa3019 100755 --- a/frontend/deployment/provider/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -2,25 +2,34 @@ echo "🔍 Validating AWS provider configuration..." -set_env_var_error_message="You must set it as environment variables in you nullplatform agent installation." - -if [ -z "${AWS_REGION:-}" ]; then - echo " ❌ AWS_REGION is missing. $set_env_var_error_message" - exit 1 -fi -echo " ✅ AWS_REGION=$AWS_REGION" - -if [ -z "${TOFU_PROVIDER_BUCKET:-}" ]; then - echo " ❌ TOFU_PROVIDER_BUCKET is missing. $set_env_var_error_message" - exit 1 -fi -echo " ✅ TOFU_PROVIDER_BUCKET=$TOFU_PROVIDER_BUCKET" - -if [ -z "${TOFU_LOCK_TABLE:-}" ]; then - echo " ❌ TOFU_LOCK_TABLE is missing. $set_env_var_error_message" +missing_vars=() + +function validate_env_var() { + local variable_name=$1 + local variable_value="${!variable_name}" + + if [ -z "$variable_value" ]; then + echo " ❌ $variable_name is missing" + missing_vars+=("$variable_name") + else + echo " ✅ $variable_name=$variable_value" + fi +} + +validate_env_var AWS_REGION +validate_env_var TOFU_PROVIDER_BUCKET +validate_env_var TOFU_LOCK_TABLE + +if [ ${#missing_vars[@]} -gt 0 ]; then + echo "" + echo " 🔧 How to fix:" + echo " Set the missing variable(s) in your nullplatform agent Helm installation:" + for var in "${missing_vars[@]}"; do + echo " • $var" + done + echo "" exit 1 fi -echo " ✅ TOFU_LOCK_TABLE=$TOFU_LOCK_TABLE" echo "✨ AWS provider configured successfully" echo "" diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats index 4dfff952..b2aba242 100644 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -49,7 +49,10 @@ run_aws_setup() { run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" " ❌ AWS_REGION is missing. You must set it as environment variables in you nullplatform agent installation." + assert_contains "$output" " ❌ AWS_REGION is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " • AWS_REGION" } @test "Should fail when TOFU_PROVIDER_BUCKET is not set" { @@ -58,7 +61,10 @@ run_aws_setup() { run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing. You must set it as environment variables in you nullplatform agent installation." + assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " • TOFU_PROVIDER_BUCKET" } @test "Should fail when TOFU_LOCK_TABLE is not set" { @@ -67,7 +73,28 @@ run_aws_setup() { run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing. You must set it as environment variables in you nullplatform agent installation." + assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " • TOFU_LOCK_TABLE" +} + +@test "Should report all the variables that are not set" { + unset AWS_REGION + unset TOFU_PROVIDER_BUCKET + unset TOFU_LOCK_TABLE + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ AWS_REGION is missing" + assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing" + assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " • AWS_REGION" + assert_contains "$output" " • TOFU_PROVIDER_BUCKET" + assert_contains "$output" " • TOFU_LOCK_TABLE" } # ============================================================================= From c18253ff64d26fc4b78b2d60240b816cd217a5bf Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 16:57:55 -0300 Subject: [PATCH 26/40] Improve cloudfront setup tests --- .../deployment/distribution/cloudfront/setup | 31 +++++----- .../distribution/cloudfront/setup_test.bats | 59 +++++++++++-------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index adfe46d3..74a29d14 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -27,12 +27,11 @@ if [ $np_exit_code -ne 0 ]; then echo " 🔒 Error: Permission denied" echo "" echo " 💡 Possible causes:" - echo " • The API token doesn't have permission to list providers" - echo " • The token has expired" + echo " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: $nrn" echo "" echo " 🔧 How to fix:" - echo " 1. Check your NP_API_KEY is set and valid" - echo " 2. Ensure you have permissions to access providers" + echo " 1. Ensure your API Key has 'Ops' permissions" + echo " 2. Ensure your API Key is set at the correct NRN hierarchy level" else echo " 📋 Error details:" @@ -49,18 +48,22 @@ distribution_bucket_name=$(echo "$asset_repository" | jq -r ' if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" ]; then echo "" - echo " ❌ No S3 bucket found in assets-repository providers" - echo "" - echo " 🤔 Found $(echo "$asset_repository" | jq '.results | length') provider(s), but none contain bucket information" - echo "" - echo " 📋 Providers found:" - echo "$asset_repository" | jq -r '.results[] | " • np provider read --id \(.id) --format json"' 2>/dev/null - echo "" - echo " 💡 Expected one provider with: attributes.bucket.name" + echo " ❌ No assets-repository provider of type AWS S3 at nrn: $nrn" echo "" + + provider_count=$(echo "$asset_repository" | jq '.results | length') + + if [ "$provider_count" -gt 0 ]; then + echo " 🤔 Found $provider_count asset-repository provider(s), but none are configured for S3." + echo "" + echo " 📋 Verify the existing providers with the nullplatform CLI:" + echo "$asset_repository" | jq -r '.results[] | " • np provider read --id \(.id) --format json"' 2>/dev/null + echo "" + fi + echo " 🔧 How to fix:" - echo " 1. Ensure an S3 bucket provider is configured in assets-repository category" - echo " 2. Verify the provider has the bucket_name attribute populated" + echo " 1. Ensure you have an asset-repository provider of type S3 configured" + echo " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" echo "" exit 1 fi diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index bfb0944c..5d57627c 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -46,31 +46,37 @@ run_cloudfront_setup() { } # TODO(federico.maleh) move this to the assertions.sh and document -set_np_mock() { - local mock_file="$1" - local exit_code="${2:-0}" - export NP_MOCK_RESPONSE="$MOCKS_DIR/asset_repository/$mock_file" - export NP_MOCK_EXIT_CODE="$exit_code" -} +#set_np_mock() { +# local mock_file="$1" +# local exit_code="${2:-0}" +# export NP_MOCK_RESPONSE="$mock_file" +# export NP_MOCK_EXIT_CODE="$exit_code" +#} # ============================================================================= # Test: Auth error case # ============================================================================= @test "Should handle permission denied error fetching the asset-repository-provider" { - set_np_mock "auth_error.json" 1 + set_np_mock "$MOCKS_DIR/asset_repository/auth_error.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " ❌ Failed to fetch assets-repository provider" assert_contains "$output" " 🔒 Error: Permission denied" + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure your API Key has 'Ops' permissions" + assert_contains "$output" " 2. Ensure your API Key is set at the correct NRN hierarchy level" } # ============================================================================= # Test: Unknown error case # ============================================================================= @test "Should handle unknown error fetching the asset-repository-provider" { - set_np_mock "unknown_error.json" 1 + set_np_mock "$MOCKS_DIR/asset_repository/unknown_error.json" 1 run source "$SCRIPT_PATH" @@ -84,37 +90,44 @@ set_np_mock() { # Test: Empty results case # ============================================================================= @test "Should fail if no asset-repository found" { - set_np_mock "no_data.json" + set_np_mock "$MOCKS_DIR/asset_repository/no_data.json" run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" " ❌ No S3 bucket found in assets-repository providers" - assert_contains "$output" " 🤔 Found 0 provider(s), but none contain bucket information" + assert_contains "$output" " ❌ No assets-repository provider of type AWS S3 at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure you have an asset-repository provider of type S3 configured" + assert_contains "$output" " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" } # ============================================================================= # Test: No providers found case # ============================================================================= @test "Should fail when no asset provider is of type s3" { - set_np_mock "no_bucket_data.json" + set_np_mock "$MOCKS_DIR/asset_repository/no_bucket_data.json" run source "$SCRIPT_PATH" - # TODO(federico.maleh) improve the how to fix instructions for these. - # It should say: 1. you need an asset provider of type s3. assert_equal "$status" "1" - assert_contains "$output" " ❌ No S3 bucket found in assets-repository providers" - assert_contains "$output" " 🤔 Found 1 provider(s), but none contain bucket information" - assert_contains "$output" " 📋 Providers found:" + assert_contains "$output" " ❌ No assets-repository provider of type AWS S3 at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + assert_contains "$output" " 🤔 Found 1 asset-repository provider(s), but none are configured for S3." + + assert_contains "$output" " 📋 Verify the existing providers with the nullplatform CLI:" assert_contains "$output" " • np provider read --id d397e46b-89b8-419d-ac14-2b483ace511c --format json" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure you have an asset-repository provider of type S3 configured" + assert_contains "$output" " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" + assert_equal "$status" "1" } # ============================================================================= # Test: S3 prefix extraction from asset URL # ============================================================================= @test "Should extracts s3_prefix from asset.url with s3 format" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" run_cloudfront_setup @@ -123,7 +136,7 @@ set_np_mock() { } @test "Should extracts s3_prefix from asset.url with http format" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" # Override asset.url in context with https format export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "https://my-asset-bucket/tools/automation/v1.0.0"') @@ -138,7 +151,7 @@ set_np_mock() { # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= @test "Should add distribution variables to TOFU_VARIABLES" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" run_cloudfront_setup @@ -156,7 +169,7 @@ set_np_mock() { } @test "Should add distribution_resource_tags_json to TOFU_VARIABLES" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' run_cloudfront_setup @@ -178,7 +191,7 @@ set_np_mock() { # Test: MODULES_TO_USE # ============================================================================= @test "Should register the provider in the MODULES_TO_USE variable when it's empty" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" run_cloudfront_setup @@ -186,7 +199,7 @@ set_np_mock() { } @test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { - set_np_mock "success.json" + set_np_mock "$MOCKS_DIR/asset_repository/success.json" export MODULES_TO_USE="existing/module" run_cloudfront_setup From e8e224afa4f2601f00887f95a69644579b22d773 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 17:35:17 -0300 Subject: [PATCH 27/40] Improve route53 setup tests + general log improvement --- .../deployment/distribution/cloudfront/setup | 6 +- frontend/deployment/network/route53/setup | 46 ++++---- frontend/deployment/provider/aws/setup | 2 +- .../distribution/cloudfront/setup_test.bats | 9 +- .../tests/network/route53/setup_test.bats | 100 ++++++++++++------ .../tests/provider/aws/setup_test.bats | 8 +- .../deployment/tests/resources/np_mocks/np | 20 +--- 7 files changed, 111 insertions(+), 80 deletions(-) diff --git a/frontend/deployment/distribution/cloudfront/setup b/frontend/deployment/distribution/cloudfront/setup index 74a29d14..01605b88 100755 --- a/frontend/deployment/distribution/cloudfront/setup +++ b/frontend/deployment/distribution/cloudfront/setup @@ -30,8 +30,7 @@ if [ $np_exit_code -ne 0 ]; then echo " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: $nrn" echo "" echo " 🔧 How to fix:" - echo " 1. Ensure your API Key has 'Ops' permissions" - echo " 2. Ensure your API Key is set at the correct NRN hierarchy level" + echo " 1. Ensure the API Key has 'Ops' permissions at the correct NRN hierarchy level" else echo " 📋 Error details:" @@ -62,8 +61,7 @@ if [ -z "$distribution_bucket_name" ] || [ "$distribution_bucket_name" = "null" fi echo " 🔧 How to fix:" - echo " 1. Ensure you have an asset-repository provider of type S3 configured" - echo " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" + echo " 1. Ensure there is an asset-repository provider of type S3 configured at the correct NRN hierarchy level" echo "" exit 1 fi diff --git a/frontend/deployment/network/route53/setup b/frontend/deployment/network/route53/setup index f5637fde..300bad50 100755 --- a/frontend/deployment/network/route53/setup +++ b/frontend/deployment/network/route53/setup @@ -5,7 +5,13 @@ echo "🔍 Validating Route53 network configuration..." hosted_zone_id=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.hosted_public_zone_id // empty') if [ -z "$hosted_zone_id" ]; then - echo " ❌ hosted_public_zone_id is not set in context. You must create a 'Cloud provider' configuration and then try again." + echo "" + echo " ❌ hosted_public_zone_id is not set in context" + echo "" + echo " 🔧 How to fix:" + echo " 1. Ensure there is an AWS cloud-provider configured at the correct NRN hierarchy level" + echo " 2. Set the 'hosted_public_zone_id' field with the Route 53 hosted zone ID" + echo "" exit 1 fi echo " ✅ hosted_zone_id=$hosted_zone_id" @@ -35,7 +41,7 @@ if [ $aws_exit_code -ne 0 ]; then echo "" echo " 🔧 How to fix:" echo " 1. Verify the hosted zone exists: aws route53 list-hosted-zones" - echo " 2. Update 'hosted_public_zone_id' in your 'Cloud provider' configuration" + echo " 2. Update 'hosted_public_zone_id' in the AWS cloud-provider configuration" elif echo "$aws_output" | grep -q "AccessDenied\|not authorized"; then echo " 🔒 Error: Permission denied when accessing Route 53" @@ -45,8 +51,8 @@ if [ $aws_exit_code -ne 0 ]; then echo " • The IAM role/user is missing the 'route53:GetHostedZone' permission" echo "" echo " 🔧 How to fix:" - echo " 1. Check your AWS credentials are configured correctly" - echo " 2. Ensure your IAM policy includes:" + echo " 1. Check the AWS credentials are configured correctly" + echo " 2. Ensure the IAM policy includes:" echo " {" echo " \"Effect\": \"Allow\"," echo " \"Action\": \"route53:GetHostedZone\"," @@ -66,14 +72,12 @@ if [ $aws_exit_code -ne 0 ]; then echo " 🔑 Error: AWS credentials issue" echo "" echo " 💡 Possible causes:" - echo " • AWS credentials are not configured" - echo " • AWS credentials have expired" - echo " • AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY environment variables are missing" + echo " • The nullplatform agent is not configured with AWS credentials" + echo " • The IAM role associated with the service account does not exist" echo "" echo " 🔧 How to fix:" - echo " 1. Run 'aws configure' to set up credentials" - echo " 2. Or set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables" - echo " 3. If using temporary credentials, refresh your session token" + echo " 1. Configure a service account in the nullplatform agent Helm installation" + echo " 2. Verify the IAM role associated with the service account exists and has the required permissions" else echo " 📋 Error details:" @@ -90,8 +94,11 @@ if [ -z "$network_domain" ] || [ "$network_domain" = "null" ]; then echo "" echo " ❌ Failed to extract domain name from hosted zone response" echo "" - echo " 🤔 The AWS API returned successfully but the domain name could not be parsed." - echo " This is unexpected - please check the hosted zone configuration." + echo " 💡 Possible causes:" + echo " • The hosted zone does not have a valid domain name configured" + echo "" + echo " 🔧 How to fix:" + echo " 1. Verify the hosted zone has a valid domain: aws route53 get-hosted-zone --id $hosted_zone_id" echo "" exit 1 fi @@ -101,10 +108,6 @@ echo " ✅ domain=$network_domain" network_subdomain="$application_slug-$scope_slug" echo " ✅ subdomain=$network_subdomain" -echo "" -echo "✨ Route53 network configured successfully" -echo "" - TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg hosted_zone_id "$hosted_zone_id" \ --arg domain "$network_domain" \ @@ -117,6 +120,7 @@ TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ scope_domain="$network_subdomain.$network_domain" +echo "" echo " 📝 Setting scope domain to '$scope_domain'..." np_output=$(np scope patch --id "$scope_id" --body "{\"domain\":\"$scope_domain\"}" --format json 2>&1) @@ -131,12 +135,10 @@ if [ $np_exit_code -ne 0 ]; then echo " 🔒 Error: Permission denied" echo "" echo " 💡 Possible causes:" - echo " • The API token doesn't have permission to update scopes" - echo " • The token has expired" + echo " • The nullplatform API Key doesn't have 'Developer' permissions" echo "" echo " 🔧 How to fix:" - echo " 1. Check your NP_API_KEY is set and valid" - echo " 2. Ensure you have permissions to update scopes" + echo " 1. Ensure the API Key has 'Developer' permissions at the correct NRN hierarchy level" else echo " 📋 Error details:" @@ -149,6 +151,10 @@ fi echo " ✅ Scope domain set successfully" +echo "" +echo "✨ Route53 network configured successfully" +echo "" + # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/provider/aws/setup b/frontend/deployment/provider/aws/setup index 8cfa3019..69052daf 100755 --- a/frontend/deployment/provider/aws/setup +++ b/frontend/deployment/provider/aws/setup @@ -23,7 +23,7 @@ validate_env_var TOFU_LOCK_TABLE if [ ${#missing_vars[@]} -gt 0 ]; then echo "" echo " 🔧 How to fix:" - echo " Set the missing variable(s) in your nullplatform agent Helm installation:" + echo " Set the missing variable(s) in the nullplatform agent Helm installation:" for var in "${missing_vars[@]}"; do echo " • $var" done diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index 5d57627c..af355d27 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -68,8 +68,7 @@ run_cloudfront_setup() { assert_contains "$output" " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: organization=1:account=2:namespace=3:application=4:scope=7" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " 1. Ensure your API Key has 'Ops' permissions" - assert_contains "$output" " 2. Ensure your API Key is set at the correct NRN hierarchy level" + assert_contains "$output" " 1. Ensure the API Key has 'Ops' permissions at the correct NRN hierarchy level" } # ============================================================================= @@ -98,8 +97,7 @@ run_cloudfront_setup() { assert_contains "$output" " ❌ No assets-repository provider of type AWS S3 at nrn: organization=1:account=2:namespace=3:application=4:scope=7" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " 1. Ensure you have an asset-repository provider of type S3 configured" - assert_contains "$output" " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" + assert_contains "$output" " 1. Ensure there is an asset-repository provider of type S3 configured at the correct NRN hierarchy level" } # ============================================================================= @@ -118,8 +116,7 @@ run_cloudfront_setup() { assert_contains "$output" " • np provider read --id d397e46b-89b8-419d-ac14-2b483ace511c --format json" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " 1. Ensure you have an asset-repository provider of type S3 configured" - assert_contains "$output" " 2. Ensure your asset-repository provider is set at the correct NRN hierarchy level" + assert_contains "$output" " 1. Ensure there is an asset-repository provider of type S3 configured at the correct NRN hierarchy level" assert_equal "$status" "1" } diff --git a/frontend/deployment/tests/network/route53/setup_test.bats b/frontend/deployment/tests/network/route53/setup_test.bats index b6f153e4..dc7309ed 100644 --- a/frontend/deployment/tests/network/route53/setup_test.bats +++ b/frontend/deployment/tests/network/route53/setup_test.bats @@ -49,8 +49,8 @@ setup() { export MODULES_TO_USE="" # Set default np scope patch mock (success) - export NP_MOCK_SCOPE_PATCH_RESPONSE="$NP_MOCKS_DIR/scope/patch/success.json" - export NP_MOCK_SCOPE_PATCH_EXIT_CODE="0" + export NP_MOCK_RESPONSE="$NP_MOCKS_DIR/scope/patch/success.json" + export NP_MOCK_EXIT_CODE="0" } # ============================================================================= @@ -60,19 +60,12 @@ run_route53_setup() { source "$SCRIPT_PATH" } -set_aws_mock() { - local mock_file="$1" - local exit_code="${2:-0}" - export AWS_MOCK_RESPONSE="$AWS_MOCKS_DIR/route53/$mock_file" - export AWS_MOCK_EXIT_CODE="$exit_code" -} - -set_np_scope_patch_mock() { - local mock_file="$1" - local exit_code="${2:-0}" - export NP_MOCK_SCOPE_PATCH_RESPONSE="$NP_MOCKS_DIR/scope/patch/$mock_file" - export NP_MOCK_SCOPE_PATCH_EXIT_CODE="$exit_code" -} +#set_np_scope_patch_mock() { +# local mock_file="$1" +# local exit_code="${2:-0}" +# export NP_MOCK_RESPONSE="$mock_file" +# export NP_MOCK_EXIT_CODE="$exit_code" +#} # ============================================================================= # Test: Required environment variables @@ -91,64 +84,98 @@ set_np_scope_patch_mock() { run source "$SCRIPT_PATH" assert_equal "$status" "1" - assert_contains "$output" " ❌ hosted_public_zone_id is not set in context. You must create a 'Cloud provider' configuration and then try again." + assert_contains "$output" " ❌ hosted_public_zone_id is not set in context" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure there is an AWS cloud-provider configured at the correct NRN hierarchy level" + assert_contains "$output" " 2. Set the 'hosted_public_zone_id' field with the Route 53 hosted zone ID" } # ============================================================================= # Test: NoSuchHostedZone error # ============================================================================= @test "Should fail if hosted zone does not exist" { - set_aws_mock "no_such_zone.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/no_such_zone.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " ❌ Failed to fetch Route 53 hosted zone information" assert_contains "$output" " 🔎 Error: Hosted zone 'Z1234567890ABC' does not exist" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The hosted zone ID is incorrect or has a typo" + assert_contains "$output" " • The hosted zone was deleted" + assert_contains "$output" " • The hosted zone ID format is wrong (should be like 'Z1234567890ABC' or '/hostedzone/Z1234567890ABC')" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Verify the hosted zone exists: aws route53 list-hosted-zones" + assert_contains "$output" " 2. Update 'hosted_public_zone_id' in the AWS cloud-provider configuration" } # ============================================================================= # Test: AccessDenied error # ============================================================================= @test "Should fail if lacking permissions to read hosted zones" { - set_aws_mock "access_denied.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/access_denied.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " 🔒 Error: Permission denied when accessing Route 53" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The AWS credentials don't have Route 53 read permissions" + assert_contains "$output" " • The IAM role/user is missing the 'route53:GetHostedZone' permission" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Check the AWS credentials are configured correctly" + assert_contains "$output" " 2. Ensure the IAM policy includes:" } # ============================================================================= # Test: InvalidInput error # ============================================================================= @test "Should fail if hosted zone id is not valid" { - set_aws_mock "invalid_input.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/invalid_input.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " ⚠️ Error: Invalid hosted zone ID format" assert_contains "$output" " The hosted zone ID 'Z1234567890ABC' is not valid." + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Use the format 'Z1234567890ABC' or '/hostedzone/Z1234567890ABC'" + assert_contains "$output" " • Find valid zone IDs with: aws route53 list-hosted-zones" } # ============================================================================= # Test: Credentials error # ============================================================================= @test "Should fail if AWS credentials are missing" { - set_aws_mock "credentials_error.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/credentials_error.json" 1 run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " 🔑 Error: AWS credentials issue" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform agent is not configured with AWS credentials" + assert_contains "$output" " • The IAM role associated with the service account does not exist" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Configure a service account in the nullplatform agent Helm installation" + assert_contains "$output" " 2. Verify the IAM role associated with the service account exists and has the required permissions" + assert_contains "$output" " 🔑 Error: AWS credentials issue" } # ============================================================================= # Test: Unknown Route53 error # ============================================================================= @test "Should handle unknown error getting the route53 hosted zone" { - set_aws_mock "unknown_error.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/unknown_error.json" 1 run source "$SCRIPT_PATH" @@ -162,31 +189,44 @@ set_np_scope_patch_mock() { # Test: Empty domain in response # ============================================================================= @test "Should handle missing hosted zone name from response" { - set_aws_mock "empty_domain.json" + set_aws_mock "$AWS_MOCKS_DIR/route53/empty_domain.json" run source "$SCRIPT_PATH" assert_equal "$status" "1" assert_contains "$output" " ❌ Failed to extract domain name from hosted zone response" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The hosted zone does not have a valid domain name configured" + assert_contains "$output" " ❌ Failed to extract domain name from hosted zone response" + + assert_contains "$output" " ❌ Failed to extract domain name from hosted zone response" + assert_contains "$output" " 1. Verify the hosted zone has a valid domain: aws route53 get-hosted-zone --id Z1234567890ABC" } # ============================================================================= # Test: Scope patch error # ============================================================================= @test "Should handle auth error updating scope domain" { - set_aws_mock "success.json" - set_np_scope_patch_mock "auth_error.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/success.json" + set_np_mock "$NP_MOCKS_DIR/scope/patch/auth_error.json" 1 run source "$SCRIPT_PATH" - assert_equal "$status" "1" +# assert_equal "$status" "1" assert_contains "$output" " ❌ Failed to update scope domain" assert_contains "$output" " 🔒 Error: Permission denied" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform API Key doesn't have 'Developer' permissions" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure the API Key has 'Developer' permissions at the correct NRN hierarchy level" } @test "Should handle unknown error updating scope domain" { - set_aws_mock "success.json" - set_np_scope_patch_mock "unknown_error.json" 1 + set_aws_mock "$AWS_MOCKS_DIR/route53/success.json" + set_np_mock "$NP_MOCKS_DIR/scope/patch/unknown_error.json" 1 run source "$SCRIPT_PATH" @@ -200,7 +240,7 @@ set_np_scope_patch_mock() { # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= @test "Should add network variables to TOFU_VARIABLES" { - set_aws_mock "success.json" + set_aws_mock "$AWS_MOCKS_DIR/route53/success.json" run_route53_setup @@ -220,7 +260,7 @@ set_np_scope_patch_mock() { # Test: MODULES_TO_USE # ============================================================================= @test "Should register the provider in the MODULES_TO_USE variable when it's empty" { - set_aws_mock "success.json" + set_aws_mock "$AWS_MOCKS_DIR/route53/success.json" run_route53_setup @@ -228,7 +268,7 @@ set_np_scope_patch_mock() { } @test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { - set_aws_mock "success.json" + set_aws_mock "$AWS_MOCKS_DIR/route53/success.json" export MODULES_TO_USE="existing/module" run_route53_setup diff --git a/frontend/deployment/tests/provider/aws/setup_test.bats b/frontend/deployment/tests/provider/aws/setup_test.bats index b2aba242..4fc50557 100644 --- a/frontend/deployment/tests/provider/aws/setup_test.bats +++ b/frontend/deployment/tests/provider/aws/setup_test.bats @@ -51,7 +51,7 @@ run_aws_setup() { assert_equal "$status" "1" assert_contains "$output" " ❌ AWS_REGION is missing" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" assert_contains "$output" " • AWS_REGION" } @@ -63,7 +63,7 @@ run_aws_setup() { assert_equal "$status" "1" assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" assert_contains "$output" " • TOFU_PROVIDER_BUCKET" } @@ -75,7 +75,7 @@ run_aws_setup() { assert_equal "$status" "1" assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" assert_contains "$output" " • TOFU_LOCK_TABLE" } @@ -91,7 +91,7 @@ run_aws_setup() { assert_contains "$output" " ❌ TOFU_PROVIDER_BUCKET is missing" assert_contains "$output" " ❌ TOFU_LOCK_TABLE is missing" assert_contains "$output" " 🔧 How to fix:" - assert_contains "$output" " Set the missing variable(s) in your nullplatform agent Helm installation:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" assert_contains "$output" " • AWS_REGION" assert_contains "$output" " • TOFU_PROVIDER_BUCKET" assert_contains "$output" " • TOFU_LOCK_TABLE" diff --git a/frontend/deployment/tests/resources/np_mocks/np b/frontend/deployment/tests/resources/np_mocks/np index 24bfd747..15f54a7f 100755 --- a/frontend/deployment/tests/resources/np_mocks/np +++ b/frontend/deployment/tests/resources/np_mocks/np @@ -1,22 +1,12 @@ #!/bin/bash # Mock np CLI for testing # -# Supports command-specific mocks: -# - NP_MOCK_SCOPE_PATCH_RESPONSE / NP_MOCK_SCOPE_PATCH_EXIT_CODE for "np scope patch" -# - NP_MOCK_PROVIDER_LIST_RESPONSE / NP_MOCK_PROVIDER_LIST_EXIT_CODE for "np provider list" -# - NP_MOCK_RESPONSE / NP_MOCK_EXIT_CODE as fallback for any command +# Usage: +# Set NP_MOCK_RESPONSE to the path of the mock JSON file to return +# Set NP_MOCK_EXIT_CODE to the exit code (default: 0) -# Detect which command is being called -if [[ "$1" == "scope" && "$2" == "patch" ]]; then - MOCK_RESPONSE="${NP_MOCK_SCOPE_PATCH_RESPONSE:-$NP_MOCK_RESPONSE}" - MOCK_EXIT_CODE="${NP_MOCK_SCOPE_PATCH_EXIT_CODE:-${NP_MOCK_EXIT_CODE:-0}}" -elif [[ "$1" == "provider" && "$2" == "list" ]]; then - MOCK_RESPONSE="${NP_MOCK_PROVIDER_LIST_RESPONSE:-$NP_MOCK_RESPONSE}" - MOCK_EXIT_CODE="${NP_MOCK_PROVIDER_LIST_EXIT_CODE:-${NP_MOCK_EXIT_CODE:-0}}" -else - MOCK_RESPONSE="$NP_MOCK_RESPONSE" - MOCK_EXIT_CODE="${NP_MOCK_EXIT_CODE:-0}" -fi +MOCK_RESPONSE="$NP_MOCK_RESPONSE" +MOCK_EXIT_CODE="${NP_MOCK_EXIT_CODE:-0}" if [ -z "$MOCK_RESPONSE" ]; then echo "No mock response configured for: np $*" >&2 From c2db57cc19cd370d5b2eae8ee00c21feb7f98dc6 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Thu, 15 Jan 2026 17:50:39 -0300 Subject: [PATCH 28/40] Improve error report --- .../tests/distribution/cloudfront/setup_test.bats | 8 -------- testing/run_bats_tests.sh | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats index af355d27..84310f80 100644 --- a/frontend/deployment/tests/distribution/cloudfront/setup_test.bats +++ b/frontend/deployment/tests/distribution/cloudfront/setup_test.bats @@ -45,14 +45,6 @@ run_cloudfront_setup() { source "$SCRIPT_PATH" } -# TODO(federico.maleh) move this to the assertions.sh and document -#set_np_mock() { -# local mock_file="$1" -# local exit_code="${2:-0}" -# export NP_MOCK_RESPONSE="$mock_file" -# export NP_MOCK_EXIT_CODE="$exit_code" -#} - # ============================================================================= # Test: Auth error case # ============================================================================= diff --git a/testing/run_bats_tests.sh b/testing/run_bats_tests.sh index 36d72173..d17384e6 100755 --- a/testing/run_bats_tests.sh +++ b/testing/run_bats_tests.sh @@ -191,4 +191,4 @@ if [ ${#FAILED_TESTS[@]} -gt 0 ]; then exit 1 fi -echo -e "${GREEN}All BATS tests passed!${NC}" \ No newline at end of file +echo -e "${GREEN}All BATS tests passed!${NC}" From 56803a09ba0109a9129ac7a1ca70b95f48cb1463 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 16 Jan 2026 14:52:35 -0300 Subject: [PATCH 29/40] Improve integration tests --- .gitignore | 2 +- Makefile | 2 +- .../cloudfront_assertions.bash | 126 ++++ .../lifecycle_test.bats} | 87 +-- .../route53_assertions.bash | 133 ++++ .../integration/volume/cache/machine.json | 1 - .../integration/volume/cache/server.test.pem | 168 ----- .../volume/cache/server.test.pem.crt | 140 ----- .../volume/cache/server.test.pem.key | 28 - integration_run.txt | 578 ++++++++++++++++++ 10 files changed, 889 insertions(+), 376 deletions(-) create mode 100644 frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/cloudfront_assertions.bash rename frontend/deployment/tests/integration/{cloudfront_lifecycle_test.bats => test_cases/aws_cloudfront_route53/lifecycle_test.bats} (52%) create mode 100644 frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/route53_assertions.bash delete mode 100644 frontend/deployment/tests/integration/volume/cache/machine.json delete mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem delete mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem.crt delete mode 100644 frontend/deployment/tests/integration/volume/cache/server.test.pem.key create mode 100644 integration_run.txt diff --git a/.gitignore b/.gitignore index 11f635a7..8a3f6a36 100644 --- a/.gitignore +++ b/.gitignore @@ -148,4 +148,4 @@ frontend/deployment/tests/integration/volume/ testing/docker/certs/ # Claude Code -.claude/ +.claude/ \ No newline at end of file diff --git a/Makefile b/Makefile index 5c471b8e..e091370b 100644 --- a/Makefile +++ b/Makefile @@ -51,4 +51,4 @@ help: @echo "" @echo "Options:" @echo " MODULE= Run tests for specific module (e.g., MODULE=frontend)" - @echo " VERBOSE=1 Show output of passing tests (integration tests only)" \ No newline at end of file + @echo " VERBOSE=1 Show output of passing tests (integration tests only)" diff --git a/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/cloudfront_assertions.bash b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/cloudfront_assertions.bash new file mode 100644 index 00000000..53fc2575 --- /dev/null +++ b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/cloudfront_assertions.bash @@ -0,0 +1,126 @@ +#!/bin/bash +# ============================================================================= +# CloudFront Assertion Functions +# +# Provides assertion functions for validating CloudFront distribution +# configuration in integration tests. +# +# Variables validated (from distribution/cloudfront/modules/variables.tf): +# - distribution_bucket_name -> Origin domain +# - distribution_s3_prefix -> Origin path +# - distribution_app_name -> Distribution comment +# - distribution_resource_tags_json -> (skipped - Moto limitation) +# +# Usage: +# source "cloudfront_assertions.bash" +# assert_cloudfront_configured "comment" "domain" "bucket" "/prefix" +# +# Note: Some CloudFront fields are not fully supported by Moto and are skipped: +# - DefaultRootObject, PriceClass, IsIPV6Enabled +# - OriginAccessControlId, Compress, CachedMethods +# - CustomErrorResponses, Tags +# ============================================================================= + +# ============================================================================= +# CloudFront Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | Distribution exists | Non-empty ID | +# | Distribution enabled | true | +# | Distribution comment | expected comment (exact match) | +# | Custom domain alias | expected_domain (exact match) | +# | Origin domain | Contains expected_bucket | +# | Origin path (S3 prefix) | expected_origin_path (exact match) | +# | Viewer protocol policy | redirect-to-https | +# | Allowed methods | Contains GET, HEAD | +# +----------------------------------+----------------------------------------+ +assert_cloudfront_configured() { + local comment="$1" + local expected_domain="$2" + local expected_bucket="$3" + local expected_origin_path="$4" + + # Get CloudFront distribution by comment + local distribution_json + distribution_json=$(aws_moto cloudfront list-distributions \ + --query "DistributionList.Items[?Comment=='$comment']" \ + --output json 2>/dev/null | jq '.[0]') + + # Distribution exists + assert_not_empty "$distribution_json" "CloudFront distribution" + + local distribution_id + distribution_id=$(echo "$distribution_json" | jq -r '.Id') + assert_not_empty "$distribution_id" "CloudFront distribution ID" + + # Distribution enabled + local distribution_enabled + distribution_enabled=$(echo "$distribution_json" | jq -r '.Enabled') + assert_true "$distribution_enabled" "CloudFront distribution enabled" + + # Distribution comment (validates distribution_app_name) + local actual_comment + actual_comment=$(echo "$distribution_json" | jq -r '.Comment') + assert_equal "$actual_comment" "$comment" + + # Custom domain alias (exact match - only one alias expected) + local alias + alias=$(echo "$distribution_json" | jq -r '.Aliases.Items[0] // empty') + if [[ -n "$alias" && "$alias" != "null" ]]; then + assert_equal "$alias" "$expected_domain" + fi + + # Origin domain (validates distribution_bucket_name) + local origin_domain + origin_domain=$(echo "$distribution_json" | jq -r '.Origins.Items[0].DomainName // empty') + assert_not_empty "$origin_domain" "Origin domain" + assert_contains "$origin_domain" "$expected_bucket" + + # Origin path (validates distribution_s3_prefix) + local origin_path + origin_path=$(echo "$distribution_json" | jq -r '.Origins.Items[0].OriginPath // empty') + assert_equal "$origin_path" "$expected_origin_path" + + # Default cache behavior - viewer protocol policy + local viewer_protocol_policy + viewer_protocol_policy=$(echo "$distribution_json" | jq -r '.DefaultCacheBehavior.ViewerProtocolPolicy // empty') + if [[ -n "$viewer_protocol_policy" && "$viewer_protocol_policy" != "null" ]]; then + assert_equal "$viewer_protocol_policy" "redirect-to-https" + fi + + # Default cache behavior - allowed methods (check GET and HEAD are present) + local allowed_methods + allowed_methods=$(echo "$distribution_json" | jq -r '.DefaultCacheBehavior.AllowedMethods.Items // [] | join(",")') + if [[ -n "$allowed_methods" ]]; then + assert_contains "$allowed_methods" "GET" + assert_contains "$allowed_methods" "HEAD" + fi +} + +# ============================================================================= +# CloudFront Not Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | Distribution exists | null/empty (deleted) | +# +----------------------------------+----------------------------------------+ +assert_cloudfront_not_configured() { + local comment="$1" + + local distribution_json + distribution_json=$(aws_moto cloudfront list-distributions \ + --query "DistributionList.Items[?Comment=='$comment']" \ + --output json 2>/dev/null | jq '.[0]') + + # jq returns "null" when array is empty, treat as deleted + if [[ -z "$distribution_json" || "$distribution_json" == "null" ]]; then + return 0 + fi + + echo "Expected CloudFront distribution to be deleted" + echo "Actual: '$distribution_json'" + return 1 +} diff --git a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats similarity index 52% rename from frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats rename to frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats index ef7ade71..211b0b68 100644 --- a/frontend/deployment/tests/integration/cloudfront_lifecycle_test.bats +++ b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats @@ -1,17 +1,39 @@ #!/usr/bin/env bats # ============================================================================= -# Integration test: CloudFront Distribution Lifecycle +# Integration test: CloudFront + Route53 Lifecycle # -# Tests the full lifecycle: create infrastructure, verify it exists, -# then destroy it and verify cleanup. +# Tests the full lifecycle of a static frontend deployment: +# 1. Create infrastructure (CloudFront distribution + Route53 record) +# 2. Verify all resources are configured correctly +# 3. Destroy infrastructure +# 4. Verify all resources are removed +# ============================================================================= + +# ============================================================================= +# Test Constants +# ============================================================================= +# Expected values derived from context.json and terraform variables + +# CloudFront variables (distribution/cloudfront/modules/variables.tf) +TEST_DISTRIBUTION_BUCKET="assets-bucket" # distribution_bucket_name +TEST_DISTRIBUTION_S3_PREFIX="/tools/automation/v1.0.0" # distribution_s3_prefix +TEST_DISTRIBUTION_APP_NAME="automation-development-tools-7" # distribution_app_name +TEST_DISTRIBUTION_COMMENT="Distribution for automation-development-tools-7" # derived from app_name + +# Route53 variables (network/route53/modules/variables.tf) +TEST_NETWORK_DOMAIN="frontend.publicdomain.com" # network_domain +TEST_NETWORK_SUBDOMAIN="automation-development-tools" # network_subdomain +TEST_NETWORK_FULL_DOMAIN="automation-development-tools.frontend.publicdomain.com" # computed + +# ============================================================================= +# Test Setup # ============================================================================= setup_file() { - # Load integration helpers and initialize AWS/LocalStack configuration source "${PROJECT_ROOT}/testing/integration_helpers.sh" + source "${PROJECT_ROOT}/testing/assertions.sh" integration_setup --cloud-provider aws - # Clear any existing mocks clear_mocks # Create AWS prerequisites @@ -24,17 +46,16 @@ setup_file() { --key-schema AttributeName=LockID,KeyType=HASH \ --billing-mode PAY_PER_REQUEST >/dev/null 2>&1 || true aws_local route53 create-hosted-zone \ - --name frontend.publicdomain.com \ + --name "$TEST_NETWORK_DOMAIN" \ --caller-reference "test-$(date +%s)" >/dev/null 2>&1 || true - # Create ACM certificate for the test domain (required for CloudFront custom domain SSL) + # Create ACM certificate for the test domain aws_local acm request-certificate \ - --domain-name "*.frontend.publicdomain.com" \ + --domain-name "*.$TEST_NETWORK_DOMAIN" \ --validation-method DNS >/dev/null 2>&1 || true # Get hosted zone ID for context override HOSTED_ZONE_ID=$(aws_local route53 list-hosted-zones --query 'HostedZones[0].Id' --output text | sed 's|/hostedzone/||') - export HOSTED_ZONE_ID } @@ -44,20 +65,17 @@ teardown_file() { integration_teardown } -# Setup runs before each test setup() { source "${PROJECT_ROOT}/testing/integration_helpers.sh" + source "${PROJECT_ROOT}/testing/assertions.sh" + source "${BATS_TEST_DIRNAME}/cloudfront_assertions.bash" + source "${BATS_TEST_DIRNAME}/route53_assertions.bash" - # Clear mocks before each test clear_mocks - - # Load context load_context "frontend/deployment/tests/resources/context.json" - - # Override hosted zone ID with the one created in setup_file override_context "providers.cloud-providers.networking.hosted_public_zone_id" "$HOSTED_ZONE_ID" - # Export common environment variables + # Export environment variables export NETWORK_LAYER="route53" export DISTRIBUTION_LAYER="cloudfront" export TOFU_PROVIDER="aws" @@ -65,36 +83,34 @@ setup() { export TOFU_LOCK_TABLE="tofu-locks" export AWS_REGION="us-east-1" export SERVICE_PATH="$INTEGRATION_MODULE_ROOT/frontend" - - # Point to LocalStack-compatible modules - export CUSTOM_TOFU_MODULES="$BATS_TEST_DIRNAME/localstack" + export CUSTOM_TOFU_MODULES="$INTEGRATION_MODULE_ROOT/frontend/deployment/tests/integration/localstack" # Setup API mocks for np CLI calls - # Note: /token is automatically mocked by clear_mocks() local mocks_dir="frontend/deployment/tests/integration/mocks/" - - # Mock the np CLI internal API calls mock_request "GET" "/category" "$mocks_dir/asset_repository/category.json" mock_request "GET" "/provider_specification" "$mocks_dir/asset_repository/list_provider_spec.json" mock_request "GET" "/provider" "$mocks_dir/asset_repository/list_provider.json" mock_request "GET" "/provider/s3-asset-repository-id" "$mocks_dir/asset_repository/get_provider.json" mock_request "PATCH" "/scope/7" "$mocks_dir/scope/patch.json" - } # ============================================================================= # Test: Create Infrastructure # ============================================================================= -@test "create infrastructure deploys S3, CloudFront, and Route53 resources" { - - # Run the initial workflow +@test "create infrastructure deploys CloudFront and Route53 resources" { run_workflow "frontend/deployment/workflows/initial.yaml" - # Verify resources were created - assert_s3_bucket_exists "assets-bucket" - assert_cloudfront_exists "Distribution for automation-development-tools-7" - assert_route53_record_exists "automation-development-tools.frontend.publicdomain.com" "A" + assert_cloudfront_configured \ + "$TEST_DISTRIBUTION_COMMENT" \ + "$TEST_NETWORK_FULL_DOMAIN" \ + "$TEST_DISTRIBUTION_BUCKET" \ + "$TEST_DISTRIBUTION_S3_PREFIX" + + assert_route53_configured \ + "$TEST_NETWORK_FULL_DOMAIN" \ + "A" \ + "$HOSTED_ZONE_ID" } # ============================================================================= @@ -103,15 +119,12 @@ setup() { @test "destroy infrastructure removes CloudFront and Route53 resources" { # Disable CloudFront before deletion (required by AWS) - if [[ -f "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" ]]; then - "$BATS_TEST_DIRNAME/scripts/disable_cloudfront.sh" "Distribution for automation-development-tools-7" + if [[ -f "$BATS_TEST_DIRNAME/../../scripts/disable_cloudfront.sh" ]]; then + "$BATS_TEST_DIRNAME/../../scripts/disable_cloudfront.sh" "$TEST_DISTRIBUTION_COMMENT" fi - # Run the delete workflow run_workflow "frontend/deployment/workflows/delete.yaml" - # Verify resources were removed (S3 bucket should remain) - assert_s3_bucket_exists "assets-bucket" - assert_cloudfront_not_exists "Distribution for automation-development-tools-7" - assert_route53_record_not_exists "automation-development-tools.frontend.publicdomain.com" "A" + assert_cloudfront_not_configured "$TEST_DISTRIBUTION_COMMENT" + assert_route53_not_configured "$TEST_NETWORK_FULL_DOMAIN" "A" "$HOSTED_ZONE_ID" } diff --git a/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/route53_assertions.bash b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/route53_assertions.bash new file mode 100644 index 00000000..9b1e8720 --- /dev/null +++ b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/route53_assertions.bash @@ -0,0 +1,133 @@ +#!/bin/bash +# ============================================================================= +# Route53 Assertion Functions +# +# Provides assertion functions for validating Route53 record configuration +# in integration tests. +# +# Variables validated (from network/route53/modules/variables.tf): +# - network_hosted_zone_id -> Record is in the correct hosted zone +# - network_domain -> Part of full domain name +# - network_subdomain -> Part of full domain name (subdomain.domain) +# +# Usage: +# source "route53_assertions.bash" +# assert_route53_configured "full.domain.com" "A" "hosted-zone-id" +# ============================================================================= + +# ============================================================================= +# Route53 Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | Record exists | Non-empty record | +# | Record in correct hosted zone | expected_hosted_zone_id | +# | Record name | domain. (with trailing dot) | +# | Record type | A (exact match) | +# | Alias target hosted zone ID | Z2FDTNDATAQYW2 (CloudFront zone) | +# | Alias target DNS name | Contains cloudfront.net | +# | Evaluate target health | false | +# +----------------------------------+----------------------------------------+ +assert_route53_configured() { + local full_domain="$1" + local record_type="${2:-A}" + local expected_hosted_zone_id="$3" + + # Verify we're querying the correct hosted zone (network_hosted_zone_id) + local zone_id + if [[ -n "$expected_hosted_zone_id" ]]; then + zone_id="$expected_hosted_zone_id" + else + # Fallback to first hosted zone if not specified + zone_id=$(aws_local route53 list-hosted-zones \ + --query "HostedZones[0].Id" \ + --output text 2>/dev/null | sed 's|/hostedzone/||') + fi + + assert_not_empty "$zone_id" "Route53 hosted zone ID" + + # Verify the hosted zone exists + local zone_info + zone_info=$(aws_local route53 get-hosted-zone \ + --id "$zone_id" \ + --output json 2>/dev/null) + assert_not_empty "$zone_info" "Route53 hosted zone info" + + # Get Route53 record details + local record_name="${full_domain}." + local record_json + record_json=$(aws_local route53 list-resource-record-sets \ + --hosted-zone-id "$zone_id" \ + --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ + --output json 2>/dev/null | jq '.[0]') + + # Record exists + assert_not_empty "$record_json" "Route53 $record_type record" + + # Record name (with trailing dot) - validates network_domain + network_subdomain + local actual_name + actual_name=$(echo "$record_json" | jq -r '.Name') + assert_equal "$actual_name" "$record_name" + + # Record type + local actual_type + actual_type=$(echo "$record_json" | jq -r '.Type') + assert_equal "$actual_type" "$record_type" + + # Alias target hosted zone ID (CloudFront's global hosted zone) + local alias_hosted_zone_id + alias_hosted_zone_id=$(echo "$record_json" | jq -r '.AliasTarget.HostedZoneId // empty') + assert_equal "$alias_hosted_zone_id" "Z2FDTNDATAQYW2" + + # Alias target DNS name (CloudFront distribution domain) + # Note: LocalStack may return domain with or without trailing dot + local alias_target + alias_target=$(echo "$record_json" | jq -r '.AliasTarget.DNSName // empty') + assert_not_empty "$alias_target" "Route53 alias target" + assert_contains "$alias_target" "cloudfront.net" + + # Evaluate target health is false (CloudFront doesn't support health checks) + local evaluate_target_health + evaluate_target_health=$(echo "$record_json" | jq -r '.AliasTarget.EvaluateTargetHealth') + assert_false "$evaluate_target_health" "Route53 evaluate target health" +} + +# ============================================================================= +# Route53 Not Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | Record exists | null/empty (deleted) | +# +----------------------------------+----------------------------------------+ +assert_route53_not_configured() { + local full_domain="$1" + local record_type="${2:-A}" + local expected_hosted_zone_id="$3" + + local zone_id + if [[ -n "$expected_hosted_zone_id" ]]; then + zone_id="$expected_hosted_zone_id" + else + zone_id=$(aws_local route53 list-hosted-zones \ + --query "HostedZones[0].Id" \ + --output text 2>/dev/null | sed 's|/hostedzone/||') + fi + + local record_name="${full_domain}." + local record_json + record_json=$(aws_local route53 list-resource-record-sets \ + --hosted-zone-id "$zone_id" \ + --query "ResourceRecordSets[?Name=='$record_name' && Type=='$record_type']" \ + --output json 2>/dev/null | jq '.[0]') + + # jq returns "null" when array is empty, treat as deleted + if [[ -z "$record_json" || "$record_json" == "null" ]]; then + return 0 + fi + + echo "Expected Route53 $record_type record to be deleted" + echo "Actual: '$record_json'" + return 1 +} diff --git a/frontend/deployment/tests/integration/volume/cache/machine.json b/frontend/deployment/tests/integration/volume/cache/machine.json deleted file mode 100644 index a76a7199..00000000 --- a/frontend/deployment/tests/integration/volume/cache/machine.json +++ /dev/null @@ -1 +0,0 @@ -{"machine_id": "dkr_6984afd086e1"} \ No newline at end of file diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem b/frontend/deployment/tests/integration/volume/cache/server.test.pem deleted file mode 100644 index d3a80887..00000000 --- a/frontend/deployment/tests/integration/volume/cache/server.test.pem +++ /dev/null @@ -1,168 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFbI7+EBqzmxtI -8GDIk5XOwXtw0c7+kmwc2aPXmVQD+LqQ3NwwUSSWkH2psjwkT13ZtLI7KRrj8IQn -RiR58ZW1gldlho7b2dvOwdnl/bSfrzoocceUKcGdnrEJ1O4MVKnL+ffw9GjpyzBp -jcRexzOOxJCRGORBaomiPm3YpLZJLX3ODyiOZSa/spdEpYeaDS9raB1d/iFg03RF -0L7GyzDo51dNI3SRXGoAFbAhPajtzrgCNLtTxUFkodqUgRoid7VrZWa4IlU6CzJK -eWmC6Y/Z120I8y0Fm/xCh+RPJ5yoyJ6GOqM2AWzB+t9Ew1T+KyHbUj1PQ/6y1Eg/ -1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3JsMDQGCCsGAQUFBwEBBCgwJjAkBggr -BgEFBQcwAYYYaHR0cDovL29jc3AuY29tb2RvY2EuY29tMA0GCSqGSIb3DQEBDAUA -A4IBAQAYh1HcdCE9nIrgJ7cz0C7M7PDmy14R3iJvm3WOnnL+5Nb+qh+cli3vA0p+ -rvSNb3I8QzvAP+u431yqqcau8vzY7qN7Q/aGNnwU4M309z/+3ri0ivCRlv79Q2R+ -/czSAaF9ffgZGclCKxO/WIu6pKJmBHaIkU4MiRTOok3JMrO66BQavHHxW/BBC5gA -CiIDEOUMsfnNkjcZ7Tvx5Dq2+UUTJnWvu6rvP3t3O9LEApE9GQDTF1w52z97GA1F -zZOFli9d31kWTz9RvdVFGD/tSo7oBmF0Ixa1DVBzJ0RHfxBdiSprhTEUxOipakyA -vGp4z7h/jnZymQyd/teRCBaho1+V ------END CERTIFICATE----- diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt b/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt deleted file mode 100644 index 626af51b..00000000 --- a/frontend/deployment/tests/integration/volume/cache/server.test.pem.crt +++ /dev/null @@ -1,140 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIMhDCCCmygAwIBAgIRAO/rUlOy5ZeSoNzstjyL90EwDQYJKoZIhvcNAQEMBQAw -SzELMAkGA1UEBhMCQVQxEDAOBgNVBAoTB1plcm9TU0wxKjAoBgNVBAMTIVplcm9T -U0wgUlNBIERvbWFpbiBTZWN1cmUgU2l0ZSBDQTAeFw0yNTEwMjIwMDAwMDBaFw0y -NjAxMjAyMzU5NTlaMCUxIzAhBgNVBAMTGmxvY2FsaG9zdC5sb2NhbHN0YWNrLmNs -b3VkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxWyO/hAas5sbSPBg -yJOVzsF7cNHO/pJsHNmj15lUA/i6kNzcMFEklpB9qbI8JE9d2bSyOyka4/CEJ0Yk -efGVtYJXZYaO29nbzsHZ5f20n686KHHHlCnBnZ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MnsQybQv0FfQKlERPSZ51eHnlAfV1SoPv10Yy+xUGUJ -5lhCLkMaTLTwJUdZ+gQek9QmRkpQgbLevni3/GcV4clXhB4PY9bpYrrWX1Uu6lzG -KAgEJTm4Diup8kyXHAc/DVL17e8vgg8CAwEAAaOB8jCB7zAfBgNVHSMEGDAWgBSg -EQojPpbxB+zirynvgqV/0DCktDAdBgNVHQ4EFgQUU3m/WqorSs9UgOHYm8Cd8rID -ZsswDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQFMAMBAf8wEQYDVR0gBAowCDAG -BgRVHSAAMEMGA1UdHwQ8MDowOKA2oDSGMmh0dHA6Ly9jcmwuY29tb2RvY2EuY29t -L0FBQUNlcnRpZmljYXRlU2VydmljZXMuY3JsMDQGCCsGAQUFBwEBBCgwJjAkBggr -BgEFBQcwAYYYaHR0cDovL29jc3AuY29tb2RvY2EuY29tMA0GCSqGSIb3DQEBDAUA -A4IBAQAYh1HcdCE9nIrgJ7cz0C7M7PDmy14R3iJvm3WOnnL+5Nb+qh+cli3vA0p+ -rvSNb3I8QzvAP+u431yqqcau8vzY7qN7Q/aGNnwU4M309z/+3ri0ivCRlv79Q2R+ -/czSAaF9ffgZGclCKxO/WIu6pKJmBHaIkU4MiRTOok3JMrO66BQavHHxW/BBC5gA -CiIDEOUMsfnNkjcZ7Tvx5Dq2+UUTJnWvu6rvP3t3O9LEApE9GQDTF1w52z97GA1F -zZOFli9d31kWTz9RvdVFGD/tSo7oBmF0Ixa1DVBzJ0RHfxBdiSprhTEUxOipakyA -vGp4z7h/jnZymQyd/teRCBaho1+V ------END CERTIFICATE----- \ No newline at end of file diff --git a/frontend/deployment/tests/integration/volume/cache/server.test.pem.key b/frontend/deployment/tests/integration/volume/cache/server.test.pem.key deleted file mode 100644 index 0e505e61..00000000 --- a/frontend/deployment/tests/integration/volume/cache/server.test.pem.key +++ /dev/null @@ -1,28 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0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R9GKgH3+ayBzB4U+EPxDh65SyZCoFFN+ -BEPaG2liGwKBgDIHR8eKHqxG6svQdjD3jX0b8ueHEPrgF1NIiv0hreP40xxtrnf3 -ohXFuD7zCW20lbzaFQLxseu0RSWEeCaR/+sYG4IC8Vq8S9zKInhUTkD4/SR3zXtb -vIEJ6Obie+XYfQOjcNz7iC00/qcZSM5vX8Bv8AGYgJjAug61iql9TBWdAoGBALHX -KEPpIDzB+aknrNbu7ddImJHTNNk9KeSLJ/EHi+p3RrxA1SlEuCozIDVVO31gRKWR -kvamc0gBbOyuciEcClGsVNiimxAZdQ/S7y1oGqHklT+wnfrS0Mrai48VUe/aSnwP -67nMdy+Xc+JlUdD1tj1OwSKacb6bsTY/t4n1bUohAoGAO7C/PpSoku5ptn2WkfV8 -o6zNPlVNBlaIWmB5puRUyOrIm5MEnB4JZ4euG8TNx6cxKPqet5OLB9SHESb7rWLx -n1KohBwHn1Eccb2m1axCNw09sIrqAWueITWm39U3nDwkLNAwTWRX+BgoGcxDqV+X -UxHXh8CQGDKSwopZsJLZiUw= ------END PRIVATE KEY----- \ No newline at end of file diff --git a/integration_run.txt b/integration_run.txt new file mode 100644 index 00000000..25d769e7 --- /dev/null +++ b/integration_run.txt @@ -0,0 +1,578 @@ + +======================================== + Integration Tests (Containerized) +======================================== + +================================================================================ + Integration Test Helpers Reference +================================================================================ + +SETUP & TEARDOWN +---------------- + integration_setup --cloud-provider + Initialize integration test environment for the specified cloud provider. + Call this in setup_file(). + + integration_teardown + Clean up integration test environment. + Call this in teardown_file(). + +AWS LOCAL COMMANDS +------------------ + aws_local + Execute AWS CLI against LocalStack (S3, Route53, DynamoDB, etc.) + Example: aws_local s3 ls + + aws_moto + Execute AWS CLI against Moto (CloudFront) + Example: aws_moto cloudfront list-distributions + +WORKFLOW EXECUTION +------------------ + run_workflow "" + Run a nullplatform workflow file. + Path is relative to module root. + Example: run_workflow "frontend/deployment/workflows/initial.yaml" + +CONTEXT HELPERS +--------------- + load_context "" + Load a context JSON file into the CONTEXT environment variable. + Example: load_context "tests/resources/context.json" + + override_context "" "" + Override a value in the current CONTEXT. + Example: override_context "providers.networking.zone_id" "Z1234567890" + +API MOCKING (Smocker) +--------------------- + clear_mocks + Clear all mocks and set up default mocks (token endpoint). + Call this at the start of each test. + + mock_request "" "" "" + Mock an API request using a response file. + File format: { "status": 200, "body": {...} } + Example: mock_request "GET" "/provider/123" "mocks/provider.json" + + mock_request "" "" '' + Mock an API request with inline response. + Example: mock_request "POST" "/deployments" 201 '{"id": "new"}' + + mock_request_with_query "" "" "" '' + Mock a request with query parameters. + Example: mock_request_with_query "GET" "/items" "type=foo" 200 '[...]' + + assert_mock_called "" "" + Assert that a mock endpoint was called. + Example: assert_mock_called "GET" "/provider/123" + + mock_call_count "" "" + Get the number of times a mock was called. + Example: count=$(mock_call_count "GET" "/provider/123") + +AWS ASSERTIONS +-------------- + assert_s3_bucket_exists "" + Assert an S3 bucket exists in LocalStack. + + assert_s3_bucket_not_exists "" + Assert an S3 bucket does not exist. + + assert_cloudfront_exists "" + Assert a CloudFront distribution exists (matched by comment). + + assert_cloudfront_not_exists "" + Assert a CloudFront distribution does not exist. + + assert_route53_record_exists "" "" + Assert a Route53 record exists. + Example: assert_route53_record_exists "app.example.com" "A" + + assert_route53_record_not_exists "" "" + Assert a Route53 record does not exist. + + assert_dynamodb_table_exists "" + Assert a DynamoDB table exists. + + assert_dynamodb_table_not_exists "" + Assert a DynamoDB table does not exist. + +GENERIC ASSERTIONS +------------------ + assert_success "" [""] + Assert a command succeeds (exit code 0). + + assert_failure "" [""] + Assert a command fails (non-zero exit code). + + assert_contains "" "" [""] + Assert a string contains a substring. + + assert_equals "" "" [""] + Assert two values are equal. + +ENVIRONMENT VARIABLES +--------------------- + LOCALSTACK_ENDPOINT LocalStack URL (default: http://localhost:4566) + MOTO_ENDPOINT Moto URL (default: http://localhost:5555) + SMOCKER_HOST Smocker admin URL (default: http://localhost:8081) + AWS_ENDPOINT_URL AWS endpoint for CLI (default: $LOCALSTACK_ENDPOINT) + INTEGRATION_MODULE_ROOT Root directory of the module being tested + +================================================================================ + +Building test runner container... +#1 [internal] load local bake definitions +#1 reading from stdin 605B done +#1 DONE 0.0s +#2 [internal] load build definition from Dockerfile.test-runner +#2 transferring dockerfile: 1.11kB done +#2 DONE 0.0s +#3 [internal] load metadata for docker.io/library/alpine:3.19 +#3 ... +#4 [auth] library/alpine:pull token for registry-1.docker.io +#4 DONE 0.0s +#3 [internal] load metadata for docker.io/library/alpine:3.19 +#3 DONE 1.4s +#5 [internal] load .dockerignore +#5 transferring context: 2B done +#5 DONE 0.0s +#6 [1/6] FROM docker.io/library/alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1 +#6 resolve docker.io/library/alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1 0.0s done +#6 DONE 0.0s +#7 [2/6] RUN apk add --no-cache bash curl jq git openssh docker-cli aws-cli ca-certificates ncurses +#7 CACHED +#8 [4/6] RUN apk add --no-cache --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community opentofu +#8 CACHED +#9 [5/6] RUN curl -fsSL https://cli.nullplatform.com/install.sh | sh +#9 CACHED +#10 [3/6] RUN apk add --no-cache bats +#10 CACHED +#11 [6/6] WORKDIR /workspace +#11 CACHED +#12 exporting to image +#12 exporting layers done +#12 exporting manifest sha256:31a3d8117773d7f0b2472bd936b8e33919a258f6e3e606e9c5da28d6a219485c done +#12 exporting config sha256:d37ea551f1fe3572130afbca0214c239ec4102c522fda4cbdf10d050d8eb06ca done +#12 exporting attestation manifest sha256:2bfbcda8b02e193750ddafe6b11eb153df63367bb8201b408f230892420aa0f1 done +#12 exporting manifest list sha256:708a03ba25f4fb1db02d9c3d92afef01583c7ce8c16a3ede282a623956c325ab done +#12 naming to docker.io/library/docker-test-runner:latest done +#12 unpacking to docker.io/library/docker-test-runner:latest done +#12 DONE 0.0s +#13 resolving provenance for metadata file +#13 DONE 0.0s + docker-test-runner Built + +Starting infrastructure services... + Network docker_integration-network Creating + Network docker_integration-network Created + Volume docker_localstack-data Creating + Volume docker_localstack-data Created + Container integration-localstack Creating + Container integration-moto Creating + Container integration-smocker Creating + Container integration-smocker Created + Container integration-nginx Creating + Container integration-moto Created + Container integration-localstack Created + Container integration-nginx Created + Container integration-smocker Starting + Container integration-moto Starting + Container integration-localstack Starting + Container integration-smocker Started + Container integration-nginx Starting + Container integration-localstack Started + Container integration-moto Started + Container integration-nginx Started +Waiting for services to be ready.. +All services ready + +[frontend] Running integration tests in ./frontend/deployment/tests/integration + +lifecycle_test.bats + create infrastructure deploys CloudFront and Route53 resources1/1 ✓ create infrastructure deploys CloudFront and Route53 resources + Loaded context from: frontend/deployment/tests/resources/context.json + Mock: GET /category -> 200 + Mock: GET /provider_specification -> 200 + Mock: GET /provider -> 200 + Mock: GET /provider/s3-asset-repository-id -> 200 + Mock: PATCH /scope/7 -> 200 + Running workflow: frontend/deployment/workflows/initial.yaml + 🚀Executing step: build_context + 🚀Executing step: setup_provider_layer + 🔍 Validating AWS provider configuration... + ✅ AWS_REGION=us-east-1 + ✅ TOFU_PROVIDER_BUCKET=tofu-state-bucket + ✅ TOFU_LOCK_TABLE=tofu-locks + ✨ AWS provider configured successfully + + 🚀Executing step: setup_network_layer + 🔍 Validating Route53 network configuration... + ✅ hosted_zone_id=GDYKKDE6GH3RGMW5WN3FDP + + 📡 Fetching domain from Route 53 hosted zone... + ✅ domain=frontend.publicdomain.com + ✅ subdomain=automation-development-tools + + 📝 Setting scope domain to 'automation-development-tools.frontend.publicdomain.com'... + ✅ Scope domain set successfully + + ✨ Route53 network configured successfully + + 🚀Executing step: setup_distribution_layer + 🔍 Validating CloudFront distribution configuration... + ✅ app_name=automation-development-tools-7 + + 📡 Fetching assets-repository provider... + ✅ bucket_name=assets-bucket + ✅ s3_prefix=/tools/automation/v1.0.0 + + ✨ CloudFront distribution configured successfully + + 🚀Executing step: build_modules + Composing modules: /workspace/frontend/deployment/tests/integration/localstack,/workspace/frontend/deployment/provider/aws/modules,/workspace/frontend/deployment/network/route53/modules,/workspace/frontend/deployment/distribution/cloudfront/modules + Target directory: /tmp/temp-np-output-60069130/output/7 + + /workspace/frontend/deployment/tests/integration/localstack + provider_override.tf + ✓ Copied modules from: /workspace/frontend/deployment/tests/integration/localstack (prefix: integration_localstack_) + + /workspace/frontend/deployment/provider/aws/modules + provider.tf + provider.tftest.hcl + variables.tf + ✓ Copied modules from: /workspace/frontend/deployment/provider/aws/modules (prefix: aws_modules_) + + /workspace/frontend/deployment/network/route53/modules + locals.tf + main.tf + outputs.tf + route53.tftest.hcl + test_locals.tf + variables.tf + ✓ Copied modules from: /workspace/frontend/deployment/network/route53/modules (prefix: route53_modules_) + + /workspace/frontend/deployment/distribution/cloudfront/modules + cloudfront.tftest.hcl + data.tf + locals.tf + main.tf + outputs.tf + test_locals.tf + variables.tf + ✓ Copied modules from: /workspace/frontend/deployment/distribution/cloudfront/modules (prefix: cloudfront_modules_) + + ✓ All modules composed successfully + 🚀Executing step: tofu + + Initializing the backend... +  + Successfully configured the backend "s3"! OpenTofu will automatically + use this backend unless the backend configuration changes. + + Initializing provider plugins... + - terraform.io/builtin/terraform is built in to OpenTofu + - Finding hashicorp/aws versions matching "~> 5.0"... + - Installing hashicorp/aws v5.100.0... + - Installed hashicorp/aws v5.100.0 (signed, key ID 0C0AF313E5FD9F80) + + Providers are signed by their developers. + If you'd like to know more about provider signing, you can read about it here: + https://opentofu.org/docs/cli/plugins/signing/ + + OpenTofu has created a lock file .terraform.lock.hcl to record the provider + selections it made above. Include this file in your version control repository + so that OpenTofu can guarantee to make the same selections by default when + you run "tofu init" in the future. + + OpenTofu has been successfully initialized! +  + You may now begin working with OpenTofu. Try running "tofu plan" to see + any changes that are required for your infrastructure. All OpenTofu commands + should now work. + + If you ever set or change modules or backend configuration for OpenTofu, + rerun this command to reinitialize your working directory. If you forget, other + commands will detect it and remind you to do so if necessary. + data.aws_s3_bucket.static: Reading... + data.aws_caller_identity.current: Reading... + data.aws_acm_certificate.custom_domain[0]: Reading... + data.aws_acm_certificate.custom_domain[0]: Read complete after 0s [id=arn:aws:acm:us-east-1:000000000000:certificate/020dc254-9668-43fc-9d52-da98ca4c7c53] + data.aws_s3_bucket.static: Read complete after 0s [id=assets-bucket] + data.aws_caller_identity.current: Read complete after 0s [id=000000000000] + + OpenTofu used the selected providers to generate the following execution + plan. Resource actions are indicated with the following symbols: + + create + + OpenTofu will perform the following actions: + +  # aws_cloudfront_distribution.static will be created +  + resource "aws_cloudfront_distribution" "static" { + + aliases = [ + + "automation-development-tools.frontend.publicdomain.com", + ] + + arn = (known after apply) + + caller_reference = (known after apply) + + comment = "Distribution for automation-development-tools-7" + + continuous_deployment_policy_id = (known after apply) + + default_root_object = "index.html" + + domain_name = (known after apply) + + enabled = true + + etag = (known after apply) + + hosted_zone_id = (known after apply) + + http_version = "http2" + + id = (known after apply) + + in_progress_validation_batches = (known after apply) + + is_ipv6_enabled = true + + last_modified_time = (known after apply) + + price_class = "PriceClass_100" + + retain_on_delete = false + + staging = false + + status = (known after apply) + + tags = { + + "ManagedBy" = "terraform" + + "Module" = "distribution/cloudfront" + + "account" = "playground" + + "account_id" = "2" + + "application" = "automation" + + "application_id" = "4" + + "deployment_id" = "8" + + "namespace" = "tools" + + "namespace_id" = "3" + + "nullplatform" = "true" + + "scope" = "development-tools" + + "scope_id" = "7" + } + + tags_all = { + + "ManagedBy" = "terraform" + + "Module" = "distribution/cloudfront" + + "account" = "playground" + + "account_id" = "2" + + "application" = "automation" + + "application_id" = "4" + + "deployment_id" = "8" + + "namespace" = "tools" + + "namespace_id" = "3" + + "nullplatform" = "true" + + "scope" = "development-tools" + + "scope_id" = "7" + } + + trusted_key_groups = (known after apply) + + trusted_signers = (known after apply) + + wait_for_deployment = true + + + custom_error_response { + + error_code = 403 + + response_code = 200 + + response_page_path = "/index.html" + } + + custom_error_response { + + error_code = 404 + + response_code = 200 + + response_page_path = "/index.html" + } + + + default_cache_behavior { + + allowed_methods = [ + + "GET", + + "HEAD", + + "OPTIONS", + ] + + cached_methods = [ + + "GET", + + "HEAD", + ] + + compress = true + + default_ttl = 3600 + + max_ttl = 86400 + + min_ttl = 0 + + target_origin_id = "S3-assets-bucket" + + trusted_key_groups = (known after apply) + + trusted_signers = (known after apply) + + viewer_protocol_policy = "redirect-to-https" + + + forwarded_values { + + headers = (known after apply) + + query_string = false + + query_string_cache_keys = (known after apply) + + + cookies { + + forward = "none" + + whitelisted_names = (known after apply) + } + } + + + grpc_config (known after apply) + } + + + ordered_cache_behavior { + + allowed_methods = [ + + "GET", + + "HEAD", + ] + + cached_methods = [ + + "GET", + + "HEAD", + ] + + compress = true + + default_ttl = 604800 + + max_ttl = 31536000 + + min_ttl = 86400 + + path_pattern = "/static/*" + + target_origin_id = "S3-assets-bucket" + + viewer_protocol_policy = "redirect-to-https" + + + forwarded_values { + + headers = (known after apply) + + query_string = false + + query_string_cache_keys = (known after apply) + + + cookies { + + forward = "none" + } + } + + + grpc_config (known after apply) + } + + + origin { + + connection_attempts = 3 + + connection_timeout = 10 + + domain_name = "assets-bucket.s3.us-east-1.amazonaws.com" + + origin_access_control_id = (known after apply) + + origin_id = "S3-assets-bucket" + + origin_path = "/tools/automation/v1.0.0" + } + + + restrictions { + + geo_restriction { + + locations = (known after apply) + + restriction_type = "none" + } + } + + + viewer_certificate { + + acm_certificate_arn = "arn:aws:acm:us-east-1:000000000000:certificate/020dc254-9668-43fc-9d52-da98ca4c7c53" + + minimum_protocol_version = "TLSv1.2_2021" + + ssl_support_method = "sni-only" + } + } + +  # aws_cloudfront_origin_access_control.static will be created +  + resource "aws_cloudfront_origin_access_control" "static" { + + arn = (known after apply) + + description = "OAC for automation-development-tools-7" + + etag = (known after apply) + + id = (known after apply) + + name = "automation-development-tools-7-oac" + + origin_access_control_origin_type = "s3" + + signing_behavior = "always" + + signing_protocol = "sigv4" + } + +  # aws_route53_record.main_alias[0] will be created +  + resource "aws_route53_record" "main_alias" { + + allow_overwrite = (known after apply) + + fqdn = (known after apply) + + id = (known after apply) + + name = "automation-development-tools.frontend.publicdomain.com" + + type = "A" + + zone_id = "GDYKKDE6GH3RGMW5WN3FDP" + + + alias { + + evaluate_target_health = false + + name = (known after apply) + + zone_id = (known after apply) + } + } + +  # aws_s3_bucket_policy.static will be created +  + resource "aws_s3_bucket_policy" "static" { + + bucket = "assets-bucket" + + id = (known after apply) + + policy = (known after apply) + } + +  # terraform_data.cloudfront_invalidation will be created +  + resource "terraform_data" "cloudfront_invalidation" { + + id = (known after apply) + + triggers_replace = [ + + "/tools/automation/v1.0.0", + ] + } + + Plan: 5 to add, 0 to change, 0 to destroy. +  + Changes to Outputs: + + distribution_bucket_arn = "arn:aws:s3:::assets-bucket" + + distribution_bucket_name = "assets-bucket" + + distribution_cloudfront_distribution_id = (known after apply) + + distribution_cloudfront_domain_name = (known after apply) + + distribution_record_type = "A" + + distribution_s3_prefix = "/tools/automation/v1.0.0" + + distribution_target_domain = (known after apply) + + distribution_target_zone_id = (known after apply) + + distribution_website_url = "https://automation-development-tools.frontend.publicdomain.com" + + network_fqdn = (known after apply) + + network_full_domain = "automation-development-tools.frontend.publicdomain.com" + + network_website_url = "https://automation-development-tools.frontend.publicdomain.com" + aws_cloudfront_origin_access_control.static: Creating... + aws_cloudfront_origin_access_control.static: Creation complete after 1s [id=S90W3RNS5SFCF] + aws_cloudfront_distribution.static: Creating... + aws_cloudfront_distribution.static: Still creating... [10s elapsed] + aws_cloudfront_distribution.static: Still creating... [20s elapsed] + aws_cloudfront_distribution.static: Still creating... [30s elapsed] + aws_cloudfront_distribution.static: Creation complete after 30s [id=Q2P6CSCO8XDGC] + terraform_data.cloudfront_invalidation: Creating... + terraform_data.cloudfront_invalidation: Provisioning with 'local-exec'... + terraform_data.cloudfront_invalidation (local-exec): Executing: ["/bin/sh" "-c" "aws cloudfront create-invalidation --endpoint-url http://moto:5000 --distribution-id Q2P6CSCO8XDGC --paths '/*'"] + aws_s3_bucket_policy.static: Creating... + aws_route53_record.main_alias[0]: Creating... + aws_s3_bucket_policy.static: Creation complete after 0s [id=assets-bucket] + terraform_data.cloudfront_invalidation (local-exec): { + terraform_data.cloudfront_invalidation (local-exec):  "Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/Q2P6CSCO8XDGC/invalidation/IYKLBMLGIZ8SL", + terraform_data.cloudfront_invalidation (local-exec):  "Invalidation": { + terraform_data.cloudfront_invalidation (local-exec):  "Id": "IYKLBMLGIZ8SL", + terraform_data.cloudfront_invalidation (local-exec):  "Status": "COMPLETED", + terraform_data.cloudfront_invalidation (local-exec):  "CreateTime": "2026-01-16T14:18:38.015000+00:00", + terraform_data.cloudfront_invalidation (local-exec):  "InvalidationBatch": { + terraform_data.cloudfront_invalidation (local-exec):  "Paths": { + terraform_data.cloudfront_invalidation (local-exec):  "Quantity": 1, + terraform_data.cloudfront_invalidation (local-exec):  "Items": [ + terraform_data.cloudfront_invalidation (local-exec):  "/*" + terraform_data.cloudfront_invalidation (local-exec):  ] + terraform_data.cloudfront_invalidation (local-exec):  }, + terraform_data.cloudfront_invalidation (local-exec):  "CallerReference": "cli-1768573118-409470" + terraform_data.cloudfront_invalidation (local-exec):  } + terraform_data.cloudfront_invalidation (local-exec):  } + terraform_data.cloudfront_invalidation (local-exec): } + terraform_data.cloudfront_invalidation: Creation complete after 0s [id=133293b4-6bfa-46fc-a238-038f04efa04d] + aws_route53_record.main_alias[0]: Still creating... [10s elapsed] + aws_route53_record.main_alias[0]: Creation complete after 15s [id=GDYKKDE6GH3RGMW5WN3FDP_automation-development-tools.frontend.publicdomain.com_A] +  + Apply complete! Resources: 5 added, 0 changed, 0 destroyed. +  + Outputs: + + distribution_bucket_arn = "arn:aws:s3:::assets-bucket" + distribution_bucket_name = "assets-bucket" + distribution_cloudfront_distribution_id = "Q2P6CSCO8XDGC" + distribution_cloudfront_domain_name = "dvgg2m1fpk30s.cloudfront.net" + distribution_record_type = "A" + distribution_s3_prefix = "/tools/automation/v1.0.0" + distribution_target_domain = "dvgg2m1fpk30s.cloudfront.net" + distribution_target_zone_id = "Z2FDTNDATAQYW2" + distribution_website_url = "https://automation-development-tools.frontend.publicdomain.com" + network_fqdn = "automation-development-tools.frontend.publicdomain.com" + network_full_domain = "automation-development-tools.frontend.publicdomain.com" + network_website_url = "https://automation-development-tools.frontend.publicdomain.com" + {} + +1 test, 0 failures + + +All integration tests passed! + +Stopping containers... From fc93b6d2016a8ef6d6c237c88717da43f7dc02b1 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 16 Jan 2026 14:58:40 -0300 Subject: [PATCH 30/40] Improve build_context_test --- .../deployment/{ => scripts}/build_context | 0 .../deployment/{ => scripts}/compose_modules | 0 frontend/deployment/{ => scripts}/do_tofu | 0 .../{ => scripts}/build_context_test.bats | 28 +++++++++---------- 4 files changed, 14 insertions(+), 14 deletions(-) rename frontend/deployment/{ => scripts}/build_context (100%) rename frontend/deployment/{ => scripts}/compose_modules (100%) rename frontend/deployment/{ => scripts}/do_tofu (100%) rename frontend/deployment/tests/{ => scripts}/build_context_test.bats (82%) diff --git a/frontend/deployment/build_context b/frontend/deployment/scripts/build_context similarity index 100% rename from frontend/deployment/build_context rename to frontend/deployment/scripts/build_context diff --git a/frontend/deployment/compose_modules b/frontend/deployment/scripts/compose_modules similarity index 100% rename from frontend/deployment/compose_modules rename to frontend/deployment/scripts/compose_modules diff --git a/frontend/deployment/do_tofu b/frontend/deployment/scripts/do_tofu similarity index 100% rename from frontend/deployment/do_tofu rename to frontend/deployment/scripts/do_tofu diff --git a/frontend/deployment/tests/build_context_test.bats b/frontend/deployment/tests/scripts/build_context_test.bats similarity index 82% rename from frontend/deployment/tests/build_context_test.bats rename to frontend/deployment/tests/scripts/build_context_test.bats index 210c29b7..1c49fe5a 100644 --- a/frontend/deployment/tests/build_context_test.bats +++ b/frontend/deployment/tests/scripts/build_context_test.bats @@ -19,13 +19,13 @@ scope_id=7 setup() { # Get the directory of the test file TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" - PROJECT_DIR="$(cd "$TEST_DIR/.." && pwd)" - PROJECT_ROOT="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../.." && pwd)" + PROJECT_ROOT="$(cd "$TEST_DIR/../../../.." && pwd)" # Load shared test utilities source "$PROJECT_ROOT/testing/assertions.sh" - CONTEXT=$(cat "$TEST_DIR/resources/context.json") + CONTEXT=$(cat "$PROJECT_DIR/tests/resources/context.json") SERVICE_PATH="$PROJECT_DIR" TEST_OUTPUT_DIR=$(mktemp -d) @@ -45,13 +45,13 @@ teardown() { # ============================================================================= run_build_context() { # Source the build_context script - source "$PROJECT_DIR/build_context" + source "$PROJECT_DIR/scripts/build_context" } # ============================================================================= # Test: TOFU_VARIABLES - verifies the entire JSON structure # ============================================================================= -@test "TOFU_VARIABLES matches expected structure" { +@test "Should generate TOFU_VARIABLES with expected structure" { run_build_context # Expected JSON - update this when adding new fields @@ -63,7 +63,7 @@ run_build_context() { # ============================================================================= # Test: TOFU_INIT_VARIABLES # ============================================================================= -@test "generates correct tf_state_key format" { +@test "Should generate correct tf_state_key format in TOFU_INIT_VARIABLES" { run_build_context # Should contain the expected backend-config key @@ -74,14 +74,14 @@ run_build_context() { # ============================================================================= # Test: TOFU_MODULE_DIR # ============================================================================= -@test "creates TOFU_MODULE_DIR with scope_id" { +@test "Should create TOFU_MODULE_DIR path with scope_id" { run_build_context # Should end with the scope_id assert_contains "$TOFU_MODULE_DIR" "$SERVICE_PATH/output/$scope_id" } -@test "TOFU_MODULE_DIR is created as directory" { +@test "Should create TOFU_MODULE_DIR as a directory" { run_build_context assert_directory_exists "$TOFU_MODULE_DIR" @@ -90,14 +90,14 @@ run_build_context() { # ============================================================================= # Test: MODULES_TO_USE initialization # ============================================================================= -@test "MODULES_TO_USE is empty by default" { +@test "Should initialize MODULES_TO_USE as empty by default" { unset CUSTOM_TOFU_MODULES run_build_context assert_empty "$MODULES_TO_USE" "MODULES_TO_USE" } -@test "MODULES_TO_USE inherits from CUSTOM_TOFU_MODULES" { +@test "Should inherit MODULES_TO_USE from CUSTOM_TOFU_MODULES" { export CUSTOM_TOFU_MODULES="custom/module1,custom/module2" run_build_context @@ -107,19 +107,19 @@ run_build_context() { # ============================================================================= # Test: exports are set # ============================================================================= -@test "exports TOFU_VARIABLES" { +@test "Should export TOFU_VARIABLES" { run_build_context assert_not_empty "$TOFU_VARIABLES" "TOFU_VARIABLES" } -@test "exports TOFU_INIT_VARIABLES" { +@test "Should export TOFU_INIT_VARIABLES" { run_build_context assert_not_empty "$TOFU_INIT_VARIABLES" "TOFU_INIT_VARIABLES" } -@test "exports TOFU_MODULE_DIR" { +@test "Should export TOFU_MODULE_DIR" { run_build_context assert_not_empty "$TOFU_MODULE_DIR" "TOFU_MODULE_DIR" @@ -128,7 +128,7 @@ run_build_context() { # ============================================================================= # Test: RESOURCE_TAGS_JSON - verifies the entire JSON structure # ============================================================================= -@test "RESOURCE_TAGS_JSON matches expected structure" { +@test "Should generate RESOURCE_TAGS_JSON with expected structure" { run_build_context # Expected JSON - update this when adding new fields From c827a14e54ed68b96d353382ef4e97f5aa5f7abe Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 16 Jan 2026 15:55:44 -0300 Subject: [PATCH 31/40] Add missing unit tests --- frontend/deployment/scripts/compose_modules | 52 ++- .../tests/scripts/compose_modules_test.bats | 353 ++++++++++++++++++ .../tests/scripts/do_tofu_test.bats | 231 ++++++++++++ frontend/deployment/tests/scripts/test.json | 2 + 4 files changed, 625 insertions(+), 13 deletions(-) create mode 100644 frontend/deployment/tests/scripts/compose_modules_test.bats create mode 100644 frontend/deployment/tests/scripts/do_tofu_test.bats create mode 100644 frontend/deployment/tests/scripts/test.json diff --git a/frontend/deployment/scripts/compose_modules b/frontend/deployment/scripts/compose_modules index 510232f4..06d9c2d9 100755 --- a/frontend/deployment/scripts/compose_modules +++ b/frontend/deployment/scripts/compose_modules @@ -14,40 +14,59 @@ script_dir="$(dirname "${BASH_SOURCE[0]}")" modules_dir="$script_dir" +echo "🔍 Validating module composition configuration..." + if [ -z "${MODULES_TO_USE:-}" ]; then - echo "✗ MODULES_TO_USE is not set" + echo "" + echo " ❌ MODULES_TO_USE is not set" + echo "" + echo " 🔧 How to fix:" + echo " • Ensure MODULES_TO_USE is set before calling compose_modules" + echo " • This is typically done by the setup scripts (provider, network, distribution)" + echo "" exit 1 fi if [ -z "${TOFU_MODULE_DIR:-}" ]; then - echo "✗ TOFU_MODULE_DIR is not set" + echo "" + echo " ❌ TOFU_MODULE_DIR is not set" + echo "" + echo " 🔧 How to fix:" + echo " • Ensure TOFU_MODULE_DIR is set before calling compose_modules" + echo " • This is typically done by the build_context script" + echo "" exit 1 fi mkdir -p "$TOFU_MODULE_DIR" -echo "Composing modules: $MODULES_TO_USE" -echo "Target directory: $TOFU_MODULE_DIR" +echo " ✅ modules=$MODULES_TO_USE" +echo " ✅ target=$TOFU_MODULE_DIR" echo "" IFS=',' read -ra modules <<< "$MODULES_TO_USE" for module in "${modules[@]}"; do module=$(echo "$module" | xargs) # trim whitespace - echo $module - - ls $module if [ ! -d "$module" ]; then - echo "✗ Module not found: $module" + echo " ❌ Module directory not found: $module" + echo "" + echo " 🔧 How to fix:" + echo " • Verify the module path is correct and the directory exists" + echo "" exit 1 fi + echo "📦 $module" + # Copy .tf files if they exist (with module prefix to avoid conflicts) if ls "$module"/*.tf 1> /dev/null 2>&1; then # Extract last two path components for prefix (e.g., "/path/to/state/aws" -> "state_aws_") parent=$(basename "$(dirname "$module")") leaf=$(basename "$module") prefix="${parent}_${leaf}_" + + copied_files=() for tf_file in "$module"/*.tf; do filename=$(basename "$tf_file") # Skip test-only files (test_*.tf) @@ -55,22 +74,29 @@ for module in "${modules[@]}"; do continue fi cp "$tf_file" "$TOFU_MODULE_DIR/${prefix}${filename}" + copied_files+=("$filename") + done + + for file in "${copied_files[@]}"; do + echo " $file" done - echo "✓ Copied modules from: $module (prefix: $prefix)" + echo " ✅ Copied ${#copied_files[@]} file(s) with prefix: $prefix" fi # Source setup script if it exists if [ -f "$module/setup" ]; then - echo " Running setup for: $module" + echo " 📡 Running setup script..." source "$module/setup" if [ $? -ne 0 ]; then - echo "✗ Setup failed for module: $module" + echo "" + echo " ❌ Setup script failed for module: $module" + echo "" exit 1 fi - echo "✓ Setup completed for: $module" + echo " ✅ Setup completed" fi echo "" done -echo "✓ All modules composed successfully" \ No newline at end of file +echo "✨ All modules composed successfully" \ No newline at end of file diff --git a/frontend/deployment/tests/scripts/compose_modules_test.bats b/frontend/deployment/tests/scripts/compose_modules_test.bats new file mode 100644 index 00000000..81ffd0bc --- /dev/null +++ b/frontend/deployment/tests/scripts/compose_modules_test.bats @@ -0,0 +1,353 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for compose_modules script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/scripts/compose_modules_test.bats +# ============================================================================= + +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/scripts/compose_modules" + + source "$PROJECT_ROOT/testing/assertions.sh" + + # Create temporary directories for testing + TEST_OUTPUT_DIR=$(mktemp -d) + TEST_MODULES_DIR=$(mktemp -d) + + export TOFU_MODULE_DIR="$TEST_OUTPUT_DIR" +} + +teardown() { + # Clean up temp directories + if [ -d "$TEST_OUTPUT_DIR" ]; then + rm -rf "$TEST_OUTPUT_DIR" + fi + if [ -d "$TEST_MODULES_DIR" ]; then + rm -rf "$TEST_MODULES_DIR" + fi +} + +# ============================================================================= +# Helper functions +# ============================================================================= +create_test_module() { + local module_path="$1" + local module_dir="$TEST_MODULES_DIR/$module_path" + mkdir -p "$module_dir" + echo "$module_dir" +} + +create_tf_file() { + local module_dir="$1" + local filename="$2" + local content="${3:-# Test terraform file}" + echo "$content" > "$module_dir/$filename" +} + +create_setup_script() { + local module_dir="$1" + local content="${2:-echo 'Setup executed'}" + echo "#!/bin/bash" > "$module_dir/setup" + echo "$content" >> "$module_dir/setup" + chmod +x "$module_dir/setup" +} + +# ============================================================================= +# Test: Required environment variables - Error messages +# ============================================================================= +@test "Should fail when MODULES_TO_USE is not set" { + unset MODULES_TO_USE + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "🔍 Validating module composition configuration..." + assert_contains "$output" " ❌ MODULES_TO_USE is not set" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Ensure MODULES_TO_USE is set before calling compose_modules" + assert_contains "$output" " • This is typically done by the setup scripts (provider, network, distribution)" +} + +@test "Should fail when TOFU_MODULE_DIR is not set" { + export MODULES_TO_USE="some/module" + unset TOFU_MODULE_DIR + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" "🔍 Validating module composition configuration..." + assert_contains "$output" " ❌ TOFU_MODULE_DIR is not set" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Ensure TOFU_MODULE_DIR is set before calling compose_modules" + assert_contains "$output" " • This is typically done by the build_context script" +} + +@test "Should fail when module directory does not exist" { + export MODULES_TO_USE="/nonexistent/module/path" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Module directory not found: /nonexistent/module/path" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Verify the module path is correct and the directory exists" +} + +# ============================================================================= +# Test: Validation success messages +# ============================================================================= +@test "Should display validation header message" { + local module_dir=$(create_test_module "test/module") + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "🔍 Validating module composition configuration..." +} + +@test "Should display modules and target in validation output" { + local module_dir=$(create_test_module "test/module") + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" " ✅ modules=$module_dir" + assert_contains "$output" " ✅ target=$TOFU_MODULE_DIR" +} + +# ============================================================================= +# Test: Module processing messages +# ============================================================================= +@test "Should display module path with package emoji when processing" { + local module_dir=$(create_test_module "network/route53") + create_tf_file "$module_dir" "main.tf" + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "📦 $module_dir" +} + +@test "Should display each copied file name" { + local module_dir=$(create_test_module "network/route53") + create_tf_file "$module_dir" "main.tf" + create_tf_file "$module_dir" "variables.tf" + create_tf_file "$module_dir" "outputs.tf" + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" " main.tf" + assert_contains "$output" " variables.tf" + assert_contains "$output" " outputs.tf" +} + +@test "Should display copy success message with file count and prefix" { + local module_dir=$(create_test_module "network/route53") + create_tf_file "$module_dir" "main.tf" + create_tf_file "$module_dir" "variables.tf" + create_tf_file "$module_dir" "outputs.tf" + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" " ✅ Copied 3 file(s) with prefix: network_route53_" +} + +@test "Should display setup script running message" { + local module_dir=$(create_test_module "provider/aws") + create_setup_script "$module_dir" "echo 'AWS provider configured'" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" " 📡 Running setup script..." +} + +@test "Should display setup completed message" { + local module_dir=$(create_test_module "provider/aws") + create_setup_script "$module_dir" "echo 'AWS provider configured'" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" " ✅ Setup completed" +} + +@test "Should display final success message" { + local module_dir=$(create_test_module "test/module") + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "✨ All modules composed successfully" +} + +# ============================================================================= +# Test: Setup script failure messages +# ============================================================================= +@test "Should display setup failed message when setup script fails" { + local module_dir=$(create_test_module "provider/aws") + # Use 'return 1' instead of 'exit 1' since sourced scripts that call exit + # will exit the entire parent script before the error message can be printed + create_setup_script "$module_dir" "echo 'Error during setup'; return 1" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " 📡 Running setup script..." + assert_contains "$output" " ❌ Setup script failed for module: $module_dir" +} + +# ============================================================================= +# Test: Module copying functionality +# ============================================================================= +@test "Should copy .tf files to TOFU_MODULE_DIR" { + local module_dir=$(create_test_module "network/route53") + create_tf_file "$module_dir" "main.tf" "resource \"aws_route53_record\" \"main\" {}" + create_tf_file "$module_dir" "variables.tf" "variable \"domain\" {}" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/network_route53_main.tf" + assert_file_exists "$TOFU_MODULE_DIR/network_route53_variables.tf" +} + +@test "Should skip test_*.tf files when copying" { + local module_dir=$(create_test_module "network/route53") + create_tf_file "$module_dir" "main.tf" + create_tf_file "$module_dir" "test_locals.tf" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/network_route53_main.tf" + assert_file_not_exists "$TOFU_MODULE_DIR/network_route53_test_locals.tf" +} + +@test "Should use correct prefix based on parent and leaf directory names" { + local module_dir=$(create_test_module "provider/aws") + create_tf_file "$module_dir" "provider.tf" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/provider_aws_provider.tf" +} + +@test "Should handle modules with no .tf files" { + local module_dir=$(create_test_module "custom/empty") + # Don't create any .tf files + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "✨ All modules composed successfully" +} + +# ============================================================================= +# Test: Multiple modules +# ============================================================================= +@test "Should process multiple modules from comma-separated list" { + local module1=$(create_test_module "provider/aws") + local module2=$(create_test_module "network/route53") + create_tf_file "$module1" "provider.tf" + create_tf_file "$module2" "main.tf" + + export MODULES_TO_USE="$module1,$module2" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/provider_aws_provider.tf" + assert_file_exists "$TOFU_MODULE_DIR/network_route53_main.tf" + # Verify both modules were logged + assert_contains "$output" "📦 $module1" + assert_contains "$output" "📦 $module2" +} + +@test "Should handle whitespace in comma-separated module list" { + local module1=$(create_test_module "provider/aws") + local module2=$(create_test_module "network/route53") + create_tf_file "$module1" "provider.tf" + create_tf_file "$module2" "main.tf" + + export MODULES_TO_USE="$module1 , $module2" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/provider_aws_provider.tf" + assert_file_exists "$TOFU_MODULE_DIR/network_route53_main.tf" +} + +# ============================================================================= +# Test: Setup scripts execution +# ============================================================================= +@test "Should execute setup script and display its output" { + local module_dir=$(create_test_module "provider/aws") + create_setup_script "$module_dir" "echo 'Custom setup message from AWS provider'" + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "Custom setup message from AWS provider" +} + +@test "Should not fail if module has no setup script" { + local module_dir=$(create_test_module "custom/nosetup") + create_tf_file "$module_dir" "main.tf" + # Don't create a setup script + + export MODULES_TO_USE="$module_dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_contains "$output" "✨ All modules composed successfully" +} + +# ============================================================================= +# Test: TOFU_MODULE_DIR creation +# ============================================================================= +@test "Should create TOFU_MODULE_DIR if it does not exist" { + local module_dir=$(create_test_module "test/module") + export MODULES_TO_USE="$module_dir" + export TOFU_MODULE_DIR="$TEST_OUTPUT_DIR/nested/deep/dir" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_directory_exists "$TOFU_MODULE_DIR" +} diff --git a/frontend/deployment/tests/scripts/do_tofu_test.bats b/frontend/deployment/tests/scripts/do_tofu_test.bats new file mode 100644 index 00000000..962b13ac --- /dev/null +++ b/frontend/deployment/tests/scripts/do_tofu_test.bats @@ -0,0 +1,231 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for do_tofu script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/scripts/do_tofu_test.bats +# ============================================================================= + +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/scripts/do_tofu" + + source "$PROJECT_ROOT/testing/assertions.sh" + + # Create temporary directory for testing + TEST_OUTPUT_DIR=$(mktemp -d) + MOCK_BIN_DIR=$(mktemp -d) + + # Setup mock tofu command + cat > "$MOCK_BIN_DIR/tofu" << 'EOF' +#!/bin/bash +# Mock tofu command - logs calls to a file for verification +echo "tofu $*" >> "$TOFU_MOCK_LOG" +exit 0 +EOF + chmod +x "$MOCK_BIN_DIR/tofu" + + # Export environment variables + export TOFU_MODULE_DIR="$TEST_OUTPUT_DIR" + export TOFU_VARIABLES='{"key": "value", "number": 42}' + export TOFU_INIT_VARIABLES="-backend-config=bucket=test-bucket -backend-config=region=us-east-1" + export ACTION="apply" + export TOFU_MOCK_LOG="$TEST_OUTPUT_DIR/tofu_calls.log" + + # Add mock bin to PATH + export PATH="$MOCK_BIN_DIR:$PATH" +} + +teardown() { + # Clean up temp directories + if [ -d "$TEST_OUTPUT_DIR" ]; then + rm -rf "$TEST_OUTPUT_DIR" + fi + if [ -d "$MOCK_BIN_DIR" ]; then + rm -rf "$MOCK_BIN_DIR" + fi +} + +# ============================================================================= +# Test: tfvars file creation +# ============================================================================= +@test "Should write TOFU_VARIABLES to .tfvars.json file" { + export TOFU_VARIABLES='{"environment": "production", "replicas": 3}' + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MODULE_DIR/.tfvars.json" + + local content=$(cat "$TOFU_MODULE_DIR/.tfvars.json") + assert_equal "$content" '{"environment": "production", "replicas": 3}' + + # Verify it's valid JSON by parsing with jq + run jq '.' "$TOFU_MODULE_DIR/.tfvars.json" + assert_equal "$status" "0" +} + +# ============================================================================= +# Test: tofu init command +# ============================================================================= +@test "Should call tofu init with correct chdir" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_file_exists "$TOFU_MOCK_LOG" + + local init_call=$(grep "tofu -chdir=" "$TOFU_MOCK_LOG" | grep "init" | head -1) + assert_contains "$init_call" "-chdir=$TOFU_MODULE_DIR" +} + +@test "Should call tofu init with -input=false" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local init_call=$(grep "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$init_call" "-input=false" +} + +@test "Should call tofu init with TOFU_INIT_VARIABLES" { + export TOFU_INIT_VARIABLES="-backend-config=bucket=my-bucket -backend-config=key=state.tfstate" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local init_call=$(grep "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$init_call" "-backend-config=bucket=my-bucket" + assert_contains "$init_call" "-backend-config=key=state.tfstate" +} + +@test "Should call tofu init with empty TOFU_INIT_VARIABLES" { + export TOFU_INIT_VARIABLES="" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local init_call=$(grep "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$init_call" "init -input=false" +} + +# ============================================================================= +# Test: tofu action command +# ============================================================================= +@test "Should call tofu with ACTION=apply" { + export ACTION="apply" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "apply" +} + +@test "Should call tofu with ACTION=destroy" { + export ACTION="destroy" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "destroy" +} + +@test "Should call tofu with ACTION=plan" { + export ACTION="plan" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "plan" +} + +@test "Should call tofu action with -auto-approve" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "-auto-approve" +} + +@test "Should call tofu action with correct var-file path" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "-var-file=$TOFU_MODULE_DIR/.tfvars.json" +} + +@test "Should call tofu action with correct chdir" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + + local action_call=$(grep -v "init" "$TOFU_MOCK_LOG" | head -1) + assert_contains "$action_call" "-chdir=$TOFU_MODULE_DIR" +} + +# ============================================================================= +# Test: Command execution order +# ============================================================================= +@test "Should call tofu init before tofu action" { + run bash "$SCRIPT_PATH" + + assert_equal "$status" "0" + assert_command_order "$TOFU_MOCK_LOG" \ + "tofu -chdir=$TOFU_MODULE_DIR init" \ + "tofu -chdir=$TOFU_MODULE_DIR apply" +} + +# ============================================================================= +# Test: Error handling +# ============================================================================= +@test "Should fail if tofu init fails" { + # Create a failing mock + cat > "$MOCK_BIN_DIR/tofu" << 'EOF' +#!/bin/bash +if [[ "$*" == *"init"* ]]; then + echo "Error: Failed to initialize" >&2 + exit 1 +fi +exit 0 +EOF + chmod +x "$MOCK_BIN_DIR/tofu" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "1" +} + +@test "Should fail if tofu action fails" { + # Create a mock that fails on action + cat > "$MOCK_BIN_DIR/tofu" << 'EOF' +#!/bin/bash +if [[ "$*" == *"apply"* ]] || [[ "$*" == *"destroy"* ]] || [[ "$*" == *"plan"* ]]; then + if [[ "$*" != *"init"* ]]; then + echo "Error: Action failed" >&2 + exit 1 + fi +fi +exit 0 +EOF + chmod +x "$MOCK_BIN_DIR/tofu" + + run bash "$SCRIPT_PATH" + + assert_equal "$status" "1" +} diff --git a/frontend/deployment/tests/scripts/test.json b/frontend/deployment/tests/scripts/test.json new file mode 100644 index 00000000..e254e642 --- /dev/null +++ b/frontend/deployment/tests/scripts/test.json @@ -0,0 +1,2 @@ +tofu -chdir=/var/folders/lz/8bf63tz10kz930s8w1553s900000gq/T/tmp.iQhGScm139 init -input=false -backend-config=bucket=test-bucket -backend-config=region=us-east-1 +tofu -chdir=/var/folders/lz/8bf63tz10kz930s8w1553s900000gq/T/tmp.iQhGScm139 apply -auto-approve -var-file=/var/folders/lz/8bf63tz10kz930s8w1553s900000gq/T/tmp.iQhGScm139/.tfvars.json From a411d111bbfa4262e13566e9cd4017651645eaa7 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Fri, 16 Jan 2026 16:12:27 -0300 Subject: [PATCH 32/40] Improve workflow setup --- frontend/deployment/scripts/do_tofu | 2 +- .../tests/scripts/do_tofu_test.bats | 14 ++++----- frontend/deployment/workflows/delete.yaml | 30 +++---------------- frontend/deployment/workflows/initial.yaml | 10 +++---- 4 files changed, 17 insertions(+), 39 deletions(-) diff --git a/frontend/deployment/scripts/do_tofu b/frontend/deployment/scripts/do_tofu index 6ecc3cfc..4d65f044 100644 --- a/frontend/deployment/scripts/do_tofu +++ b/frontend/deployment/scripts/do_tofu @@ -10,4 +10,4 @@ CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") cd "$CURRENT_DIR" tofu -chdir="$TOFU_MODULE_DIR" init -input=false $TOFU_INIT_VARIABLES -tofu -chdir="$TOFU_MODULE_DIR" "$ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" +tofu -chdir="$TOFU_MODULE_DIR" "$TOFU_ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" diff --git a/frontend/deployment/tests/scripts/do_tofu_test.bats b/frontend/deployment/tests/scripts/do_tofu_test.bats index 962b13ac..67748dbd 100644 --- a/frontend/deployment/tests/scripts/do_tofu_test.bats +++ b/frontend/deployment/tests/scripts/do_tofu_test.bats @@ -35,7 +35,7 @@ EOF export TOFU_MODULE_DIR="$TEST_OUTPUT_DIR" export TOFU_VARIABLES='{"key": "value", "number": 42}' export TOFU_INIT_VARIABLES="-backend-config=bucket=test-bucket -backend-config=region=us-east-1" - export ACTION="apply" + export TOFU_ACTION="apply" export TOFU_MOCK_LOG="$TEST_OUTPUT_DIR/tofu_calls.log" # Add mock bin to PATH @@ -119,8 +119,8 @@ teardown() { # ============================================================================= # Test: tofu action command # ============================================================================= -@test "Should call tofu with ACTION=apply" { - export ACTION="apply" +@test "Should call tofu with TOFU_ACTION=apply" { + export TOFU_ACTION="apply" run bash "$SCRIPT_PATH" @@ -130,8 +130,8 @@ teardown() { assert_contains "$action_call" "apply" } -@test "Should call tofu with ACTION=destroy" { - export ACTION="destroy" +@test "Should call tofu with TOFU_ACTION=destroy" { + export TOFU_ACTION="destroy" run bash "$SCRIPT_PATH" @@ -141,8 +141,8 @@ teardown() { assert_contains "$action_call" "destroy" } -@test "Should call tofu with ACTION=plan" { - export ACTION="plan" +@test "Should call tofu with TOFU_ACTION=plan" { + export TOFU_ACTION="plan" run bash "$SCRIPT_PATH" diff --git a/frontend/deployment/workflows/delete.yaml b/frontend/deployment/workflows/delete.yaml index 05f33bcb..1fae8f33 100644 --- a/frontend/deployment/workflows/delete.yaml +++ b/frontend/deployment/workflows/delete.yaml @@ -1,26 +1,4 @@ -provider_categories: - - cloud-providers -steps: - - name: build_context - type: script - file: "$SERVICE_PATH/deployment/build_context" - output: - - name: TOFU_VARIABLES - type: environment - - name: setup_provider_layer - type: script - file: "$SERVICE_PATH/deployment/provider/$TOFU_PROVIDER/setup" - - name: setup_network_layer - type: script - file: "$SERVICE_PATH/deployment/network/$NETWORK_LAYER/setup" - - name: setup_distribution_layer - type: script - file: "$SERVICE_PATH/deployment/distribution/$DISTRIBUTION_LAYER/setup" - - name: build_modules - type: script - file: "$SERVICE_PATH/deployment/compose_modules" - - name: tofu - type: script - file: "$SERVICE_PATH/deployment/do_tofu" - configuration: - ACTION: "destroy" +include: + - "$SERVICE_PATH/deployment/workflows/initial.yaml" +configuration: + TOFU_ACTION: "destroy" \ No newline at end of file diff --git a/frontend/deployment/workflows/initial.yaml b/frontend/deployment/workflows/initial.yaml index bca07c63..abcdafca 100644 --- a/frontend/deployment/workflows/initial.yaml +++ b/frontend/deployment/workflows/initial.yaml @@ -1,9 +1,11 @@ provider_categories: - cloud-providers +configuration: + TOFU_ACTION: "apply" steps: - name: build_context type: script - file: "$SERVICE_PATH/deployment/build_context" + file: "$SERVICE_PATH/deployment/scripts/build_context" output: - name: TOFU_VARIABLES type: environment @@ -18,9 +20,7 @@ steps: file: "$SERVICE_PATH/deployment/distribution/$DISTRIBUTION_LAYER/setup" - name: build_modules type: script - file: "$SERVICE_PATH/deployment/compose_modules" + file: "$SERVICE_PATH/deployment/scripts/compose_modules" - name: tofu type: script - file: "$SERVICE_PATH/deployment/do_tofu" - configuration: - ACTION: "apply" \ No newline at end of file + file: "$SERVICE_PATH/deployment/scripts/do_tofu" \ No newline at end of file From e4eecec4a83cd8fc2f7f2d5f079941398246a6a4 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Mon, 19 Jan 2026 14:45:39 -0300 Subject: [PATCH 33/40] Azure without integration tests --- .../blob-cdn/modules/blob-cdn.tftest.hcl | 373 +++++++++++ .../distribution/blob-cdn/modules/locals.tf | 22 + .../distribution/blob-cdn/modules/main.tf | 206 +++---- .../distribution/blob-cdn/modules/outputs.tf | 44 ++ .../blob-cdn/modules/test_locals.tf | 51 ++ .../blob-cdn/modules/variables.tf | 27 + .../deployment/distribution/blob-cdn/setup | 123 +++- .../azure_dns/modules/azure_dns.tftest.hcl | 197 ++++++ .../network/azure_dns/modules/locals.tf | 7 + .../network/azure_dns/modules/main.tf | 95 +-- .../network/azure_dns/modules/outputs.tf | 14 + .../network/azure_dns/modules/test_locals.tf | 39 ++ .../network/azure_dns/modules/variables.tf | 15 + frontend/deployment/network/azure_dns/setup | 172 +++++- .../provider/azure/modules/provider.tf | 17 +- .../azure/modules/provider.tftest.hcl | 89 +++ .../provider/azure/modules/variables.tf | 16 +- frontend/deployment/provider/azure/setup | 65 +- frontend/deployment/scripts/do_tofu | 1 + .../distribution/blob-cdn/setup_test.bats | 202 ++++++ .../mocks/asset_repository/list_provider.json | 2 +- .../asset_repository/list_provider_spec.json | 7 + .../azure_asset_repository/get_provider.json | 17 + .../azure_asset_repository/list_provider.json | 18 + .../cdn_assertions.bash | 138 +++++ .../dns_assertions.bash | 105 ++++ .../lifecycle_test.bats | 135 ++++ .../tests/network/azure_dns/setup_test.bats | 289 +++++++++ .../tests/provider/azure/setup_test.bats | 203 ++++++ .../deployment/tests/resources/azure_mocks/az | 18 + .../azure_mocks/dns_zone/access_denied.json | 1 + .../dns_zone/credentials_error.json | 1 + .../azure_mocks/dns_zone/empty_name.json | 6 + .../dns_zone/invalid_subscription.json | 1 + .../azure_mocks/dns_zone/not_found.json | 1 + .../azure_mocks/dns_zone/success.json | 17 + .../azure_mocks/dns_zone/unknown_error.json | 1 + .../tests/resources/context_azure.json | 170 ++++++ .../no_storage_account_data.json | 12 + .../asset_repository_azure/success.json | 17 + integration_run.txt | 578 ------------------ .../azure-mock-provider/provider_override.tf | 2 +- testing/bin/az | 265 ++++++++ testing/docker/azure-mock/main.go | 2 +- testing/docker/certs/cert.pem | 45 +- testing/docker/certs/key.pem | 79 ++- 46 files changed, 3017 insertions(+), 888 deletions(-) create mode 100644 frontend/deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl create mode 100644 frontend/deployment/distribution/blob-cdn/modules/locals.tf create mode 100644 frontend/deployment/distribution/blob-cdn/modules/outputs.tf create mode 100644 frontend/deployment/distribution/blob-cdn/modules/test_locals.tf create mode 100644 frontend/deployment/distribution/blob-cdn/modules/variables.tf create mode 100644 frontend/deployment/network/azure_dns/modules/azure_dns.tftest.hcl create mode 100644 frontend/deployment/network/azure_dns/modules/locals.tf create mode 100644 frontend/deployment/network/azure_dns/modules/outputs.tf create mode 100644 frontend/deployment/network/azure_dns/modules/test_locals.tf create mode 100644 frontend/deployment/network/azure_dns/modules/variables.tf create mode 100644 frontend/deployment/provider/azure/modules/provider.tftest.hcl create mode 100644 frontend/deployment/tests/distribution/blob-cdn/setup_test.bats create mode 100644 frontend/deployment/tests/integration/mocks/azure_asset_repository/get_provider.json create mode 100644 frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider.json create mode 100644 frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash create mode 100644 frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/dns_assertions.bash create mode 100644 frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats create mode 100644 frontend/deployment/tests/network/azure_dns/setup_test.bats create mode 100644 frontend/deployment/tests/provider/azure/setup_test.bats create mode 100755 frontend/deployment/tests/resources/azure_mocks/az create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/access_denied.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/credentials_error.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/empty_name.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/invalid_subscription.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/not_found.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/success.json create mode 100644 frontend/deployment/tests/resources/azure_mocks/dns_zone/unknown_error.json create mode 100644 frontend/deployment/tests/resources/context_azure.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository_azure/no_storage_account_data.json create mode 100644 frontend/deployment/tests/resources/np_mocks/asset_repository_azure/success.json delete mode 100644 integration_run.txt create mode 100755 testing/bin/az diff --git a/frontend/deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl b/frontend/deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl new file mode 100644 index 00000000..8ae74d95 --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl @@ -0,0 +1,373 @@ +# ============================================================================= +# Unit tests for distribution/blob-cdn module +# +# Run: tofu test +# ============================================================================= + +mock_provider "azurerm" { + mock_data "azurerm_storage_account" { + defaults = { + primary_web_host = "mystaticstorage.z13.web.core.windows.net" + } + } + + mock_resource "azurerm_cdn_endpoint" { + defaults = { + id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Cdn/profiles/my-app-prod-cdn/endpoints/my-app-prod" + fqdn = "my-app-prod.azureedge.net" + } + } +} + +variables { + distribution_storage_account = "mystaticstorage" + distribution_container_name = "$web" + distribution_blob_prefix = "app/scope-1" + distribution_app_name = "my-app-prod" + network_full_domain = "" + network_domain = "" + distribution_resource_tags_json = { + Environment = "production" + Application = "my-app" + } + azure_provider = { + subscription_id = "00000000-0000-0000-0000-000000000000" + resource_group = "my-resource-group" + storage_account = "mytfstatestorage" + container = "tfstate" + } + provider_resource_tags_json = { + Team = "platform" + } +} + +# ============================================================================= +# Test: CDN Profile is created +# ============================================================================= +run "creates_cdn_profile" { + command = plan + + assert { + condition = azurerm_cdn_profile.static.name == "my-app-prod-cdn" + error_message = "CDN profile name should be 'my-app-prod-cdn'" + } + + assert { + condition = azurerm_cdn_profile.static.sku == "Standard_Microsoft" + error_message = "CDN profile SKU should be 'Standard_Microsoft'" + } + + assert { + condition = azurerm_cdn_profile.static.resource_group_name == "my-resource-group" + error_message = "CDN profile should be in 'my-resource-group'" + } +} + +# ============================================================================= +# Test: CDN Endpoint is created +# ============================================================================= +run "creates_cdn_endpoint" { + command = plan + + assert { + condition = azurerm_cdn_endpoint.static.name == "my-app-prod" + error_message = "CDN endpoint name should be 'my-app-prod'" + } + + assert { + condition = azurerm_cdn_endpoint.static.profile_name == "my-app-prod-cdn" + error_message = "CDN endpoint profile should be 'my-app-prod-cdn'" + } +} + +# ============================================================================= +# Test: CDN Endpoint origin configuration +# ============================================================================= +run "cdn_endpoint_origin_configuration" { + command = plan + + assert { + condition = azurerm_cdn_endpoint.static.origin_host_header == "mystaticstorage.z13.web.core.windows.net" + error_message = "Origin host header should be storage account primary web host" + } + + assert { + condition = one(azurerm_cdn_endpoint.static.origin).name == "blob-origin" + error_message = "Origin name should be 'blob-origin'" + } + + assert { + condition = one(azurerm_cdn_endpoint.static.origin).host_name == "mystaticstorage.z13.web.core.windows.net" + error_message = "Origin host name should be storage account primary web host" + } +} + +# ============================================================================= +# Test: No custom domain without network_full_domain +# ============================================================================= +run "no_custom_domain_without_network_domain" { + command = plan + + assert { + condition = local.distribution_has_custom_domain == false + error_message = "Should not have custom domain when network_full_domain is empty" + } + + assert { + condition = length(azurerm_cdn_endpoint_custom_domain.static) == 0 + error_message = "Should not create custom domain resource when network_full_domain is empty" + } +} + +# ============================================================================= +# Test: Custom domain with network_full_domain +# ============================================================================= +run "has_custom_domain_with_network_domain" { + command = plan + + variables { + network_full_domain = "cdn.example.com" + } + + assert { + condition = local.distribution_has_custom_domain == true + error_message = "Should have custom domain when network_full_domain is set" + } + + assert { + condition = local.distribution_full_domain == "cdn.example.com" + error_message = "Full domain should be 'cdn.example.com'" + } + + assert { + condition = length(azurerm_cdn_endpoint_custom_domain.static) == 1 + error_message = "Should create custom domain resource when network_full_domain is set" + } + + assert { + condition = azurerm_cdn_endpoint_custom_domain.static[0].host_name == "cdn.example.com" + error_message = "Custom domain host name should be 'cdn.example.com'" + } +} + +# ============================================================================= +# Test: Origin path normalization - removes double slashes +# ============================================================================= +run "origin_path_normalizes_leading_slash" { + command = plan + + variables { + distribution_blob_prefix = "/app" + } + + assert { + condition = local.distribution_origin_path == "/app" + error_message = "Origin path should be '/app' not '//app'" + } +} + +# ============================================================================= +# Test: Origin path normalization - adds leading slash if missing +# ============================================================================= +run "origin_path_adds_leading_slash" { + command = plan + + variables { + distribution_blob_prefix = "app" + } + + assert { + condition = local.distribution_origin_path == "/app" + error_message = "Origin path should add leading slash" + } +} + +# ============================================================================= +# Test: Origin path normalization - handles empty prefix +# ============================================================================= +run "origin_path_handles_empty" { + command = plan + + variables { + distribution_blob_prefix = "" + } + + assert { + condition = local.distribution_origin_path == "" + error_message = "Origin path should be empty when prefix is empty" + } +} + +# ============================================================================= +# Test: Origin path normalization - trims trailing slashes +# ============================================================================= +run "origin_path_trims_trailing_slash" { + command = plan + + variables { + distribution_blob_prefix = "/app/subfolder/" + } + + assert { + condition = local.distribution_origin_path == "/app/subfolder" + error_message = "Origin path should trim trailing slashes" + } +} + +# ============================================================================= +# Test: Cross-module locals for DNS integration +# ============================================================================= +run "cross_module_locals_for_dns" { + command = plan + + assert { + condition = local.distribution_record_type == "CNAME" + error_message = "Record type should be 'CNAME' for Azure CDN records" + } +} + +# ============================================================================= +# Test: Outputs from data source +# ============================================================================= +run "outputs_from_data_source" { + command = plan + + assert { + condition = output.distribution_storage_account == "mystaticstorage" + error_message = "distribution_storage_account should be 'mystaticstorage'" + } +} + +# ============================================================================= +# Test: Outputs from variables +# ============================================================================= +run "outputs_from_variables" { + command = plan + + assert { + condition = output.distribution_blob_prefix == "app/scope-1" + error_message = "distribution_blob_prefix should be 'app/scope-1'" + } + + assert { + condition = output.distribution_container_name == "$web" + error_message = "distribution_container_name should be '$web'" + } +} + +# ============================================================================= +# Test: DNS-related outputs +# ============================================================================= +run "dns_related_outputs" { + command = plan + + assert { + condition = output.distribution_record_type == "CNAME" + error_message = "distribution_record_type should be 'CNAME'" + } +} + +# ============================================================================= +# Test: Website URL without network domain +# ============================================================================= +run "website_url_without_network_domain" { + command = plan + + assert { + condition = startswith(output.distribution_website_url, "https://") + error_message = "distribution_website_url should start with 'https://'" + } +} + +# ============================================================================= +# Test: Website URL with network domain +# ============================================================================= +run "website_url_with_network_domain" { + command = plan + + variables { + network_full_domain = "cdn.example.com" + } + + assert { + condition = output.distribution_website_url == "https://cdn.example.com" + error_message = "distribution_website_url should be 'https://cdn.example.com'" + } +} + +# ============================================================================= +# Test: CDN endpoint has SPA routing delivery rule +# ============================================================================= +run "cdn_endpoint_has_spa_routing" { + command = plan + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[0].name == "sparouting" + error_message = "Should have sparouting delivery rule" + } + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[0].order == 1 + error_message = "SPA routing rule should have order 1" + } +} + +# ============================================================================= +# Test: CDN endpoint has static cache delivery rule +# ============================================================================= +run "cdn_endpoint_has_static_cache" { + command = plan + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[1].name == "staticcache" + error_message = "Should have staticcache delivery rule" + } + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[1].order == 2 + error_message = "Static cache rule should have order 2" + } +} + +# ============================================================================= +# Test: CDN endpoint has HTTPS redirect delivery rule +# ============================================================================= +run "cdn_endpoint_has_https_redirect" { + command = plan + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[2].name == "httpsredirect" + error_message = "Should have httpsredirect delivery rule" + } + + assert { + condition = azurerm_cdn_endpoint.static.delivery_rule[2].order == 3 + error_message = "HTTPS redirect rule should have order 3" + } +} + +# ============================================================================= +# Test: Custom domain has managed HTTPS +# ============================================================================= +run "custom_domain_has_managed_https" { + command = plan + + variables { + network_full_domain = "cdn.example.com" + } + + assert { + condition = azurerm_cdn_endpoint_custom_domain.static[0].cdn_managed_https[0].certificate_type == "Dedicated" + error_message = "Custom domain should use dedicated certificate" + } + + assert { + condition = azurerm_cdn_endpoint_custom_domain.static[0].cdn_managed_https[0].protocol_type == "ServerNameIndication" + error_message = "Custom domain should use SNI protocol" + } + + assert { + condition = azurerm_cdn_endpoint_custom_domain.static[0].cdn_managed_https[0].tls_version == "TLS12" + error_message = "Custom domain should use TLS 1.2" + } +} diff --git a/frontend/deployment/distribution/blob-cdn/modules/locals.tf b/frontend/deployment/distribution/blob-cdn/modules/locals.tf new file mode 100644 index 00000000..580c7228 --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/locals.tf @@ -0,0 +1,22 @@ +# ============================================================================= +# Locals for Azure CDN Distribution +# ============================================================================= + +locals { + # Use network_full_domain from network layer (provided via cross-module locals when composed) + distribution_has_custom_domain = local.network_full_domain != "" + distribution_full_domain = local.network_full_domain + + # Normalize blob_prefix: trim leading/trailing slashes, then add single leading slash if non-empty + distribution_blob_prefix_trimmed = trim(var.distribution_blob_prefix, "/") + distribution_origin_path = local.distribution_blob_prefix_trimmed != "" ? "/${local.distribution_blob_prefix_trimmed}" : "" + + distribution_tags = merge(var.distribution_resource_tags_json, { + ManagedBy = "terraform" + Module = "distribution/blob-cdn" + }) + + # Cross-module references (consumed by network/azure_dns) + distribution_target_domain = azurerm_cdn_endpoint.static.fqdn + distribution_record_type = "CNAME" +} diff --git a/frontend/deployment/distribution/blob-cdn/modules/main.tf b/frontend/deployment/distribution/blob-cdn/modules/main.tf index 152c1c22..775e8178 100644 --- a/frontend/deployment/distribution/blob-cdn/modules/main.tf +++ b/frontend/deployment/distribution/blob-cdn/modules/main.tf @@ -1,134 +1,99 @@ -# Azure Blob Storage + CDN Hosting -# Resources for Azure static hosting with CDN - -variable "distribution_app_name" { - description = "Application name" - type = string -} - -variable "distribution_environment" { - description = "Environment (dev, staging, prod)" - type = string - default = "prod" -} - -variable "distribution_location" { - description = "Azure region" - type = string - default = "eastus2" -} - -variable "distribution_custom_domain" { - description = "Custom domain (e.g., app.example.com)" - type = string - default = null -} - -variable "distribution_cdn_sku" { - description = "CDN Profile SKU" - type = string - default = "Standard_Microsoft" -} +# ============================================================================= +# Azure CDN Distribution +# +# Creates an Azure CDN profile and endpoint for static website hosting +# using Azure Blob Storage as the origin. +# ============================================================================= + +# Get storage account details +data "azurerm_storage_account" "static" { + name = var.distribution_storage_account + resource_group_name = var.azure_provider.resource_group +} + +# CDN Profile +resource "azurerm_cdn_profile" "static" { + name = "${var.distribution_app_name}-cdn" + location = "global" + resource_group_name = var.azure_provider.resource_group + sku = "Standard_Microsoft" -variable "distribution_tags" { - description = "Resource tags" - type = map(string) - default = {} + tags = local.distribution_tags } -locals { - distribution_storage_account_name = lower(replace("${var.distribution_app_name}${var.distribution_environment}static", "-", "")) - - distribution_default_tags = merge(var.distribution_tags, { - Application = var.distribution_app_name - Environment = var.distribution_environment - ManagedBy = "terraform" - }) -} +# CDN Endpoint +resource "azurerm_cdn_endpoint" "static" { + name = var.distribution_app_name + profile_name = azurerm_cdn_profile.static.name + location = "global" + resource_group_name = var.azure_provider.resource_group -resource "azurerm_resource_group" "main" { - name = "rg-${var.distribution_app_name}-${var.distribution_environment}" - location = var.distribution_location - tags = local.distribution_default_tags -} + origin_host_header = data.azurerm_storage_account.static.primary_web_host -resource "azurerm_storage_account" "static" { - name = substr(local.distribution_storage_account_name, 0, 24) - resource_group_name = azurerm_resource_group.main.name - location = azurerm_resource_group.main.location - account_tier = "Standard" - account_replication_type = "LRS" - account_kind = "StorageV2" - - static_website { - index_document = "index.html" - error_404_document = "index.html" + origin { + name = "blob-origin" + host_name = data.azurerm_storage_account.static.primary_web_host } - min_tls_version = "TLS1_2" - enable_https_traffic_only = true - allow_nested_items_to_be_public = false + # SPA routing - redirect 404s to index.html + delivery_rule { + name = "sparouting" + order = 1 - blob_properties { - versioning_enabled = true + url_file_extension_condition { + operator = "LessThan" + match_values = ["1"] + } - cors_rule { - allowed_headers = ["*"] - allowed_methods = ["GET", "HEAD", "OPTIONS"] - allowed_origins = ["*"] - exposed_headers = ["*"] - max_age_in_seconds = 3600 + url_rewrite_action { + destination = "/index.html" + preserve_unmatched_path = false + source_pattern = "/" } } - tags = local.distribution_default_tags -} - -resource "azurerm_cdn_profile" "main" { - name = "cdn-${var.distribution_app_name}-${var.distribution_environment}" - location = "global" - resource_group_name = azurerm_resource_group.main.name - sku = var.distribution_cdn_sku - tags = local.distribution_default_tags -} + # Cache configuration + delivery_rule { + name = "staticcache" + order = 2 -resource "azurerm_cdn_endpoint" "static" { - name = "${var.distribution_app_name}-${var.distribution_environment}" - profile_name = azurerm_cdn_profile.main.name - location = "global" - resource_group_name = azurerm_resource_group.main.name + url_path_condition { + operator = "BeginsWith" + match_values = ["/static/"] + } - origin { - name = "static-website" - host_name = azurerm_storage_account.static.primary_web_host + cache_expiration_action { + behavior = "Override" + duration = "7.00:00:00" + } } - origin_host_header = azurerm_storage_account.static.primary_web_host + # HTTPS redirect + delivery_rule { + name = "httpsredirect" + order = 3 - is_compression_enabled = true - content_types_to_compress = [ - "application/javascript", - "application/json", - "application/xml", - "text/css", - "text/html", - "text/javascript", - "text/plain", - "text/xml", - "image/svg+xml" - ] + request_scheme_condition { + operator = "Equal" + match_values = ["HTTP"] + } - querystring_caching_behaviour = "IgnoreQueryString" + url_redirect_action { + redirect_type = "Found" + protocol = "Https" + } + } - tags = local.distribution_default_tags + tags = local.distribution_tags } -resource "azurerm_cdn_endpoint_custom_domain" "main" { - count = var.distribution_custom_domain != null ? 1 : 0 +# Custom domain configuration (when network layer provides domain) +resource "azurerm_cdn_endpoint_custom_domain" "static" { + count = local.distribution_has_custom_domain ? 1 : 0 - name = replace(var.distribution_custom_domain, ".", "-") + name = "custom-domain" cdn_endpoint_id = azurerm_cdn_endpoint.static.id - host_name = var.distribution_custom_domain + host_name = local.distribution_full_domain cdn_managed_https { certificate_type = "Dedicated" @@ -136,28 +101,3 @@ resource "azurerm_cdn_endpoint_custom_domain" "main" { tls_version = "TLS12" } } - -output "distribution_resource_group_name" { - description = "Resource Group name" - value = azurerm_resource_group.main.name -} - -output "distribution_storage_account_name" { - description = "Storage Account name" - value = azurerm_storage_account.static.name -} - -output "distribution_cdn_endpoint_hostname" { - description = "CDN Endpoint hostname" - value = azurerm_cdn_endpoint.static.fqdn -} - -output "distribution_website_url" { - description = "Website URL" - value = var.distribution_custom_domain != null ? "https://${var.distribution_custom_domain}" : "https://${azurerm_cdn_endpoint.static.fqdn}" -} - -output "distribution_upload_command" { - description = "Command to upload files" - value = "az storage blob upload-batch --account-name ${azurerm_storage_account.static.name} --destination '$web' --source ./dist" -} diff --git a/frontend/deployment/distribution/blob-cdn/modules/outputs.tf b/frontend/deployment/distribution/blob-cdn/modules/outputs.tf new file mode 100644 index 00000000..d5a61e5a --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/outputs.tf @@ -0,0 +1,44 @@ +output "distribution_storage_account" { + description = "Azure Storage account name" + value = var.distribution_storage_account +} + +output "distribution_container_name" { + description = "Azure Storage container name" + value = var.distribution_container_name +} + +output "distribution_blob_prefix" { + description = "Blob prefix path for this scope" + value = var.distribution_blob_prefix +} + +output "distribution_cdn_profile_name" { + description = "Azure CDN profile name" + value = azurerm_cdn_profile.static.name +} + +output "distribution_cdn_endpoint_name" { + description = "Azure CDN endpoint name" + value = azurerm_cdn_endpoint.static.name +} + +output "distribution_cdn_endpoint_hostname" { + description = "Azure CDN endpoint hostname" + value = azurerm_cdn_endpoint.static.fqdn +} + +output "distribution_target_domain" { + description = "Target domain for DNS records (CDN endpoint hostname)" + value = local.distribution_target_domain +} + +output "distribution_record_type" { + description = "DNS record type (CNAME for Azure CDN)" + value = local.distribution_record_type +} + +output "distribution_website_url" { + description = "Website URL" + value = local.distribution_has_custom_domain ? "https://${local.distribution_full_domain}" : "https://${azurerm_cdn_endpoint.static.fqdn}" +} diff --git a/frontend/deployment/distribution/blob-cdn/modules/test_locals.tf b/frontend/deployment/distribution/blob-cdn/modules/test_locals.tf new file mode 100644 index 00000000..1826c78d --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/test_locals.tf @@ -0,0 +1,51 @@ +# ============================================================================= +# Test-only locals +# +# This file provides the network_* locals that are normally defined by the +# network layer (Azure DNS, etc.) when modules are composed. +# This file is only used for running isolated unit tests. +# +# NOTE: Files matching test_*.tf are skipped by compose_modules +# ============================================================================= + +# Test-only variables to allow tests to control the network values +variable "network_full_domain" { + description = "Test-only: Full domain from network layer (e.g., app.example.com)" + type = string + default = "" +} + +variable "network_domain" { + description = "Test-only: Root domain from network layer (e.g., example.com)" + type = string + default = "" +} + +variable "network_dns_zone_name" { + description = "Azure DNS zone name" + type = string + default = "" +} + +variable "network_subdomain" { + description = "Subdomain for the distribution" + type = string + default = "" +} + +variable "azure_provider" { + description = "Azure provider configuration" + type = object({ + subscription_id = string + resource_group = string + storage_account = string + container = string + }) +} + +locals { + # These locals are normally provided by network modules (e.g., Azure DNS) + # For testing, we bridge from variables to locals + network_full_domain = var.network_full_domain + network_domain = var.network_domain +} diff --git a/frontend/deployment/distribution/blob-cdn/modules/variables.tf b/frontend/deployment/distribution/blob-cdn/modules/variables.tf new file mode 100644 index 00000000..39bd4ccf --- /dev/null +++ b/frontend/deployment/distribution/blob-cdn/modules/variables.tf @@ -0,0 +1,27 @@ +variable "distribution_storage_account" { + description = "Azure Storage account name for static website distribution" + type = string +} + +variable "distribution_container_name" { + description = "Azure Storage container name (defaults to $web for static websites)" + type = string + default = "$web" +} + +variable "distribution_blob_prefix" { + description = "Blob path prefix for this scope's files (e.g., '/app-name/scope-id')" + type = string + default = "/" +} + +variable "distribution_app_name" { + description = "Application name (used for resource naming)" + type = string +} + +variable "distribution_resource_tags_json" { + description = "Resource tags as JSON object" + type = map(string) + default = {} +} \ No newline at end of file diff --git a/frontend/deployment/distribution/blob-cdn/setup b/frontend/deployment/distribution/blob-cdn/setup index 76eecc7a..c6841497 100755 --- a/frontend/deployment/distribution/blob-cdn/setup +++ b/frontend/deployment/distribution/blob-cdn/setup @@ -1,18 +1,127 @@ #!/bin/bash -# Azure Blob + CDN Hosting Setup -distribution_app_name=$(echo "$CONTEXT" | jq -r .application.slug) -distribution_environment=$(echo "$CONTEXT" | jq -r .scope.slug) +echo "🔍 Validating Azure CDN distribution configuration..." + +application_slug=$(echo "$CONTEXT" | jq -r .application.slug) +scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +scope_id=$(echo "$CONTEXT" | jq -r .scope.id) +asset_url=$(echo "$CONTEXT" | jq -r .asset.url) + +distribution_app_name="$application_slug-$scope_slug-$scope_id" +echo " ✅ app_name=$distribution_app_name" + +echo "" +echo " 📡 Fetching assets-repository provider..." + +nrn=$(echo "$CONTEXT" | jq -r .scope.nrn) + +asset_repository=$(np provider list --nrn "$nrn" --categories assets-repository --format json 2>&1) +np_exit_code=$? + +echo $asset_repository | jq . + +if [ $np_exit_code -ne 0 ]; then + echo "" + echo " ❌ Failed to fetch assets-repository provider" + echo "" + + if echo "$asset_repository" | grep -q "unauthorized\|forbidden\|401\|403"; then + echo " 🔒 Error: Permission denied" + echo "" + echo " 💡 Possible causes:" + echo " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: $nrn" + echo "" + echo " 🔧 How to fix:" + echo " 1. Ensure the API Key has 'Ops' permissions at the correct NRN hierarchy level" + + else + echo " 📋 Error details:" + echo "$asset_repository" | sed 's/^/ /' + fi + + echo "" + exit 1 +fi + +# Look for Azure Blob Storage provider (storage_account and container) +distribution_storage_account=$(echo "$asset_repository" | jq -r ' + [.results[] | select(.attributes.storage_account.name != null)] | first | .attributes.storage_account.name // empty +') + +distribution_container_name=$(echo "$asset_repository" | jq -r ' + [.results[] | select(.attributes.container.name != null)] | first | .attributes.container.name // empty +') + +if [ -z "$distribution_storage_account" ] || [ "$distribution_storage_account" = "null" ]; then + echo "" + echo " ❌ No assets-repository provider of type Azure Blob Storage at nrn: $nrn" + echo "" + + provider_count=$(echo "$asset_repository" | jq '.results | length') + + if [ "$provider_count" -gt 0 ]; then + echo " 🤔 Found $provider_count asset-repository provider(s), but none are configured for Azure Blob Storage." + echo "" + echo " 📋 Verify the existing providers with the nullplatform CLI:" + echo "$asset_repository" | jq -r '.results[] | " • np provider read --id \(.id) --format json"' 2>/dev/null + echo "" + fi + + echo " 🔧 How to fix:" + echo " 1. Ensure there is an asset-repository provider of type Azure Blob Storage configured at the correct NRN hierarchy level" + echo "" + exit 1 +fi + +echo " ✅ storage_account=$distribution_storage_account" +echo " ✅ container=${distribution_container_name:-"\$web"}" + +# Set default container to $web if not specified (Azure static website container) +distribution_container_name=${distribution_container_name:-"\$web"} + +# Blob path prefix for multi-scope storage support +# Extract path from asset_url (supports https:// format) +# Removes protocol and storage account/container prefix, keeps the path with leading slash +if [[ "$asset_url" == https://* ]]; then + # Extract path after the container name + distribution_blob_prefix="/${asset_url#https://*/*/}" +else + distribution_blob_prefix="/" +fi + +# Normalize empty prefix to just "/" +if [ "$distribution_blob_prefix" = "/" ] || [ -z "$distribution_blob_prefix" ]; then + distribution_blob_prefix="/" +fi + +echo " ✅ blob_prefix=${distribution_blob_prefix:-"(root)"}" + +RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg storage_account "$distribution_storage_account" \ + --arg container_name "$distribution_container_name" \ --arg app_name "$distribution_app_name" \ - --arg environment "$distribution_environment" \ + --argjson resource_tags_json "$RESOURCE_TAGS_JSON" \ + --arg blob_prefix "$distribution_blob_prefix" \ '. + { + distribution_storage_account: $storage_account, + distribution_container_name: $container_name, distribution_app_name: $app_name, - distribution_environment: $environment + distribution_blob_prefix: $blob_prefix, + distribution_resource_tags_json: $resource_tags_json }') +echo "" +echo "✨ Azure CDN distribution configured successfully" +echo "" + # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -module_name="${script_dir#*deployment/}" -MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi diff --git a/frontend/deployment/network/azure_dns/modules/azure_dns.tftest.hcl b/frontend/deployment/network/azure_dns/modules/azure_dns.tftest.hcl new file mode 100644 index 00000000..b5dd0088 --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/azure_dns.tftest.hcl @@ -0,0 +1,197 @@ +# ============================================================================= +# Unit tests for network/azure_dns module +# +# Run: tofu test +# ============================================================================= + +mock_provider "azurerm" { + mock_data "azurerm_dns_zone" { + defaults = { + id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Network/dnszones/example.com" + } + } +} + +variables { + network_dns_zone_name = "example.com" + network_domain = "example.com" + network_subdomain = "app" + + azure_provider = { + subscription_id = "00000000-0000-0000-0000-000000000000" + resource_group = "my-resource-group" + storage_account = "mytfstatestorage" + container = "tfstate" + } + + # These come from the distribution module (e.g., blob-cdn) + distribution_target_domain = "myapp.azureedge.net" + distribution_record_type = "CNAME" +} + +# ============================================================================= +# Test: Full domain is computed correctly with subdomain +# ============================================================================= +run "full_domain_with_subdomain" { + command = plan + + assert { + condition = local.network_full_domain == "app.example.com" + error_message = "Full domain should be 'app.example.com', got '${local.network_full_domain}'" + } +} + +# ============================================================================= +# Test: Full domain is computed correctly without subdomain (apex) +# ============================================================================= +run "full_domain_apex" { + command = plan + + variables { + network_subdomain = "" + } + + assert { + condition = local.network_full_domain == "example.com" + error_message = "Full domain should be 'example.com' for apex, got '${local.network_full_domain}'" + } +} + +# ============================================================================= +# Test: CNAME record is created for CNAME type +# ============================================================================= +run "creates_cname_record_for_type_cname" { + command = plan + + variables { + distribution_record_type = "CNAME" + } + + assert { + condition = length(azurerm_dns_cname_record.main) == 1 + error_message = "Should create one CNAME record" + } + + assert { + condition = length(azurerm_dns_a_record.main) == 0 + error_message = "Should not create A record when type is CNAME" + } +} + +# ============================================================================= +# Test: A record is created for A type +# ============================================================================= +run "creates_a_record_for_type_a" { + command = plan + + variables { + distribution_record_type = "A" + } + + assert { + condition = length(azurerm_dns_a_record.main) == 1 + error_message = "Should create one A record" + } + + assert { + condition = length(azurerm_dns_cname_record.main) == 0 + error_message = "Should not create CNAME record when type is A" + } +} + +# ============================================================================= +# Test: CNAME record configuration +# ============================================================================= +run "cname_record_configuration" { + command = plan + + variables { + distribution_record_type = "CNAME" + } + + assert { + condition = azurerm_dns_cname_record.main[0].zone_name == "example.com" + error_message = "CNAME record should use the correct DNS zone" + } + + assert { + condition = azurerm_dns_cname_record.main[0].name == "app" + error_message = "CNAME record name should be the subdomain" + } + + assert { + condition = azurerm_dns_cname_record.main[0].ttl == 300 + error_message = "CNAME TTL should be 300" + } + + assert { + condition = azurerm_dns_cname_record.main[0].record == "myapp.azureedge.net" + error_message = "CNAME record should point to distribution target domain" + } + + assert { + condition = azurerm_dns_cname_record.main[0].resource_group_name == "my-resource-group" + error_message = "CNAME record should be in the correct resource group" + } +} + +# ============================================================================= +# Test: A record configuration +# ============================================================================= +run "a_record_configuration" { + command = plan + + variables { + distribution_record_type = "A" + distribution_target_domain = "10.0.0.1" + } + + assert { + condition = azurerm_dns_a_record.main[0].zone_name == "example.com" + error_message = "A record should use the correct DNS zone" + } + + assert { + condition = azurerm_dns_a_record.main[0].name == "app" + error_message = "A record name should be the subdomain" + } + + assert { + condition = azurerm_dns_a_record.main[0].ttl == 300 + error_message = "A record TTL should be 300" + } + + assert { + condition = azurerm_dns_a_record.main[0].resource_group_name == "my-resource-group" + error_message = "A record should be in the correct resource group" + } +} + +# ============================================================================= +# Test: Outputs +# ============================================================================= +run "outputs_are_correct" { + command = plan + + assert { + condition = output.network_full_domain == "app.example.com" + error_message = "network_full_domain output should be 'app.example.com'" + } + + assert { + condition = output.network_website_url == "https://app.example.com" + error_message = "network_website_url output should be 'https://app.example.com'" + } +} + +# ============================================================================= +# Test: DNS zone variable is correctly passed +# ============================================================================= +run "dns_zone_variable_configuration" { + command = plan + + assert { + condition = var.network_dns_zone_name == "example.com" + error_message = "DNS zone name variable should be 'example.com'" + } +} diff --git a/frontend/deployment/network/azure_dns/modules/locals.tf b/frontend/deployment/network/azure_dns/modules/locals.tf new file mode 100644 index 00000000..b2cba907 --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/locals.tf @@ -0,0 +1,7 @@ +locals { + # Expose network_domain for cross-module use (e.g., certificate lookup) + network_domain = var.network_domain + + # Compute full domain from domain + subdomain + network_full_domain = var.network_subdomain != "" ? "${var.network_subdomain}.${var.network_domain}" : var.network_domain +} diff --git a/frontend/deployment/network/azure_dns/modules/main.tf b/frontend/deployment/network/azure_dns/modules/main.tf index 01252949..59380599 100644 --- a/frontend/deployment/network/azure_dns/modules/main.tf +++ b/frontend/deployment/network/azure_dns/modules/main.tf @@ -1,79 +1,34 @@ -# Azure DNS Configuration -# Creates DNS records pointing to hosting resources (CDN, Static Web Apps, etc.) +# ============================================================================= +# Azure DNS Network +# +# Creates DNS records in Azure DNS zone for the distribution endpoint. +# Supports both CNAME records (for CDN endpoints) and A records (for static IPs). +# ============================================================================= -variable "network_resource_group" { - description = "Resource group containing the DNS zone" - type = string +# Get DNS zone details +data "azurerm_dns_zone" "main" { + name = var.network_dns_zone_name + resource_group_name = var.azure_provider.resource_group } -variable "network_zone_name" { - description = "Azure DNS zone name" - type = string -} - -variable "network_domain" { - description = "Domain/subdomain for the application" - type = string -} - -variable "network_target_domain" { - description = "Target domain (for CNAME records)" - type = string -} - -variable "network_ttl" { - description = "DNS record TTL in seconds" - type = number - default = 300 -} - -variable "network_create_www" { - description = "Create www subdomain record as well" - type = bool - default = true -} - -variable "network_tags" { - description = "Resource tags" - type = map(string) - default = {} -} - -# CNAME record for main domain +# CNAME record for CDN endpoints resource "azurerm_dns_cname_record" "main" { - name = var.network_domain == var.network_zone_name ? "@" : replace(var.network_domain, ".${var.network_zone_name}", "") - zone_name = var.network_zone_name - resource_group_name = var.network_resource_group - ttl = var.network_ttl - record = var.network_target_domain + count = local.distribution_record_type == "CNAME" ? 1 : 0 - tags = var.network_tags + name = var.network_subdomain + zone_name = var.network_dns_zone_name + resource_group_name = var.azure_provider.resource_group + ttl = 300 + record = local.distribution_target_domain } -# WWW subdomain -resource "azurerm_dns_cname_record" "www" { - count = var.network_create_www ? 1 : 0 - - name = "www" - zone_name = var.network_zone_name - resource_group_name = var.network_resource_group - ttl = var.network_ttl - record = var.network_target_domain - - tags = var.network_tags -} - -output "network_domain" { - description = "Configured domain" - value = var.network_domain -} - -output "network_fqdn" { - description = "Fully qualified domain name" - value = azurerm_dns_cname_record.main.fqdn -} +# A record for static IPs (if needed in the future) +resource "azurerm_dns_a_record" "main" { + count = local.distribution_record_type == "A" ? 1 : 0 -output "network_website_url" { - description = "Website URL" - value = "https://${var.network_domain}" + name = var.network_subdomain + zone_name = var.network_dns_zone_name + resource_group_name = var.azure_provider.resource_group + ttl = 300 + records = [local.distribution_target_domain] } diff --git a/frontend/deployment/network/azure_dns/modules/outputs.tf b/frontend/deployment/network/azure_dns/modules/outputs.tf new file mode 100644 index 00000000..5b339bea --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/outputs.tf @@ -0,0 +1,14 @@ +output "network_full_domain" { + description = "Full domain name (subdomain.domain or just domain)" + value = local.network_full_domain +} + +output "network_fqdn" { + description = "Fully qualified domain name" + value = local.distribution_record_type == "CNAME" ? azurerm_dns_cname_record.main[0].fqdn : azurerm_dns_a_record.main[0].fqdn +} + +output "network_website_url" { + description = "Website URL" + value = "https://${local.network_full_domain}" +} diff --git a/frontend/deployment/network/azure_dns/modules/test_locals.tf b/frontend/deployment/network/azure_dns/modules/test_locals.tf new file mode 100644 index 00000000..1d4d7d47 --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/test_locals.tf @@ -0,0 +1,39 @@ +# ============================================================================= +# Test-only locals +# +# This file provides the distribution_* locals that are normally defined by the +# distribution layer (blob-cdn, etc.) when modules are composed. +# This file is only used for running isolated unit tests. +# +# NOTE: Files matching test_*.tf are skipped by compose_modules +# ============================================================================= + +# Test-only variables to allow tests to control the distribution values +variable "distribution_target_domain" { + description = "Test-only: Target domain from distribution provider" + type = string + default = "myapp.azureedge.net" +} + +variable "distribution_record_type" { + description = "Test-only: DNS record type (A or CNAME)" + type = string + default = "CNAME" +} + +variable "azure_provider" { + description = "Azure provider configuration" + type = object({ + subscription_id = string + resource_group = string + storage_account = string + container = string + }) +} + +locals { + # These locals are normally provided by distribution modules (e.g., blob-cdn) + # For testing, we bridge from variables to locals + distribution_target_domain = var.distribution_target_domain + distribution_record_type = var.distribution_record_type +} diff --git a/frontend/deployment/network/azure_dns/modules/variables.tf b/frontend/deployment/network/azure_dns/modules/variables.tf new file mode 100644 index 00000000..2c0c0fbd --- /dev/null +++ b/frontend/deployment/network/azure_dns/modules/variables.tf @@ -0,0 +1,15 @@ +variable "network_dns_zone_name" { + description = "Azure DNS zone name" + type = string +} + +variable "network_domain" { + description = "Root domain name (e.g., example.com)" + type = string +} + +variable "network_subdomain" { + description = "Subdomain prefix (e.g., 'app' for app.example.com, empty string for apex)" + type = string + default = "" +} \ No newline at end of file diff --git a/frontend/deployment/network/azure_dns/setup b/frontend/deployment/network/azure_dns/setup index 224600cb..6f55271d 100755 --- a/frontend/deployment/network/azure_dns/setup +++ b/frontend/deployment/network/azure_dns/setup @@ -1,43 +1,171 @@ #!/bin/bash -# Azure DNS Setup -# Configures DNS variables based on hosting output +echo "🔍 Validating Azure DNS network configuration..." -network_resource_group=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.dns_resource_group // empty') -network_zone_name=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.dns_zone_name // empty') +public_dns_zone_name=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.public_dns_zone_name // empty') -if [ -z "$network_resource_group" ]; then - echo "✗ dns_resource_group is not set in context" +if [ -z "$public_dns_zone_name" ]; then + echo "" + echo " ❌ public_dns_zone_name is not set in context" + echo "" + echo " 🔧 How to fix:" + echo " • Ensure there is an Azure cloud-provider configured at the correct NRN hierarchy level" + echo " • Set the 'public_dns_zone_name' field with the Azure DNS zone name" + echo "" exit 1 fi +echo " ✅ public_dns_zone_name=$public_dns_zone_name" -if [ -z "$network_zone_name" ]; then - echo "✗ dns_zone_name is not set in context" +application_slug=$(echo "$CONTEXT" | jq -r .application.slug) +scope_slug=$(echo "$CONTEXT" | jq -r .scope.slug) +scope_id=$(echo "$CONTEXT" | jq -r .scope.id) +public_dns_zone_resource_group_name=$(echo "$CONTEXT" | jq -r '.providers["cloud-providers"].networking.public_dns_zone_resource_group_name // empty') + +if [ -z "$public_dns_zone_resource_group_name" ]; then + echo "" + echo " ❌ public_dns_zone_resource_group_name is not set in context" + echo "" + echo " 🔧 How to fix:" + echo " • Ensure the Azure cloud-provider has 'public_dns_zone_resource_group_name' configured" + echo "" + exit 1 +fi +echo " ✅ public_dns_zone_resource_group_name=$public_dns_zone_resource_group_name" + +echo "" +echo " 📡 Verifying Azure DNS zone..." + +az_output=$(az network dns zone show --name "$public_dns_zone_name" --resource-group "$public_dns_zone_resource_group_name" 2>&1) +az_exit_code=$? + +if [ $az_exit_code -ne 0 ]; then + echo "" + echo " ❌ Failed to fetch Azure DNS zone information" + echo "" + + if echo "$az_output" | grep -q "ResourceNotFound\|NotFound"; then + echo " 🔎 Error: DNS zone '$public_dns_zone_name' does not exist in resource group '$public_dns_zone_resource_group_name'" + echo "" + echo " 💡 Possible causes:" + echo " • The DNS zone name is incorrect or has a typo" + echo " • The DNS zone was deleted" + echo " • The resource group is incorrect" + echo "" + echo " 🔧 How to fix:" + echo " • Verify the DNS zone exists: az network dns zone list --resource-group $public_dns_zone_resource_group_name" + echo " • Update 'public_dns_zone_name' in the Azure cloud-provider configuration" + + elif echo "$az_output" | grep -q "AuthorizationFailed\|Forbidden\|403"; then + echo " 🔒 Error: Permission denied when accessing Azure DNS" + echo "" + echo " 💡 Possible causes:" + echo " • The Azure credentials don't have DNS Zone read permissions" + echo " • The service principal is missing the 'DNS Zone Contributor' role" + echo "" + echo " 🔧 How to fix:" + echo " • Check the Azure credentials are configured correctly" + echo " • Ensure the service principal has 'DNS Zone Reader' or 'DNS Zone Contributor' role" + + elif echo "$az_output" | grep -q "InvalidSubscriptionId\|SubscriptionNotFound"; then + echo " ⚠️ Error: Invalid subscription" + echo "" + echo " 🔧 How to fix:" + echo " • Verify the Azure subscription is correct" + echo " • Check the service principal has access to the subscription" + + elif echo "$az_output" | grep -q "AADSTS\|InvalidAuthenticationToken\|ExpiredAuthenticationToken"; then + echo " 🔑 Error: Azure credentials issue" + echo "" + echo " 💡 Possible causes:" + echo " • The nullplatform agent is not configured with Azure credentials" + echo " • The service principal credentials have expired" + echo "" + echo " 🔧 How to fix:" + echo " • Configure Azure credentials in the nullplatform agent" + echo " • Verify the service principal credentials are valid" + + else + echo " 📋 Error details:" + echo "$az_output" | sed 's/^/ /' + fi + + echo "" exit 1 fi -# Get domain from scope or context -network_domain=$(echo "$TOFU_VARIABLES" | jq -r '.scope_domain // empty') -if [ -z "$network_domain" ]; then - network_domain=$(echo "$CONTEXT" | jq -r '.scope.domain // empty') +network_domain=$(echo "$az_output" | jq -r '.name') + +if [ -z "$network_domain" ] || [ "$network_domain" = "null" ]; then + echo "" + echo " ❌ Failed to extract domain name from DNS zone response" + echo "" + echo " 💡 Possible causes:" + echo " • The DNS zone does not have a valid domain name configured" + echo "" + echo " 🔧 How to fix:" + echo " • Verify the DNS zone has a valid domain: az network dns zone show --name $public_dns_zone_name --resource-group $public_dns_zone_resource_group_name" + echo "" + exit 1 fi -# Get target from hosting outputs (CDN endpoint, Static Web App hostname, etc.) -network_target_domain=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_cdn_endpoint_hostname // .distribution_default_hostname // empty') +echo " ✅ domain=$network_domain" + +network_subdomain="$application_slug-$scope_slug" +echo " ✅ subdomain=$network_subdomain" TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ - --arg resource_group "$network_resource_group" \ - --arg zone_name "$network_zone_name" \ + --arg dns_zone_name "$public_dns_zone_name" \ --arg domain "$network_domain" \ - --arg target_domain "$network_target_domain" \ + --arg subdomain "$network_subdomain" \ '. + { - network_resource_group: $resource_group, - network_zone_name: $zone_name, + network_dns_zone_name: $dns_zone_name, network_domain: $domain, - network_target_domain: $target_domain + network_subdomain: $subdomain }') +scope_domain="$network_subdomain.$network_domain" + +echo "" +echo " 📝 Setting scope domain to '$scope_domain'..." + +np_output=$(np scope patch --id "$scope_id" --body "{\"domain\":\"$scope_domain\"}" --format json 2>&1) +np_exit_code=$? + +if [ $np_exit_code -ne 0 ]; then + echo "" + echo " ❌ Failed to update scope domain" + echo "" + + if echo "$np_output" | grep -q "unauthorized\|forbidden\|401\|403"; then + echo " 🔒 Error: Permission denied" + echo "" + echo " 💡 Possible causes:" + echo " • The nullplatform API Key doesn't have 'Developer' permissions" + echo "" + echo " 🔧 How to fix:" + echo " • Ensure the API Key has 'Developer' permissions at the correct NRN hierarchy level" + + else + echo " 📋 Error details:" + echo "$np_output" | sed 's/^/ /' + fi + + echo "" + exit 1 +fi + +echo " ✅ Scope domain set successfully" + +echo "" +echo "✨ Azure DNS network configured successfully" +echo "" + # Add module to composition list script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -module_name="${script_dir#*deployment/}" -MODULES_TO_USE="${MODULES_TO_USE:+$MODULES_TO_USE,}$module_name" +module_name="${script_dir}/modules" + +if [[ -n $MODULES_TO_USE ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi diff --git a/frontend/deployment/provider/azure/modules/provider.tf b/frontend/deployment/provider/azure/modules/provider.tf index 285e92ea..e31e8d27 100644 --- a/frontend/deployment/provider/azure/modules/provider.tf +++ b/frontend/deployment/provider/azure/modules/provider.tf @@ -1,3 +1,7 @@ +# ============================================================================= +# Azure Provider Configuration +# ============================================================================= + terraform { required_version = ">= 1.4.0" @@ -8,10 +12,21 @@ terraform { } } - backend "azurerm" {} + backend "azurerm" { + # Backend configuration is provided via -backend-config flags: + # - storage_account_name + # - container_name + # - resource_group_name + # - key (provided by build_context) + } } provider "azurerm" { features {} + subscription_id = var.azure_provider.subscription_id + + default_tags { + tags = var.provider_resource_tags_json + } } diff --git a/frontend/deployment/provider/azure/modules/provider.tftest.hcl b/frontend/deployment/provider/azure/modules/provider.tftest.hcl new file mode 100644 index 00000000..19bc2bc1 --- /dev/null +++ b/frontend/deployment/provider/azure/modules/provider.tftest.hcl @@ -0,0 +1,89 @@ +# ============================================================================= +# Unit tests for provider/azure module +# +# Run: tofu test +# ============================================================================= + +mock_provider "azurerm" {} + +variables { + azure_provider = { + subscription_id = "00000000-0000-0000-0000-000000000000" + resource_group = "my-resource-group" + storage_account = "mytfstatestorage" + container = "tfstate" + } + + provider_resource_tags_json = { + Environment = "test" + Project = "frontend" + ManagedBy = "terraform" + } +} + +# ============================================================================= +# Test: Provider configuration is valid +# ============================================================================= +run "provider_configuration_is_valid" { + command = plan + + assert { + condition = var.azure_provider.subscription_id == "00000000-0000-0000-0000-000000000000" + error_message = "Azure subscription ID should match" + } + + assert { + condition = var.azure_provider.resource_group == "my-resource-group" + error_message = "Resource group should be my-resource-group" + } + + assert { + condition = var.azure_provider.storage_account == "mytfstatestorage" + error_message = "Storage account should be mytfstatestorage" + } + + assert { + condition = var.azure_provider.container == "tfstate" + error_message = "Container should be tfstate" + } +} + +# ============================================================================= +# Test: Default tags are configured +# ============================================================================= +run "default_tags_are_configured" { + command = plan + + assert { + condition = var.provider_resource_tags_json["Environment"] == "test" + error_message = "Environment tag should be 'test'" + } + + assert { + condition = var.provider_resource_tags_json["ManagedBy"] == "terraform" + error_message = "ManagedBy tag should be 'terraform'" + } +} + +# ============================================================================= +# Test: Required variables validation +# ============================================================================= +run "azure_provider_requires_subscription_id" { + command = plan + + variables { + azure_provider = { + subscription_id = "" + resource_group = "rg" + storage_account = "storage" + container = "container" + } + } + + # Empty subscription_id should still be syntactically valid but semantically wrong + # This tests that the variable structure is enforced + assert { + condition = var.azure_provider.subscription_id == "" + error_message = "Empty subscription_id should be accepted by variable type" + } +} diff --git a/frontend/deployment/provider/azure/modules/variables.tf b/frontend/deployment/provider/azure/modules/variables.tf index 843cad7a..78a78a8b 100644 --- a/frontend/deployment/provider/azure/modules/variables.tf +++ b/frontend/deployment/provider/azure/modules/variables.tf @@ -1,9 +1,15 @@ variable "azure_provider" { description = "Azure provider configuration" type = object({ - subscription_id = string - resource_group_name = string - storage_account_name = string - container_name = string + subscription_id = string + resource_group = string + storage_account = string + container = string }) -} \ No newline at end of file +} + +variable "provider_resource_tags_json" { + description = "Resource tags as JSON object - applied as default tags to all Azure resources" + type = map(string) + default = {} +} diff --git a/frontend/deployment/provider/azure/setup b/frontend/deployment/provider/azure/setup index ff2216d8..be430fbe 100755 --- a/frontend/deployment/provider/azure/setup +++ b/frontend/deployment/provider/azure/setup @@ -1,40 +1,53 @@ #!/bin/bash -if [ -z "${AZURE_SUBSCRIPTION_ID:-}" ]; then - echo "✗ AZURE_SUBSCRIPTION_ID is not set" +echo "🔍 Validating Azure provider configuration..." + +missing_vars=() + +function validate_env_var() { + local variable_name=$1 + local variable_value="${!variable_name}" + + if [ -z "$variable_value" ]; then + echo " ❌ $variable_name is missing" + missing_vars+=("$variable_name") + else + echo " ✅ $variable_name=$variable_value" + fi +} + +validate_env_var AZURE_SUBSCRIPTION_ID +validate_env_var AZURE_RESOURCE_GROUP +validate_env_var TOFU_PROVIDER_STORAGE_ACCOUNT +validate_env_var TOFU_PROVIDER_CONTAINER + +if [ ${#missing_vars[@]} -gt 0 ]; then + echo "" + echo " 🔧 How to fix:" + echo " Set the missing variable(s) in the nullplatform agent Helm installation:" + for var in "${missing_vars[@]}"; do + echo " • $var" + done + echo "" exit 1 fi -if [ -z "${TOFU_PROVIDER_RESOURCE_GROUP:-}" ]; then - echo "✗ TOFU_PROVIDER_RESOURCE_GROUP is not set" - exit 1 -fi - -if [ -z "${TOFU_PROVIDER_STORAGE_ACCOUNT:-}" ]; then - echo "✗ TOFU_PROVIDER_STORAGE_ACCOUNT is not set" - exit 1 -fi +echo "✨ Azure provider configured successfully" +echo "" -if [ -z "${TOFU_PROVIDER_CONTAINER:-}" ]; then - echo "✗ TOFU_PROVIDER_CONTAINER is not set" - exit 1 -fi +RESOURCE_TAGS_JSON=${RESOURCE_TAGS_JSON:-"{}"} TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ --arg subscription_id "$AZURE_SUBSCRIPTION_ID" \ - --arg resource_group "$TOFU_PROVIDER_RESOURCE_GROUP" \ + --arg resource_group "$AZURE_RESOURCE_GROUP" \ --arg storage_account "$TOFU_PROVIDER_STORAGE_ACCOUNT" \ --arg container "$TOFU_PROVIDER_CONTAINER" \ - '. + {azure_provider: { - subscription_id: $subscription_id, - resource_group_name: $resource_group, - storage_account_name: $storage_account, - container_name: $container - }}') - -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"resource_group_name=$TOFU_PROVIDER_RESOURCE_GROUP\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"storage_account_name=$TOFU_PROVIDER_STORAGE_ACCOUNT\"" -TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=\"container_name=$TOFU_PROVIDER_CONTAINER\"" + --argjson resource_tags_json "$RESOURCE_TAGS_JSON" \ + '. + {azure_provider: {subscription_id: $subscription_id, resource_group: $resource_group, storage_account: $storage_account, container: $container}, provider_resource_tags_json: $resource_tags_json}') + +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=storage_account_name=$TOFU_PROVIDER_STORAGE_ACCOUNT" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=container_name=$TOFU_PROVIDER_CONTAINER" +TOFU_INIT_VARIABLES="$TOFU_INIT_VARIABLES -backend-config=resource_group_name=$AZURE_RESOURCE_GROUP" script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" module_name="${script_dir}/modules" diff --git a/frontend/deployment/scripts/do_tofu b/frontend/deployment/scripts/do_tofu index 4d65f044..8d8d47d5 100644 --- a/frontend/deployment/scripts/do_tofu +++ b/frontend/deployment/scripts/do_tofu @@ -9,5 +9,6 @@ CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") cd "$CURRENT_DIR" +echo $TOFU_INIT_VARIABLES tofu -chdir="$TOFU_MODULE_DIR" init -input=false $TOFU_INIT_VARIABLES tofu -chdir="$TOFU_MODULE_DIR" "$TOFU_ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" diff --git a/frontend/deployment/tests/distribution/blob-cdn/setup_test.bats b/frontend/deployment/tests/distribution/blob-cdn/setup_test.bats new file mode 100644 index 00000000..61cc16cb --- /dev/null +++ b/frontend/deployment/tests/distribution/blob-cdn/setup_test.bats @@ -0,0 +1,202 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for distribution/blob-cdn/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/distribution/blob-cdn/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/distribution/blob-cdn/setup" + RESOURCES_DIR="$PROJECT_DIR/tests/resources" + MOCKS_DIR="$RESOURCES_DIR/np_mocks" + + # Load shared test utilities + source "$PROJECT_ROOT/testing/assertions.sh" + + # Add mock np to PATH (must be first) + export PATH="$MOCKS_DIR:$PATH" + + # Load context with Azure-specific asset URL + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools", "id": "7", "nrn": "organization=1:account=2:namespace=3:application=4:scope=7"}, + "asset": {"url": "https://mystaticstorage.blob.core.windows.net/$web/tools/automation/v1.0.0"} + }' + + # Initialize TOFU_VARIABLES with required fields + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export MODULES_TO_USE="" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_blob_cdn_setup() { + source "$SCRIPT_PATH" +} + +# ============================================================================= +# Test: Auth error case +# ============================================================================= +@test "Should handle permission denied error fetching the asset-repository-provider" { + set_np_mock "$MOCKS_DIR/asset_repository/auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to fetch assets-repository provider" + assert_contains "$output" " 🔒 Error: Permission denied" + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform API Key doesn't have 'Ops' permissions at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure the API Key has 'Ops' permissions at the correct NRN hierarchy level" +} + +# ============================================================================= +# Test: Unknown error case +# ============================================================================= +@test "Should handle unknown error fetching the asset-repository-provider" { + set_np_mock "$MOCKS_DIR/asset_repository/unknown_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to fetch assets-repository provider" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error fetching provider" +} + +# ============================================================================= +# Test: Empty results case +# ============================================================================= +@test "Should fail if no asset-repository found" { + set_np_mock "$MOCKS_DIR/asset_repository/no_data.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ No assets-repository provider of type Azure Blob Storage at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure there is an asset-repository provider of type Azure Blob Storage configured at the correct NRN hierarchy level" +} + +# ============================================================================= +# Test: No providers found case +# ============================================================================= +@test "Should fail when no asset provider is of type Azure Blob Storage" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/no_storage_account_data.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ No assets-repository provider of type Azure Blob Storage at nrn: organization=1:account=2:namespace=3:application=4:scope=7" + assert_contains "$output" " 🤔 Found 1 asset-repository provider(s), but none are configured for Azure Blob Storage." + + assert_contains "$output" " 📋 Verify the existing providers with the nullplatform CLI:" + assert_contains "$output" " • np provider read --id d397e46b-89b8-419d-ac14-2b483ace511c --format json" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " 1. Ensure there is an asset-repository provider of type Azure Blob Storage configured at the correct NRN hierarchy level" +} + +# ============================================================================= +# Test: Blob prefix extraction from asset URL +# ============================================================================= +@test "Should extract blob_prefix from asset.url with https format" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + + run_blob_cdn_setup + + local blob_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_blob_prefix') + assert_equal "$blob_prefix" "/tools/automation/v1.0.0" +} + +@test "Should use root prefix when asset.url has no path" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + + # Override asset.url in context with no path + export CONTEXT=$(echo "$CONTEXT" | jq '.asset.url = "other://bucket"') + + run_blob_cdn_setup + + local blob_prefix=$(echo "$TOFU_VARIABLES" | jq -r '.distribution_blob_prefix') + assert_equal "$blob_prefix" "/" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "Should add distribution variables to TOFU_VARIABLES" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + + run_blob_cdn_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "distribution_storage_account": "mystaticstorage", + "distribution_container_name": "$web", + "distribution_app_name": "automation-development-tools-7", + "distribution_blob_prefix": "/tools/automation/v1.0.0", + "distribution_resource_tags_json": {} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +@test "Should add distribution_resource_tags_json to TOFU_VARIABLES" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' + + run_blob_cdn_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "distribution_storage_account": "mystaticstorage", + "distribution_container_name": "$web", + "distribution_app_name": "automation-development-tools-7", + "distribution_blob_prefix": "/tools/automation/v1.0.0", + "distribution_resource_tags_json": {"Environment": "production", "Team": "platform"} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + + run_blob_cdn_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/distribution/blob-cdn/modules" +} + +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { + set_np_mock "$MOCKS_DIR/asset_repository_azure/success.json" + export MODULES_TO_USE="existing/module" + + run_blob_cdn_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/distribution/blob-cdn/modules" +} diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json index e7b3e9e7..e24ca793 100644 --- a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json +++ b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider.json @@ -8,7 +8,7 @@ "dimensions": {}, "groups": [], "id": "s3-asset-repository-id", - "nrn": "organization=1255165411:account=95118862", + "nrn": "organization=1:account=2", "specification_id": "s3-asset-repository-spec-id", "tags": [], "updated_at": "2026-01-07T16:28:17.036Z" diff --git a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json index c621bf02..e723a0fd 100644 --- a/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json +++ b/frontend/deployment/tests/integration/mocks/asset_repository/list_provider_spec.json @@ -8,6 +8,13 @@ "categories": [ {"slug": "assets-repository"} ] + }, + { + "id": "azure-asset-repository-spec-id", + "slug": "azure-assets", + "categories": [ + {"slug": "assets-repository"} + ] } ] } diff --git a/frontend/deployment/tests/integration/mocks/azure_asset_repository/get_provider.json b/frontend/deployment/tests/integration/mocks/azure_asset_repository/get_provider.json new file mode 100644 index 00000000..a15383a0 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/azure_asset_repository/get_provider.json @@ -0,0 +1,17 @@ +{ + "status": 200, + "body": { + "id": "azure-blob-asset-repository-id", + "specification_id": "azure-asset-repository-spec-id", + "category": "assets-repository-id", + "attributes": { + "storage_account": { + "name": "assetsaccount", + "resource_group": "test-resource-group" + }, + "container": { + "name": "assets" + } + } + } +} diff --git a/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider.json b/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider.json new file mode 100644 index 00000000..80e2e608 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider.json @@ -0,0 +1,18 @@ +{ + "status": 200, + "body": { + "results": [ + { + "category": "assets-repository-id", + "created_at": "2026-01-07T16:28:17.036Z", + "dimensions": {}, + "groups": [], + "id": "azure-blob-asset-repository-id", + "nrn": "organization=1:account=2", + "specification_id": "azure-asset-repository-spec-id", + "tags": [], + "updated_at": "2026-01-07T16:28:17.036Z" + } + ] + } +} diff --git a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash new file mode 100644 index 00000000..9d3383ad --- /dev/null +++ b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash @@ -0,0 +1,138 @@ +#!/bin/bash +# ============================================================================= +# Azure CDN Assertion Functions +# +# Provides assertion functions for validating Azure CDN endpoint +# configuration in integration tests using the Azure Mock API server. +# +# Variables validated (from distribution/azure_blob_cdn/modules/variables.tf): +# - distribution_storage_account -> Origin host +# - distribution_app_name -> CDN profile/endpoint name +# +# Usage: +# source "cdn_assertions.bash" +# assert_azure_cdn_configured "app-name" "storage-account" "sub-id" "rg" +# +# Note: Uses azure_mock() helper from integration_helpers.sh +# ============================================================================= + +# ============================================================================= +# Azure CDN Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | CDN Profile exists | Non-empty ID | +# | CDN Profile provisioning state | Succeeded | +# | CDN Endpoint exists | Non-empty ID | +# | CDN Endpoint provisioning state | Succeeded | +# | CDN Endpoint hostname | Contains azureedge.net | +# | Origin host contains | storage account name | +# +----------------------------------+----------------------------------------+ +assert_azure_cdn_configured() { + local app_name="$1" + local storage_account="$2" + local subscription_id="$3" + local resource_group="$4" + + # Derive CDN profile and endpoint names from app_name + # The terraform module uses: "${var.distribution_app_name}-cdn-profile" + local profile_name="${app_name}-cdn-profile" + local endpoint_name="${app_name}-cdn-endpoint" + + # Get CDN Profile + local profile_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}" + local profile_json + profile_json=$(azure_mock "$profile_path") + + # Profile exists + local profile_id + profile_id=$(echo "$profile_json" | jq -r '.id // empty') + assert_not_empty "$profile_id" "Azure CDN Profile ID" + + # Profile provisioning state + local profile_state + profile_state=$(echo "$profile_json" | jq -r '.properties.provisioningState // empty') + assert_equal "$profile_state" "Succeeded" + + # Get CDN Endpoint + local endpoint_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}/endpoints/${endpoint_name}" + local endpoint_json + endpoint_json=$(azure_mock "$endpoint_path") + + # Endpoint exists + local endpoint_id + endpoint_id=$(echo "$endpoint_json" | jq -r '.id // empty') + assert_not_empty "$endpoint_id" "Azure CDN Endpoint ID" + + # Endpoint provisioning state + local endpoint_state + endpoint_state=$(echo "$endpoint_json" | jq -r '.properties.provisioningState // empty') + assert_equal "$endpoint_state" "Succeeded" + + # Endpoint hostname contains azureedge.net + local hostname + hostname=$(echo "$endpoint_json" | jq -r '.properties.hostName // empty') + assert_not_empty "$hostname" "Azure CDN Endpoint hostname" + assert_contains "$hostname" "azureedge.net" + + # Origin host contains storage account name + local origin_host + origin_host=$(echo "$endpoint_json" | jq -r '.properties.origins[0].properties.hostName // empty') + assert_not_empty "$origin_host" "Azure CDN Origin host" + assert_contains "$origin_host" "$storage_account" +} + +# ============================================================================= +# Azure CDN Not Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | CDN Profile exists | null/empty (deleted) | +# | CDN Endpoint exists | null/empty (deleted) | +# +----------------------------------+----------------------------------------+ +assert_azure_cdn_not_configured() { + local app_name="$1" + local subscription_id="$2" + local resource_group="$3" + + local profile_name="${app_name}-cdn-profile" + local endpoint_name="${app_name}-cdn-endpoint" + + # Check CDN Profile is deleted + local profile_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}" + local profile_json + profile_json=$(azure_mock "$profile_path") + + local profile_error + profile_error=$(echo "$profile_json" | jq -r '.error.code // empty') + if [[ "$profile_error" != "ResourceNotFound" ]]; then + local profile_id + profile_id=$(echo "$profile_json" | jq -r '.id // empty') + if [[ -n "$profile_id" && "$profile_id" != "null" ]]; then + echo "Expected Azure CDN Profile to be deleted" + echo "Actual: '$profile_json'" + return 1 + fi + fi + + # Check CDN Endpoint is deleted + local endpoint_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}/endpoints/${endpoint_name}" + local endpoint_json + endpoint_json=$(azure_mock "$endpoint_path") + + local endpoint_error + endpoint_error=$(echo "$endpoint_json" | jq -r '.error.code // empty') + if [[ "$endpoint_error" != "ResourceNotFound" ]]; then + local endpoint_id + endpoint_id=$(echo "$endpoint_json" | jq -r '.id // empty') + if [[ -n "$endpoint_id" && "$endpoint_id" != "null" ]]; then + echo "Expected Azure CDN Endpoint to be deleted" + echo "Actual: '$endpoint_json'" + return 1 + fi + fi + + return 0 +} diff --git a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/dns_assertions.bash b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/dns_assertions.bash new file mode 100644 index 00000000..c970fc2a --- /dev/null +++ b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/dns_assertions.bash @@ -0,0 +1,105 @@ +#!/bin/bash +# ============================================================================= +# Azure DNS Assertion Functions +# +# Provides assertion functions for validating Azure DNS CNAME record +# configuration in integration tests using the Azure Mock API server. +# +# Variables validated (from network/azure_dns/modules/variables.tf): +# - network_domain -> DNS zone name +# - network_subdomain -> CNAME record name +# +# Usage: +# source "dns_assertions.bash" +# assert_azure_dns_configured "subdomain" "domain.com" "sub-id" "rg" +# +# Note: Uses azure_mock() helper from integration_helpers.sh +# ============================================================================= + +# ============================================================================= +# Azure DNS Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | CNAME Record exists | Non-empty ID | +# | Record name | expected subdomain | +# | CNAME target | Non-empty (points to CDN) | +# | TTL | > 0 | +# +----------------------------------+----------------------------------------+ +assert_azure_dns_configured() { + local subdomain="$1" + local zone_name="$2" + local subscription_id="$3" + local resource_group="$4" + + # Get DNS CNAME Record + local record_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Network/dnszones/${zone_name}/CNAME/${subdomain}" + local record_json + record_json=$(azure_mock "$record_path") + + # Record exists + local record_id + record_id=$(echo "$record_json" | jq -r '.id // empty') + assert_not_empty "$record_id" "Azure DNS CNAME Record ID" + + # Record name + local record_name + record_name=$(echo "$record_json" | jq -r '.name // empty') + assert_equal "$record_name" "$subdomain" + + # CNAME target (should point to CDN endpoint) + local cname_target + cname_target=$(echo "$record_json" | jq -r '.properties.CNAMERecord.cname // empty') + assert_not_empty "$cname_target" "Azure DNS CNAME target" + + # The CNAME should point to the Azure CDN endpoint (azureedge.net) + assert_contains "$cname_target" "azureedge.net" + + # TTL should be positive + local ttl + ttl=$(echo "$record_json" | jq -r '.properties.TTL // 0') + if [[ "$ttl" -le 0 ]]; then + echo "Expected TTL > 0, got $ttl" + return 1 + fi + + # FQDN should be set correctly + local fqdn + fqdn=$(echo "$record_json" | jq -r '.properties.fqdn // empty') + assert_contains "$fqdn" "${subdomain}.${zone_name}" +} + +# ============================================================================= +# Azure DNS Not Configured Assertion +# ============================================================================= +# +----------------------------------+----------------------------------------+ +# | Assertion | Expected Value | +# +----------------------------------+----------------------------------------+ +# | CNAME Record exists | null/empty (deleted) | +# +----------------------------------+----------------------------------------+ +assert_azure_dns_not_configured() { + local subdomain="$1" + local zone_name="$2" + local subscription_id="$3" + local resource_group="$4" + + # Check CNAME Record is deleted + local record_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Network/dnszones/${zone_name}/CNAME/${subdomain}" + local record_json + record_json=$(azure_mock "$record_path") + + local record_error + record_error=$(echo "$record_json" | jq -r '.error.code // empty') + if [[ "$record_error" != "ResourceNotFound" ]]; then + local record_id + record_id=$(echo "$record_json" | jq -r '.id // empty') + if [[ -n "$record_id" && "$record_id" != "null" ]]; then + echo "Expected Azure DNS CNAME Record to be deleted" + echo "Actual: '$record_json'" + return 1 + fi + fi + + return 0 +} diff --git a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats new file mode 100644 index 00000000..2f7cdb9b --- /dev/null +++ b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats @@ -0,0 +1,135 @@ +#!/usr/bin/env bats +# ============================================================================= +# Integration test: Azure BlobCDN + Azure DNS Lifecycle +# +# Tests the full lifecycle of a static frontend deployment on Azure: +# 1. Create infrastructure (CDN endpoint + DNS CNAME record) +# 2. Verify all resources are configured correctly +# 3. Destroy infrastructure +# 4. Verify all resources are removed +# ============================================================================= + +# ============================================================================= +# Test Constants +# ============================================================================= +# Expected values derived from context_azure.json and terraform variables + +# CDN variables (distribution/azure_blob_cdn/modules/variables.tf) +TEST_DISTRIBUTION_STORAGE_ACCOUNT="assetsaccount" # distribution_storage_account +TEST_DISTRIBUTION_CONTAINER="assets" # distribution_container +TEST_DISTRIBUTION_S3_PREFIX="/tools/automation/v1.0.0" # distribution_s3_prefix +TEST_DISTRIBUTION_APP_NAME="automation-development-tools-7" # distribution_app_name + +# DNS variables (network/azure_dns/modules/variables.tf) +TEST_NETWORK_DOMAIN="frontend.publicdomain.com" # network_domain +TEST_NETWORK_SUBDOMAIN="automation-development-tools" # network_subdomain +TEST_NETWORK_FULL_DOMAIN="automation-development-tools.frontend.publicdomain.com" # computed + +# Azure resource identifiers +TEST_SUBSCRIPTION_ID="mock-subscription-id" +TEST_RESOURCE_GROUP="test-resource-group" +TEST_DNS_ZONE_RESOURCE_GROUP="dns-resource-group" + +# ============================================================================= +# Test Setup +# ============================================================================= + +setup_file() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + source "${PROJECT_ROOT}/testing/assertions.sh" + integration_setup --cloud-provider azure + + clear_mocks + + # Pre-create Azure DNS zone in the mock server (required for data source lookup) + echo "Creating test prerequisites in Azure Mock..." + + # Create DNS zone via REST API + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_DNS_ZONE_RESOURCE_GROUP}/providers/Microsoft.Network/dnszones/${TEST_NETWORK_DOMAIN}" \ + '{"location": "global", "tags": {}}' >/dev/null 2>&1 || true + + # Create Storage Account via REST API (for data source lookup) + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_RESOURCE_GROUP}/providers/Microsoft.Storage/storageAccounts/${TEST_DISTRIBUTION_STORAGE_ACCOUNT}" \ + '{"location": "eastus", "kind": "StorageV2", "sku": {"name": "Standard_LRS", "tier": "Standard"}}' >/dev/null 2>&1 || true + + export TEST_SUBSCRIPTION_ID + export TEST_RESOURCE_GROUP + export TEST_DNS_ZONE_RESOURCE_GROUP +} + +teardown_file() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + clear_mocks + integration_teardown +} + +setup() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + source "${PROJECT_ROOT}/testing/assertions.sh" + source "${BATS_TEST_DIRNAME}/cdn_assertions.bash" + source "${BATS_TEST_DIRNAME}/dns_assertions.bash" + + clear_mocks + load_context "frontend/deployment/tests/resources/context_azure.json" + + # Export environment variables + export NETWORK_LAYER="azure_dns" + export DISTRIBUTION_LAYER="blob-cdn" + export TOFU_PROVIDER="azure" + export SERVICE_PATH="$INTEGRATION_MODULE_ROOT/frontend" + export CUSTOM_TOFU_MODULES="$INTEGRATION_MODULE_ROOT/testing/azure-mock-provider" + + # Azure provider required environment variables + export AZURE_SUBSCRIPTION_ID="$TEST_SUBSCRIPTION_ID" + export AZURE_RESOURCE_GROUP="$TEST_RESOURCE_GROUP" + # Use Azurite's storage account for backend (like LocalStack for AWS) + export TOFU_PROVIDER_STORAGE_ACCOUNT="${AZURITE_ACCOUNT_NAME:-devstoreaccount1}" + export TOFU_PROVIDER_CONTAINER="tfstate" + + # Setup API mocks for np CLI calls + local mocks_dir="frontend/deployment/tests/integration/mocks/" + mock_request "GET" "/category" "$mocks_dir/asset_repository/category.json" + mock_request "GET" "/provider_specification" "$mocks_dir/asset_repository/list_provider_spec.json" + mock_request "GET" "/provider" "$mocks_dir/azure_asset_repository/list_provider.json" + mock_request "GET" "/provider/azure-blob-asset-repository-id" "$mocks_dir/azure_asset_repository/get_provider.json" + mock_request "PATCH" "/scope/7" "$mocks_dir/scope/patch.json" +} + +# ============================================================================= +# Test: Create Infrastructure +# ============================================================================= + +#@test "create infrastructure deploys Azure CDN and DNS resources" { +# run_workflow "frontend/deployment/workflows/initial.yaml" +# +# assert_azure_cdn_configured \ +# "$TEST_DISTRIBUTION_APP_NAME" \ +# "$TEST_DISTRIBUTION_STORAGE_ACCOUNT" \ +# "$TEST_SUBSCRIPTION_ID" \ +# "$TEST_RESOURCE_GROUP" +# +# assert_azure_dns_configured \ +# "$TEST_NETWORK_SUBDOMAIN" \ +# "$TEST_NETWORK_DOMAIN" \ +# "$TEST_SUBSCRIPTION_ID" \ +# "$TEST_DNS_ZONE_RESOURCE_GROUP" +#} + +# ============================================================================= +# Test: Destroy Infrastructure +# ============================================================================= + +#@test "destroy infrastructure removes Azure CDN and DNS resources" { +# run_workflow "frontend/deployment/workflows/delete.yaml" +# +# assert_azure_cdn_not_configured \ +# "$TEST_DISTRIBUTION_APP_NAME" \ +# "$TEST_SUBSCRIPTION_ID" \ +# "$TEST_RESOURCE_GROUP" +# +# assert_azure_dns_not_configured \ +# "$TEST_NETWORK_SUBDOMAIN" \ +# "$TEST_NETWORK_DOMAIN" \ +# "$TEST_SUBSCRIPTION_ID" \ +# "$TEST_DNS_ZONE_RESOURCE_GROUP" +#} diff --git a/frontend/deployment/tests/network/azure_dns/setup_test.bats b/frontend/deployment/tests/network/azure_dns/setup_test.bats new file mode 100644 index 00000000..8b26ce02 --- /dev/null +++ b/frontend/deployment/tests/network/azure_dns/setup_test.bats @@ -0,0 +1,289 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for network/azure_dns/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/network/azure_dns/setup_test.bats +# ============================================================================= + +# Setup - runs before each test +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/network/azure_dns/setup" + RESOURCES_DIR="$PROJECT_DIR/tests/resources" + AZURE_MOCKS_DIR="$RESOURCES_DIR/azure_mocks" + NP_MOCKS_DIR="$RESOURCES_DIR/np_mocks" + + # Load shared test utilities + source "$PROJECT_ROOT/testing/assertions.sh" + + # Add mock az and np to PATH (must be first) + export PATH="$AZURE_MOCKS_DIR:$NP_MOCKS_DIR:$PATH" + + # Load context with public_dns_zone_name and public_dns_zone_resource_group_name + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools", "id": "7"}, + "providers": { + "cloud-providers": { + "networking": { + "public_dns_zone_resource_group_name": "my-resource-group", + "public_dns_zone_name": "example.com" + } + } + } + }' + + # Initialize TOFU_VARIABLES with existing keys to verify script merges (not replaces) + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export MODULES_TO_USE="" + + # Set default np scope patch mock (success) + export NP_MOCK_RESPONSE="$NP_MOCKS_DIR/scope/patch/success.json" + export NP_MOCK_EXIT_CODE="0" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_azure_dns_setup() { + source "$SCRIPT_PATH" +} + +# ============================================================================= +# Test: Required environment variables +# ============================================================================= +@test "Should fail when public_dns_zone_name is not present in context" { + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools"}, + "providers": { + "cloud-providers": { + "public_dns_zone_resource_group_name": "my-resource-group", + "networking": {} + } + } + }' + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ public_dns_zone_name is not set in context" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Ensure there is an Azure cloud-provider configured at the correct NRN hierarchy level" + assert_contains "$output" " • Set the 'public_dns_zone_name' field with the Azure DNS zone name" +} + +@test "Should fail when public_dns_zone_resource_group_name is not present in context" { + export CONTEXT='{ + "application": {"slug": "automation"}, + "scope": {"slug": "development-tools"}, + "providers": { + "cloud-providers": { + "networking": { + "public_dns_zone_name": "example.com" + } + } + } + }' + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ public_dns_zone_resource_group_name is not set in context" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Ensure the Azure cloud-provider has 'public_dns_zone_resource_group_name' configured" +} + +# ============================================================================= +# Test: ResourceNotFound error +# ============================================================================= +@test "Should fail if DNS zone does not exist" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/not_found.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to fetch Azure DNS zone information" + assert_contains "$output" " 🔎 Error: DNS zone 'example.com' does not exist in resource group 'my-resource-group'" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The DNS zone name is incorrect or has a typo" + assert_contains "$output" " • The DNS zone was deleted" + assert_contains "$output" " • The resource group is incorrect" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Verify the DNS zone exists: az network dns zone list --resource-group my-resource-group" + assert_contains "$output" " • Update 'public_dns_zone_name' in the Azure cloud-provider configuration" +} + +# ============================================================================= +# Test: AccessDenied error +# ============================================================================= +@test "Should fail if lacking permissions to read DNS zones" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/access_denied.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " 🔒 Error: Permission denied when accessing Azure DNS" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The Azure credentials don't have DNS Zone read permissions" + assert_contains "$output" " • The service principal is missing the 'DNS Zone Contributor' role" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Check the Azure credentials are configured correctly" + assert_contains "$output" " • Ensure the service principal has 'DNS Zone Reader' or 'DNS Zone Contributor' role" +} + +# ============================================================================= +# Test: InvalidSubscription error +# ============================================================================= +@test "Should fail if subscription is invalid" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/invalid_subscription.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ⚠️ Error: Invalid subscription" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Verify the Azure subscription is correct" + assert_contains "$output" " • Check the service principal has access to the subscription" +} + +# ============================================================================= +# Test: Credentials error +# ============================================================================= +@test "Should fail if Azure credentials are missing" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/credentials_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " 🔑 Error: Azure credentials issue" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform agent is not configured with Azure credentials" + assert_contains "$output" " • The service principal credentials have expired" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Configure Azure credentials in the nullplatform agent" + assert_contains "$output" " • Verify the service principal credentials are valid" +} + +# ============================================================================= +# Test: Unknown Azure DNS error +# ============================================================================= +@test "Should handle unknown error getting the Azure DNS zone" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/unknown_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error fetching Azure DNS zone." +} + +# ============================================================================= +# Test: Empty domain in response +# ============================================================================= +@test "Should handle missing DNS zone name from response" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/empty_name.json" + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to extract domain name from DNS zone response" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The DNS zone does not have a valid domain name configured" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Verify the DNS zone has a valid domain: az network dns zone show --name example.com --resource-group my-resource-group" +} + +# ============================================================================= +# Test: Scope patch error +# ============================================================================= +@test "Should handle auth error updating scope domain" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/success.json" + set_np_mock "$NP_MOCKS_DIR/scope/patch/auth_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_contains "$output" " ❌ Failed to update scope domain" + assert_contains "$output" " 🔒 Error: Permission denied" + + assert_contains "$output" " 💡 Possible causes:" + assert_contains "$output" " • The nullplatform API Key doesn't have 'Developer' permissions" + + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " • Ensure the API Key has 'Developer' permissions at the correct NRN hierarchy level" +} + +@test "Should handle unknown error updating scope domain" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/success.json" + set_np_mock "$NP_MOCKS_DIR/scope/patch/unknown_error.json" 1 + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ Failed to update scope domain" + assert_contains "$output" " 📋 Error details:" + assert_contains "$output" "Unknown error updating scope" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "Should add network variables to TOFU_VARIABLES" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/success.json" + + run_azure_dns_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "network_dns_zone_name": "example.com", + "network_domain": "example.com", + "network_subdomain": "automation-development-tools" +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/success.json" + + run_azure_dns_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/network/azure_dns/modules" +} + +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { + set_az_mock "$AZURE_MOCKS_DIR/dns_zone/success.json" + export MODULES_TO_USE="existing/module" + + run_azure_dns_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/network/azure_dns/modules" +} diff --git a/frontend/deployment/tests/provider/azure/setup_test.bats b/frontend/deployment/tests/provider/azure/setup_test.bats new file mode 100644 index 00000000..a6f0b39c --- /dev/null +++ b/frontend/deployment/tests/provider/azure/setup_test.bats @@ -0,0 +1,203 @@ +#!/usr/bin/env bats +# ============================================================================= +# Unit tests for provider/azure/setup script +# +# Requirements: +# - bats-core: brew install bats-core +# - jq: brew install jq +# +# Run tests: +# bats tests/provider/azure/setup_test.bats +# ============================================================================= + +setup() { + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)" + PROJECT_DIR="$(cd "$TEST_DIR/../../.." && pwd)" + PROJECT_ROOT="$(cd "$PROJECT_DIR/../.." && pwd)" + SCRIPT_PATH="$PROJECT_DIR/provider/azure/setup" + + source "$PROJECT_ROOT/testing/assertions.sh" + + export AZURE_SUBSCRIPTION_ID="00000000-0000-0000-0000-000000000000" + export AZURE_RESOURCE_GROUP="my-resource-group" + export TOFU_PROVIDER_STORAGE_ACCOUNT="mytfstatestorage" + export TOFU_PROVIDER_CONTAINER="tfstate" + + # Base tofu variables + export TOFU_VARIABLES='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7" + }' + + export TOFU_INIT_VARIABLES="" + export MODULES_TO_USE="" +} + +# ============================================================================= +# Helper functions +# ============================================================================= +run_azure_setup() { + source "$SCRIPT_PATH" +} + +# ============================================================================= +# Test: Required environment variables +# ============================================================================= +@test "Should fail when AZURE_SUBSCRIPTION_ID is not set" { + unset AZURE_SUBSCRIPTION_ID + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ AZURE_SUBSCRIPTION_ID is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" + assert_contains "$output" " • AZURE_SUBSCRIPTION_ID" +} + +@test "Should fail when AZURE_RESOURCE_GROUP is not set" { + unset AZURE_RESOURCE_GROUP + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ AZURE_RESOURCE_GROUP is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" + assert_contains "$output" " • AZURE_RESOURCE_GROUP" +} + +@test "Should fail when TOFU_PROVIDER_STORAGE_ACCOUNT is not set" { + unset TOFU_PROVIDER_STORAGE_ACCOUNT + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ TOFU_PROVIDER_STORAGE_ACCOUNT is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" + assert_contains "$output" " • TOFU_PROVIDER_STORAGE_ACCOUNT" +} + +@test "Should fail when TOFU_PROVIDER_CONTAINER is not set" { + unset TOFU_PROVIDER_CONTAINER + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ TOFU_PROVIDER_CONTAINER is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" + assert_contains "$output" " • TOFU_PROVIDER_CONTAINER" +} + +@test "Should report all the variables that are not set" { + unset AZURE_SUBSCRIPTION_ID + unset AZURE_RESOURCE_GROUP + unset TOFU_PROVIDER_STORAGE_ACCOUNT + unset TOFU_PROVIDER_CONTAINER + + run source "$SCRIPT_PATH" + + assert_equal "$status" "1" + assert_contains "$output" " ❌ AZURE_SUBSCRIPTION_ID is missing" + assert_contains "$output" " ❌ AZURE_RESOURCE_GROUP is missing" + assert_contains "$output" " ❌ TOFU_PROVIDER_STORAGE_ACCOUNT is missing" + assert_contains "$output" " ❌ TOFU_PROVIDER_CONTAINER is missing" + assert_contains "$output" " 🔧 How to fix:" + assert_contains "$output" " Set the missing variable(s) in the nullplatform agent Helm installation:" + assert_contains "$output" " • AZURE_SUBSCRIPTION_ID" + assert_contains "$output" " • AZURE_RESOURCE_GROUP" + assert_contains "$output" " • TOFU_PROVIDER_STORAGE_ACCOUNT" + assert_contains "$output" " • TOFU_PROVIDER_CONTAINER" +} + +# ============================================================================= +# Test: TOFU_VARIABLES - verifies the entire JSON structure +# ============================================================================= +@test "Should add azure_provider field to TOFU_VARIABLES" { + run_azure_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "azure_provider": { + "subscription_id": "00000000-0000-0000-0000-000000000000", + "resource_group": "my-resource-group", + "storage_account": "mytfstatestorage", + "container": "tfstate" + }, + "provider_resource_tags_json": {} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +@test "Should add provider_resource_tags_json to TOFU_VARIABLES" { + export RESOURCE_TAGS_JSON='{"Environment": "production", "Team": "platform"}' + + run_azure_setup + + local expected='{ + "application_slug": "automation", + "scope_slug": "development-tools", + "scope_id": "7", + "azure_provider": { + "subscription_id": "00000000-0000-0000-0000-000000000000", + "resource_group": "my-resource-group", + "storage_account": "mytfstatestorage", + "container": "tfstate" + }, + "provider_resource_tags_json": {"Environment": "production", "Team": "platform"} +}' + + assert_json_equal "$TOFU_VARIABLES" "$expected" "TOFU_VARIABLES" +} + +# ============================================================================= +# Test: TOFU_INIT_VARIABLES - backend configuration +# ============================================================================= +@test "Should add storage_account_name configuration to TOFU_INIT_VARIABLES" { + run_azure_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=storage_account_name=mytfstatestorage" +} + +@test "Should add container_name configuration to TOFU_INIT_VARIABLES" { + run_azure_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=container_name=tfstate" +} + +@test "Should add resource_group_name configuration to TOFU_INIT_VARIABLES" { + run_azure_setup + + assert_contains "$TOFU_INIT_VARIABLES" "-backend-config=resource_group_name=my-resource-group" +} + +@test "Should append to TOFU_INIT_VARIABLES when it previous settings are present" { + export TOFU_INIT_VARIABLES="-var=existing=value" + + run_azure_setup + + assert_equal "$TOFU_INIT_VARIABLES" "-var=existing=value -backend-config=storage_account_name=mytfstatestorage -backend-config=container_name=tfstate -backend-config=resource_group_name=my-resource-group" +} + +# ============================================================================= +# Test: MODULES_TO_USE +# ============================================================================= +@test "Should register the provider in the MODULES_TO_USE variable when it's empty" { + run_azure_setup + + assert_equal "$MODULES_TO_USE" "$PROJECT_DIR/provider/azure/modules" +} + +@test "Should append the provider in the MODULES_TO_USE variable when it's not empty" { + export MODULES_TO_USE="existing/module" + + run_azure_setup + + assert_equal "$MODULES_TO_USE" "existing/module,$PROJECT_DIR/provider/azure/modules" +} diff --git a/frontend/deployment/tests/resources/azure_mocks/az b/frontend/deployment/tests/resources/azure_mocks/az new file mode 100755 index 00000000..69109597 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/az @@ -0,0 +1,18 @@ +#!/bin/bash +# Mock az CLI for testing +# Set AZ_MOCK_RESPONSE to the path of the mock file to return +# Set AZ_MOCK_EXIT_CODE to the exit code (default: 0) + +if [ -z "$AZ_MOCK_RESPONSE" ]; then + echo "AZ_MOCK_RESPONSE not set" >&2 + exit 1 +fi + +if [ -f "$AZ_MOCK_RESPONSE" ]; then + cat "$AZ_MOCK_RESPONSE" +else + echo "Mock file not found: $AZ_MOCK_RESPONSE" >&2 + exit 1 +fi + +exit "${AZ_MOCK_EXIT_CODE:-0}" diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/access_denied.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/access_denied.json new file mode 100644 index 00000000..69673831 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/access_denied.json @@ -0,0 +1 @@ +AuthorizationFailed: The client does not have authorization to perform action 'Microsoft.Network/dnszones/read' over scope '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Network/dnszones/example.com' diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/credentials_error.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/credentials_error.json new file mode 100644 index 00000000..e866bc96 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/credentials_error.json @@ -0,0 +1 @@ +AADSTS700016: Application with identifier 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' was not found in the directory. InvalidAuthenticationToken. diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/empty_name.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/empty_name.json new file mode 100644 index 00000000..1af2865d --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/empty_name.json @@ -0,0 +1,6 @@ +{ + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Network/dnszones/example.com", + "name": null, + "type": "Microsoft.Network/dnszones", + "location": "global" +} diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/invalid_subscription.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/invalid_subscription.json new file mode 100644 index 00000000..6fba5221 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/invalid_subscription.json @@ -0,0 +1 @@ +InvalidSubscriptionId: The provided subscription identifier 'invalid' is malformed or invalid. diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/not_found.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/not_found.json new file mode 100644 index 00000000..fdd91ad7 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/not_found.json @@ -0,0 +1 @@ +ResourceNotFound: The resource 'example.com' was not found in resource group 'my-resource-group' diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/success.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/success.json new file mode 100644 index 00000000..37f9262c --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/success.json @@ -0,0 +1,17 @@ +{ + "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Network/dnszones/example.com", + "name": "example.com", + "type": "Microsoft.Network/dnszones", + "location": "global", + "tags": {}, + "properties": { + "maxNumberOfRecordSets": 10000, + "numberOfRecordSets": 5, + "nameServers": [ + "ns1-01.azure-dns.com.", + "ns2-01.azure-dns.net.", + "ns3-01.azure-dns.org.", + "ns4-01.azure-dns.info." + ] + } +} diff --git a/frontend/deployment/tests/resources/azure_mocks/dns_zone/unknown_error.json b/frontend/deployment/tests/resources/azure_mocks/dns_zone/unknown_error.json new file mode 100644 index 00000000..f115bae1 --- /dev/null +++ b/frontend/deployment/tests/resources/azure_mocks/dns_zone/unknown_error.json @@ -0,0 +1 @@ +Unknown error fetching Azure DNS zone. diff --git a/frontend/deployment/tests/resources/context_azure.json b/frontend/deployment/tests/resources/context_azure.json new file mode 100644 index 00000000..56f90dd3 --- /dev/null +++ b/frontend/deployment/tests/resources/context_azure.json @@ -0,0 +1,170 @@ +{ + "account": { + "created_at": "2023-01-31T21:53:32.597Z", + "id": 2, + "metadata": {}, + "name": "Playground", + "nrn": "organization=1:account=2", + "organization_id": 1, + "repository_prefix": "playground-repos", + "repository_provider": "github", + "settings": {}, + "slug": "playground", + "status": "active", + "updated_at": "2023-01-31T21:53:32.597Z" + }, + "application": { + "auto_deploy_on_creation": false, + "created_at": "2025-10-07T03:22:21.385Z", + "id": 4, + "is_mono_repo": false, + "messages": [], + "metadata": {}, + "name": "Automation", + "namespace_id": 3, + "nrn": "organization=1:account=2:namespace=3:application=4", + "repository_app_path": null, + "repository_url": "https://github.com/playground-repos/tools-automation", + "settings": {}, + "slug": "automation", + "status": "active", + "tags": {}, + "template_id": 1037172878, + "updated_at": "2025-10-07T03:22:30.695Z" + }, + "asset": { + "id": 6, + "build_id": 612605537, + "name": "main", + "type": "bundle", + "url": "https://assetsaccount.blob.core.windows.net/assets/tools/automation/v1.0.0", + "platform": "x86_64", + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:build=5:asset=6" + }, + "deployment": { + "created_at": "2025-12-22T18:27:54.701Z", + "created_by": 123456789, + "deployment_group_id": null, + "deployment_token": "dep-token", + "expires_at": null, + "external_strategy_id": 10, + "id": 8, + "messages": [], + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:scope=7:deployment=8", + "parameters": [], + "release_id": 9, + "scope_id": 7, + "status": "creating", + "status_in_scope": "inactive", + "status_started_at": { + "creating": "2025-12-22T18:27:54.629Z" + }, + "strategy": "initial", + "strategy_data": { + "parameters": { + "metrics": { + "enabled": false, + "rules": [] + }, + "traffic": { + "enable_auto_switch": false, + "interval": 10, + "step": "0.1" + } + } + }, + "updated_at": "2025-12-23T13:22:06.345Z", + "updated_by": null + }, + "namespace": { + "account_id": 2, + "created_at": "2025-05-15T21:34:40.725Z", + "id": 3, + "metadata": {}, + "name": "Tools", + "nrn": "organization=1:account=2:namespace=3", + "slug": "tools", + "status": "active", + "updated_at": "2025-05-15T21:34:40.725Z" + }, + "parameters": { + "results": [ + { + "destination_path": null, + "id": 10, + "name": "TEST", + "type": "environment", + "values": [ + { + "id": "11", + "value": "testing-tools" + } + ], + "variable": "TEST", + "version_id": 12 + } + ] + }, + "providers": { + "cloud-providers": { + "authentication": { + "client_id": "test-client", + "subscription_id": "test-subscription", + "tenant_id": "test-tenant" + }, + "networking": { + "application_domain": false, + "domain_name": "publicdomain.com", + "private_dns_zone_name": "frontend.privatedomain.com", + "private_dns_zone_resource_group_name": "private-dns-resource-grou", + "public_dns_zone_name": "frontend.publicdomain.com", + "public_dns_zone_resource_group_name": "dns-resource-group" + } + } + }, + "release": { + "application_id": 4, + "build_id": 5, + "created_at": "2025-12-12T13:07:27.435Z", + "id": 9, + "metadata": {}, + "nrn": "organization=1:account=2:namespace=3:application=4:release=9", + "semver": "v1.0.0", + "status": "active", + "updated_at": "2025-12-12T13:07:27.702Z" + }, + "scope": { + "application_id": 4, + "asset_name": "main", + "capabilities": {}, + "created_at": "2025-12-22T18:27:04.949Z", + "dimensions": { + "country": "argentina", + "environment": "development" + }, + "domain": "", + "domains": [], + "external_created": false, + "id": 7, + "instance_id": "some-instance-id", + "messages": [], + "metadata": {}, + "name": "Development tools", + "nrn": "organization=1:account=2:namespace=3:application=4:scope=7", + "profiles": [ + "environment_development", + "environment_development_country_argentina" + ], + "provider": "scope-type-id", + "requested_spec": {}, + "runtime_configurations": [], + "slug": "development-tools", + "status": "active", + "tags": [], + "tier": "important", + "type": "custom", + "updated_at": "2025-12-29T18:25:55.908Z" + } +} diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/no_storage_account_data.json b/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/no_storage_account_data.json new file mode 100644 index 00000000..7c32671f --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/no_storage_account_data.json @@ -0,0 +1,12 @@ +{ + "results": [ + { + "id": "d397e46b-89b8-419d-ac14-2b483ace511c", + "name": "Other Storage Provider", + "type": "other", + "attributes": { + "endpoint": "https://example.com" + } + } + ] +} diff --git a/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/success.json b/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/success.json new file mode 100644 index 00000000..21d55159 --- /dev/null +++ b/frontend/deployment/tests/resources/np_mocks/asset_repository_azure/success.json @@ -0,0 +1,17 @@ +{ + "results": [ + { + "id": "d397e46b-89b8-419d-ac14-2b483ace511c", + "name": "Azure Blob Storage", + "type": "azure-blob-storage", + "attributes": { + "storage_account": { + "name": "mystaticstorage" + }, + "container": { + "name": "$web" + } + } + } + ] +} diff --git a/integration_run.txt b/integration_run.txt deleted file mode 100644 index 25d769e7..00000000 --- a/integration_run.txt +++ /dev/null @@ -1,578 +0,0 @@ - -======================================== - Integration Tests (Containerized) -======================================== - -================================================================================ - Integration Test Helpers Reference -================================================================================ - -SETUP & TEARDOWN ----------------- - integration_setup --cloud-provider - Initialize integration test environment for the specified cloud provider. - Call this in setup_file(). - - integration_teardown - Clean up integration test environment. - Call this in teardown_file(). - -AWS LOCAL COMMANDS ------------------- - aws_local - Execute AWS CLI against LocalStack (S3, Route53, DynamoDB, etc.) - Example: aws_local s3 ls - - aws_moto - Execute AWS CLI against Moto (CloudFront) - Example: aws_moto cloudfront list-distributions - -WORKFLOW EXECUTION ------------------- - run_workflow "" - Run a nullplatform workflow file. - Path is relative to module root. - Example: run_workflow "frontend/deployment/workflows/initial.yaml" - -CONTEXT HELPERS ---------------- - load_context "" - Load a context JSON file into the CONTEXT environment variable. - Example: load_context "tests/resources/context.json" - - override_context "" "" - Override a value in the current CONTEXT. - Example: override_context "providers.networking.zone_id" "Z1234567890" - -API MOCKING (Smocker) ---------------------- - clear_mocks - Clear all mocks and set up default mocks (token endpoint). - Call this at the start of each test. - - mock_request "" "" "" - Mock an API request using a response file. - File format: { "status": 200, "body": {...} } - Example: mock_request "GET" "/provider/123" "mocks/provider.json" - - mock_request "" "" '' - Mock an API request with inline response. - Example: mock_request "POST" "/deployments" 201 '{"id": "new"}' - - mock_request_with_query "" "" "" '' - Mock a request with query parameters. - Example: mock_request_with_query "GET" "/items" "type=foo" 200 '[...]' - - assert_mock_called "" "" - Assert that a mock endpoint was called. - Example: assert_mock_called "GET" "/provider/123" - - mock_call_count "" "" - Get the number of times a mock was called. - Example: count=$(mock_call_count "GET" "/provider/123") - -AWS ASSERTIONS --------------- - assert_s3_bucket_exists "" - Assert an S3 bucket exists in LocalStack. - - assert_s3_bucket_not_exists "" - Assert an S3 bucket does not exist. - - assert_cloudfront_exists "" - Assert a CloudFront distribution exists (matched by comment). - - assert_cloudfront_not_exists "" - Assert a CloudFront distribution does not exist. - - assert_route53_record_exists "" "" - Assert a Route53 record exists. - Example: assert_route53_record_exists "app.example.com" "A" - - assert_route53_record_not_exists "" "" - Assert a Route53 record does not exist. - - assert_dynamodb_table_exists "" - Assert a DynamoDB table exists. - - assert_dynamodb_table_not_exists "" - Assert a DynamoDB table does not exist. - -GENERIC ASSERTIONS ------------------- - assert_success "" [""] - Assert a command succeeds (exit code 0). - - assert_failure "" [""] - Assert a command fails (non-zero exit code). - - assert_contains "" "" [""] - Assert a string contains a substring. - - assert_equals "" "" [""] - Assert two values are equal. - -ENVIRONMENT VARIABLES ---------------------- - LOCALSTACK_ENDPOINT LocalStack URL (default: http://localhost:4566) - MOTO_ENDPOINT Moto URL (default: http://localhost:5555) - SMOCKER_HOST Smocker admin URL (default: http://localhost:8081) - AWS_ENDPOINT_URL AWS endpoint for CLI (default: $LOCALSTACK_ENDPOINT) - INTEGRATION_MODULE_ROOT Root directory of the module being tested - -================================================================================ - -Building test runner container... -#1 [internal] load local bake definitions -#1 reading from stdin 605B done -#1 DONE 0.0s -#2 [internal] load build definition from Dockerfile.test-runner -#2 transferring dockerfile: 1.11kB done -#2 DONE 0.0s -#3 [internal] load metadata for docker.io/library/alpine:3.19 -#3 ... -#4 [auth] library/alpine:pull token for registry-1.docker.io -#4 DONE 0.0s -#3 [internal] load metadata for docker.io/library/alpine:3.19 -#3 DONE 1.4s -#5 [internal] load .dockerignore -#5 transferring context: 2B done -#5 DONE 0.0s -#6 [1/6] FROM docker.io/library/alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1 -#6 resolve docker.io/library/alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1 0.0s done -#6 DONE 0.0s -#7 [2/6] RUN apk add --no-cache bash curl jq git openssh docker-cli aws-cli ca-certificates ncurses -#7 CACHED -#8 [4/6] RUN apk add --no-cache --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community opentofu -#8 CACHED -#9 [5/6] RUN curl -fsSL https://cli.nullplatform.com/install.sh | sh -#9 CACHED -#10 [3/6] RUN apk add --no-cache bats -#10 CACHED -#11 [6/6] WORKDIR /workspace -#11 CACHED -#12 exporting to image -#12 exporting layers done -#12 exporting manifest sha256:31a3d8117773d7f0b2472bd936b8e33919a258f6e3e606e9c5da28d6a219485c done -#12 exporting config sha256:d37ea551f1fe3572130afbca0214c239ec4102c522fda4cbdf10d050d8eb06ca done -#12 exporting attestation manifest sha256:2bfbcda8b02e193750ddafe6b11eb153df63367bb8201b408f230892420aa0f1 done -#12 exporting manifest list sha256:708a03ba25f4fb1db02d9c3d92afef01583c7ce8c16a3ede282a623956c325ab done -#12 naming to docker.io/library/docker-test-runner:latest done -#12 unpacking to docker.io/library/docker-test-runner:latest done -#12 DONE 0.0s -#13 resolving provenance for metadata file -#13 DONE 0.0s - docker-test-runner Built - -Starting infrastructure services... - Network docker_integration-network Creating - Network docker_integration-network Created - Volume docker_localstack-data Creating - Volume docker_localstack-data Created - Container integration-localstack Creating - Container integration-moto Creating - Container integration-smocker Creating - Container integration-smocker Created - Container integration-nginx Creating - Container integration-moto Created - Container integration-localstack Created - Container integration-nginx Created - Container integration-smocker Starting - Container integration-moto Starting - Container integration-localstack Starting - Container integration-smocker Started - Container integration-nginx Starting - Container integration-localstack Started - Container integration-moto Started - Container integration-nginx Started -Waiting for services to be ready.. -All services ready - -[frontend] Running integration tests in ./frontend/deployment/tests/integration - -lifecycle_test.bats - create infrastructure deploys CloudFront and Route53 resources1/1 ✓ create infrastructure deploys CloudFront and Route53 resources - Loaded context from: frontend/deployment/tests/resources/context.json - Mock: GET /category -> 200 - Mock: GET /provider_specification -> 200 - Mock: GET /provider -> 200 - Mock: GET /provider/s3-asset-repository-id -> 200 - Mock: PATCH /scope/7 -> 200 - Running workflow: frontend/deployment/workflows/initial.yaml - 🚀Executing step: build_context - 🚀Executing step: setup_provider_layer - 🔍 Validating AWS provider configuration... - ✅ AWS_REGION=us-east-1 - ✅ TOFU_PROVIDER_BUCKET=tofu-state-bucket - ✅ TOFU_LOCK_TABLE=tofu-locks - ✨ AWS provider configured successfully - - 🚀Executing step: setup_network_layer - 🔍 Validating Route53 network configuration... - ✅ hosted_zone_id=GDYKKDE6GH3RGMW5WN3FDP - - 📡 Fetching domain from Route 53 hosted zone... - ✅ domain=frontend.publicdomain.com - ✅ subdomain=automation-development-tools - - 📝 Setting scope domain to 'automation-development-tools.frontend.publicdomain.com'... - ✅ Scope domain set successfully - - ✨ Route53 network configured successfully - - 🚀Executing step: setup_distribution_layer - 🔍 Validating CloudFront distribution configuration... - ✅ app_name=automation-development-tools-7 - - 📡 Fetching assets-repository provider... - ✅ bucket_name=assets-bucket - ✅ s3_prefix=/tools/automation/v1.0.0 - - ✨ CloudFront distribution configured successfully - - 🚀Executing step: build_modules - Composing modules: /workspace/frontend/deployment/tests/integration/localstack,/workspace/frontend/deployment/provider/aws/modules,/workspace/frontend/deployment/network/route53/modules,/workspace/frontend/deployment/distribution/cloudfront/modules - Target directory: /tmp/temp-np-output-60069130/output/7 - - /workspace/frontend/deployment/tests/integration/localstack - provider_override.tf - ✓ Copied modules from: /workspace/frontend/deployment/tests/integration/localstack (prefix: integration_localstack_) - - /workspace/frontend/deployment/provider/aws/modules - provider.tf - provider.tftest.hcl - variables.tf - ✓ Copied modules from: /workspace/frontend/deployment/provider/aws/modules (prefix: aws_modules_) - - /workspace/frontend/deployment/network/route53/modules - locals.tf - main.tf - outputs.tf - route53.tftest.hcl - test_locals.tf - variables.tf - ✓ Copied modules from: /workspace/frontend/deployment/network/route53/modules (prefix: route53_modules_) - - /workspace/frontend/deployment/distribution/cloudfront/modules - cloudfront.tftest.hcl - data.tf - locals.tf - main.tf - outputs.tf - test_locals.tf - variables.tf - ✓ Copied modules from: /workspace/frontend/deployment/distribution/cloudfront/modules (prefix: cloudfront_modules_) - - ✓ All modules composed successfully - 🚀Executing step: tofu - - Initializing the backend... -  - Successfully configured the backend "s3"! OpenTofu will automatically - use this backend unless the backend configuration changes. - - Initializing provider plugins... - - terraform.io/builtin/terraform is built in to OpenTofu - - Finding hashicorp/aws versions matching "~> 5.0"... - - Installing hashicorp/aws v5.100.0... - - Installed hashicorp/aws v5.100.0 (signed, key ID 0C0AF313E5FD9F80) - - Providers are signed by their developers. - If you'd like to know more about provider signing, you can read about it here: - https://opentofu.org/docs/cli/plugins/signing/ - - OpenTofu has created a lock file .terraform.lock.hcl to record the provider - selections it made above. Include this file in your version control repository - so that OpenTofu can guarantee to make the same selections by default when - you run "tofu init" in the future. - - OpenTofu has been successfully initialized! -  - You may now begin working with OpenTofu. Try running "tofu plan" to see - any changes that are required for your infrastructure. All OpenTofu commands - should now work. - - If you ever set or change modules or backend configuration for OpenTofu, - rerun this command to reinitialize your working directory. If you forget, other - commands will detect it and remind you to do so if necessary. - data.aws_s3_bucket.static: Reading... - data.aws_caller_identity.current: Reading... - data.aws_acm_certificate.custom_domain[0]: Reading... - data.aws_acm_certificate.custom_domain[0]: Read complete after 0s [id=arn:aws:acm:us-east-1:000000000000:certificate/020dc254-9668-43fc-9d52-da98ca4c7c53] - data.aws_s3_bucket.static: Read complete after 0s [id=assets-bucket] - data.aws_caller_identity.current: Read complete after 0s [id=000000000000] - - OpenTofu used the selected providers to generate the following execution - plan. Resource actions are indicated with the following symbols: - + create - - OpenTofu will perform the following actions: - -  # aws_cloudfront_distribution.static will be created -  + resource "aws_cloudfront_distribution" "static" { - + aliases = [ - + "automation-development-tools.frontend.publicdomain.com", - ] - + arn = (known after apply) - + caller_reference = (known after apply) - + comment = "Distribution for automation-development-tools-7" - + continuous_deployment_policy_id = (known after apply) - + default_root_object = "index.html" - + domain_name = (known after apply) - + enabled = true - + etag = (known after apply) - + hosted_zone_id = (known after apply) - + http_version = "http2" - + id = (known after apply) - + in_progress_validation_batches = (known after apply) - + is_ipv6_enabled = true - + last_modified_time = (known after apply) - + price_class = "PriceClass_100" - + retain_on_delete = false - + staging = false - + status = (known after apply) - + tags = { - + "ManagedBy" = "terraform" - + "Module" = "distribution/cloudfront" - + "account" = "playground" - + "account_id" = "2" - + "application" = "automation" - + "application_id" = "4" - + "deployment_id" = "8" - + "namespace" = "tools" - + "namespace_id" = "3" - + "nullplatform" = "true" - + "scope" = "development-tools" - + "scope_id" = "7" - } - + tags_all = { - + "ManagedBy" = "terraform" - + "Module" = "distribution/cloudfront" - + "account" = "playground" - + "account_id" = "2" - + "application" = "automation" - + "application_id" = "4" - + "deployment_id" = "8" - + "namespace" = "tools" - + "namespace_id" = "3" - + "nullplatform" = "true" - + "scope" = "development-tools" - + "scope_id" = "7" - } - + trusted_key_groups = (known after apply) - + trusted_signers = (known after apply) - + wait_for_deployment = true - - + custom_error_response { - + error_code = 403 - + response_code = 200 - + response_page_path = "/index.html" - } - + custom_error_response { - + error_code = 404 - + response_code = 200 - + response_page_path = "/index.html" - } - - + default_cache_behavior { - + allowed_methods = [ - + "GET", - + "HEAD", - + "OPTIONS", - ] - + cached_methods = [ - + "GET", - + "HEAD", - ] - + compress = true - + default_ttl = 3600 - + max_ttl = 86400 - + min_ttl = 0 - + target_origin_id = "S3-assets-bucket" - + trusted_key_groups = (known after apply) - + trusted_signers = (known after apply) - + viewer_protocol_policy = "redirect-to-https" - - + forwarded_values { - + headers = (known after apply) - + query_string = false - + query_string_cache_keys = (known after apply) - - + cookies { - + forward = "none" - + whitelisted_names = (known after apply) - } - } - - + grpc_config (known after apply) - } - - + ordered_cache_behavior { - + allowed_methods = [ - + "GET", - + "HEAD", - ] - + cached_methods = [ - + "GET", - + "HEAD", - ] - + compress = true - + default_ttl = 604800 - + max_ttl = 31536000 - + min_ttl = 86400 - + path_pattern = "/static/*" - + target_origin_id = "S3-assets-bucket" - + viewer_protocol_policy = "redirect-to-https" - - + forwarded_values { - + headers = (known after apply) - + query_string = false - + query_string_cache_keys = (known after apply) - - + cookies { - + forward = "none" - } - } - - + grpc_config (known after apply) - } - - + origin { - + connection_attempts = 3 - + connection_timeout = 10 - + domain_name = "assets-bucket.s3.us-east-1.amazonaws.com" - + origin_access_control_id = (known after apply) - + origin_id = "S3-assets-bucket" - + origin_path = "/tools/automation/v1.0.0" - } - - + restrictions { - + geo_restriction { - + locations = (known after apply) - + restriction_type = "none" - } - } - - + viewer_certificate { - + acm_certificate_arn = "arn:aws:acm:us-east-1:000000000000:certificate/020dc254-9668-43fc-9d52-da98ca4c7c53" - + minimum_protocol_version = "TLSv1.2_2021" - + ssl_support_method = "sni-only" - } - } - -  # aws_cloudfront_origin_access_control.static will be created -  + resource "aws_cloudfront_origin_access_control" "static" { - + arn = (known after apply) - + description = "OAC for automation-development-tools-7" - + etag = (known after apply) - + id = (known after apply) - + name = "automation-development-tools-7-oac" - + origin_access_control_origin_type = "s3" - + signing_behavior = "always" - + signing_protocol = "sigv4" - } - -  # aws_route53_record.main_alias[0] will be created -  + resource "aws_route53_record" "main_alias" { - + allow_overwrite = (known after apply) - + fqdn = (known after apply) - + id = (known after apply) - + name = "automation-development-tools.frontend.publicdomain.com" - + type = "A" - + zone_id = "GDYKKDE6GH3RGMW5WN3FDP" - - + alias { - + evaluate_target_health = false - + name = (known after apply) - + zone_id = (known after apply) - } - } - -  # aws_s3_bucket_policy.static will be created -  + resource "aws_s3_bucket_policy" "static" { - + bucket = "assets-bucket" - + id = (known after apply) - + policy = (known after apply) - } - -  # terraform_data.cloudfront_invalidation will be created -  + resource "terraform_data" "cloudfront_invalidation" { - + id = (known after apply) - + triggers_replace = [ - + "/tools/automation/v1.0.0", - ] - } - - Plan: 5 to add, 0 to change, 0 to destroy. -  - Changes to Outputs: - + distribution_bucket_arn = "arn:aws:s3:::assets-bucket" - + distribution_bucket_name = "assets-bucket" - + distribution_cloudfront_distribution_id = (known after apply) - + distribution_cloudfront_domain_name = (known after apply) - + distribution_record_type = "A" - + distribution_s3_prefix = "/tools/automation/v1.0.0" - + distribution_target_domain = (known after apply) - + distribution_target_zone_id = (known after apply) - + distribution_website_url = "https://automation-development-tools.frontend.publicdomain.com" - + network_fqdn = (known after apply) - + network_full_domain = "automation-development-tools.frontend.publicdomain.com" - + network_website_url = "https://automation-development-tools.frontend.publicdomain.com" - aws_cloudfront_origin_access_control.static: Creating... - aws_cloudfront_origin_access_control.static: Creation complete after 1s [id=S90W3RNS5SFCF] - aws_cloudfront_distribution.static: Creating... - aws_cloudfront_distribution.static: Still creating... [10s elapsed] - aws_cloudfront_distribution.static: Still creating... [20s elapsed] - aws_cloudfront_distribution.static: Still creating... [30s elapsed] - aws_cloudfront_distribution.static: Creation complete after 30s [id=Q2P6CSCO8XDGC] - terraform_data.cloudfront_invalidation: Creating... - terraform_data.cloudfront_invalidation: Provisioning with 'local-exec'... - terraform_data.cloudfront_invalidation (local-exec): Executing: ["/bin/sh" "-c" "aws cloudfront create-invalidation --endpoint-url http://moto:5000 --distribution-id Q2P6CSCO8XDGC --paths '/*'"] - aws_s3_bucket_policy.static: Creating... - aws_route53_record.main_alias[0]: Creating... - aws_s3_bucket_policy.static: Creation complete after 0s [id=assets-bucket] - terraform_data.cloudfront_invalidation (local-exec): { - terraform_data.cloudfront_invalidation (local-exec):  "Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/Q2P6CSCO8XDGC/invalidation/IYKLBMLGIZ8SL", - terraform_data.cloudfront_invalidation (local-exec):  "Invalidation": { - terraform_data.cloudfront_invalidation (local-exec):  "Id": "IYKLBMLGIZ8SL", - terraform_data.cloudfront_invalidation (local-exec):  "Status": "COMPLETED", - terraform_data.cloudfront_invalidation (local-exec):  "CreateTime": "2026-01-16T14:18:38.015000+00:00", - terraform_data.cloudfront_invalidation (local-exec):  "InvalidationBatch": { - terraform_data.cloudfront_invalidation (local-exec):  "Paths": { - terraform_data.cloudfront_invalidation (local-exec):  "Quantity": 1, - terraform_data.cloudfront_invalidation (local-exec):  "Items": [ - terraform_data.cloudfront_invalidation (local-exec):  "/*" - terraform_data.cloudfront_invalidation (local-exec):  ] - terraform_data.cloudfront_invalidation (local-exec):  }, - terraform_data.cloudfront_invalidation (local-exec):  "CallerReference": "cli-1768573118-409470" - terraform_data.cloudfront_invalidation (local-exec):  } - terraform_data.cloudfront_invalidation (local-exec):  } - terraform_data.cloudfront_invalidation (local-exec): } - terraform_data.cloudfront_invalidation: Creation complete after 0s [id=133293b4-6bfa-46fc-a238-038f04efa04d] - aws_route53_record.main_alias[0]: Still creating... [10s elapsed] - aws_route53_record.main_alias[0]: Creation complete after 15s [id=GDYKKDE6GH3RGMW5WN3FDP_automation-development-tools.frontend.publicdomain.com_A] -  - Apply complete! Resources: 5 added, 0 changed, 0 destroyed. -  - Outputs: - - distribution_bucket_arn = "arn:aws:s3:::assets-bucket" - distribution_bucket_name = "assets-bucket" - distribution_cloudfront_distribution_id = "Q2P6CSCO8XDGC" - distribution_cloudfront_domain_name = "dvgg2m1fpk30s.cloudfront.net" - distribution_record_type = "A" - distribution_s3_prefix = "/tools/automation/v1.0.0" - distribution_target_domain = "dvgg2m1fpk30s.cloudfront.net" - distribution_target_zone_id = "Z2FDTNDATAQYW2" - distribution_website_url = "https://automation-development-tools.frontend.publicdomain.com" - network_fqdn = "automation-development-tools.frontend.publicdomain.com" - network_full_domain = "automation-development-tools.frontend.publicdomain.com" - network_website_url = "https://automation-development-tools.frontend.publicdomain.com" - {} - -1 test, 0 failures - - -All integration tests passed! - -Stopping containers... diff --git a/testing/azure-mock-provider/provider_override.tf b/testing/azure-mock-provider/provider_override.tf index 6b1a4406..822b17b2 100644 --- a/testing/azure-mock-provider/provider_override.tf +++ b/testing/azure-mock-provider/provider_override.tf @@ -29,4 +29,4 @@ provider "azurerm" { default_tags { tags = var.resource_tags } -} +} \ No newline at end of file diff --git a/testing/bin/az b/testing/bin/az new file mode 100755 index 00000000..67e18dcc --- /dev/null +++ b/testing/bin/az @@ -0,0 +1,265 @@ +#!/bin/bash +# ============================================================================= +# Mock Azure CLI for Integration Testing +# +# This script emulates the Azure CLI by querying the Azure Mock server. +# Only implements the commands needed for integration testing. +# +# Supported commands: +# az network dns zone show --name --resource-group +# az network dns record-set cname show --name --zone-name --resource-group +# az cdn profile show --name --resource-group +# az cdn endpoint show --name --profile-name --resource-group +# az storage account show --name --resource-group +# ============================================================================= + +AZURE_MOCK_ENDPOINT="${AZURE_MOCK_ENDPOINT:-http://localhost:8090}" +ARM_SUBSCRIPTION_ID="${ARM_SUBSCRIPTION_ID:-mock-subscription-id}" + +# Parse command structure +cmd="$1" +subcmd="$2" +action="$3" + +case "$cmd" in + network) + case "$subcmd" in + dns) + case "$action" in + zone) + shift 3 + subaction="$1" + case "$subaction" in + show) + shift + # Parse arguments + while [[ $# -gt 0 ]]; do + case $1 in + --name|-n) zone_name="$2"; shift 2 ;; + --resource-group|-g) resource_group="$2"; shift 2 ;; + *) shift ;; + esac + done + + if [[ -z "$zone_name" || -z "$resource_group" ]]; then + echo "ERROR: --name and --resource-group are required" >&2 + exit 1 + fi + + # Query Azure Mock + response=$(curl -s "${AZURE_MOCK_ENDPOINT}/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${resource_group}/providers/Microsoft.Network/dnszones/${zone_name}") + + # Check for error + error_code=$(echo "$response" | jq -r '.error.code // empty') + if [[ -n "$error_code" ]]; then + echo "ERROR: ResourceNotFound - The DNS zone '${zone_name}' was not found." >&2 + exit 1 + fi + + echo "$response" + ;; + *) + echo "ERROR: Unknown subaction: $subaction" >&2 + exit 1 + ;; + esac + ;; + record-set) + shift 3 + record_type="$1" + subaction="$2" + case "$record_type" in + cname) + case "$subaction" in + show) + shift 2 + # Parse arguments + while [[ $# -gt 0 ]]; do + case $1 in + --name|-n) record_name="$2"; shift 2 ;; + --zone-name|-z) zone_name="$2"; shift 2 ;; + --resource-group|-g) resource_group="$2"; shift 2 ;; + *) shift ;; + esac + done + + if [[ -z "$record_name" || -z "$zone_name" || -z "$resource_group" ]]; then + echo "ERROR: --name, --zone-name, and --resource-group are required" >&2 + exit 1 + fi + + # Query Azure Mock + response=$(curl -s "${AZURE_MOCK_ENDPOINT}/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${resource_group}/providers/Microsoft.Network/dnszones/${zone_name}/CNAME/${record_name}") + + # Check for error + error_code=$(echo "$response" | jq -r '.error.code // empty') + if [[ -n "$error_code" ]]; then + echo "ERROR: ResourceNotFound - The CNAME record '${record_name}' was not found." >&2 + exit 1 + fi + + echo "$response" + ;; + *) + echo "ERROR: Unknown subaction: $subaction" >&2 + exit 1 + ;; + esac + ;; + *) + echo "ERROR: Unknown record type: $record_type" >&2 + exit 1 + ;; + esac + ;; + *) + echo "ERROR: Unknown dns action: $action" >&2 + exit 1 + ;; + esac + ;; + *) + echo "ERROR: Unknown network subcommand: $subcmd" >&2 + exit 1 + ;; + esac + ;; + + cdn) + case "$subcmd" in + profile) + case "$action" in + show) + shift 3 + # Parse arguments + while [[ $# -gt 0 ]]; do + case $1 in + --name|-n) profile_name="$2"; shift 2 ;; + --resource-group|-g) resource_group="$2"; shift 2 ;; + *) shift ;; + esac + done + + if [[ -z "$profile_name" || -z "$resource_group" ]]; then + echo "ERROR: --name and --resource-group are required" >&2 + exit 1 + fi + + # Query Azure Mock + response=$(curl -s "${AZURE_MOCK_ENDPOINT}/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}") + + # Check for error + error_code=$(echo "$response" | jq -r '.error.code // empty') + if [[ -n "$error_code" ]]; then + echo "ERROR: ResourceNotFound - The CDN profile '${profile_name}' was not found." >&2 + exit 1 + fi + + echo "$response" + ;; + *) + echo "ERROR: Unknown cdn profile action: $action" >&2 + exit 1 + ;; + esac + ;; + endpoint) + case "$action" in + show) + shift 3 + # Parse arguments + while [[ $# -gt 0 ]]; do + case $1 in + --name|-n) endpoint_name="$2"; shift 2 ;; + --profile-name) profile_name="$2"; shift 2 ;; + --resource-group|-g) resource_group="$2"; shift 2 ;; + *) shift ;; + esac + done + + if [[ -z "$endpoint_name" || -z "$profile_name" || -z "$resource_group" ]]; then + echo "ERROR: --name, --profile-name, and --resource-group are required" >&2 + exit 1 + fi + + # Query Azure Mock + response=$(curl -s "${AZURE_MOCK_ENDPOINT}/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}/endpoints/${endpoint_name}") + + # Check for error + error_code=$(echo "$response" | jq -r '.error.code // empty') + if [[ -n "$error_code" ]]; then + echo "ERROR: ResourceNotFound - The CDN endpoint '${endpoint_name}' was not found." >&2 + exit 1 + fi + + echo "$response" + ;; + *) + echo "ERROR: Unknown cdn endpoint action: $action" >&2 + exit 1 + ;; + esac + ;; + *) + echo "ERROR: Unknown cdn subcommand: $subcmd" >&2 + exit 1 + ;; + esac + ;; + + storage) + case "$subcmd" in + account) + case "$action" in + show) + shift 3 + # Parse arguments + while [[ $# -gt 0 ]]; do + case $1 in + --name|-n) account_name="$2"; shift 2 ;; + --resource-group|-g) resource_group="$2"; shift 2 ;; + *) shift ;; + esac + done + + if [[ -z "$account_name" || -z "$resource_group" ]]; then + echo "ERROR: --name and --resource-group are required" >&2 + exit 1 + fi + + # Query Azure Mock + response=$(curl -s "${AZURE_MOCK_ENDPOINT}/subscriptions/${ARM_SUBSCRIPTION_ID}/resourceGroups/${resource_group}/providers/Microsoft.Storage/storageAccounts/${account_name}") + + # Check for error + error_code=$(echo "$response" | jq -r '.error.code // empty') + if [[ -n "$error_code" ]]; then + echo "ERROR: ResourceNotFound - The storage account '${account_name}' was not found." >&2 + exit 1 + fi + + echo "$response" + ;; + *) + echo "ERROR: Unknown storage account action: $action" >&2 + exit 1 + ;; + esac + ;; + *) + echo "ERROR: Unknown storage subcommand: $subcmd" >&2 + exit 1 + ;; + esac + ;; + + version) + echo '{"azure-cli": "2.99.0-mock", "azure-cli-core": "2.99.0-mock"}' + ;; + + *) + echo "ERROR: Unknown command: $cmd" >&2 + echo "This is a mock Azure CLI for integration testing." >&2 + echo "Supported commands: network dns zone, cdn profile/endpoint, storage account" >&2 + exit 1 + ;; +esac diff --git a/testing/docker/azure-mock/main.go b/testing/docker/azure-mock/main.go index 57c81baf..74439640 100644 --- a/testing/docker/azure-mock/main.go +++ b/testing/docker/azure-mock/main.go @@ -3666,4 +3666,4 @@ func main() { if err := http.ListenAndServe(":8080", server); err != nil { log.Fatalf("Server failed: %v", err) } -} +} \ No newline at end of file diff --git a/testing/docker/certs/cert.pem b/testing/docker/certs/cert.pem index 4fb8ae7a..62193133 100644 --- a/testing/docker/certs/cert.pem +++ b/testing/docker/certs/cert.pem @@ -1,18 +1,31 @@ -----BEGIN CERTIFICATE----- -MIIC7zCCAdegAwIBAgIJAOZGAGxa+MH3MA0GCSqGSIb3DQEBCwUAMB8xHTAbBgNV -BAMMFGFwaS5udWxscGxhdGZvcm0uY29tMB4XDTI2MDExMzE4MDUzNVoXDTI3MDEx -MzE4MDUzNVowHzEdMBsGA1UEAwwUYXBpLm51bGxwbGF0Zm9ybS5jb20wggEiMA0G -CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDFbtqvyx8oYOIUXIUIv5RzTPQXa4df -xVg1YNq1/hTv+dZaO/I5ufgJAtp9VC8kHppBV1yYUQ27c8aKVgTsl870RXjZW6Rq -nJUXnH5VGLRvnV0X8wjlBSFi3UZNM4AUB/BnILyt5XMWZaV0cOtujvbZ7Wqjt/As -Q8rDqYdeCZkIzA8SG2JiDBy1zModx/Fy2gKrC56JPO0+DnIz9NMHcefD/vPoFklf -JjuEw0z9B15Cy10tWEKQY8WKypdXIKProif9P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3lhclK0HzYfZHblw +J2h1JbnAvRstdbj6jXM/ndPujj8Mt+NSGWd2a9b1C4nwnZA6E7NkMwORXXXRxeRh +yf7c33W1W0HIKUA8p/PhXpYCEZy5tBX+wUcHPlKdECbs0skn1420wN8Oa7Tr6/hy +2AslWZfXZMEWDGbGlSt57qsppkdy3Xtt2KsSdbYgtLTcshfThF9KXVKXYHRf+dll +aaAj79fF9dMxDiMpWb84cTZWWQ== -----END CERTIFICATE----- diff --git a/testing/docker/certs/key.pem b/testing/docker/certs/key.pem index 4e8cdc37..592dd4f4 100644 --- a/testing/docker/certs/key.pem +++ b/testing/docker/certs/key.pem @@ -1,27 +1,52 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEAxW7ar8sfKGDiFFyFCL+Uc0z0F2uHX8VYNWDatf4U7/nWWjvy -Obn4CQLafVQvJB6aQVdcmFENu3PGilYE7JfO9EV42VukapyVF5x+VRi0b51dF/MI -5QUhYt1GTTOAFAfwZyC8reVzFmWldHDrbo722e1qo7fwLEPKw6mHXgmZCMwPEhti -YgwctczKHcfxctoCqwueiTztPg5yM/TTB3Hnw/7z6BZJXyY7hMNM/QdeQstdLVhC -kGPFisqXVyCj66In/Tzu1aEQjC9C6k2QwwZZnqbDaVUNpuz26Pl5zA/fQwun0ZIH -qDuNccxLrBUaVDjqENM0RFvLNX9LiiLVKAunywIDAQABAoIBAQCqUGm4UnoLZCBD -PljI5jOSj2TVuz3pwRzJ10Z0zr2TEqv15Vacs6+jXmHK0c9k0j1xdoJ7Jxi4hzRT -a8cNrhuqny+UGbko+vCmkKpukEQ/MLn5Cf+3SQi293lI6c9n+D1LSXNac+AnBwKH -A04lbpDGCEgA+ZDnLogA6ALNfWwVqCWoA6MXTPfhG7pAt20gz2hJdHBBiMDvI4fL -itSzuPxqNY/BT+/ReNj4rjzLSkICdxwvvxj812FTCjjF3wN/ixku64MtQL1NxL8R -aJJzz/h9wXpstUbNHidsIlAnSrV9mD3D+3DwjrV2cX54aCr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026 18:14:17 -0300 Subject: [PATCH 34/40] Add azure integration tests --- .../list_provider_spec.json | 14 ++++ .../cdn_assertions.bash | 11 +-- .../lifecycle_test.bats | 84 ++++++++++++------- 3 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider_spec.json diff --git a/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider_spec.json b/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider_spec.json new file mode 100644 index 00000000..910b1572 --- /dev/null +++ b/frontend/deployment/tests/integration/mocks/azure_asset_repository/list_provider_spec.json @@ -0,0 +1,14 @@ +{ + "status": 200, + "body": { + "results": [ + { + "id": "azure-asset-repository-spec-id", + "slug": "azure-assets", + "categories": [ + {"slug": "assets-repository"} + ] + } + ] + } +} diff --git a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash index 9d3383ad..e7149095 100644 --- a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash +++ b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/cdn_assertions.bash @@ -36,9 +36,10 @@ assert_azure_cdn_configured() { local resource_group="$4" # Derive CDN profile and endpoint names from app_name - # The terraform module uses: "${var.distribution_app_name}-cdn-profile" - local profile_name="${app_name}-cdn-profile" - local endpoint_name="${app_name}-cdn-endpoint" + # The terraform module uses: "${var.distribution_app_name}-cdn" for profile + # and "${var.distribution_app_name}" for endpoint + local profile_name="${app_name}-cdn" + local endpoint_name="${app_name}" # Get CDN Profile local profile_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}" @@ -97,8 +98,8 @@ assert_azure_cdn_not_configured() { local subscription_id="$2" local resource_group="$3" - local profile_name="${app_name}-cdn-profile" - local endpoint_name="${app_name}-cdn-endpoint" + local profile_name="${app_name}-cdn" + local endpoint_name="${app_name}" # Check CDN Profile is deleted local profile_path="/subscriptions/${subscription_id}/resourceGroups/${resource_group}/providers/Microsoft.Cdn/profiles/${profile_name}" diff --git a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats index 2f7cdb9b..78622454 100644 --- a/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats +++ b/frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats @@ -44,10 +44,15 @@ setup_file() { # Pre-create Azure DNS zone in the mock server (required for data source lookup) echo "Creating test prerequisites in Azure Mock..." - # Create DNS zone via REST API + # Create DNS zone in dns-resource-group (for validation step in setup_network_layer) azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_DNS_ZONE_RESOURCE_GROUP}/providers/Microsoft.Network/dnszones/${TEST_NETWORK_DOMAIN}" \ '{"location": "global", "tags": {}}' >/dev/null 2>&1 || true + # Also create DNS zone in test-resource-group (for Terraform data source lookup) + # The azure_dns module uses var.azure_provider.resource_group which is test-resource-group + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_RESOURCE_GROUP}/providers/Microsoft.Network/dnszones/${TEST_NETWORK_DOMAIN}" \ + '{"location": "global", "tags": {}}' >/dev/null 2>&1 || true + # Create Storage Account via REST API (for data source lookup) azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_RESOURCE_GROUP}/providers/Microsoft.Storage/storageAccounts/${TEST_DISTRIBUTION_STORAGE_ACCOUNT}" \ '{"location": "eastus", "kind": "StorageV2", "sku": {"name": "Standard_LRS", "tier": "Standard"}}' >/dev/null 2>&1 || true @@ -82,8 +87,8 @@ setup() { # Azure provider required environment variables export AZURE_SUBSCRIPTION_ID="$TEST_SUBSCRIPTION_ID" export AZURE_RESOURCE_GROUP="$TEST_RESOURCE_GROUP" - # Use Azurite's storage account for backend (like LocalStack for AWS) - export TOFU_PROVIDER_STORAGE_ACCOUNT="${AZURITE_ACCOUNT_NAME:-devstoreaccount1}" + # Use mock storage account for backend (handled by azure-mock) + export TOFU_PROVIDER_STORAGE_ACCOUNT="devstoreaccount1" export TOFU_PROVIDER_CONTAINER="tfstate" # Setup API mocks for np CLI calls @@ -93,43 +98,58 @@ setup() { mock_request "GET" "/provider" "$mocks_dir/azure_asset_repository/list_provider.json" mock_request "GET" "/provider/azure-blob-asset-repository-id" "$mocks_dir/azure_asset_repository/get_provider.json" mock_request "PATCH" "/scope/7" "$mocks_dir/scope/patch.json" + + # Ensure tfstate container exists in azure-mock for Terraform backend + curl -s -X PUT "${AZURE_MOCK_ENDPOINT}/tfstate?restype=container" \ + -H "Host: devstoreaccount1.blob.core.windows.net" \ + -H "x-ms-version: 2021-06-08" >/dev/null 2>&1 || true + + # Ensure DNS zone exists in azure-mock (for validation and Terraform data source) + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_DNS_ZONE_RESOURCE_GROUP}/providers/Microsoft.Network/dnszones/${TEST_NETWORK_DOMAIN}" \ + '{"location": "global", "tags": {}}' >/dev/null 2>&1 || true + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_RESOURCE_GROUP}/providers/Microsoft.Network/dnszones/${TEST_NETWORK_DOMAIN}" \ + '{"location": "global", "tags": {}}' >/dev/null 2>&1 || true + + # Ensure storage account exists in azure-mock (for Terraform data source) + azure_mock_put "/subscriptions/${TEST_SUBSCRIPTION_ID}/resourceGroups/${TEST_RESOURCE_GROUP}/providers/Microsoft.Storage/storageAccounts/${TEST_DISTRIBUTION_STORAGE_ACCOUNT}" \ + '{"location": "eastus", "kind": "StorageV2", "sku": {"name": "Standard_LRS", "tier": "Standard"}}' >/dev/null 2>&1 || true } # ============================================================================= # Test: Create Infrastructure # ============================================================================= -#@test "create infrastructure deploys Azure CDN and DNS resources" { -# run_workflow "frontend/deployment/workflows/initial.yaml" -# -# assert_azure_cdn_configured \ -# "$TEST_DISTRIBUTION_APP_NAME" \ -# "$TEST_DISTRIBUTION_STORAGE_ACCOUNT" \ -# "$TEST_SUBSCRIPTION_ID" \ -# "$TEST_RESOURCE_GROUP" -# -# assert_azure_dns_configured \ -# "$TEST_NETWORK_SUBDOMAIN" \ -# "$TEST_NETWORK_DOMAIN" \ -# "$TEST_SUBSCRIPTION_ID" \ -# "$TEST_DNS_ZONE_RESOURCE_GROUP" -#} +@test "create infrastructure deploys Azure CDN and DNS resources" { + run_workflow "frontend/deployment/workflows/initial.yaml" + + assert_azure_cdn_configured \ + "$TEST_DISTRIBUTION_APP_NAME" \ + "$TEST_DISTRIBUTION_STORAGE_ACCOUNT" \ + "$TEST_SUBSCRIPTION_ID" \ + "$TEST_RESOURCE_GROUP" + + assert_azure_dns_configured \ + "$TEST_NETWORK_SUBDOMAIN" \ + "$TEST_NETWORK_DOMAIN" \ + "$TEST_SUBSCRIPTION_ID" \ + "$TEST_RESOURCE_GROUP" +} # ============================================================================= # Test: Destroy Infrastructure # ============================================================================= -#@test "destroy infrastructure removes Azure CDN and DNS resources" { -# run_workflow "frontend/deployment/workflows/delete.yaml" -# -# assert_azure_cdn_not_configured \ -# "$TEST_DISTRIBUTION_APP_NAME" \ -# "$TEST_SUBSCRIPTION_ID" \ -# "$TEST_RESOURCE_GROUP" -# -# assert_azure_dns_not_configured \ -# "$TEST_NETWORK_SUBDOMAIN" \ -# "$TEST_NETWORK_DOMAIN" \ -# "$TEST_SUBSCRIPTION_ID" \ -# "$TEST_DNS_ZONE_RESOURCE_GROUP" -#} +@test "destroy infrastructure removes Azure CDN and DNS resources" { + run_workflow "frontend/deployment/workflows/delete.yaml" + + assert_azure_cdn_not_configured \ + "$TEST_DISTRIBUTION_APP_NAME" \ + "$TEST_SUBSCRIPTION_ID" \ + "$TEST_RESOURCE_GROUP" + + assert_azure_dns_not_configured \ + "$TEST_NETWORK_SUBDOMAIN" \ + "$TEST_NETWORK_DOMAIN" \ + "$TEST_SUBSCRIPTION_ID" \ + "$TEST_DNS_ZONE_RESOURCE_GROUP" +} From ad37629352a171418ba2a96cba74c08c95421013 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 20 Jan 2026 11:43:08 -0300 Subject: [PATCH 35/40] Move provider overrides to share folder --- .../localstack/provider_override.tf | 38 ------------------- .../lifecycle_test.bats | 2 +- 2 files changed, 1 insertion(+), 39 deletions(-) delete mode 100644 frontend/deployment/tests/integration/localstack/provider_override.tf diff --git a/frontend/deployment/tests/integration/localstack/provider_override.tf b/frontend/deployment/tests/integration/localstack/provider_override.tf deleted file mode 100644 index 587982c2..00000000 --- a/frontend/deployment/tests/integration/localstack/provider_override.tf +++ /dev/null @@ -1,38 +0,0 @@ -# Override file for LocalStack + Moto testing -# This file is copied into the module directory during integration tests -# to configure the AWS provider to use mock endpoints -# -# LocalStack (port 4566): S3, Route53, STS, IAM, DynamoDB, ACM -# Moto (port 5000): CloudFront - -# Set CloudFront endpoint for AWS CLI commands (used by cache invalidation) -variable "distribution_cloudfront_endpoint_url" { - default = "http://moto:5000" -} - -provider "aws" { - region = var.aws_provider.region - access_key = "test" - secret_key = "test" - skip_credentials_validation = true - skip_metadata_api_check = true - skip_requesting_account_id = true - - endpoints { - # LocalStack services (using Docker service name) - s3 = "http://localstack:4566" - route53 = "http://localstack:4566" - sts = "http://localstack:4566" - iam = "http://localstack:4566" - dynamodb = "http://localstack:4566" - acm = "http://localstack:4566" - # Moto services (CloudFront not in LocalStack free tier) - cloudfront = "http://moto:5000" - } - - default_tags { - tags = var.provider_resource_tags_json - } - - s3_use_path_style = true -} diff --git a/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats index 211b0b68..ce2f2ea6 100644 --- a/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats +++ b/frontend/deployment/tests/integration/test_cases/aws_cloudfront_route53/lifecycle_test.bats @@ -83,7 +83,7 @@ setup() { export TOFU_LOCK_TABLE="tofu-locks" export AWS_REGION="us-east-1" export SERVICE_PATH="$INTEGRATION_MODULE_ROOT/frontend" - export CUSTOM_TOFU_MODULES="$INTEGRATION_MODULE_ROOT/frontend/deployment/tests/integration/localstack" + export CUSTOM_TOFU_MODULES="$INTEGRATION_MODULE_ROOT/testing/localstack-provider" # Setup API mocks for np CLI calls local mocks_dir="frontend/deployment/tests/integration/mocks/" From 37dc50478d4b2b6f127933be6074a0baae62462a Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 20 Jan 2026 12:31:13 -0300 Subject: [PATCH 36/40] Claude generation helper --- frontend/README.md | 692 ++++++++++++++++++++++++ frontend/deployment/scripts/setup-layer | 642 ++++++++++++++++++++++ 2 files changed, 1334 insertions(+) create mode 100644 frontend/README.md create mode 100755 frontend/deployment/scripts/setup-layer diff --git a/frontend/README.md b/frontend/README.md new file mode 100644 index 00000000..a790369c --- /dev/null +++ b/frontend/README.md @@ -0,0 +1,692 @@ +# Frontend Deployment Module + +This module provides infrastructure-as-code for deploying static frontend applications across multiple cloud providers. It uses a **layered architecture** that separates concerns and enables mix-and-match combinations of providers, DNS solutions, and CDN/hosting platforms. + +## Table of Contents + +- [Architecture Overview](#architecture-overview) +- [Layer System](#layer-system) +- [Variable Naming Conventions](#variable-naming-conventions) +- [Cross-Layer Communication](#cross-layer-communication) +- [Adding New Layer Implementations](#adding-new-layer-implementations) +- [Setup Script Patterns](#setup-script-patterns) +- [Testing](#testing) +- [Quick Reference](#quick-reference) + +--- + +## Architecture Overview + +``` +┌─────────────────────────────────────────────────────────────────┐ +│ WORKFLOW ENGINE │ +│ (workflows/initial.yaml, workflows/delete.yaml) │ +└─────────────────────────────────────────────────────────────────┘ + │ + ▼ +┌─────────────────────────────────────────────────────────────────┐ +│ LAYER COMPOSITION │ +│ │ +│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────┐ │ +│ │ PROVIDER │ │ NETWORK │ │ DISTRIBUTION │ │ +│ │ LAYER │──▶ LAYER │──▶ LAYER │ │ +│ └──────────────┘ └──────────────┘ └──────────────────────┘ │ +│ │ +│ Implementations: Implementations: Implementations: │ +│ • aws • route53 • cloudfront │ +│ • azure • azure_dns • blob-cdn │ +│ • gcp • cloud_dns • amplify │ +│ • firebase │ +│ • gcs-cdn │ +│ • static-web-apps │ +└─────────────────────────────────────────────────────────────────┘ + │ + ▼ +┌─────────────────────────────────────────────────────────────────┐ +│ TERRAFORM/OPENTOFU │ +│ (composed modules from all active layers) │ +└─────────────────────────────────────────────────────────────────┘ +``` + +### Layer Flow + +1. **Provider Layer**: Configures cloud credentials, state backend, and resource tags +2. **Network Layer**: Sets up DNS zones and records, calculates domains +3. **Distribution Layer**: Deploys CDN/hosting with references to network outputs + +--- + +## Layer System + +Each layer consists of two components: + +### 1. Setup Script (`setup`) + +A bash script that: +- Validates required inputs (environment variables, context) +- Fetches external data (cloud APIs, nullplatform API) +- Updates `TOFU_VARIABLES` with layer-specific configuration +- Registers the module directory in `MODULES_TO_USE` + +### 2. Modules Directory (`modules/`) + +Terraform/OpenTofu files: +- `main.tf` - Resource definitions +- `variables.tf` - Input variable declarations +- `locals.tf` - Computed values and cross-layer references +- `outputs.tf` - Exported values for other layers +- `test_locals.tf` - Test-only stubs (skipped during composition) + +### Directory Structure + +``` +frontend/deployment/ +├── provider/ +│ └── {cloud}/ +│ ├── setup # Validation & module registration +│ └── modules/ +│ ├── provider.tf # Backend & provider config +│ └── variables.tf +│ +├── network/ +│ └── {dns_provider}/ +│ ├── setup +│ └── modules/ +│ ├── main.tf +│ ├── variables.tf +│ ├── locals.tf +│ ├── outputs.tf +│ └── test_locals.tf +│ +├── distribution/ +│ └── {cdn_provider}/ +│ ├── setup +│ └── modules/ +│ ├── main.tf +│ ├── variables.tf +│ ├── locals.tf +│ ├── outputs.tf +│ └── test_locals.tf +│ +├── scripts/ # Shared helper scripts +├── workflows/ # Workflow definitions +└── tests/ # Unit and integration tests +``` + +--- + +## Variable Naming Conventions + +**All variables MUST use layer-prefixed naming** for clarity and to avoid conflicts: + +| Layer | Prefix | Examples | +|-------|--------|----------| +| **Provider** | `{cloud}_provider` | `azure_provider`, `aws_provider`, `gcp_provider` | +| **Provider** | `provider_*` | `provider_resource_tags_json` | +| **Network** | `network_*` | `network_domain`, `network_subdomain`, `network_full_domain`, `network_dns_zone_name` | +| **Distribution** | `distribution_*` | `distribution_storage_account`, `distribution_app_name`, `distribution_blob_prefix` | + +### Provider Object Structure + +Each cloud provider uses an object variable: + +```hcl +# Azure +variable "azure_provider" { + type = object({ + subscription_id = string + resource_group = string + storage_account = string # For Terraform state + container = string # For Terraform state + }) +} + +# AWS +variable "aws_provider" { + type = object({ + region = string + state_bucket = string + lock_table = string + }) +} + +# GCP +variable "gcp_provider" { + type = object({ + project = string + region = string + bucket = string + }) +} +``` + +### Cross-Layer Shared Variables + +These variables are used by multiple layers and MUST use consistent naming across implementations: + +| Variable | Set By | Used By | Description | +|----------|--------|---------|-------------| +| `network_full_domain` | Network | Distribution | Full domain (e.g., `app.example.com`) | +| `network_domain` | Network | Distribution | Base domain (e.g., `example.com`) | +| `network_subdomain` | Network | Distribution | Subdomain part (e.g., `app`) | +| `distribution_target_domain` | Distribution | Network | CDN endpoint hostname for DNS record | +| `distribution_record_type` | Distribution | Network | DNS record type (`CNAME` or `A`) | + +--- + +## Cross-Layer Communication + +Layers communicate through **locals** that are merged when modules are composed together. + +### How It Works + +1. Each layer defines locals in `locals.tf` +2. When modules are composed, all locals are merged into a single namespace +3. Layers can reference each other's locals directly + +### Example: Network → Distribution + +**Network layer exports** (`network/azure_dns/modules/locals.tf`): +```hcl +locals { + network_full_domain = "${var.network_subdomain}.${var.network_domain}" + network_domain = var.network_domain +} +``` + +**Distribution layer consumes** (`distribution/blob-cdn/modules/locals.tf`): +```hcl +locals { + # References network layer's local directly + distribution_has_custom_domain = local.network_full_domain != "" + distribution_full_domain = local.network_full_domain +} +``` + +### Test Locals (`test_locals.tf`) + +For unit testing modules in isolation, use `test_locals.tf` to stub cross-layer dependencies: + +```hcl +# File: test_locals.tf +# NOTE: Files matching test_*.tf are skipped by compose_modules + +variable "network_full_domain" { + description = "Test-only: Simulates network layer output" + default = "" +} + +locals { + network_full_domain = var.network_full_domain +} +``` + +--- + +## Adding New Layer Implementations + +### Quick Start with Boilerplate Script + +Use the provided script to generate the folder structure: + +```bash +# Create a new network layer implementation +./scripts/setup-layer --type network --name cloudflare + +# Create a new distribution layer implementation +./scripts/setup-layer --type distribution --name netlify + +# Create a new provider layer implementation +./scripts/setup-layer --type provider --name digitalocean +``` + +This creates: +``` +frontend/deployment/{type}/{name}/ +├── setup # Boilerplate setup script +└── modules/ + ├── main.tf # Empty, ready for resources + ├── variables.tf # Layer-prefixed variables + ├── locals.tf # Cross-layer locals + ├── outputs.tf # Layer outputs + └── test_locals.tf # Test stubs +``` + +### Manual Steps After Generation + +1. **Edit `setup` script**: Add validation logic and TOFU_VARIABLES updates +2. **Edit `modules/main.tf`**: Add Terraform resources +3. **Edit `modules/variables.tf`**: Define required inputs +4. **Update `modules/locals.tf`**: Add cross-layer references +5. **Add tests**: Create `tests/{type}/{name}/` with `.tftest.hcl` files + +--- + +## Setup Script Patterns + +### Required Structure + +Every setup script must: + +```bash +#!/bin/bash +# ============================================================================= +# {Layer Type}: {Implementation Name} +# +# Brief description of what this layer does. +# ============================================================================= + +set -euo pipefail + +# 1. VALIDATION PHASE +echo "🔍 Validating {Implementation} configuration..." +echo "" + +# Validate required variables +if [ -z "${REQUIRED_VAR:-}" ]; then + echo " ❌ REQUIRED_VAR is missing" + echo "" + echo " 💡 Possible causes:" + echo " • Variable not set in environment" + echo "" + echo " 🔧 How to fix:" + echo " • Set REQUIRED_VAR in your environment" + exit 1 +fi +echo " ✅ REQUIRED_VAR=$REQUIRED_VAR" + +# 2. EXTERNAL DATA FETCHING (if needed) +echo "" +echo " 📡 Fetching {resource}..." +# Call APIs, validate responses + +# 3. UPDATE TOFU_VARIABLES +TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ + --arg var_name "$var_value" \ + '. + { + layer_variable_name: $var_name + }') + +# 4. REGISTER MODULE +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n ${MODULES_TO_USE:-} ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi + +echo "" +echo "✨ {Implementation} configured successfully" +echo "" +``` + +### Logging Conventions + +| Icon | Usage | +|------|-------| +| `🔍` | Starting validation phase | +| `✅` | Successful validation | +| `❌` | Failed validation | +| `📡` | Fetching external data | +| `📝` | Performing an action | +| `💡` | Possible causes of error | +| `🔧` | How to fix instructions | +| `📋` | Debug information | +| `✨` | Success summary | + +### Error Handling Pattern + +```bash +if [ $? -ne 0 ]; then + echo " ❌ Failed to {action}" + echo "" + + # Classify error type + if echo "$output" | grep -q "NotFound"; then + echo " 🔎 Error: Resource not found" + elif echo "$output" | grep -q "Forbidden\|403"; then + echo " 🔒 Error: Permission denied" + else + echo " ⚠️ Error: Unknown error" + fi + + echo "" + echo " 💡 Possible causes:" + echo " • Cause 1" + echo " • Cause 2" + echo "" + echo " 🔧 How to fix:" + echo " 1. Step 1" + echo " 2. Step 2" + echo "" + echo " 📋 Error details:" + echo "$output" | sed 's/^/ /' + + exit 1 +fi +``` + +--- + +## Testing + +We use **three types of tests** to ensure quality at different levels: + +| Test Type | What it Tests | Location | Command | +|-----------|---------------|----------|---------| +| **Unit Tests (BATS)** | Bash setup scripts | `tests/{layer_type}/{name}/` | `make test-unit` | +| **Tofu Tests** | Terraform modules | `{layer_type}/{name}/modules/*.tftest.hcl` | `make test-tofu` | +| **Integration Tests** | Full workflow execution | `tests/integration/test_cases/` | `make test-integration` | + +### 1. Unit Tests (BATS) + +Test bash setup scripts in isolation using mocked commands. + +**Location:** `frontend/deployment/tests/{layer_type}/{name}/setup_test.bats` + +**Run:** `make test-unit` or `make test-unit MODULE=frontend` + +**Example files:** +- Provider: [`tests/provider/azure/setup_test.bats`](deployment/tests/provider/azure/setup_test.bats) +- Network: [`tests/network/azure_dns/setup_test.bats`](deployment/tests/network/azure_dns/setup_test.bats) +- Distribution: [`tests/distribution/blob-cdn/setup_test.bats`](deployment/tests/distribution/blob-cdn/setup_test.bats) + +**Structure:** +```bash +#!/usr/bin/env bats + +setup() { + # Mock external commands (jq, az, aws, np, etc.) + # Set required environment variables + export CONTEXT='{"key": "value"}' + export TOFU_VARIABLES='{}' +} + +@test "validates required environment variable" { + unset REQUIRED_VAR + run source_setup + assert_failure + assert_output --partial "REQUIRED_VAR is missing" +} + +@test "sets TOFU_VARIABLES correctly" { + export REQUIRED_VAR="test-value" + run source_setup + assert_success + # Check TOFU_VARIABLES was updated correctly +} +``` + +### 2. Tofu Tests (OpenTofu/Terraform) + +Test Terraform modules using `tofu test` with mock providers. + +**Location:** `frontend/deployment/{layer_type}/{name}/modules/{name}.tftest.hcl` + +**Run:** `make test-tofu` or `make test-tofu MODULE=frontend` + +**Example files:** +- Provider: [`provider/azure/modules/provider.tftest.hcl`](deployment/provider/azure/modules/provider.tftest.hcl) +- Network: [`network/azure_dns/modules/azure_dns.tftest.hcl`](deployment/network/azure_dns/modules/azure_dns.tftest.hcl) +- Distribution: [`distribution/blob-cdn/modules/blob-cdn.tftest.hcl`](deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl) + +**Structure:** +```hcl +# ============================================================================= +# Mock Providers +# ============================================================================= +mock_provider "azurerm" {} + +# ============================================================================= +# Test Variables +# ============================================================================= +variables { + network_domain = "example.com" + network_subdomain = "app" +} + +# ============================================================================= +# Tests +# ============================================================================= +run "test_dns_record_created" { + command = plan + + assert { + condition = azurerm_dns_cname_record.main[0].name == "app" + error_message = "CNAME record name should be 'app'" + } +} + +run "test_full_domain_output" { + command = plan + + assert { + condition = output.network_full_domain == "app.example.com" + error_message = "Full domain should be 'app.example.com'" + } +} +``` + +### 3. Integration Tests (BATS) + +Test complete workflows with mocked external dependencies (LocalStack, Azure Mock, Smocker). + +**Location:** `frontend/deployment/tests/integration/test_cases/{scenario}/lifecycle_test.bats` + +**Run:** `make test-integration` or `make test-integration MODULE=frontend` + +**Example file:** [`tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats`](deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats) + +**What's mocked:** +- **LocalStack**: AWS services (S3, Route53, STS, IAM, DynamoDB, ACM) +- **Moto**: CloudFront (not in LocalStack free tier) +- **Azure Mock**: Azure ARM APIs (CDN, DNS, Storage) + Blob Storage +- **Smocker**: nullplatform API + +**Structure:** +```bash +#!/usr/bin/env bats + +# Test constants derived from context +TEST_DISTRIBUTION_APP_NAME="my-app" +TEST_NETWORK_DOMAIN="example.com" + +setup_file() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + integration_setup --cloud-provider azure # or: aws + # Pre-create required resources in mocks +} + +teardown_file() { + integration_teardown +} + +setup() { + source "${PROJECT_ROOT}/testing/integration_helpers.sh" + load_context "frontend/deployment/tests/resources/context.json" + + # Configure layer selection + export NETWORK_LAYER="azure_dns" + export DISTRIBUTION_LAYER="blob-cdn" + export TOFU_PROVIDER="azure" + + # Setup API mocks + mock_request "GET" "/provider" "mocks/provider.json" +} + +@test "create infrastructure deploys resources" { + run_workflow "frontend/deployment/workflows/initial.yaml" + + assert_azure_cdn_configured "$TEST_DISTRIBUTION_APP_NAME" ... + assert_azure_dns_configured "$TEST_NETWORK_DOMAIN" ... +} + +@test "destroy infrastructure removes resources" { + run_workflow "frontend/deployment/workflows/delete.yaml" + + assert_azure_cdn_not_configured ... + assert_azure_dns_not_configured ... +} +``` + +### Running Tests + +```bash +# Run all tests +make test-all + +# Run specific test types +make test-unit # BATS unit tests for bash scripts +make test-tofu # OpenTofu module tests +make test-integration # Full workflow integration tests + +# Run tests for specific module +make test-unit MODULE=frontend +make test-tofu MODULE=frontend +make test-integration MODULE=frontend + +# Run with verbose output (integration only) +make test-integration VERBOSE=1 +``` + +--- + +## Quick Reference + +### Environment Variables by Provider + +#### AWS +```bash +export TOFU_PROVIDER=aws +export AWS_REGION=us-east-1 +export TOFU_PROVIDER_BUCKET=my-state-bucket +export TOFU_LOCK_TABLE=my-lock-table +``` + +#### Azure +```bash +export TOFU_PROVIDER=azure +export AZURE_SUBSCRIPTION_ID=xxx +export AZURE_RESOURCE_GROUP=my-rg +export TOFU_PROVIDER_STORAGE_ACCOUNT=mystateaccount +export TOFU_PROVIDER_CONTAINER=tfstate +``` + +#### GCP +```bash +export TOFU_PROVIDER=gcp +export GOOGLE_PROJECT=my-project +export GOOGLE_REGION=us-central1 +export TOFU_PROVIDER_BUCKET=my-state-bucket +``` + +### Layer Selection + +```bash +export NETWORK_LAYER=route53 # or: azure_dns, cloud_dns +export DISTRIBUTION_LAYER=cloudfront # or: blob-cdn, amplify, firebase, etc. +``` + +--- + +## Claude Prompt for Implementing New Layers + +When asking Claude to help implement a new layer, use this prompt template: + +```` +I need to implement a new {layer_type} layer for {provider_name} in the frontend deployment module. + +**IMPORTANT:** Before starting, please read `frontend/README.md` to understand: +- The layer system architecture and how layers interact +- Variable naming conventions (layer prefixes) +- Cross-layer communication via locals +- Setup script patterns and logging conventions +- Testing requirements (unit, tofu, integration) + +**Context:** +- Layer type: {network|distribution|provider} +- Provider/Service: {e.g., Cloudflare, Netlify, DigitalOcean} +- The boilerplate has been created at: frontend/deployment/{layer_type}/{name}/ + +**Requirements:** + +1. **Setup Script** (`setup`): + - Validate these inputs: {list required env vars or context values} + - Fetch data from: {APIs to call} + - Set these TOFU_VARIABLES: {list variables with layer prefix} + - Follow the logging pattern with icons (🔍 ✅ ❌ 📡 💡 🔧 ✨) + - Include comprehensive error handling with causes and fix instructions + +2. **Terraform Module** (`modules/`): + - Create these resources: {list resources} + - Use variable prefix: `{layer_type}_` (e.g., `network_`, `distribution_`) + - Reference cross-layer locals: {e.g., `local.network_full_domain`} + - Export these outputs: {list outputs} + +3. **Cross-Layer Integration:** + - This layer needs from other layers: {list dependencies} + - This layer provides to other layers: {list exports} + +4. **Tests** (create all three types): + - **Unit tests (BATS)**: Test the setup script validation and variable handling + - **Tofu tests**: Test the Terraform module with mock providers + - **Integration tests**: Test the full workflow with mocked cloud APIs + +**Naming Conventions:** +- Variables: `{layer_type}_{descriptive_name}` (e.g., `distribution_bucket_name`) +- Use same variable names as other {layer_type} implementations for shared concepts +- Provider object: `{cloud}_provider` with standard fields + +**Reference Files by Layer Type:** + +For PROVIDER layers, reference: +- Setup script: `frontend/deployment/provider/azure/setup` +- Terraform module: `frontend/deployment/provider/azure/modules/` +- Unit test (BATS): `frontend/deployment/tests/provider/azure/setup_test.bats` +- Tofu test: `frontend/deployment/provider/azure/modules/provider.tftest.hcl` + +For NETWORK layers, reference: +- Setup script: `frontend/deployment/network/azure_dns/setup` +- Terraform module: `frontend/deployment/network/azure_dns/modules/` +- Unit test (BATS): `frontend/deployment/tests/network/azure_dns/setup_test.bats` +- Tofu test: `frontend/deployment/network/azure_dns/modules/azure_dns.tftest.hcl` + +For DISTRIBUTION layers, reference: +- Setup script: `frontend/deployment/distribution/blob-cdn/setup` +- Terraform module: `frontend/deployment/distribution/blob-cdn/modules/` +- Unit test (BATS): `frontend/deployment/tests/distribution/blob-cdn/setup_test.bats` +- Tofu test: `frontend/deployment/distribution/blob-cdn/modules/blob-cdn.tftest.hcl` + +For INTEGRATION tests, reference: +- `frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats` + +Please generate the setup script, terraform files, and all three types of tests following the patterns in the reference files. +```` + +--- + +## Troubleshooting + +### Common Issues + +| Issue | Cause | Solution | +|-------|-------|----------| +| "Variable not found" | Missing setup script execution | Ensure workflow runs all setup scripts in order | +| "Local not found" | Missing cross-layer local | Add to `locals.tf` or check `test_locals.tf` for unit tests | +| "Module not composed" | `MODULES_TO_USE` not updated | Verify setup script appends to `MODULES_TO_USE` | +| "Backend not configured" | Missing provider setup | Run provider layer setup first | + +### Debug Commands + +```bash +# Check composed modules +echo $MODULES_TO_USE + +# Check TOFU_VARIABLES +echo $TOFU_VARIABLES | jq . + +# Validate terraform +cd /path/to/composed/modules && tofu validate +``` diff --git a/frontend/deployment/scripts/setup-layer b/frontend/deployment/scripts/setup-layer new file mode 100755 index 00000000..85ef3f0a --- /dev/null +++ b/frontend/deployment/scripts/setup-layer @@ -0,0 +1,642 @@ +#!/bin/bash +# ============================================================================= +# Layer Boilerplate Generator +# +# Creates the folder structure and template files for a new layer implementation. +# +# Usage: +# ./setup-layer --type network --name cloudflare +# ./setup-layer --type distribution --name netlify +# ./setup-layer --type provider --name digitalocean +# +# This will create: +# frontend/deployment/{type}/{name}/ +# ├── setup +# └── modules/ +# ├── main.tf +# ├── variables.tf +# ├── locals.tf +# ├── outputs.tf +# └── test_locals.tf +# ============================================================================= + +set -euo pipefail + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +NC='\033[0m' + +# Script location +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +DEPLOYMENT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" + +# Parse arguments +LAYER_TYPE="" +LAYER_NAME="" + +while [[ $# -gt 0 ]]; do + case $1 in + --type|-t) + LAYER_TYPE="$2" + shift 2 + ;; + --name|-n) + LAYER_NAME="$2" + shift 2 + ;; + --help|-h) + echo "Usage: $0 --type --name " + echo "" + echo "Examples:" + echo " $0 --type network --name cloudflare" + echo " $0 --type distribution --name netlify" + echo " $0 --type provider --name digitalocean" + exit 0 + ;; + *) + echo -e "${RED}Unknown option: $1${NC}" + exit 1 + ;; + esac +done + +# Validate arguments +if [[ -z "$LAYER_TYPE" ]]; then + echo -e "${RED}Error: --type is required${NC}" + echo "Valid types: provider, network, distribution" + exit 1 +fi + +if [[ -z "$LAYER_NAME" ]]; then + echo -e "${RED}Error: --name is required${NC}" + exit 1 +fi + +if [[ ! "$LAYER_TYPE" =~ ^(provider|network|distribution)$ ]]; then + echo -e "${RED}Error: Invalid layer type '$LAYER_TYPE'${NC}" + echo "Valid types: provider, network, distribution" + exit 1 +fi + +# Sanitize name (lowercase, replace spaces with underscores) +LAYER_NAME=$(echo "$LAYER_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '_' | tr '-' '_') + +# Target directory +TARGET_DIR="$DEPLOYMENT_DIR/$LAYER_TYPE/$LAYER_NAME" + +if [[ -d "$TARGET_DIR" ]]; then + echo -e "${RED}Error: Directory already exists: $TARGET_DIR${NC}" + exit 1 +fi + +echo "" +echo -e "${CYAN}Creating $LAYER_TYPE layer: $LAYER_NAME${NC}" +echo "" + +# Create directory structure +mkdir -p "$TARGET_DIR/modules" + +# Determine variable prefix based on layer type +case $LAYER_TYPE in + provider) + VAR_PREFIX="${LAYER_NAME}_provider" + ;; + network) + VAR_PREFIX="network" + ;; + distribution) + VAR_PREFIX="distribution" + ;; +esac + +# ============================================================================= +# Generate setup script +# ============================================================================= + +cat > "$TARGET_DIR/setup" << 'SETUP_HEADER' +#!/bin/bash +# ============================================================================= +SETUP_HEADER + +# Capitalize first letter of layer type and name +LAYER_TYPE_CAP="$(echo "${LAYER_TYPE:0:1}" | tr '[:lower:]' '[:upper:]')${LAYER_TYPE:1}" +LAYER_NAME_CAP="$(echo "${LAYER_NAME:0:1}" | tr '[:lower:]' '[:upper:]')${LAYER_NAME:1}" + +cat >> "$TARGET_DIR/setup" << SETUP_META +# ${LAYER_TYPE_CAP}: ${LAYER_NAME} +# +# TODO: Add description of what this layer does. +# +# Required environment variables: +# - TODO: List required env vars +# +# Required context values: +# - TODO: List required context paths +# ============================================================================= + +set -euo pipefail + +SETUP_META + +cat >> "$TARGET_DIR/setup" << 'SETUP_BODY' +echo "🔍 Validating configuration..." +echo "" + +# ============================================================================= +# Input Validation +# ============================================================================= + +# TODO: Add validation for required environment variables +# Example: +# if [ -z "${REQUIRED_VAR:-}" ]; then +# echo " ❌ REQUIRED_VAR is missing" +# echo "" +# echo " 💡 Possible causes:" +# echo " • Variable not set in environment" +# echo "" +# echo " 🔧 How to fix:" +# echo " • export REQUIRED_VAR=value" +# exit 1 +# fi +# echo " ✅ REQUIRED_VAR=$REQUIRED_VAR" + +# TODO: Add validation for context values +# Example: +# config_value=$(echo "$CONTEXT" | jq -r '.path.to.value // empty') +# if [ -z "$config_value" ]; then +# echo " ❌ config_value not found in context" +# exit 1 +# fi +# echo " ✅ config_value=$config_value" + +# ============================================================================= +# External Data Fetching (if needed) +# ============================================================================= + +# TODO: Add API calls to fetch external data +# Example: +# echo "" +# echo " 📡 Fetching resource..." +# api_response=$(curl -s "https://api.example.com/resource") +# if [ $? -ne 0 ]; then +# echo " ❌ Failed to fetch resource" +# exit 1 +# fi + +# ============================================================================= +# Update TOFU_VARIABLES +# ============================================================================= + +# TODO: Add layer-specific variables to TOFU_VARIABLES +# Use the appropriate prefix for your layer type: +# - provider: {cloud}_provider object + provider_* vars +# - network: network_* vars +# - distribution: distribution_* vars +# +# Example: +# TOFU_VARIABLES=$(echo "$TOFU_VARIABLES" | jq \ +# --arg var1 "$value1" \ +# --arg var2 "$value2" \ +# '. + { +# layer_variable1: $var1, +# layer_variable2: $var2 +# }') + +# ============================================================================= +# Register Module +# ============================================================================= + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +module_name="${script_dir}/modules" + +if [[ -n ${MODULES_TO_USE:-} ]]; then + MODULES_TO_USE="$MODULES_TO_USE,$module_name" +else + MODULES_TO_USE="$module_name" +fi + +echo "" +echo "✨ Configuration completed successfully" +echo "" +SETUP_BODY + +chmod +x "$TARGET_DIR/setup" +echo -e " ${GREEN}✓${NC} Created setup script" + +# ============================================================================= +# Generate main.tf +# ============================================================================= + +cat > "$TARGET_DIR/modules/main.tf" << MAIN_TF +# ============================================================================= +# ${LAYER_TYPE_CAP}: ${LAYER_NAME} +# +# TODO: Add description of resources managed by this module. +# ============================================================================= + +# TODO: Add terraform resources +# +# Example for network layer: +# resource "cloudflare_record" "main" { +# zone_id = data.cloudflare_zone.main.id +# name = var.network_subdomain +# value = local.distribution_target_domain +# type = local.distribution_record_type +# ttl = 300 +# proxied = true +# } +# +# Example for distribution layer: +# resource "netlify_site" "main" { +# name = var.distribution_app_name +# custom_domain = local.network_full_domain +# } +MAIN_TF + +echo -e " ${GREEN}✓${NC} Created modules/main.tf" + +# ============================================================================= +# Generate variables.tf +# ============================================================================= + +case $LAYER_TYPE in + provider) + cat > "$TARGET_DIR/modules/variables.tf" << VARIABLES_TF +# ============================================================================= +# Provider Layer Variables +# ============================================================================= + +variable "${LAYER_NAME}_provider" { + description = "${LAYER_NAME_CAP} provider configuration" + type = object({ + # TODO: Define provider-specific fields + # Example: + # api_token = string + # account_id = string + }) +} + +variable "provider_resource_tags_json" { + description = "Resource tags to apply to all resources" + type = map(string) + default = {} +} +VARIABLES_TF + ;; + + network) + cat > "$TARGET_DIR/modules/variables.tf" << 'VARIABLES_TF' +# ============================================================================= +# Network Layer Variables +# +# NOTE: Use consistent naming with other network implementations: +# - network_domain: Base domain (e.g., "example.com") +# - network_subdomain: Subdomain part (e.g., "app") +# - network_dns_zone_name: DNS zone identifier +# ============================================================================= + +variable "network_domain" { + description = "Base domain name (e.g., example.com)" + type = string +} + +variable "network_subdomain" { + description = "Subdomain for the application (e.g., app)" + type = string +} + +variable "network_dns_zone_name" { + description = "DNS zone name/identifier" + type = string +} + +# TODO: Add provider-specific variables with network_ prefix +# Example: +# variable "network_zone_id" { +# description = "Cloudflare zone ID" +# type = string +# } +VARIABLES_TF + ;; + + distribution) + cat > "$TARGET_DIR/modules/variables.tf" << 'VARIABLES_TF' +# ============================================================================= +# Distribution Layer Variables +# +# NOTE: Use consistent naming with other distribution implementations: +# - distribution_app_name: Application/site name +# - distribution_storage_account / distribution_bucket: Asset storage +# - distribution_container / distribution_prefix: Asset path +# ============================================================================= + +variable "distribution_app_name" { + description = "Application name for the CDN/hosting" + type = string +} + +# TODO: Add provider-specific variables with distribution_ prefix +# Example for storage-backed CDN: +# variable "distribution_bucket" { +# description = "S3/GCS bucket name for assets" +# type = string +# } +# +# variable "distribution_prefix" { +# description = "Path prefix within the bucket" +# type = string +# default = "" +# } +VARIABLES_TF + ;; +esac + +echo -e " ${GREEN}✓${NC} Created modules/variables.tf" + +# ============================================================================= +# Generate locals.tf +# ============================================================================= + +case $LAYER_TYPE in + provider) + cat > "$TARGET_DIR/modules/locals.tf" << 'LOCALS_TF' +# ============================================================================= +# Provider Layer Locals +# ============================================================================= + +locals { + # Provider layers typically don't need many locals + # Add any computed values here +} +LOCALS_TF + ;; + + network) + cat > "$TARGET_DIR/modules/locals.tf" << 'LOCALS_TF' +# ============================================================================= +# Network Layer Locals +# +# These locals are used by the distribution layer for cross-module integration. +# ============================================================================= + +locals { + # Computed full domain - REQUIRED for distribution layer + network_full_domain = "${var.network_subdomain}.${var.network_domain}" + + # Expose base domain for cross-module use + network_domain = var.network_domain +} +LOCALS_TF + ;; + + distribution) + cat > "$TARGET_DIR/modules/locals.tf" << 'LOCALS_TF' +# ============================================================================= +# Distribution Layer Locals +# +# Cross-layer integration with network layer. +# ============================================================================= + +locals { + # Check if custom domain is configured (from network layer) + distribution_has_custom_domain = local.network_full_domain != "" + + # Full domain from network layer + distribution_full_domain = local.network_full_domain + + # TODO: Set these based on your CDN/hosting provider + # These are consumed by the network layer for DNS record creation + # + # distribution_target_domain: The CDN endpoint hostname (e.g., "d123.cloudfront.net") + # distribution_record_type: DNS record type ("CNAME" or "A" for alias) + # + # Example: + # distribution_target_domain = netlify_site.main.ssl_url + # distribution_record_type = "CNAME" +} +LOCALS_TF + ;; +esac + +echo -e " ${GREEN}✓${NC} Created modules/locals.tf" + +# ============================================================================= +# Generate outputs.tf +# ============================================================================= + +case $LAYER_TYPE in + provider) + cat > "$TARGET_DIR/modules/outputs.tf" << 'OUTPUTS_TF' +# ============================================================================= +# Provider Layer Outputs +# ============================================================================= + +# Provider layers typically don't have outputs +# The provider configuration is used implicitly by other resources +OUTPUTS_TF + ;; + + network) + cat > "$TARGET_DIR/modules/outputs.tf" << 'OUTPUTS_TF' +# ============================================================================= +# Network Layer Outputs +# ============================================================================= + +output "network_full_domain" { + description = "Full domain name (subdomain.domain)" + value = local.network_full_domain +} + +output "network_website_url" { + description = "Full website URL with protocol" + value = "https://${local.network_full_domain}" +} + +# TODO: Add provider-specific outputs +# Example: +# output "network_fqdn" { +# description = "Fully qualified domain name with trailing dot" +# value = "${local.network_full_domain}." +# } +OUTPUTS_TF + ;; + + distribution) + cat > "$TARGET_DIR/modules/outputs.tf" << 'OUTPUTS_TF' +# ============================================================================= +# Distribution Layer Outputs +# ============================================================================= + +output "distribution_cdn_endpoint_hostname" { + description = "CDN endpoint hostname" + value = local.distribution_target_domain +} + +output "distribution_website_url" { + description = "Website URL (custom domain if configured, otherwise CDN URL)" + value = local.distribution_has_custom_domain ? "https://${local.distribution_full_domain}" : "https://${local.distribution_target_domain}" +} + +# TODO: Add provider-specific outputs +# Example: +# output "distribution_site_id" { +# description = "Netlify site ID" +# value = netlify_site.main.id +# } +OUTPUTS_TF + ;; +esac + +echo -e " ${GREEN}✓${NC} Created modules/outputs.tf" + +# ============================================================================= +# Generate test_locals.tf +# ============================================================================= + +case $LAYER_TYPE in + provider) + cat > "$TARGET_DIR/modules/test_locals.tf" << 'TEST_LOCALS_TF' +# ============================================================================= +# Test-Only Locals +# +# NOTE: Files matching test_*.tf are skipped by compose_modules. +# This file provides stubs for unit testing in isolation. +# ============================================================================= + +# Provider layers typically don't need test locals +TEST_LOCALS_TF + ;; + + network) + cat > "$TARGET_DIR/modules/test_locals.tf" << 'TEST_LOCALS_TF' +# ============================================================================= +# Test-Only Locals +# +# NOTE: Files matching test_*.tf are skipped by compose_modules. +# This file provides stubs for unit testing in isolation. +# ============================================================================= + +# Network layer needs distribution layer outputs for DNS records +variable "distribution_target_domain" { + description = "Test-only: CDN endpoint hostname from distribution layer" + type = string + default = "test-cdn.example.net" +} + +variable "distribution_record_type" { + description = "Test-only: DNS record type from distribution layer" + type = string + default = "CNAME" +} + +locals { + distribution_target_domain = var.distribution_target_domain + distribution_record_type = var.distribution_record_type +} +TEST_LOCALS_TF + ;; + + distribution) + cat > "$TARGET_DIR/modules/test_locals.tf" << 'TEST_LOCALS_TF' +# ============================================================================= +# Test-Only Locals +# +# NOTE: Files matching test_*.tf are skipped by compose_modules. +# This file provides stubs for unit testing in isolation. +# ============================================================================= + +# Distribution layer needs network layer outputs for custom domain +variable "network_full_domain" { + description = "Test-only: Full domain from network layer" + type = string + default = "" +} + +variable "network_domain" { + description = "Test-only: Base domain from network layer" + type = string + default = "example.com" +} + +locals { + network_full_domain = var.network_full_domain + network_domain = var.network_domain +} +TEST_LOCALS_TF + ;; +esac + +echo -e " ${GREEN}✓${NC} Created modules/test_locals.tf" + +# ============================================================================= +# Create test directory structure +# ============================================================================= + +TEST_DIR="$DEPLOYMENT_DIR/tests/$LAYER_TYPE/$LAYER_NAME" +mkdir -p "$TEST_DIR" + +cat > "$TEST_DIR/${LAYER_NAME}.tftest.hcl" << TEST_HCL +# ============================================================================= +# Unit Tests: ${LAYER_TYPE}/${LAYER_NAME} +# ============================================================================= + +mock_provider "${LAYER_NAME}" {} + +# ============================================================================= +# Test Variables +# ============================================================================= + +variables { + # TODO: Add test variable values +} + +# ============================================================================= +# Tests +# ============================================================================= + +run "test_basic_configuration" { + command = plan + + # TODO: Add assertions + # assert { + # condition = resource.type.name != null + # error_message = "Resource should be created" + # } +} +TEST_HCL + +echo -e " ${GREEN}✓${NC} Created tests/$LAYER_TYPE/$LAYER_NAME/${LAYER_NAME}.tftest.hcl" + +# ============================================================================= +# Summary +# ============================================================================= + +echo "" +echo -e "${GREEN}Layer created successfully!${NC}" +echo "" +echo "Created structure:" +echo " $TARGET_DIR/" +echo " ├── setup" +echo " └── modules/" +echo " ├── main.tf" +echo " ├── variables.tf" +echo " ├── locals.tf" +echo " ├── outputs.tf" +echo " └── test_locals.tf" +echo "" +echo " $TEST_DIR/" +echo " └── ${LAYER_NAME}.tftest.hcl" +echo "" +echo -e "${YELLOW}Next steps:${NC}" +echo " 1. Edit the setup script to add validation and TOFU_VARIABLES" +echo " 2. Edit modules/main.tf to add Terraform resources" +echo " 3. Update modules/variables.tf with required inputs" +echo " 4. Update modules/locals.tf with cross-layer references" +echo " 5. Add unit tests to tests/$LAYER_TYPE/$LAYER_NAME/" +echo "" +echo -e "${CYAN}Tip:${NC} See frontend/README.md for the Claude prompt template" +echo " to help implement the setup script and Terraform files." +echo "" From 33606b8422a05224b56b4a7d1d3185b49f9998e8 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Tue, 20 Jan 2026 12:38:34 -0300 Subject: [PATCH 37/40] IA model agnostic readme --- frontend/README.md | 73 +++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/frontend/README.md b/frontend/README.md index a790369c..f16f79e8 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -591,53 +591,36 @@ export DISTRIBUTION_LAYER=cloudfront # or: blob-cdn, amplify, firebase, etc. --- -## Claude Prompt for Implementing New Layers +## AI Assistant Prompt for Implementing New Layers -When asking Claude to help implement a new layer, use this prompt template: +When asking an AI assistant to help implement a new layer, just paste this prompt: ```` -I need to implement a new {layer_type} layer for {provider_name} in the frontend deployment module. - -**IMPORTANT:** Before starting, please read `frontend/README.md` to understand: -- The layer system architecture and how layers interact -- Variable naming conventions (layer prefixes) -- Cross-layer communication via locals -- Setup script patterns and logging conventions -- Testing requirements (unit, tofu, integration) - -**Context:** -- Layer type: {network|distribution|provider} -- Provider/Service: {e.g., Cloudflare, Netlify, DigitalOcean} -- The boilerplate has been created at: frontend/deployment/{layer_type}/{name}/ - -**Requirements:** - -1. **Setup Script** (`setup`): - - Validate these inputs: {list required env vars or context values} - - Fetch data from: {APIs to call} - - Set these TOFU_VARIABLES: {list variables with layer prefix} - - Follow the logging pattern with icons (🔍 ✅ ❌ 📡 💡 🔧 ✨) - - Include comprehensive error handling with causes and fix instructions - -2. **Terraform Module** (`modules/`): - - Create these resources: {list resources} - - Use variable prefix: `{layer_type}_` (e.g., `network_`, `distribution_`) - - Reference cross-layer locals: {e.g., `local.network_full_domain`} - - Export these outputs: {list outputs} - -3. **Cross-Layer Integration:** - - This layer needs from other layers: {list dependencies} - - This layer provides to other layers: {list exports} - -4. **Tests** (create all three types): - - **Unit tests (BATS)**: Test the setup script validation and variable handling - - **Tofu tests**: Test the Terraform module with mock providers - - **Integration tests**: Test the full workflow with mocked cloud APIs - -**Naming Conventions:** -- Variables: `{layer_type}_{descriptive_name}` (e.g., `distribution_bucket_name`) -- Use same variable names as other {layer_type} implementations for shared concepts -- Provider object: `{cloud}_provider` with standard fields +I need to implement a new layer in the frontend deployment module. + +**IMPORTANT:** Before starting: + +1. Read `frontend/README.md` to understand: + - The layer system architecture and how layers interact + - Variable naming conventions (layer prefixes) + - Cross-layer communication via locals + - Setup script patterns and logging conventions + - Testing requirements (unit, tofu, integration) + +2. Ask me for the following information: + - Layer type (provider, network, or distribution) + - Provider/service name (e.g., Cloudflare, Netlify, DigitalOcean) + - Required environment variables or context values to validate + - External APIs to call (if any) + - Terraform resources to create + - Cross-layer dependencies and exports + +3. After gathering requirements, generate: + - Setup script with validation and TOFU_VARIABLES + - Terraform module (main.tf, variables.tf, locals.tf, outputs.tf, test_locals.tf) + - Unit tests (BATS) for the setup script + - Tofu tests for the Terraform module + - Integration test additions (if applicable) **Reference Files by Layer Type:** @@ -661,8 +644,6 @@ For DISTRIBUTION layers, reference: For INTEGRATION tests, reference: - `frontend/deployment/tests/integration/test_cases/azure_blobcdn_azuredns/lifecycle_test.bats` - -Please generate the setup script, terraform files, and all three types of tests following the patterns in the reference files. ```` --- From 7f4bfa0876bb96b5996437fe49cf0fc5df3ed4a1 Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Wed, 21 Jan 2026 18:28:05 -0300 Subject: [PATCH 38/40] Do tofu improvements --- frontend/deployment/scripts/do_tofu | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/deployment/scripts/do_tofu b/frontend/deployment/scripts/do_tofu index 8d8d47d5..4d65f044 100644 --- a/frontend/deployment/scripts/do_tofu +++ b/frontend/deployment/scripts/do_tofu @@ -9,6 +9,5 @@ CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}") cd "$CURRENT_DIR" -echo $TOFU_INIT_VARIABLES tofu -chdir="$TOFU_MODULE_DIR" init -input=false $TOFU_INIT_VARIABLES tofu -chdir="$TOFU_MODULE_DIR" "$TOFU_ACTION" -auto-approve -var-file="$TOFU_VAR_FILE" From 6dbecb0e45150982e4029052cc9e7896d6d22e2f Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Mon, 26 Jan 2026 13:48:05 -0300 Subject: [PATCH 39/40] Fix tests --- frontend/deployment/network/azure_dns/setup | 17 ++++++++++------- .../deployment/tests/resources/azure_mocks/az | 12 ++++++++---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/frontend/deployment/network/azure_dns/setup b/frontend/deployment/network/azure_dns/setup index 6f55271d..10b201e2 100755 --- a/frontend/deployment/network/azure_dns/setup +++ b/frontend/deployment/network/azure_dns/setup @@ -35,15 +35,18 @@ echo " ✅ public_dns_zone_resource_group_name=$public_dns_zone_resource_group echo "" echo " 📡 Verifying Azure DNS zone..." -az_output=$(az network dns zone show --name "$public_dns_zone_name" --resource-group "$public_dns_zone_resource_group_name" 2>&1) +stderr_file=$(mktemp) +az_output=$(az network dns zone show --name "$public_dns_zone_name" --resource-group "$public_dns_zone_resource_group_name" 2>"$stderr_file") az_exit_code=$? +az_stderr=$(cat "$stderr_file") +rm -f "$stderr_file" if [ $az_exit_code -ne 0 ]; then echo "" echo " ❌ Failed to fetch Azure DNS zone information" echo "" - if echo "$az_output" | grep -q "ResourceNotFound\|NotFound"; then + if echo "$az_stderr" | grep -q "ResourceNotFound\|NotFound"; then echo " 🔎 Error: DNS zone '$public_dns_zone_name' does not exist in resource group '$public_dns_zone_resource_group_name'" echo "" echo " 💡 Possible causes:" @@ -55,7 +58,7 @@ if [ $az_exit_code -ne 0 ]; then echo " • Verify the DNS zone exists: az network dns zone list --resource-group $public_dns_zone_resource_group_name" echo " • Update 'public_dns_zone_name' in the Azure cloud-provider configuration" - elif echo "$az_output" | grep -q "AuthorizationFailed\|Forbidden\|403"; then + elif echo "$az_stderr" | grep -q "AuthorizationFailed\|Forbidden\|403"; then echo " 🔒 Error: Permission denied when accessing Azure DNS" echo "" echo " 💡 Possible causes:" @@ -66,14 +69,14 @@ if [ $az_exit_code -ne 0 ]; then echo " • Check the Azure credentials are configured correctly" echo " • Ensure the service principal has 'DNS Zone Reader' or 'DNS Zone Contributor' role" - elif echo "$az_output" | grep -q "InvalidSubscriptionId\|SubscriptionNotFound"; then + elif echo "$az_stderr" | grep -q "InvalidSubscriptionId\|SubscriptionNotFound"; then echo " ⚠️ Error: Invalid subscription" echo "" echo " 🔧 How to fix:" echo " • Verify the Azure subscription is correct" echo " • Check the service principal has access to the subscription" - elif echo "$az_output" | grep -q "AADSTS\|InvalidAuthenticationToken\|ExpiredAuthenticationToken"; then + elif echo "$az_stderr" | grep -q "AADSTS\|InvalidAuthenticationToken\|ExpiredAuthenticationToken"; then echo " 🔑 Error: Azure credentials issue" echo "" echo " 💡 Possible causes:" @@ -86,7 +89,7 @@ if [ $az_exit_code -ne 0 ]; then else echo " 📋 Error details:" - echo "$az_output" | sed 's/^/ /' + echo "$az_stderr" | sed 's/^/ /' fi echo "" @@ -168,4 +171,4 @@ if [[ -n $MODULES_TO_USE ]]; then MODULES_TO_USE="$MODULES_TO_USE,$module_name" else MODULES_TO_USE="$module_name" -fi +fi \ No newline at end of file diff --git a/frontend/deployment/tests/resources/azure_mocks/az b/frontend/deployment/tests/resources/azure_mocks/az index 69109597..d33b723f 100755 --- a/frontend/deployment/tests/resources/azure_mocks/az +++ b/frontend/deployment/tests/resources/azure_mocks/az @@ -9,10 +9,14 @@ if [ -z "$AZ_MOCK_RESPONSE" ]; then fi if [ -f "$AZ_MOCK_RESPONSE" ]; then - cat "$AZ_MOCK_RESPONSE" + exit_code="${AZ_MOCK_EXIT_CODE:-0}" + if [ "$exit_code" -eq 0 ]; then + cat "$AZ_MOCK_RESPONSE" + else + cat "$AZ_MOCK_RESPONSE" >&2 + fi + exit "$exit_code" else echo "Mock file not found: $AZ_MOCK_RESPONSE" >&2 exit 1 -fi - -exit "${AZ_MOCK_EXIT_CODE:-0}" +fi \ No newline at end of file From a337c6dbd2d8a35a3c1f2295bae26f380bbcf1be Mon Sep 17 00:00:00 2001 From: Federico Maleh Date: Mon, 26 Jan 2026 14:21:52 -0300 Subject: [PATCH 40/40] Fix integration test certificates --- run_all_tests.sh | 138 ------------------------------- testing/docker/certs/cert.pem | 31 ------- testing/docker/certs/key.pem | 52 ------------ testing/docker/generate-certs.sh | 10 ++- 4 files changed, 6 insertions(+), 225 deletions(-) delete mode 100755 run_all_tests.sh delete mode 100644 testing/docker/certs/cert.pem delete mode 100644 testing/docker/certs/key.pem diff --git a/run_all_tests.sh b/run_all_tests.sh deleted file mode 100755 index 3559547d..00000000 --- a/run_all_tests.sh +++ /dev/null @@ -1,138 +0,0 @@ -#!/bin/bash -# ============================================================================= -# Test runner for all tests (BATS + OpenTofu + Integration) -# -# Usage: -# ./run_all_tests.sh # Run all tests -# ./run_all_tests.sh frontend # Run tests for frontend module only -# ./run_all_tests.sh --skip-integration # Skip integration tests -# ./run_all_tests.sh --only-integration # Run only integration tests -# ============================================================================= - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -cd "$SCRIPT_DIR" - -# Colors -RED='\033[0;31m' -GREEN='\033[0;32m' -CYAN='\033[0;36m' -NC='\033[0m' - -# Parse arguments -MODULE="" -SKIP_INTEGRATION=false -ONLY_INTEGRATION=false - -for arg in "$@"; do - case $arg in - --skip-integration) - SKIP_INTEGRATION=true - ;; - --only-integration) - ONLY_INTEGRATION=true - ;; - *) - MODULE="$arg" - ;; - esac -done - -echo "" -echo "========================================" -echo " Running All Tests" -echo "========================================" -echo "" - -# Track failures -BATS_FAILED=0 -TOFU_FAILED=0 -INTEGRATION_FAILED=0 - -# Run unit tests unless only-integration is specified -if [ "$ONLY_INTEGRATION" = false ]; then - # Run BATS tests - echo -e "${CYAN}[BATS]${NC} Running bash tests..." - echo "" - if ./run_tests.sh $MODULE; then - echo -e "${GREEN}[BATS] All bash tests passed${NC}" - else - BATS_FAILED=1 - echo -e "${RED}[BATS] Some bash tests failed${NC}" - fi - - echo "" - echo "----------------------------------------" - echo "" - - # Run OpenTofu tests - echo -e "${CYAN}[TOFU]${NC} Running OpenTofu tests..." - echo "" - if ./run_tofu_tests.sh $MODULE; then - echo -e "${GREEN}[TOFU] All OpenTofu tests passed${NC}" - else - TOFU_FAILED=1 - echo -e "${RED}[TOFU] Some OpenTofu tests failed${NC}" - fi -fi - -# Run integration tests unless skip-integration is specified -if [ "$SKIP_INTEGRATION" = false ]; then - echo "" - echo "----------------------------------------" - echo "" - - echo -e "${CYAN}[INTEGRATION]${NC} Running integration tests..." - echo "" - if ./run_integration_tests.sh $MODULE; then - echo -e "${GREEN}[INTEGRATION] All integration tests passed${NC}" - else - INTEGRATION_FAILED=1 - echo -e "${RED}[INTEGRATION] Some integration tests failed${NC}" - fi -fi - -# Summary -echo "" -echo "========================================" -echo " Summary" -echo "========================================" -echo "" - -ALL_PASSED=true - -if [ "$ONLY_INTEGRATION" = false ]; then - if [ $BATS_FAILED -eq 0 ]; then - echo -e "${GREEN}BATS tests: PASSED${NC}" - else - echo -e "${RED}BATS tests: FAILED${NC}" - ALL_PASSED=false - fi - - if [ $TOFU_FAILED -eq 0 ]; then - echo -e "${GREEN}OpenTofu tests: PASSED${NC}" - else - echo -e "${RED}OpenTofu tests: FAILED${NC}" - ALL_PASSED=false - fi -fi - -if [ "$SKIP_INTEGRATION" = false ]; then - if [ $INTEGRATION_FAILED -eq 0 ]; then - echo -e "${GREEN}Integration tests: PASSED${NC}" - else - echo -e "${RED}Integration tests: FAILED${NC}" - ALL_PASSED=false - fi -fi - -echo "" - -if [ "$ALL_PASSED" = true ]; then - echo -e "${GREEN}All tests passed!${NC}" - exit 0 -else - echo -e "${RED}Some tests failed${NC}" - exit 1 -fi diff --git a/testing/docker/certs/cert.pem b/testing/docker/certs/cert.pem deleted file mode 100644 index 62193133..00000000 --- a/testing/docker/certs/cert.pem +++ /dev/null @@ -1,31 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIFTzCCAzegAwIBAgIJAKYiFW96jfCZMA0GCSqGSIb3DQEBCwUAMCExHzAdBgNV -BAMMFmludGVncmF0aW9uLXRlc3QtcHJveHkwHhcNMjYwMTE5MTUwNDU4WhcNMzYw -MTE3MTUwNDU4WjAhMR8wHQYDVQQDDBZpbnRlZ3JhdGlvbi10ZXN0LXByb3h5MIIC -IjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxQyROLpKynRIjYmK4I7kHgq7 -L4dZFLG7gR3ObG29lj/Nha6BaxrxeS7I716hy+L45gyRHnuyOdC+82bsUEpb0PXA -qkWSbm9nhAkmp0GfQKkhhySiOxnyL2RtZgrcqCRqX+OROHG8o6K2PcgAq1NEUCCp -qT2rIBpROUbjQjoiCnH6AUEkNc2AYahK1w/lKNZG5wYMXq01n/jQT7lNP58b6J+G -y4qNPOWl7maEYKXdMeU0Di/+H71dKmq5Ag6sngdZzqYsWf3NzajJI+H6jE/kTTHZ -8ldBKsus6Y16ll8EKm6vxm8dTmu4SoM/qbQW9PJw6qUqKOze4HQ2/GnlkI4Zat0A -16sYQHA1j94MItV2B1j/6ITHcGQwRuUJS60hU1OYQBaelnTfJfaDn+2ynQgnUeop -HczgIAGzHOPR25KSjJP9eBeqYK+01hcSRfVr0uwPijaZVOIFXkPvEsRUvoS/Ofkk -BaPJdJzpIVlAC1AAXgkjGaaj+Mqlp5onlm3bvTWDFuo2WWXYEXcNeZ8KNK0olIca -r/5DcOywSFWJSbJlD1mmiF7cQSQc0F4KgNQ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testing/docker/certs/key.pem deleted file mode 100644 index 592dd4f4..00000000 --- a/testing/docker/certs/key.pem +++ /dev/null @@ -1,52 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDFDJE4ukrKdEiN -iYrgjuQeCrsvh1kUsbuBHc5sbb2WP82FroFrGvF5LsjvXqHL4vjmDJEee7I50L7z -ZuxQSlvQ9cCqRZJub2eECSanQZ9AqSGHJKI7GfIvZG1mCtyoJGpf45E4cbyjorY9 -yACrU0RQIKmpPasgGlE5RuNCOiIKcfoBQSQ1zYBhqErXD+Uo1kbnBgxerTWf+NBP -uU0/nxvon4bLio085aXuZoRgpd0x5TQOL/4fvV0qarkCDqyeB1nOpixZ/c3NqMkj -4fqMT+RNMdnyV0Eqy6zpjXqWXwQqbq/Gbx1Oa7hKgz+ptBb08nDqpSoo7N7gdDb8 -aeWQjhlq3QDXqxhAcDWP3gwi1XYHWP/ohMdwZDBG5QlLrSFTU5hAFp6WdN8l9oOf -7bKdCCdR6ikdzOAgAbMc49HbkpKMk/14F6pgr7TWFxJF9WvS7A+KNplU4gVeQ+8S -xFS+hL85+SQFo8l0nOkhWUALUABeCSMZpqP4yqWnmieWbdu9NYMW6jZZZdgRdw15 -nwo0rSiUhxqv/kNw7LBIVYlJsmUPWaaIXtxBJBzQXgqA1BJw585IgF7wvzuj5usX -9r1LtA09w7eTtdX8B0jS6vsSnMKytqDbCUuN3ZbFfS2gpFoPRinNQPi4BJHv70iV -hUvA4zLa7IeJoDaglw2xP2aytDzkQQIDAQABAoICAQCCY0x9AxiWWtffgFH7QdJE -5sjyLFeP0API7lY3fW5kS5fNi6lrnAqJK6IecroRVgFpCIvGZgeLJkwUd9iLUIjs -/pEcmqjIlsMipYOETXH5sXDUIjOPdB3DqmqRiUJ1qJMTHFxtwyUWCocY3o1C0Ph1 -JQffS0U/GusAQZ4Dpr/7tWu/BMHXMEJxXJEZOhVjLlcAbAonY+oGDvi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3lrbABKUsa1KS1X+pLUrRLp51qjp -4r+q8VsBfm7mFZvVEJU7aBxNa6gb8EVXPyq7YUM2L5aZySCOyXPPPIJ12KS8Q5lg -lXRw/EL0eV8K3WP/szUlyzgUbpEFlvk= ------END PRIVATE KEY----- diff --git a/testing/docker/generate-certs.sh b/testing/docker/generate-certs.sh index 02f7f7bf..1c3069f8 100755 --- a/testing/docker/generate-certs.sh +++ b/testing/docker/generate-certs.sh @@ -1,5 +1,6 @@ #!/bin/bash -# Generate self-signed certificates for smocker TLS +# Generate self-signed certificates for integration test TLS proxy +# These certificates are used by nginx to proxy requests to mock services CERT_DIR="$(dirname "$0")/certs" mkdir -p "$CERT_DIR" @@ -7,13 +8,14 @@ mkdir -p "$CERT_DIR" # Generate private key openssl genrsa -out "$CERT_DIR/key.pem" 2048 2>/dev/null -# Generate self-signed certificate +# Generate self-signed certificate with all required SANs +# These hostnames match the nginx proxy configuration openssl req -new -x509 \ -key "$CERT_DIR/key.pem" \ -out "$CERT_DIR/cert.pem" \ -days 365 \ - -subj "/CN=api.nullplatform.com" \ - -addext "subjectAltName=DNS:api.nullplatform.com,DNS:localhost" \ + -subj "/CN=integration-test-proxy" \ + -addext "subjectAltName=DNS:api.nullplatform.com,DNS:management.azure.com,DNS:login.microsoftonline.com,DNS:devstoreaccount1.blob.core.windows.net,DNS:localhost" \ 2>/dev/null echo "Certificates generated in $CERT_DIR"