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