VirtualBox

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

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

Installer/linux: add post command and a couple of adjustments to headless X.Org script.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1#!/bin/sh
2#
3# VirtualBox X Server auto-start service unit test.
4#
5# Copyright (C) 2012 Oracle Corporation
6#
7# This file is part of VirtualBox Open Source Edition (OSE), as
8# available from http://www.virtualbox.org. This file is free software;
9# you can redistribute it and/or modify it under the terms of the GNU
10# General Public License (GPL) as published by the Free Software
11# Foundation, in version 2 as it comes in the "COPYING" file of the
12# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14#
15
16## The function definition at the start of every non-trivial shell script!
17abort()
18{
19 ## $@, ... Error text to output to standard error in printf format.
20 format="$1"
21 shift
22 printf "${TEST_NAME}: ${format}" "$@" >&2
23 exit 1
24}
25
26## Print a TESTING line. Takes printf arguments but without a '\n'.
27print_line()
28{
29 format="$1"
30 shift
31 printf "${TEST_NAME}: TESTING ${format}... " "$@"
32}
33
34## Expected a process to complete within a certain time and call a function if
35# it does which should check whether the test was successful and print status
36# information. The function takes the exit status as its single parameter.
37expect_exit()
38{
39 PID="$1" ## The PID we are waiting for.
40 TIME_OUT="$2" ## The time-out before we terminate the process.
41 TEST_FUNCTION="$3" ## The function to call on exit to check the test result.
42
43 # Give it time to complete.
44 { sleep "${TIME_OUT}"; kill "${PID}" 2>/dev/null; } &
45
46 wait "${PID}"
47 STATUS="$?"
48 case "${STATUS}" in
49 143) # SIGTERM
50 printf "\nFAILED: time-out.\n"
51 ;;
52 *)
53 ${TEST_FUNCTION} "${STATUS}"
54esac
55}
56
57## Create a simple configuration file. Add items onto the end to override them
58# on an item-by-item basis.
59create_basic_configuration_file()
60{
61 FILE_NAME="$1" ## The name of the configuration file to create.
62 BASE_FOLDER="$2" ## The basic folder for creating things under.
63 cat > "${FILE_NAME}" << EOF
64HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
65HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
66HEADLESS_X_ORG_LOG_FILE="log"
67HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
68HEADLESS_X_ORG_CHECK_PREREQUISITES=
69HEADLESS_X_ORG_SERVER_PRE_COMMAND=
70HEADLESS_X_ORG_SERVER_COMMAND="echo \\\${screen} \\\${conf_file} \\\${log_file}"
71HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="log.\\\${screen}"
72EOF
73
74}
75
76# Get the directory where the script is located and the parent.
77OUR_FOLDER="$(dirname "$0")"
78OUR_FOLDER=$(cd "${OUR_FOLDER}" && pwd)
79VBOX_FOLDER=$(cd "${OUR_FOLDER}/.." && pwd)
80[ -d "${VBOX_FOLDER}" ] ||
81 abort "Failed to change to directory ${VBOX_FOLDER}.\n"
82cd "${VBOX_FOLDER}"
83
84# Get our name for output.
85TEST_NAME="$(basename "$0" .sh)"
86
87# And remember the full path.
88TEST_NAME_FULL="${OUR_FOLDER}/$(basename "$0")"
89
90# We use this to test a long-running process
91[ x"$1" = "x--test-sleep" ] &&
92 while true; do true; done
93
94# Create a temporary directory for configuration and logging.
95for i in 0 1 2 3 4 5 6 7 8 9; do
96 TEST_FOLDER="/tmp/${TEST_NAME}${i}"
97 mkdir -m 0700 "${TEST_FOLDER}" 2>/dev/null && break
98done
99[ -d "${TEST_FOLDER}" ] || abort "Failed to create a temporary folder\n"
100# Clean up. Small race here, but probably not important.
101trap "rm -r \"${TEST_FOLDER}\" 2>/dev/null" EXIT HUP INT QUIT ABRT TERM
102# Server configuration folder.
103XORG_FOLDER="${TEST_FOLDER}/xorg"
104mkdir -p "${XORG_FOLDER}"
105
106# Simple start-up test.
107print_line "simple start-up test"
108create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
109touch "${XORG_FOLDER}/xorg.conf.2"
110touch "${XORG_FOLDER}/xorg.conf.4"
111
112test_simple_start_up()
113{
114 STATUS="$1"
115 case "${STATUS}" in
116 0)
117 LOG_FOLDER="${TEST_FOLDER}/log"
118 LOG="${LOG_FOLDER}/log"
119 if grep -q "2 ${XORG_FOLDER}/xorg.conf.2 ${LOG_FOLDER}/log.2" "${LOG}" &&
120 grep -q "4 ${XORG_FOLDER}/xorg.conf.4 ${LOG_FOLDER}/log.4" "${LOG}"; then
121 printf "SUCCESS.\n"
122 else
123 printf "\nFAILED: incorrect log output.\n"
124 fi
125 ;;
126 *)
127 printf "\nFAILED: exit status ${STATUS}.\n"
128 esac
129}
130
131./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
132PID=$!
133expect_exit "${PID}" 5 test_simple_start_up
134rm "${XORG_FOLDER}"/xorg.conf.*
135
136# No configuration files.
137print_line "no configuration files"
138
139test_should_fail()
140{
141 STATUS="$1"
142 case "${STATUS}" in
143 0)
144 printf "\nFAILED: successful exit when an error was expected.\n"
145 ;;
146 *)
147 printf "SUCCESS.\n" # At least it behaved the way we wanted.
148 esac
149}
150
151./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
152PID=$!
153expect_exit "${PID}" 5 test_should_fail
154
155# Bad configuration files.
156print_line "bad configuration files"
157touch "${XORG_FOLDER}/xorg.conf.2"
158touch "${XORG_FOLDER}/xorg.conf.4"
159touch "${XORG_FOLDER}/xorg.conf.other"
160./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
161PID=$!
162expect_exit "${PID}" 5 test_should_fail
163rm "${XORG_FOLDER}/"xorg.conf.*
164
165# Set up a configuration file for a long-running command.
166create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
167cat >> "${TEST_FOLDER}/conf" << EOF
168HEADLESS_X_ORG_SERVER_COMMAND="\"${TEST_NAME_FULL}\" --test-sleep \\\${screen}"
169EOF
170
171# Long running server command.
172print_line "long running server command (sleeps)"
173touch "${XORG_FOLDER}/xorg.conf.1"
174touch "${XORG_FOLDER}/xorg.conf.5"
175FAILURE=""
176./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
177PID="$!"
178STARTED=""
179for i in 1 2 3 4 5; do
180 sleep 1 # Make sure it runs for at least one second.
181 if ps -a -f | grep "${TEST_NAME}.*1" | grep -q -v grep &&
182 ps -a -f | grep "${TEST_NAME}.*5" | grep -q -v grep; then
183 STARTED="true"
184 break
185 fi
186done
187[ -n "${STARTED}" ] || FAILURE="\nFAILED to start servers.\n"
188[ -n "${PID}" ] && kill "${PID}" 2>/dev/null
189STOPPED=""
190if [ -z "${FAILURE}" ]; then
191 for i in 1 2 3 4 5; do
192 if ! ps -a -f | grep "${TEST_NAME}.*1" | grep -q -v grep &&
193 ! ps -a -f | grep "${TEST_NAME}.*5" | grep -q -v grep; then
194 STOPPED="true"
195 break;
196 fi
197 sleep 1
198 done
199 [ -n "${STOPPED}" ] ||
200 FAILURE="\nFAILED to stop servers.\n" # To terminate or not to terminate?
201fi
202if [ -n "${FAILURE}" ]; then
203 printf "${FAILURE}"
204else
205 printf "SUCCESS.\n"
206fi
207rm "${XORG_FOLDER}/"xorg.conf.*
208
209# Set up a configuration file with a pre-requisite.
210create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
211cat >> "${TEST_FOLDER}/conf" << EOF
212HEADLESS_X_ORG_CHECK_PREREQUISITES="[ -e \\"${TEST_FOLDER}/run/prereq\\" ]"
213EOF
214
215# Pre-requisite test.
216print_line "configuration file with pre-requisite (sleeps)"
217touch "${XORG_FOLDER}/xorg.conf.2"
218touch "${XORG_FOLDER}/xorg.conf.4"
219FAILURE=""
220./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
221PID="$!"
222sleep 1
223ps -p "${PID}" > /dev/null 2>&1 || FAILURE="\nFAILED to wait for pre-requisite.\n"
224touch "${TEST_FOLDER}/run/prereq"
225if [ -z "${FAILURE}" ]; then
226 expect_exit "${PID}" 10 test_simple_start_up
227else
228 printf "${FAILURE}"
229fi
230rm -r "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run"
231
232# Set up our pre-command test configuration file.
233create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
234cat >> "${TEST_FOLDER}/conf" << EOF
235HEADLESS_X_ORG_SERVER_PRE_COMMAND="touch \"${TEST_FOLDER}/run/pre\""
236HEADLESS_X_ORG_SERVER_COMMAND="cp \"${TEST_FOLDER}/run/pre\" \"${TEST_FOLDER}/run/pre2\""
237EOF
238
239# Pre-command test.
240print_line "pre-command test"
241touch "${XORG_FOLDER}/xorg.conf.2"
242
243test_pre_command()
244{
245 STATUS="$1"
246 case "${STATUS}" in
247 0)
248 LOG_FOLDER="${TEST_FOLDER}/log"
249 LOG="${LOG_FOLDER}/log"
250 if [ -e "${TEST_FOLDER}/run/pre" ] && [ -e "${TEST_FOLDER}/run/pre2" ]; then
251 printf "SUCCESS.\n"
252 else
253 printf "\nFAILED: pre-command not executed.\n"
254 fi
255 ;;
256 *)
257 printf "\nFAILED: exit status ${STATUS}.\n"
258 esac
259}
260
261rm -f "${TEST_FOLDER}/run/pre"
262./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
263PID=$!
264expect_exit "${PID}" 5 test_pre_command
265rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}"/run/pre*
266
267# Set up our post-command test configuration file.
268create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
269cat >> "${TEST_FOLDER}/conf" << EOF
270HEADLESS_X_ORG_SERVER_COMMAND="rm -f \"${TEST_FOLDER}/run/post\""
271HEADLESS_X_ORG_SERVER_POST_COMMAND="touch \"${TEST_FOLDER}/run/post\""
272EOF
273
274# Post-command test.
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 [ -e "${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"
298./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
299PID=$!
300expect_exit "${PID}" 5 test_post_command
301rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run/post"
Note: See TracBrowser for help on using the repository browser.

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