Skip to content

Commit 598fece

Browse files
committed
allow not to run composer at test container start, to ease parallel execution
1 parent 39ed1e4 commit 598fece

5 files changed

Lines changed: 71 additions & 150 deletions

File tree

tests/ci/docker/entrypoint.sh

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,9 @@ sed -e "s?^group =.*?group = ${USERNAME}?g" --in-place "${FPMCONF}"
7373
sed -e "s?^listen.owner =.*?listen.owner = ${USERNAME}?g" --in-place "${FPMCONF}"
7474
sed -e "s?^listen.group =.*?listen.group = ${USERNAME}?g" --in-place "${FPMCONF}"
7575

76-
if [ -f "${TESTS_ROOT_DIR}/composer.json" ]; then
77-
echo "[$(date)] Running Composer..."
78-
79-
# @todo if there is a composer.lock file present, there are chances it might be a leftover from when running the
80-
# container using a different os/php version. We should then back it up / do some symlink magic to make sure that
81-
# it matches the current php version and a hash of composer.json... (also symlink the vendor folder).
82-
# Make it at least optional to run composer at container start
83-
su "${USERNAME}" -c "cd ${TESTS_ROOT_DIR} && composer install"
84-
else
85-
# @todo should we exit?
86-
echo "Missing file '${TESTS_ROOT_DIR}/composer.json' - was the container started without the correct mount?" >&2
76+
# We make it optional to run composer at container start
77+
if [ "${INSTALL_ON_START}" = true ]; then
78+
/root/setup/setup_app.sh "${TESTS_ROOT_DIR}"
8779
fi
8880

8981
trap clean_up TERM

tests/ci/matrix.sh

Lines changed: 0 additions & 112 deletions
This file was deleted.

tests/ci/setup/setup_app.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
# Install and configure the 'app'
4+
# Has to be run as admin
5+
6+
set -e
7+
8+
echo "Installing the app dependencies..."
9+
10+
TESTS_ROOT_DIR="${1}"
11+
USERNAME="${2:-docker}"
12+
13+
if [ -f "${TESTS_ROOT_DIR}/composer.json" ]; then
14+
echo "[$(date)] Running Composer..."
15+
16+
# @todo if there is a composer.lock file present, there are chances it might be a leftover from when running the
17+
# container using a different os/php version. We could then back it up / do some symlink magic to make sure
18+
# that it matches the current php version and a hash of composer.json... (also symlink the vendor folder).
19+
20+
su "${USERNAME}" -c "cd ${TESTS_ROOT_DIR} && composer install --no-interaction --audit"
21+
else
22+
echo "Missing file '${TESTS_ROOT_DIR}/composer.json' - was the container started without the correct mount?" >&2
23+
exit 1
24+
fi

tests/ci/setup/setup_code_coverage.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ PHPCONFDIR_FPM=$(echo "$PHPCONFDIR_CLI" | sed 's|/cli/|/fpm/|')
1313

