tstHeadlessXOrg.sh revision c7450c9040258e7d6fee49769efbf82ba7ecee6e
# $Id$
#
# VirtualBox X Server auto-start service 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.
#
## The function definition at the start of every non-trivial shell script!
abort()
{
## $@, ... Error text to output to standard error in printf format.
format="$1"
shift
exit 1
}
## Print a TESTING line. Takes printf arguments but without a '\n'.
{
format="$1"
shift
printf "${TEST_NAME}: TESTING ${format}... " "$@"
}
## Expected a process to complete within a certain time and call a function if
# it does which should check whether the test was successful and print status
# information. The function takes the exit status as its single parameter.
{
# Give it time to complete.
wait "${PID}"
STATUS="$?"
143) # SIGTERM
printf "\nFAILED: time-out.\n"
;;
*)
${TEST_FUNCTION} "${STATUS}"
esac
}
## Create a simple configuration file. Add items onto the end to override them
# on an item-by-item basis.
{
TEST_FOLDER="${1}"
BASE_FOLDER="${TEST_FOLDER}"
XORG_FOLDER="${TEST_FOLDER}/xorg"
mkdir -p "${XORG_FOLDER}"
HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
HEADLESS_X_ORG_LOG_FILE="log"
HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="true"
HEADLESS_X_ORG_SERVER_PRE_COMMAND=
HEADLESS_X_ORG_SERVER_COMMAND="echo"
EOF
}
# 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.
# And remember the full path.
# Create a temporary directory for configuration and logging.
{
###############################################################################
# Simple start-up test. #
###############################################################################
print_line "simple start-up test"
create_basic_configuration "${TEST_FOLDER_BASE}simple_start-up_test/"
{
STATUS="$1"
0)
LOG_FOLDER="${TEST_FOLDER}/log"
LOG="${LOG_FOLDER}/log"
printf "SUCCESS.\n"
else
printf "\nFAILED: incorrect log output.\n"
fi
;;
*)
printf "\nFAILED: exit status ${STATUS}.\n"
esac
}
PID=$!
###############################################################################
# No configuration files. #
###############################################################################
create_basic_configuration "${TEST_FOLDER_BASE}no_configuration_files/"
print_line "no configuration files"
{
STATUS="$1"
0)
printf "\nFAILED: successful exit when an error was expected.\n"
;;
*)
esac
}
PID=$!
###############################################################################
# Bad configuration files. #
###############################################################################
print_line "bad configuration files"
create_basic_configuration "${TEST_FOLDER_BASE}bad_configuration_files/"
touch "${XORG_FOLDER}/xorg.conf.other"
PID=$!
###############################################################################
# Long running server command. #
###############################################################################
# Set up a configuration file for a long-running command.
create_basic_configuration "${TEST_FOLDER_BASE}long-running_command/"
cat >> "${TEST_FOLDER}conf" << EOF
HEADLESS_X_ORG_SERVER_COMMAND="${TEST_FOLDER}command.sh"
EOF
cat > "${TEST_FOLDER}command.sh" << EOF
#!/bin/sh
touch "${TEST_FOLDER}stopped"
touch "${TEST_FOLDER}started"
trap "touch \\"${TEST_FOLDER}stopped\\"; exit" TERM
rm "${TEST_FOLDER}stopped"
while true; do :; done
EOF
chmod a+x "${TEST_FOLDER}command.sh"
print_line "long running server command"
FAILURE=""
PID="$!"
while [ ! -f "${TEST_FOLDER}started" ]; do :; done
while [ -f "${TEST_FOLDER}stopped" ]; do :; done
while [ ! -f "${TEST_FOLDER}stopped" ]; do :; done
printf "SUCCESS.\n"
###############################################################################
# Pre-requisite test. #
###############################################################################
# Set up a configuration file with a pre-requisite.
create_basic_configuration "${TEST_FOLDER_BASE}pre-requisite/"
cat >> "${TEST_FOLDER}conf" << EOF
HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="false"
EOF
print_line "configuration file with failed pre-requisite"
echo "\nFAILED to stop for failed pre-requisite.\n"
else
echo "SUCCESS"
fi
###############################################################################
# Pre-command test. #
###############################################################################
# Set up our pre-command test configuration file.
create_basic_configuration "${TEST_FOLDER_BASE}pre-command/"
cat >> "${TEST_FOLDER}conf" << EOF
test_pre_command_server_pre_command()
{
touch "${TEST_FOLDER}/run/pre"
}
test_pre_command_server_command()
{
cp "${TEST_FOLDER}/run/pre" "${TEST_FOLDER}/run/pre2"
}
HEADLESS_X_ORG_SERVER_PRE_COMMAND="test_pre_command_server_pre_command"
HEADLESS_X_ORG_SERVER_COMMAND="test_pre_command_server_command"
EOF
print_line "pre-command test"
{
STATUS="$1"
0)
LOG_FOLDER="${TEST_FOLDER}/log"
LOG="${LOG_FOLDER}/log"
printf "SUCCESS.\n"
else
printf "\nFAILED: pre-command not executed.\n"
fi
;;
*)
printf "\nFAILED: exit status ${STATUS}.\n"
esac
}
PID=$!
###############################################################################
# Post-command test. #
###############################################################################
# Set up our post-command test configuration file.
create_basic_configuration "${TEST_FOLDER_BASE}post-command/"
cat >> "${TEST_FOLDER}conf" << EOF
test_post_command_post_command()
{
echo "\${1}" > "${TEST_FOLDER}/run/post"
}
HEADLESS_X_ORG_SERVER_POST_COMMAND="test_post_command_post_command"
EOF
print_line "post-command test"
{
STATUS="$1"
0)
LOG_FOLDER="${TEST_FOLDER}/log"
LOG="${LOG_FOLDER}/log"
printf "SUCCESS.\n"
else
printf "\nFAILED: post-command not executed.\n"
fi
;;
*)
printf "\nFAILED: exit status ${STATUS}.\n"
esac
}
PID=$!