#!/bin/bash -e
#
# Copyright 2016-2017, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#
#     * Neither the name of the copyright holder nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
# src/test/rpmem_basic/TEST8 -- unit test for rpmem create request
#                               in 2-to-1 configuration
#
export UNITTEST_NAME=rpmem_basic/TEST8
export UNITTEST_NUM=8

# standard unit test setup
. ../unittest/unittest.sh

require_test_type medium

setup

. setup2to1.sh

for node in 1 2; do

	create_poolset $DIR/pool0.set  8M:${PART_DIR[${node}]}/pool0.part0 8M:${PART_DIR[${node}]}/pool0.part1
	create_poolset $DIR/pool1.set 16M:${PART_DIR[${node}]}/pool1.part0
	create_poolset $DIR/pool2.set 16M:${PART_DIR[${node}]}/pool2.part0
	create_poolset $DIR/pool3.set  8M:${PART_DIR[${node}]}/pool3.part0 8M:${PART_DIR[${node}]}/pool3.part1
	# Pool set file with duplicated part file
	create_poolset $DIR/pool4.set  8M:${PART_DIR[${node}]}/pool3.part0 8M:${PART_DIR[${node}]}/pool4.part1
	# Pool set file for test of read operation
	create_poolset $DIR/pool5.set  8M:${PART_DIR[${node}]}/pool5.part0 8M:${PART_DIR[${node}]}/pool5.part1

	cp $DIR/pool0.set $DIR/pool0r.set

	run_on_node ${REPLICA[${node}]} "rm -rf $POOLS_DIR $POOLS_PART && mkdir -p $POOLS_DIR/subdir && mkdir -p $POOLS_PART"

	copy_files_to_node ${REPLICA[${node}]} $POOLS_DIR $DIR/{pool0.set,pool0r.set,pool1.set,pool3.set,pool4.set,pool5.set}
	copy_files_to_node ${REPLICA[${node}]} $POOLS_DIR/subdir/ $DIR/pool2.set

	# Remove read permission
	run_on_node ${REPLICA[${node}]} chmod a-r $POOLS_DIR/pool0r.set

	# Prepare pool5 for test of read and persist operations
	expect_normal_exit run_on_node ${node} ./rpmem_basic$EXESUFFIX\
		test_create 3 pool5.set ${REP_ADDR[${node}]} mem 8M test_close 3
	expect_normal_exit run_on_node ${REPLICA[${node}]} ./rpmem_basic$EXESUFFIX\
		fill_pool $POOLS_DIR/pool5.set 1234

	ARGS=""
	#
	# CREATE
	#
	# Create pool from non-existing pool set file
	ARGS="$ARGS test_create 0 poolX.set ${REP_ADDR[${node}]} pool 8M"
	# Create pool from pool set file which can't be read
	ARGS="$ARGS test_create 0 pool0r.set ${REP_ADDR[${node}]} pool 8M"
	# Create too big pool
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 32M"
	# Create pool and close it - local pool from file
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 8M test_close 0"
	# Create already existing pool
	ARGS="$ARGS test_create 0 pool0.set ${REP_ADDR[${node}]} pool 8M"
	# Create pool and close it - local pool from memory
	ARGS="$ARGS test_create 0 pool1.set ${REP_ADDR[${node}]} mem 8M test_close 0"
	# Create pool from pool set with subdirectory
	ARGS="$ARGS test_create 0 subdir/pool2.set ${REP_ADDR[${node}]} mem 8M test_close 0"
	# Create pool but don't close it immediately
	ARGS="$ARGS test_create 0 pool3.set ${REP_ADDR[${node}]} mem 8M"
	# Create already created and not closed pool
	ARGS="$ARGS test_create 1 pool3.set ${REP_ADDR[${node}]} mem 8M"
	# Create pool from pool set which has duplicated part file
	ARGS="$ARGS test_create 2 pool4.set ${REP_ADDR[${node}]} mem 8M"
	# Close previously created pool
	ARGS="$ARGS test_close 0"

	#
	# OPEN
	#
	# Open pool from non-existing pool set file
	ARGS="$ARGS test_open 0 poolX.set ${REP_ADDR[${node}]} pool 8M init"
	# Open pool from pool set file which can't be read
	ARGS="$ARGS test_open 0 pool0r.set ${REP_ADDR[${node}]} pool 8M init"
	# Open too big pool
	ARGS="$ARGS test_open 0 pool0.set ${REP_ADDR[${node}]} pool 32M init"
	# Open pool and close it - local pool from file
	ARGS="$ARGS test_open 0 pool0.set ${REP_ADDR[${node}]} pool 8M init test_close 0"
	# Open pool and close it - local pool from memory
	ARGS="$ARGS test_open 0 pool1.set ${REP_ADDR[${node}]} mem 8M init test_close 0"
	# Open pool from pool set with subdirectory
	ARGS="$ARGS test_open 0 subdir/pool2.set ${REP_ADDR[${node}]} pool 8M init test_close 0"
	# Open pool but don't close it immediately
	ARGS="$ARGS test_open 0 pool3.set ${REP_ADDR[${node}]} pool 8M init"
	# Open already opened and not closed pool
	ARGS="$ARGS test_open 1 pool3.set ${REP_ADDR[${node}]} pool 8M init"
	# Open pool from pool set which has duplicated part file
	ARGS="$ARGS test_open 2 pool4.set ${REP_ADDR[${node}]} pool 8M init"
	# Close previously created pool
	ARGS="$ARGS test_close 0"

	#
	# READ & PERSIST
	#
	ARGS="$ARGS test_open 3 pool5.set ${REP_ADDR[${node}]} pool 8M init"
	ARGS="$ARGS test_read 3 1234"
	ARGS="$ARGS test_persist 3 4321 1 1"
	ARGS="$ARGS test_persist 3 4321 8 8"
	ARGS="$ARGS test_close 3"

	ARGS_NODE[${node}]=$ARGS

done

expect_normal_exit run_on_node_background 1 ${PID_FILE[1]} ./rpmem_basic$EXESUFFIX ${ARGS_NODE[1]}
expect_normal_exit run_on_node_background 2 ${PID_FILE[2]} ./rpmem_basic$EXESUFFIX ${ARGS_NODE[2]}

expect_normal_exit wait_on_node 1 ${PID_FILE[1]}
expect_normal_exit wait_on_node 2 ${PID_FILE[2]}

check

pass
