#!/bin/bash

# Test cluster membership by killing and adding nodes

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

for i in 0 1; do
    _start_sheep $i
done

_wait_for_sheep 2
# start Sheepdog with two nodes
$COLLIE cluster format -c 1
sleep 1

for i in 2 3 4; do
    _kill_sheep $((i - 2))
    sleep 1
    _start_sheep $i
    sleep 2
done

_wait_for_sheep 2 4

$COLLIE cluster shutdown -p 7004
_wait_for_sheep_stop

for i in 0 1 2 3 4; do
    _start_sheep $i
    sleep 1
done

_wait_for_sheep_stop 0
_wait_for_sheep_stop 1
_wait_for_sheep_stop 2

_wait_for_sheep 2 4

echo check whether Sheepdog is working with two nodes
for i in 3 4; do
    $COLLIE cluster info -p 700$i | _filter_cluster_info
done

# add the other nodes
for i in 0 1 2; do
    _start_sheep $i
    sleep 1
done

_wait_for_sheep 5
echo check whether all nodes have the same cluster info
for i in 0 1 2 3 4; do
    $COLLIE cluster info -p 700$i | _filter_cluster_info
done
