btrfs-progs: introduce TEST_TOP and INTERNAL_BIN for tests
[platform/upstream/btrfs-progs.git] / tests / fsck-tests.sh
1 #!/bin/bash
2 #
3 # loop through all of our bad images and make sure fsck repairs them properly
4
5 LANG=C
6 SCRIPT_DIR=$(dirname $(readlink -f "$0"))
7 INTERNAL_BIN=$(readlink -f "$SCRIPT_DIR/../")
8 TEST_TOP=$(readlink -f "$SCRIPT_DIR/../tests/")
9 TOP=$(readlink -f "$SCRIPT_DIR/../")
10 if ! [ -f "$TOP/btrfs" ];then
11         TOP=$(dirname `which btrfs`)
12 fi
13 TEST_DEV=${TEST_DEV:-}
14 RESULTS="$TEST_TOP/fsck-tests-results.txt"
15 IMAGE="$TEST_TOP/test.img"
16
17 source "$TEST_TOP/common"
18
19 export INTERNAL_BIN
20 export TEST_TOP
21 export TOP
22 export RESULTS
23 export LANG
24 export IMAGE
25 export TEST_DEV
26
27 rm -f "$RESULTS"
28
29 # test rely on corrupting blocks tool
30 check_prereq btrfs-corrupt-block
31 check_prereq btrfs-image
32 check_prereq btrfs
33 check_prereq btrfstune
34 check_kernel_support
35
36 run_one_test() {
37         local testname
38
39         testname="$1"
40         echo "    [TEST/fsck]   $(basename $testname)"
41         cd "$testname"
42         echo "=== Entering $testname" >> "$RESULTS"
43         if [ -x test.sh ]; then
44                 # Type 2
45                 ./test.sh
46                 if [ $? -ne 0 ]; then
47                         if [[ $TEST_LOG =~ dump ]]; then
48                                 cat "$RESULTS"
49                         fi
50                         _fail "test failed for case $(basename $testname)"
51                 fi
52         else
53                 # Type 1
54                 check_all_images
55         fi
56         cd "$TEST_TOP"
57 }
58
59 # Each dir contains one type of error for btrfsck test.
60 # Each dir must be one of the following 2 types:
61 # 1) Only btrfs-image dump
62 #    Only contains one or several btrfs-image dumps (.img)
63 #    Each image will be tested by generic test routine
64 #    (btrfsck --repair and btrfsck).
65 #    This is for case that btree-healthy images.
66 # 2) Custom test script
67 #    This dir contains test.sh which will do custom image
68 #    generation/check/verification.
69 #    This is for case btrfs-image can't dump or case needs extra
70 #    check/verify
71
72 for i in $(find "$TEST_TOP/fsck-tests" -maxdepth 1 -mindepth 1 -type d  \
73         ${TEST:+-name "$TEST"} | sort)
74 do
75         run_one_test "$i"
76 done