feat(runner): add the own code runner contract, client and nsjail service
This commit is contained in:
1 parent
5e1db37746
commit
cdf6bcbb91
109 files changed
+24084
-28
No files matched your search
@@ -0,0 +1,97 @@
|
||||
"""Check that the dev-only code runner switches stay out of production files.
|
||||
|
||||
The local runner Compose file gives the container `privileged: true` and
|
||||
serves plain HTTP, because nsjail needs namespaces and a writable cgroup tree
|
||||
inside Docker Desktop. Production runs the runner only in its dedicated VM.
|
||||
These tests read files; the render test also needs the docker CLI.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
DOCKER_DIR = ROOT / "deploy" / "docker"
|
||||
DEV_FILE = DOCKER_DIR / "docker-compose.runner.dev.yml"
|
||||
DEPLOY_MAKEFILE = ROOT / "deploy" / "Makefile"
|
||||
RUNNER_DOCKERFILE = ROOT / "runner" / "Dockerfile"
|
||||
RELEASE_SCRIPT = ROOT / "deploy" / "scripts" / "release_manifest.py"
|
||||
DOCKER = os.environ.get("DOCKER", "docker")
|
||||
DEV_SWITCHES = ("privileged", "RUNNER_DEV_ALLOW_PLAINTEXT_HTTP", "docker-compose.runner.dev.yml")
|
||||
# Make targets that may use the dev runner file.
|
||||
DEV_TARGETS = {"config", "runner-dev-config", "runner-dev-build", "runner-dev-up", "runner-dev-down", "runner-dev-logs"}
|
||||
TARGET = re.compile(r"^([A-Za-z0-9_-]+):")
|
||||
|
||||
|
||||
class RunnerDevFilesTest(unittest.TestCase):
|
||||
def test_the_dev_file_is_named_and_marked_as_development_only(self) -> None:
|
||||
self.assertIn(".dev.", DEV_FILE.name)
|
||||
header = DEV_FILE.read_text(encoding="utf-8").splitlines()[0]
|
||||
self.assertTrue(header.startswith("# DEVELOPMENT ONLY"), header)
|
||||
|
||||
def test_production_compose_files_have_no_dev_switch(self) -> None:
|
||||
for path in sorted(DOCKER_DIR.glob("docker-compose*.yml")):
|
||||
if path == DEV_FILE:
|
||||
continue
|
||||
text = path.read_text(encoding="utf-8")
|
||||
for switch in DEV_SWITCHES:
|
||||
with self.subTest(file=path.name, switch=switch):
|
||||
self.assertNotIn(switch, text)
|
||||
|
||||
def test_the_production_image_has_no_dev_switch(self) -> None:
|
||||
text = RUNNER_DOCKERFILE.read_text(encoding="utf-8")
|
||||
self.assertNotIn("RUNNER_DEV_", text)
|
||||
self.assertNotIn("--privileged", text)
|
||||
|
||||
def test_release_tooling_never_uses_the_dev_file(self) -> None:
|
||||
self.assertNotIn(DEV_FILE.name, RELEASE_SCRIPT.read_text(encoding="utf-8"))
|
||||
|
||||
def test_only_dev_targets_use_the_dev_file(self) -> None:
|
||||
target = None
|
||||
users = set()
|
||||
for line in DEPLOY_MAKEFILE.read_text(encoding="utf-8").splitlines():
|
||||
match = TARGET.match(line)
|
||||
if match and not line.startswith(("\t", " ")):
|
||||
target = match.group(1)
|
||||
if "$(RUNNER_DEV_FILE)" in line and line.startswith("\t"):
|
||||
users.add(target)
|
||||
self.assertTrue(users)
|
||||
self.assertLessEqual(users, DEV_TARGETS)
|
||||
|
||||
|
||||
class RunnerDevRenderTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
if shutil.which(DOCKER) is None:
|
||||
raise unittest.SkipTest("docker CLI is not available")
|
||||
result = subprocess.run(
|
||||
[DOCKER, "compose", "-p", "runner-dev-check", "-f", str(DEV_FILE), "config", "--format", "json"],
|
||||
env={**os.environ, "RUNNER_DEV_TOKEN_FILE": "/dev/null"},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
raise AssertionError(f"docker compose config failed: {result.stderr}")
|
||||
cls.runner = json.loads(result.stdout)["services"]["runner"]
|
||||
|
||||
def test_the_dev_runner_publishes_only_loopback(self) -> None:
|
||||
ports = self.runner.get("ports") or []
|
||||
self.assertTrue(ports)
|
||||
for port in ports:
|
||||
self.assertEqual(port["host_ip"], "127.0.0.1")
|
||||
|
||||
def test_the_dev_runner_reads_the_token_from_a_secret_file(self) -> None:
|
||||
environment = self.runner["environment"]
|
||||
self.assertEqual(environment["RUNNER_API_TOKEN_FILE"], "/run/secrets/runner_api_token")
|
||||
self.assertNotIn("RUNNER_API_TOKEN", environment)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user