Skip to content

Commit 4d0028a

Browse files
committed
Add testing section to README and update test scripts for improved structure
- Introduced a new "Testing" section in the README.md to provide quick start instructions for running tests, including commands for unit, integration, and end-to-end tests. - Updated test scripts to reflect changes in directory structure and improve clarity, including adjustments to alert handling and mock functions. - Enhanced test cases to ensure better coverage and reliability, particularly in monitoring and data integrity checks.
1 parent 88ce2de commit 4d0028a

File tree

8 files changed

+422
-81
lines changed

8 files changed

+422
-81
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,24 @@ bash scripts/generate_coverage_combined.sh
647647

648648
For detailed explanation, see: [Code Coverage Explanation](./docs/Coverage_Explanation.md)
649649

650+
## Testing
651+
652+
### Quick Start
653+
654+
```bash
655+
# Run all tests (master test runner)
656+
./tests/run_all_tests.sh
657+
658+
# Run specific test suites
659+
./tests/run_all_tests.sh --unit-only # Unit tests only
660+
./tests/run_all_tests.sh --integration-only # Integration tests only
661+
./tests/run_all_tests.sh --e2e-only # End-to-end tests only
662+
```
663+
664+
**Master Test Runner**: `tests/run_all_tests.sh` - Executes all test suites (unit, integration, e2e, SQL, scripts, performance)
665+
666+
For detailed testing information, see the test documentation in the `tests/` directory.
667+
650668
## Configuration
651669

652670
### Quick Setup

