VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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