Testing — Log Analyzer¶
Verify each section of the report against known data.
Basic Run¶
Expected structure:
=== Log Analysis Report ===
File: sample_access.log
Lines: 2,000
Analyzed: 2024-01-15 09:00:00
── Traffic ──────────────────────────────
Total requests: 2,000
Unique IPs: 10
Total bytes: 47.2 MB
── Status Codes ─────────────────────────
200 1,667 (83.4%)
404 200 (10.0%)
...
── Top 10 IPs ───────────────────────────
192.168.1.42 204 requests
...
Verify Status Code Counts Manually¶
Test --top Flag¶
Should show exactly 3 IP entries, not 10.
Test --output Flag¶
bash log_analyzer.sh --output /tmp/report.txt sample_access.log
ls -la /tmp/report.txt
cat /tmp/report.txt | head -5
Report written to: /tmp/report.txt
-rw-r--r-- 1 user user 2048 Jan 15 09:00 /tmp/report.txt
=== Log Analysis Report ===
...
Test with Empty Log¶
Should not crash. Total requests should be 0.
Test with Only Errors¶
The error section should list all lines. Status codes should show only 500.
Test Error Handling¶
Error: '/no/such/file.log' not found
Exit: 1
Error: LOGFILE required
Usage: ...
Exit: 1
Verify Byte Count¶
This raw number should match (within rounding) the "Total bytes" in your report.
Performance Test¶
# Generate a large log and time the analysis
bash gen_sample_log.sh 100000 large_access.log
time bash log_analyzer.sh large_access.log > /dev/null
Should complete in under 10 seconds on any modern machine.
Checklist¶
□ Report header shows correct filename and line count
□ Status code percentages sum to ~100%
□ Top IPs counts match manual grep
□ --top N limits output correctly
□ --output writes file, not stdout
□ Empty log file handled without errors
□ Missing file returns exit code 1 with clear message
□ shellcheck log_analyzer.sh returns 0 warnings