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!
|
---|
17 | abort()
|
---|
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'.
|
---|
27 | print_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.
|
---|
37 | expect_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}"
|
---|
54 | esac
|
---|
55 | }
|
---|
56 |
|
---|
57 | ## Create a simple configuration file. Add items onto the end to override them
|
---|
58 | # on an item-by-item basis.
|
---|
59 | create_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
|
---|
64 | HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
|
---|
65 | HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
|
---|
66 | HEADLESS_X_ORG_LOG_FILE="log"
|
---|
67 | HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
|
---|
68 | HEADLESS_X_ORG_CHECK_PREREQUISITES=
|
---|
69 | HEADLESS_X_ORG_SERVER_PRE_COMMAND=
|
---|
70 | HEADLESS_X_ORG_SERVER_COMMAND="echo \\\${screen} \\\${conf_file} \\\${log_file}"
|
---|
71 | HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="log.\\\${screen}"
|
---|
72 | EOF
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | # Get the directory where the script is located and the parent.
|
---|
77 | OUR_FOLDER="$(dirname "$0")"
|
---|
78 | OUR_FOLDER=$(cd "${OUR_FOLDER}" && pwd)
|
---|
79 | VBOX_FOLDER=$(cd "${OUR_FOLDER}/.." && pwd)
|
---|
80 | [ -d "${VBOX_FOLDER}" ] ||
|
---|
81 | abort "Failed to change to directory ${VBOX_FOLDER}.\n"
|
---|
82 | cd "${VBOX_FOLDER}"
|
---|
83 |
|
---|
84 | # Get our name for output.
|
---|
85 | TEST_NAME="$(basename "$0" .sh)"
|
---|
86 |
|
---|
87 | # And remember the full path.
|
---|
88 | TEST_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.
|
---|
95 | for 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
|
---|
98 | done
|
---|
99 | [ -d "${TEST_FOLDER}" ] || abort "Failed to create a temporary folder\n"
|
---|
100 | # Clean up. Small race here, but probably not important.
|
---|
101 | trap "rm -r \"${TEST_FOLDER}\" 2>/dev/null" EXIT HUP INT QUIT ABRT TERM
|
---|
102 | # Server configuration folder.
|
---|
103 | XORG_FOLDER="${TEST_FOLDER}/xorg"
|
---|
104 | mkdir -p "${XORG_FOLDER}"
|
---|
105 |
|
---|
106 | # Simple start-up test.
|
---|
107 | print_line "simple start-up test"
|
---|
108 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
109 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
110 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
111 |
|
---|
112 | test_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" &
|
---|
132 | PID=$!
|
---|
133 | expect_exit "${PID}" 5 test_simple_start_up
|
---|
134 | rm "${XORG_FOLDER}"/xorg.conf.*
|
---|
135 |
|
---|
136 | # No configuration files.
|
---|
137 | print_line "no configuration files"
|
---|
138 |
|
---|
139 | test_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" &
|
---|
152 | PID=$!
|
---|
153 | expect_exit "${PID}" 5 test_should_fail
|
---|
154 |
|
---|
155 | # Bad configuration files.
|
---|
156 | print_line "bad configuration files"
|
---|
157 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
158 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
159 | touch "${XORG_FOLDER}/xorg.conf.other"
|
---|
160 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
161 | PID=$!
|
---|
162 | expect_exit "${PID}" 5 test_should_fail
|
---|
163 | rm "${XORG_FOLDER}/"xorg.conf.*
|
---|
164 |
|
---|
165 | # Set up a configuration file for a long-running command.
|
---|
166 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
167 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
168 | HEADLESS_X_ORG_SERVER_COMMAND="\"${TEST_NAME_FULL}\" --test-sleep \\\${screen}"
|
---|
169 | EOF
|
---|
170 |
|
---|
171 | # Long running server command.
|
---|
172 | print_line "long running server command (sleeps)"
|
---|
173 | touch "${XORG_FOLDER}/xorg.conf.1"
|
---|
174 | touch "${XORG_FOLDER}/xorg.conf.5"
|
---|
175 | FAILURE=""
|
---|
176 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
177 | PID="$!"
|
---|
178 | STARTED=""
|
---|
179 | for 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
|
---|
186 | done
|
---|
187 | [ -n "${STARTED}" ] || FAILURE="\nFAILED to start servers.\n"
|
---|
188 | [ -n "${PID}" ] && kill "${PID}" 2>/dev/null
|
---|
189 | STOPPED=""
|
---|
190 | if [ -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?
|
---|
201 | fi
|
---|
202 | if [ -n "${FAILURE}" ]; then
|
---|
203 | printf "${FAILURE}"
|
---|
204 | else
|
---|
205 | printf "SUCCESS.\n"
|
---|
206 | fi
|
---|
207 | rm "${XORG_FOLDER}/"xorg.conf.*
|
---|
208 |
|
---|
209 | # Set up a configuration file with a pre-requisite.
|
---|
210 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
211 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
212 | HEADLESS_X_ORG_CHECK_PREREQUISITES="[ -e \\"${TEST_FOLDER}/run/prereq\\" ]"
|
---|
213 | EOF
|
---|
214 |
|
---|
215 | # Pre-requisite test.
|
---|
216 | print_line "configuration file with pre-requisite (sleeps)"
|
---|
217 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
218 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
219 | FAILURE=""
|
---|
220 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
221 | PID="$!"
|
---|
222 | sleep 1
|
---|
223 | ps -p "${PID}" > /dev/null 2>&1 || FAILURE="\nFAILED to wait for pre-requisite.\n"
|
---|
224 | touch "${TEST_FOLDER}/run/prereq"
|
---|
225 | if [ -z "${FAILURE}" ]; then
|
---|
226 | expect_exit "${PID}" 10 test_simple_start_up
|
---|
227 | else
|
---|
228 | printf "${FAILURE}"
|
---|
229 | fi
|
---|
230 | rm -r "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run"
|
---|
231 |
|
---|
232 | # Set up our pre-command test configuration file.
|
---|
233 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
234 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
235 | HEADLESS_X_ORG_SERVER_PRE_COMMAND="touch \"${TEST_FOLDER}/run/pre\""
|
---|
236 | HEADLESS_X_ORG_SERVER_COMMAND="cp \"${TEST_FOLDER}/run/pre\" \"${TEST_FOLDER}/run/pre2\""
|
---|
237 | EOF
|
---|
238 |
|
---|
239 | # Pre-command test.
|
---|
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 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
263 | PID=$!
|
---|
264 | expect_exit "${PID}" 5 test_pre_command
|
---|
265 | rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}"/run/pre*
|
---|
266 |
|
---|
267 | # Set up our post-command test configuration file.
|
---|
268 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
269 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
270 | HEADLESS_X_ORG_SERVER_COMMAND="rm -f \"${TEST_FOLDER}/run/post\""
|
---|
271 | HEADLESS_X_ORG_SERVER_POST_COMMAND="touch \"${TEST_FOLDER}/run/post\""
|
---|
272 | EOF
|
---|
273 |
|
---|
274 | # Post-command test.
|
---|
275 | print_line "post-command test"
|
---|
276 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
277 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
278 |
|
---|
279 | test_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 |
|
---|
297 | rm -f "${TEST_FOLDER}/run/post"
|
---|
298 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
299 | PID=$!
|
---|
300 | expect_exit "${PID}" 5 test_post_command
|
---|
301 | rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run/post"
|
---|