]> git.ipfire.org Git - thirdparty/util-linux.git/blame - tests/functions.sh
docs: clean up TODO file and add a new resuest for 2.14
[thirdparty/util-linux.git] / tests / functions.sh
CommitLineData
e83446da 1
5db4f8e3
KZ
2TS_OUTDIR="output"
3TS_DIFFDIR="diff"
4TS_EXPECTEDDIR="expected"
5TS_INPUTDIR="input"
57a917d6 6TS_VERBOSE="no"
e83446da 7
b30cd7ee
KZ
8function ts_skip {
9 echo " IGNORE ($1)"
3dfa278e
KZ
10 if [ -n "$2" ] && [ -b "$2" ]; then
11 ts_device_deinit "$2"
12 fi
b30cd7ee
KZ
13 exit 0
14}
15
09892fb6
KZ
16function ts_skip_nonroot {
17 if [ $UID != 0 ]; then
18 ts_skip "not root permissions"
19 fi
20}
21
05de8126
KZ
22function ts_failed {
23 if [ x"$1" == x"" ]; then
24 echo " FAILED ($TS_NAME)"
25 else
26 echo " FAILED ($1)"
27 fi
28 exit 1
29}
30
31function ts_ok {
32 if [ x"$1" == x"" ]; then
33 echo " OK"
34 else
35 echo " OK ($1)"
36 fi
37 exit 0
38}
39
57a917d6
KZ
40function ts_log {
41 echo "$1" >> $TS_OUTPUT
42 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
43}
44
e83446da 45function ts_init {
e9ce5ccc 46 export LANG="en_US.UTF-8":
e83446da
KZ
47 TS_NAME=$(basename $0)
48 if [ ! -d $TS_OUTDIR ]; then
49 mkdir -p $TS_OUTDIR
50 fi
51 if [ ! -d $TS_DIFFDIR ]; then
52 mkdir -p $TS_DIFFDIR
53 fi
57a917d6 54 [ "$1" == "--verbose" ] && TS_VERBOSE="yes"
e83446da
KZ
55 TS_OUTPUT="$TS_OUTDIR/$TS_NAME"
56 TS_DIFF="$TS_DIFFDIR/$TS_NAME"
57 TS_EXPECTED="$TS_EXPECTEDDIR/$TS_NAME"
e9ce5ccc 58 TS_INPUT="$TS_INPUTDIR/$TS_NAME"
c98825ac 59 TS_MOUNTPOINT="$(pwd)/$TS_OUTDIR/${TS_NAME}_mnt"
66822df3
KZ
60 TS_BLKIDCACHE=""
61 TS_HAS_VOLUMEID="no"
62
63 ldd $TS_CMD_MOUNT | grep -q 'libvolume_id' &> /dev/null
64 if [ "$?" == "0" ]; then
65 TS_HAS_VOLUMEID="yes"
66 fi
67 if [ "$TS_HAS_VOLUMEID" != "yes" ]; then
68 if [ -f "/etc/blkid/blkid.tab" ]; then
69 TS_BLKIDCACHE="/etc/blkid/blkid.tab"
70 elif [ -f "/etc/blkid.tab" ]; then
71 TS_BLKIDCACHE="/etc/blkid.tab"
72 fi
73 fi
e83446da 74
97cdb3cb 75 rm -f $TS_OUTPUT
3dfa278e 76 touch $TS_OUTPUT
97cdb3cb 77
e83446da
KZ
78 printf "%15s: %-25s ..." "$TS_COMPONENT" "$TS_DESC"
79}
80
81function ts_finalize {
82 local res=0
83
84 if [ -s $TS_EXPECTED ]; then
85 if [ -s $TS_OUTPUT ]; then
86 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
87 if [ -s $TS_DIFF ]; then
88 res=1
89 fi
90 else
e9ce5ccc 91 res=1
e83446da 92 fi
97cdb3cb
KZ
93 else
94 echo " IGNORE (expected output undefined)"
95 exit 0
e83446da
KZ
96 fi
97 if [ $res -eq 0 ]; then
05de8126 98 ts_ok $1
e83446da 99 else
05de8126 100 ts_failed $1
e83446da
KZ
101 fi
102}
103
3dfa278e 104function ts_die {
57a917d6 105 ts_log "$1"
3dfa278e
KZ
106 if [ -n "$2" ] && [ -b "$2" ]; then
107 ts_device_deinit "$2"
c98825ac 108 ts_fstab_clean # for sure...
3dfa278e
KZ
109 fi
110 ts_finalize
111}
112
df7e52d7 113function ts_device_init {
3dfa278e
KZ
114 local IMAGE="$TS_OUTDIR/$TS_NAME.img"
115 local IMAGE_RE=$( echo "$IMAGE" | sed 's:/:\\/:g' )
116 local DEV=""
05de8126 117
66822df3
KZ
118 if [ "$TS_HAS_VOLUMEID" != "yes" ] && [ -f "$TS_BLKIDCACHE" ]; then
119 cp $TS_BLKIDCACHE $OUTDIR/$TS_NAME.cache
120 fi
121
df7e52d7 122 dd if=/dev/zero of="$IMAGE" bs=1M count=5 &> /dev/null
05de8126 123
df7e52d7 124 $TS_CMD_LOSETUP -f "$IMAGE" 2>&1 >> $TS_OUTPUT
3dfa278e 125 DEV=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' )
df7e52d7 126
3dfa278e
KZ
127 if [ -z "$DEV" ]; then
128 ts_device_deinit $DEV
df7e52d7
KZ
129 return 1 # error
130 fi
131
3dfa278e 132 echo $DEV
df7e52d7
KZ
133 return 0 # succes
134}
135
136function ts_device_deinit {
3dfa278e
KZ
137 local DEV="$1"
138
139 if [ -b "$DEV" ]; then
140 $TS_CMD_UMOUNT "$DEV" &> /dev/null
141 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
66822df3
KZ
142 if [ "$TS_HAS_VOLUMEID" != "yes" ] && [ -f "$TS_BLKIDCACHE" ] && [ -s "$OUTDIR/$TS_NAME.cache" ]; then
143 # We have to remove the device from cache otherwise
144 # libblkid will reuse cached information. The cache
145 # refresh time is 2 seconds -- that's too long. We
146 # re-use the same device more quickly. --kzak
147 mv -f $OUTDIR/$TS_NAME.cache $TS_BLKIDCACHE
148 fi
df7e52d7
KZ
149 fi
150}
064b8c38 151
3dfa278e 152function ts_udev_dev_support {
66822df3 153 if [ "$TS_HAS_VOLUMEID" == "yes" ] && [ ! -L "/dev/disk/$1/$2" ]; then
064b8c38
KZ
154 return 1
155 fi
156 return 0
157}
158
3f5bda01
KZ
159function ts_uuid_by_devname {
160 local DEV="$1"
161 local UUID=""
162 if [ -x "$TS_ECMD_BLKID" ]; then
163 UUID=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "UUID" $DEV | sed 's/.*UUID="//g; s/"//g')
164 elif [ -x "$TS_ECMD_VOLID" ]; then
165 UUID=$($TS_ECMD_VOLID -u $DEV)
166 fi
167 echo $UUID
168}
169
170function ts_label_by_devname {
171 local DEV="$1"
172 local TYPE=""
173 if [ -x "$TS_ECMD_BLKID" ]; then
174 LABEL=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "LABEL" $DEV | sed 's/.*LABEL="//g; s/"//g')
175 elif [ -x "$TS_ECMD_VOLID" ]; then
176 LABEL=$($TS_ECMD_VOLID -l $DEV)
177 fi
178 echo $LABEL
179}
180
181function ts_fstype_by_devname {
182 local DEV="$1"
183 local TYPE=""
184 if [ -x "$TS_ECMD_BLKID" ]; then
185 TYPE=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "TYPE" $DEV | sed 's/.*TYPE="//g; s/"//g')
186 elif [ -x "$TS_ECMD_VOLID" ]; then
187 TYPE=$($TS_ECMD_VOLID -t $DEV)
188 fi
189 echo $TYPE
190}
191
192function ts_device_has {
193 local TAG="$1"
194 local VAL="$2"
195 local DEV="$3"
3dfa278e 196 local vl=""
3f5bda01
KZ
197
198 case $TAG in
199 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
200 "LABEL") vl=$(ts_label_by_devname $DEV);;
201 "UUID") vl=$(ts_uuid_by_devname $DEV);;
202 *) return 1;;
203 esac
204
205 if [ "$vl" == "$VAL" ]; then
206 return 0
207 fi
208 return 1
209}
3dfa278e
KZ
210
211function ts_device_has_uuid {
212 ts_uuid_by_devname "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$'
213 return $?
214}
215
216function ts_swapoff {
217 local DEV="$1"
218
219 # swapoff doesn't exist in build tree
220 if [ ! -x "$TS_CMD_SWAPOFF" ]; then
221 ln -sf $TS_CMD_SWAPON $TS_CMD_SWAPOFF
222 REMSWAPOFF="true"
223 fi
224 $TS_CMD_SWAPOFF $DEV 2>&1 >> $TS_OUTPUT
225 if [ -n "$REMSWAPOFF" ]; then
226 rm -f $TS_CMD_SWAPOFF
227 fi
228}
c98825ac
KZ
229
230function ts_fstab_open {
231 echo "# <!-- util-linux-ng test entry" >> /etc/fstab
232}
233
234function ts_fstab_close {
235 echo "# -->" >> /etc/fstab
236}
237
238function ts_fstab_addline {
239 local SPEC="$1"
240 local MNT=${2:-"$TS_MOUNTPOINT"}
241 local FS=${3:-"auto"}
242 local OPT=${4:-"default"}
243
244 echo "$SPEC $MNT $FS defaults 0 0" >> /etc/fstab
245}
246
247function ts_fstab_add {
248 ts_fstab_open
249 ts_fstab_addline "$*"
250 ts_fstab_close
251}
252
253function ts_fstab_clean {
254 sed --in-place "
255/# <!-- util-linux-ng/!b
256:a
257/# -->/!{
258 N
259 ba
260}
261s/# <!-- util-linux-ng.*-->//;
262/^$/d" /etc/fstab
263}
264