#!/bin/bash # Mock Integration Test Script for debian-forge # Tests the mock integration functionality set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Test configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" TEST_DIR="$PROJECT_DIR/test-mock-integration" OUTPUT_DIR="$TEST_DIR/output" LOG_FILE="$TEST_DIR/mock-test.log" # Test results TESTS_PASSED=0 TESTS_FAILED=0 TESTS_TOTAL=0 # Function to print colored output print_status() { local status=$1 local message=$2 case $status in "INFO") echo -e "${BLUE}[INFO]${NC} $message" ;; "SUCCESS") echo -e "${GREEN}[SUCCESS]${NC} $message" ;; "WARNING") echo -e "${YELLOW}[WARNING]${NC} $message" ;; "ERROR") echo -e "${RED}[ERROR]${NC} $message" ;; esac } # Function to run a test run_test() { local test_name=$1 local test_command=$2 local expected_exit_code=${3:-0} TESTS_TOTAL=$((TESTS_TOTAL + 1)) print_status "INFO" "Running test: $test_name" if eval "$test_command" >> "$LOG_FILE" 2>&1; then if [ $? -eq $expected_exit_code ]; then print_status "SUCCESS" "Test passed: $test_name" TESTS_PASSED=$((TESTS_PASSED + 1)) return 0 else print_status "ERROR" "Test failed: $test_name (wrong exit code)" TESTS_FAILED=$((TESTS_FAILED + 1)) return 1 fi else print_status "ERROR" "Test failed: $test_name" TESTS_FAILED=$((TESTS_FAILED + 1)) return 1 fi } # Function to check if mock is available check_mock() { print_status "INFO" "Checking mock availability..." if python3 -c "import mock" 2>/dev/null; then print_status "SUCCESS" "mock is available" return 0 else print_status "WARNING" "mock is not available - some tests will be skipped" return 1 fi } # Function to setup test environment setup_test_environment() { print_status "INFO" "Setting up test environment..." # Create test directory mkdir -p "$TEST_DIR" mkdir -p "$OUTPUT_DIR" # Create test source directory mkdir -p "$TEST_DIR/source" # Create a simple test package cat > "$TEST_DIR/source/hello.c" << 'EOF' #include int main() { printf("Hello from debian-forge mock integration!\n"); return 0; } EOF cat > "$TEST_DIR/source/Makefile" << 'EOF' hello: hello.c gcc -o hello hello.c clean: rm -f hello install: hello install -m 755 hello /usr/local/bin/ EOF print_status "SUCCESS" "Test environment setup complete" } # Function to test mock stage compilation test_mock_stage_compilation() { print_status "INFO" "Testing mock stage compilation..." # Test Python syntax run_test "Mock Stage Syntax" "python3 -m py_compile $PROJECT_DIR/stages/org.osbuild.deb-mock.py" # Test JSON schema validation run_test "Mock Stage Schema" "python3 -c \"import json; json.load(open('$PROJECT_DIR/stages/org.osbuild.deb-mock.meta.json'))\"" } # Function to test mock stage basic functionality test_mock_stage_basic() { if ! check_mock; then print_status "WARNING" "Skipping mock stage tests - mock not available" return 0 fi print_status "INFO" "Testing mock stage basic functionality..." # Test stage help run_test "Mock Stage Help" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --help" # Test invalid options run_test "Mock Stage Invalid Options" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{}'" 1 } # Function to test manifest validation test_manifest_validation() { print_status "INFO" "Testing manifest validation..." # Test mock build manifest run_test "Mock Build Manifest" "python3 -c \"import json; json.load(open('$PROJECT_DIR/test/data/manifests/debian/debian-mock-build.json'))\"" # Test mock APT integration manifest run_test "Mock APT Integration Manifest" "python3 -c \"import json; json.load(open('$PROJECT_DIR/test/data/manifests/debian/debian-mock-apt-integration.json'))\"" } # Function to test mock integration with osbuild test_osbuild_integration() { if ! check_mock; then print_status "WARNING" "Skipping osbuild integration tests - mock not available" return 0 fi print_status "INFO" "Testing osbuild integration..." # Test with mock build manifest run_test "OSBuild Mock Integration" "cd $PROJECT_DIR && python3 -m osbuild --output-dir $OUTPUT_DIR --libdir . --json test/data/manifests/debian/debian-mock-build.json" } # Function to test mock environment management test_mock_environment_management() { if ! check_mock; then print_status "WARNING" "Skipping mock environment tests - mock not available" return 0 fi print_status "INFO" "Testing mock environment management..." # Test environment creation run_test "Mock Environment Creation" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"create\", \"mock_options\": {\"environment\": \"test-env\", \"architecture\": \"amd64\", \"suite\": \"trixie\"}}'" # Test environment listing run_test "Mock Environment Listing" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"list_environments\"}'" # Test environment destruction run_test "Mock Environment Destruction" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"destroy\", \"mock_options\": {\"environment\": \"test-env\"}}'" } # Function to test mock file operations test_mock_file_operations() { if ! check_mock; then print_status "WARNING" "Skipping mock file operation tests - mock not available" return 0 fi print_status "INFO" "Testing mock file operations..." # Create test environment python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{"action": "create", "mock_options": {"environment": "test-file-ops", "architecture": "amd64", "suite": "trixie"}}' >> "$LOG_FILE" 2>&1 # Test file copy in run_test "Mock File Copy In" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"copy_files\", \"mock_options\": {\"environment\": \"test-file-ops\"}, \"copy_operations\": [{\"type\": \"in\", \"source\": \"$TEST_DIR/source\", \"destination\": \"/build/source\"}]}'" # Test file copy out run_test "Mock File Copy Out" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"copy_files\", \"mock_options\": {\"environment\": \"test-file-ops\"}, \"copy_operations\": [{\"type\": \"out\", \"source\": \"/build/source\", \"destination\": \"$TEST_DIR/output\"}]}'" # Clean up python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{"action": "destroy", "mock_options": {"environment": "test-file-ops"}}' >> "$LOG_FILE" 2>&1 } # Function to test mock command execution test_mock_command_execution() { if ! check_mock; then print_status "WARNING" "Skipping mock command execution tests - mock not available" return 0 fi print_status "INFO" "Testing mock command execution..." # Create test environment python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{"action": "create", "mock_options": {"environment": "test-commands", "architecture": "amd64", "suite": "trixie"}}' >> "$LOG_FILE" 2>&1 # Test command execution run_test "Mock Command Execution" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"execute\", \"mock_options\": {\"environment\": \"test-commands\"}, \"commands\": [[\"ls\", \"-la\", \"/\"], [\"uname\", \"-a\"]]}'" # Test package installation run_test "Mock Package Installation" "python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{\"action\": \"install_packages\", \"mock_options\": {\"environment\": \"test-commands\"}, \"packages\": [\"build-essential\"]}'" # Clean up python3 $PROJECT_DIR/stages/org.osbuild.deb-mock.py --tree $TEST_DIR --options '{"action": "destroy", "mock_options": {"environment": "test-commands"}}' >> "$LOG_FILE" 2>&1 } # Function to cleanup test environment cleanup_test_environment() { print_status "INFO" "Cleaning up test environment..." # Remove test directory if [ -d "$TEST_DIR" ]; then rm -rf "$TEST_DIR" fi print_status "SUCCESS" "Test environment cleanup complete" } # Function to print test summary print_test_summary() { echo echo "==========================================" echo "Mock Integration Test Summary" echo "==========================================" echo "Total tests: $TESTS_TOTAL" echo "Passed: $TESTS_PASSED" echo "Failed: $TESTS_FAILED" echo "Success rate: $(( (TESTS_PASSED * 100) / TESTS_TOTAL ))%" echo "==========================================" if [ $TESTS_FAILED -eq 0 ]; then print_status "SUCCESS" "All tests passed!" return 0 else print_status "ERROR" "Some tests failed. Check $LOG_FILE for details." return 1 fi } # Main execution main() { print_status "INFO" "Starting debian-forge mock integration tests..." # Setup setup_test_environment # Run tests test_mock_stage_compilation test_mock_stage_basic test_manifest_validation test_osbuild_integration test_mock_environment_management test_mock_file_operations test_mock_command_execution # Print summary print_test_summary local exit_code=$? # Cleanup cleanup_test_environment exit $exit_code } # Run main function main "$@"