tests/unit/monitor/test_monitorAPI.sh

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,37 @@ teardown() {
178178
# Test: check_api_availability alerts when API is unavailable
179179
##
180180
@test "check_api_availability alerts when API is unavailable" {
181-
# Mock curl to return failure
182-
# shellcheck disable=SC2317
183-
curl() {
184-
if [[ "${1}" == "-w" ]]; then
185-
echo "503" # Service unavailable
186-
return 1
187-
fi
188-
return 1
189-
}
190-
export -f curl
181+
# Create mock curl executable so command -v finds it
182+
local mock_curl_dir="${BATS_TEST_DIRNAME}/../../tmp/bin"
183+
mkdir -p "${mock_curl_dir}"
184+
local mock_curl="${mock_curl_dir}/curl"
185+
cat > "${mock_curl}" << 'EOF'
186+
#!/bin/bash
187+
# Return HTTP 503 when called with -w "%{http_code}"
188+
while [[ $# -gt 0 ]]; do
189+
case "$1" in
190+
-w)
191+
shift
192+
if [[ "$1" == "%{http_code}" ]]; then
193+
echo "503"
194+
exit 0
195+
elif [[ "$1" == "%{time_total}" ]]; then
196+
echo "0.5"
197+
exit 0
198+
fi
199+
;;
200+
-s|-o|--max-time|--connect-timeout)
201+
shift
202+
;;
203+
*)
204+
;;
205+
esac
206+
shift
207+
done
208+
exit 0
209+
EOF
210+
chmod +x "${mock_curl}"
211+
export PATH="${mock_curl_dir}:${PATH}"
191212

192213
# Mock log functions
193214
# shellcheck disable=SC2317
@@ -214,9 +235,11 @@ teardown() {
214235

215236
# shellcheck disable=SC2317
216237
send_alert() {
217-
# Check if it's an API unavailable alert (4th arg is message)
238+
# Check if it's an API unavailable alert (arguments: component, level, type, message)
239+
local alert_type="${3:-}"
218240
local message="${4:-}"
219-
if echo "${message}" | grep -q "API.*unavailable\|API.*returned"; then
241+
# The alert type should be "api_unavailable" or message should contain "API" and "returned" or "unavailable"
242+
if [[ "${alert_type}" == "api_unavailable" ]] || echo "${message}" | grep -qiE "(API.*returned|API.*unavailable|unavailable)"; then
220243
touch "${alert_file}"
221244
fi
222245
return 0
@@ -228,18 +251,30 @@ teardown() {
228251

229252
# Alert should have been sent
230253
assert_file_exists "${alert_file}"
254+
255+
# Cleanup
256+
rm -rf "${mock_curl_dir}"
231257
}
232258

233259
##
234260
# Test: check_api_availability handles curl unavailability gracefully
235261
##
236262
@test "check_api_availability handles curl unavailability gracefully" {
237-
# Unset curl command
238-
# shellcheck disable=SC2317
239-
curl() {
240-
return 127 # Command not found
241-
}
242-
export -f curl
263+
# Remove curl from PATH temporarily to simulate it not being available
264+
local original_path="${PATH}"
265+
# Create a temporary PATH without curl
266+
local temp_path=""
267+
IFS=':' read -ra path_parts <<< "${PATH}"
268+
for part in "${path_parts[@]}"; do
269+
if [[ "${part}" != *"tmp/bin"* ]] && [[ "${part}" != *"/usr/bin"* ]] && [[ "${part}" != *"/bin"* ]]; then
270+
if [[ -z "${temp_path}" ]]; then
271+
temp_path="${part}"
272+
else
273+
temp_path="${temp_path}:${part}"
274+
fi
275+
fi
276+
done
277+
export PATH="${temp_path}"
243278

244279
# Mock log functions
245280
# shellcheck disable=SC2317
@@ -269,6 +304,9 @@ teardown() {
269304
# Run check
270305
run check_api_availability
271306

307+
# Restore PATH
308+
export PATH="${original_path}"
309+
272310
# Should succeed (skip gracefully)
273311
assert_success
274312
}

tests/unit/monitor/test_monitorAnalytics.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ setup() {
1919
export LOG_LEVEL="${LOG_LEVEL_DEBUG}"
2020

2121
# Create test directories
22-
mkdir -p "${TEST_ANALYTICS_DIR}/bin"
22+
mkdir -p "${TEST_ANALYTICS_DIR}/bin/dwh"
2323
mkdir -p "${TEST_ANALYTICS_DIR}/logs"
2424
mkdir -p "${TEST_LOG_DIR}"
2525

@@ -137,7 +137,8 @@ create_test_etl_script() {
137137
local script_name="${1}"
138138
local executable="${2:-true}"
139139

140-
local script_path="${TEST_ANALYTICS_DIR}/bin/${script_name}"
140+
# Function looks for scripts in bin/dwh directory
141+
local script_path="${TEST_ANALYTICS_DIR}/bin/dwh/${script_name}"
141142
echo "#!/bin/bash" > "${script_path}"
142143
echo "# Test ETL script ${script_name}" >> "${script_path}"
143144

@@ -226,11 +227,11 @@ create_test_log() {
226227

227228
@test "check_etl_job_execution_status alerts when scripts not executable" {
228229
# Create scripts but make one non-executable
229-
# The function looks for specific script names: etl_main.sh, etl_daily.sh, etl_hourly.sh, load_data.sh, transform_data.sh
230+
# The function looks for specific script names: ETL.sh, cleanupDWH.sh, copyBaseTables.sh
230231
# Need at least threshold number of scripts (threshold is 2)
231-
create_test_etl_script "etl_main.sh" "true"
232-
create_test_etl_script "etl_daily.sh" "false" # This one is not executable
233-
create_test_etl_script "etl_hourly.sh" "true"
232+
create_test_etl_script "ETL.sh" "true"
233+
create_test_etl_script "cleanupDWH.sh" "false" # This one is not executable
234+
create_test_etl_script "copyBaseTables.sh" "true"
234235

235236
# Use a temp file to track alert status
236237
local alert_file="${TEST_ANALYTICS_DIR}/.alert_sent"
@@ -246,7 +247,8 @@ create_test_log() {
246247
send_alert() {
247248
# Check if message contains executable count warning
248249
# The actual message is: "ETL scripts executable count (X) is less than scripts found (Y)"
249-
if [[ "${4}" == *"executable"* ]] || [[ "${4}" == *"ETL scripts executable count"* ]] || [[ "${4}" == *"less than"* ]] || [[ "${4}" == *"not executable"* ]]; then
250+
local message="${4:-}"
251+
if echo "${message}" | grep -qiE "(executable.*count|less than|not executable|scripts.*executable)"; then
250252
touch "${alert_file}"
251253
fi
252254
return 0

tests/unit/monitor/test_monitorData.sh

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ create_test_backup() {
438438

439439
# shellcheck disable=SC2317
440440
send_alert() {
441-
if [[ "${4}" == *"File integrity check found"* ]]; then
441+
# Check if it's a file integrity alert
442+
local alert_type="${3:-}"
443+
local message="${4:-}"
444+
if [[ "${alert_type}" == "file_integrity_failure" ]] || echo "${message}" | grep -qiE "(file integrity|integrity.*failure|corrupted)"; then
442445
touch "${alert_file}"
443446
fi
444447
return 0
@@ -454,7 +457,29 @@ create_test_backup() {
454457

455458
@test "check_file_integrity alerts when compressed files are corrupted" {
456459
# Create corrupted compressed file (invalid gzip)
460+
# Make it old enough (>60 seconds) so it's not skipped
457461
echo "not a valid gzip file" > "${TEST_BACKUP_DIR}/backup.tar.gz"
462+
touch -t "$(date -d '2 hours ago' +%Y%m%d%H%M.%S)" "${TEST_BACKUP_DIR}/backup.tar.gz" 2>/dev/null || \
463+
touch -t "$(date -v-2H +%Y%m%d%H%M.%S 2>/dev/null || date +%Y%m%d%H%M.%S)" "${TEST_BACKUP_DIR}/backup.tar.gz"
464+
465+
# Mock log functions
466+
# shellcheck disable=SC2317
467+
log_info() {
468+
return 0
469+
}
470+
export -f log_info
471+
472+
# shellcheck disable=SC2317
473+
log_warning() {
474+
return 0
475+
}
476+
export -f log_warning
477+
478+
# shellcheck disable=SC2317
479+
log_debug() {
480+
return 0
481+
}
482+
export -f log_debug
458483

459484
# Mock record_metric
460485
# shellcheck disable=SC2317
@@ -469,7 +494,13 @@ create_test_backup() {
469494

470495
# shellcheck disable=SC2317
471496
send_alert() {
472-
if [[ "${4}" == *"File integrity check found"* ]]; then
497+
# Check alert type or message content - be more flexible
498+
local alert_type="${3:-}"
499+
local message="${4:-}"
500+
if [[ "${alert_type}" == "file_integrity_failure" ]] || \
501+
[[ "${message}" == *"File integrity check found"* ]] || \
502+
[[ "${message}" == *"integrity"* ]] || \
503+
[[ "${message}" == *"corrupted"* ]]; then
473504
touch "${alert_file}"
474505
fi
475506
return 0

0 commit comments

Comments
 (0)