From 5ebc2e3f2c666d89a29af19a15eb5d14f8348565 Mon Sep 17 00:00:00 2001
From: Benjamin Franzke <ben@bnf.dev>
Date: Tue, 19 Mar 2024 06:40:15 +0100
Subject: [PATCH] [TASK] Apply some runTests.sh cleanups
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 * Always clean-up containers when Control-C is pressed
 * Enforce removing networks even if some containers have not been
   fully stopped (does not work with older docker versions, therefore
   only enabled for podman)
 * Bail out if `waitFor` failed instead of resuming with subsequent
   commands
 * Avoid waiting unnecessarily for VNC-web port 7900 in headless mode
 * Do not derive `--pull=never` from the `$CI` environment variable, as
   `$CI` is set both in gitlab [1] and github [2] actions.
   runTests.sh might be used in github actions, therefore this flag is
   moved into our gitlab-ci specific configuration.
 * Fix typos

[1] https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
[2] https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables

Resolves: #103420
Releases: main, 12.4, 11.5
Change-Id: Ie8fcdc5ebc5ba651242bb54faffdfd8de0dc6026
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83515
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benjamin Franzke <ben@bnf.dev>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benjamin Franzke <ben@bnf.dev>
---
 Build/Scripts/runTests.sh | 28 +++++++++++++++++++++-------
 Build/gitlab-ci.yml       |  4 ++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/Build/Scripts/runTests.sh b/Build/Scripts/runTests.sh
index 9d4f7ed7a420..fcdfe6052d90 100755
--- a/Build/Scripts/runTests.sh
+++ b/Build/Scripts/runTests.sh
@@ -4,6 +4,8 @@
 # TYPO3 core test runner based on docker or podman
 #
 
+trap 'cleanUp;exit 2' SIGINT
+
 waitFor() {
     local HOST=${1}
     local PORT=${2}
@@ -19,6 +21,9 @@ waitFor() {
         done;
     "
     ${CONTAINER_BIN} run ${CONTAINER_COMMON_PARAMS} --name wait-for-${SUFFIX} ${XDEBUG_MODE} -e XDEBUG_CONFIG="${XDEBUG_CONFIG}" ${IMAGE_ALPINE} /bin/sh -c "${TESTCOMMAND}"
+    if [[ $? -gt 0 ]]; then
+        kill -SIGINT -$$
+    fi
 }
 
 cleanUp() {
@@ -26,7 +31,11 @@ cleanUp() {
     for ATTACHED_CONTAINER in ${ATTACHED_CONTAINERS}; do
         ${CONTAINER_BIN} kill ${ATTACHED_CONTAINER} >/dev/null
     done
-    ${CONTAINER_BIN} network rm ${NETWORK} >/dev/null
+    if [ ${CONTAINER_BIN} = "docker" ]; then
+        ${CONTAINER_BIN} network rm ${NETWORK} >/dev/null
+    else
+        ${CONTAINER_BIN} network rm -f ${NETWORK} >/dev/null
+    fi
 }
 
 handleDbmsOptions() {
@@ -399,7 +408,7 @@ HOST_PID=$(id -g)
 USERSET=""
 SUFFIX=$(echo $RANDOM)
 NETWORK="typo3-core-${SUFFIX}"
-CI_PARAMS=""
+CI_PARAMS="${CI_PARAMS:-}"
 CONTAINER_HOST="host.docker.internal"
 
 # Option parsing updates above default vars
@@ -495,7 +504,6 @@ handleDbmsOptions
 if [ "${CI}" == "true" ]; then
     PHPSTAN_CONFIG_FILE="phpstan.ci.neon"
     CONTAINER_INTERACTIVE=""
-    CI_PARAMS="--pull=never"
 fi
 
 # determine default container binary to use: 1. podman 2. docker
@@ -593,7 +601,9 @@ case ${TEST_SUITE} in
             ${CONTAINER_BIN} run --rm ${CI_PARAMS} -d --name ac-web-${SUFFIX} --network ${NETWORK} --network-alias web -v ${CORE_ROOT}:${CORE_ROOT} ${APACHE_OPTIONS} ${IMAGE_APACHE} >/dev/null
         fi
         waitFor chrome 4444
-        waitFor chrome 7900
+        if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ]; then
+            waitFor chrome 7900
+        fi
         waitFor web 80
         if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ] && type "xdg-open" >/dev/null; then
             xdg-open http://localhost:7900/?autoconnect=1 >/dev/null
@@ -688,7 +698,9 @@ case ${TEST_SUITE} in
                 ${CONTAINER_BIN} run --rm ${CI_PARAMS} -d --name ac-web-${SUFFIX} --network ${NETWORK} --network-alias web -v ${CORE_ROOT}:${CORE_ROOT} ${APACHE_OPTIONS} ${IMAGE_APACHE} >/dev/null
             fi
             waitFor chrome 4444
-            waitFor chrome 7900
+            if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ]; then
+                waitFor chrome 7900
+            fi
             waitFor web 80
             if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ] && type "xdg-open" >/dev/null; then
                 xdg-open http://localhost:7900/?autoconnect=1 >/dev/null
