diff --git a/solution/tests/e2e/.env.template b/solution/tests/e2e/.env.template new file mode 100644 index 0000000..014650c --- /dev/null +++ b/solution/tests/e2e/.env.template @@ -0,0 +1,3 @@ +# Below all environment variables and default values + +BACKEND_BASE_URL=http://127.0.0.1:8080 diff --git a/solution/tests/e2e/.gitignore b/solution/tests/e2e/.gitignore new file mode 100644 index 0000000..b96e392 --- /dev/null +++ b/solution/tests/e2e/.gitignore @@ -0,0 +1,170 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + +# PyPI configuration file +.pypirc + +# Ruff files +.ruff_cache diff --git a/solution/tests/e2e/README.md b/solution/tests/e2e/README.md new file mode 100644 index 0000000..2ac51a1 --- /dev/null +++ b/solution/tests/e2e/README.md @@ -0,0 +1,30 @@ +# E2E tests for AdNova + +## Prerequisites + +Ensure you have the following installed on your system: + +- [Python](https://www.python.org/) (>=3.10,<3.12) +- [uv](https://docs.astral.sh/uv/) +- [Docker](https://www.docker.com/) +- [Docker compose](https://docs.docker.com/compose/) + +## Warning + +Plese note that containers will use ports 13241 to 13246 and 8080, so there is must be no listeners on this ports range. + +## Install dependencies + +```bash +uv sync --no-dev +``` + +### Customize environment + +Customize environment with `docker run` command (or bind .env file to container), for all environment vars and default values see [.env.template](./.env.template). + +## Run tests + +```bash +uv run pytest . +``` diff --git a/solution/tests/e2e/conftest.py b/solution/tests/e2e/conftest.py new file mode 100644 index 0000000..2ab75ec --- /dev/null +++ b/solution/tests/e2e/conftest.py @@ -0,0 +1,68 @@ +import os +import subprocess +import time +from collections.abc import Generator + +import httpx +import pytest +from dotenv import load_dotenv + +load_dotenv() + +BACKEND_BASE_URL = os.getenv("BACKEND_BASE_URL", "http://127.0.0.1:8080") + + +@pytest.fixture(scope="session", autouse=True) +def docker_compose() -> Generator[None]: + # btw, this is just in case you've forgotten to shut down compose :) + subprocess.run( + executable="docker", + args=[ + "docker", + "compose", + "down", + "-v", + ], + check=True, + ) + subprocess.run( + executable="docker", + args=[ + "docker", + "compose", + "--project-name", + "adnova-testing", + "up", + "-d", + "--build", + "--force-recreate", + "--remove-orphans", + ], + check=True, + ) + time.sleep(5) + + yield + + subprocess.run( + executable="docker", + args=[ + "docker", + "compose", + "--project-name", + "adnova-testing", + "down", + "-v", + ], + check=True, + ) + + +@pytest.fixture(scope="session") +def client() -> Generator[httpx.Client]: + with httpx.Client(base_url=BACKEND_BASE_URL, timeout=10.0) as client: + yield client + + +def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: + items.sort(key=lambda item: "test_health" not in item.nodeid) diff --git a/solution/tests/e2e/pyproject.toml b/solution/tests/e2e/pyproject.toml new file mode 100644 index 0000000..cdaf1a1 --- /dev/null +++ b/solution/tests/e2e/pyproject.toml @@ -0,0 +1,110 @@ +[project] +name = "adnova-e2e-tests" +version = "0.1.0" +readme = "README.md" +requires-python = ">=3.10,<3.12" +dependencies = [ + "httpx>=0.28.1", + "pytest>=8.3.4", + "python-dotenv>=1.0.1", +] + +[dependency-groups] +dev = [ + "ruff>=0.9.6", +] + +[tool.ruff] +builtins = [] +cache-dir = ".ruff_cache" +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".mypy_cache", + ".nox", + ".pants.d", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + "__pypackages__", + "_build", + "buck-out", + "dist", + "migrations", + "node_modules", + "venv", +] +extend-exclude = [] +extend-include = [] +fix = false +fix-only = false +force-exclude = true +include = ["*.py", "*.pyi", "*.ipynb", "**/pyproject.toml"] +indent-width = 4 +line-length = 79 +namespace-packages = [] +output-format = "full" +preview = false +required-version = ">=0.8.4" +respect-gitignore = true +show-fixes = true +src = [".", "src"] +target-version = "py310" +unsafe-fixes = false + +[tool.ruff.analyze] +detect-string-imports = true +direction = "Dependencies" +exclude = [] +include-dependencies = {} +preview = false + +[tool.ruff.format] +docstring-code-format = true +docstring-code-line-length = 79 +exclude = [] +indent-style = "space" +line-ending = "lf" +preview = false +quote-style = "double" +skip-magic-trailing-comma = false + +[tool.ruff.lint] +allowed-confusables = ["ℹ"] +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" +exclude = ["tests.py"] +explicit-preview-rules = false +extend-fixable = [] +extend-per-file-ignores = {} +extend-safe-fixes = [] +extend-select = [] +extend-unsafe-fixes = [] +external = [] +fixable = ["ALL"] +ignore = [ + "ARG", + "D", + "ANN401", + "COM812", + "FBT001", + "FBT002", + "N813", + "S101", +] +logger-objects = [] +per-file-ignores = {} +preview = false +select = ["ALL"] +task-tags = ["TODO", "FIXME", "HACK", "WORKOUT"] +typing-modules = [] +unfixable = [] + +[tool.ruff.lint.pylint] +max-args = 6 + diff --git a/solution/tests/e2e/scripts/check b/solution/tests/e2e/scripts/check new file mode 100755 index 0000000..7f50734 --- /dev/null +++ b/solution/tests/e2e/scripts/check @@ -0,0 +1,8 @@ +#!/bin/bash + +GREEN='\033[1;32m' +NC='\033[0m' + +uvx ruff format . +uvx ruff check . --fix +printf "${GREEN}Linters/formatters runned${NC}\n" diff --git a/solution/tests/e2e/tests/__init__.py b/solution/tests/e2e/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/solution/tests/e2e/tests/test_backend_health.py b/solution/tests/e2e/tests/test_backend_health.py new file mode 100644 index 0000000..e506766 --- /dev/null +++ b/solution/tests/e2e/tests/test_backend_health.py @@ -0,0 +1,29 @@ +import logging +from http import HTTPStatus as status + +from httpx import Client + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def test_healthcheck(client: Client) -> None: + response = client.get("/health?format=json") + assert response.status_code == status.OK + + response_data = response.json() + + unhealthy_services = [ + service + for service, status in response_data.items() + if status != "working" + ] + + for service in unhealthy_services: + logger.error( + "Service %s unhealthy: %s", service, response_data[service] + ) + + assert not unhealthy_services, ( + f"Some services are unhealthy: {', '.join(unhealthy_services)}" + )