mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Review of 9eabacc: lstat/O_NOFOLLOW only protected the final path
component, so a symlinked ancestor (e.g. /var/run) was accepted and
mkdir(parents=True) followed ancestors before validation. The opt-in log
directory is now walked from "/" one component at a time with
O_DIRECTORY|O_NOFOLLOW relative to the parent fd, missing components are
created 0700 relative to that fd, relative paths and ".." are rejected,
and ownership/permissions are checked on the final opened fd.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
603 lines
23 KiB
Python
603 lines
23 KiB
Python
"""CPU-only launch contracts; no CUDA imports, real workers, or network binds."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import errno
|
|
import os
|
|
import socket
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, Mock, call
|
|
|
|
import psutil
|
|
import pytest
|
|
|
|
from obliteratus.distributed.launcher import _REQUIRED_ENV
|
|
from tests.conditional import _llama_tp_harness as harness
|
|
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("shape", "topology", "rendezvous"),
|
|
[
|
|
(
|
|
"local_tp2", ["--nnodes=1", "--nproc-per-node=2"],
|
|
[
|
|
"--rdzv-backend=static", "--master-addr=10.1.2.3",
|
|
"--master-port=23456", "--node-rank=0",
|
|
],
|
|
),
|
|
(
|
|
"two_host_tp2", ["--nnodes=2", "--nproc-per-node=1"],
|
|
["--rdzv-backend=c10d", "--rdzv-endpoint=10.1.2.3:23456"],
|
|
),
|
|
],
|
|
)
|
|
def test_torchrun_command_uses_shape_specific_rendezvous(shape, topology, rendezvous):
|
|
command = harness._torchrun_command(
|
|
shape=shape,
|
|
master_addr="10.1.2.3",
|
|
master_port=23456,
|
|
rendezvous_id="test-attempt",
|
|
worker_path=Path("worker.py"),
|
|
job_path=Path("job.json"),
|
|
)
|
|
assert command == [
|
|
sys.executable, "-m", "torch.distributed.run", *topology,
|
|
*rendezvous, "--rdzv-id=test-attempt", "--max-restarts=0",
|
|
"worker.py", "--obliteratus-tp-worker", "job.json",
|
|
]
|
|
|
|
|
|
def test_torchrun_command_rejects_unknown_shape():
|
|
with pytest.raises(ValueError, match="unknown TP qualification shape"):
|
|
harness._torchrun_command(
|
|
shape="unknown", master_addr="10.1.2.3", master_port=23456,
|
|
rendezvous_id="attempt", worker_path=Path("worker.py"), job_path=Path("job.json"),
|
|
)
|
|
|
|
|
|
def _address(value, family=socket.AF_INET):
|
|
return SimpleNamespace(family=family, address=value)
|
|
|
|
|
|
@pytest.fixture
|
|
def network_interfaces(monkeypatch):
|
|
interfaces = {}
|
|
monkeypatch.delenv("OBLITERATUS_TP_INTERFACE", raising=False)
|
|
monkeypatch.setattr(psutil, "net_if_addrs", lambda: interfaces)
|
|
listener = MagicMock()
|
|
listener.getsockname.return_value = ("ignored", 23456)
|
|
socket_factory = MagicMock()
|
|
socket_factory.return_value.__enter__.return_value = listener
|
|
monkeypatch.setattr(harness.socket, "socket", socket_factory)
|
|
return interfaces, listener, socket_factory
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"virtual_interface",
|
|
["docker0", "br-075fd822bba9", "veth0", "virbr0", "cni0", "flannel0", "tailscale0", "lo"],
|
|
)
|
|
def test_private_network_prefers_sorted_physical_interfaces(network_interfaces, virtual_interface):
|
|
interfaces, listener, socket_factory = network_interfaces
|
|
# Insertion order differs from the deterministic interface-name order.
|
|
interfaces.update({
|
|
"enp2s0": [_address("10.1.2.4")],
|
|
virtual_interface: [_address("172.27.0.1")],
|
|
"enp1s0": [_address("10.1.2.3")],
|
|
})
|
|
assert harness._private_network_profile() == ("enp1s0", "10.1.2.3", "10.1.2.3/32", 23456)
|
|
socket_factory.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
|
|
listener.bind.assert_called_once_with(("10.1.2.3", 0))
|
|
socket_factory.return_value.__exit__.assert_called_once()
|
|
|
|
|
|
@pytest.mark.parametrize("no_private_nic", [False, True])
|
|
def test_private_network_falls_back_to_sorted_virtual_interfaces(
|
|
network_interfaces, no_private_nic,
|
|
):
|
|
interfaces, listener, _ = network_interfaces
|
|
interfaces.update({
|
|
"docker0": [_address("172.18.0.1")],
|
|
"br-075fd822bba9": [_address("172.27.0.1")],
|
|
})
|
|
if no_private_nic:
|
|
interfaces["enp1s0"] = [_address("203.0.113.1")]
|
|
assert harness._private_network_profile() == (
|
|
"br-075fd822bba9", "172.27.0.1", "172.27.0.1/32", 23456,
|
|
)
|
|
listener.bind.assert_called_once_with(("172.27.0.1", 0))
|
|
|
|
|
|
def test_private_network_uses_bridge_when_it_is_the_only_interface(network_interfaces):
|
|
interfaces, _, _ = network_interfaces
|
|
interfaces["br-only"] = [_address("172.27.0.1")]
|
|
assert harness._private_network_profile() == (
|
|
"br-only", "172.27.0.1", "172.27.0.1/32", 23456,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("interface", ["br-test", "enp2s0"])
|
|
def test_private_network_honors_exact_override(network_interfaces, monkeypatch, interface):
|
|
interfaces, listener, _ = network_interfaces
|
|
interfaces.update({
|
|
"br-test": [_address("172.27.0.1")],
|
|
"enp1s0": [_address("10.1.2.3")],
|
|
"enp2s0": [_address("192.168.1.2")],
|
|
})
|
|
monkeypatch.setenv("OBLITERATUS_TP_INTERFACE", interface)
|
|
address = interfaces[interface][0].address
|
|
assert harness._private_network_profile() == (interface, address, f"{address}/32", 23456)
|
|
listener.bind.assert_called_once_with((address, 0))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"records",
|
|
[
|
|
None, [], [_address("203.0.113.1")], [_address("127.0.1.1")],
|
|
[_address("100.64.0.1")], [_address("fe80::1", socket.AF_INET6)],
|
|
[_address("invalid")], [_address(None)],
|
|
],
|
|
)
|
|
def test_private_network_invalid_override_skips_without_fallback(
|
|
network_interfaces, monkeypatch, records,
|
|
):
|
|
interfaces, _, socket_factory = network_interfaces
|
|
interfaces["enp1s0"] = [_address("10.1.2.3")]
|
|
if records is not None:
|
|
interfaces["chosen"] = records
|
|
monkeypatch.setenv("OBLITERATUS_TP_INTERFACE", "chosen")
|
|
with pytest.raises(
|
|
pytest.skip.Exception, match="non-coverage: OBLITERATUS_TP_INTERFACE=.*RFC1918",
|
|
):
|
|
harness._private_network_profile()
|
|
socket_factory.assert_not_called()
|
|
|
|
|
|
def test_private_network_skips_without_any_private_ipv4(network_interfaces):
|
|
interfaces, _, socket_factory = network_interfaces
|
|
interfaces["enp1s0"] = [_address("203.0.113.1"), _address("::1", socket.AF_INET6)]
|
|
with pytest.raises(pytest.skip.Exception, match="non-coverage: no private numeric"):
|
|
harness._private_network_profile()
|
|
socket_factory.assert_not_called()
|
|
|
|
|
|
def test_worker_environment_keeps_parent_harness_controls_out(monkeypatch, tmp_path):
|
|
controls = {"OBLITERATUS_TP_WORKER_LOG_DIR", "OBLITERATUS_TP_INTERFACE"}
|
|
for name in controls:
|
|
monkeypatch.setenv(name, "parent-only")
|
|
environment, allowed = harness._worker_environment(
|
|
repository_root=tmp_path, run_id="attempt", interface="enp1s0",
|
|
)
|
|
assert controls.isdisjoint(environment)
|
|
assert controls.isdisjoint(allowed)
|
|
assert environment["GLOO_SOCKET_IFNAME"] == "enp1s0"
|
|
assert environment["NCCL_SOCKET_IFNAME"] == "enp1s0"
|
|
assert environment["OBLITERATUS_RUN_ID"] == "attempt"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("name", "value"),
|
|
[
|
|
("LC_CTYPE", "C.UTF-8"),
|
|
("ROLE_NAME", "default"),
|
|
("TORCHELASTIC_SIGNALS_TO_HANDLE", "SIGTERM,SIGINT,SIGHUP,SIGQUIT"),
|
|
],
|
|
)
|
|
def test_worker_environment_allows_torchrun_and_locale_keys(monkeypatch, tmp_path, name, value):
|
|
monkeypatch.setenv(name, value)
|
|
environment, allowed = harness._worker_environment(
|
|
repository_root=tmp_path, run_id="attempt", interface="enp1s0",
|
|
)
|
|
assert name in allowed
|
|
assert environment[name] == value
|
|
|
|
|
|
def test_worker_environment_allows_documented_torchrun_213_worker_keys(tmp_path):
|
|
_, allowed = harness._worker_environment(
|
|
repository_root=tmp_path, run_id="attempt", interface="enp1s0",
|
|
)
|
|
worker_keys = {
|
|
"CUDA_VISIBLE_DEVICES",
|
|
"GROUP_RANK",
|
|
"GROUP_WORLD_SIZE",
|
|
"HOME",
|
|
"LANG",
|
|
"LC_CTYPE",
|
|
"LOCAL_RANK",
|
|
"LOCAL_WORLD_SIZE",
|
|
"MASTER_ADDR",
|
|
"MASTER_PORT",
|
|
"OMP_NUM_THREADS",
|
|
"PATH",
|
|
"PYTHONNOUSERSITE",
|
|
"RANK",
|
|
"ROLE_NAME",
|
|
"ROLE_RANK",
|
|
"ROLE_WORLD_SIZE",
|
|
"TORCHELASTIC_ERROR_FILE",
|
|
"TORCHELASTIC_MAX_RESTARTS",
|
|
"TORCHELASTIC_RESTART_COUNT",
|
|
"TORCHELASTIC_RUN_ID",
|
|
"TORCHELASTIC_SIGNALS_TO_HANDLE",
|
|
"TORCHELASTIC_USE_AGENT_STORE",
|
|
"TORCH_NCCL_ASYNC_ERROR_HANDLING",
|
|
"WORLD_SIZE",
|
|
}
|
|
assert worker_keys <= _REQUIRED_ENV | set(allowed)
|
|
|
|
|
|
def test_process_group_discards_output_by_default(monkeypatch, tmp_path):
|
|
monkeypatch.delenv("OBLITERATUS_TP_WORKER_LOG_DIR", raising=False)
|
|
process = Mock(returncode=7)
|
|
popen = Mock(return_value=process)
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
environment = {"PATH": "/usr/bin"}
|
|
assert harness._run_process_group(["worker"], cwd=tmp_path, environment=environment) == 7
|
|
popen.assert_called_once_with(
|
|
["worker"], cwd=tmp_path, env=environment,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True,
|
|
)
|
|
process.wait.assert_called_once_with(timeout=600)
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("outcome", ["success", "nonzero", "timeout", "launch_failure"])
|
|
@pytest.mark.parametrize("existing_directory", [False, True])
|
|
def test_process_group_captures_private_logs_and_closes_them(
|
|
monkeypatch, tmp_path, outcome, existing_directory,
|
|
):
|
|
directory = tmp_path / "private-logs"
|
|
if existing_directory:
|
|
directory.mkdir(mode=0o700)
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
|
descriptors = []
|
|
opened_descriptors = []
|
|
directory_descriptors = []
|
|
opened = []
|
|
real_open = os.open
|
|
|
|
def open_log(path, flags, mode=0o777, *, dir_fd=None):
|
|
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
|
opened_descriptors.append(descriptor)
|
|
if flags & os.O_DIRECTORY:
|
|
assert flags == os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
|
if dir_fd is None:
|
|
assert path == "/"
|
|
else:
|
|
assert Path(path).name == path
|
|
assert dir_fd == directory_descriptors[-1]
|
|
directory_descriptors.append(descriptor)
|
|
else:
|
|
assert dir_fd == directory_descriptors[-1]
|
|
opened.append((path, flags, mode))
|
|
return descriptor
|
|
|
|
monkeypatch.setattr(harness.os, "open", open_log)
|
|
process = Mock(returncode=7 if outcome == "nonzero" else 0, pid=12345)
|
|
|
|
def wait(**_kwargs):
|
|
# Only the final directory and log descriptors remain open for the child.
|
|
for descriptor in [directory_descriptors[-1], *descriptors[-2:]]:
|
|
os.fstat(descriptor)
|
|
if outcome == "timeout" and process.wait.call_count == 1:
|
|
raise subprocess.TimeoutExpired("worker", 600)
|
|
|
|
process.wait.side_effect = wait
|
|
killpg = Mock()
|
|
monkeypatch.setattr(harness.os, "killpg", killpg)
|
|
|
|
def launch(command, **kwargs):
|
|
assert command == ["worker"]
|
|
assert kwargs["cwd"] == tmp_path
|
|
assert kwargs["env"] == {"PATH": "/usr/bin"}
|
|
assert kwargs["start_new_session"] is True
|
|
for name in ("stdout", "stderr"):
|
|
descriptor = kwargs[name]
|
|
descriptors.append(descriptor)
|
|
assert stat.S_IMODE(os.fstat(descriptor).st_mode) == 0o600
|
|
os.write(descriptor, name.encode())
|
|
if outcome == "launch_failure":
|
|
raise OSError("launch failed")
|
|
return process
|
|
|
|
monkeypatch.setattr(harness.subprocess, "Popen", launch)
|
|
|
|
def run():
|
|
opened_descriptors.clear()
|
|
try:
|
|
return harness._run_process_group(
|
|
["worker"], cwd=tmp_path, environment={"PATH": "/usr/bin"},
|
|
)
|
|
finally:
|
|
# Check this launch before another launch can reuse its descriptor numbers.
|
|
for descriptor in opened_descriptors:
|
|
with pytest.raises(OSError) as error:
|
|
os.fstat(descriptor)
|
|
assert error.value.errno == errno.EBADF
|
|
|
|
if outcome == "timeout":
|
|
with pytest.raises(pytest.fail.Exception, match="process-hard deadline"):
|
|
run()
|
|
killpg.assert_called_once_with(process.pid, harness.signal.SIGKILL)
|
|
assert process.wait.call_args_list == [call(timeout=600), call(timeout=30)]
|
|
elif outcome == "launch_failure":
|
|
with pytest.raises(OSError, match="launch failed"):
|
|
run()
|
|
process.wait.assert_not_called()
|
|
else:
|
|
# Reusing the directory creates a fresh pair and preserves the first pair.
|
|
for _ in range(2):
|
|
assert run() == process.returncode
|
|
assert stat.S_IMODE(directory.stat().st_mode) == 0o700
|
|
paths = list(directory.iterdir())
|
|
assert len(paths) == len(descriptors) == len(opened)
|
|
assert len(directory_descriptors) == len(directory.parts) * (len(descriptors) // 2)
|
|
for name, flags, mode in opened:
|
|
assert Path(name).name == name
|
|
path = directory / name
|
|
assert flags == os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW
|
|
assert mode == 0o600
|
|
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
|
assert path.read_bytes() == (b"stdout" if path.name.endswith("stdout.log") else b"stderr")
|
|
|
|
|
|
@pytest.mark.parametrize("mode", [0o777, 0o770, 0o702])
|
|
def test_process_group_rejects_group_or_other_writable_log_directory(monkeypatch, tmp_path, mode):
|
|
directory = tmp_path / "shared-logs"
|
|
directory.mkdir()
|
|
directory.chmod(mode)
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*group/other write bits"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert list(directory.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", ["", "/"])
|
|
def test_process_group_rejects_symlinked_log_directory(monkeypatch, tmp_path, suffix):
|
|
target = tmp_path / "private-logs"
|
|
target.mkdir(mode=0o700)
|
|
directory = tmp_path / "linked-logs"
|
|
directory.symlink_to(target, target_is_directory=True)
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", f"{directory}{suffix}")
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*not a symlink"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert list(target.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("existing_directory", [False, True])
|
|
def test_process_group_rejects_symlinked_log_ancestor(monkeypatch, tmp_path, existing_directory):
|
|
target = tmp_path / "target"
|
|
target.mkdir(mode=0o700)
|
|
directory = target / "private-logs"
|
|
if existing_directory:
|
|
directory.mkdir(mode=0o700)
|
|
ancestor = tmp_path / "linked-ancestor"
|
|
ancestor.symlink_to(target, target_is_directory=True)
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(ancestor / directory.name))
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*not a symlink"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert sorted(target.iterdir()) == ([directory] if existing_directory else [])
|
|
if existing_directory:
|
|
assert list(directory.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("swap_after_open", [False, True])
|
|
def test_worker_log_directory_does_not_follow_an_ancestor_swapped_to_a_symlink(
|
|
monkeypatch, tmp_path, swap_after_open,
|
|
):
|
|
ancestor = tmp_path / "ancestor"
|
|
ancestor.mkdir(mode=0o700)
|
|
displaced = tmp_path / "displaced"
|
|
target = tmp_path / "target"
|
|
target.mkdir(mode=0o700)
|
|
real_open = os.open
|
|
descriptors = []
|
|
swapped = False
|
|
|
|
def open_directory(path, flags, mode=0o777, *, dir_fd=None):
|
|
nonlocal swapped
|
|
assert flags == os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
|
if dir_fd is None:
|
|
assert path == "/"
|
|
else:
|
|
assert Path(path).name == path
|
|
assert dir_fd == descriptors[-1]
|
|
swapping = path == ancestor.name
|
|
if swapping and not swap_after_open:
|
|
ancestor.rename(displaced)
|
|
ancestor.symlink_to(target, target_is_directory=True)
|
|
swapped = True
|
|
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
|
descriptors.append(descriptor)
|
|
if swapping and swap_after_open:
|
|
ancestor.rename(displaced)
|
|
ancestor.symlink_to(target, target_is_directory=True)
|
|
swapped = True
|
|
return descriptor
|
|
|
|
monkeypatch.setattr(harness.os, "open", open_directory)
|
|
if swap_after_open:
|
|
descriptor = harness._open_worker_log_directory(ancestor / "private-logs")
|
|
try:
|
|
actual = os.fstat(descriptor)
|
|
expected = (displaced / "private-logs").stat()
|
|
assert (actual.st_dev, actual.st_ino) == (expected.st_dev, expected.st_ino)
|
|
finally:
|
|
os.close(descriptor)
|
|
else:
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*not a symlink"):
|
|
harness._open_worker_log_directory(ancestor / "private-logs")
|
|
assert list(displaced.iterdir()) == []
|
|
assert swapped
|
|
assert list(target.iterdir()) == []
|
|
for descriptor in descriptors:
|
|
with pytest.raises(OSError) as error:
|
|
os.fstat(descriptor)
|
|
assert error.value.errno == errno.EBADF
|
|
|
|
|
|
def test_worker_log_directory_creates_missing_components_privately(monkeypatch, tmp_path):
|
|
directory = tmp_path / "missing-parent" / "missing-child" / "private-logs"
|
|
real_mkdir = os.mkdir
|
|
created = []
|
|
|
|
def mkdir_component(path, mode=0o777, *, dir_fd=None):
|
|
assert dir_fd is not None
|
|
assert Path(path).name == path
|
|
assert mode == 0o700
|
|
real_mkdir(path, mode, dir_fd=dir_fd)
|
|
created.append(path)
|
|
|
|
monkeypatch.setattr(harness.os, "mkdir", mkdir_component)
|
|
descriptor = harness._open_worker_log_directory(directory)
|
|
try:
|
|
assert stat.S_ISDIR(os.fstat(descriptor).st_mode)
|
|
for path in (directory.parent.parent, directory.parent, directory):
|
|
assert stat.S_IMODE(path.stat().st_mode) == 0o700
|
|
finally:
|
|
os.close(descriptor)
|
|
assert created == ["missing-parent", "missing-child", "private-logs"]
|
|
|
|
|
|
@pytest.mark.parametrize("mode", [0o700, 0o750])
|
|
def test_worker_log_directory_accepts_existing_valid_directory(tmp_path, mode):
|
|
directory = tmp_path / "private-logs"
|
|
directory.mkdir()
|
|
directory.chmod(mode)
|
|
descriptor = harness._open_worker_log_directory(directory)
|
|
try:
|
|
opened = os.fstat(descriptor)
|
|
expected = directory.stat()
|
|
assert (opened.st_dev, opened.st_ino) == (expected.st_dev, expected.st_ino)
|
|
assert stat.S_IMODE(opened.st_mode) == mode
|
|
assert opened.st_uid == os.getuid()
|
|
finally:
|
|
os.close(descriptor)
|
|
|
|
|
|
@pytest.mark.parametrize("setting", ["", ".", "relative/logs", "../logs", "parent/../logs"])
|
|
def test_process_group_rejects_empty_or_relative_log_directory(monkeypatch, tmp_path, setting):
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", setting)
|
|
popen = Mock()
|
|
open_directory = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
monkeypatch.setattr(harness.os, "open", open_directory)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*absolute path"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
open_directory.assert_not_called()
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("suffix", ["../private-logs", "missing/../private-logs", "missing/.."])
|
|
def test_process_group_rejects_parent_components_in_log_directory(monkeypatch, tmp_path, suffix):
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", f"{tmp_path}/{suffix}")
|
|
popen = Mock()
|
|
open_directory = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
monkeypatch.setattr(harness.os, "open", open_directory)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*without '\\.\\.'"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
open_directory.assert_not_called()
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_process_group_rejects_log_directory_owned_by_another_uid(monkeypatch, tmp_path):
|
|
directory = tmp_path / "other-user-logs"
|
|
directory.mkdir(mode=0o700)
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
|
current_uid = directory.stat().st_uid + 1
|
|
monkeypatch.setattr(harness.os, "getuid", lambda: current_uid)
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*owned by the current uid"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert list(directory.iterdir()) == []
|
|
|
|
|
|
def test_process_group_rejects_a_file_as_log_directory(monkeypatch, tmp_path):
|
|
path = tmp_path / "existing-file"
|
|
path.write_bytes(b"preserve this file")
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(path))
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*directory"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert path.read_bytes() == b"preserve this file"
|
|
|
|
|
|
def test_process_group_rejects_log_directory_replaced_during_open(monkeypatch, tmp_path):
|
|
directory = tmp_path / "private-logs"
|
|
directory.mkdir(mode=0o700)
|
|
displaced = tmp_path / "displaced-logs"
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
|
real_open = os.open
|
|
descriptors = []
|
|
|
|
def open_directory(path, flags, mode=0o777, *, dir_fd=None):
|
|
if path == directory.name:
|
|
assert dir_fd is not None
|
|
directory.rename(displaced)
|
|
directory.mkdir(mode=0o700)
|
|
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
|
descriptors.append(descriptor)
|
|
return descriptor
|
|
|
|
monkeypatch.setattr(harness.os, "open", open_directory)
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*changed while opening"):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert list(directory.iterdir()) == list(displaced.iterdir()) == []
|
|
assert len(descriptors) == len(directory.parts)
|
|
for descriptor in descriptors:
|
|
with pytest.raises(OSError) as error:
|
|
os.fstat(descriptor)
|
|
assert error.value.errno == errno.EBADF
|
|
|
|
|
|
def test_process_group_never_overwrites_an_existing_log(monkeypatch, tmp_path):
|
|
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(tmp_path))
|
|
monkeypatch.setattr(harness.uuid, "uuid4", lambda: SimpleNamespace(hex="fixed"))
|
|
existing = tmp_path / "llama-tp-fixed-stderr.log"
|
|
existing.write_bytes(b"preserve this log")
|
|
descriptors = []
|
|
real_open = os.open
|
|
|
|
def open_log(path, flags, mode=0o777, *, dir_fd=None):
|
|
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
|
descriptors.append(descriptor)
|
|
return descriptor
|
|
|
|
monkeypatch.setattr(harness.os, "open", open_log)
|
|
popen = Mock()
|
|
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
|
with pytest.raises(FileExistsError):
|
|
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
|
popen.assert_not_called()
|
|
assert existing.read_bytes() == b"preserve this log"
|
|
assert len(descriptors) == len(tmp_path.parts) + 1
|
|
for descriptor in descriptors:
|
|
with pytest.raises(OSError) as error:
|
|
os.fstat(descriptor)
|
|
assert error.value.errno == errno.EBADF
|