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 change to the parent.
|
---|
77 | VBOX_FOLDER="$(dirname "$0")/.."
|
---|
78 | VBOX_FOLDER=$(cd "${VBOX_FOLDER}" && pwd)
|
---|
79 | [ -d "${VBOX_FOLDER}" ] ||
|
---|
80 | abort "Failed to change to directory ${VBOX_FOLDER}.\n"
|
---|
81 | cd "${VBOX_FOLDER}"
|
---|
82 |
|
---|
83 | # Get our name for output.
|
---|
84 | TEST_NAME="$(basename "$0" .sh)"
|
---|
85 |
|
---|
86 | # Create a temporary directory for configuration and logging.
|
---|
87 | for i in 0 1 2 3 4 5 6 7 8 9; do
|
---|
88 | TEST_FOLDER="/tmp/${TEST_NAME}${i}"
|
---|
89 | mkdir -m 0700 "${TEST_FOLDER}" 2>/dev/null && break
|
---|
90 | done
|
---|
91 | [ -d "${TEST_FOLDER}" ] || abort "Failed to create a temporary folder\n"
|
---|
92 | # Clean up. Small race here, but probably not important.
|
---|
93 | trap "rm -r \"${TEST_FOLDER}\" 2>/dev/null" EXIT HUP INT QUIT ABRT TERM
|
---|
94 | # Server configuration folder.
|
---|
95 | XORG_FOLDER="${TEST_FOLDER}/xorg"
|
---|
96 | mkdir -p "${XORG_FOLDER}"
|
---|
97 |
|
---|
98 | # Simple start-up test.
|
---|
99 | print_line "simple start-up test"
|
---|
100 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
101 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
102 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
103 |
|
---|
104 | test_simple_start_up()
|
---|
105 | {
|
---|
106 | STATUS="$1"
|
---|
107 | case "${STATUS}" in
|
---|
108 | 0)
|
---|
109 | LOG_FOLDER="${TEST_FOLDER}/log"
|
---|
110 | LOG="${LOG_FOLDER}/log"
|
---|
111 | if grep -q "2 ${XORG_FOLDER}/xorg.conf.2 ${LOG_FOLDER}/log.2" "${LOG}" &&
|
---|
112 | grep -q "4 ${XORG_FOLDER}/xorg.conf.4 ${LOG_FOLDER}/log.4" "${LOG}"; then
|
---|
113 | printf "SUCCESS.\n"
|
---|
114 | else
|
---|
115 | printf "\nFAILED: incorrect log output.\n"
|
---|
116 | fi
|
---|
117 | ;;
|
---|
118 | *)
|
---|
119 | printf "\nFAILED: exit status ${STATUS}.\n"
|
---|
120 | esac
|
---|
121 | }
|
---|
122 |
|
---|
123 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
124 | PID=$!
|
---|
125 | expect_exit "${PID}" 5 test_simple_start_up
|
---|
126 | rm "${XORG_FOLDER}"/xorg.conf.*
|
---|
127 |
|
---|
128 | # No configuration files.
|
---|
129 | print_line "no configuration files"
|
---|
130 |
|
---|
131 | test_should_fail()
|
---|
132 | {
|
---|
133 | STATUS="$1"
|
---|
134 | case "${STATUS}" in
|
---|
135 | 0)
|
---|
136 | printf "\nFAILED: successful exit when an error was expected.\n"
|
---|
137 | ;;
|
---|
138 | *)
|
---|
139 | printf "SUCCESS.\n" # At least it behaved the way we wanted.
|
---|
140 | esac
|
---|
141 | }
|
---|
142 |
|
---|
143 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
144 | PID=$!
|
---|
145 | expect_exit "${PID}" 5 test_should_fail
|
---|
146 |
|
---|
147 | # Bad configuration files.
|
---|
148 | print_line "bad configuration files"
|
---|
149 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
150 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
151 | touch "${XORG_FOLDER}/xorg.conf.other"
|
---|
152 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
153 | PID=$!
|
---|
154 | expect_exit "${PID}" 5 test_should_fail
|
---|
155 | rm "${XORG_FOLDER}/"xorg.conf.*
|
---|
156 |
|
---|
157 | # Set up a configuration file for a long-running command.
|
---|
158 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
159 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
160 | HEADLESS_X_ORG_SERVER_COMMAND="echo $$ > ${TEST_FOLDER}/pid.\\\${screen}; cat"
|
---|
161 | EOF
|
---|
162 |
|
---|
163 | # Long running server command.
|
---|
164 | print_line "long running server command"
|
---|
165 | touch "${XORG_FOLDER}/xorg.conf.1"
|
---|
166 | touch "${XORG_FOLDER}/xorg.conf.5"
|
---|
167 | FAILURE=""
|
---|
168 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf"
|
---|
169 | PID="$!"
|
---|
170 | [ -r "${TEST_FOLDER}/pid.1" ] &&
|
---|
171 | [ -r "${TEST_FOLDER}/pid.5" ] &&
|
---|
172 | ps -p "$(cat "${TEST_FOLDER}/pid.1")" >/dev/null 2>&1 &&
|
---|
173 | ps -p "$(cat "${TEST_FOLDER}/pid.5")" >/dev/null 2>&1 ||
|
---|
174 | FAILURE="\nFAILED to start servers.\n"
|
---|
175 | [ -n "${PID}" ] && kill "${PID}"
|
---|
176 | { [ -r "${TEST_FOLDER}/pid.1" ] &&
|
---|
177 | ps -p "$(cat "${TEST_FOLDER}/pid.1")" >/dev/null 2>&1; } ||
|
---|
178 | { [ -r "${TEST_FOLDER}/pid.5" ] &&
|
---|
179 | ps -p "$(cat "${TEST_FOLDER}/pid.5")" >/dev/null 2>&1; } &&
|
---|
180 | FAILURE="\nFAILED to stop servers.\n" # To terminate or not to terminate?
|
---|
181 | if [ -z "${FAILURE}" ]; then
|
---|
182 | printf "${FAILURE}"
|
---|
183 | else
|
---|
184 | printf "SUCCESS.\n"
|
---|
185 | fi
|
---|
186 | rm "${XORG_FOLDER}/"xorg.conf.*
|
---|
187 | rm -f "${TEST_FOLDER}/pid.1" "${TEST_FOLDER}/pid.5"
|
---|
188 |
|
---|
189 | # Set up a configuration file with a pre-requisite.
|
---|
190 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
191 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
192 | HEADLESS_X_ORG_CHECK_PREREQUISITES="[ -e \\"${TEST_FOLDER}/run/prereq\\" ]"
|
---|
193 | EOF
|
---|
194 |
|
---|
195 | # Pre-requisite test.
|
---|
196 | print_line "configuration file with pre-requisite (sleeps)"
|
---|
197 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
198 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
199 | FAILURE=""
|
---|
200 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
201 | PID="$!"
|
---|
202 | sleep 1
|
---|
203 | ps -p "${PID}" > /dev/null 2>&1 || FAILURE="\nFAILED to wait for pre-requisite.\n"
|
---|
204 | touch "${TEST_FOLDER}/run/prereq"
|
---|
205 | if [ -z "${FAILURE}" ]; then
|
---|
206 | expect_exit "${PID}" 10 test_simple_start_up
|
---|
207 | else
|
---|
208 | printf "${FAILURE}"
|
---|
209 | fi
|
---|
210 | rm -r "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run"
|
---|
211 |
|
---|
212 | # Set up our pre-command test configuration file.
|
---|
213 | create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
|
---|
214 | cat >> "${TEST_FOLDER}/conf" << EOF
|
---|
215 | HEADLESS_X_ORG_SERVER_PRE_COMMAND="touch \"${TEST_FOLDER}/run/pre\""
|
---|
216 | EOF
|
---|
217 |
|
---|
218 | # Pre-command test.
|
---|
219 | print_line "pre-command test"
|
---|
220 | touch "${XORG_FOLDER}/xorg.conf.2"
|
---|
221 | touch "${XORG_FOLDER}/xorg.conf.4"
|
---|
222 |
|
---|
223 | test_pre_command()
|
---|
224 | {
|
---|
225 | STATUS="$1"
|
---|
226 | case "${STATUS}" in
|
---|
227 | 0)
|
---|
228 | LOG_FOLDER="${TEST_FOLDER}/log"
|
---|
229 | LOG="${LOG_FOLDER}/log"
|
---|
230 | if [ -e "${TEST_FOLDER}/run/pre" ]; then
|
---|
231 | printf "SUCCESS.\n"
|
---|
232 | else
|
---|
233 | printf "\nFAILED: pre-command not executed.\n"
|
---|
234 | fi
|
---|
235 | ;;
|
---|
236 | *)
|
---|
237 | printf "\nFAILED: exit status ${STATUS}.\n"
|
---|
238 | esac
|
---|
239 | }
|
---|
240 |
|
---|
241 | rm -f "${TEST_FOLDER}/run/pre"
|
---|
242 | ./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
|
---|
243 | PID=$!
|
---|
244 | expect_exit "${PID}" 5 test_pre_command
|
---|
245 | rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run/pre"
|
---|