summaryrefslogtreecommitdiffstats
path: root/src/VBox/Installer/linux/testcase/tstHeadlessXOrg.sh
blob: 51c6ddf79731b4c5e069b6dd7b2ffdea24399a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh
# $Id: tstHeadlessXOrg.sh $
## @file
# VirtualBox X Server auto-start service unit test.
#

#
# Copyright (C) 2012-2023 Oracle and/or its affiliates.
#
# This file is part of VirtualBox base platform packages, as
# available from https://www.virtualbox.org.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, in version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-3.0-only
#

## The function definition at the start of every non-trivial shell script!
abort()
{
  ## $@, ... Error text to output to standard error in printf format.
  format="$1"
  shift
  printf "${TEST_NAME}: ${format}" "$@" >&2
  exit 1
}

## Print a TESTING line.  Takes printf arguments but without a '\n'.
print_line()
{
  format="$1"
  shift
  printf "${TEST_NAME}: TESTING ${format}... " "$@"
}

## Expected a process to complete within a certain time and call a function if
# it does which should check whether the test was successful and print status
# information.  The function takes the exit status as its single parameter.
expect_exit()
{
  PID="$1"            ## The PID we are waiting for.
  TIME_OUT="$2"       ## The time-out before we terminate the process.
  TEST_FUNCTION="$3"  ## The function to call on exit to check the test result.

  # Give it time to complete.
  { sleep "${TIME_OUT}"; kill "${PID}" 2>/dev/null; } &

  wait "${PID}"
  STATUS="$?"
  case "${STATUS}" in
  143) # SIGTERM
    printf "\nFAILED: time-out.\n"
    ;;
  *)
    ${TEST_FUNCTION} "${STATUS}"
esac
}

## Create a simple configuration file.  Add items onto the end to override them
# on an item-by-item basis.
create_basic_configuration()
{
  TEST_FOLDER="${1}"
  FILE_NAME="${TEST_FOLDER}conf"    ## The name of the configuration file to create.
  BASE_FOLDER="${TEST_FOLDER}"
  XORG_FOLDER="${TEST_FOLDER}/xorg"
  mkdir -p "${XORG_FOLDER}"
  cat > "${FILE_NAME}" << EOF
HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
HEADLESS_X_ORG_LOG_FILE="log"
HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="true"
HEADLESS_X_ORG_SERVER_PRE_COMMAND=
HEADLESS_X_ORG_SERVER_COMMAND="echo"
EOF

}

# Get the directory where the script is located and the parent.
OUR_FOLDER="$(dirname "$0")"
OUR_FOLDER=$(cd "${OUR_FOLDER}" && pwd)
VBOX_FOLDER=$(cd "${OUR_FOLDER}/.." && pwd)
[ -d "${VBOX_FOLDER}" ] ||
  abort "Failed to change to directory ${VBOX_FOLDER}.\n"
cd "${VBOX_FOLDER}"

# Get our name for output.
TEST_NAME="$(basename "$0" .sh)"

# And remember the full path.
TEST_NAME_FULL="${OUR_FOLDER}/$(basename "$0")"

# Create a temporary directory for configuration and logging.
TEST_FOLDER_BASE="/tmp/${TEST_NAME} 99/"  # Space in the name to test quoting.
if [ -n "${TESTBOX_PATH_SCRATCH}" ]; then
    TEST_FOLDER_BASE="${TESTBOX_PATH_SCRATCH}/${TEST_NAME} 99/"
fi
{
  rm -rf "${TEST_FOLDER_BASE}" 2>/dev/null &&
    mkdir -m 0700 "${TEST_FOLDER_BASE}" 2>/dev/null
} || abort "Could not create test folder (${TEST_FOLDER_BASE}).\n"

###############################################################################
# Simple start-up test.                                                       #
###############################################################################
print_line "simple start-up test"
create_basic_configuration "${TEST_FOLDER_BASE}simple_start-up_test/"
touch "${XORG_FOLDER}/xorg.conf.2"
touch "${XORG_FOLDER}/xorg.conf.4"

test_simple_start_up()
{
  STATUS="$1"
  case "${STATUS}" in
  0)
    LOG_FOLDER="${TEST_FOLDER}/log"
    LOG="${LOG_FOLDER}/log"
    if grep -q "2 ${XORG_FOLDER}/xorg.conf.2 ${LOG_FOLDER}/Xorg.2.log" "${LOG}" &&
      grep -q "4 ${XORG_FOLDER}/xorg.conf.4 ${LOG_FOLDER}/Xorg.4.log" "${LOG}"; then
      printf "SUCCESS.\n"
    else
      printf "\nFAILED: incorrect log output.\n"
    fi
    ;;
  *)
    printf "\nFAILED: exit status ${STATUS}.\n"
  esac
}

scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID=$!
expect_exit "${PID}" 5 test_simple_start_up

###############################################################################
# No configuration files.                                                     #
###############################################################################
create_basic_configuration "${TEST_FOLDER_BASE}no_configuration_files/"
print_line "no configuration files"

test_should_fail()
{
  STATUS="$1"
  case "${STATUS}" in
  0)
    printf "\nFAILED: successful exit when an error was expected.\n"
    ;;
  *)
    printf "SUCCESS.\n"  # At least it behaved the way we wanted.
  esac
}

scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID=$!
expect_exit "${PID}" 5 test_should_fail

###############################################################################
# Bad configuration files.                                                    #
###############################################################################
print_line "bad configuration files"
create_basic_configuration "${TEST_FOLDER_BASE}bad_configuration_files/"
touch "${XORG_FOLDER}/xorg.conf.2"
touch "${XORG_FOLDER}/xorg.conf.4"
touch "${XORG_FOLDER}/xorg.conf.other"
scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID=$!
expect_exit "${PID}" 5 test_should_fail

###############################################################################
# Long running server command.                                                #
###############################################################################

# Set up a configuration file for a long-running command.
create_basic_configuration "${TEST_FOLDER_BASE}long-running_command/"
cat >> "${TEST_FOLDER}conf" << EOF
HEADLESS_X_ORG_SERVER_COMMAND="${TEST_FOLDER}command.sh"
EOF

cat > "${TEST_FOLDER}command.sh" << EOF
#!/bin/sh
touch "${TEST_FOLDER}stopped"
touch "${TEST_FOLDER}started"
trap "touch \\"${TEST_FOLDER}stopped\\"; exit" TERM
rm "${TEST_FOLDER}stopped"
while true; do :; done
EOF
chmod a+x "${TEST_FOLDER}command.sh"

print_line "long running server command"
touch "${XORG_FOLDER}/xorg.conf.5"
FAILURE=""
scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID="$!"
while [ ! -f "${TEST_FOLDER}started" ]; do :; done
while [ -f "${TEST_FOLDER}stopped" ]; do :; done
[ -n "${PID}" ] && kill "${PID}" 2>/dev/null
while [ ! -f "${TEST_FOLDER}stopped" ]; do :; done
printf "SUCCESS.\n"

###############################################################################
# Pre-requisite test.                                                         #
###############################################################################

# Set up a configuration file with a pre-requisite.
create_basic_configuration "${TEST_FOLDER_BASE}pre-requisite/"
cat >> "${TEST_FOLDER}conf" << EOF
HEADLESS_X_ORG_WAIT_FOR_PREREQUISITES="false"
EOF

print_line "configuration file with failed pre-requisite"
touch "${XORG_FOLDER}/xorg.conf.2"
touch "${XORG_FOLDER}/xorg.conf.4"
if scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf"; then
  echo "\nFAILED to stop for failed pre-requisite.\n"
else
  echo "SUCCESS"
fi

###############################################################################
# Pre-command test.                                                           #
###############################################################################

# Set up our pre-command test configuration file.
create_basic_configuration "${TEST_FOLDER_BASE}pre-command/"

cat >> "${TEST_FOLDER}conf" << EOF
test_pre_command_server_pre_command()
{
  touch "${TEST_FOLDER}/run/pre"
}
test_pre_command_server_command()
{
  cp "${TEST_FOLDER}/run/pre" "${TEST_FOLDER}/run/pre2"
}
HEADLESS_X_ORG_SERVER_PRE_COMMAND="test_pre_command_server_pre_command"
HEADLESS_X_ORG_SERVER_COMMAND="test_pre_command_server_command"
EOF

print_line "pre-command test"
touch "${XORG_FOLDER}/xorg.conf.2"

test_pre_command()
{
  STATUS="$1"
  case "${STATUS}" in
  0)
    LOG_FOLDER="${TEST_FOLDER}/log"
    LOG="${LOG_FOLDER}/log"
    if [ -e "${TEST_FOLDER}/run/pre" ] && [ -e "${TEST_FOLDER}/run/pre2" ]; then
      printf "SUCCESS.\n"
    else
      printf "\nFAILED: pre-command not executed.\n"
    fi
    ;;
  *)
    printf "\nFAILED: exit status ${STATUS}.\n"
  esac
}

rm -f "${TEST_FOLDER}/run/pre"
scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID=$!
expect_exit "${PID}" 5 test_pre_command

###############################################################################
# Post-command test.                                                          #
###############################################################################

# Set up our post-command test configuration file.
create_basic_configuration "${TEST_FOLDER_BASE}post-command/"
cat >> "${TEST_FOLDER}conf" << EOF
test_post_command_post_command()
{
  echo "\${1}" > "${TEST_FOLDER}/run/post"
}
HEADLESS_X_ORG_SERVER_POST_COMMAND="test_post_command_post_command"
EOF

print_line "post-command test"
touch "${XORG_FOLDER}/xorg.conf.2"
touch "${XORG_FOLDER}/xorg.conf.4"

test_post_command()
{
  STATUS="$1"
  case "${STATUS}" in
  0)
    LOG_FOLDER="${TEST_FOLDER}/log"
    LOG="${LOG_FOLDER}/log"
    if grep -q "2 4" "${TEST_FOLDER}/run/post"; then
      printf "SUCCESS.\n"
    else
      printf "\nFAILED: post-command not executed.\n"
    fi
    ;;
  *)
    printf "\nFAILED: exit status ${STATUS}.\n"
  esac
}

rm -f "${TEST_FOLDER}/run/post"
scripts/VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}conf" &
PID=$!
expect_exit "${PID}" 5 test_post_command