diff --git a/infra/modules/providers/azure/app-insights/test.sh b/infra/modules/providers/azure/app-insights/test.sh index c1a76e26547ac4ed3ce243169aae24116dbd752a..3e0a767f43160028cc3ea6842c5e7b0a5f61451e 100755 --- a/infra/modules/providers/azure/app-insights/test.sh +++ b/infra/modules/providers/azure/app-insights/test.sh @@ -74,14 +74,12 @@ setup_configuration() { create_tfvars_files() { local tfvars_content=" -appinsights_name = \"$APP_INSIGHTS_NAME\" -service_plan_resource_group_name = \"$RESOURCE_GROUP_NAME\" -appinsights_application_type = \"$APP_INSIGHTS_TYPE\" -workspace_id = \"$WORKSPACE_ID\" +name = \"$APP_INSIGHTS_NAME\" +resource_group_name = \"$RESOURCE_GROUP_NAME\" +location = \"$LOCATION\" resource_tags = { - environment = \"testing\" - module = \"app-insights\" + osdu = \"module\" }" create_base_tfvars_files "$tfvars_content" } diff --git a/infra/modules/providers/azure/app-insights/testing/main.tf b/infra/modules/providers/azure/app-insights/testing/main.tf index 65b7257b6b7523722aae05fb5febdb7c607ff4da..bd315c42b0494e46d45b494561e38cae757c3f68 100644 --- a/infra/modules/providers/azure/app-insights/testing/main.tf +++ b/infra/modules/providers/azure/app-insights/testing/main.tf @@ -16,28 +16,20 @@ provider "azurerm" { features {} } -module "resource_group" { - source = "../../resource-group" - - name = "osdu-module" - location = "eastus2" -} - # Create Log Analytics workspace for App Insights resource "azurerm_log_analytics_workspace" "test" { - name = "osdu-module-workspace-${module.resource_group.random}" - resource_group_name = module.resource_group.name - location = module.resource_group.location + name = "${var.name}-workspace" + resource_group_name = var.resource_group_name + location = var.location sku = "PerGB2018" retention_in_days = 30 } module "app-insights" { - source = "../" - depends_on = [module.resource_group] + source = "../" - appinsights_name = "osdu-module-app-insights-${module.resource_group.random}" - service_plan_resource_group_name = module.resource_group.name + appinsights_name = var.name + service_plan_resource_group_name = var.resource_group_name appinsights_application_type = "java" workspace_id = azurerm_log_analytics_workspace.test.id @@ -45,3 +37,28 @@ module "app-insights" { osdu = "module" } } + +# Variables +variable "name" { + type = string + description = "The name of the App Insights instance" +} + +variable "resource_group_name" { + type = string + description = "The name of the resource group" +} + +variable "location" { + type = string + description = "The location of the App Insights instance" + default = "eastus2" +} + +variable "resource_tags" { + type = map(string) + description = "Resource tags" + default = { + osdu = "module" + } +} diff --git a/infra/modules/providers/azure/app-insights/testing/unit_test.go b/infra/modules/providers/azure/app-insights/testing/unit_test.go index dc9c0b9c32d253b6b03cc88823116f0d5f8d7ca3..10390bd59e7d869bf9bc9f8c6f8cf073a0131cdf 100644 --- a/infra/modules/providers/azure/app-insights/testing/unit_test.go +++ b/infra/modules/providers/azure/app-insights/testing/unit_test.go @@ -10,13 +10,16 @@ import ( "github.com/microsoft/cobalt/test-harness/infratests" ) -var workspace = "osdu-services-" + strings.ToLower(random.UniqueId()) -var location = "eastus" -var count = 5 +var workspace = "appinsights-" + strings.ToLower(random.UniqueId()) +var count = 2 // One for Log Analytics workspace and one for App Insights var tfOptions = &terraform.Options{ TerraformDir: "./", - Upgrade: false, + Upgrade: true, + Vars: map[string]interface{}{ + "name": "appinsights" + strings.ToLower(random.UniqueId()), + "resource_group_name": "osdu-module", // Fixed name for unit tests + }, } func asMap(t *testing.T, jsonString string) map[string]interface{} { @@ -28,9 +31,11 @@ func asMap(t *testing.T, jsonString string) map[string]interface{} { } func TestTemplate(t *testing.T) { - - expectedResult := asMap(t, `{ - "application_type" : "java" + expectedAppInsights := asMap(t, `{ + "application_type": "java", + "tags": { + "osdu": "module" + } }`) testFixture := infratests.UnitTestFixture{ @@ -40,7 +45,7 @@ func TestTemplate(t *testing.T) { PlanAssertions: nil, ExpectedResourceCount: count, ExpectedResourceAttributeValues: infratests.ResourceDescription{ - "module.app-insights.azurerm_application_insights.appinsights": expectedResult, + "module.app-insights.azurerm_application_insights.appinsights": expectedAppInsights, }, } diff --git a/infra/modules/providers/azure/keyvault/test.sh b/infra/modules/providers/azure/keyvault/test.sh new file mode 100755 index 0000000000000000000000000000000000000000..bc9b5b0f33d0d70db5e6842968c6d9f8cf64b81a --- /dev/null +++ b/infra/modules/providers/azure/keyvault/test.sh @@ -0,0 +1,153 @@ +#!/bin/bash + +# Exit on error +set -e + +############################### +# Source Common Functions +############################### +COMMON_LIB="../test-functions.sh" +if [ ! -f "$COMMON_LIB" ]; then + echo "Error: Common library not found at $COMMON_LIB" + exit 1 +fi +source "$COMMON_LIB" + + +############################### +# Script Configuration +############################### +SCRIPT_DIR=$(get_script_dir) +setup_test_directories "$SCRIPT_DIR" + + +############################### +# Required Environment Variables +############################### +validate_azure_credentials + + +############################### +# Optional Variables +############################### +# These can be overridden by setting them before running the script +RESOURCE_GROUP_PREFIX=${RESOURCE_GROUP_NAME:-${RESOURCE_GROUP_PREFIX:-"terraform-test"}} +DEFAULT_LOCATION="eastus2" +KEYVAULT_NAME=${KEYVAULT_NAME:-""} # Will be auto-generated if not provided +KEYVAULT_SKU=${KEYVAULT_SKU:-"standard"} +PUBLIC_NETWORK_ACCESS=${PUBLIC_NETWORK_ACCESS:-"true"} +PURGE_PROTECTION=${PURGE_PROTECTION:-"false"} + + +############################### +# Help Documentation +############################### +print_help() { + print_common_help "Key Vault" "\n- If not specified, a unique Key Vault name will be generated\n- Default SKU is 'standard'\n- Default public network access is enabled\n- Default purge protection is disabled" +} + + +############################### +# Required Module Functions +############################### +setup_configuration() { + # Setup base configuration first + setup_base_configuration "$RESOURCE_GROUP_PREFIX" "$DEFAULT_LOCATION" "$@" + + # Generate Key Vault name if not provided + if [ -z "$KEYVAULT_NAME" ]; then + KEYVAULT_NAME=$(generate_unique_name "" "kv") + fi + + # Export additional variables for Go tests + export KEYVAULT_NAME + export KEYVAULT_SKU + export PUBLIC_NETWORK_ACCESS + export PURGE_PROTECTION +} + +create_tfvars_files() { + local tfvars_content=" +keyvault_name = \"$KEYVAULT_NAME\" +resource_group_name = \"$RESOURCE_GROUP_NAME\" +keyvault_sku = \"$KEYVAULT_SKU\" +public_network_access_enabled = $PUBLIC_NETWORK_ACCESS +keyvault_purge_protection_enabled = $PURGE_PROTECTION + +resource_tags = { + environment = \"testing\" + module = \"keyvault\" +}" + create_base_tfvars_files "$tfvars_content" +} + + +############################### +# Optional Module Functions +############################### +validate_variables() { + validate_base_variables + + # Validate Key Vault specific variables + if [[ ! "$KEYVAULT_NAME" =~ ^[a-zA-Z0-9-]{3,24}$ ]]; then + log "Error: Key Vault name must be alphanumeric with hyphens and between 3-24 characters" 1 + exit 1 + fi + + # Validate SKU + if [[ ! "$KEYVAULT_SKU" =~ ^(standard|premium)$ ]]; then + log "Error: Key Vault SKU must be either 'standard' or 'premium'" 1 + exit 1 + fi + + # Validate boolean values + if [[ ! "$PUBLIC_NETWORK_ACCESS" =~ ^(true|false)$ ]]; then + log "Error: public_network_access_enabled must be either 'true' or 'false'" 1 + exit 1 + fi + + if [[ ! "$PURGE_PROTECTION" =~ ^(true|false)$ ]]; then + log "Error: keyvault_purge_protection_enabled must be either 'true' or 'false'" 1 + exit 1 + fi + + log "Using Key Vault: $KEYVAULT_NAME" 6 + log "Using SKU: $KEYVAULT_SKU" 6 + log "Public Network Access: $PUBLIC_NETWORK_ACCESS" 6 + log "Purge Protection: $PURGE_PROTECTION" 6 +} + + +############################### +# Main Execution +############################### +main() { + # Trap cleanup on exit + trap 'cleanup; cleanup_resource_group "$RESOURCE_GROUP_NAME" "$ARM_SUBSCRIPTION_ID"' EXIT + + # Setup configuration + setup_configuration "$@" + + # Validate variables + validate_variables + + # Setup Azure and create resource group + setup_azure_with_rg + + # Create tfvars file + create_tfvars_files + + # Run all tests + run_standard_test_sequence +} + +# Check for help flag +case "$1" in + -h|--help) + print_help + exit 0 + ;; +esac + +# Execute main function +main "$@" \ No newline at end of file diff --git a/infra/modules/providers/azure/keyvault/testing/main.tf b/infra/modules/providers/azure/keyvault/testing/main.tf index bd633de571d3a6a6134fb10466886406aed6fe10..26abe03462d4839cfc6e956d9a84fe4e504bcad7 100644 --- a/infra/modules/providers/azure/keyvault/testing/main.tf +++ b/infra/modules/providers/azure/keyvault/testing/main.tf @@ -16,19 +16,62 @@ provider "azurerm" { features {} } -module "resource_group" { - source = "../../resource-group" +module "keyvault" { + source = "../" + + keyvault_name = var.keyvault_name + resource_group_name = var.resource_group_name + keyvault_sku = "standard" + public_network_access_enabled = true + keyvault_purge_protection_enabled = false - name = "osdu-module" - location = "eastus2" + resource_tags = { + environment = "testing" + module = "keyvault" + } } -module "keyvault" { - source = "../" - depends_on = [module.resource_group] +# Variables +variable "keyvault_name" { + type = string + description = "The name of the key vault" +} - resource_group_name = module.resource_group.name - resource_tags = { - osdu = "module" +variable "resource_group_name" { + type = string + description = "The name of the resource group" +} + +variable "location" { + type = string + description = "The location of the key vault" + default = "eastus2" +} + +# Add all the variables that were previously in tfvars +variable "keyvault_sku" { + type = string + description = "The SKU of the Key Vault" + default = "standard" +} + +variable "public_network_access_enabled" { + type = bool + description = "Whether public network access is enabled" + default = true +} + +variable "keyvault_purge_protection_enabled" { + type = bool + description = "Whether purge protection is enabled" + default = false +} + +variable "resource_tags" { + type = map(string) + description = "Resource tags" + default = { + environment = "testing" + module = "keyvault" } } diff --git a/infra/modules/providers/azure/keyvault/testing/unit_test.go b/infra/modules/providers/azure/keyvault/testing/unit_test.go index 5a9d38dc74810b41ce981f623cc8cad0cca0eb08..a09475faedce9224e2c668fc3d1fcde1abe73e25 100644 --- a/infra/modules/providers/azure/keyvault/testing/unit_test.go +++ b/infra/modules/providers/azure/keyvault/testing/unit_test.go @@ -16,6 +16,7 @@ package test import ( "encoding/json" + "strings" "testing" "github.com/gruntwork-io/terratest/modules/random" @@ -23,13 +24,16 @@ import ( "github.com/microsoft/cobalt/test-harness/infratests" ) -var name = "keyvault-" -var location = "eastus" -var count = 6 +var workspace = "keyvault-" + strings.ToLower(random.UniqueId()) +var count = 2 // Updated to include both data sources and resources var tfOptions = &terraform.Options{ TerraformDir: "./", Upgrade: true, + Vars: map[string]interface{}{ + "keyvault_name": "kv" + strings.ToLower(random.UniqueId()), + "resource_group_name": "osdu-module", // Fixed name for unit tests + }, } func asMap(t *testing.T, jsonString string) map[string]interface{} { @@ -41,20 +45,18 @@ func asMap(t *testing.T, jsonString string) map[string]interface{} { } func TestTemplate(t *testing.T) { - expectedKeyVault := asMap(t, `{ - "name" : "spkeyvault", - "resource_group_name" : "osdu-module", - "sku_name" : "standard", - "tags" : { - "osdu" : "module" + "sku_name": "standard", + "tags": { + "environment": "testing", + "module": "keyvault" } }`) testFixture := infratests.UnitTestFixture{ GoTest: t, TfOptions: tfOptions, - Workspace: name + random.UniqueId(), + Workspace: workspace, PlanAssertions: nil, ExpectedResourceCount: count, ExpectedResourceAttributeValues: infratests.ResourceDescription{ diff --git a/infra/modules/providers/azure/keyvault/variables.tf b/infra/modules/providers/azure/keyvault/variables.tf index 0d8f11c87e0b43d03bfcd10ca7f9f78dbb31dbb5..7179f0fc677c2e2b3ceb3eda218b7bbbf8612dfb 100644 --- a/infra/modules/providers/azure/keyvault/variables.tf +++ b/infra/modules/providers/azure/keyvault/variables.tf @@ -53,7 +53,7 @@ variable "keyvault_certificate_permissions" { variable "keyvault_purge_protection_enabled" { description = "Vault or an object in the deleted state cannot be purged until the retention period has passed. Once Purge Protection has been Enabled it's not possible to Disable it" type = bool - default = true + default = false } variable "public_network_access_enabled" { diff --git a/infra/modules/providers/azure/log-analytics/test.sh b/infra/modules/providers/azure/log-analytics/test.sh index ced5db769d12593246497af65915bc89856479e7..8e5c4d2fa5070128d6510e47e743c0764c295bc1 100755 --- a/infra/modules/providers/azure/log-analytics/test.sh +++ b/infra/modules/providers/azure/log-analytics/test.sh @@ -61,7 +61,21 @@ setup_configuration() { } create_tfvars_files() { - local tfvars_content="name = \"$LOG_ANALYTICS_NAME\"\nresource_group_name = \"$RESOURCE_GROUP_NAME\"\n\nsolutions = [\n {\n solution_name = \"ContainerInsights\"\n publisher = \"Microsoft\"\n product = \"OMSGallery/ContainerInsights\"\n }\n]\n\nresource_tags = {\n osdu = \"module\"\n}" + local tfvars_content=" +name = \"$LOG_ANALYTICS_NAME\" +resource_group_name = \"$RESOURCE_GROUP_NAME\" + +solutions = [ + { + solution_name = \"ContainerInsights\" + publisher = \"Microsoft\" + product = \"OMSGallery/ContainerInsights\" + } +] + +resource_tags = { + osdu = \"module\" +}" create_base_tfvars_files "$tfvars_content" } diff --git a/infra/modules/providers/azure/log-analytics/testing/main.tf b/infra/modules/providers/azure/log-analytics/testing/main.tf index 88014997e276695e49044fa553229286c0f4cb43..f9c7cecce1df32383b5d71e55c200354933fb918 100644 --- a/infra/modules/providers/azure/log-analytics/testing/main.tf +++ b/infra/modules/providers/azure/log-analytics/testing/main.tf @@ -12,23 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. +# terraform { +# required_providers { +# azurerm = { +# source = "hashicorp/azurerm" +# version = "=3.90.0" +# } +# } +# } + provider "azurerm" { features {} } -module "resource_group" { - source = "../../resource-group" - - name = "osdu-module" - location = "eastus2" -} - module "log_analytics" { - source = "../" - depends_on = [module.resource_group] + source = "../" - name = "osdu-module-logs-${module.resource_group.random}" - resource_group_name = module.resource_group.name + name = var.name + resource_group_name = var.resource_group_name solutions = [ { @@ -38,8 +39,48 @@ module "log_analytics" { } ] - # Tags resource_tags = { osdu = "module" } } + +# Variables +variable "name" { + type = string + description = "The name of the Log Analytics workspace" +} + +variable "resource_group_name" { + type = string + description = "The name of the resource group" +} + +variable "location" { + type = string + description = "The location of the Log Analytics workspace" + default = "eastus2" +} + +variable "solutions" { + type = list(object({ + solution_name = string + publisher = string + product = string + })) + description = "The solutions to add to the workspace" + default = [ + { + solution_name = "ContainerInsights" + publisher = "Microsoft" + product = "OMSGallery/ContainerInsights" + } + ] +} + +variable "resource_tags" { + type = map(string) + description = "Resource tags" + default = { + osdu = "module" + } +} diff --git a/infra/modules/providers/azure/log-analytics/testing/unit_test.go b/infra/modules/providers/azure/log-analytics/testing/unit_test.go index 06b09a3a71f40827a4a3425511c2e447850b0f32..df9f0b5d35398f88a36d6e8335cd281e03f783a4 100644 --- a/infra/modules/providers/azure/log-analytics/testing/unit_test.go +++ b/infra/modules/providers/azure/log-analytics/testing/unit_test.go @@ -16,6 +16,7 @@ package test import ( "encoding/json" + "strings" "testing" "github.com/gruntwork-io/terratest/modules/random" @@ -23,13 +24,16 @@ import ( "github.com/microsoft/cobalt/test-harness/infratests" ) -var name = "logs-" -var location = "eastus" -var count = 5 +var workspace = "logs-" + strings.ToLower(random.UniqueId()) +var count = 2 // One for the workspace and one for the solution var tfOptions = &terraform.Options{ TerraformDir: "./", Upgrade: true, + Vars: map[string]interface{}{ + "name": "logs" + strings.ToLower(random.UniqueId()), + "resource_group_name": "osdu-module", // Fixed name for unit tests + }, } func asMap(t *testing.T, jsonString string) map[string]interface{} { @@ -41,19 +45,21 @@ func asMap(t *testing.T, jsonString string) map[string]interface{} { } func TestTemplate(t *testing.T) { - - expectedResult := asMap(t, `{ - "retention_in_days": 30 + expectedLogAnalytics := asMap(t, `{ + "retention_in_days": 30, + "tags": { + "osdu": "module" + } }`) testFixture := infratests.UnitTestFixture{ GoTest: t, TfOptions: tfOptions, - Workspace: name + random.UniqueId(), + Workspace: workspace, PlanAssertions: nil, ExpectedResourceCount: count, ExpectedResourceAttributeValues: infratests.ResourceDescription{ - "module.log_analytics.azurerm_log_analytics_workspace.main": expectedResult, + "module.log_analytics.azurerm_log_analytics_workspace.main": expectedLogAnalytics, }, }