Skip to content

Commit 4c6e688

Browse files
committed
add unit tests for bash scripts
1 parent 67bc9d8 commit 4c6e688

5 files changed

Lines changed: 349 additions & 4 deletions

File tree

kubernetes/linux/main.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ startTime=$(date +%s)
55

66
echo "startup script start @ $(date +'%Y-%m-%dT%H:%M:%S')"
77

8-
getClusterCloudEnvironment() {
9-
# Supported cloud environments
10-
SUPPORTED_CLOUDS=("azurepubliccloud" "azurechinacloud" "azureusgovernmentcloud" "usnat" "ussec" "bleu")
8+
# Supported cloud environments
9+
SUPPORTED_CLOUDS=("azurepubliccloud" "azurechinacloud" "azureusgovernmentcloud" "usnat" "ussec" "bleu")
1110

11+
getClusterCloudEnvironment() {
1212
# Use provided cloud environment variable if it's set and valid
1313
if [ -n "$CLUSTER_CLOUD_ENVIRONMENT" ]; then
1414
for cloud in "${SUPPORTED_CLOUDS[@]}"; do
@@ -27,7 +27,7 @@ getClusterCloudEnvironment() {
2727
fi
2828

2929
# Map domain to cloud environment
30-
case "$domain" in
30+
case "$domain" in
3131
"opinsights.azure.com")
3232
echo "azurepubliccloud"
3333
;;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

test/unit-tests/test_framework.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
3+
# Test framework utilities for shell script unit testing
4+
5+
# Initialize test counters
6+
TESTS_RUN=0
7+
TESTS_PASSED=0
8+
TESTS_FAILED=0
9+
10+
# Colors for output
11+
GREEN='\033[0;32m'
12+
RED='\033[0;31m'
13+
NC='\033[0m' # No Color
14+
15+
# Test environment setup
16+
setup_test_env() {
17+
# Create temporary directory for test artifacts
18+
TEST_DIR=$(mktemp -d)
19+
export TEST_DIR
20+
}
21+
22+
# Test environment cleanup
23+
cleanup_test_env() {
24+
# Remove temporary test directory
25+
if [ -d "$TEST_DIR" ]; then
26+
rm -rf "$TEST_DIR"
27+
fi
28+
}
29+
30+
# Mock file creation helper
31+
mock_file() {
32+
local file_path="$1"
33+
local content="$2"
34+
mkdir -p "$(dirname "$file_path")"
35+
echo "$content" > "$file_path"
36+
}
37+
38+
# Assertion helpers
39+
assert_equals() {
40+
local expected="$1"
41+
local actual="$2"
42+
local message="${3:-}"
43+
44+
TESTS_RUN=$((TESTS_RUN + 1))
45+
46+
if [ "$expected" = "$actual" ]; then
47+
echo -e "${GREEN}✓ Test passed${NC}: Expected '$expected', got '$actual' $message"
48+
TESTS_PASSED=$((TESTS_PASSED + 1))
49+
return 0
50+
else
51+
echo -e "${RED}✗ Test failed${NC}: Expected '$expected', but got '$actual' $message"
52+
TESTS_FAILED=$((TESTS_FAILED + 1))
53+
return 1
54+
fi
55+
}
56+
57+
# Print test summary
58+
print_test_summary() {
59+
echo ""
60+
echo "Test Summary:"
61+
echo "============"
62+
echo "Total tests: $TESTS_RUN"
63+
echo -e "${GREEN}Tests passed: $TESTS_PASSED${NC}"
64+
echo -e "${RED}Tests failed: $TESTS_FAILED${NC}"
65+
66+
if [ $TESTS_FAILED -eq 0 ]; then
67+
echo -e "\n${GREEN}All tests passed!${NC}"
68+
return 0
69+
else
70+
echo -e "\n${RED}Some tests failed.${NC}"
71+
return 1
72+
fi
73+
}
74+
75+
# Run before each test
76+
setup() {
77+
# Create a fresh test environment
78+
setup_test_env
79+
80+
# Create mock directories that tests might need
81+
mkdir -p "$TEST_DIR/etc/ama-logs-secret"
82+
}
83+
84+
# Run after each test
85+
teardown() {
86+
# Clean up test artifacts
87+
cleanup_test_env
88+
89+
# Clear any environment variables set during tests
90+
unset CLUSTER_CLOUD_ENVIRONMENT
91+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
# Extracted function for testing getClusterCloudEnvironment
4+
# Original source: kubernetes/linux/main.sh
5+
6+
# Required variables
7+
SUPPORTED_CLOUDS=("azurepubliccloud" "azurechinacloud" "azureusgovernmentcloud" "usnat" "ussec" "bleu")
8+
9+
getClusterCloudEnvironment() {
10+
# Use provided cloud environment variable if it's set and valid
11+
if [ -n "$CLUSTER_CLOUD_ENVIRONMENT" ]; then
12+
for cloud in "${SUPPORTED_CLOUDS[@]}"; do
13+
if [ "$CLUSTER_CLOUD_ENVIRONMENT" == "$cloud" ]; then
14+
echo "$CLUSTER_CLOUD_ENVIRONMENT"
15+
return
16+
fi
17+
done
18+
# If environment variable is set but not valid, return unknown without domain prefix
19+
echo "unknown"
20+
return
21+
fi
22+
23+
# Fallback to reading from the AMA logs secret if not set or not supported
24+
# Default domain
25+
domain="opinsights.azure.com"
26+
if [ -e "$TEST_DIR/etc/ama-logs-secret/DOMAIN" ]; then
27+
domain=$(cat "$TEST_DIR/etc/ama-logs-secret/DOMAIN")
28+
fi
29+
30+
# Map domain to cloud environment
31+
case "$domain" in
32+
"opinsights.azure.com")
33+
echo "azurepubliccloud"
34+
;;
35+
"opinsights.azure.cn")
36+
echo "azurechinacloud"
37+
;;
38+
"opinsights.azure.us")
39+
echo "azureusgovernmentcloud"
40+
;;
41+
"opinsights.azure.eaglex.ic.gov")
42+
echo "usnat"
43+
;;
44+
"opinsights.azure.microsoft.scloud")
45+
echo "ussec"
46+
;;
47+
"opinsights.sovcloud-api.fr")
48+
echo "bleu"
49+
;;
50+
""|*)
51+
echo "unknown"
52+
;;
53+
esac
54+
}

