btrfs-progs: tests, use non-interactive sudo helper
[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 }
50
51 # some tests need to mount the recovered image and do verifications call
52 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
53 # needs to fail otherwise; using sudo by default for now
54 sudo=
55 have_root_helper=0
56 export sudo
57 export have_root_helper
58 setup_root_helper()
59 {
60         if [ $UID != 0 ]; then
61                 sudo="sudo --non-interactive"
62         fi
63         have_root_helper=1
64 }