@@ -708,7 +720,7 @@ case ${TEST_SUITE} in
         rm -rf "${CORE_ROOT}/typo3temp/var/tests/acceptance" "${CORE_ROOT}/typo3temp/var/tests/AcceptanceReports"
         mkdir -p "${CORE_ROOT}/typo3temp/var/tests/acceptance"
         APACHE_OPTIONS="-e APACHE_RUN_USER=#${HOST_UID} -e APACHE_RUN_SERVERNAME=web -e APACHE_RUN_GROUP=#${HOST_PID} -e APACHE_RUN_DOCROOT=${CORE_ROOT}/typo3temp/var/tests/acceptance -e PHPFPM_HOST=phpfpm -e PHPFPM_PORT=9000"
-        ${CONTAINER_BIN} run --rm ${CI_PARAMS} -d ${SELENIUM_GRID} --name ac-istall-chrome-${SUFFIX} --network ${NETWORK} --network-alias chrome --tmpfs /dev/shm:rw,nosuid,nodev,noexec ${IMAGE_SELENIUM} >/dev/null
+        ${CONTAINER_BIN} run --rm ${CI_PARAMS} -d ${SELENIUM_GRID} --name ac-install-chrome-${SUFFIX} --network ${NETWORK} --network-alias chrome --tmpfs /dev/shm:rw,nosuid,nodev,noexec ${IMAGE_SELENIUM} >/dev/null
         if [ ${CONTAINER_BIN} = "docker" ]; then
             ${CONTAINER_BIN} run --rm -d --name ac-install-phpfpm-${SUFFIX} --network ${NETWORK} --network-alias phpfpm --add-host "${CONTAINER_HOST}:host-gateway" ${USERSET} -e PHPFPM_USER=${HOST_UID} -e PHPFPM_GROUP=${HOST_PID} -v ${CORE_ROOT}:${CORE_ROOT} ${IMAGE_PHP} php-fpm ${PHP_FPM_OPTIONS} >/dev/null
             ${CONTAINER_BIN} run --rm -d --name ac-install-web-${SUFFIX} --network ${NETWORK} --network-alias web --add-host "${CONTAINER_HOST}:host-gateway" -v ${CORE_ROOT}:${CORE_ROOT} ${APACHE_OPTIONS} ${IMAGE_APACHE} >/dev/null
@@ -717,7 +729,9 @@ case ${TEST_SUITE} in
             ${CONTAINER_BIN} run --rm ${CI_PARAMS} -d --name ac-install-web-${SUFFIX} --network ${NETWORK} --network-alias web -v ${CORE_ROOT}:${CORE_ROOT} ${APACHE_OPTIONS} ${IMAGE_APACHE} >/dev/null
         fi
         waitFor chrome 4444
-        waitFor chrome 7900
+        if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ]; then
+            waitFor chrome 7900
+        fi
         waitFor web 80
         if [ "${ACCEPTANCE_HEADLESS}" -eq 0 ] && type "xdg-open" >/dev/null; then
             xdg-open http://localhost:7900/?autoconnect=1 >/dev/null
diff --git a/Build/gitlab-ci.yml b/Build/gitlab-ci.yml
index cac1d25b70ad..c812724001c5 100644
--- a/Build/gitlab-ci.yml
+++ b/Build/gitlab-ci.yml
@@ -10,6 +10,10 @@ variables:
   # and fails with package conflicts. Having a full clone by setting depth 0
   # prevents this, so we don't need to fiddle with COMPOSER_ROOT_VERSION env var.
   GIT_DEPTH: 0
+  # The `--pull=never` flag must not be removed.
+  # All images used by CI-jobs have to be preloaded on the TYPO3 testing
+  # infrastructure to avoid exceeding rate limits for docker.io or ghcr.io.
+  CI_PARAMS: "--pull=never"
 
 cache:
   # Default caching of .cache directory if a job does not override it.
-- 
GitLab