Files
ukmesh/viewshed-worker/tests/test_checker_shipping.py
hermes-gadget 79190f8382 fix(ci): ship the external-consumer SQL checker with the worker image (#196)
The Workers and Compose job runs the worker suite inside the built image
(`python -m unittest discover -s /app/tests`). The relation-qualification
test resolved the checker as `<repo>/scripts/check_external_consumer_sql.py`
via `Path(__file__).parents[2]`, which inside the container is `/scripts`,
and the image only carries `viewshed-worker/`, so the job died with
`ModuleNotFoundError: check_external_consumer_sql` before any test ran.

The image builds with `context: viewshed-worker`, so a repo-level `scripts/`
file cannot be COPYed in without widening the build context (and the release
workflow). Move the implementation next to the worker (`viewshed-worker/
check_external_consumer_sql.py`), leave a thin shim at the old path so
`backend`'s `test:external-consumers:sql` keeps working from a checkout, and
add the module to the Dockerfile COPY.

The test now resolves the module from the worker root, and a new in-image
test asserts the checker imports from that root, so dropping it from the
image fails with a named test instead of a bare import error.
2026-11-05 16:44:29 +00:00

54 lines
2.2 KiB
Python

"""Guard: the external-consumer SQL checker must ship inside the worker image.
``test_relation_qualification.py`` imports the checker from the worker root, and
CI runs this suite inside the built image (``python -m unittest discover -s
/app/tests``). When the checker lived only in the repo-level ``scripts/``
directory the container had no such path and the whole job died with
``ModuleNotFoundError: check_external_consumer_sql``.
This test can only pass when the module is present in the image's import path,
so deleting it from the ``COPY`` line in ``viewshed-worker/Dockerfile`` turns
into a named failure instead of a bare import error.
"""
import contextlib
import importlib
import io
import sys
import unittest
from pathlib import Path
WORKER_ROOT = Path(__file__).resolve().parents[1]
if str(WORKER_ROOT) not in sys.path:
sys.path.insert(0, str(WORKER_ROOT))
class CheckerShippingTest(unittest.TestCase):
def test_checker_is_importable_from_the_worker_root(self):
try:
checker = importlib.import_module("check_external_consumer_sql")
except ModuleNotFoundError as error: # pragma: no cover - the CI failure
self.fail(
f"check_external_consumer_sql is not importable from {WORKER_ROOT}: "
f"{error}. The worker image builds from viewshed-worker/, so the "
"module has to be COPYed there (see viewshed-worker/Dockerfile); "
"a repo-level scripts/ directory does not exist in the container."
)
module_file = getattr(checker, "__file__", None)
if module_file is None: # pragma: no cover - builtins only
self.fail("checker module has no __file__")
self.assertEqual(
Path(module_file).resolve().parent,
WORKER_ROOT,
"the checker must resolve from the worker tree, not a repo-level scripts/ dir",
)
def test_shipped_checker_cli_accepts_the_worker_tree(self):
checker = importlib.import_module("check_external_consumer_sql")
with contextlib.redirect_stdout(io.StringIO()):
exit_code = checker.main(["--root", str(WORKER_ROOT)])
self.assertEqual(exit_code, 0)
if __name__ == "__main__":
unittest.main()