tstInstallInit.sh revision 71fdb328d336f29be4f9ba0f2c4623a76ebd775c
# $Id$
#
# VirtualBox init file creator unit test.
#
# Copyright (C) 2012 Oracle Corporation
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# 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=" "
## 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.
{
cat >&2 << EOF
${TEST_NAME}: TESTING $1
EOF
}
## Set the failure message if it is not yet set.
fail_msg()
{
}
# Get the directory where the script is located and the parent.
[ -d "${VBOX_FOLDER}" ] ||
abort "Failed to change to directory ${VBOX_FOLDER}.\n"
cd "${VBOX_FOLDER}"
# Get our name for output.
# Create a trivial test service in temporary directory $1 with name $2.
{
#!/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.
{
#!/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%'
helpers/generate_service_file --command '/usr/bin
expected='TEST1%hello
world TST2 TEST '\''p
0
world'\'
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%'
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%'
helpers/generate_service_file --command '/usr/bin/hello' --format shell --description '' --stop-command /usr/bin/stop --stop-arguments hello`
echo "SUCCESS (3)";;
*)
cat << EOF
FAILED: expected
${expected}
but got
${out}
EOF
esac
# Create a simulated init system layout.
{
tmpdir="${1}"
for i in 0 1 2 3 4 5 6; do
done
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.
else
fi
# Try to start the service using the symbolic links which should have been
# created.
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.
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.
# 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.
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.
fail_msg "\"scripts/install_service\" failed."
# After uninstallation this should be the only file left in the init tree.
rm "${tmpdir}/service"
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."
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."
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."
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."
fail_msg "files were not installed on forced enable."
# Final summary.
if test -n "${failed}"; then
echo "${failed}"
else
echo SUCCESS
fi