Skip to content

refactor tests suite in order to run it locally outside docker #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 33 additions & 40 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,18 @@ on:
pull_request:
branches: [main]

env:
LATEST_PY_VERSION: '3.11'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add python 3.12

timeout-minutes: 20

services:
db_service:
image: ghcr.io/stac-utils/pgstac:v0.7.10
env:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: postgis
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
PGUSER: username
PGPASSWORD: password
PGDATABASE: postgis
ALLOW_IP_RANGE: 0.0.0.0/0
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 10s
--health-retries 10
--log-driver none
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this external service when using postgres pytest fixture


steps:
- name: Check out repository code
uses: actions/checkout@v4
Expand All @@ -50,25 +30,26 @@ jobs:
cache-dependency-path: setup.py

- name: Lint code
if: ${{ matrix.python-version == 3.8 }}
if: ${{ matrix.python-version == env.LATEST_PY_VERSION }}
run: |
python -m pip install pre-commit
pre-commit run --all-files

- name: Install
- name: install lib postgres
run: |
sudo apt update
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | sudo apt-key add -
echo "deb [arch=amd64] http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list
sudo apt update
sudo apt-get install --yes libpq-dev postgis postgresql-14-postgis-3

- name: Install dependencies
run: |
pip install .[dev,server]
python -m pip install --upgrade pip
python -m pip install .[dev,server]

- name: Run test suite
run: make test
env:
ENVIRONMENT: testing
POSTGRES_USER: username
POSTGRES_PASS: password
POSTGRES_DBNAME: postgis
POSTGRES_HOST_READER: localhost
POSTGRES_HOST_WRITER: localhost
POSTGRES_PORT: 5432
run: python -m pytest --cov stac_fastapi.pgstac --cov-report xml --cov-report term-missing

validate:
runs-on: ubuntu-latest
Expand All @@ -90,17 +71,23 @@ jobs:
--log-driver none
ports:
- 5432:5432

steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: ${{ env.LATEST_PY_VERSION }}
cache: pip
cache-dependency-path: setup.py

- name: Install stac-fastapi and stac-api-validator
run: pip install .[server] stac-api-validator==0.4.1
run: |
python -m pip install --upgrade pip
python -m pip install .[server] stac-api-validator==0.4.1

- name: Load data and validate
run: python -m stac_fastapi.pgstac.app & ./scripts/wait-for-it.sh localhost:8080 && python ./scripts/ingest_joplin.py http://localhost:8080 && ./scripts/validate http://localhost:8080
env:
Expand All @@ -120,15 +107,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: ${{ env.LATEST_PY_VERSION }}
cache: pip
cache-dependency-path: setup.py

- name: Install with documentation dependencies
run: pip install .[docs,dev,server]
run: |
python -m pip install --upgrade pip
python -m pip install .[docs,dev,server]

- name: Generate API docs
run: make docs

- name: Build documentation
run: mkdocs build --strict
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ repos:
rev: 5.12.0
hooks:
- id: isort
language_version: python3.8
language_version: python
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change enables to run pre-commit on other python version

- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
args: ["--safe"]
language_version: python3.8
language_version: python
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
language_version: python3.8
language_version: python
args: [
# E501 let black handle all line length decisions
# W503 black conflicts with "line break before operator" rule
Expand All @@ -26,7 +26,7 @@ repos:
rev: v2.1.1
hooks:
- id: pydocstyle
language_version: python3.8
language_version: python
exclude: ".*(test|scripts).*"
args:
[
Expand All @@ -46,7 +46,7 @@ repos:
rev: 6.3.0
hooks:
- id: pydocstyle
language_version: python3.8
language_version: python
exclude: ".*(test|scripts).*"
#args: [
# Don't require docstrings for tests
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM python:3.8-slim as base
ARG PYTHON_VERSION=3.11

FROM python:${PYTHON_VERSION}-slim as base

# Any python libraries that require system libraries to be installed will likely
# need the following packages in order to build
Expand All @@ -16,6 +18,6 @@ WORKDIR /app

COPY . /app

RUN pip install -e .[dev,server]
RUN python -m pip install -e .[server]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This image is now used only for the server part. we'll use another image for testing (make test)


CMD ["uvicorn", "stac_fastapi.pgstac.app:app", "--host", "0.0.0.0", "--port", "8080"]
CMD ["uvicorn", "stac_fastapi.pgstac.app:app", "--host", "0.0.0.0", "--port", "8080"]
19 changes: 19 additions & 0 deletions Dockerfile.tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG PYTHON_VERSION=3.11

FROM python:${PYTHON_VERSION}-slim as base

# Any python libraries that require system libraries to be installed will likely
# need the following packages in order to build
RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y build-essential git libpq-dev postgresql-15-postgis-3 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash newuser
USER newuser

WORKDIR /app
COPY . /app

RUN python -m pip install -e .[dev,server] --user
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ run = docker-compose run --rm \
-e APP_PORT=${APP_PORT} \
app

runtests = docker-compose run --rm tests

.PHONY: image
image:
docker-compose build
Expand All @@ -28,7 +30,7 @@ docker-shell:

.PHONY: test
test:
$(run) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/tests/ && pytest -vvv --log-cli-level $(LOG_LEVEL)'
$(runtests) /bin/bash -c 'export && python -m pytest /app/tests/api/test_api.py --log-cli-level $(LOG_LEVEL)'

.PHONY: run-database
run-database:
Expand Down
14 changes: 13 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ services:
app:
container_name: stac-fastapi-pgstac
image: stac-utils/stac-fastapi-pgstac
platform: linux/amd64
build: .
environment:
- APP_HOST=0.0.0.0
Expand Down Expand Up @@ -31,6 +30,19 @@ services:
- database
command: bash -c "./scripts/wait-for-it.sh database:5432 && python -m stac_fastapi.pgstac.app"

tests:
container_name: stac-fastapi-pgstac-test
image: stac-utils/stac-fastapi-pgstac-test
build:
context: .
dockerfile: Dockerfile.tests
environment:
- ENVIRONMENT=local
- DB_MIN_CONN_SIZE=1
- DB_MAX_CONN_SIZE=1
- USE_API_HYDRATE=${USE_API_HYDRATE:-false}
command: bash -c "python -m pytest -s -vv"

database:
container_name: stac-db
image: ghcr.io/stac-utils/pgstac:v0.7.10
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dev": [
"pystac[validation]",
"pypgstac[psycopg]==0.7.*",
"pytest-postgresql",
"pytest",
"pytest-cov",
"pytest-asyncio>=0.17,<0.23.0",
Expand Down
13 changes: 11 additions & 2 deletions tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ async def search(query: Dict[str, Any]) -> List[Item]:


@pytest.mark.asyncio
async def test_wrapped_function(load_test_data) -> None:
async def test_wrapped_function(load_test_data, database) -> None:
# Ensure wrappers, e.g. Planetary Computer's rate limiting, work.
# https://github.com/gadomski/planetary-computer-apis/blob/2719ccf6ead3e06de0784c39a2918d4d1811368b/pccommon/pccommon/redis.py#L205-L238

Expand Down Expand Up @@ -672,7 +672,16 @@ async def get_collection(
collection_id, request=request, **kwargs
)

settings = Settings(testing=True)
settings = Settings(
postgres_user=database.user,
postgres_pass=database.password,
postgres_host_reader=database.host,
postgres_host_writer=database.host,
postgres_port=database.port,
postgres_dbname=database.dbname,
testing=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing=True could be removed because we are setting the dbname in the settings

)

extensions = [
TransactionExtension(client=TransactionsClient(), settings=settings),
FieldsExtension(),
Expand Down
Loading