Git init
[external/insserv.git] / tests / suite
1 #
2 # Common test suite definitions, declarations, and functions
3 #
4 set -eC
5 set +o posix
6 unset ${!LC@}
7 export LANG=POSIX
8
9 : ${insserv:=${PWD}/insserv}
10 : ${tmpdir:=${PWD}/root}
11 : ${initddir:=${tmpdir}/etc/init.d}
12 : ${insconf:=${tmpdir}/etc/insserv.conf}
13 : ${overridedir:=${tmpdir}/etc/insserv/override}
14 : ${issuse=""}
15 : ${debug:=""}
16
17 declare -i retval=0
18 declare -i checkfailed=0
19 declare -i testcount=0
20 declare -i testfailed=0
21 finish_test ()
22 {
23     if test 0 -ne $retval ; then
24         echo "error: $testcount test executed, $checkfailed fatal tests failed, $testfailed nonfatal test failed."
25     else
26         echo "success: $testcount test executed, $testfailed nonfatal tests failed."
27     fi
28     rm -rf ${tmpdir}
29     return $retval
30 }
31 trap 'finish_test' EXIT
32
33 mkdir -p $initddir
34
35 if test -n "${issuse}" ; then
36     runlevel_path ()
37     {
38         local -a level=($@)
39         level=(${level[@]/%s/S})
40         level=(${level[@]/#/rc})
41         level=(${level[@]/rc[bB]*/boot})
42         printf "${initddir}/%s.d\n" ${level[@]}
43     }
44     list_rclinks()
45     {
46         pushd $initddir/ &> /dev/null
47         echo ${initddir##*/}:
48         ls ${1+"$@"} *
49         popd &> /dev/null
50     }
51     cat <<-'EOF' > $insconf
52         $local_fs       boot.localfs +boot.crypto
53         $network        network
54         $named          +named +dnsmasq +lwresd $network
55         $remote_fs      $local_fs +nfs +smbfs
56         $syslog         syslog
57         $portmap        portmap
58         $time           boot.clock +ntp
59         <interactive>   boot.crypto boot.clock boot.localfs boot.rootfsck apache apache2 kdump ntp
60         EOF
61 else
62     runlevel_path ()
63     {
64         printf "${initddir}/../rc%s.d\n" ${1+"$@"}
65     }
66     list_rclinks()
67     {
68         pushd $initddir/../ &> /dev/null
69         ls ${1+"$@"} *
70         popd &> /dev/null
71     }
72     cat <<-'EOF' > $insconf
73         $local_fs       +mountall +umountfs
74         $network        +networking +ifupdown
75         $named          +named +dnsmasq +lwresd +bind9 $network
76         $remote_fs      $local_fs +mountnfs +mountnfs-bootclean +umountnfs +sendsigs
77         $syslog         +syslog +sysklogd
78         $portmap        portmap
79         $time           hwclock
80         <interactive>   udev mountdevsubfs checkroot checkfs console-screen
81         EOF
82 fi
83 chmod u+w,a+r $insconf
84
85 insserv_reg ()
86 {
87     script=$(printf "${initddir}/%s\n" ${1+"$@"})
88     $insserv $debug -c $insconf -p $initddir -o $overridedir $script
89 }
90
91 insserv_del ()
92 {
93     script=$(printf "${initddir}/%s\n" ${1+"$@"})
94     $insserv $debug -c $insconf -p $initddir -o $overridedir -r $script
95 }
96
97 relpath ()
98 {
99     local OLDIFS IFS
100     local -a orgwords
101     local -a locwords
102     local -i relp=0
103     local -i deep=0
104     local p l
105     local path=""
106
107     OLDIFS="$IFS"; IFS=/
108     eval orgwords=(${1// /\\\\ }) ; shift
109     eval locwords=(${1// /\\\\ }) ; shift
110     IFS="$OLDIFS"
111     unset OLDIFS
112
113     deep=0
114     relp=0
115     for l in "${locwords[@]}" ; do
116         if test "$l" = ".." ; then
117             ((deep++))
118             continue
119         elif test $deep -gt 0 ; then
120             while ((deep-- > 0)) ; do
121                 unset locwords[$((relp+deep))]
122                 (((relp-1)-deep < 0)) || unset locwords[$(((relp-1)-deep))]
123             done
124             continue
125         fi
126         ((relp++))
127     done
128     locwords=(${locwords[@]})
129
130     deep=0
131     relp=0
132     for p in "${orgwords[@]}" ; do
133         if test "$p" = ".." ; then
134             ((deep++))
135             continue
136         elif test $deep -gt 0 ; then
137             while ((deep-- > 0)) ; do
138                 unset orgwords[$((relp+deep))]
139                 (((relp-1)-deep < 0)) || unset orgwords[$(((relp-1)-deep))]
140             done
141             continue
142         fi
143         ((relp++))
144     done
145     orgwords=(${orgwords[@]})
146
147     deep=0
148     relp=0
149     for p in "${orgwords[@]}" ; do
150         eval l="\${locwords[$((deep++))]}"
151         if test "$l" != "$p" -o $relp -ne 0 ; then
152             ((relp++))
153             path="${path}/$p"
154             test -n "$l" || continue
155                 if test $relp -eq 1 ; then
156                     path="..${path}"
157                 else
158                     path="../${path}"
159                 fi
160          fi
161     done
162     unset orgwords p l
163
164     if test $deep -lt ${#locwords[@]} ; then
165         relp=0
166         while test $relp -lt $deep; do
167             unset locwords[$((relp++))]
168         done
169         while test ${#locwords[@]} -gt 0  ; do
170             path="../${path}"
171             unset locwords[$((relp++))]
172         done
173     fi
174
175     echo "$path"
176 }
177
178 counttest ()
179 {
180     testcount=$(expr $testcount + 1)
181 }
182
183 error ()
184 {
185     echo error: $@
186     checkfailed=$(expr $checkfailed + 1)
187     retval=1
188 }
189
190 warning ()
191 {
192     echo warning: $@
193     testfailed=$(expr $testfailed + 1)
194 }
195
196 addscript ()
197 {
198     local scriptname=$1
199     local script=${initddir}/$scriptname
200     cat > $script
201     chmod u+w,a+rx $script
202 }
203
204 insertscript ()
205 {
206     local scriptname=$1
207     addscript   $scriptname
208     insserv_reg $scriptname
209 }
210
211 present_ok ()
212 {
213     local rcdpath=$(runlevel_path $1); shift
214     local script=$1;   shift
215     local ret=0
216     test -L ${rcdpath}/[KS][0-9][0-9]$script || ret=1
217     counttest
218     return $ret
219 }
220
221 check_script_present ()
222 {
223     local runlevel=$1; shift
224     local script=$1;   shift
225     present_ok $runlevel $script && return 0
226     error "script $script not present in runlevel $runlevel"
227 }
228
229 test_script_present ()
230 {
231     local runlevel=$1; shift
232     local script=$1;   shift
233     present_ok $runlevel $script && return 0
234     warning "script $script not present in runlevel $runlevel"
235 }
236
237 check_script_not_present ()
238 {
239     local runlevel=$1; shift
240     local script=$1;   shift
241     if present_ok $runlevel $script ; then
242         error "script $script present in runlevel $runlevel"
243     fi
244     return 0
245 }
246
247 test_script_not_present ()
248 {
249     local runlevel=$1; shift
250     local script=$1;   shift
251     if present_ok $runlevel $script ; then
252         warning "script $script present in runlevel $runlevel"
253     fi
254     return 0
255 }
256
257 order_ok ()
258 {
259     local rcdpath=$(runlevel_path $1); shift
260     local script1=$1;  shift
261     local script2=$1;  shift
262     local ret=0 scr order=""
263     pushd $rcdpath &> /dev/null
264     if test -n "${issuse}" ; then
265         for scr in S[0-9][0-9]* ; do
266             test -e "${scr}" || continue
267             test -L "${scr}" || continue
268             case "${scr#S[0-9][0-9]}" in
269             ${script1}) order="${order:+$order }${script1}" ;;
270             ${script2}) order="${order:+$order }${script2}" ;;
271             esac
272         done
273     else
274         for scr in [SK][0-9][0-9]* ; do
275             test -e "${scr}" || continue
276             test -L "${scr}" || continue
277             case "${scr#[SK][0-9][0-9]}" in
278             ${script1}) order="${order:+$order }${script1}" ;;
279             ${script2}) order="${order:+$order }${script2}" ;;
280             esac
281         done
282     fi
283     popd &> /dev/null
284     test "$order" = "$script1 $script2" || ret=1
285     counttest
286     return $ret
287 }
288
289 # Fatal check
290 check_order ()
291 {
292     local runlevel=$1
293     local script1=$2
294     local script2=$3
295     order_ok ${1+"$@"} || error   "incorrect $runlevel sequence $script1 not before $script2" || true
296 }
297
298 # Non-fatal check
299 test_order ()
300 {
301     local runlevel=$1
302     local script1=$2
303     local script2=$3
304     order_ok ${1+"$@"} || warning "incorrect $runlevel sequence $script1 not before $script2" || true
305 }
306