btrfs-progs: New btrfsck test infrastructure
[platform/upstream/btrfs-progs.git] / tests / common
1 #!/bin/bash
2 #
3 # Common routines for all tests
4 #
5
6 _fail()
7 {
8         echo "$*" | tee -a $RESULT
9         exit 1
10 }
11
12 run_check()
13 {
14         echo "############### $@" >> $RESULT 2>&1
15         "$@" >> $RESULT 2>&1 || _fail "failed: $@"
16 }
17
18 check_prereq()
19 {
20         if ! [ -f $top/$1 ]; then
21                 _fail "Failed prerequisities: $1";
22         fi
23 }
24
25 check_image()
26 {
27         image=$1
28         echo "testing image $(basename $image)" >> $RESULT
29         $top/btrfs check $image >> $RESULT 2>&1
30         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
31
32         run_check $top/btrfs check --repair $image
33         run_check $top/btrfs check $image
34 }
35
36 check_all_images()
37 {
38         dir=$1
39         for i in $(find $dir -iname '*.img')
40         do
41                 echo "extracting image $(basename $i)" >> $RESULT
42                 $top/btrfs-image -r $i $i.restored || \
43                         _fail "failed to extract image $i"
44
45                 check_image $i.restored
46
47                 rm $i.restored
48         done
49 }