Skip to content

Commit 7ebfcbc

Browse files
committed
add unit tests
1 parent 4c6e688 commit 7ebfcbc

13 files changed

Lines changed: 1040 additions & 0 deletions

test/unit-tests/README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Import test framework
2+
. (Join-Path $PSScriptRoot ".." "test_framework.ps1")
3+
4+
# Import function to test
5+
. (Join-Path $PSScriptRoot ".." "test_functions" "Get-ClusterCloudEnvironment.ps1")
6+
7+
function Test-ValidEnvironmentVariables {
8+
$testCases = @(
9+
"azurepubliccloud",
10+
"azurechinacloud",
11+
"azureusgovernmentcloud",
12+
"usnat",
13+
"ussec",
14+
"bleu"
15+
)
16+
17+
foreach ($cloud in $testCases) {
18+
Setup
19+
$env:CLUSTER_CLOUD_ENVIRONMENT = $cloud
20+
$result = Get-ClusterCloudEnvironment
21+
Assert-Equals $cloud $result "(with CLUSTER_CLOUD_ENVIRONMENT=$cloud)"
22+
Teardown
23+
}
24+
}
25+
26+
function Test-InvalidEnvironmentVariable {
27+
Setup
28+
$env:CLUSTER_CLOUD_ENVIRONMENT = "invalidcloud"
29+
$result = Get-ClusterCloudEnvironment
30+
Assert-Equals "unknown" $result "(with invalid CLUSTER_CLOUD_ENVIRONMENT)"
31+
Teardown
32+
}
33+
34+
function Test-DomainFileFallback {
35+
$testCases = @(
36+
@{domain="opinsights.azure.com"; expected="azurepubliccloud"},
37+
@{domain="opinsights.azure.cn"; expected="azurechinacloud"},
38+
@{domain="opinsights.azure.us"; expected="azureusgovernmentcloud"},
39+
@{domain="opinsights.azure.eaglex.ic.gov"; expected="usnat"},
40+
@{domain="opinsights.azure.microsoft.scloud"; expected="ussec"},
41+
@{domain="opinsights.sovcloud-api.fr"; expected="bleu"}
42+
)
43+
44+
foreach ($testCase in $testCases) {
45+
Setup
46+
Remove-Item Env:\CLUSTER_CLOUD_ENVIRONMENT -ErrorAction SilentlyContinue
47+
Mock-File "etc/ama-logs-secret/DOMAIN" $testCase.domain
48+
$result = Get-ClusterCloudEnvironment
49+
Assert-Equals $testCase.expected $result "(with domain=$($testCase.domain))"
50+
Teardown
51+
}
52+
}
53+
54+
function Test-InvalidDomain {
55+
Setup
56+
Remove-Item Env:\CLUSTER_CLOUD_ENVIRONMENT -ErrorAction SilentlyContinue
57+
Mock-File "etc/ama-logs-secret/DOMAIN" "invalid.domain.com"
58+
$result = Get-ClusterCloudEnvironment
59+
Assert-Equals "unknown" $result "(with invalid domain)"
60+
Teardown
61+
}
62+
63+
function Test-EmptyDomain {
64+
Setup
65+
Remove-Item Env:\CLUSTER_CLOUD_ENVIRONMENT -ErrorAction SilentlyContinue
66+
Mock-File "etc/ama-logs-secret/DOMAIN" ""
67+
$result = Get-ClusterCloudEnvironment
68+
Assert-Equals "unknown" $result "(with empty domain)"
69+
Teardown
70+
}
71+
72+
function Test-MissingDomainFile {
73+
Setup
74+
Remove-Item Env:\CLUSTER_CLOUD_ENVIRONMENT -ErrorAction SilentlyContinue
75+
# Don't create the domain file
76+
$result = Get-ClusterCloudEnvironment
77+
Assert-Equals "azurepubliccloud" $result "(with missing domain file - should use default)"
78+
Teardown
79+
}
80+
81+
function Test-EnvPrecedenceOverDomain {
82+
Setup
83+
$env:CLUSTER_CLOUD_ENVIRONMENT = "azurepubliccloud"
84+
Mock-File "etc/ama-logs-secret/DOMAIN" "opinsights.azure.cn"
85+
$result = Get-ClusterCloudEnvironment
86+
Assert-Equals "azurepubliccloud" $result "(env should take precedence over domain)"
87+
Teardown
88+
}
89+
90+
# Run all tests
91+
function Run-AllTests {
92+
Write-Host "Running tests for Get-ClusterCloudEnvironment..."
93+
Write-Host "=============================================="
94+
95+
Test-ValidEnvironmentVariables
96+
Test-InvalidEnvironmentVariable
97+
Test-DomainFileFallback
98+
Test-InvalidDomain
99+
Test-EmptyDomain
100+
Test-MissingDomainFile
101+
Test-EnvPrecedenceOverDomain
102+
103+
Print-TestSummary
104+
}
105+
106+
# Run tests if script is executed directly
107+
if ($MyInvocation.InvocationName -ne '.') {
108+
Run-AllTests
109+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Import test framework
2+
. (Join-Path $PSScriptRoot ".." "test_framework.ps1")
3+
4+
# Import function to test
5+
. (Join-Path $PSScriptRoot ".." "test_functions" "Get-LogAnalyticsWorkspaceDomain.ps1")
6+
7+
function Test-ValidDomains {
8+
$testCases = @(
9+
@{
10+
domain = "opinsights.azure.com"
11+
expected = "opinsights.azure.com"
12+
},
13+
@{
14+
domain = "opinsights.azure.cn"
15+
expected = "opinsights.azure.cn"
16+
},
17+
@{
18+
domain = "opinsights.azure.us"
19+
expected = "opinsights.azure.us"
20+
},
21+
@{
22+
domain = "opinsights.azure.eaglex.ic.gov"
23+
expected = "opinsights.azure.eaglex.ic.gov"
24+
},
25+
@{
26+
domain = "opinsights.azure.microsoft.scloud"
27+
expected = "opinsights.azure.microsoft.scloud"
28+
},
29+
@{
30+
domain = "opinsights.sovcloud-api.fr"
31+
expected = "opinsights.sovcloud-api.fr"
32+
}
33+
)
34+
35+
foreach ($testCase in $testCases) {
36+
Setup
37+
Mock-File "etc/ama-logs-secret/DOMAIN" $testCase.domain
38+
$result = Get-LogAnalyticsWorkspaceDomain
39+
Assert-Equals $testCase.expected $result "(for domain $($testCase.domain))"
40+
Teardown
41+
}
42+
}
43+
44+
function Test-UnknownDomain {
45+
$testCases = @(
46+
"unknown.domain.com",
47+
"azure.com",
48+
"opinsights.invalid.com",
49+
"something.azure.cn",
50+
"opinsights.azure.invalid"
51+
)
52+
53+
foreach ($domain in $testCases) {
54+
Setup
55+
Mock-File "etc/ama-logs-secret/DOMAIN" $domain
56+
$result = Get-LogAnalyticsWorkspaceDomain
57+
Assert-Equals "opinsights.azure.com" $result "(unknown domain $domain should default to opinsights.azure.com)"
58+
Teardown
59+
}
60+
}
61+
62+
function Test-EmptyDomain {
63+
Setup
64+
Mock-File "etc/ama-logs-secret/DOMAIN" ""
65+
$result = Get-LogAnalyticsWorkspaceDomain
66+
Assert-Equals "opinsights.azure.com" $result "(empty domain should default to opinsights.azure.com)"
67+
Teardown
68+
}
69+
70+
function Test-MissingDomainFile {
71+
Setup
72+
# Don't create the domain file
73+
$result = Get-LogAnalyticsWorkspaceDomain
74+
Assert-Equals "opinsights.azure.com" $result "(missing domain file should default to opinsights.azure.com)"
75+
Teardown
76+
}
77+
78+
function Test-CaseSensitivity {
79+
$testCases = @(
80+
"OPINSIGHTS.AZURE.COM",
81+
"Opinsights.Azure.Com",
82+
"opinsights.AZURE.cn",
83+
"OPINSIGHTS.azure.us"
84+
)
85+
86+
foreach ($domain in $testCases) {
87+
Setup
88+
Mock-File "etc/ama-logs-secret/DOMAIN" $domain
89+
$result = Get-LogAnalyticsWorkspaceDomain
90+
Assert-Equals "opinsights.azure.com" $result "(case-sensitive check for $domain)"
91+
Teardown
92+
}
93+
}
94+
95+
# Run all tests
96+
function Run-AllTests {
97+
Write-Host "Running tests for Get-LogAnalyticsWorkspaceDomain..."
98+
Write-Host "================================================"
99+
100+
Test-ValidDomains
101+
Test-UnknownDomain
102+
Test-EmptyDomain
103+
Test-MissingDomainFile
104+
Test-CaseSensitivity
105+
106+
Print-TestSummary
107+
}
108+
109+
# Run tests if script is executed directly
110+
if ($MyInvocation.InvocationName -ne '.') {
111+
Run-AllTests
112+
}

0 commit comments

Comments
 (0)