|
| 1 | +# Unit Tests for Azure Monitor Container Insights |
| 2 | + |
| 3 | +This directory contains unit tests for various components of the Azure Monitor Container Insights agent. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +``` |
| 8 | +test/unit-tests/ |
| 9 | +├── test_framework.sh # Common test utilities and helper functions (Bash) |
| 10 | +├── test_framework.ps1 # Common test utilities and helper functions (PowerShell) |
| 11 | +├── test_main.sh # Main test runner for Bash tests |
| 12 | +├── test_main.ps1 # Main test runner for PowerShell tests |
| 13 | +├── test_functions/ # Isolated functions for testing |
| 14 | +│ ├── getClusterCloudEnvironment.sh # Linux/Bash version |
| 15 | +│ ├── Get-ClusterCloudEnvironment.ps1 # Windows/PowerShell version |
| 16 | +│ ├── Get-McsEndpoint.ps1 |
| 17 | +│ ├── Get-McsGlobalEndpoint.ps1 |
| 18 | +│ ├── Is-SupportedCloudEnvironment.ps1 |
| 19 | +│ └── Get-LogAnalyticsWorkspaceDomain.ps1 |
| 20 | +└── test_cases/ # Individual test case files |
| 21 | + ├── test_getClusterCloudEnvironment.sh |
| 22 | + ├── Test-GetClusterCloudEnvironment.ps1 |
| 23 | + ├── Test-GetMcsEndpoint.ps1 |
| 24 | + ├── Test-GetMcsGlobalEndpoint.ps1 |
| 25 | + ├── Test-IsSupportedCloudEnvironment.ps1 |
| 26 | + └── Test-GetLogAnalyticsWorkspaceDomain.ps1 |
| 27 | +``` |
| 28 | + |
| 29 | +## Running the Tests |
| 30 | + |
| 31 | +### Linux/Bash Tests |
| 32 | +To run all Bash unit tests: |
| 33 | +```bash |
| 34 | +chmod +x test/unit-tests/test_main.sh |
| 35 | +./test/unit-tests/test_main.sh |
| 36 | +``` |
| 37 | + |
| 38 | +To run a specific Bash test file: |
| 39 | +```bash |
| 40 | +chmod +x test/unit-tests/test_cases/test_getClusterCloudEnvironment.sh |
| 41 | +./test/unit-tests/test_cases/test_getClusterCloudEnvironment.sh |
| 42 | +``` |
| 43 | + |
| 44 | +### Windows/PowerShell Tests |
| 45 | +To run all PowerShell unit tests: |
| 46 | +```powershell |
| 47 | +./test/unit-tests/test_main.ps1 |
| 48 | +``` |
| 49 | + |
| 50 | +To run a specific PowerShell test file: |
| 51 | +```powershell |
| 52 | +./test/unit-tests/test_cases/Test-GetClusterCloudEnvironment.ps1 |
| 53 | +``` |
| 54 | + |
| 55 | +## Available Tests |
| 56 | + |
| 57 | +### Cloud Environment Detection (Linux & Windows) |
| 58 | +Tests the cloud environment detection logic which determines the Azure cloud environment from either: |
| 59 | +- Environment variable (CLUSTER_CLOUD_ENVIRONMENT) |
| 60 | +- Domain file (/etc/ama-logs-secret/DOMAIN) |
| 61 | + |
| 62 | +#### Functionality Tested |
| 63 | +- Valid environment variable values |
| 64 | +- Invalid environment variable values |
| 65 | +- Domain file fallback with valid values |
| 66 | +- Invalid domain values |
| 67 | +- Empty domain file |
| 68 | +- Missing domain file |
| 69 | +- Environment variable precedence over domain file |
| 70 | + |
| 71 | +### MCS Endpoint Tests |
| 72 | +Tests the MCS endpoint determination based on cloud environment: |
| 73 | +- Public cloud endpoints |
| 74 | +- China cloud endpoints |
| 75 | +- US Government cloud endpoints |
| 76 | +- Other sovereign cloud endpoints |
| 77 | +- Defaults and fallbacks |
| 78 | + |
| 79 | +### MCS Global Endpoint Tests |
| 80 | +Tests global endpoint determination including: |
| 81 | +- Canary region detection |
| 82 | +- Cloud environment-specific endpoints |
| 83 | +- Default endpoints |
| 84 | +- Environment variable handling |
| 85 | + |
| 86 | +### Log Analytics Workspace Domain Tests |
| 87 | +Tests workspace domain determination: |
| 88 | +- Valid domain mappings |
| 89 | +- Invalid domains |
| 90 | +- Default domain handling |
| 91 | +- Empty/missing configurations |
| 92 | + |
| 93 | +## Adding New Tests |
| 94 | + |
| 95 | +### For Linux/Bash Tests |
| 96 | +1. Create a new function file in `test_functions/` containing only the function to be tested |
| 97 | +2. Create a new test file in `test_cases/` (prefix with `test_`) |
| 98 | +3. Import the test framework and function file in your test file |
| 99 | +4. Add your test cases following the existing patterns |
| 100 | +5. Make the files executable: |
| 101 | +```bash |
| 102 | +chmod +x test/unit-tests/test_functions/your_function.sh |
| 103 | +chmod +x test/unit-tests/test_cases/test_your_function.sh |
| 104 | +``` |
| 105 | + |
| 106 | +### For Windows/PowerShell Tests |
| 107 | +1. Create a new function file in `test_functions/` (use PascalCase naming) |
| 108 | +2. Create a new test file in `test_cases/` (prefix with `Test-`) |
| 109 | +3. Import the test framework and function file in your test file |
| 110 | +4. Add your test cases following the existing patterns |
| 111 | + |
| 112 | +## Test Framework Features |
| 113 | + |
| 114 | +Common features for both Bash and PowerShell frameworks: |
| 115 | +- Setup/teardown for each test |
| 116 | +- Mock file creation support |
| 117 | +- Assertion helpers |
| 118 | +- Test result reporting |
| 119 | +- Temporary test directory management |
| 120 | + |
| 121 | +## Test Output |
| 122 | + |
| 123 | +Tests will produce output in the following format: |
| 124 | +``` |
| 125 | +Running tests for [TestName]... |
| 126 | +============================================== |
| 127 | +✓ Test passed: Expected 'value', got 'value' (test description) |
| 128 | +✗ Test failed: Expected 'value', but got 'value' (test description) |
| 129 | +
|
| 130 | +Test Summary: |
| 131 | +============ |
| 132 | +Total tests: X |
| 133 | +Tests passed: Y |
| 134 | +Tests failed: Z |
| 135 | +``` |
| 136 | + |
| 137 | +## Debugging Failed Tests |
| 138 | + |
| 139 | +When tests fail, you can: |
| 140 | +1. Run individual test files for more focused debugging |
| 141 | +2. Check the test temporary directory during test execution |
| 142 | +3. Add debug echo statements in the test functions |
| 143 | +4. Review the assertions in the failing test cases |
| 144 | + |
| 145 | +## Contributing |
| 146 | + |
| 147 | +When adding new test cases: |
| 148 | +1. Follow the existing patterns in test_cases/ |
| 149 | +2. Isolate function dependencies in test_functions/ |
| 150 | +3. Add proper test descriptions in assertions |
| 151 | +4. Update this README.md with details of new test cases |
| 152 | +5. Follow the appropriate naming conventions for your platform |
| 153 | + - Linux/Bash: lowercase with underscores |
| 154 | + - Windows/PowerShell: PascalCase |
0 commit comments