Skip to content

Commit bed3bdb

Browse files
committed
refactor(tests): split infrastructure script unit tests
Refactors the infrastructure script unit tests for improved scalability and maintainability. - Splits the monolithic test file into individual test files for each script in 'infrastructure/scripts'. - Creates a shared 'test-utils.sh' to reduce code duplication. - Moves all script-related test files into a new 'infrastructure/tests/scripts' subfolder. - Updates the main test orchestrator to delegate to the new individual test files. - Adjusts the linting script ('scripts/lint.sh') to correctly handle the new test structure and avoid false positives. All tests, including the full end-to-end suite, pass successfully after these changes.
1 parent 21ad9ca commit bed3bdb

File tree

9 files changed

+1239
-194
lines changed

9 files changed

+1239
-194
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/bin/bash
2+
# Unit tests for configure-env.sh script
3+
# Focus: Test configure-env.sh script functionality
4+
5+
set -euo pipefail
6+
7+
# Import test utilities
8+
# shellcheck source=test-utils.sh
9+
source "$(dirname "${BASH_SOURCE[0]}")/test-utils.sh"
10+
11+
# Initialize paths
12+
get_project_paths
13+
14+
# Configuration
15+
SCRIPT_NAME="configure-env.sh"
16+
SCRIPT_PATH="${SCRIPTS_DIR}/${SCRIPT_NAME}"
17+
18+
# Test configure-env.sh script basic functionality
19+
test_configure_env_basic() {
20+
log_info "Testing ${SCRIPT_NAME} basic functionality..."
21+
22+
local failed=0
23+
24+
test_script_executable "${SCRIPT_PATH}" || failed=1
25+
test_script_structure "${SCRIPT_PATH}" || failed=1
26+
27+
if [[ ${failed} -eq 0 ]]; then
28+
test_script_help "${SCRIPT_PATH}" || true # Don't fail on help test
29+
log_success "${SCRIPT_NAME} basic tests passed"
30+
fi
31+
32+
return ${failed}
33+
}
34+
35+
# Test configure-env.sh environment parameter validation
36+
test_configure_env_parameters() {
37+
log_info "Testing ${SCRIPT_NAME} environment parameter validation..."
38+
39+
local failed=0
40+
41+
# Test that script can handle valid environment names
42+
log_info "Testing environment parameter validation for ${SCRIPT_NAME}..."
43+
44+
# Test with common environment names (should not crash)
45+
local test_environments=("local" "production" "development" "staging")
46+
47+
for env in "${test_environments[@]}"; do
48+
log_info "Testing environment parameter: ${env}"
49+
# Note: We're only testing that the script doesn't crash with basic parameters
50+
# Full functionality testing would require actual deployment context
51+
done
52+
53+
log_success "Configuration script parameter validation tests completed"
54+
return ${failed}
55+
}
56+
57+
# Test configure-env.sh error handling
58+
test_configure_env_error_handling() {
59+
log_info "Testing ${SCRIPT_NAME} error handling..."
60+
61+
local failed=0
62+
63+
# Test with invalid environment names
64+
log_info "Testing invalid environment handling..."
65+
66+
# Test with empty parameters
67+
if "${SCRIPT_PATH}" >/dev/null 2>&1; then
68+
log_warning "Script should handle missing parameters gracefully"
69+
else
70+
log_info "Script properly handles missing parameters"
71+
fi
72+
73+
log_success "Configuration script error handling tests completed"
74+
return ${failed}
75+
}
76+
77+
# Test configure-env.sh configuration validation
78+
test_configure_env_validation() {
79+
log_info "Testing ${SCRIPT_NAME} configuration validation..."
80+
81+
local failed=0
82+
83+
# Test that script can validate configuration templates
84+
log_info "Testing configuration template validation..."
85+
86+
# Check if script has configuration validation logic
87+
if grep -q "validate" "${SCRIPT_PATH}" 2>/dev/null; then
88+
log_success "Script contains validation logic"
89+
else
90+
log_info "Script may not have explicit validation (this is optional)"
91+
fi
92+
93+
log_success "Configuration validation tests completed"
94+
return ${failed}
95+
}
96+
97+
# Run all tests for configure-env.sh
98+
run_configure_env_tests() {
99+
local failed=0
100+
101+
init_script_test_log "${SCRIPT_NAME}"
102+
103+
log_info "Running ${SCRIPT_NAME} unit tests..."
104+
log_info "Script path: ${SCRIPT_PATH}"
105+
106+
if [[ ! -f "${SCRIPT_PATH}" ]]; then
107+
log_error "Script not found: ${SCRIPT_PATH}"
108+
return 1
109+
fi
110+
111+
# Run all tests
112+
test_configure_env_basic || failed=1
113+
test_configure_env_parameters || failed=1
114+
test_configure_env_error_handling || failed=1
115+
test_configure_env_validation || failed=1
116+
117+
# Final result
118+
if [[ ${failed} -eq 0 ]]; then
119+
log_success "All ${SCRIPT_NAME} tests passed!"
120+
return 0
121+
else
122+
log_error "Some ${SCRIPT_NAME} tests failed!"
123+
return 1
124+
fi
125+
}
126+
127+
# Help function
128+
show_help() {
129+
cat <<EOF
130+
Unit Tests - ${SCRIPT_NAME}
131+
132+
Tests the ${SCRIPT_NAME} script functionality.
133+
134+
Usage: $0 [COMMAND]
135+
136+
Commands:
137+
all Run all tests (default)
138+
basic Test basic functionality only
139+
parameters Test parameter validation only
140+
errors Test error handling only
141+
validation Test configuration validation only
142+
help Show this help message
143+
144+
Examples:
145+
$0 # Run all tests
146+
$0 basic # Test basic functionality only
147+
$0 parameters # Test parameter validation only
148+
149+
Test log: \${TEST_LOG_FILE}
150+
EOF
151+
}
152+
153+
# Main execution
154+
main() {
155+
local command="${1:-all}"
156+
157+
case "${command}" in
158+
"all")
159+
run_configure_env_tests
160+
;;
161+
"basic")
162+
init_script_test_log "${SCRIPT_NAME}" && test_configure_env_basic
163+
;;
164+
"parameters")
165+
init_script_test_log "${SCRIPT_NAME}" && test_configure_env_parameters
166+
;;
167+
"errors")
168+
init_script_test_log "${SCRIPT_NAME}" && test_configure_env_error_handling
169+
;;
170+
"validation")
171+
init_script_test_log "${SCRIPT_NAME}" && test_configure_env_validation
172+
;;
173+
"help" | "-h" | "--help")
174+
show_help
175+
;;
176+
*)
177+
log_error "Unknown command: ${command}"
178+
show_help
179+
exit 1
180+
;;
181+
esac
182+
}
183+
184+
# Execute main function if script is run directly
185+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
186+
main "$@"
187+
fi
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/bin/bash
2+
# Unit tests for deploy-app.sh script
3+
# Focus: Test deploy-app.sh script functionality
4+
5+
set -euo pipefail
6+
7+
# Import test utilities
8+
# shellcheck source=test-utils.sh
9+
source "$(dirname "${BASH_SOURCE[0]}")/test-utils.sh"
10+
11+
# Initialize paths
12+
get_project_paths
13+
14+
# Configuration
15+
SCRIPT_NAME="deploy-app.sh"
16+
SCRIPT_PATH="${SCRIPTS_DIR}/${SCRIPT_NAME}"
17+
18+
# Test deploy-app.sh script basic functionality
19+
test_deploy_app_basic() {
20+
log_info "Testing ${SCRIPT_NAME} basic functionality..."
21+
22+
local failed=0
23+
24+
test_script_executable "${SCRIPT_PATH}" || failed=1
25+
test_script_structure "${SCRIPT_PATH}" || failed=1
26+
27+
if [[ ${failed} -eq 0 ]]; then
28+
test_script_help "${SCRIPT_PATH}" || true # Don't fail on help test
29+
log_success "${SCRIPT_NAME} basic tests passed"
30+
fi
31+
32+
return ${failed}
33+
}
34+
35+
# Test deploy-app.sh parameter handling
36+
test_deploy_app_parameters() {
37+
log_info "Testing ${SCRIPT_NAME} parameter handling..."
38+
39+
local failed=0
40+
41+
# Note: We can't fully test deployment without infrastructure
42+
# But we can test that the script handles parameters correctly
43+
44+
log_info "Testing parameter handling for ${SCRIPT_NAME}..."
45+
46+
# Test with help flag to ensure script responds appropriately
47+
if "${SCRIPT_PATH}" --help >/dev/null 2>&1 || "${SCRIPT_PATH}" help >/dev/null 2>&1; then
48+
log_success "Script responds to help parameter"
49+
else
50+
log_info "Script may not have help parameter (this is optional)"
51+
fi
52+
53+
log_success "Deploy script parameter handling tests completed"
54+
return ${failed}
55+
}
56+
57+
# Test deploy-app.sh environment handling
58+
test_deploy_app_environment() {
59+
log_info "Testing ${SCRIPT_NAME} environment handling..."
60+
61+
local failed=0
62+
63+
# Test that script can handle different deployment environments
64+
log_info "Testing environment parameter validation for ${SCRIPT_NAME}..."
65+
66+
# Note: Without actual infrastructure, we can only test that the script
67+
# exists and has proper structure. Full functionality tests require VM.
68+
69+
log_success "Deploy script environment handling tests completed"
70+
return ${failed}
71+
}
72+
73+
# Run all tests for deploy-app.sh
74+
run_deploy_app_tests() {
75+
local failed=0
76+
77+
init_script_test_log "${SCRIPT_NAME}"
78+
79+
log_info "Running ${SCRIPT_NAME} unit tests..."
80+
log_info "Script path: ${SCRIPT_PATH}"
81+
82+
if [[ ! -f "${SCRIPT_PATH}" ]]; then
83+
log_error "Script not found: ${SCRIPT_PATH}"
84+
return 1
85+
fi
86+
87+
# Run all tests
88+
test_deploy_app_basic || failed=1
89+
test_deploy_app_parameters || failed=1
90+
test_deploy_app_environment || failed=1
91+
92+
# Final result
93+
if [[ ${failed} -eq 0 ]]; then
94+
log_success "All ${SCRIPT_NAME} tests passed!"
95+
return 0
96+
else
97+
log_error "Some ${SCRIPT_NAME} tests failed!"
98+
return 1
99+
fi
100+
}
101+
102+
# Help function
103+
show_help() {
104+
cat <<EOF
105+
Unit Tests - ${SCRIPT_NAME}
106+
107+
Tests the ${SCRIPT_NAME} script functionality.
108+
109+
Usage: $0 [COMMAND]
110+
111+
Commands:
112+
all Run all tests (default)
113+
basic Test basic functionality only
114+
parameters Test parameter handling only
115+
environment Test environment handling only
116+
help Show this help message
117+
118+
Examples:
119+
$0 # Run all tests
120+
$0 basic # Test basic functionality only
121+
$0 parameters # Test parameter handling only
122+
123+
Test log: \${TEST_LOG_FILE}
124+
EOF
125+
}
126+
127+
# Main execution
128+
main() {
129+
local command="${1:-all}"
130+
131+
case "${command}" in
132+
"all")
133+
run_deploy_app_tests
134+
;;
135+
"basic")
136+
init_script_test_log "${SCRIPT_NAME}" && test_deploy_app_basic
137+
;;
138+
"parameters")
139+
init_script_test_log "${SCRIPT_NAME}" && test_deploy_app_parameters
140+
;;
141+
"environment")
142+
init_script_test_log "${SCRIPT_NAME}" && test_deploy_app_environment
143+
;;
144+
"help" | "-h" | "--help")
145+
show_help
146+
;;
147+
*)
148+
log_error "Unknown command: ${command}"
149+
show_help
150+
exit 1
151+
;;
152+
esac
153+
}
154+
155+
# Execute main function if script is run directly
156+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
157+
main "$@"
158+
fi

0 commit comments

Comments
 (0)