btrfs-progs: New btrfsck test infrastructure
[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 # It's GPL, same as everything else in this tree.
6 #
7
8 unset top
9 unset LANG
10 LANG=C
11 script_dir=$(dirname $(realpath $0))
12 top=$(realpath $script_dir/../)
13 TEST_DEV=${TEST_DEV:-}
14 TEST_MNT=${TEST_MNT:-$top/tests/mnt}
15 RESULT="$top/tests/fsck-tests-results.txt"
16
17 source $top/tests/common
18
19 # Allow child test to use $top and $RESULT
20 export top
21 export RESULT
22 # For custom script needs to verfiy recovery
23 export TEST_MNT
24 export LANG
25
26 rm -f $RESULT
27 mkdir -p $TEST_MNT || _fail "unable to create mount point on $TEST_MNT"
28
29 # test rely on corrupting blocks tool
30 check_prereq btrfs-corrupt-block
31 check_prereq btrfs-image
32 check_prereq btrfs
33
34 # Each dir contains one type of error for btrfsck test.
35 # Each dir must be one of the following 2 types:
36 # 1) Only btrfs-image dump
37 #    Only contains one or several btrfs-image dumps (.img)
38 #    Each image will be tested by generic test routine
39 #    (btrfsck --repair and btrfsck).
40 #    This is for case that btree-healthy images.
41 # 2) Custom test script
42 #    This dir contains test.sh which will do custom image
43 #    generation/check/verification.
44 #    This is for case btrfs-image can't dump or case needs extra
45 #    check/verify
46
47 for i in $(find $top/tests/fsck-tests -maxdepth 1 -mindepth 1 -type d | sort)
48 do
49         echo "     [TEST]    $(basename $i)"
50         cd $i
51         if [ -x test.sh ]; then
52                 # Type 2
53                 ./test.sh
54                 if [ $? -ne 0 ]; then
55                         _fail "test failed for case $(basename $i)"
56                 fi
57         else
58                 # Type 1
59                 check_all_images `pwd`
60         fi
61         cd $top
62 done
63
64 if [ -z $TEST_DEV ] || [ -z $TEST_MNT ];then
65         echo "     [NOTRUN] extent tree rebuild"
66         exit 0
67 fi
68
69 # test whether fsck can rebuild a corrupted extent tree
70 test_extent_tree_rebuild()
71 {
72         echo "     [TEST]    extent tree rebuild"
73         $top/mkfs.btrfs -f $TEST_DEV >> /dev/null 2>&1 || _fail "fail to mkfs"
74
75         run_check mount $TEST_DEV $TEST_MNT
76         cp -aR /lib/modules/`uname -r`/ $TEST_MNT 2>&1
77
78         for i in `seq 1 100`;do
79                 $top/btrfs sub snapshot $TEST_MNT \
80                         $TEST_MNT/snapaaaaaaa_$i >& /dev/null
81         done
82         run_check umount $TEST_DEV
83
84         # get extent root bytenr
85         extent_root_bytenr=`$top/btrfs-debug-tree -r $TEST_DEV | \
86                             grep extent | awk '{print $7}'`
87         if [ -z $extent_root_bytenr ];then
88                 _fail "fail to get extent root bytenr"
89         fi
90
91         # corrupt extent root node block
92         run_check $top/btrfs-corrupt-block -l $extent_root_bytenr \
93                 -b 4096 $TEST_DEV
94
95         $top/btrfs check $TEST_DEV >& /dev/null && \
96                         _fail "btrfs check should detect failure"
97         run_check $top/btrfs check --init-extent-tree $TEST_DEV
98         run_check $top/btrfs check $TEST_DEV
99 }
100
101 test_extent_tree_rebuild