|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Import test framework |
| 4 | +source "$(dirname "$0")/../test_framework.sh" |
| 5 | + |
| 6 | +# Source only the function we want to test |
| 7 | +source "$(dirname "$0")/../test_functions/getClusterCloudEnvironment.sh" |
| 8 | + |
| 9 | +# Test valid environment variable values |
| 10 | +test_valid_environment_variables() { |
| 11 | + local test_cases=( |
| 12 | + "azurepubliccloud" |
| 13 | + "azurechinacloud" |
| 14 | + "azureusgovernmentcloud" |
| 15 | + "usnat" |
| 16 | + "ussec" |
| 17 | + "bleu" |
| 18 | + ) |
| 19 | + |
| 20 | + for cloud in "${test_cases[@]}"; do |
| 21 | + setup |
| 22 | + export CLUSTER_CLOUD_ENVIRONMENT="$cloud" |
| 23 | + result=$(getClusterCloudEnvironment) |
| 24 | + assert_equals "$cloud" "$result" "(with CLUSTER_CLOUD_ENVIRONMENT=$cloud)" |
| 25 | + teardown |
| 26 | + done |
| 27 | +} |
| 28 | + |
| 29 | +# Test invalid environment variable value |
| 30 | +test_invalid_environment_variable() { |
| 31 | + setup |
| 32 | + export CLUSTER_CLOUD_ENVIRONMENT="invalidcloud" |
| 33 | + result=$(getClusterCloudEnvironment) |
| 34 | + assert_equals "unknown" "$result" "(with invalid CLUSTER_CLOUD_ENVIRONMENT)" |
| 35 | + teardown |
| 36 | +} |
| 37 | + |
| 38 | +# Test domain file fallback with valid values |
| 39 | +test_domain_file_fallback() { |
| 40 | + local test_cases=( |
| 41 | + "opinsights.azure.com:azurepubliccloud" |
| 42 | + "opinsights.azure.cn:azurechinacloud" |
| 43 | + "opinsights.azure.us:azureusgovernmentcloud" |
| 44 | + "opinsights.azure.eaglex.ic.gov:usnat" |
| 45 | + "opinsights.azure.microsoft.scloud:ussec" |
| 46 | + "opinsights.sovcloud-api.fr:bleu" |
| 47 | + ) |
| 48 | + |
| 49 | + for test_case in "${test_cases[@]}"; do |
| 50 | + setup |
| 51 | + local domain="${test_case%%:*}" |
| 52 | + local expected="${test_case#*:}" |
| 53 | + |
| 54 | + # Unset environment variable to test fallback |
| 55 | + unset CLUSTER_CLOUD_ENVIRONMENT |
| 56 | + |
| 57 | + # Create domain file |
| 58 | + mock_file "$TEST_DIR/etc/ama-logs-secret/DOMAIN" "$domain" |
| 59 | + |
| 60 | + result=$(getClusterCloudEnvironment) |
| 61 | + assert_equals "$expected" "$result" "(with domain=$domain)" |
| 62 | + teardown |
| 63 | + done |
| 64 | +} |
| 65 | + |
| 66 | +# Test with invalid domain file value |
| 67 | +test_invalid_domain() { |
| 68 | + setup |
| 69 | + unset CLUSTER_CLOUD_ENVIRONMENT |
| 70 | + mock_file "$TEST_DIR/etc/ama-logs-secret/DOMAIN" "invalid.domain.com" |
| 71 | + |
| 72 | + result=$(getClusterCloudEnvironment) |
| 73 | + assert_equals "unknown" "$result" "(with invalid domain)" |
| 74 | + teardown |
| 75 | +} |
| 76 | + |
| 77 | +# Test with empty domain file |
| 78 | +test_empty_domain() { |
| 79 | + setup |
| 80 | + unset CLUSTER_CLOUD_ENVIRONMENT |
| 81 | + mock_file "$TEST_DIR/etc/ama-logs-secret/DOMAIN" "" |
| 82 | + |
| 83 | + result=$(getClusterCloudEnvironment) |
| 84 | + assert_equals "unknown" "$result" "(with empty domain)" |
| 85 | + teardown |
| 86 | +} |
| 87 | + |
| 88 | +# Test with missing domain file |
| 89 | +test_missing_domain_file() { |
| 90 | + setup |
| 91 | + unset CLUSTER_CLOUD_ENVIRONMENT |
| 92 | + # Don't create the domain file |
| 93 | + |
| 94 | + result=$(getClusterCloudEnvironment) |
| 95 | + assert_equals "azurepubliccloud" "$result" "(with missing domain file)" |
| 96 | + teardown |
| 97 | +} |
| 98 | + |
| 99 | +# Test environment variable precedence over domain file |
| 100 | +test_env_precedence_over_domain() { |
| 101 | + setup |
| 102 | + export CLUSTER_CLOUD_ENVIRONMENT="azurepubliccloud" |
| 103 | + mock_file "$TEST_DIR/etc/ama-logs-secret/DOMAIN" "opinsights.azure.cn" |
| 104 | + |
| 105 | + result=$(getClusterCloudEnvironment) |
| 106 | + assert_equals "azurepubliccloud" "$result" "(env should take precedence over domain)" |
| 107 | + teardown |
| 108 | +} |
| 109 | + |
| 110 | +# Run all tests |
| 111 | +run_all_tests() { |
| 112 | + echo "Running tests for getClusterCloudEnvironment..." |
| 113 | + echo "==============================================" |
| 114 | + |
| 115 | + test_valid_environment_variables |
| 116 | + test_invalid_environment_variable |
| 117 | + test_domain_file_fallback |
| 118 | + test_invalid_domain |
| 119 | + test_empty_domain |
| 120 | + test_missing_domain_file |
| 121 | + test_env_precedence_over_domain |
| 122 | + |
| 123 | + print_test_summary |
| 124 | +} |
| 125 | + |
| 126 | +# Run tests if script is executed directly |
| 127 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 128 | + run_all_tests |
| 129 | +fi |
0 commit comments