From 9cb88f50a552c34d6886aa3c1910396e59b3f564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Hagberg?= Date: Sun, 1 Dec 2024 09:54:04 +0100 Subject: [PATCH] Do installation and testing inside a container --- .github/workflows/test.yml | 13 +++--- ci/Dockerfile | 14 +++++++ ci/run_testsuite_and_record_V2.sh | 66 +++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 ci/Dockerfile create mode 100755 ci/run_testsuite_and_record_V2.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 776687bf..b38b92bc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,20 +14,15 @@ jobs: python-version: - '3.11' - '3.12' + - '3.13' env: # Disable colors and formatting in Rich console output TERM: dumb steps: - name: Checkout uses: actions/checkout@v4 - - name: Install uv - uses: astral-sh/setup-uv@v2 - - name: Set up Python ${{ matrix.python-version }} - run: uv python install ${{ matrix.python-version }} - - name: Install mreg-cli - run: uv sync - name: Test and compare api calls - run: uv run ci/run_testsuite_and_record.sh + run: ci/run_testsuite_and_record_V2.sh ${{ matrix.python-version }} tox: name: tox @@ -38,6 +33,7 @@ jobs: python-version: - "3.11" - "3.12" + - '3.13' steps: - uses: actions/checkout@v4 - name: Install uv @@ -68,6 +64,7 @@ jobs: python-version: - "3.11" - "3.12" + - '3.13' steps: - uses: actions/checkout@v4 - name: Install uv @@ -77,4 +74,4 @@ jobs: - name: Install dependencies run: uv sync - name: Run unittest - run: uv run pytest \ No newline at end of file + run: uv run pytest diff --git a/ci/Dockerfile b/ci/Dockerfile new file mode 100644 index 00000000..377d5944 --- /dev/null +++ b/ci/Dockerfile @@ -0,0 +1,14 @@ +# This container image is only used temporarily for testing during a CI process. +FROM ubuntu:latest +ARG python_version=3.12 + +SHELL ["/bin/bash", "-c"] +RUN apt update +RUN apt install -y curl git +RUN curl -LsSf https://astral.sh/uv/install.sh | sh +RUN /root/.local/bin/uv self update +RUN /root/.local/bin/uv python install ${python_version} +COPY . /build +RUN cd /build; /root/.local/bin/uv sync + +CMD cd /build/ci; /root/.local/bin/uv run bash -c 'echo "test" | mreg-cli -u test -d example.org --url http://127.0.0.1:8000 --source testsuite --record new_testsuite_log.json --record-without-timestamps -v ERROR >/dev/null' diff --git a/ci/run_testsuite_and_record_V2.sh b/ci/run_testsuite_and_record_V2.sh new file mode 100755 index 00000000..36d2bce0 --- /dev/null +++ b/ci/run_testsuite_and_record_V2.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# exit immediately on error +set -e + +# clean up on exit, even if something fails +function cleanup { + set +e + if [[ -n "$GITHUB_ACTIONS" ]]; then + docker compose --ansi=never down + else + docker compose down + fi + docker ps -a | grep "mreg-" | awk '{print $1}' | xargs -r docker stop + docker ps -a | grep "mreg-" | awk '{print $1}' | xargs -r docker rm + docker images | grep "mreg-" | awk '{print $1}' | xargs -r docker rmi + rm -f new_testsuite_log.json + echo "cleanup done." +} +trap cleanup EXIT + +# chdir to where this script is +cd `dirname $0` + +# get Python version from argument +PYTHON_VERSION=3.12 +if [ ! -z "$1" ]; then + PYTHON_VERSION=$1 +fi +echo "Python version $PYTHON_VERSION" + +# build a container image for mreg-cli +docker build -f Dockerfile -t mreg-cli --build-arg python_version=$PYTHON_VERSION .. + +# start mreg+postgres in containers +if [[ -n "$GITHUB_ACTIONS" ]]; then + docker compose --ansi=never pull --quiet + docker compose --ansi=never up -d +else + docker compose up -d +fi + +# give mreg some time to create the database schema and start up +sleep 5s + +# create a superuser +docker exec -t mreg /app/manage.py create_mreg_superuser --username test --password test + +# test connectivity +#docker run --rm --tty --network host --entrypoint curl mreg-cli --head http://127.0.0.1:8000/admin/login/ + +# start the mreg-cli container, which will automatically run the test suite +echo "Running the tests..." +docker run --name mreg-cli --network host --tty mreg-cli +docker commit mreg-cli finished-mreg-tests # because inside is the file new_testsuite_log.json which we want to look at + +# show a detailed diff (and review if running locally) +if [[ -n "$GITHUB_ACTIONS" ]]; then + docker run --rm --tty --entrypoint bash finished-mreg-tests -c 'cd /build/ci; /root/.local/bin/uv run diff.py testsuite-result.json new_testsuite_log.json' + exit $? +else + docker run -it --name finished-mreg-tests --entrypoint bash finished-mreg-tests -c 'cd /build/ci; /root/.local/bin/uv run diff.py testsuite-result.json new_testsuite_log.json --review' + EXITCODE=$? + docker cp finished-mreg-tests:/build/ci/testsuite-result.json . + exit $EXITCODE +fi