VirtualBox

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

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

Installer/linux: headless X server service clean-up.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1#!/bin/sh
2# $Id: tstHeadlessXOrg.sh 44063 2012-12-07 14:58:52Z vboxsync $
3#
4# VirtualBox X Server auto-start service unit test.
5#
6# Copyright (C) 2012 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## The function definition at the start of every non-trivial shell script!
18abort()
19{
20 ## $@, ... Error text to output to standard error in printf format.
21 format="$1"
22 shift
23 printf "${TEST_NAME}: ${format}" "$@" >&2
24 exit 1
25}
26
27## Print a TESTING line. Takes printf arguments but without a '\n'.
28print_line()
29{
30 format="$1"
31 shift
32 printf "${TEST_NAME}: TESTING ${format}... " "$@"
33}
34
35## Expected a process to complete within a certain time and call a function if
36# it does which should check whether the test was successful and print status
37# information. The function takes the exit status as its single parameter.
38expect_exit()
39{
40 PID="$1" ## The PID we are waiting for.
41 TIME_OUT="$2" ## The time-out before we terminate the process.
42 TEST_FUNCTION="$3" ## The function to call on exit to check the test result.
43
44 # Give it time to complete.
45 { sleep "${TIME_OUT}"; kill "${PID}" 2>/dev/null; } &
46
47 wait "${PID}"
48 STATUS="$?"
49 case "${STATUS}" in
50 143) # SIGTERM
51 printf "\nFAILED: time-out.\n"
52 ;;
53 *)
54 ${TEST_FUNCTION} "${STATUS}"
55esac
56}
57
58## Create a simple configuration file. Add items onto the end to override them
59# on an item-by-item basis.
60create_basic_configuration()
61{
62 TEST_FOLDER="${1}"
63 FILE_NAME="${TEST_FOLDER}conf" ## The name of the configuration file to create.
64 BASE_FOLDER="${TEST_FOLDER}"
65 XORG_FOLDER="${TEST_FOLDER}/xorg"
66 mkdir -p "${XORG_FOLDER}"
67 cat > "${FILE_NAME}" << EOF
68HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
69HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
70HEADLESS_X_ORG_LOG_FILE="log"
71HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
72HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="true"
73HEADLESS_X_ORG_SERVER_PRE_COMMAND=
74HEADLESS_X_ORG_SERVER_COMMAND="echo"
75EOF
76
77}
78
79# Get the directory where the script is located and the parent.
80OUR_FOLDER="$(dirname "$0")"
81OUR_FOLDER=$(cd "${OUR_FOLDER}" && pwd)
82VBOX_FOLDER=$(cd "${OUR_FOLDER}/.." && pwd)
83[ -d "${VBOX_FOLDER}" ] ||
84 abort "Failed to change to directory ${VBOX_FOLDER}.\n"
85cd "${VBOX_FOLDER}"
86
87# Get our name for output.
88TEST_NAME="$(basename "$0" .sh)"
89
90# And remember the full path.
91TEST_NAME_FULL="${OUR_FOLDER}/$(basename "$0")"
92
93# Create a temporary directory for configuration and logging.
94TEST_FOLDER_BASE="/tmp/${TEST_NAME} 99/" # Space in the name to test quoting.
95{
96 rm -rf "${TEST_FOLDER_BASE}" 2>/dev/null &&
97 mkdir -m 0700 "${TEST_FOLDER_BASE}" 2>/dev/null
98} || abort "Could not create test folder.\n"
99
100###############################################################################
101# Simple start-up test. #
102###############################################################################
103print_line "simple start-up test"
104create_basic_configuration "${TEST_FOLDER_BASE}simple_start-up_test/"
105touch "${XORG_FOLDER}/xorg.conf.2"
106touch "${XORG_FOLDER}/xorg.conf.4"
107
108test_simple_start_up()
109{
110 STATUS="$1"
111 case "${STATUS}" in
112 0)
113 LOG_FOLDER="${TEST_FOLDER}/log"
114 LOG="${LOG_FOLDER}/log"
115 if grep -q "2 ${XORG_FOLDER}/xorg.conf.2 ${LOG_FOLDER}/Xorg.2.log" "${LOG}" &&
116 grep -q "4 ${XORG_FOLDER}/xorg.conf.4 ${LOG_FOLDER}/Xorg.4.log" "${LOG}"; then
117 printf "SUCCESS.\n"
118 else
119 printf "\nFAILED: incorrect log output.\n"
120 fi
121 ;;
122 *)
123 printf "\nFAILED: exit status ${STATUS}.\n"
124 esac
125}
126
127scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
128PID=$!
129expect_exit "${PID}" 5 test_simple_start_up
130
131###############################################################################
132# No configuration files. #
133###############################################################################
134create_basic_configuration "${TEST_FOLDER_BASE}no_configuration_files/"
135print_line "no configuration files"
136
137test_should_fail()
138{
139 STATUS="$1"
140 case "${STATUS}" in
141 0)
142 printf "\nFAILED: successful exit when an error was expected.\n"
143 ;;
144 *)
145 printf "SUCCESS.\n" # At least it behaved the way we wanted.
146 esac
147}
148
149scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
150PID=$!
151expect_exit "${PID}" 5 test_should_fail
152
153###############################################################################
154# Bad configuration files. #
155###############################################################################
156print_line "bad configuration files"
157create_basic_configuration "${TEST_FOLDER_BASE}bad_configuration_files/"
158touch "${XORG_FOLDER}/xorg.conf.2"
159touch "${XORG_FOLDER}/xorg.conf.4"
160touch "${XORG_FOLDER}/xorg.conf.other"
161scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
162PID=$!
163expect_exit "${PID}" 5 test_should_fail
164
165###############################################################################
166# Long running server command. #
167###############################################################################
168
169# Set up a configuration file for a long-running command.
170create_basic_configuration "${TEST_FOLDER_BASE}long-running_command/"
171cat >> "${TEST_FOLDER}conf" << EOF
172HEADLESS_X_ORG_SERVER_COMMAND="${TEST_FOLDER}command.sh"
173EOF
174
175cat > "${TEST_FOLDER}command.sh" << EOF
176#!/bin/sh
177touch "${TEST_FOLDER}stopped"
178touch "${TEST_FOLDER}started"
179trap "touch \\"${TEST_FOLDER}stopped\\"; exit" TERM
180rm "${TEST_FOLDER}stopped"
181while true; do :; done
182EOF
183chmod a+x "${TEST_FOLDER}command.sh"
184
185print_line "long running server command"
186touch "${XORG_FOLDER}/xorg.conf.5"
187FAILURE=""
188scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
189PID="$!"
190while [ ! -f "${TEST_FOLDER}started" ]; do :; done
191while [ -f "${TEST_FOLDER}stopped" ]; do :; done
192[ -n "${PID}" ] && kill "${PID}" 2>/dev/null
193while [ ! -f "${TEST_FOLDER}stopped" ]; do :; done
194printf "SUCCESS.\n"
195
196###############################################################################
197# Pre-requisite test. #
198###############################################################################
199
200# Set up a configuration file with a pre-requisite.
201create_basic_configuration "${TEST_FOLDER_BASE}pre-requisite/"
202cat >> "${TEST_FOLDER}conf" << EOF
203HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="false"
204EOF
205
206print_line "configuration file with failed pre-requisite"
207touch "${XORG_FOLDER}/xorg.conf.2"
208touch "${XORG_FOLDER}/xorg.conf.4"
209if scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf"; then
210 echo "\nFAILED to stop for failed pre-requisite.\n"
211else
212 echo "SUCCESS"
213fi
214
215###############################################################################
216# Pre-command test. #
217###############################################################################
218
219# Set up our pre-command test configuration file.
220create_basic_configuration "${TEST_FOLDER_BASE}pre-command/"
221
222cat >> "${TEST_FOLDER}conf" << EOF
223test_pre_command_server_pre_command()
224{
225 touch "${TEST_FOLDER}/run/pre"
226}
227test_pre_command_server_command()
228{
229 cp "${TEST_FOLDER}/run/pre" "${TEST_FOLDER}/run/pre2"
230}
231HEADLESS_X_ORG_SERVER_PRE_COMMAND="test_pre_command_server_pre_command"
232HEADLESS_X_ORG_SERVER_COMMAND="test_pre_command_server_command"
233EOF
234
235print_line "pre-command test"
236touch "${XORG_FOLDER}/xorg.conf.2"
237
238test_pre_command()
239{
240 STATUS="$1"
241 case "${STATUS}" in
242 0)
243 LOG_FOLDER="${TEST_FOLDER}/log"
244 LOG="${LOG_FOLDER}/log"
245 if [ -e "${TEST_FOLDER}/run/pre" ] && [ -e "${TEST_FOLDER}/run/pre2" ]; then
246 printf "SUCCESS.\n"
247 else
248 printf "\nFAILED: pre-command not executed.\n"
249 fi
250 ;;
251 *)
252 printf "\nFAILED: exit status ${STATUS}.\n"
253 esac
254}
255
256rm -f "${TEST_FOLDER}/run/pre"
257scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
258PID=$!
259expect_exit "${PID}" 5 test_pre_command
260
261###############################################################################
262# Post-command test. #
263###############################################################################
264
265# Set up our post-command test configuration file.
266create_basic_configuration "${TEST_FOLDER_BASE}post-command/"
267cat >> "${TEST_FOLDER}conf" << EOF
268test_post_command_post_command()
269{
270 echo "\${1}" > "${TEST_FOLDER}/run/post"
271}
272HEADLESS_X_ORG_SERVER_POST_COMMAND="test_post_command_post_command"
273EOF
274
275print_line "post-command test"
276touch "${XORG_FOLDER}/xorg.conf.2"
277touch "${XORG_FOLDER}/xorg.conf.4"
278
279test_post_command()
280{
281 STATUS="$1"
282 case "${STATUS}" in
283 0)
284 LOG_FOLDER="${TEST_FOLDER}/log"
285 LOG="${LOG_FOLDER}/log"
286 if grep -q "2 4" "${TEST_FOLDER}/run/post"; then
287 printf "SUCCESS.\n"
288 else
289 printf "\nFAILED: post-command not executed.\n"
290 fi
291 ;;
292 *)
293 printf "\nFAILED: exit status ${STATUS}.\n"
294 esac
295}
296
297rm -f "${TEST_FOLDER}/run/post"
298scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
299PID=$!
300expect_exit "${PID}" 5 test_post_command
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