#!/bin/bash

# Test cluster rejecting node with wrong epoch

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


# create a node who has wrong epoch
_start_sheep 1
_wait_for_sheep 1 1
$COLLIE cluster format -p 7001 -c 1
$COLLIE cluster shutdown -p 7001
_wait_for_sheep_stop

# start Sheepdog with one node
_start_sheep 0
_wait_for_sheep 1
$COLLIE cluster format -p 7000 -c 1

for i in `seq 0 5`; do
    _start_sheep 1  # should fail
    sleep 1
    _start_sheep 2  # should succeed
    _wait_for_sheep 2

    if [ "`$COLLIE node list -p 7002 -r | wc -l`" -ne 2 ]; then
	echo "test failed"
	$COLLIE cluster info -p 7000 | _filter_cluster_info
	$COLLIE cluster info -p 7002 | _filter_cluster_info
	exit 1
    fi

    _kill_sheep 2
    sleep 1
done

echo "success"
