#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016-2018, Intel Corporation
#
#
# pmempool_transform/TEST19 -- test for transform with SINGLEHDR option
#
# removing the SINGLEHDR option
# case: two replicas larger than the pool size, able to embrace new headers
#

. ../unittest/unittest.sh

require_test_type medium
require_fs_type any

setup

LOG=out${UNITTEST_NUM}.log
LOG_TEMP=out${UNITTEST_NUM}_part.log
rm -f $LOG && touch $LOG
rm -f $LOG_TEMP && touch $LOG_TEMP

LAYOUT=OBJ_LAYOUT$SUFFIX
POOLSET_1=$DIR/poolset.1
POOLSET_2=$DIR/poolset.2
POOLSET_3=$DIR/poolset.3
POOLSET_4=$DIR/poolset.4
POOLSET_5=$DIR/poolset.5

# Create poolset files
create_poolset $POOLSET_1 \
	O SINGLEHDR \
	20M:$DIR/part00:x \
	20M:$DIR/part01:x \
	20M:$DIR/part02:x

create_poolset $POOLSET_2 \
	O SINGLEHDR \
	20M:$DIR/part00:x \
	20M:$DIR/part01:x \
	20M:$DIR/part02:x \
	R \
	20M:$DIR/part10:x \
	20M:$DIR/part11:x \
	20M:$DIR/part12:x \
	10M:$DIR/part13:x

create_poolset $POOLSET_3 \
	O SINGLEHDR \
	20M:$DIR/part10:x \
	20M:$DIR/part11:x \
	20M:$DIR/part12:x \
	10M:$DIR/part13:x

create_poolset $POOLSET_4 \
	O SINGLEHDR \
	20M:$DIR/part00:x \
	20M:$DIR/part01:x \
	20M:$DIR/part02:x \
	10M:$DIR/part03:x \
	R \
	20M:$DIR/part10:x \
	20M:$DIR/part11:x \
	20M:$DIR/part12:x \
	10M:$DIR/part13:x

create_poolset $POOLSET_5 \
	20M:$DIR/part00:x \
	20M:$DIR/part01:x \
	20M:$DIR/part02:x \
	10M:$DIR/part03:x \
	R \
	20M:$DIR/part10:x \
	20M:$DIR/part11:x \
	20M:$DIR/part12:x \
	10M:$DIR/part13:x

# CLI script for writing some data hitting all the parts
WRITE_SCRIPT=$DIR/write_data
cat << EOF > $WRITE_SCRIPT
pr 45M
srcp 0 TestOK111
srcp 20M TestOK222
srcp 40M TestOK333
EOF

# CLI script for reading 9 characters from all the parts
READ_SCRIPT=$DIR/read_data
cat << EOF > $READ_SCRIPT
srpr 0 9
srpr 20M 9
srpr 40M 9
EOF

# Create a pool
expect_normal_exit $PMEMPOOL$EXESUFFIX create --layout=$LAYOUT obj $POOLSET_1 \
	>> $LOG_TEMP

# Transform poolset - add a second, larger, replica
expect_normal_exit $PMEMPOOL$EXESUFFIX transform $POOLSET_1 $POOLSET_2 >> \
	$LOG_TEMP

# Transform poolset - remove the first replica
expect_normal_exit $PMEMPOOL$EXESUFFIX transform $POOLSET_2 $POOLSET_3 >> \
	$LOG_TEMP

# Transform poolset - add a second larger replica
expect_normal_exit $PMEMPOOL$EXESUFFIX transform $POOLSET_3 $POOLSET_4 >> \
	$LOG_TEMP

# Write some data into the pool, hitting all the part files
expect_normal_exit $PMEMOBJCLI$EXESUFFIX -s $WRITE_SCRIPT $POOLSET_4 >> \
	$LOG_TEMP

# Check if correctly written
expect_normal_exit $PMEMOBJCLI$EXESUFFIX -s $READ_SCRIPT $POOLSET_4 >> \
	$LOG_TEMP

# Transform poolset
expect_normal_exit $PMEMPOOL$EXESUFFIX transform $POOLSET_4 $POOLSET_5 >> \
	$LOG_TEMP

# Check if correctly transformed
expect_normal_exit $PMEMOBJCLI$EXESUFFIX -s $READ_SCRIPT $POOLSET_5 >> \
	$LOG_TEMP

# Check metadata by pmempool info
dump_pool_info $POOLSET_5 >> $LOG_TEMP
dump_pool_info $DIR/part00 >> $LOG_TEMP
dump_pool_info $DIR/part01 >> $LOG_TEMP
dump_pool_info $DIR/part02 >> $LOG_TEMP
dump_pool_info $DIR/part03 >> $LOG_TEMP
dump_pool_info $DIR/part10 >> $LOG_TEMP
dump_pool_info $DIR/part11 >> $LOG_TEMP
dump_pool_info $DIR/part12 >> $LOG_TEMP
dump_pool_info $DIR/part13 >> $LOG_TEMP

mv $LOG_TEMP $LOG
check

pass
