#!/bin/bash # Скрипт для тестирования reconciler (Трек 5). set -e echo "=== Testing Reconciler (Трек 5) ===" echo # Проверка, что стенд запущен. if ! docker compose ps | grep -q "Up"; then echo "Error: Docker Compose stack is not running" exit 1 fi # Установка зависимостей для reconciler. echo "Installing Python dependencies..." pip3 install -q psycopg2-binary boto3 2>/dev/null || pip install -q psycopg2-binary boto3 # 1. Baseline: reconciler на чистой системе. echo "Step 1: Running reconciler on clean system (baseline)..." python3 scripts/reconciler.py \ --db-host localhost \ --s3-endpoint http://localhost:9000 \ --grace-period-hours 0 \ --dry-run \ --output evidence/reconciler_baseline.json echo "Baseline report saved to evidence/reconciler_baseline.json" echo # 2. Создание тестовых orphan объектов в S3. echo "Step 2: Creating orphan objects in S3..." docker compose exec -T minio sh -c " mc alias set myminio http://localhost:9000 minioadmin minioadmin echo 'orphan test data' | mc pipe myminio/messenger-attachments/attachments/test-orphan-1.jpg echo 'orphan test data 2' | mc pipe myminio/messenger-attachments/attachments/test-orphan-2.png mc ls myminio/messenger-attachments/attachments/ | grep test-orphan " echo "Created 2 orphan objects" echo # 3. Создание missing file (удаление объекта из S3, но оставляем запись в БД). echo "Step 3: Simulating missing file (requires manual setup via API)..." echo "Note: This step requires an actual attachment uploaded via API first." echo "Skipping automatic missing file creation in test script." echo # 4. Запуск reconciler в dry-run режиме. echo "Step 4: Running reconciler in dry-run mode..." python3 scripts/reconciler.py \ --db-host localhost \ --s3-endpoint http://localhost:9000 \ --grace-period-hours 0 \ --dry-run \ --output evidence/reconciler_dryrun.json echo "Dry-run report saved to evidence/reconciler_dryrun.json" echo # 5. Запуск reconciler в live режиме (удаление orphans). echo "Step 5: Running reconciler in LIVE mode (cleanup)..." python3 scripts/reconciler.py \ --db-host localhost \ --s3-endpoint http://localhost:9000 \ --grace-period-hours 0 \ --output evidence/reconciler_cleanup.json echo "Cleanup report saved to evidence/reconciler_cleanup.json" echo # 6. Проверка, что orphans удалены. echo "Step 6: Verifying orphan cleanup..." docker compose exec -T minio sh -c " mc alias set myminio http://localhost:9000 minioadmin minioadmin mc ls myminio/messenger-attachments/attachments/ | grep test-orphan || echo 'All test orphans removed successfully' " echo # 7. Тест grace period (свежие файлы не трогаются). echo "Step 7: Testing grace period protection..." docker compose exec -T minio sh -c " mc alias set myminio http://localhost:9000 minioadmin minioadmin echo 'fresh orphan' | mc pipe myminio/messenger-attachments/attachments/test-fresh.jpg " python3 scripts/reconciler.py \ --db-host localhost \ --s3-endpoint http://localhost:9000 \ --grace-period-hours 1 \ --dry-run \ --output evidence/reconciler_grace.json # Проверяем, что свежий файл НЕ попал в список orphans. if grep -q "test-fresh.jpg" evidence/reconciler_grace.json; then echo "WARNING: Fresh file should not be in orphans list (grace period failed)" else echo "Grace period protection works: fresh file excluded from cleanup" fi # Очистка тестового файла. docker compose exec -T minio sh -c " mc alias set myminio http://localhost:9000 minioadmin minioadmin mc rm myminio/messenger-attachments/attachments/test-fresh.jpg " echo echo "=== Reconciler Testing Complete ===" echo "Reports saved in evidence/ directory:" echo " - reconciler_baseline.json" echo " - reconciler_dryrun.json" echo " - reconciler_cleanup.json" echo " - reconciler_grace.json"