From fb94937237af7a8f3ac0347e22bfb4eb1c0b344a Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Wed, 11 Jun 2025 18:46:31 +0200 Subject: [PATCH] Current test setup is too restrictive, allow running a series of executables in different contexts instead --- test/README.md | 13 ++++--------- test/test.sh | 26 ++++++++++++-------------- test/worker-node/.multinode | 1 - test/worker-node/01run.sh | 9 +++++++++ test/worker-node/{run.sh => 02run.sh} | 0 test/worker-node/02run.sh.context | 1 + 6 files changed, 26 insertions(+), 24 deletions(-) create mode 100755 test/worker-node/01run.sh rename test/worker-node/{run.sh => 02run.sh} (100%) create mode 100644 test/worker-node/02run.sh.context diff --git a/test/README.md b/test/README.md index ce96a32..b2a6d4a 100644 --- a/test/README.md +++ b/test/README.md @@ -49,8 +49,8 @@ As the sandbox container is also expected to be torn down post test, the scripts ## Anatomy of a test case -The `test.sh` script looks for an exectuable file called `run.sh` in each directory. -When found, it is executed and signals success by returning a 0 exit code. +The `test.sh` script looks for all exectuable files in each directory. +When found, all of them are executed in series and are expected to signal success by returning a 0 exit code. Any other exit code is considered a failure and will halt execution of further tests. There is an `util.sh` file containing a few commonly used helpers which can be used by putting the following prelude to a new test case: @@ -68,10 +68,5 @@ In case the swarm setup should be compose of multiple nodes, a `.multinode` file A multinode setup will contain one manager (`manager`) and two worker nodes (`worker1` and `worker2`). -If a test is expected to run in the context of a node other than the `manager`, the hostname can be put in the `.multinode` file. - -> [!IMPORTANT] -> When running against a multi-node setup and targeting a non-manager node, the test script will automatically deploy a stack named `test_stack` based on the compose file in the test directory. -> This is required because the non-manager node cannot deploy the stack itself from within the test script. -> This also means, you cannot mount local directories created in your test script, as the containers are already created when the script runs. -> You can work around this limitation by creating named volumes and then `docker cp`ing the contents your test needs to inspect. +If a test is expected to run in the context of a node other than the `manager`, you can create a `.context` file containing the name of the node you want the test to run in. +E.g. if your script `02run.sh` is expected to be run on `worker2`, create a file called `02run.sh.context` with the content `worker2` diff --git a/test/test.sh b/test/test.sh index dc2c8de..c695326 100755 --- a/test/test.sh +++ b/test/test.sh @@ -36,7 +36,6 @@ for dir in $(find $find_args | sort); do echo "################################################" echo "" - test="${dir}/run.sh" export TARBALL=$tarball export SOURCE=$(dirname $(pwd)) @@ -45,13 +44,6 @@ for dir in $(find $find_args | sort); do fi docker compose --profile $compose_profile up -d --wait - test_context=manager - if [ -f "${dir}/.multinode" ] && [ -s "${dir}/.multinode" ]; then - test_context=$(cat $dir/.multinode) - echo "Running tests on $test_context instead of manager" - fi - docker compose exec $test_context /bin/sh -c "docker load -i /cache/image.tar.gz" - if [ -f "${dir}/.swarm" ]; then docker compose exec manager docker swarm init elif [ -f "${dir}/.multinode" ]; then @@ -60,16 +52,22 @@ for dir in $(find $find_args | sort); do token=$(docker compose exec manager docker swarm join-token -q worker) docker compose exec worker1 docker swarm join --token $token $manager_ip:2377 docker compose exec worker2 docker swarm join --token $token $manager_ip:2377 - - if [ "$test_context" != "manager" ]; then - docker compose exec -w "/code/$dir" manager docker stack deploy --compose-file="docker-compose.yml" test_stack - fi fi - docker compose exec -e TEST_VERSION=$IMAGE_TAG $test_context /bin/sh -c "/code/$test" + for svc in $(docker compose ps -q); do + docker exec $svc /bin/sh -c "docker load -i /cache/image.tar.gz" + done + + for executable in $(find $dir -type f -executable | sort); do + context="manager" + if [ -f "$executable.context" ]; then + context=$(cat "$executable.context") + fi + docker compose exec -e TEST_VERSION=$IMAGE_TAG $context /bin/sh -c "/code/$executable" + done docker compose --profile $compose_profile down echo "" - echo "$test passed" + echo "$dir passed" echo "" done diff --git a/test/worker-node/.multinode b/test/worker-node/.multinode index 6f352b2..e69de29 100644 --- a/test/worker-node/.multinode +++ b/test/worker-node/.multinode @@ -1 +0,0 @@ -worker1 diff --git a/test/worker-node/01run.sh b/test/worker-node/01run.sh new file mode 100755 index 0000000..6f2d29d --- /dev/null +++ b/test/worker-node/01run.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +cd $(dirname $0) +. ../util.sh +current_test=$(basename $(pwd)) + +docker stack deploy --compose-file=docker-compose.yml test_stack diff --git a/test/worker-node/run.sh b/test/worker-node/02run.sh similarity index 100% rename from test/worker-node/run.sh rename to test/worker-node/02run.sh diff --git a/test/worker-node/02run.sh.context b/test/worker-node/02run.sh.context new file mode 100644 index 0000000..6f352b2 --- /dev/null +++ b/test/worker-node/02run.sh.context @@ -0,0 +1 @@ +worker1