#!/bin/bash

# Test cluster snapshot

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 `seq 0 3`; do
	_start_sheep $i;
done
_wait_for_sheep 4

TMPDIR=`mktemp -d`

$COLLIE cluster format -c 3

$COLLIE vdi create test1 10M
$COLLIE vdi create test2 10M

_random | $COLLIE vdi write test1
_random | $COLLIE vdi write test2
$COLLIE vdi read test1 | md5sum > /tmp/csum.11.org
$COLLIE vdi read test2 | md5sum > /tmp/csum.21.org
$COLLIE vdi snapshot test1
$COLLIE vdi snapshot test2
$COLLIE cluster snapshot save s1 $TMPDIR
$COLLIE cluster snapshot list $TMPDIR | _filter_date

_random | $COLLIE vdi write test1
_random | $COLLIE vdi write test2
$COLLIE vdi read test1 | md5sum > /tmp/csum.12.org
$COLLIE vdi read test2 | md5sum > /tmp/csum.22.org
$COLLIE vdi snapshot test1
$COLLIE vdi snapshot test2
$COLLIE cluster snapshot save s2 $TMPDIR
$COLLIE cluster snapshot list $TMPDIR | _filter_date

$COLLIE vdi list | _filter_short_date

_cleanup
for i in `seq 0 3`; do
	_start_sheep $i;
done
_wait_for_sheep 4

$COLLIE cluster format -c 3
$COLLIE cluster snapshot load s1 $TMPDIR
$COLLIE vdi list | _filter_short_date

$COLLIE vdi read test1 | md5sum > /tmp/csum.11.new
$COLLIE vdi read test2 | md5sum > /tmp/csum.21.new
diff -u /tmp/csum.11.org /tmp/csum.11.new
diff -u /tmp/csum.21.org /tmp/csum.21.new

_cleanup
for i in `seq 0 3`; do
	_start_sheep $i;
done
_wait_for_sheep 4

$COLLIE cluster snapshot load s2 $TMPDIR
$COLLIE vdi list | _filter_short_date

_cleanup
for i in `seq 0 3`; do
	_start_sheep $i;
done
_wait_for_sheep 4

$COLLIE cluster snapshot load 2 $TMPDIR
$COLLIE vdi list | _filter_short_date

$COLLIE vdi read -s 2 test1 | md5sum > /tmp/csum.12.new
$COLLIE vdi read -s 2 test2 | md5sum > /tmp/csum.22.new
diff -u /tmp/csum.12.org /tmp/csum.12.new
diff -u /tmp/csum.22.org /tmp/csum.22.new

$COLLIE vdi read test1 | md5sum > /tmp/csum.12.new
$COLLIE vdi read test2 | md5sum > /tmp/csum.22.new
diff -u /tmp/csum.12.org /tmp/csum.12.new
diff -u /tmp/csum.22.org /tmp/csum.22.new

$COLLIE vdi read -s 1 test1 | md5sum > /tmp/csum.11.new
$COLLIE vdi read -s 1 test2 | md5sum > /tmp/csum.21.new
diff -u /tmp/csum.11.org /tmp/csum.11.new
diff -u /tmp/csum.21.org /tmp/csum.21.new

rm -rf $TMPDIR
