#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# pmempool_check/TEST31 -- test for check command with badblocks
#                          - bad blocks in the regular file
#

. ../unittest/unittest.sh

require_test_type medium
require_build_type debug nondebug

require_badblock_tests_enabled block_device
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

setup

. ../common_badblock.sh

MOUNT_DIR="$DIR/mnt-pmem"
badblock_test_init block_device $MOUNT_DIR

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

POOLSET=$DIR/testset1
create_poolset $POOLSET 10M:$DIR/testfile0:z 10M:$MOUNT_DIR/testfile1:z 10M:$DIR/testfile2:z \
			R 30M:$DIR/testfile3:z

expect_normal_exit $PMEMPOOL$EXESUFFIX rm $POOLSET
expect_normal_exit $PMEMPOOL$EXESUFFIX create obj --layout pmempool$SUFFIX $POOLSET

turn_on_checking_bad_blocks $POOLSET

# inject bad block:
FIRST_SECTOR=$(expect_normal_exit $EXTENTS $MOUNT_DIR/testfile1 -l 0)
ndctl_inject_error $NAMESPACE $FIRST_SECTOR 8

expect_bad_blocks $NAMESPACE

expect_abnormal_exit "$PMEMPOOL$EXESUFFIX check -v $POOLSET >> $LOG"

expect_bad_blocks $NAMESPACE

ndctl_uninject_error $FULLDEV $NAMESPACE $FIRST_SECTOR 8

badblock_test_fini $MOUNT_DIR

check

pass
