#!/bin/bash
# iSCSI + RAID5 Docker测试脚本

set -e

echo "=== MarkBase Docker Test Environment ==="

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "ERROR: Docker not running"
    echo "Start Docker Desktop or run: docker daemon"
    exit 1
fi

echo ""
echo "Step 1: Building Docker images..."
docker-compose -f docker/docker-compose.yml build

echo ""
echo "Step 2: Starting test containers..."
docker-compose -f docker/docker-compose.yml up -d

echo ""
echo "Step 3: Waiting for containers to start..."
sleep 10

echo ""
echo "Step 4: Checking RAID test container..."
docker-compose -f docker/docker-compose.yml ps raid_test

echo ""
echo "Step 5: Running RAID5 configuration..."
docker-compose -f docker/docker-compose.yml exec raid_test \
    ./target/release/configure_iscsi docker_test \
    --disks /tmp/test_disks/disk1.img /tmp/test_disks/disk2.img /tmp/test_disks/disk3.img

echo ""
echo "Step 6: Verifying RAID5 status..."
docker-compose -f docker/docker-compose.yml exec raid_test \
    sudo dmsetup status markbase_docker_test

echo ""
echo "Step 7: Checking WebDAV server..."
docker-compose -f docker/docker-compose.yml ps webdav_server

echo ""
echo "Step 8: Testing WebDAV endpoint..."
curl -s http://localhost:4919/api/v2/tree/docker_test | head -20

echo ""
echo "Step 9: Running performance test (fio)..."
docker-compose -f docker/docker-compose.yml exec raid_test \
    fio --filename=/dev/mapper/markbase_docker_test \
    --direct=1 \
    --rw=read \
    --bs=4k \
    --size=100M \
    --iodepth=32 \
    --name=raid5_perf_test

echo ""
echo "=== Test Complete ==="
echo "Containers running:"
docker-compose -f docker/docker-compose.yml ps

echo ""
echo "To stop containers:"
echo "  docker-compose -f docker/docker-compose.yml down"
echo ""
echo "To view logs:"
echo "  docker-compose -f docker/docker-compose.yml logs raid_test"
echo "  docker-compose -f docker/docker-compose.yml logs webdav_server"