#!/bin/sh # Runs the unit tests (tests/, stdlib unittest) inside the real images, with no # network: tests/bot in the bot image, tests/service in the api image. Exit code # is non-zero when either suite fails. # # Two ways to get the code under test into the container: # default the working tree (LichessClientTG_bot/, LichessWebServices/) # is bind-mounted read-only over /app, so local edits are # tested without rebuilding the image; # RUN_TESTS_FROM_IMAGE=1 # the code baked into the image is tested and only tests/ is # copied in (docker cp). Used by the CI workflow, where the # images were just built from the same commit and bind # mounts from the runner are not reliable. # tests/ lives at /tests in the container (not /app/tests: a mount point cannot # be created inside the read-only /app mount) and is discovered with the # top-level directory "/", so test modules import as tests.bot.* / tests.service.* # while PYTHONPATH=/app keeps the application modules importable. # # BOT_IMAGE / API_IMAGE override the image names (defaults: docker-compose.yml). set -u cd "$(dirname "$0")" || exit 2 BOT_IMAGE="${BOT_IMAGE:-192.168.8.174:3000/vrubel/lichess-bot:latest}" API_IMAGE="${API_IMAGE:-192.168.8.174:3000/vrubel/lichess-api:latest}" TEST_DB_DIR=/tmp/lichess-tests # run_suite [extra docker env args...] run_suite() { image="$1"; code_dir="$2"; suite="$3"; shift 3 cmd="mkdir -p $TEST_DB_DIR && cd /app && python -m unittest discover -s /tests/$suite -t /" if [ "${RUN_TESTS_FROM_IMAGE:-0}" = "1" ]; then cid=$(docker create --network none -w /app \ -e PYTHONDONTWRITEBYTECODE=1 -e PYTHONPATH=/app "$@" \ "$image" sh -c "$cmd") || return 1 docker cp tests "$cid":/tests || { docker rm -f "$cid" >/dev/null; return 1; } docker start -a "$cid" rc=$? docker rm -f "$cid" >/dev/null 2>&1 return $rc fi docker run --rm --network none -w /app \ -v "$PWD/$code_dir":/app:ro -v "$PWD/tests":/tests:ro \ -e PYTHONDONTWRITEBYTECODE=1 -e PYTHONPATH=/app "$@" \ "$image" sh -c "$cmd" } status=0 echo "=== bot tests (tests/bot) in $BOT_IMAGE" run_suite "$BOT_IMAGE" LichessClientTG_bot bot \ -e TELEGRAM_BOT_TOKEN=123456:ABCDEF -e ADMINPANEL_TELEGRAM_BOT_TOKEN=123456:ABCDEF \ -e DATABASE_PATH=$TEST_DB_DIR/lichess_bot.db -e LOG_LEVEL=WARNING \ || status=1 echo echo "=== stats service tests (tests/service) in $API_IMAGE" run_suite "$API_IMAGE" LichessWebServices service \ -e LOG_LEVEL=WARNING \ || status=1 echo if [ "$status" -eq 0 ]; then echo "=== ALL TEST SUITES PASSED" else echo "=== TESTS FAILED (see above)" >&2 fi exit $status