askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_docker_dependencies.py · 7.1 KB · 146 lines
| 1 | """The Dockerfile's dependency layer mirrors pyproject.toml's `server` and `scheduler` extras, and |
| 2 | scripts/install.sh (which the one-click install runs) installs the same two. |
| 3 | |
| 4 | The image runs both the server and (docker-compose.yml's object-daemon service) the daemon, and every |
| 5 | schedule the shipped packages declare is cron, so the daemon needs croniter: the Dockerfile installs the |
| 6 | `server` extra plus the `scheduler` extra. Its comment says to keep the two in sync; this is what makes |
| 7 | that true. Plain text parsing, no network, no Docker, and no tomllib (Python 3.10 has none). |
| 8 | """ |
| 9 | import re |
| 10 | from pathlib import Path |
| 11 | |
| 12 | REPO_ROOT = Path(__file__).resolve().parents[1] |
| 13 | IMAGE_EXTRAS = ("server", "scheduler") |
| 14 | |
| 15 | |
| 16 | def _extras(pyproject_text): |
| 17 | """{extra name: [requirement, ...]} from [project.optional-dependencies].""" |
| 18 | table = re.search(r"^\[project\.optional-dependencies\]\s*$(.*?)(?=^\[)", pyproject_text, |
| 19 | re.MULTILINE | re.DOTALL) |
| 20 | assert table, "pyproject.toml has no [project.optional-dependencies] table" |
| 21 | extras = {} |
| 22 | for name, body in re.findall(r"^([A-Za-z0-9_-]+)\s*=\s*\[(.*?)^\]", table.group(1), |
| 23 | re.MULTILINE | re.DOTALL): |
| 24 | lines = (line.split("#", 1)[0] for line in body.splitlines()) |
| 25 | extras[name] = re.findall(r'"([^"]+)"', "\n".join(lines)) |
| 26 | return extras |
| 27 | |
| 28 | |
| 29 | def _dockerfile_requirements(dockerfile_text): |
| 30 | """The requirements the default image installs for the server and daemon: the quoted names of the one |
| 31 | RUN instruction that pip-installs uvicorn (later stages, such as `tools`, add their own).""" |
| 32 | joined = re.sub(r"\\\n", " ", dockerfile_text) |
| 33 | runs = [line for line in joined.splitlines() |
| 34 | if line.lstrip().startswith("RUN") and "pip install" in line and "uvicorn" in line] |
| 35 | assert len(runs) == 1, f"expected one RUN that installs uvicorn, found {len(runs)}" |
| 36 | installs = [part for part in runs[0].split("&&") if "pip install" in part and "--upgrade pip" not in part] |
| 37 | assert len(installs) == 1, "expected one dependency install in that RUN" |
| 38 | return re.findall(r'"([^"]+)"', installs[0]) |
| 39 | |
| 40 | |
| 41 | def _wanted(pyproject_text): |
| 42 | extras = _extras(pyproject_text) |
| 43 | return sorted(req for name in IMAGE_EXTRAS for req in extras[name]) |
| 44 | |
| 45 | |
| 46 | def test_the_image_installs_exactly_the_server_and_scheduler_extras(): |
| 47 | wanted = _wanted((REPO_ROOT / "pyproject.toml").read_text()) |
| 48 | installed = sorted(_dockerfile_requirements((REPO_ROOT / "Dockerfile").read_text())) |
| 49 | assert installed == wanted |
| 50 | # the parsers found something: an empty-equals-empty pass would prove nothing |
| 51 | assert any(req.startswith("croniter") for req in installed) |
| 52 | assert any(req.startswith("uvicorn") for req in installed) |
| 53 | |
| 54 | |
| 55 | def test_the_scheduler_extra_stays_out_of_the_server_extra_and_the_base_install(): |
| 56 | text = (REPO_ROOT / "pyproject.toml").read_text() |
| 57 | extras = _extras(text) |
| 58 | assert not any(req.startswith("croniter") for req in extras["server"]) |
| 59 | assert re.search(r"^dependencies\s*=\s*\[\s*\]", text, re.MULTILINE) |
| 60 | |
| 61 | |
| 62 | def test_the_comparison_fails_when_the_dockerfile_and_pyproject_disagree(): |
| 63 | pyproject = (REPO_ROOT / "pyproject.toml").read_text() |
| 64 | dockerfile = (REPO_ROOT / "Dockerfile").read_text() |
| 65 | wanted = _wanted(pyproject) |
| 66 | |
| 67 | # croniter dropped from the image (the bug this guards): the lists differ |
| 68 | without = dockerfile.replace(' "croniter>=2.0"', "") |
| 69 | assert without != dockerfile |
| 70 | assert sorted(_dockerfile_requirements(without)) != wanted |
| 71 | |
| 72 | # a pin changed in one place only |
| 73 | repinned = dockerfile.replace('"websockets>=12.0"', '"websockets>=13.0"') |
| 74 | assert repinned != dockerfile |
| 75 | assert sorted(_dockerfile_requirements(repinned)) != wanted |
| 76 | |
| 77 | # and the other direction: pyproject gains a requirement the image lacks |
| 78 | widened = pyproject.replace('"croniter>=2.0",', '"croniter>=2.0",\n "extra-thing>=1",') |
| 79 | assert widened != pyproject |
| 80 | assert _wanted(widened) != sorted(_dockerfile_requirements(dockerfile)) |
| 81 | |
| 82 | |
| 83 | def test_extras_parser_ignores_comments_and_other_tables(): |
| 84 | sample = ( |
| 85 | '[project]\nname = "x"\ndependencies = []\n' |
| 86 | "[project.optional-dependencies]\n" |
| 87 | "# a comment with \"quoted\" words\n" |
| 88 | 'one = [\n "a>=1", # trailing "note"\n # "commented>=9",\n "b>=2",\n]\n' |
| 89 | 'two = [\n "c>=3",\n]\n' |
| 90 | '[project.urls]\nHomepage = "https://example.invalid"\n' |
| 91 | ) |
| 92 | assert _extras(sample) == {"one": ["a>=1", "b>=2"], "two": ["c>=3"]} |
| 93 | |
| 94 | |
| 95 | def test_install_sh_installs_the_scheduler_extra_for_the_daemon_unit_it_creates(): |
| 96 | """install.sh writes and starts dbbasic-object-daemon.service in the same virtualenv, and the one-click |
| 97 | install runs install.sh: without croniter there, that daemon can never run a cron schedule.""" |
| 98 | text = (REPO_ROOT / "scripts" / "install.sh").read_text() |
| 99 | installs = [line for line in text.splitlines() if "pip install" in line and "-e" in line] |
| 100 | assert len(installs) == 1, installs |
| 101 | match = re.search(r"\[([a-z,]+)\]", installs[0]) |
| 102 | assert match, installs[0] |
| 103 | assert set(match.group(1).split(",")) == set(IMAGE_EXTRAS) |
| 104 | assert "dbbasic-object-daemon.service" in text |
| 105 | |
| 106 | |
| 107 | # The optional `tools` stage: the third-party libraries objects ported from older servers import. |
| 108 | TOOLS_PACKAGES = ("pillow", "pyyaml", "requests", "markdown") |
| 109 | |
| 110 | |
| 111 | def _tools_requirements(dockerfile_text): |
| 112 | """The quoted requirements the `tools` stage pip-installs.""" |
| 113 | joined = re.sub(r"\\\n", " ", dockerfile_text) |
| 114 | stage = re.search(r"^FROM\s+\S+\s+AS\s+tools\s*$(.*?)(?=^FROM\s|\Z)", joined, |
| 115 | re.MULTILINE | re.DOTALL | re.IGNORECASE) |
| 116 | assert stage, "the Dockerfile has no `tools` stage" |
| 117 | installs = [line for line in stage.group(1).splitlines() |
| 118 | if line.lstrip().startswith("RUN") and "pip install" in line] |
| 119 | return [req for line in installs for req in re.findall(r'"([^"]+)"', line)] |
| 120 | |
| 121 | |
| 122 | def test_the_tools_stage_installs_every_expected_package_exactly_pinned(): |
| 123 | installed = _tools_requirements((REPO_ROOT / "Dockerfile").read_text()) |
| 124 | names = {} |
| 125 | for req in installed: |
| 126 | match = re.fullmatch(r"([A-Za-z0-9_.-]+)==\d+(\.\d+)*", req) |
| 127 | assert match, f"tools-stage requirement {req!r} is not pinned with ==" |
| 128 | names[match.group(1).lower()] = req |
| 129 | assert sorted(names) == sorted(TOOLS_PACKAGES) |
| 130 | |
| 131 | |
| 132 | def test_the_tools_stage_check_fails_when_markdown_is_missing_or_unpinned(): |
| 133 | dockerfile = (REPO_ROOT / "Dockerfile").read_text() |
| 134 | pin = next(req for req in _tools_requirements(dockerfile) if req.startswith("markdown==")) |
| 135 | without = dockerfile.replace(f' "{pin}"', "") |
| 136 | assert without != dockerfile |
| 137 | assert not any(req.startswith("markdown") for req in _tools_requirements(without)) |
| 138 | # the default image stays stdlib-only plus the server extras: markdown is in the tools stage only |
| 139 | assert not any(req.startswith("markdown") for req in _dockerfile_requirements(dockerfile)) |
| 140 | |
| 141 | |
| 142 | def test_the_docker_doc_names_every_tools_package(): |
| 143 | text = (REPO_ROOT / "docs" / "docker-deployment.md").read_text() |
| 144 | section = text.split("## Optional Tools Image", 1)[1].split("\n## ", 1)[0] |
| 145 | for name in TOOLS_PACKAGES: |
| 146 | assert f"`{name}`" in section, name |