#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2015-2019, Intel Corporation

#
# src/test/util_poolset/TEST0 -- unit test for util_pool_create()
#

. ../unittest/unittest.sh

require_test_type medium

require_fs_type non-pmem

exclude_ppc64

setup

export TEST_LOG_LEVEL=4
export TEST_LOG_FILE=./test$UNITTEST_NUM.log

MIN_POOL=$((4 * 1024 * 1024)) # 4MiB

mkdir -p $DIR/subdir1
mkdir -p $DIR/subdir2

# non-existing files (delete on failure)
create_poolset $DIR/testset1 $MIN_POOL:$DIR/testfile11:x # pass
create_poolset $DIR/testset2 $MIN_POOL:$DIR/testfile21:x \
	$MIN_POOL:$DIR/testfile22:x # pass
create_poolset $DIR/testset3 $MIN_POOL:$DIR/testfile31:x \
	$MIN_POOL:$DIR/testfile32:z:$MIN_POOL # pass
create_poolset $DIR/testset4 1MB:$DIR/testfile41:x \
	$MIN_POOL:$DIR/testfile42:x # fail - part1 too small
create_poolset $DIR/testset5 $MIN_POOL:$DIR/testfile51:x \
	1MB:$DIR/testfile52:x # fail - part2 too small
create_poolset $DIR/testset6 $MIN_POOL:$DIR/testfile61:x \
	$MIN_POOL:$DIR/nodir/testfile62:x # fail - part2 non-existing dir
create_poolset $DIR/testset7 $MIN_POOL:$DIR/testfile71:x \
	$MIN_POOL:/proc/testfile72:x # fail - part2 can't write to dir
create_poolset $DIR/testset8 $MIN_POOL:$DIR/testfile81:x \
	1G:$DIR/testfile82:x # fail - part2 no space left

# exiting files (do not delete on failure)
create_poolset $DIR/testset10 $MIN_POOL:$DIR/testfile101:z \
	$MIN_POOL:$DIR/testfile102:z:$MIN_POOL:0444 # fail - part2 read-only
create_poolset $DIR/testset11 $MIN_POOL:$DIR/testfile111:z:$MIN_POOL \
	$MIN_POOL:$DIR/testfile112:z 1M:$DIR/testfile113:z:$MIN_POOL # fail - part3 too small
create_poolset $DIR/testset12 $MIN_POOL:$DIR/testfile121:z:$MIN_POOL \
	12K:$DIR/testfile122:z:12K # fail - part2 too small
create_poolset $DIR/testset13 12K:$DIR/testfile131:z:12K \
	$MIN_POOL:$DIR/testfile132:z:$MIN_POOL # fail - part2 too small
create_poolset $DIR/testset14 $MIN_POOL:$DIR/testfile141:z:$MIN_POOL \
	8M:$DIR/testfile142:z:$MIN_POOL # fail - part2 size doesn't match
create_poolset $DIR/testset15 $MIN_POOL:$DIR/testfile151:z:$MIN_POOL \
	$MIN_POOL:$DIR/testfile152:z:3M # fail - part2 size doesn't match
create_poolset $DIR/testset16 1M:$DIR/testfile161:z:1M \
	$MIN_POOL:$DIR/testfile162:z:$MIN_POOL # fail - part1 too small
create_poolset $DIR/testset17 2M:$DIR/testfile171:z \
	2M:$DIR/testfile172:z 2M:$DIR/testfile173:z # pass
create_poolset $DIR/testset18 2M:$DIR/subdir1/testfile181:z \
	3M:$DIR/subdir2/testfile182:z # pass

# mixed (some files exist, some don't)
create_poolset $DIR/testset20 $MIN_POOL:$DIR/testfile201:x \
	$MIN_POOL:$DIR/testfile202:n # fail - part2 non-zeroed file
create_poolset $DIR/testset21 $MIN_POOL:$DIR/testfile21:x \
	$MIN_POOL:$DIR/testfile22:x # fail - part2 valid hdr (reuse file from case #2)
create_poolset $DIR/testset22 $MIN_POOL:$DIR/testfile221:x \
	$MIN_POOL:$DIR/testfile222:h # fail - part2 zeroed hdr (rest is non-zeroed)
create_poolset $DIR/testset23 $MIN_POOL:$DIR/testfile231:x \
	$MIN_POOL:$DIR/testfile232:z # fail - can't read set file
create_poolset $DIR/testset24 $MIN_POOL:$DIR/testfile241:x $MIN_POOL:$DIR/testfile242:x \
	r 8M:$DIR/testfile243:x r $MIN_POOL:$DIR/testfile244:x \
	$MIN_POOL:$DIR/testfile245:x # pass - replicas
create_poolset $DIR/testset25 $MIN_POOL:$DIR/testfile251:z $MIN_POOL:$DIR/testfile252:x \
	r 1M:$DIR/testfile253:z # fail - replica too small
create_poolset $DIR/testset26 $MIN_POOL:$DIR/testfile261:z 2M:$DIR/testfile262:z \
	r 8M:$DIR/testfile263 r 6M:$DIR/testfile264 # pass - pmem/non-pmem

expect_normal_exit ./util_poolset$EXESUFFIX c $MIN_POOL \
	$DIR/testset0 $DIR/testset1\
	$DIR/testset2 $DIR/testset3\
	$DIR/testset4 $DIR/testset5\
	$DIR/testset6\
	-mo:/proc/testfile72 $DIR/testset7\
	-mf:$((1024*1024*1024)) $DIR/testset8\
	-mo:$DIR/testfile102 $DIR/testset10\
	$DIR/testset11\
	$DIR/testset12 $DIR/testset13\
	$DIR/testset14 $DIR/testset15\
	$DIR/testset18\
	$DIR/testset20 $DIR/testset21\
	$DIR/testset22\
	-mo:$DIR/testset23 $DIR/testset23\
	$DIR/testset24 $DIR/testset25\
	-mp:$((6*1024*1024)) $DIR/testset26

check_files $DIR/testfile11\
	$DIR/testfile21 $DIR/testfile22\
	$DIR/testfile31 $DIR/testfile32\
	$DIR/testfile101 $DIR/testfile102\
	$DIR/testfile111 $DIR/testfile112\
	$DIR/testfile121 $DIR/testfile122\
	$DIR/testfile131 $DIR/testfile132\
	$DIR/testfile141 $DIR/testfile142\
	$DIR/testfile151 $DIR/testfile152\
	$DIR/testfile161 $DIR/testfile162\
	$DIR/testfile171 $DIR/testfile172 $DIR/testfile173\
	$DIR/subdir1/testfile181 $DIR/subdir2/testfile182\
	$DIR/testfile202\
	$DIR/testfile221 $DIR/testfile222\
	$DIR/testfile232\
	$DIR/testfile241 $DIR/testfile242 $DIR/testfile243 $DIR/testfile244 $DIR/testfile245\
	$DIR/testfile251 $DIR/testfile253\
	$DIR/testfile261 $DIR/testfile262 $DIR/testfile263 $DIR/testfile264

check_no_files $DIR/testfile41 $DIR/testfile42\
	$DIR/testfile51 $DIR/testfile52\
	$DIR/testfile61\
	$DIR/testfile81 $DIR/testfile82\
	$DIR/testfile201\
	$DIR/testfile231\
	$DIR/testfile252

$GREP "<1>" $TEST_LOG_FILE | sed -e "s/^.*\][ ]*//g" > ./grep$UNITTEST_NUM.log

check

pass
