|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +# |
| 4 | +# Copyright (C) 2026 Collabora Limited |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) |
| 9 | +api_dir=$(cd -- "$script_dir/.." && pwd) |
| 10 | +stack_dir=$(cd -- "$api_dir/.." && pwd) |
| 11 | +core_dir=${KERNELCI_CORE_DIR:-"$stack_dir/kernelci-core"} |
| 12 | + |
| 13 | +python_image=${KERNELCI_TEST_PYTHON_IMAGE:-python:3.12-slim} |
| 14 | +redis_image=${KERNELCI_TEST_REDIS_IMAGE:-redis:6.2} |
| 15 | +mongo_image=${KERNELCI_TEST_MONGO_IMAGE:-mongo:5.0} |
| 16 | +run_id="$$-${RANDOM}" |
| 17 | +network="kernelci-api-checks-$run_id" |
| 18 | +redis_container="kernelci-api-redis-$run_id" |
| 19 | +mongo_container="kernelci-api-mongo-$run_id" |
| 20 | + |
| 21 | +if [[ ! -f "$core_dir/pyproject.toml" ]]; then |
| 22 | + echo "kernelci-core not found at $core_dir" >&2 |
| 23 | + echo "Set KERNELCI_CORE_DIR to its checkout path." >&2 |
| 24 | + exit 2 |
| 25 | +fi |
| 26 | + |
| 27 | +if [[ $# -gt 0 ]]; then |
| 28 | + pytest_args=("$@") |
| 29 | +else |
| 30 | + pytest_args=( |
| 31 | + -q |
| 32 | + tests/unit_tests/test_db.py |
| 33 | + tests/unit_tests/test_telemetry_handler.py |
| 34 | + tests/unit_tests/test_root_handler.py |
| 35 | + ) |
| 36 | +fi |
| 37 | + |
| 38 | +cleanup() { |
| 39 | + local exit_code=$? |
| 40 | + trap - EXIT INT TERM |
| 41 | + docker stop "$redis_container" "$mongo_container" >/dev/null 2>&1 || true |
| 42 | + docker network rm "$network" >/dev/null 2>&1 || true |
| 43 | + exit "$exit_code" |
| 44 | +} |
| 45 | +trap cleanup EXIT INT TERM |
| 46 | + |
| 47 | +echo "Creating isolated test services" |
| 48 | +docker network create "$network" >/dev/null |
| 49 | +docker run -d --rm \ |
| 50 | + --name "$redis_container" \ |
| 51 | + --network "$network" \ |
| 52 | + --network-alias redis \ |
| 53 | + "$redis_image" >/dev/null |
| 54 | +docker run -d --rm \ |
| 55 | + --name "$mongo_container" \ |
| 56 | + --network "$network" \ |
| 57 | + --network-alias db \ |
| 58 | + "$mongo_image" >/dev/null |
| 59 | + |
| 60 | +echo "Running pytest and Ruff in $python_image" |
| 61 | +docker run --rm \ |
| 62 | + --network "$network" \ |
| 63 | + --volume "$api_dir:/workspace/kernelci-api:ro" \ |
| 64 | + --volume "$core_dir:/workspace/kernelci-core:ro" \ |
| 65 | + --workdir /tmp \ |
| 66 | + --env SECRET_KEY=test-secret \ |
| 67 | + --env KCI_INITIAL_PASSWORD=test-password \ |
| 68 | + --env PYTEST_ADDOPTS=--asyncio-mode=auto \ |
| 69 | + --env PIP_ROOT_USER_ACTION=ignore \ |
| 70 | + --env RUFF_CACHE_DIR=/tmp/ruff-cache \ |
| 71 | + "$python_image" \ |
| 72 | + sh -lc ' |
| 73 | + cp -a /workspace/kernelci-api /tmp/kernelci-api |
| 74 | + cp -a /workspace/kernelci-core /tmp/kernelci-core |
| 75 | + pip install --disable-pip-version-check -q \ |
| 76 | + "/tmp/kernelci-api[tests]" requests pyyaml jinja2 ruff==0.15.12 |
| 77 | + export PYTHONPATH="/tmp/kernelci-core${PYTHONPATH:+:$PYTHONPATH}" |
| 78 | + cd /tmp/kernelci-api |
| 79 | + pytest "$@" |
| 80 | + ruff check \ |
| 81 | + api/db.py \ |
| 82 | + api/main.py \ |
| 83 | + tests/unit_tests/test_db.py \ |
| 84 | + tests/unit_tests/test_telemetry_handler.py \ |
| 85 | + tests/unit_tests/test_root_handler.py |
| 86 | + ruff format --check \ |
| 87 | + api/db.py \ |
| 88 | + api/main.py \ |
| 89 | + tests/unit_tests/test_db.py \ |
| 90 | + tests/unit_tests/test_telemetry_handler.py \ |
| 91 | + tests/unit_tests/test_root_handler.py |
| 92 | + ' sh "${pytest_args[@]}" |
0 commit comments