Answer stability round 1: 1 🔴, 5 🟡 (0 misread); the 🔴, the rule text and the PATH half of the exemption were answered in the principles round 1 commit; exempt no docker under sudo or doas, test unknown memory and the shim without a share, keep helped beside unlimited, drop a dead fallback
This commit is contained in:
@@ -71,6 +71,12 @@ def unlimited(arguments: list[str]) -> bool:
|
||||
return subcommand in STARTS_CONTAINER and not limited and not helped(arguments)
|
||||
|
||||
|
||||
def helped(arguments: list[str]) -> bool:
|
||||
"""Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes."""
|
||||
after = subcommand_at(arguments)[-1] + 1
|
||||
return "--help" in arguments[:after + 1]
|
||||
|
||||
|
||||
def capped(arguments: list[str], share: str) -> list[str]:
|
||||
"""docker's arguments, with each limit a container they make lacks added right after the subcommand.
|
||||
|
||||
@@ -109,9 +115,3 @@ def option_at(arguments: list[str], options: tuple[str, ...], fits: typing.Calla
|
||||
if word == option and fits(following):
|
||||
return Option(at, 2, following)
|
||||
return None
|
||||
|
||||
|
||||
def helped(arguments: list[str]) -> bool:
|
||||
"""Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes."""
|
||||
after = subcommand_at(arguments)[-1] + 1
|
||||
return "--help" in arguments[:after + 1]
|
||||
|
||||
@@ -6,7 +6,7 @@ import typing
|
||||
from round_titles import CAP, lacks_counts, past_cap
|
||||
from git_command import commits
|
||||
from rule_hook.heavy_command import heavy
|
||||
from shell_command import launched, Simple
|
||||
from shell_command import launched, Simple, tool_name
|
||||
from review_folder import REPORTS
|
||||
from docker_command import unlimited
|
||||
|
||||
@@ -32,7 +32,7 @@ RULES: dict[RuleName, Rule] = {
|
||||
),
|
||||
"docker-run-memory": Rule(
|
||||
"a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap",
|
||||
"docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits, unless it names docker by its path or sets its own PATH. A container a script or docker compose starts is not checked",
|
||||
"docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits, unless it names docker by its path, sets its own PATH, or runs through sudo or doas. A container a script or docker compose starts is not checked",
|
||||
),
|
||||
"ignorefile-sorted": Rule(
|
||||
"each block of an ignore file is sorted in code-point order, as LC_ALL=C sort does, since its order carries no meaning; where order matters, such as a re-include after its exclude, split the block with a blank line or a comment",
|
||||
@@ -156,8 +156,9 @@ def unlimited_container(words: list[str]) -> bool:
|
||||
docker = launched(words, {"docker"})
|
||||
if docker is None or not unlimited(docker.arguments):
|
||||
return False
|
||||
# On Linux, run-heavy's docker adds the limits a container lacks, where PATH finds it: docker named bare, with no PATH of the command's own.
|
||||
capped = "run-heavy" in docker.launchers and words[len(words) - len(docker.arguments) - 1] == "docker" and not any(word.startswith("PATH=") for word in words)
|
||||
# On Linux, run-heavy's docker adds the limits a container lacks, where PATH finds it: docker named bare, with no PATH of the command's own, and no sudo or doas, which reset PATH.
|
||||
before = words[:len(words) - len(docker.arguments) - 1]
|
||||
capped = "run-heavy" in docker.launchers and words[len(before)] == "docker" and not any(word.startswith("PATH=") or tool_name(word) in ("doas", "sudo") for word in before)
|
||||
return not capped
|
||||
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ class CommandBreaches(unittest.TestCase):
|
||||
self.assertEqual(checked(command), [])
|
||||
|
||||
def test_a_docker_run_or_create_without_a_memory_limit_breaks_docker_run_memory(self):
|
||||
for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true"]:
|
||||
for command in ["docker run --rm node:24.22.1-alpine3.24 node x.js", "cd /w && docker run -v /w:/w alpine:3.22.2 true", "docker create alpine:3.22.2", "docker container run alpine:3.22.2 true", "docker container create alpine:3.22.2", "nice -n 19 docker run alpine:3.22.2 true", "docker -H unix:///d.sock run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 true", "docker run -m 4g alpine:3.22.2 true", "docker run --memory=4g alpine:3.22.2 sh -c 'x --memory-swap 4g'", "docker run -m 4g alpine:3.22.2 tool --memory-swap", "docker run -m 4g --memory-swap -1 alpine:3.22.2 true", "docker run -m 4g --memory-swap=0 alpine:3.22.2 true", "docker run --rm python:3.13.1 python -m pytest", "docker run alpine:3.22.2 git commit -m x", "timeout 60 docker run alpine:3.22.2 true", "docker run node:24.22.1-alpine3.24 node --help", "docker run alpine:3.22.2 --help", "docker run -m 4g alpine:3.22.2 sleep 60", "~/.claude/bin/run-heavy /usr/bin/docker run alpine:3.22.2 true", "docker run --memory-swap 4g alpine:3.22.2 grep -m 1 x", "run-heavy env PATH=/usr/bin docker run alpine:3.22.2 true", "run-heavy sudo docker run alpine:3.22.2 true"]:
|
||||
with self.subTest(command):
|
||||
self.assertEqual(checked(command), ["docker-run-memory"])
|
||||
|
||||
|
||||
@@ -32,4 +32,4 @@ def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
|
||||
def without_path_entry(path: str) -> str:
|
||||
"""path less PATH_ENTRY, so the real docker is the one found."""
|
||||
return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry or ".").resolve() != PATH_ENTRY)
|
||||
return os.pathsep.join(entry for entry in path.split(os.pathsep) if pathlib.Path(entry).resolve() != PATH_ENTRY)
|
||||
|
||||
@@ -115,6 +115,16 @@ class RunHeavy(unittest.TestCase):
|
||||
result = self.run_tool("sh", "-c", "command -v docker || echo none", env={**self.env, "PATH": folder.name})
|
||||
self.assertEqual((result.returncode, result.stdout), (0, "none\n"))
|
||||
|
||||
def test_the_capped_docker_passes_a_call_on_unchanged_without_a_share(self):
|
||||
env = self.fake_docker()
|
||||
env.pop("RUN_HEAVY_MEMORY", None)
|
||||
shim = pathlib.Path(TOOL).parent / "run_heavy" / "path_entry" / "docker"
|
||||
result = subprocess.run([str(shim), "run", "img"], capture_output=True, text=True, env=env, timeout=30)
|
||||
self.assertEqual((result.returncode, result.stdout), (0, "run img\n"))
|
||||
|
||||
def test_no_cap_where_the_memory_is_unknown(self):
|
||||
self.assertEqual(main.capping(None, 4, self.fake_docker()), {})
|
||||
|
||||
def test_a_capped_docker_that_finds_no_docker_exits_127_saying_so(self):
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
|
||||
Reference in New Issue
Block a user