VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/testcase/tstInstallInit.sh@ 44824

Last change on this file since 44824 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
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
20tab=" "
21tmpbase="/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!
24abort()
25{
26 ## $1 Error text to output to standard error in printf format.
27 cat >&2 << EOF
28${TEST_NAME}: $1
29EOF
30 exit 1
31}
32
33## Print a TESTING line.
34print_line()
35{
36 cat >&2 << EOF
37${TEST_NAME}: TESTING $1
38EOF
39}
40
41## Set the failure message if it is not yet set.
42fail_msg()
43{
44 test -z "${failed}" && failed="FAILED: ${1}"
45}
46
47# Get the directory where the script is located and the parent.
48OUR_FOLDER=`dirname "$0"`
49OUR_FOLDER=`cd "${OUR_FOLDER}" && pwd`
50VBOX_FOLDER=`cd "${OUR_FOLDER}/.." && pwd`
51[ -d "${VBOX_FOLDER}" ] ||
52 abort "Failed to change to directory ${VBOX_FOLDER}.\n"
53cd "${VBOX_FOLDER}"
54
55# Get our name for output.
56TEST_NAME="$(basename "$0" .sh)"
57
58# Create a trivial test service in temporary directory $1 with name $2.
59test_service()
60{
61 cat > "${1}/${2}" << EOF
62#!/bin/sh
63trap "touch \"${1}/stopped\"; exit" TERM
64echo "1: \${1} 2: \${2} 3: \${3}" > "${1}/started"
65while true; do true; done
66EOF
67chmod u+x "${1}/${2}"
68}
69
70# Create a trivial test command in temporary directory $1 with name $2.
71test_oneshot()
72{
73 cat > "${1}/${2}" << EOF
74#!/bin/sh
75if test "\${1}" = start; then
76 touch "${1}/started"
77else
78 rm "${1}/started"
79fi
80exit 0
81EOF
82chmod 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.
87print_line "generation of shell script from template."
88input='TEST1%DESCRIPTION%%%%SERVICE_NAME%%STOP_COMMAND% TST2 TEST %ARGUMENTS%%COMMAND%'
89out=`echo "${input}" |
90 helpers/generate_service_file --command '/usr/bin
91aries/hello
92world' --arguments 'p\x0a0\n\ \t' --format shell --description ''`
93expected='TEST1%hello
94world TST2 TEST '\''p
950
96 '"${tab}"\'\''/usr/bin
97aries/hello
98world'\'
99case "${out}" in ${expected})
100echo "SUCCESS (1)";;
101*)
102cat << EOF
103FAILED: expected
104${expected}
105but got
106${out}
107EOF
108esac
109input='TEST%HAVE_STOP_COMMAND%%SERVICE_NAME%%STOP_COMMAND% TST2
110 TEST %COMMAND%'
111out=`echo "${input}" |
112 helpers/generate_service_file --command '/usr/bin/hello' --format shell --description ''`
113expected='TEST TEST '\''/usr/bin/hello'\'''
114case "${out}" in ${expected})
115echo "SUCCESS (2)";;
116*)
117cat << EOF
118FAILED: expected
119${expected}
120but got
121${out}
122EOF
123esac
124input='TEST%HAVE_STOP_COMMAND%%SERVICE_NAME%%STOP_COMMAND% %STOP_ARGUMENTS% TST2
125 TEST %COMMAND%'
126out=`echo "${input}" |
127 helpers/generate_service_file --command '/usr/bin/hello' --format shell --description '' --stop-command /usr/bin/stop --stop-arguments hello`
128expected='TESThello'\''/usr/bin/stop'\'' '\''hello'\'' TST2
129 TEST '\''/usr/bin/hello'\'''
130case "${out}" in ${expected})
131echo "SUCCESS (3)";;
132*)
133cat << EOF
134FAILED: expected
135${expected}
136but got
137${out}
138EOF
139esac
140
141# Create a simulated init system layout.
142create_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.
156print_line "installing an init script."
157failed=""
158# Create a simulated init system layout.
159tmpdir="${tmpbase}0"
160create_simulated_init_tree "${tmpdir}"
161# Create the service binary.
162test_service "${tmpdir}" "service"
163# And install it.
164scripts/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.
167if 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\""
170else
171 fail_msg "\"${tmpdir}/init.d/service\" not correctly created."
172fi
173test -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.
177if "${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
184else
185 fail_msg "could not start \"${tmpdir}/rc3.d/S20service\"."
186fi
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.
192if "${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."
195else
196 fail_msg "could not stop \"${tmpdir}/rc.d/rc6.d/K80service\"."
197fi
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.
202if test -n "${failed}"; then
203 echo "${failed}"
204else
205 echo SUCCESS
206fi
207
208# Test an one shot init script installation.
209print_line "installing a one shot init script."
210failed=""
211# Create a simulated init system layout.
212tmpdir="${tmpbase}0"
213create_simulated_init_tree "${tmpdir}"
214# Create the command binary.
215test_oneshot "${tmpdir}" "command"
216# And install the script.
217scripts/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.
220test -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.
224if "${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."
227else
228 fail_msg "could not start \"${tmpdir}/rc3.d/S20command\"."
229fi
230# Try to stop the service using the symbolic links which should have been
231# created.
232if "${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."
235else
236 fail_msg "could not stop \"${tmpdir}/rc.d/rc6.d/K80command\"."
237fi
238# Final summary.
239if test -n "${failed}"; then
240 echo "${failed}"
241else
242 echo SUCCESS
243fi
244
245# Test an init script removal.
246print_line "removing an init script."
247failed=""
248# Create a simulated init system layout.
249tmpdir="${tmpbase}0"
250create_simulated_init_tree "${tmpdir}"
251# Create the service binary.
252test_service "${tmpdir}" "service"
253# Install it.
254scripts/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.
257scripts/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.
260rm "${tmpdir}/service"
261test "x`find "${tmpdir}" -type f -o -type l`" = "x" ||
262 fail_msg "not all files were removed."
263# Final summary.
264if test -n "${failed}"; then
265 echo "${failed}"
266else
267 echo SUCCESS
268fi
269
270# Test an enabled init script update with --disable.
271print_line "updating an enabled init script with --disable."
272failed=""
273# Create a simulated init system layout.
274tmpdir="${tmpbase}1"
275create_simulated_init_tree "${tmpdir}"
276# Create the service binary.
277test_service "${tmpdir}" "service"
278# Install it.
279scripts/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.
282scripts/install_service --prefix "${tmpdir}" --disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
283 fail_msg "\"scripts/install_service\" failed."
284test "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.
287if test -n "${failed}"; then
288 echo "${failed}"
289else
290 echo SUCCESS
291fi
292
293# Test updating a disabled init script with --enable.
294print_line "updating a disabled init script with --enable."
295failed=""
296# Create a simulated init system layout.
297tmpdir="${tmpbase}2"
298create_simulated_init_tree "${tmpdir}"
299# Create the service binary.
300test_service "${tmpdir}" "service"
301# Install it.
302scripts/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.
305scripts/install_service --prefix "${tmpdir}" --enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
306 fail_msg "\"scripts/install_service\" failed."
307test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l`" = "x" ||
308 fail_msg "files were installed on non-forced enable."
309# Final summary.
310if test -n "${failed}"; then
311 echo "${failed}"
312else
313 echo SUCCESS
314fi
315
316# Test an enabled init script update with --force-disable.
317print_line "updating an enabled init script with --force-disable."
318failed=""
319# Create a simulated init system layout.
320tmpdir="${tmpbase}3"
321create_simulated_init_tree "${tmpdir}"
322# Create the service binary.
323test_service "${tmpdir}" "service"
324# Install it.
325scripts/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.
328scripts/install_service --prefix "${tmpdir}" --force-disable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
329 fail_msg "\"scripts/install_service\" failed."
330test "x`find "${tmpdir}"/rc*.d "${tmpdir}/runlevel" -type l`" = "x" ||
331 fail_msg "links were not removed on forced disable."
332# Final summary.
333if test -n "${failed}"; then
334 echo "${failed}"
335else
336 echo SUCCESS
337fi
338
339# Test updating a disabled init script with --force-enable.
340print_line "updating a disabled init script with --force-enable."
341failed=""
342# Create a simulated init system layout.
343tmpdir="${tmpbase}4"
344create_simulated_init_tree "${tmpdir}"
345# Create the service binary.
346test_service "${tmpdir}" "service"
347# Install it.
348scripts/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.
351scripts/install_service --prefix "${tmpdir}" --force-enable -- --command "${tmpdir}/service" --arguments "test of my\ arguments" --description "My description" ||
352 fail_msg "\"scripts/install_service\" failed."
353test "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.
356if test -n "${failed}"; then
357 echo "${failed}"
358else
359 echo SUCCESS
360fi
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette