diff --git a/MANIFEST.in b/MANIFEST.in index 782113de4..0e2ea6067 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,3 +4,5 @@ recursive-include contract-tests/src *.py py.typed recursive-include contract-tests/consumer *.py *.ini include test-backends/pyproject.toml test-backends/README.rst test-backends/LICENSE recursive-include test-backends/src *.py py.typed +include server/pyproject.toml server/README.rst server/LICENSE +recursive-include server/src *.py py.typed diff --git a/contract-tests/consumer/core_checks.py b/contract-tests/consumer/core_checks.py new file mode 100644 index 000000000..5dd3a0831 --- /dev/null +++ b/contract-tests/consumer/core_checks.py @@ -0,0 +1,14 @@ +"""Check that the core wheel works without the Flask server extra.""" + +import importlib.util + +from mock_vws import MockVWS + +__all__ = ["pytest_sessionstart"] + + +def pytest_sessionstart() -> None: + """Import and enter the core mock without a Flask installation.""" + assert importlib.util.find_spec(name="flask") is None + with MockVWS(): + pass diff --git a/contract-tests/consumer/test_installed_suite.py b/contract-tests/consumer/test_installed_suite.py index 17d204045..6fccd615c 100644 --- a/contract-tests/consumer/test_installed_suite.py +++ b/contract-tests/consumer/test_installed_suite.py @@ -43,6 +43,7 @@ def test_installed_suite( for filename in ( "pytest.ini", "dependency_checks.py", + "core_checks.py", "contract_checks.py", ): _ = shutil.copyfile(src=consumer / filename, dst=tmp_path / filename) @@ -63,6 +64,22 @@ def test_installed_suite( ], cwd=tmp_path, ) + core = next(dist.glob(pattern="vws_python_mock-*.whl")) + _run(args=[uv, "pip", "install", "--python", python, core], cwd=tmp_path) + _run( + args=[ + python, + "-m", + "pytest", + "-p", + "core_checks", + "--pyargs", + "vws_mock_contract_tests.database_summary", + "--collect-only", + "-q", + ], + cwd=tmp_path, + ) _run( args=[ uv, @@ -74,6 +91,11 @@ def test_installed_suite( ], cwd=tmp_path, ) + health_tests = tmp_path / "test_server_health.py" + _ = shutil.copyfile( + src=repository / "tests/mock_vws/test_healthcheck.py", dst=health_tests + ) + _run(args=[python, "-m", "pytest", health_tests, "-q"], cwd=tmp_path) _run( args=[ python, diff --git a/docs/requirements.txt b/docs/requirements.txt index 48e8c6d74..2eee22b05 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -2,3 +2,4 @@ . ./contract-tests ./test-backends +./server diff --git a/prek.toml b/prek.toml index c20646e45..e7644ba8b 100644 --- a/prek.toml +++ b/prek.toml @@ -318,7 +318,7 @@ hooks = [ { id = "pylint", name = "pylint", - entry = "uv run --group=dev -m pylint *.py src/ tests/ contract-tests/src/ contract-tests/consumer/ test-backends/src/ docs/ ci/ admin/", + entry = "uv run --group=dev -m pylint *.py src/ tests/ server/src/ contract-tests/src/ contract-tests/consumer/ test-backends/src/ docs/ ci/ admin/", language = "python", stages = ["manual"], pass_filenames = false, diff --git a/pyproject.toml b/pyproject.toml index b4c18561f..b362b499b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,6 @@ dynamic = [ ] dependencies = [ "beartype>=0.23.0", - "flask>=3.0.3", "httpx>=0.27.0", "httpx2>=2.12", "mock-response-delay>=2026.9.12", @@ -52,6 +51,7 @@ dependencies = [ "vws-auth-tools>=2024.7.12", "werkzeug>=3.1.2", ] +optional-dependencies.server = [ "flask>=3.0.3", "vws-python-mock-server==0.0.0" ] urls.Documentation = "https://vws-python.github.io/vws-python-mock/" urls.Source = "https://github.com/VWS-Python/vws-python-mock" @@ -134,6 +134,7 @@ dev = [ "vws-mock-contract-tests==0.0.0", "vws-mock-test-backends==0.0.0", "vws-python==2026.10.9", + "vws-python-mock-server==0.0.0", "vws-test-fixtures==2026.10.8", "vws-web-tools==2026.8.27", "yamlfix==1.19.1", @@ -168,11 +169,12 @@ fallback_version = "0.0.0" sources.vws-mock-contract-tests = { workspace = true } sources.vws-mock-test-backends = { workspace = true } sources.vws-python-mock = { workspace = true } -workspace.members = [ "contract-tests", "test-backends" ] +sources.vws-python-mock-server = { workspace = true } +workspace.members = [ "contract-tests", "server", "test-backends" ] [tool.ruff] line-length = 79 -src = [ "contract-tests/src", "src", "test-backends/src" ] +src = [ "contract-tests/src", "server/src", "src", "test-backends/src" ] lint.select = [ "ALL", ] @@ -513,14 +515,14 @@ markers = [ "requires_docker_build", ] # Legacy import modules keep the existing CI partitions stable. -norecursedirs = [ "contract-tests", "test-backends" ] +norecursedirs = [ "contract-tests", "server", "test-backends" ] # Options for pytest-retry. retries = "10" retry_delay = "10" xfail_strict = true [tool.coverage] -run.source = [ "ci/", "contract-tests/src/", "src/", "test-backends/src/", "tests/" ] +run.source = [ "ci/", "contract-tests/src/", "server/src/", "src/", "test-backends/src/", "tests/" ] # Import-only compatibility modules contain no assertions or backend logic. # Their authoritative implementations remain subject to full coverage. run.omit = [ diff --git a/server/LICENSE b/server/LICENSE new file mode 100644 index 000000000..c9f18d1a3 --- /dev/null +++ b/server/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Adam Dangoor + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/server/README.rst b/server/README.rst new file mode 100644 index 000000000..e1d45c672 --- /dev/null +++ b/server/README.rst @@ -0,0 +1,39 @@ +VWS mock server +=============== + +This separately buildable project owns server code while it is prepared for extraction. +The health probe is migrated first. +The three Flask services follow in separate changes. +Shared models, validation and API behavior stay in ``vws-python-mock``. + +Build and install +----------------- + +Build the workspace wheels and install them together: + +.. code-block:: console + + $ uv build --all-packages --wheel --out-dir dist + $ uv pip install dist/*.whl + +The initial server version is ``0.0.0``. +Build the server and core library from the same repository commit. +For a Git installation, use that revision and the ``server`` subdirectory for this project. +The core's ``server`` extra selects this package and Flask when they are available in the workspace or as built wheels. +The default core installation does not require Flask. + +The server consumes public core models and ``mock_vws.server_api``. +Its runtime dependencies do not include the contract suite or test adapters. +Consumers load those development dependencies explicitly. + +Health checks +------------- + +The published images continue to probe the local service on port 5000: + +.. code-block:: console + + $ python -m vws_mock_server.healthcheck + +The probe accepts the existing unauthorized, forbidden and missing-route responses as healthy. +It fails on connection errors, timeouts and other HTTP statuses. diff --git a/server/pyproject.toml b/server/pyproject.toml new file mode 100644 index 000000000..7bfecd700 --- /dev/null +++ b/server/pyproject.toml @@ -0,0 +1,31 @@ +[build-system] +build-backend = "setuptools.build_meta" +requires = [ "setuptools" ] + +[project] +name = "vws-python-mock-server" +version = "0.0.0" +description = "Flask services for the Vuforia Web Services mock." +readme = "README.rst" +license = "MIT" +license-files = [ "LICENSE" ] +authors = [ { name = "Adam Dangoor", email = "adamdangoor@gmail.com" } ] +requires-python = ">=3.15" +classifiers = [ + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.15", +] +dependencies = [ + "beartype>=0.23.1", + "flask>=3.0.3", + "pydantic>=2.6", + "pydantic-settings>=2.6.1", + "requests>=2.32.3", + "vws-python-mock", + "werkzeug>=3.1.2", +] +urls.Source = "https://github.com/VWS-Python/vws-python-mock" + +[tool.setuptools] +packages.find.where = [ "src" ] +package-data.vws_mock_server = [ "py.typed" ] diff --git a/server/src/vws_mock_server/__init__.py b/server/src/vws_mock_server/__init__.py new file mode 100644 index 000000000..cdd6e4167 --- /dev/null +++ b/server/src/vws_mock_server/__init__.py @@ -0,0 +1 @@ +"""Flask services and deployment helpers for the VWS mock.""" diff --git a/server/src/vws_mock_server/healthcheck.py b/server/src/vws_mock_server/healthcheck.py new file mode 100644 index 000000000..0f8af1eee --- /dev/null +++ b/server/src/vws_mock_server/healthcheck.py @@ -0,0 +1,35 @@ +"""Health check for the Flask server.""" + +import http.client +import sys +from http import HTTPStatus + +from beartype import beartype + + +@beartype +def flask_app_healthy(port: int) -> bool: + """Check if the Flask app is healthy.""" + conn = http.client.HTTPConnection(host="localhost", port=port, timeout=5) + try: + conn.request(method="GET", url="/some-random-endpoint") + response = conn.getresponse() + # ``OSError`` covers ``TimeoutError``, ``ConnectionRefusedError`` and + # ``socket.gaierror``. + # ``ConnectionRefusedError`` is the expected error while the container is + # starting up and nothing is yet listening on the port. + except OSError, http.client.HTTPException: + return False + finally: + conn.close() + + return response.status in { + HTTPStatus.NOT_FOUND, + HTTPStatus.UNAUTHORIZED, + HTTPStatus.FORBIDDEN, + } + + +# Containers execute this process entry point; import-driven tests use its API. +if __name__ == "__main__": # pragma: no cover + sys.exit(int(not flask_app_healthy(port=5000))) diff --git a/server/src/vws_mock_server/py.typed b/server/src/vws_mock_server/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/src/mock_vws/_flask_server/Dockerfile b/src/mock_vws/_flask_server/Dockerfile index b9a5b398b..e55fe4a7d 100644 --- a/src/mock_vws/_flask_server/Dockerfile +++ b/src/mock_vws/_flask_server/Dockerfile @@ -21,12 +21,12 @@ WORKDIR /app # Install the locked dependencies before copying the source, so that # source edits do not invalidate the dependency layer. COPY --chown=10001:10001 pyproject.toml uv.lock /app/ -RUN uv sync --frozen --no-cache --no-dev --no-install-workspace +RUN uv sync --frozen --no-cache --no-dev --extra server --no-install-workspace COPY --chown=10001:10001 . /app -RUN uv sync --locked --no-cache --no-dev +RUN uv sync --locked --no-cache --no-dev --extra server EXPOSE 5000 ENTRYPOINT ["python"] -HEALTHCHECK --interval=1s --timeout=10s --start-period=5s --retries=3 CMD ["python", "/app/src/mock_vws/_flask_server/healthcheck.py"] +HEALTHCHECK --interval=1s --timeout=10s --start-period=5s --retries=3 CMD ["python", "-m", "vws_mock_server.healthcheck"] FROM base AS vws ENV VWS_HOST=0.0.0.0 diff --git a/src/mock_vws/_flask_server/healthcheck.py b/src/mock_vws/_flask_server/healthcheck.py index 0f8af1eee..72b2f2d39 100644 --- a/src/mock_vws/_flask_server/healthcheck.py +++ b/src/mock_vws/_flask_server/healthcheck.py @@ -1,33 +1,10 @@ -"""Health check for the Flask server.""" +"""Compatibility import for the server package's health check.""" -import http.client import sys -from http import HTTPStatus -from beartype import beartype +from vws_mock_server.healthcheck import flask_app_healthy - -@beartype -def flask_app_healthy(port: int) -> bool: - """Check if the Flask app is healthy.""" - conn = http.client.HTTPConnection(host="localhost", port=port, timeout=5) - try: - conn.request(method="GET", url="/some-random-endpoint") - response = conn.getresponse() - # ``OSError`` covers ``TimeoutError``, ``ConnectionRefusedError`` and - # ``socket.gaierror``. - # ``ConnectionRefusedError`` is the expected error while the container is - # starting up and nothing is yet listening on the port. - except OSError, http.client.HTTPException: - return False - finally: - conn.close() - - return response.status in { - HTTPStatus.NOT_FOUND, - HTTPStatus.UNAUTHORIZED, - HTTPStatus.FORBIDDEN, - } +__all__ = ["flask_app_healthy"] # Containers execute this process entry point; import-driven tests use its API. diff --git a/test-backends/pyproject.toml b/test-backends/pyproject.toml index bd25570d5..291029394 100644 --- a/test-backends/pyproject.toml +++ b/test-backends/pyproject.toml @@ -28,7 +28,7 @@ dependencies = [ "urllib3>=2.8", "vws-mock-contract-tests==0", "vws-python>=2026.10.9", - "vws-python-mock", + "vws-python-mock[server]", ] urls.Source = "https://github.com/VWS-Python/vws-python-mock" diff --git a/uv.lock b/uv.lock index 7b5cbf505..60ddae268 100644 --- a/uv.lock +++ b/uv.lock @@ -7,6 +7,7 @@ members = [ "vws-mock-contract-tests", "vws-mock-test-backends", "vws-python-mock", + "vws-python-mock-server", ] [[package]] @@ -2700,7 +2701,7 @@ dependencies = [ { name = "urllib3" }, { name = "vws-mock-contract-tests" }, { name = "vws-python" }, - { name = "vws-python-mock" }, + { name = "vws-python-mock", extra = ["server"] }, ] [package.metadata] @@ -2717,7 +2718,7 @@ requires-dist = [ { name = "urllib3", specifier = ">=2.8" }, { name = "vws-mock-contract-tests", editable = "contract-tests" }, { name = "vws-python", specifier = ">=2026.10.9" }, - { name = "vws-python-mock", editable = "." }, + { name = "vws-python-mock", extras = ["server"], editable = "." }, ] [[package]] @@ -2742,7 +2743,6 @@ name = "vws-python-mock" source = { editable = "." } dependencies = [ { name = "beartype" }, - { name = "flask" }, { name = "httpx" }, { name = "httpx2" }, { name = "mock-response-delay" }, @@ -2760,6 +2760,12 @@ dependencies = [ { name = "werkzeug" }, ] +[package.optional-dependencies] +server = [ + { name = "flask" }, + { name = "vws-python-mock-server" }, +] + [package.dev-dependencies] dev = [ { name = "actionlint-py" }, @@ -2828,6 +2834,7 @@ dev = [ { name = "vws-mock-contract-tests" }, { name = "vws-mock-test-backends" }, { name = "vws-python" }, + { name = "vws-python-mock-server" }, { name = "vws-test-fixtures" }, { name = "vws-web-tools" }, { name = "yamlfix" }, @@ -2841,7 +2848,7 @@ release = [ [package.metadata] requires-dist = [ { name = "beartype", specifier = ">=0.23.0" }, - { name = "flask", specifier = ">=3.0.3" }, + { name = "flask", marker = "extra == 'server'", specifier = ">=3.0.3" }, { name = "httpx", specifier = ">=0.27.0" }, { name = "httpx2", specifier = ">=2.12" }, { name = "mock-response-delay", specifier = ">=2026.9.12" }, @@ -2856,8 +2863,10 @@ requires-dist = [ { name = "respx", specifier = ">=0.21.0" }, { name = "tzdata", marker = "sys_platform == 'win32'" }, { name = "vws-auth-tools", specifier = ">=2024.7.12" }, + { name = "vws-python-mock-server", marker = "extra == 'server'", editable = "server" }, { name = "werkzeug", specifier = ">=3.1.2" }, ] +provides-extras = ["server"] [package.metadata.requires-dev] dev = [ @@ -2927,6 +2936,7 @@ dev = [ { name = "vws-mock-contract-tests", editable = "contract-tests" }, { name = "vws-mock-test-backends", editable = "test-backends" }, { name = "vws-python", specifier = "==2026.10.9" }, + { name = "vws-python-mock-server", editable = "server" }, { name = "vws-test-fixtures", specifier = "==2026.10.8" }, { name = "vws-web-tools", specifier = "==2026.8.27" }, { name = "yamlfix", specifier = "==1.19.1" }, @@ -2937,6 +2947,31 @@ release = [ { name = "towncrier", specifier = "==26.9.0" }, ] +[[package]] +name = "vws-python-mock-server" +version = "0.0.0" +source = { editable = "server" } +dependencies = [ + { name = "beartype" }, + { name = "flask" }, + { name = "pydantic" }, + { name = "pydantic-settings" }, + { name = "requests" }, + { name = "vws-python-mock" }, + { name = "werkzeug" }, +] + +[package.metadata] +requires-dist = [ + { name = "beartype", specifier = ">=0.23.1" }, + { name = "flask", specifier = ">=3.0.3" }, + { name = "pydantic", specifier = ">=2.6" }, + { name = "pydantic-settings", specifier = ">=2.6.1" }, + { name = "requests", specifier = ">=2.32.3" }, + { name = "vws-python-mock", editable = "." }, + { name = "werkzeug", specifier = ">=3.1.2" }, +] + [[package]] name = "vws-test-fixtures" version = "2026.10.8"