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