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