1414
enable_cc() {
1515
if [ -L "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini"; fi
16-
sudo ln -s $(realpath tests/ci/config/codecoverage_xdebug.ini) "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini"
16+
sudo ln -s "$(realpath tests/ci/config/codecoverage_xdebug.ini)" "${PHPCONFDIR_CLI}/99-codecoverage_xdebug.ini"
1717
if [ -L "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini" ]; then sudo rm "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini"; fi
18-
sudo ln -s $(realpath tests/ci/config/codecoverage_xdebug.ini) "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini"
18+
sudo ln -s "$(realpath tests/ci/config/codecoverage_xdebug.ini)" "${PHPCONFDIR_FPM}/99-codecoverage_xdebug.ini"
1919

2020
sudo service php-fpm restart
2121
}

tests/ci/vm.sh

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,29 @@ HOST_HTTPPORT="${HOST_HTTPPORT:-80}"
2020
HOST_HTTPSPORT="${HOST_HTTPSPORT:-443}"
2121
HOST_PROXYPORT="${HOST_PROXYPORT:-8080}"
2222

23+
CONTAINER_INSTALL_ON_START="${CONTAINER_INSTALL_ON_START:-true}"
2324
CONTAINER_NAME_PREFIX="${CONTAINER_NAME_PREFIX:-phpxmlrpc}"
2425
CONTAINER_IMAGE_PREFIX="${CONTAINER_IMAGE_PREFIX:-phpxmlrpc_}"
2526
CONTAINER_USER=docker
2627
CONTAINER_WORKSPACE_DIR="/home/${CONTAINER_USER}/workspace"
2728

28-
ROOT_DIR="$(dirname -- "$(dirname -- "$(dirname -- "$(readlink -f "$0")")")")"
2929
IMAGE_NAME="${CONTAINER_NAME_PREFIX}:${UBUNTU_VERSION}-${PHP_VERSION}"
3030
CONTAINER_NAME="${CONTAINER_IMAGE_PREFIX}${UBUNTU_VERSION}_${PHP_VERSION}"
3131

32+
ROOT_DIR="$(dirname -- "$(dirname -- "$(dirname -- "$(readlink -f "$0")")")")"
33+
3234
cd "$(dirname -- "$(readlink -f "$0")")"
3335

3436
help() {
3537
printf "Usage: vm.sh [OPTIONS] ACTION [OPTARGS]
3638
37-
Manages the Test Environment Docker Container
39+
Manages the Test Environment (Docker Container)
3840
3941
Commands:
4042
build build or rebuild the container image with the test env
4143
cleanup remove the container and its image
42-
enter start a shell session in the container
43-
exec [\$command] runs a single command in the container
44+
enter start a shell session in the running container
45+
exec [\$command] run a single command in the running container
4446
inspect
4547
logs
4648
port
@@ -58,14 +60,15 @@ Options:
5860
-h print help
5961
6062
Environment variables:
61-
to be set before the 'build' action
63+
used by the 'build' action
6264
PHP_VERSION default value: 'default', ie. the stock php version from the Ubuntu version in use. Other possible values: 5.6, 7.0 .. 7.4, 8.0 .. 8.4
6365
UBUNTU_VERSION default value: jammy. Other possible values: xenial, bionic, focal, noble
64-
to be set before the 'start' action
66+
used by the 'start' action
6567
HOST_HTTPPORT default value: 80. Set to 'no' not to publish the container's http port to the host
6668
HOST_HTTPSPORT default value: 443. Set to 'no' not to publish the container's https port to the host
6769
HOST_PROXYPORT default value: 8080. Set to 'no' not to publish the container's proxy http port to the host
68-
to be set before the 'runtests' and 'runcoverage' actions:
70+
CONTAINER_INSTALL_ON_START default value: true. Change to avoid having Composer be run at the start of the Container
71+
used by the 'runtests' and 'runcoverage' actions:
6972
HTTPSVERIFYHOST 0, 1 or 2. Default and recommended: 0
7073
HTTPSIGNOREPEER 0 or 1. Default and recommended: 1
7174
SSLVERSION 0 (auto), 2 (SSLv2) to 7 (tls 1.3). Default: 0
@@ -97,21 +100,21 @@ build() {
97100
if [ "$1" = '-r' ]; then
98101
# stop and remove existing containers built from a previous version of this image
99102
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
100-
stop
103+
stop -q
101104
docker rm "${CONTAINER_NAME}"
102105
fi
103106
fi
104107
fi
105108
}
106109

107110
start() {
108-
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" = running ]; then
109-
# @todo we should check that the env vars have not changed, and give a warning if so
111+
if [ "$(docker inspect --format '{{.State.Status}}' "${CONTAINER_NAME}" 2>/dev/null)" = running ]; then
112+
# @todo we should check that the env vars have not changed, and give a warning if so. Doable using `docker container cp`...
110113
echo "${CONTAINER_NAME} already started..."
111114
else
112115
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
113116
echo "starting existing container ${CONTAINER_NAME}..."
114-
# @todo we should check that the env vars have not changed, and give a warning if so. Doable using `inspect`...
117+
# @todo we should check that the env vars have not changed, and give a warning if so. Doable using `docker container inspect`...
115118
if docker start "${CONTAINER_NAME}"; then
116119
wait_for_bootstrap
117120
fi
@@ -129,11 +132,13 @@ start() {
129132
if [ "$HOST_PROXYPORT" != no ] && [ "$HOST_PROXYPORT" != '' ]; then
130133
PORTMAPPING="-p $((HOST_PROXYPORT-0)):8080 "
131134
fi
135+
# @todo map the composer cache dir to a host folder, to speed up reinstalls
132136
if docker run -d \
133137
$PORTMAPPING \
134138
--name "${CONTAINER_NAME}" \
135139
--env "CONTAINER_USER_UID=$(id -u)" --env "CONTAINER_USER_GID=$(id -g)" \
136140
--env "TESTS_ROOT_DIR=${CONTAINER_WORKSPACE_DIR}" \
141+
--env "INSTALL_ON_START=${CONTAINER_INSTALL_ON_START}" \
137142
--env HTTPSERVER=localhost \
138143
--env HTTPURI=/tests/index.php?demo=server/server.php \
139144
--env HTTPSSERVER=localhost \
@@ -148,11 +153,15 @@ start() {
148153
}
149154

150155
stop() {
151-
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" = exited ]; then
152-
echo "${CONTAINER_NAME} already stopped"
156+
if [ "$(docker inspect --format '{{.State.Status}}' "${CONTAINER_NAME}" 2>/dev/null)" = exited ]; then
157+
if [ "$1" != -q ]; then
158+
echo "${CONTAINER_NAME} already stopped"
159+
fi
153160
else
154161
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
155-
echo "stopping ${CONTAINER_NAME}..."
162+
if [ "$1" != -q ]; then
163+
echo "stopping ${CONTAINER_NAME}..."
164+
fi
156165
docker stop "${CONTAINER_NAME}"
157166
fi
158167
fi
@@ -166,7 +175,7 @@ case "${ACTION}" in
166175

167176
cleanup)
168177
if docker inspect "${CONTAINER_NAME}" >/dev/null 2>/dev/null; then
169-
docker stop "${CONTAINER_NAME}"
178+
stop -q
170179
docker rm "${CONTAINER_NAME}"
171180
fi
172181
docker rmi "${IMAGE_NAME}"
@@ -201,30 +210,38 @@ case "${ACTION}" in
201210
;;
202211

203212
runcoverage)
204-
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" != running ]; then
213+
if [ "$(docker inspect --format '{{.State.Status}}' "${CONTAINER_NAME}" 2>/dev/null)" != running ]; then
205214
start
206215
fi
207216
test -t 1 && USE_TTY="-t"
208-
# @todo clean up /tmp/phpxmlrpc and .phpunit.result.cache
209-
# @todo run composer install if it was not yet run or we are asked to
210-
if [ ! -d build ]; then mkdir build; fi
211-
docker exec -t "${CONTAINER_NAME}" "${CONTAINER_WORKSPACE_DIR}/tests/ci/setup/setup_code_coverage.sh" enable
217+
# @todo clean up /tmp/phpxmlrpc (in setup_code_coverage.sh?) and possibly .phpunit.result.cache
218+
# run composer install if it was not yet run
219+
if [ ! -f ../../composer.lock ] || [ ! -d ../../vendor ]; then
220+
# @todo do not swallow _all_ composer errors - just stuff such as an abandoned package
221+
docker exec $USE_TTY "${CONTAINER_NAME}" /root/setup/setup_app.sh "${CONTAINER_WORKSPACE_DIR}" || true
222+
fi
223+
if [ ! -d ./var/coverage ]; then mkdir -p ./var/coverage; fi
224+
docker exec -t "${CONTAINER_NAME}" "/root/setup/setup_code_coverage.sh" enable
212225
docker exec -i $USE_TTY \
213226
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
214227
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \
215228
--env "SSLVERSION=${SSLVERSION}" \
216229
--env DEBUG="${DEBUG}" \
217-
"${CONTAINER_NAME}" su "${CONTAINER_USER}" -c "./vendor/bin/phpunit --coverage-html build/coverage -v tests"
218-
docker exec -t "${CONTAINER_NAME}" "${CONTAINER_WORKSPACE_DIR}/tests/ci/setup/setup_code_coverage.sh" disable
230+
"${CONTAINER_NAME}" su "${CONTAINER_USER}" -c "./vendor/bin/phpunit --coverage-html tests/ci/var/coverage -v tests"
231+
docker exec -t "${CONTAINER_NAME}" "/root/setup/setup_code_coverage.sh" disable
219232
;;
220233

221234
runtests)
222-
if [ "$(docker inspect --format '{{.State.Status}}' ${CONTAINER_NAME} 2>/dev/null)" != running ]; then
235+
if [ "$(docker inspect --format '{{.State.Status}}' "${CONTAINER_NAME}" 2>/dev/null)" != running ]; then
223236
start
224237
fi
225238
test -t 1 && USE_TTY="-t"
226-
# @todo clean up /tmp/phpxmlrpc and .phpunit.result.cache
227-
# @todo run composer install if it was not yet run or we are asked to
239+
# @todo clean up .phpunit.result.cache?
240+
# run composer install if it was not yet run
241+
if [ ! -f ../../composer.lock ] || [ ! -d ../../vendor ] ; then
242+
# @todo do not swallow _all_ composer errors - just stuff such as an abandoned package
243+
docker exec $USE_TTY "${CONTAINER_NAME}" /root/setup/setup_app.sh "${CONTAINER_WORKSPACE_DIR}" || true
244+
fi
228245
docker exec -i $USE_TTY \
229246
--env "HTTPSVERIFYHOST=${HTTPSVERIFYHOST}" \
230247
--env "HTTPSIGNOREPEER=${HTTPSIGNOREPEER}" \

0 commit comments

Comments
 (0)