#!/bin/bash

# Check for a recovery segfault in older versions

seq=`basename $0`
echo "QA output created by $seq"

here=`pwd`
tmp=/tmp/$$
status=1        # failure is the default!

# get standard environment, filters and checks
. ./common.rc
. ./common.filter

_cleanup

VDI_NAME="test"
VDI_SIZE="20M"

echo "starting two sheep in the first zone"
for i in `seq 0 1`; do
    $SHEEP $STORE/$i -d -z 0 -p 700$i -c $DRIVER -y 127.0.0.1
done

echo "starting a sheep in the second zone"
for i in `seq 2 2`; do
    $SHEEP $STORE/$i -d -z 1 -p 700$i -c $DRIVER -y 127.0.0.1
done

_wait_for_sheep "3"

echo "formatting cluster"
$COLLIE cluster format -c 2

echo "creating vdi ${NAME}"
$COLLIE vdi create ${VDI_NAME} ${VDI_SIZE}

echo "filling ${VDI_NAME} with data"
qemu-io -c "write 0 ${VDI_SIZE}" sheepdog:${VDI_NAME} | _filter_qemu_io

echo "reading back ${VDI_NAME} from second zone"
qemu-io -c "read 0 1m" sheepdog:localhost:7002:${VDI_NAME} | _filter_qemu_io

echo "starting a sheep in the third zone"
for i in `seq 3 3`; do
    $SHEEP $STORE/$i -d -z 2 -p 700$i -c $DRIVER -y 127.0.0.1
done

sleep 10

echo "check that all sheep are alive"
for i in `seq 0 3`; do
    $COLLIE cluster info -p 700$i | _filter_cluster_info
done
