Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -8,16 +8,18 @@ adf-codec has two pairs of calls, and each function here names the one it runs:
|
||||
"""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import fcntl
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from machine_load.slots import HELD, slot_count, slot_folder, usable_cpus, waited_slot
|
||||
from machine_load.slots import HELD, Lock, slot_count, slot_folder, usable_cpus, waited_slot
|
||||
|
||||
SHIM = pathlib.Path(__file__).resolve().parent / "node_shim"
|
||||
SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs")
|
||||
@@ -62,7 +64,13 @@ def docker_args(image: str, mode: str) -> list[str]:
|
||||
return ["docker", "run", "--rm", "-i", "--memory", MEMORY, "--memory-swap", MEMORY, "--network", "none", "--read-only", image, mode]
|
||||
|
||||
|
||||
def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any] = subprocess.run) -> Ran | Failure:
|
||||
def docker(args: list[str], text: str, seconds: int) -> Ran | Failure:
|
||||
"""Runs a docker command line as a process of this one: the shim's shell, as git_repo.git is git's."""
|
||||
return docker_ran(args, text, seconds, subprocess.run)
|
||||
|
||||
|
||||
def docker_ran(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any]) -> Ran | Failure:
|
||||
"""What call, which runs a command line as subprocess.run does, made of a docker command line on text."""
|
||||
try:
|
||||
done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds)
|
||||
except FileNotFoundError:
|
||||
@@ -73,16 +81,22 @@ def docker(args: list[str], text: str, seconds: int, call: typing.Callable[...,
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = None) -> typing.Iterator[Failure | None]:
|
||||
"""Holds one of the machine's heavy slots, which run-heavy shares, for the with block. Under run-heavy the process already holds one, so it doesn't wait."""
|
||||
def machine_slot() -> typing.Iterator[Failure | None]:
|
||||
"""heavy_slot on this process's environment, home, processors, stderr and clock: the shim's shell, as docker is."""
|
||||
with heavy_slot(os.environ, pathlib.Path.home(), slot_count(usable_cpus()), lambda text: print(text, file=sys.stderr, flush=True), time.sleep, fcntl.flock) as refused:
|
||||
yield refused
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def heavy_slot(env: typing.Mapping[str, str], home: pathlib.Path, slots: int, say: typing.Callable[[str], None], sleep: typing.Callable[[float], None], lock: Lock) -> typing.Iterator[Failure | None]:
|
||||
"""Holds one of the machine's heavy slots, which run-heavy shares, for the with block, telling say where it waits for one. Under run-heavy the process already holds one, so it doesn't wait."""
|
||||
if HELD in env:
|
||||
yield None
|
||||
return
|
||||
folder = slot_folder(env, pathlib.Path.home())
|
||||
count = slot_count(usable_cpus()) if slots is None else slots
|
||||
folder = slot_folder(env, home)
|
||||
try:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
_, fd = waited_slot(folder, count, lambda: print(f"adf-codec: waiting for a heavy slot, of {count} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True))
|
||||
_, fd = waited_slot(folder, slots, lambda: say(f"adf-codec: waiting for a heavy slot, of {slots} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them"), sleep, lock)
|
||||
except OSError as failure:
|
||||
yield Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}")
|
||||
return
|
||||
@@ -92,7 +106,7 @@ def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = N
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = heavy_slot) -> str | Failure:
|
||||
def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = machine_slot) -> str | Failure:
|
||||
"""The tag of the image for the shim folder, built from that folder first where this machine has no image under that tag.
|
||||
|
||||
The build waits for a heavy slot. Once this process holds the slot, it inspects again, as another process may have built the image meanwhile."""
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import contextlib
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
@@ -161,7 +162,7 @@ def test_docker_passes_the_text_on_stdin_and_returns_the_output():
|
||||
seen.update(kwargs, args=args)
|
||||
return subprocess.CompletedProcess(args, 3, "out", "err")
|
||||
|
||||
assert convert.docker(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err")
|
||||
assert convert.docker_ran(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err")
|
||||
assert seen == {"args": ["docker", "x"], "input": "text", "capture_output": True, "text": True, "encoding": "utf-8", "timeout": 7}
|
||||
|
||||
|
||||
@@ -169,7 +170,7 @@ def test_docker_missing_becomes_a_failure():
|
||||
def call(args, **kwargs):
|
||||
raise FileNotFoundError
|
||||
|
||||
failure = convert.docker(["docker", "x"], "", 7, call)
|
||||
failure = convert.docker_ran(["docker", "x"], "", 7, call)
|
||||
assert failure == Failure("docker is not installed, and the adf-codec shim runs in a container")
|
||||
|
||||
|
||||
@@ -177,7 +178,7 @@ def test_docker_that_takes_too_long_becomes_a_failure():
|
||||
def call(args, **kwargs):
|
||||
raise subprocess.TimeoutExpired(args, 7)
|
||||
|
||||
assert convert.docker(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds")
|
||||
assert convert.docker_ran(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds")
|
||||
|
||||
|
||||
def shim_dir(tmp_path):
|
||||
@@ -260,19 +261,27 @@ def test_a_failure_to_start_docker_passes_through_inspect_and_build(tmp_path, an
|
||||
assert convert.ensure_image(tmp_path, run_docker, Slot(run_docker)) == DOWN
|
||||
|
||||
|
||||
def not_said(text):
|
||||
raise AssertionError(f"said {text}")
|
||||
|
||||
|
||||
def not_slept(seconds):
|
||||
raise AssertionError(f"slept {seconds} seconds")
|
||||
|
||||
|
||||
def test_a_heavy_slot_is_held_until_the_block_ends(tmp_path):
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, 1) as held:
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held:
|
||||
assert held is None
|
||||
assert slots.taken_slot(tmp_path / "run-heavy", 1) is None
|
||||
slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1)
|
||||
assert slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock) is None
|
||||
slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock)
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path):
|
||||
(tmp_path / "run-heavy").mkdir()
|
||||
slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1)
|
||||
slot, fd = slots.taken_slot(tmp_path / "run-heavy", 1, fcntl.flock)
|
||||
try:
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path), slots.HELD: "0"}, 1) as held:
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path), slots.HELD: "0"}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held:
|
||||
assert held is None
|
||||
finally:
|
||||
os.close(fd)
|
||||
@@ -280,7 +289,7 @@ def test_a_process_under_run_heavy_takes_no_second_slot(tmp_path):
|
||||
|
||||
def test_a_heavy_slot_folder_that_cannot_be_made_yields_a_failure(tmp_path):
|
||||
(tmp_path / "run-heavy").write_text("a file where the folder goes")
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, 1) as held:
|
||||
with convert.heavy_slot({"XDG_STATE_HOME": str(tmp_path)}, tmp_path, 1, not_said, not_slept, fcntl.flock) as held:
|
||||
assert isinstance(held, Failure) and held.reason.startswith(f"cannot take a heavy slot in {tmp_path / 'run-heavy'}: ")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user