btrfs-progs: tests: common: add helper run_check_stdout
[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 $RESULTS
9         exit 1
10 }
11
12 _not_run()
13 {
14         echo "    [NOTRUN] $*"
15         exit 0
16 }
17
18 run_check()
19 {
20         echo "############### $@" >> $RESULTS 2>&1
21         "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
22 }
23
24 # same as run_check but the stderr+stdout output is duplicated on stdout and
25 # can be processed further
26 run_check_stdout()
27 {
28         echo "############### $@" >> $RESULTS 2>&1
29         "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
30 }
31
32 check_prereq()
33 {
34         if ! [ -f $TOP/$1 ]; then
35                 _fail "Failed prerequisities: $1";
36         fi
37 }
38
39 check_image()
40 {
41         local image
42
43         image=$1
44         echo "testing image $(basename $image)" >> $RESULTS
45         $TOP/btrfs check $image >> $RESULTS 2>&1
46         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
47
48         run_check $TOP/btrfs check --repair $image
49         run_check $TOP/btrfs check $image
50 }
51
52 # Process all image dumps in a given directory,
53 # - raw btrfs filesystem images, suffix .raw
54 # - dtto compressed by XZ, suffix .raw.xz
55 # - meta-dump images with suffix .img
56 # - dtto compressed by XZ, suffix .img.xz
57 check_all_images()
58 {
59         dir=$1
60         for image in $(find $dir \( -iname '*.img' -o   \
61                                 -iname '*.img.xz' -o    \
62                                 -iname '*.raw' -o       \
63                                 -iname '*.raw.xz' \) | sort)
64         do
65                 cleanme=
66                 case "$image" in
67                 *.img)
68                         rm -f $image.restored
69                         : ;;
70                 *.img.xz)
71                         xz --decompress --keep "$image" || \
72                                 _fail "failed to decompress image $image"
73                         image=${image%%.xz}
74                         rm -f $image.restored
75                         cleanme=$image
76                         ;;
77                 *.raw)
78                         cp --sparse=auto $image $image.restored
79                         ;;
80                 *.raw.xz)
81                         xz --decompress --keep "$image" || \
82                                 _fail "failed to decompress image $image"
83                         image=${image%%.xz}
84                         mv "$image" "$image".restored
85                         ;;
86                 esac
87
88                 if ! [ -f $image.restored ]; then
89                         echo "restoring image $(basename $image)" >> $RESULTS
90                         $TOP/btrfs-image -r $image $image.restored || \
91                                 _fail "failed to restore image $image"
92                 fi
93
94                 check_image $image.restored
95
96                 rm -f $image.restored $cleanme
97         done
98 }
99
100 # some tests need to mount the recovered image and do verifications call
101 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
102 # needs to fail otherwise; using sudo by default for now
103 SUDO_HELPER=
104 NEED_SUDO_VALIDATE=unknown
105 export SUDO_HELPER
106 export NEED_SUDO_VALIDATE
107 root_helper()
108 {
109         if [ $UID -eq 0 ]; then
110                 "$@"
111         else
112                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
113                         sudo -v -n &>/dev/null || \
114                                 _not_run "Need to validate sudo credentials"
115                         sudo -n "$@"
116                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
117                         sudo -n /bin/true &> /dev/null || \
118                                 _not_run "Need to validate sudo user settings"
119                         sudo -n "$@"
120                 else
121                         # should not happen
122                         _not_run "Need to validate root privileges"
123                 fi
124         fi
125 }
126
127 setup_root_helper()
128 {
129         if [ $UID -eq 0 ]; then
130                 return
131         fi
132
133         # Test for old sudo or special settings, which make sudo -v fail even
134         # if user setting is NOPASSWD
135         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
136
137         # Newer sudo or default sudo setting
138         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
139
140         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
141                 _not_run "Need to validate root privileges"
142         fi
143         SUDO_HELPER=root_helper
144 }