#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016-2018, Intel Corporation
#
#
# pmempool_sync/TEST5 -- test for checking pmempool sync;
#                        pmem/issues#275
#

. ../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=$DIR/pool0.set

# Create poolset file
create_poolset $POOLSET \
	20M:$DIR/testfile00:x \
	30M:$DIR/testfile01:x \
	R \
	30M:$DIR/testfile10:x \
	20M:$DIR/testfile11:x

# Create a poolset
expect_normal_exit $PMEMPOOL$EXESUFFIX create --layout=$LAYOUT obj $POOLSET
cat $LOG >> $LOG_TEMP

# Delete the second part in the primary replica
rm -f $DIR/testfile01

# Synchronize replicas
expect_normal_exit $PMEMPOOL$EXESUFFIX sync $POOLSET >> $LOG_TEMP

# Check if restored part file have the same permissions as other parts
stat $STAT_PERM $DIR/testfile00 >> $LOG_TEMP
stat $STAT_PERM $DIR/testfile01 >> $LOG_TEMP

# Delete the first part in the second replica
rm -f $DIR/testfile10

# Change permissions of all the files
chmod 600 $DIR/testfile*

# Synchronize replicas
expect_normal_exit $PMEMPOOL$EXESUFFIX sync $POOLSET >> $LOG_TEMP

# Check if restored part file have the same permissions as other parts
stat $STAT_PERM $DIR/testfile10 >> $LOG_TEMP
stat $STAT_PERM $DIR/testfile11 >> $LOG_TEMP

mv $LOG_TEMP $LOG
check

pass
