1 | #!/bin/sh
|
---|
2 | # $Id: tstHeadlessXOrg.sh 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # VirtualBox X Server auto-start service unit test.
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2012-2023 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!
|
---|
30 | abort()
|
---|
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'.
|
---|
40 | print_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.
|
---|
50 | expect_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}"
|
---|
67 | esac
|
---|
68 | }
|
---|
69 |
|
---|
70 | ## Create a simple configuration file. Add items onto the end to override them
|
---|
71 | # on an item-by-item basis.
|
---|
72 | create_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
|
---|
80 | HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
|
---|
81 | HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
|
---|
82 | HEADLESS_X_ORG_LOG_FILE="log"
|
---|
83 | HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
|
---|
84 | HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="true"
|
---|
85 | HEADLESS_X_ORG_SERVER_PRE_COMMAND=
|
---|
86 | HEADLESS_X_ORG_SERVER_COMMAND="echo"
|
---|
87 | EOF
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | # Get the directory where the script is located and the parent.
|
---|
92 | OUR_FOLDER="$(dirname "$0")"
|
---|
93 | OUR_FOLDER=$(cd "${OUR_FOLDER}" && pwd)
|
---|
94 | VBOX_FOLDER=$(cd "${OUR_FOLDER}/.." && pwd)
|
---|
95 | [ -d "${VBOX_FOLDER}" ] ||
|
---|
96 | abort "Failed to change to directory ${VBOX_FOLDER}.\n"
|
---|
97 | cd "${VBOX_FOLDER}"
|
---|
98 |
|
---|
99 | # Get our name for output.
|
---|
100 | TEST_NAME="$(basename "$0" .sh)"
|
---|
101 |
|
---|
102 | # And remember the full path.
|
---|
103 | TEST_NAME_FULL="${OUR_FOLDER}/$(basename "$0")"
|
---|
104 |
|
---|
105 | # Create a temporary directory for configuration and logging.
|
---|
106 | TEST_FOLDER_BASE="/tmp/${TEST_NAME} 99/" # Space in the name to test quoting.
|
---|
107 | if [ -n "${TESTBOX_PATH_SCRATCH}" ]; then
|
---|
108 | TEST_FOLDER_BASE="${TESTBOX_PATH_SCRATCH}/${TEST_NAME} 99/"
|
---|
109 | fi
|
---|
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 | ###############################################################################
|
---|
118 | print_line "simple start-up test"
|
---|
119 | create_basic_configuration "${TEST_FOLDER_BASE}simple_start-up_test/"
|
---|
120 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
121 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
122 |
|
---|
123 | test_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 |
|
---|
142 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
143 | PID=$!
|
---|
144 | expect_exit "${PID}" 5 test_simple_start_up
|
---|
145 |
|
---|
146 | ###############################################################################
|
---|
147 | # No configuration files. #
|
---|
148 | ###############################################################################
|
---|
149 | create_basic_configuration "${TEST_FOLDER_BASE}no_configuration_files/"
|
---|
150 | print_line "no configuration files"
|
---|
151 |
|
---|
152 | test_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 |
|
---|
164 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
165 | PID=$!
|
---|
166 | expect_exit "${PID}" 5 test_should_fail
|
---|
167 |
|
---|
168 | ###############################################################################
|
---|
169 | # Bad configuration files. #
|
---|
170 | ###############################################################################
|
---|
171 | print_line "bad configuration files"
|
---|
172 | create_basic_configuration "${TEST_FOLDER_BASE}bad_configuration_files/"
|
---|
173 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
174 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
175 | touch "${XORG_FOLDER}/xorg.conf.other"
|
---|
176 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
177 | PID=$!
|
---|
178 | expect_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.
|
---|
185 | create_basic_configuration "${TEST_FOLDER_BASE}long-running_command/"
|
---|
186 | cat >> "${TEST_FOLDER}conf" << EOF
|
---|
187 | HEADLESS_X_ORG_SERVER_COMMAND="${TEST_FOLDER}command.sh"
|
---|
188 | EOF
|
---|
189 |
|
---|
190 | cat > "${TEST_FOLDER}command.sh" << EOF
|
---|
191 | #!/bin/sh
|
---|
192 | touch "${TEST_FOLDER}stopped"
|
---|
193 | touch "${TEST_FOLDER}started"
|
---|
194 | trap "touch \\"${TEST_FOLDER}stopped\\"; exit" TERM
|
---|
195 | rm "${TEST_FOLDER}stopped"
|
---|
196 | while true; do :; done
|
---|
197 | EOF
|
---|
198 | chmod a+x "${TEST_FOLDER}command.sh"
|
---|
199 |
|
---|
200 | print_line "long running server command"
|
---|
201 | touch "${XORG_FOLDER}/xorg.conf.5"
|
---|
202 | FAILURE=""
|
---|
203 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
204 | PID="$!"
|
---|
205 | while [ ! -f "${TEST_FOLDER}started" ]; do :; done
|
---|
206 | while [ -f "${TEST_FOLDER}stopped" ]; do :; done
|
---|
207 | [ -n "${PID}" ] && kill "${PID}" 2>/dev/null
|
---|
208 | while [ ! -f "${TEST_FOLDER}stopped" ]; do :; done
|
---|
209 | printf "SUCCESS.\n"
|
---|
210 |
|
---|
211 | ###############################################################################
|
---|
212 | # Pre-requisite test. #
|
---|
213 | ###############################################################################
|
---|
214 |
|
---|
215 | # Set up a configuration file with a pre-requisite.
|
---|
216 | create_basic_configuration "${TEST_FOLDER_BASE}pre-requisite/"
|
---|
217 | cat >> "${TEST_FOLDER}conf" << EOF
|
---|
218 | HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="false"
|
---|
219 | EOF
|
---|
220 |
|
---|
221 | print_line "configuration file with failed pre-requisite"
|
---|
222 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
223 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
224 | if scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf"; then
|
---|
225 | echo "\nFAILED to stop for failed pre-requisite.\n"
|
---|
226 | else
|
---|
227 | echo "SUCCESS"
|
---|
228 | fi
|
---|
229 |
|
---|
230 | ###############################################################################
|
---|
231 | # Pre-command test. #
|
---|
232 | ###############################################################################
|
---|
233 |
|
---|
234 | # Set up our pre-command test configuration file.
|
---|
235 | create_basic_configuration "${TEST_FOLDER_BASE}pre-command/"
|
---|
236 |
|
---|
237 | cat >> "${TEST_FOLDER}conf" << EOF
|
---|
238 | test_pre_command_server_pre_command()
|
---|
239 | {
|
---|
240 | touch "${TEST_FOLDER}/run/pre"
|
---|
241 | }
|
---|
242 | test_pre_command_server_command()
|
---|
243 | {
|
---|
244 | cp "${TEST_FOLDER}/run/pre" "${TEST_FOLDER}/run/pre2"
|
---|
245 | }
|
---|
246 | HEADLESS_X_ORG_SERVER_PRE_COMMAND="test_pre_command_server_pre_command"
|
---|
247 | HEADLESS_X_ORG_SERVER_COMMAND="test_pre_command_server_command"
|
---|
248 | EOF
|
---|
249 |
|
---|
250 | print_line "pre-command test"
|
---|
251 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
252 |
|
---|
253 | test_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 |
|
---|
271 | rm -f "${TEST_FOLDER}/run/pre"
|
---|
272 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
273 | PID=$!
|
---|
274 | expect_exit "${PID}" 5 test_pre_command
|
---|
275 |
|
---|
276 | ###############################################################################
|
---|
277 | # Post-command test. #
|
---|
278 | ###############################################################################
|
---|
279 |
|
---|
280 | # Set up our post-command test configuration file.
|
---|
281 | create_basic_configuration "${TEST_FOLDER_BASE}post-command/"
|
---|
282 | cat >> "${TEST_FOLDER}conf" << EOF
|
---|
283 | test_post_command_post_command()
|
---|
284 | {
|
---|
285 | echo "\${1}" > "${TEST_FOLDER}/run/post"
|
---|
286 | }
|
---|
287 | HEADLESS_X_ORG_SERVER_POST_COMMAND="test_post_command_post_command"
|
---|
288 | EOF
|
---|
289 |
|
---|
290 | print_line "post-command test"
|
---|
291 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
292 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
293 |
|
---|
294 | test_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 |
|
---|
312 | rm -f "${TEST_FOLDER}/run/post"
|
---|
313 | scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
|
---|
314 | PID=$!
|
---|
315 | expect_exit "${PID}" 5 test_post_command
|
---|