1 | #!/bin/sh
|
---|
2 | # $Id: tstInstallInit.sh 44529 2013-02-04 15:54:15Z vboxsync $
|
---|
3 | #
|
---|
4 | # VirtualBox init file creator unit test.
|
---|
5 | #
|
---|
6 | # Copyright (C) 2012-2013 Oracle Corporation
|
---|
7 | #
|
---|
8 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | # available from http://www.virtualbox.org. This file is free software;
|
---|
10 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | # General Public License (GPL) as published by the Free Software
|
---|
12 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | #
|
---|
16 |
|
---|
17 | # This will initially be a skeleton with a couple of tests - add more for quick
|
---|
18 | # debugging when you suspect that something isn't working as specified.
|
---|
19 |
|
---|
20 | tab=" "
|
---|
21 | tmpbase="/tmp/tstInstallInit 99" # Space in the name for a little stress...
|
---|
22 |
|
---|
23 | ## The function definition at the start of every non-trivial shell script!
|
---|
24 | abort()
|
---|
25 | {
|
---|
26 | ## $1 Error text to output to standard error in printf format.
|
---|
27 | cat >&2 << EOF
|
---|
28 | ${TEST_NAME}: $1
|
---|
29 | EOF
|
---|
30 | exit 1
|
---|
31 | }
|
---|
32 |
|
---|
33 | ## Print a TESTING line.
|
---|
34 | print_line()
|
---|
35 | {
|
---|
36 | cat >&2 << EOF
|
---|
37 | ${TEST_NAME}: TESTING $1
|
---|
38 | EOF
|
---|
39 | }
|
---|
40 |
|
---|
41 | ## Set the failure message if it is not yet set.
|
---|
42 | fail_msg()
|
---|
43 | {
|
---|
44 | test -z "${failed}" && failed="FAILED: ${1}"
|
---|
45 | }
|
---|
46 |
|
---|
47 | # Get the directory where the script is located and the parent.
|
---|
48 | OUR_FOLDER=`dirname "$0"`
|
---|
49 | OUR_FOLDER=`cd "${OUR_FOLDER}" && pwd`
|
---|
50 | VBOX_FOLDER=`cd "${OUR_FOLDER}/.." && pwd`
|
---|
51 | [ -d "${VBOX_FOLDER}" ] ||
|
---|
52 | abort "Failed to change to directory ${VBOX_FOLDER}.\n"
|
---|
53 | cd "${VBOX_FOLDER}"
|
---|
54 |
|
---|
55 | # Get our name for output.
|
---|
56 | TEST_NAME="$(basename "$0" .sh)"
|
---|
57 |
|
---|
58 | # Create a trivial test service in temporary directory $1 with name $2.
|
---|
59 | test_service()
|
---|
60 | {
|
---|
61 | cat > "${1}/${2}" << EOF
|
---|
62 | #!/bin/sh
|
---|
63 | trap "touch \"${1}/stopped\"; exit" TERM
|
---|
64 | echo "1: \${1} 2: \${2} 3: \${3}" > "${1}/started"
|
---|
65 | while true; do true; done
|
---|
66 | EOF
|
---|
67 | chmod u+x "${1}/${2}"
|
---|
68 | }
|
---|
69 |
|
---|
70 | # Create a trivial test command in temporary directory $1 with name $2.
|
---|
71 | test_oneshot()
|
---|
72 | {
|
---|
73 | cat > "${1}/${2}" << EOF
|
---|
74 | #!/bin/sh
|
---|
75 | if test "\${1}" = start; then
|
---|
76 | touch "${1}/started"
|
---|
77 | else
|
---|
78 | rm "${1}/started"
|
---|
79 | fi
|
---|
80 | exit 0
|
---|
81 | EOF
|
---|
82 | chmod u+x "${1}/${2}"
|
---|
83 | }
|
---|
84 |
|
---|
85 | # Test some dodgy input values against generate_service_file.
|
---|
86 | # Make sure there is a substitution pattern at the end too.
|
---|
87 | print_line "generation of shell script from template."
|
---|
88 | input='TEST1%DESCRIPTION%%%%SERVICE_NAME%%STOP_COMMAND% TST2 TEST %ARGUMENTS%%COMMAND%'
|
---|
89 | out=`echo "${input}" |
|
---|
90 | helpers/generate_service_file --command '/usr/bin
|
---|
91 | aries/hello
|
---|
92 | world' --arguments 'p\x0a0\n\ \t' --format shell --description ''`
|
---|
93 | expected='TEST1%hello
|
---|
94 | world TST2 TEST '\''p
|
---|
95 | 0
|
---|
96 | '"${tab}"\'\''/usr/bin
|
---|
97 | aries/hello
|
---|
98 | world'\'
|
---|
99 | case "${out}" in ${expected})
|
---|
100 | echo "SUCCESS (1)";;
|
---|
101 | *)
|
---|
102 | cat << EOF
|
---|
103 | FAILED: expected
|
---|
104 | ${expected}
|
---|
105 | but got
|
---|
106 | ${out}
|
---|
107 | EOF
|
---|
108 | esac
|
---|
109 | input='TEST%HAVE_STOP_COMMAND%%SERVICE_NAME%%STOP_COMMAND% TST2
|
---|
110 | TEST %COMMAND%'
|
---|
111 | out=`echo "${input}" |
|
---|
112 | helpers/generate_service_file --command '/usr/bin/hello' --format shell --description ''`
|
---|
113 | expected='TEST TEST '\''/usr/bin/hello'\'''
|
---|
114 | case "${out}" in ${expected})
|
---|
115 | echo "SUCCESS (2)";;
|
---|
116 | *)
|
---|
117 | cat << EOF
|
---|
118 | FAILED: expected
|
---|
119 | ${expected}
|
---|
120 | but got
|
---|
121 | ${out}
|
---|
122 | EOF
|
---|
123 | esac
|
---|
124 | input='TEST%HAVE_STOP_COMMAND%%SERVICE_NAME%%STOP_COMMAND% %STOP_ARGUMENTS% TST2
|
---|
125 | TEST %COMMAND%'
|
---|
126 | out=`echo "${input}" |
|
---|
127 | helpers/generate_service_file --command '/usr/bin/hello' --format shell --description '' --stop-command /usr/bin/stop --stop-arguments hello`
|
---|
128 | expected='TESThello'\''/usr/bin/stop'\'' '\''hello'\'' TST2
|
---|
129 | TEST '\''/usr/bin/hello'\'''
|
---|
130 | case "${out}" in ${expected})
|
---|
131 | echo "SUCCESS (3)";;
|
---|
132 | *)
|
---|
133 | cat << EOF
|
---|
134 | FAILED: expected
|
---|
135 | ${expected}
|
---|
136 | but got
|
---|
137 | ${out}
|
---|
138 | EOF
|
---|
139 | esac
|
---|
140 |
|
---|
141 | # Create a simulated init system layout.
|
---|
142 | create_simulated_init_tree()
|
---|
143 | {
|
---|
144 | tmpdir="${1}"
|
---|
145 | rm -rf "${tmpdir}"
|
---|
146 | mkdir -m 0700 "${tmpdir}" || abort "Failed to create a temporary folder."
|
---|
147 | mkdir -p "${tmpdir}/init.d/" "${tmpdir}/rc.d/init.d/"
|
---|
148 | for i in 0 1 2 3 4 5 6; do
|
---|
149 | mkdir "${tmpdir}/rc${i}.d/" "${tmpdir}/rc.d/rc${i}.d/"
|
---|
150 | done
|
---|
151 | mkdir -p "${tmpdir}/runlevel/default" "${tmpdir}/runlevel/boot"
|
---|
152 | mkdir "${tmpdir}/run"
|
---|
153 | }
|
---|
154 |
|
---|
155 | # Test an init script installation.
|
---|
156 | print_line "installing an init script."
|
---|
157 | failed=""
|
---|
158 | # Create a simulated init system layout.
|
---|
159 | tmpdir="${tmpbase}0"
|
---|
160 | create_simulated_init_tree "${tmpdir}"
|
---|
161 | # Create the service binary.
|
---|
162 | test_service "${tmpdir}" "service"
|
---|
163 | # And install it.
|
---|
164 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
165 | fail_msg "\"scripts/install_service\" failed."
|
---|
166 | # Check that the main service file was created as specified.
|
---|
167 | if test -x "${tmpdir}/init.d/service"; then
|
---|
168 | grep "Short-Description: My description" "${tmpdir}/init.d/service" >/dev/null ||
|
---|
169 | fail_msg "Description not set in \"${tmpdir}/init.d/service\""
|
---|
170 | else
|
---|
171 | fail_msg "\"${tmpdir}/init.d/service\" not correctly created."
|
---|
172 | fi
|
---|
173 | test -x "${tmpdir}/init.d/rc.d/service" &&
|
---|
174 | fail_msg "\"${tmpdir}/init.d/rc.d/service\" created but shouldn't have been."
|
---|
175 | # Try to start the service using the symbolic links which should have been
|
---|
176 | # created.
|
---|
177 | if "${tmpdir}/rc3.d/S20service" --prefix "${tmpdir}" --lsb-functions "" start >/dev/null 2>&1; then
|
---|
178 | if grep "1: test 2: of 3: my arguments" "${tmpdir}/started" >/dev/null; then
|
---|
179 | test -f "${tmpdir}/stopped" &&
|
---|
180 | fail_msg "\"${tmpdir}/rc3.d/S20service\" stopped immediately."
|
---|
181 | else
|
---|
182 | fail_msg "\"${tmpdir}/rc3.d/S20service\" did not start correctly."
|
---|
183 | fi
|
---|
184 | else
|
---|
185 | fail_msg "could not start \"${tmpdir}/rc3.d/S20service\"."
|
---|
186 | fi
|
---|
187 | # Check the status.
|
---|
188 | "${tmpdir}/rc.d/rc5.d/S20service" --prefix "${tmpdir}" --lsb-functions "" status >/dev/null 2>&1 ||
|
---|
189 | fail_msg "\"${tmpdir}/rc.d/rc5.d/S20service\" reported the wrong status."
|
---|
190 | # Try to stop the service using the symbolic links which should have been
|
---|
191 | # created.
|
---|
192 | if "${tmpdir}/rc.d/rc6.d/K80service" --prefix "${tmpdir}" --lsb-functions "" stop >/dev/null 2>&1; then
|
---|
193 | test -f "${tmpdir}/stopped" ||
|
---|
194 | echo "\"${tmpdir}/rc.d/rc6.d/K80service\" did not stop correctly."
|
---|
195 | else
|
---|
196 | fail_msg "could not stop \"${tmpdir}/rc.d/rc6.d/K80service\"."
|
---|
197 | fi
|
---|
198 | # Check the status again - now it should be stopped.
|
---|
199 | "${tmpdir}/runlevel/service" --prefix "${tmpdir}" --lsb-functions "" status >/dev/null 2>&1 &&
|
---|
200 | fail_msg "\"${tmpdir}/runlevel/service\" reported the wrong status."
|
---|
201 | # Final summary.
|
---|
202 | if test -n "${failed}"; then
|
---|
203 | echo "${failed}"
|
---|
204 | else
|
---|
205 | echo SUCCESS
|
---|
206 | fi
|
---|
207 |
|
---|
208 | # Test an one shot init script installation.
|
---|
209 | print_line "installing a one shot init script."
|
---|
210 | failed=""
|
---|
211 | # Create a simulated init system layout.
|
---|
212 | tmpdir="${tmpbase}0"
|
---|
213 | create_simulated_init_tree "${tmpdir}"
|
---|
214 | # Create the command binary.
|
---|
215 | test_oneshot "${tmpdir}" "command"
|
---|
216 | # And install the script.
|
---|
217 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/command" --arguments "start" --description "My description" --stop-command "${tmpdir}/command" --stop-arguments "stop" --one-shot ||
|
---|
218 | fail_msg "\"scripts/install_service\" failed."
|
---|
219 | # Sanity test.
|
---|
220 | test -f "${tmpdir}/started" &&
|
---|
221 | fail_msg "\"${tmpdir}/started\" already exists!"
|
---|
222 | # Try to start the service using the symbolic links which should have been
|
---|
223 | # created.
|
---|
224 | if "${tmpdir}/rc3.d/S20command" --prefix "${tmpdir}" --lsb-functions "" start >/dev/null 2>&1; then
|
---|
225 | test -f "${tmpdir}/started" ||
|
---|
226 | fail_msg "\"${tmpdir}/rc3.d/S20command\" did not start correctly."
|
---|
227 | else
|
---|
228 | fail_msg "could not start \"${tmpdir}/rc3.d/S20command\"."
|
---|
229 | fi
|
---|
230 | # Try to stop the service using the symbolic links which should have been
|
---|
231 | # created.
|
---|
232 | if "${tmpdir}/rc.d/rc6.d/K80command" --prefix "${tmpdir}" --lsb-functions "" stop >/dev/null 2>&1; then
|
---|
233 | test -f "${tmpdir}/started" &&
|
---|
234 | echo "\"${tmpdir}/rc.d/rc6.d/K80command\" did not stop correctly."
|
---|
235 | else
|
---|
236 | fail_msg "could not stop \"${tmpdir}/rc.d/rc6.d/K80command\"."
|
---|
237 | fi
|
---|
238 | # Final summary.
|
---|
239 | if test -n "${failed}"; then
|
---|
240 | echo "${failed}"
|
---|
241 | else
|
---|
242 | echo SUCCESS
|
---|
243 | fi
|
---|
244 |
|
---|
245 | # Test an init script removal.
|
---|
246 | print_line "removing an init script."
|
---|
247 | failed=""
|
---|
248 | # Create a simulated init system layout.
|
---|
249 | tmpdir="${tmpbase}0"
|
---|
250 | create_simulated_init_tree "${tmpdir}"
|
---|
251 | # Create the service binary.
|
---|
252 | test_service "${tmpdir}" "service"
|
---|
253 | # Install it.
|
---|
254 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
255 | fail_msg "\"scripts/install_service\" failed."
|
---|
256 | # And remove it again.
|
---|
257 | scripts/install_service --prefix "${tmpdir}" --remove -- --command "${tmpdir}/service" ||
|
---|
258 | fail_msg "\"scripts/install_service\" failed."
|
---|
259 | # After uninstallation this should be the only file left in the init tree.
|
---|
260 | rm "${tmpdir}/service"
|
---|
261 | test "x`find "${tmpdir}" -type f -o -type l`" = "x" ||
|
---|
262 | fail_msg "not all files were removed."
|
---|
263 | # Final summary.
|
---|
264 | if test -n "${failed}"; then
|
---|
265 | echo "${failed}"
|
---|
266 | else
|
---|
267 | echo SUCCESS
|
---|
268 | fi
|
---|
269 |
|
---|
270 | # Test an enabled init script update with --disable.
|
---|
271 | print_line "updating an enabled init script with --disable."
|
---|
272 | failed=""
|
---|
273 | # Create a simulated init system layout.
|
---|
274 | tmpdir="${tmpbase}1"
|
---|
275 | create_simulated_init_tree "${tmpdir}"
|
---|
276 | # Create the service binary.
|
---|
277 | test_service "${tmpdir}" "service"
|
---|
278 | # Install it.
|
---|
279 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
280 | fail_msg "\"scripts/install_service\" failed."
|
---|
281 | # Install it disabled without forcing.
|
---|
282 | scripts/install_service --prefix "${tmpdir}" --disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
283 | fail_msg "\"scripts/install_service\" failed."
|
---|
284 | test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l | wc -l`" = "x15" ||
|
---|
285 | fail_msg "links were removed on non-forced disable."
|
---|
286 | # Final summary.
|
---|
287 | if test -n "${failed}"; then
|
---|
288 | echo "${failed}"
|
---|
289 | else
|
---|
290 | echo SUCCESS
|
---|
291 | fi
|
---|
292 |
|
---|
293 | # Test updating a disabled init script with --enable.
|
---|
294 | print_line "updating a disabled init script with --enable."
|
---|
295 | failed=""
|
---|
296 | # Create a simulated init system layout.
|
---|
297 | tmpdir="${tmpbase}2"
|
---|
298 | create_simulated_init_tree "${tmpdir}"
|
---|
299 | # Create the service binary.
|
---|
300 | test_service "${tmpdir}" "service"
|
---|
301 | # Install it.
|
---|
302 | scripts/install_service --prefix "${tmpdir}" --disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
303 | fail_msg "\"scripts/install_service\" failed."
|
---|
304 | # Install it disabled without forcing.
|
---|
305 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
306 | fail_msg "\"scripts/install_service\" failed."
|
---|
307 | test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l`" = "x" ||
|
---|
308 | fail_msg "files were installed on non-forced enable."
|
---|
309 | # Final summary.
|
---|
310 | if test -n "${failed}"; then
|
---|
311 | echo "${failed}"
|
---|
312 | else
|
---|
313 | echo SUCCESS
|
---|
314 | fi
|
---|
315 |
|
---|
316 | # Test an enabled init script update with --force-disable.
|
---|
317 | print_line "updating an enabled init script with --force-disable."
|
---|
318 | failed=""
|
---|
319 | # Create a simulated init system layout.
|
---|
320 | tmpdir="${tmpbase}3"
|
---|
321 | create_simulated_init_tree "${tmpdir}"
|
---|
322 | # Create the service binary.
|
---|
323 | test_service "${tmpdir}" "service"
|
---|
324 | # Install it.
|
---|
325 | scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
326 | fail_msg "\"scripts/install_service\" failed."
|
---|
327 | # Install it disabled without forcing.
|
---|
328 | scripts/install_service --prefix "${tmpdir}" --force-disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
329 | fail_msg "\"scripts/install_service\" failed."
|
---|
330 | test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l`" = "x" ||
|
---|
331 | fail_msg "links were not removed on forced disable."
|
---|
332 | # Final summary.
|
---|
333 | if test -n "${failed}"; then
|
---|
334 | echo "${failed}"
|
---|
335 | else
|
---|
336 | echo SUCCESS
|
---|
337 | fi
|
---|
338 |
|
---|
339 | # Test updating a disabled init script with --force-enable.
|
---|
340 | print_line "updating a disabled init script with --force-enable."
|
---|
341 | failed=""
|
---|
342 | # Create a simulated init system layout.
|
---|
343 | tmpdir="${tmpbase}4"
|
---|
344 | create_simulated_init_tree "${tmpdir}"
|
---|
345 | # Create the service binary.
|
---|
346 | test_service "${tmpdir}" "service"
|
---|
347 | # Install it.
|
---|
348 | scripts/install_service --prefix "${tmpdir}" --disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
349 | fail_msg "\"scripts/install_service\" failed."
|
---|
350 | # Install it disabled without forcing.
|
---|
351 | scripts/install_service --prefix "${tmpdir}" --force-enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
|
---|
352 | fail_msg "\"scripts/install_service\" failed."
|
---|
353 | test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l | wc -l`" = "x15" ||
|
---|
354 | fail_msg "files were not installed on forced enable."
|
---|
355 | # Final summary.
|
---|
356 | if test -n "${failed}"; then
|
---|
357 | echo "${failed}"
|
---|
358 | else
|
---|
359 | echo SUCCESS
|
---|
360 | fi
|
---|