#!/bin/bash # Test script for Particle-OS path management and initialization # Tests the new configuration-based path system and initialization commands set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Test configuration APT_LAYER_SCRIPT="./apt-layer.sh" TEST_DIR="/tmp/particle-os-test-$$" # Function to cleanup test environment cleanup() { log_info "Cleaning up test environment..." if [[ -d "$TEST_DIR" ]]; then rm -rf "$TEST_DIR" fi # Remove any test directories that might have been created sudo rm -rf "/var/lib/apt-layer-test" 2>/dev/null || true sudo rm -rf "/var/log/apt-layer-test" 2>/dev/null || true sudo rm -rf "/var/cache/apt-layer-test" 2>/dev/null || true sudo rm -rf "/usr/local/etc/apt-layer-test" 2>/dev/null || true } # Function to run test run_test() { local test_name="$1" local test_command="$2" local expected_exit_code="${3:-0}" log_info "Running test: $test_name" log_info "Command: $test_command" if eval "$test_command"; then if [[ $? -eq $expected_exit_code ]]; then log_success "Test passed: $test_name" return 0 else log_error "Test failed: $test_name (unexpected exit code: $?)" return 1 fi else if [[ $? -eq $expected_exit_code ]]; then log_success "Test passed: $test_name (expected failure)" return 0 else log_error "Test failed: $test_name (unexpected exit code: $?)" return 1 fi fi } # Main test execution main() { log_info "Starting Particle-OS path management tests" # Check if apt-layer.sh exists if [[ ! -f "$APT_LAYER_SCRIPT" ]]; then log_error "apt-layer.sh not found. Please run from the tools directory." exit 1 fi # Make sure it's executable chmod +x "$APT_LAYER_SCRIPT" # Test 1: Check if new commands are available log_info "=== Test 1: Check new initialization commands ===" run_test "Help command shows new options" "$APT_LAYER_SCRIPT --help | grep -E '(--init|--reinit|--rm-init|--status)'" 0 # Test 2: Test system status (should work without initialization) log_info "=== Test 2: Test system status command ===" run_test "System status command" "$APT_LAYER_SCRIPT --status" 0 # Test 3: Test initialization log_info "=== Test 3: Test system initialization ===" run_test "System initialization" "sudo $APT_LAYER_SCRIPT --init" 0 # Test 4: Verify directories were created log_info "=== Test 4: Verify directories were created ===" local dirs=( "/var/lib/apt-layer" "/var/log/apt-layer" "/var/cache/apt-layer" "/usr/local/etc/apt-layer" "/var/lib/apt-layer/live-overlay" "/var/lib/apt-layer/deployments" "/var/lib/apt-layer/backups" "/var/lib/apt-layer/images" "/var/lib/apt-layer/layers" "/etc/apt-layer/bootloader" "/etc/apt-layer/kargs" "/var/lib/apt-layer/containers" "/var/lib/apt-layer/oci-images" "/var/lib/apt-layer/exports" "/var/lib/apt-layer/composefs" "/var/cache/apt-layer/composefs" ) for dir in "${dirs[@]}"; do if [[ -d "$dir" ]]; then log_success "Directory exists: $dir" else log_error "Directory missing: $dir" return 1 fi done # Test 5: Test system status after initialization log_info "=== Test 5: Test system status after initialization ===" run_test "System status after init" "$APT_LAYER_SCRIPT --status" 0 # Test 6: Test reinitialization log_info "=== Test 6: Test system reinitialization ===" run_test "System reinitialization" "sudo $APT_LAYER_SCRIPT --reinit" 0 # Test 7: Verify directories still exist after reinit log_info "=== Test 7: Verify directories after reinit ===" for dir in "${dirs[@]}"; do if [[ -d "$dir" ]]; then log_success "Directory exists after reinit: $dir" else log_error "Directory missing after reinit: $dir" return 1 fi done # Test 8: Test live overlay start (should work now) log_info "=== Test 8: Test live overlay start ===" run_test "Live overlay start" "sudo $APT_LAYER_SCRIPT --live-overlay start" 0 # Test 9: Test live overlay status log_info "=== Test 9: Test live overlay status ===" run_test "Live overlay status" "$APT_LAYER_SCRIPT --live-overlay status" 0 # Test 10: Test live overlay stop log_info "=== Test 10: Test live overlay stop ===" run_test "Live overlay stop" "sudo $APT_LAYER_SCRIPT --live-overlay stop" 0 # Test 11: Test system removal (cleanup) log_info "=== Test 11: Test system removal ===" run_test "System removal" "sudo $APT_LAYER_SCRIPT --rm-init" 0 # Test 12: Verify directories were removed log_info "=== Test 12: Verify directories were removed ===" for dir in "${dirs[@]}"; do if [[ ! -d "$dir" ]]; then log_success "Directory removed: $dir" else log_warning "Directory still exists: $dir" fi done log_success "All Particle-OS path management tests completed successfully!" } # Trap to ensure cleanup on exit trap cleanup EXIT # Run main test main "$@"