296 lines
8.8 KiB
Bash
Executable File
296 lines
8.8 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# Comprehensive testing script for camera simulation
|
||
# Tests all camera source backends and configurations
|
||
|
||
set -e
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
||
TEST_DATA_DIR="$PROJECT_DIR/test_data"
|
||
|
||
# Colors for output
|
||
RED='\033[0;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
NC='\033[0m' # No Color
|
||
|
||
# Test results
|
||
TESTS_PASSED=0
|
||
TESTS_FAILED=0
|
||
|
||
# Function to print colored output
|
||
print_status() {
|
||
local status=$1
|
||
local message=$2
|
||
|
||
case $status in
|
||
"SUCCESS")
|
||
echo -e "${GREEN}✅ $message${NC}"
|
||
((TESTS_PASSED++))
|
||
;;
|
||
"FAILURE")
|
||
echo -e "${RED}❌ $message${NC}"
|
||
((TESTS_FAILED++))
|
||
;;
|
||
"INFO")
|
||
echo -e "${YELLOW}ℹ️ $message${NC}"
|
||
;;
|
||
*)
|
||
echo "$message"
|
||
;;
|
||
esac
|
||
}
|
||
|
||
# Function to run a test
|
||
run_test() {
|
||
local test_name=$1
|
||
local command=$2
|
||
local timeout_duration=${3:-10}
|
||
|
||
echo ""
|
||
echo "Running test: $test_name"
|
||
echo "Command: $command"
|
||
|
||
if timeout ${timeout_duration}s bash -c "$command" > /tmp/test_output.log 2>&1; then
|
||
print_status "SUCCESS" "$test_name passed"
|
||
return 0
|
||
else
|
||
print_status "FAILURE" "$test_name failed"
|
||
echo "Output:"
|
||
tail -20 /tmp/test_output.log
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
# Create test data directory
|
||
create_test_data() {
|
||
print_status "INFO" "Creating test data directory..."
|
||
mkdir -p "$TEST_DATA_DIR"
|
||
|
||
# Check if ffmpeg is available
|
||
if ! command -v ffmpeg &> /dev/null; then
|
||
print_status "FAILURE" "FFmpeg not found. Please install FFmpeg."
|
||
exit 1
|
||
fi
|
||
|
||
# Create various test videos
|
||
print_status "INFO" "Generating test videos..."
|
||
|
||
# 1. Small test video (480p, 5 seconds)
|
||
if [ ! -f "$TEST_DATA_DIR/test_480p.mp4" ]; then
|
||
ffmpeg -f lavfi -i testsrc2=size=640x480:rate=30 -t 5 \
|
||
-vf "drawtext=text='480p Test':fontsize=20:fontcolor=white:x=10:y=10" \
|
||
-c:v libx264 -preset ultrafast \
|
||
-y "$TEST_DATA_DIR/test_480p.mp4" 2>/dev/null
|
||
print_status "SUCCESS" "Created test_480p.mp4"
|
||
fi
|
||
|
||
# 2. Medium test video (720p, 10 seconds)
|
||
if [ ! -f "$TEST_DATA_DIR/test_720p.mp4" ]; then
|
||
ffmpeg -f lavfi -i testsrc=size=1280x720:rate=30 -t 10 \
|
||
-vf "drawtext=text='720p Test':fontsize=24:fontcolor=white:x=10:y=10" \
|
||
-c:v libx264 -preset ultrafast \
|
||
-y "$TEST_DATA_DIR/test_720p.mp4" 2>/dev/null
|
||
print_status "SUCCESS" "Created test_720p.mp4"
|
||
fi
|
||
|
||
# 3. High resolution test (1080p, 5 seconds)
|
||
if [ ! -f "$TEST_DATA_DIR/test_1080p.mp4" ]; then
|
||
ffmpeg -f lavfi -i testsrc2=size=1920x1080:rate=30 -t 5 \
|
||
-vf "drawtext=text='1080p Test':fontsize=30:fontcolor=white:x=10:y=10" \
|
||
-c:v libx264 -preset ultrafast \
|
||
-y "$TEST_DATA_DIR/test_1080p.mp4" 2>/dev/null
|
||
print_status "SUCCESS" "Created test_1080p.mp4"
|
||
fi
|
||
|
||
# 4. Meteor simulation video (with brightness changes)
|
||
if [ ! -f "$TEST_DATA_DIR/meteor_simulation.mp4" ]; then
|
||
ffmpeg -f lavfi -i "nullsrc=s=1280x720:r=30" -t 10 \
|
||
-vf "geq=r='if(between(t,2,2.1)+between(t,5,5.2)+between(t,8,8.1),255,50)':g='if(between(t,2,2.1)+between(t,5,5.2)+between(t,8,8.1),255,50)':b='if(between(t,2,2.1)+between(t,5,5.2)+between(t,8,8.1),255,50)',drawtext=text='Meteor Simulation':fontsize=24:fontcolor=white:x=(w-text_w)/2:y=50" \
|
||
-c:v libx264 -preset ultrafast \
|
||
-y "$TEST_DATA_DIR/meteor_simulation.mp4" 2>/dev/null
|
||
print_status "SUCCESS" "Created meteor_simulation.mp4"
|
||
fi
|
||
|
||
ls -lh "$TEST_DATA_DIR"/*.mp4 2>/dev/null || true
|
||
}
|
||
|
||
# Build the project
|
||
build_project() {
|
||
print_status "INFO" "Building meteor-edge-client..."
|
||
cd "$PROJECT_DIR"
|
||
|
||
if cargo build --features camera_sim --release; then
|
||
print_status "SUCCESS" "Build completed successfully"
|
||
else
|
||
print_status "FAILURE" "Build failed"
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
# Test 1: Test pattern sources
|
||
test_patterns() {
|
||
print_status "INFO" "Testing pattern generators..."
|
||
|
||
local patterns=("static" "noise" "bar" "meteor" "checkerboard" "gradient")
|
||
|
||
for pattern in "${patterns[@]}"; do
|
||
run_test "Pattern: $pattern" \
|
||
"cd $PROJECT_DIR && cargo run --features camera_sim -- test --camera-source test:$pattern --frames 100" \
|
||
5
|
||
done
|
||
}
|
||
|
||
# Test 2: File reader
|
||
test_file_reader() {
|
||
print_status "INFO" "Testing file reader..."
|
||
|
||
local test_files=("test_480p.mp4" "test_720p.mp4" "test_1080p.mp4" "meteor_simulation.mp4")
|
||
|
||
for file in "${test_files[@]}"; do
|
||
if [ -f "$TEST_DATA_DIR/$file" ]; then
|
||
run_test "File: $file" \
|
||
"cd $PROJECT_DIR && cargo run --features camera_sim -- test --camera-source file:test_data/$file --frames 50" \
|
||
10
|
||
else
|
||
print_status "FAILURE" "Test file not found: $file"
|
||
fi
|
||
done
|
||
}
|
||
|
||
# Test 3: Performance benchmarks
|
||
test_performance() {
|
||
print_status "INFO" "Running performance benchmarks..."
|
||
|
||
# Test pattern performance
|
||
run_test "Pattern benchmark (1000 frames)" \
|
||
"cd $PROJECT_DIR && cargo run --release --features camera_sim -- benchmark --camera-source test:meteor --frames 1000" \
|
||
30
|
||
|
||
# File reader performance
|
||
if [ -f "$TEST_DATA_DIR/test_720p.mp4" ]; then
|
||
run_test "File reader benchmark" \
|
||
"cd $PROJECT_DIR && cargo run --release --features camera_sim -- benchmark --camera-source file:test_data/test_720p.mp4 --frames 300" \
|
||
30
|
||
fi
|
||
}
|
||
|
||
# Test 4: Memory leak check
|
||
test_memory_leaks() {
|
||
print_status "INFO" "Testing for memory leaks..."
|
||
|
||
if command -v valgrind &> /dev/null; then
|
||
run_test "Memory leak check" \
|
||
"cd $PROJECT_DIR && valgrind --leak-check=full --error-exitcode=1 ./target/release/meteor-edge-client test --camera-source test:static --frames 100" \
|
||
60
|
||
else
|
||
print_status "INFO" "Valgrind not available, skipping memory leak test"
|
||
fi
|
||
}
|
||
|
||
# Test 5: Concurrent sources
|
||
test_concurrent() {
|
||
print_status "INFO" "Testing concurrent camera sources..."
|
||
|
||
# Start multiple instances in background
|
||
cd "$PROJECT_DIR"
|
||
|
||
./target/release/meteor-edge-client test --camera-source test:meteor --frames 100 &
|
||
PID1=$!
|
||
|
||
./target/release/meteor-edge-client test --camera-source test:noise --frames 100 &
|
||
PID2=$!
|
||
|
||
./target/release/meteor-edge-client test --camera-source test:bar --frames 100 &
|
||
PID3=$!
|
||
|
||
# Wait for all to complete
|
||
if wait $PID1 && wait $PID2 && wait $PID3; then
|
||
print_status "SUCCESS" "Concurrent sources test passed"
|
||
else
|
||
print_status "FAILURE" "Concurrent sources test failed"
|
||
fi
|
||
}
|
||
|
||
# Test 6: Error handling
|
||
test_error_handling() {
|
||
print_status "INFO" "Testing error handling..."
|
||
|
||
# Test with non-existent file
|
||
run_test "Non-existent file handling" \
|
||
"cd $PROJECT_DIR && ! cargo run --features camera_sim -- test --camera-source file:nonexistent.mp4 --frames 10" \
|
||
5
|
||
|
||
# Test with invalid pattern
|
||
run_test "Invalid pattern handling" \
|
||
"cd $PROJECT_DIR && ! cargo run --features camera_sim -- test --camera-source test:invalid_pattern --frames 10" \
|
||
5
|
||
}
|
||
|
||
# Main test execution
|
||
main() {
|
||
echo "================================================"
|
||
echo "Camera Simulation Test Suite"
|
||
echo "================================================"
|
||
|
||
# Parse arguments
|
||
TEST_SUITE=${1:-all}
|
||
|
||
# Create test data
|
||
create_test_data
|
||
|
||
# Build project
|
||
build_project
|
||
|
||
# Run tests based on selection
|
||
case $TEST_SUITE in
|
||
patterns)
|
||
test_patterns
|
||
;;
|
||
files)
|
||
test_file_reader
|
||
;;
|
||
performance)
|
||
test_performance
|
||
;;
|
||
memory)
|
||
test_memory_leaks
|
||
;;
|
||
concurrent)
|
||
test_concurrent
|
||
;;
|
||
errors)
|
||
test_error_handling
|
||
;;
|
||
all)
|
||
test_patterns
|
||
test_file_reader
|
||
test_performance
|
||
test_memory_leaks
|
||
test_concurrent
|
||
test_error_handling
|
||
;;
|
||
*)
|
||
echo "Usage: $0 [all|patterns|files|performance|memory|concurrent|errors]"
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
# Print summary
|
||
echo ""
|
||
echo "================================================"
|
||
echo "Test Summary"
|
||
echo "================================================"
|
||
print_status "SUCCESS" "Tests passed: $TESTS_PASSED"
|
||
|
||
if [ $TESTS_FAILED -gt 0 ]; then
|
||
print_status "FAILURE" "Tests failed: $TESTS_FAILED"
|
||
exit 1
|
||
else
|
||
print_status "SUCCESS" "All tests passed!"
|
||
fi
|
||
}
|
||
|
||
# Run main function
|
||
main "$@" |