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