#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# pmempool_sync/TEST28 -- test for sync command with badblocks
#                         - bad blocks in a DAX device
#                         - blocks: offset: 0 length: 16
#
# This test verifies whether pmempool-sync is able to fix bad blocks
# in the first 8kB of the pool (in the pool header) located in a DAX device.
#

. ../unittest/unittest.sh

require_test_type medium
require_build_type debug nondebug

require_badblock_tests_enabled dax_device
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

setup

. ../common_badblock.sh

badblock_test_init dax_device

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

POOLSET=$DIR/testset1
create_poolset $POOLSET AUTO:$FULLDEV:x \
			R \
			8M:$DIR/testfile1:z

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

expect_normal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX c v &>> $LOG"

turn_on_checking_bad_blocks $POOLSET

# inject bad block at offset 0 (the pool header)
# DO NOT CHANGE THIS OFFSET! IT HAS TO BE EQUAL 0!
ndctl_inject_error $NAMESPACE 0 16

expect_bad_blocks $NAMESPACE

expect_abnormal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX v" > /dev/null 2>&1

expect_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX sync -b -v $POOLSET >> $LOG"

print_bad_blocks $NAMESPACE

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

print_bad_blocks $NAMESPACE

expect_normal_exit "$PMEMPOOL$EXESUFFIX info --bad-blocks=yes $POOLSET >> $LOG"

print_bad_blocks $NAMESPACE

expect_normal_exit "$OBJ_VERIFY$EXESUFFIX $POOLSET pmempool$SUFFIX v &>> $LOG"

ndctl_uninject_error $FULLDEV $NAMESPACE 0 16
badblock_test_fini

check

pass
