Skip to content

Testing — Log Analyzer

Verify each section of the report against known data.

Basic Run

cd ~/projects/log-analyzer
bash log_analyzer.sh sample_access.log

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

# Manually count 200s
grep -c '" 200 ' sample_access.log
# Must match the 200 row in the report

Test --top Flag

bash log_analyzer.sh --top 3 sample_access.log | grep -A5 "Top 3 IPs"

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

touch /tmp/empty.log
bash log_analyzer.sh /tmp/empty.log

Should not crash. Total requests should be 0.

Test with Only Errors

grep '" 500 ' sample_access.log > /tmp/errors_only.log
bash log_analyzer.sh /tmp/errors_only.log

The error section should list all lines. Status codes should show only 500.

Test Error Handling

# Missing file
bash log_analyzer.sh /no/such/file.log
echo "Exit: $?"
Error: '/no/such/file.log' not found Exit: 1
# No argument
bash log_analyzer.sh
Error: LOGFILE required Usage: ... Exit: 1

Verify Byte Count

# Sum bytes column manually
awk '{sum += $10} END {print sum}' sample_access.log

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

implementation | enhancements