test/unit-tests/test_main.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
# Main test runner for all unit tests
4+
5+
# Colors for output
6+
BLUE='\033[0;34m'
7+
GREEN='\033[0;32m'
8+
RED='\033[0;31m'
9+
NC='\033[0m' # No Color
10+
11+
# Directory where test cases are located
12+
TEST_CASES_DIR="$(dirname "$0")/test_cases"
13+
14+
# Variables to track overall test results
15+
TOTAL_TESTS_RUN=0
16+
TOTAL_TESTS_PASSED=0
17+
TOTAL_TESTS_FAILED=0
18+
19+
# Find and run all test files
20+
run_all_test_files() {
21+
echo -e "${BLUE}Running all unit tests...${NC}"
22+
echo "========================="
23+
24+
# Track if any test suite failed
25+
local any_suite_failed=0
26+
27+
# Find all test files in the test_cases directory
28+
for test_file in "$TEST_CASES_DIR"/test_*.sh; do
29+
if [ -f "$test_file" ]; then
30+
echo -e "\n${BLUE}Running tests from: $(basename "$test_file")${NC}"
31+
echo "----------------------------------------"
32+
33+
# Make the test file executable
34+
chmod +x "$test_file"
35+
36+
# Run the test file
37+
if "$test_file"; then
38+
echo -e "${BLUE}Completed tests from: $(basename "$test_file")${NC}"
39+
else
40+
any_suite_failed=1
41+
echo -e "${RED}Tests failed in: $(basename "$test_file")${NC}"
42+
fi
43+
fi
44+
done
45+
46+
echo -e "\n${BLUE}All test suites completed.${NC}"
47+
return $any_suite_failed
48+
}
49+
50+
# Main execution
51+
main() {
52+
# Ensure the test cases directory exists
53+
if [ ! -d "$TEST_CASES_DIR" ]; then
54+
echo "Error: Test cases directory not found: $TEST_CASES_DIR"
55+
exit 1
56+
fi
57+
58+
# Run all test files
59+
if run_all_test_files; then
60+
echo -e "\n${GREEN}All test suites passed!${NC}"
61+
exit 0
62+
else
63+
echo -e "\n${RED}Some test suites failed.${NC}"
64+
exit 1
65+
fi
66+
}
67+
68+
# Run main if the script is being executed directly
69+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
70+
main
71+
fi

0 commit comments

Comments
 (0)