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