Skip to content

Commit

Permalink
Integration test (#68)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
	- Introduced integration tests for health and ping endpoints.
- **Chores**
- Updated CI/CD pipeline with a new executor for improved Docker caching
and integration testing.
	- Modified GitHub release creation steps for enhanced automation.
- **Refactor**
	- Updated Dockerfile for optimized application startup.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
csikb authored Mar 22, 2024
1 parent c87729b commit cfd582b
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 40 deletions.
39 changes: 27 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ executors:
python:
docker:
- image: cimg/python:3.12.2
python-machine:
machine:
docker_layer_caching: true
image: ubuntu-2204:edge
defaults:
docker_publish: &docker_publish
image: bsstudio/bss-web-file-api
Expand All @@ -34,30 +38,39 @@ jobs:
- run: poetry run black . --check
- run: poetry run pylint src
- run: poetry run mypy -p src
github_release:
docker:
- image: cimg/base:stable
steps:
- checkout
- github-cli/install:
version: 2.43.1
- github-cli/setup:
version: 2.43.1
- run: gh release create << pipeline.git.tag >> -t << pipeline.git.tag >> --generate-notes
coverage:
executor: python
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run: |
poetry run pytest --cov=src \
poetry run pytest tests --cov=src \
--cov-fail-under=100 \
--cov-report=html \
--cov-report json
- codecov/upload
- store_artifacts:
path: htmlcov
integration:
executor: python-machine
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
pre-install-steps:
- run: pip install poetry
- run: poetry run pytest tests-int
github_release:
docker:
- image: cimg/base:stable
steps:
- checkout
- github-cli/install:
version: 2.43.1
- github-cli/setup:
version: 2.43.1
- run: gh release create << pipeline.git.tag >> -t << pipeline.git.tag >> --generate-notes
workflows:
Build:
jobs:
Expand All @@ -68,8 +81,10 @@ workflows:
- python/test:
name: Unit tests
pkg-manager: poetry
test-tool: pytest
test-tool-args: tests
version: 3.12.2
- integration:
name: Integration tests
- docker/hadolint:
name: Lint Dockerfile
- docker/publish:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RUN pip install --no-cache-dir ./wheels/*

COPY ./src ./src

CMD ["uvicorn", "src.bss_web_file_server.main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "80"]
EXPOSE 80
LABEL org.opencontainers.image.source="https://github.com/BSStudio/bss-web-file-api"
LABEL org.opencontainers.image.description="BSS Web file API"
Expand Down
10 changes: 3 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ services:
ports:
- "80"
environment:
SERVER_BASE_PATH: /assets
SERVER_BASE_PATH: /home/nonroot/assets
healthcheck:
test: "wget --tries=1 --no-verbose -qO- http://localhost:80/health | grep -q UP"
start_period: 5s
test: python -c "import requests, sys; sys.exit(0 if 'UP' in requests.get('http://localhost/health').text else 1)"
start_period: 2s
interval: 10s
timeout: 5s
retries: 5
volumes:
- assets:/assets
volumes:
assets:
318 changes: 307 additions & 11 deletions poetry.lock

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
fastapi = {version="0.110.0", extras=["all"]}
uvicorn = {version = "0.29.0", extras = ["standard"]}
pillow = "10.2.0"
pillow-avif-plugin = "1.4.3"
python = "^3.12"
# used for UploadFile
python-multipart = "0.0.9"
requests = "2.31.0"
uvicorn = {version = "0.29.0", extras = ["standard"]}

[tool.poetry.group.dev.dependencies]
black = "24.3.0"
pre-commit = "3.6.2"
pylint = "3.1.0"
isort = "5.13.2"
types-Pillow = "10.2.0.20240311"
mypy = "1.9.0"
pylint = "3.1.0"
pre-commit = "3.6.2"
types-Pillow = "10.2.0.20240311"

[tool.poetry.group.test.dependencies]
httpx = "^0.27.0"
pytest = "^8.0.0"
pytest-mock = "^3.10.0"
pytest-cov = "^4.1.0"
pytest = "8.0.0"
pytest-cov = "4.1.0"
pytest-mock = "3.10.0"
testcontainers = "4.1.1"

[tool.isort]
profile = "black"
Expand Down
Empty file added tests-int/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests-int/test_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

import pytest
import requests
from testcontainers.compose import DockerCompose


@pytest.fixture(scope="session")
def compose():
print("Starting compose")
compose = DockerCompose(
context=os.getcwd(),
compose_file_name="docker-compose.yml",
build=True,
wait=True,
)
compose.start()
yield compose
compose.stop()


def test_health(compose: DockerCompose):
host = compose.get_service_host("app")
port = compose.get_service_port("app", 80)
assert "Application startup complete." in compose.get_logs("app")[0]
response = requests.get(f"http://{host}:{port}/health")
assert response.status_code == 200
assert response.text == "UP"


def test_ping(compose: DockerCompose):
host = compose.get_service_host("app")
port = compose.get_service_port("app", 80)
response = requests.get(f"http://{host}:{port}/ping")
assert response.status_code == 200
assert response.text == "PONG"

0 comments on commit cfd582b

Please sign in to comment.