Files
2026-11-09 08:43:30 +03:00

96 lines
3.3 KiB
Python

"""The container entry point, run with a stub `python` that records how it was called."""
import os
import stat
import subprocess
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[3]
ENTRYPOINT = ROOT / "docker" / "entrypoint.sh"
STUB = """#!/bin/sh
echo "$*" >> "$STUB_LOG"
if [ -n "${STUB_FAIL_ON:-}" ]; then
case "$*" in *"$STUB_FAIL_ON"*) exit 3;; esac
fi
exit 0
"""
@pytest.fixture
def run(tmp_path):
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
stub = bin_dir / "python"
stub.write_text(STUB)
stub.chmod(stub.stat().st_mode | stat.S_IEXEC)
log = tmp_path / "calls.log"
def go(*args, **env):
full = {"PATH": f"{bin_dir}:/usr/bin:/bin", "STUB_LOG": str(log), **env}
proc = subprocess.run(["sh", str(ENTRYPOINT), *args], env=full, capture_output=True, text=True, cwd=tmp_path, check=False)
calls = log.read_text().splitlines() if log.exists() else []
if log.exists():
log.unlink()
return proc.returncode, calls
return go
def test_serve_is_the_default_and_does_not_touch_the_database_by_itself(run):
assert run() == (0, ["-m estimator serve"])
assert run("serve") == (0, ["-m estimator serve"])
@pytest.mark.parametrize("value", ["false", "", "yes", "1", "TRUE"])
def test_only_the_exact_value_true_turns_migrate_on_start_on(run, value):
assert run("serve", MIGRATE_ON_START=value) == (0, ["-m estimator serve"])
def test_migrations_run_before_the_service_starts(run):
code, calls = run("serve", MIGRATE_ON_START="true")
assert code == 0
assert calls == ["-m estimator wait-db --timeout 60", "-m estimator migrate", "-m estimator serve"] # in this order
def test_the_wait_time_is_configurable(run):
_, calls = run("serve", MIGRATE_ON_START="true", DB_WAIT_TIMEOUT="5")
assert calls[0] == "-m estimator wait-db --timeout 5"
def test_a_failed_migration_stops_the_start(run):
code, calls = run("serve", MIGRATE_ON_START="true", STUB_FAIL_ON="migrate")
assert code == 3
assert "-m estimator serve" not in calls and calls[-1] == "-m estimator migrate"
def test_an_unreachable_database_stops_the_start_before_any_migration(run):
code, calls = run("serve", MIGRATE_ON_START="true", STUB_FAIL_ON="wait-db")
assert code == 3 and calls == ["-m estimator wait-db --timeout 60"]
def test_the_migrate_job_waits_then_applies_and_exits(run):
assert run("migrate") == (0, ["-m estimator wait-db --timeout 60", "-m estimator migrate head"])
assert run("migrate", "0001")[1][-1] == "-m estimator migrate 0001" # an explicit revision can be given
def test_the_migrate_job_fails_when_the_database_never_comes_up(run):
code, calls = run("migrate", STUB_FAIL_ON="wait-db")
assert code == 3 and calls == ["-m estimator wait-db --timeout 60"]
@pytest.mark.parametrize(
"args",
[("rag", "import", "/data/refs.csv", "--project", "P"), ("hash-password",), ("rag", "stats")],
)
def test_other_commands_pass_through_unchanged(run, args):
assert run(*args) == (0, ["-m estimator " + " ".join(args)])
def test_the_script_is_executable_and_posix_sh():
assert os.access(ENTRYPOINT, os.X_OK)
first = ENTRYPOINT.read_text().splitlines()[0]
assert first == "#!/bin/sh" # the slim image has no bash
assert "set -eu" in ENTRYPOINT.read_text()