Skip to content

Commit

Permalink
Add ci-prepare script to setup db (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
uriyyo authored Dec 10, 2024
1 parent cab00f7 commit 728113d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 25 deletions.
33 changes: 11 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 }}
Expand All @@ -72,4 +61,4 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
file: ./coverage.xml
files: ./coverage.xml
67 changes: 67 additions & 0 deletions scripts/ci-prepare.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions scripts/ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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"
Expand Down Expand Up @@ -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

0 comments on commit 728113d

Please sign in to comment.