From 728113da82fea17e4c8d465df50ad1677d606ac3 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 10 Dec 2024 11:10:51 +0100 Subject: [PATCH] Add ci-prepare script to setup db (#1390) --- .github/workflows/test.yml | 33 +++++++------------ scripts/ci-prepare.sh | 67 ++++++++++++++++++++++++++++++++++++++ scripts/ci-test.sh | 7 ++-- 3 files changed, 82 insertions(+), 25 deletions(-) create mode 100755 scripts/ci-prepare.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2000ab09..525ae559 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,26 +28,6 @@ jobs: with: python-version: ${{ matrix.python-version }} - - name: Set up Postgresql - uses: harmon758/postgresql-action@v1 - with: - postgresql db: 'postgres' - postgresql user: 'postgres' - postgresql password: 'postgres' - - - name: Set up MongoDB - uses: supercharge/mongodb-github-action@1.11.0 - - - name: Set up Scylla - run: | - export DOCKER_ID=$(docker run -d scylladb/scylla:latest --cluster-name test ) - export CQL_TEST_HOST=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' ${DOCKER_ID}) - while ! nc -z ${CQL_TEST_HOST} 9042; do - sleep 0.1 # wait for 1/10 of the second before check again - done - - echo "CQL_TEST_HOST=${CQL_TEST_HOST}" >> $GITHUB_ENV - - name: Install Poetry uses: snok/install-poetry@v1.4.1 @@ -59,10 +39,19 @@ jobs: ~/.cache/pypoetry/virtualenvs key: ${{ runner.os }}-py${{ matrix.python-version }}-poetry-${{ hashFiles('poetry.lock') }}-v1 + - name: Cache Docker images + uses: ScribeMD/docker-cache@0.5.0 + with: + key: docker-${{ runner.os }}-ci + + - name: Prepare DBs + shell: bash + run: | + bash scripts/ci-prepare.sh + - name: Run tests shell: bash env: - CQL_TEST_HOST: ${{ env.CQL_TEST_HOST }} PY_VERSION: ${{ matrix.python-version }} PYDANTIC_V2: ${{ matrix.pydantic_v2 }} FASTAPI_PRE_0_112_4: ${{ matrix.fastapi_pre_0_112_4 }} @@ -72,4 +61,4 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v5 with: - file: ./coverage.xml + files: ./coverage.xml diff --git a/scripts/ci-prepare.sh b/scripts/ci-prepare.sh new file mode 100755 index 00000000..13dd3252 --- /dev/null +++ b/scripts/ci-prepare.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +set -x + +function is_docker_container_running() { + docker ps -a --format '{{.Names}}' | grep -q "$1" +} + +function wait_for_port() { + echo "Waiting for port $1 to be open" + + port="$1"; shift + retries="${1:-30}"; + + while ! nc -z localhost "$port"; do + sleep 1 + retries=$((retries - 1)) + if [[ "$retries" -le 0 ]]; then + echo "Timeout waiting for port $port" + exit 1 + fi + done + + echo "Port $port is open" +} + +DOCKER_NAME_PREFIX="${DOCKER_NAME_PREFIX:-fastapi-pagination}" + +POSTGRES_NAME="${DOCKER_NAME_PREFIX}-postgres" +CASSANDRA_NAME="${DOCKER_NAME_PREFIX}-cassandra" +MONGO_NAME="${DOCKER_NAME_PREFIX}-mongo" + +if ! is_docker_container_running "${POSTGRES_NAME}"; then + echo "Starting PostgreSQL" + + POSTGRES_DB="${POSTGRES_DB:-postgres}" + POSTGRES_USER="${POSTGRES_USER:-postgres}" + POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-postgres}" + POSTGRES_VERSION="${POSTGRES_VERSION:-latest}" + + docker run -d -p 5432:5432 \ + -e POSTGRES_DB="${POSTGRES_DB}" \ + -e POSTGRES_USER="${POSTGRES_USER}" \ + -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \ + --name="${POSTGRES_NAME}" \ + postgres:${POSTGRES_VERSION} +fi + +if ! is_docker_container_running "${CASSANDRA_NAME}"; then + echo "Starting ScyllaDB" + + docker run -d -p 9042:9042 \ + --name="${CASSANDRA_NAME}" \ + scylladb/scylla:latest +fi + +if ! is_docker_container_running "${MONGO_NAME}"; then + echo "Starting MongoDB" + + docker run -d -p 27017:27017 \ + --name="${MONGO_NAME}" \ + mongo:latest +fi + +wait_for_port 5432 +wait_for_port 9042 +wait_for_port 27017 diff --git a/scripts/ci-test.sh b/scripts/ci-test.sh index d23ee377..bedbee59 100755 --- a/scripts/ci-test.sh +++ b/scripts/ci-test.sh @@ -2,7 +2,6 @@ set -ex -CQL_TEST_HOST="${CQL_TEST_HOST:-localhost}" PYDANTIC_V2="${PYDANTIC_V2:-true}" FASTAPI_PRE_0_112_4="${FASTAPI_PRE_0_112_4:-false}" @@ -14,8 +13,7 @@ function _pytest() { poetry run pytest "$@" \ --cov=fastapi_pagination \ --cov-append \ - --cov-report=xml \ - --cassandra-dsn="${CQL_TEST_HOST}" + --cov-report=xml } echo "Installing dependencies" @@ -66,3 +64,6 @@ echo "Running orm tests" _pip install "databases<0.9.0" orm _pytest tests -m orm _pip uninstall -y orm + +echo "Restore env" +poetry install -E all --sync