mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
185 lines
7.3 KiB
Python
185 lines
7.3 KiB
Python
"""Simulated unsupported-runtime contracts; real pidfds stay covered by crash tests."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import errno
|
|
import json
|
|
import os
|
|
import signal
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus import mcp_execution as execution
|
|
from obliteratus.mcp_policy import READ_TOOLS, McpPolicy
|
|
from obliteratus.mcp_server import create_server
|
|
from obliteratus.mcp_worker import cancel_guarded
|
|
from obliteratus.run_archive import RunArchive
|
|
from tests import test_mcp_execution as cases
|
|
|
|
execution_case = cases.execution
|
|
|
|
|
|
@pytest.mark.parametrize('source', ['mcp_execution.py', 'mcp_worker.py'])
|
|
def test_execution_changes_select_pidfd_capability_contracts(source):
|
|
from scripts.select_pr_tests import select_tests
|
|
|
|
root = Path(__file__).parents[1]
|
|
selected = select_tests(
|
|
[f'obliteratus/{source}'],
|
|
policy=json.loads((root / 'ci/pr-test-policy.json').read_text()),
|
|
risk_maps=[json.loads((root / 'ci/test-risk-map.json').read_text())],
|
|
project_root=root,
|
|
)
|
|
assert 'tests/test_mcp_pidfd_capability.py' in selected
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def pidfds(monkeypatch):
|
|
opened, signals = [], []
|
|
|
|
def open_pidfd(pid):
|
|
fd = os.open(os.devnull, os.O_RDONLY)
|
|
opened.append((pid, fd))
|
|
return fd
|
|
|
|
def send(fd, number):
|
|
os.fstat(fd)
|
|
signals.append((fd, number))
|
|
|
|
# Explicit fake backend for capability-refusal unit tests, including builds
|
|
# without these attributes. This does not qualify a host for execution.
|
|
monkeypatch.setattr(os, 'pidfd_open', open_pidfd, raising=False)
|
|
monkeypatch.setattr(signal, 'pidfd_send_signal', send, raising=False)
|
|
monkeypatch.setattr(os, 'kill', lambda *_: pytest.fail('PID-only signal fallback'))
|
|
monkeypatch.setattr(os, 'killpg', lambda *_: pytest.fail('PID-only group fallback'))
|
|
return SimpleNamespace(opened=opened, signals=signals)
|
|
|
|
|
|
def test_pidfd_probe_targets_self_with_signal_zero_and_closes_descriptor(pidfds):
|
|
execution.require_pidfd_support()
|
|
assert len(pidfds.opened) == 1
|
|
pid, fd = pidfds.opened[0]
|
|
assert pid == os.getpid()
|
|
assert pidfds.signals == [(fd, 0)]
|
|
with pytest.raises(OSError, match='Bad file descriptor'):
|
|
os.fstat(fd)
|
|
|
|
|
|
@pytest.mark.parametrize('module,name', [(os, 'pidfd_open'), (signal, 'pidfd_send_signal')])
|
|
@pytest.mark.parametrize('replacement', ['missing', None])
|
|
def test_execution_startup_requires_both_callable_bindings(
|
|
tmp_path, monkeypatch, pidfds, module, name, replacement,
|
|
):
|
|
if replacement == 'missing':
|
|
monkeypatch.delattr(module, name)
|
|
else:
|
|
monkeypatch.setattr(module, name, replacement)
|
|
with pytest.raises(ValueError, match='pidfd'):
|
|
execution.ExecutionTools(McpPolicy(archive_root=tmp_path, execution_enabled=True))
|
|
assert not pidfds.opened and not pidfds.signals
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize('module,name', [(os, 'pidfd_open'), (signal, 'pidfd_send_signal')])
|
|
@pytest.mark.parametrize('error', [OSError(errno.ENOSYS, 'private details'),
|
|
PermissionError(errno.EPERM, 'private details'),
|
|
NotImplementedError('private details')])
|
|
def test_unusable_pidfd_syscall_fails_closed_and_closes_any_descriptor(
|
|
monkeypatch, pidfds, module, name, error,
|
|
):
|
|
def fail(*_):
|
|
raise error
|
|
|
|
monkeypatch.setattr(module, name, fail)
|
|
with pytest.raises(ValueError, match='pidfd') as caught:
|
|
execution.require_pidfd_support()
|
|
assert 'private details' not in str(caught.value)
|
|
for _, fd in pidfds.opened:
|
|
with pytest.raises(OSError, match='Bad file descriptor'):
|
|
os.fstat(fd)
|
|
|
|
|
|
def test_missing_pidfds_keep_read_only_server_but_refuse_execution(tmp_path, monkeypatch):
|
|
pytest.importorskip('mcp') # Protocol registration requires the optional SDK.
|
|
monkeypatch.delattr(os, 'pidfd_open')
|
|
monkeypatch.delattr(signal, 'pidfd_send_signal')
|
|
policy = McpPolicy(archive_root=tmp_path)
|
|
server = create_server(policy)
|
|
assert sorted(tool.name for tool in asyncio.run(server.list_tools())) == sorted(READ_TOOLS)
|
|
with pytest.raises(ValueError, match='pidfd'):
|
|
create_server(replace(policy, execution_enabled=True))
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_capability_loss_before_plan_creates_no_token_or_archive(execution_case, monkeypatch):
|
|
tools, _, calls = execution_case
|
|
monkeypatch.delattr(signal, 'pidfd_send_signal')
|
|
with pytest.raises(ValueError, match='pidfd'):
|
|
cases.plan(execution_case)
|
|
assert not tools._plans and not calls
|
|
assert list(tools.policy.archive_root.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize('direct', [False, True])
|
|
def test_capability_loss_before_launch_refuses_before_archive_mutation(
|
|
execution_case, monkeypatch, direct,
|
|
):
|
|
tools, _, calls = execution_case
|
|
planned = cases.plan(execution_case)
|
|
spec = tools._plans[planned.token][1]
|
|
monkeypatch.delattr(os, 'pidfd_open')
|
|
with pytest.raises(ValueError, match='pidfd'):
|
|
if direct:
|
|
RunArchive(tools.policy.archive_root, read_only=True).launch(
|
|
execution.build_arguments(spec), guard=spec,
|
|
popen=lambda *_a, **_kw: pytest.fail('unsupported worker launched'),
|
|
)
|
|
else:
|
|
tools.launch_run(planned.token)
|
|
if not direct:
|
|
with pytest.raises(ValueError, match='token'):
|
|
tools.launch_run(planned.token)
|
|
assert not calls
|
|
assert list(tools.policy.archive_root.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize('module,name', [(os, 'pidfd_open'), (signal, 'pidfd_send_signal')])
|
|
@pytest.mark.parametrize('failure', ['missing', 'denied'])
|
|
def test_capability_loss_before_cancel_preserves_manifest_and_events(
|
|
execution_case, monkeypatch, module, name, failure,
|
|
):
|
|
tools, _, _ = execution_case
|
|
planned = cases.plan(execution_case)
|
|
tools.launch_run(planned.token)
|
|
directory = tools.policy.archive_root / planned.run_id
|
|
before = {name: (directory / name).read_bytes() for name in ('manifest.json', 'events.jsonl')}
|
|
if failure == 'missing':
|
|
monkeypatch.delattr(module, name)
|
|
else:
|
|
def deny(*_):
|
|
raise PermissionError(errno.EPERM, 'denied')
|
|
monkeypatch.setattr(module, name, deny)
|
|
with pytest.raises(ValueError, match='pidfd'):
|
|
tools.cancel_run(planned.run_id)
|
|
assert {name: (directory / name).read_bytes() for name in before} == before
|
|
|
|
|
|
def test_terminal_cancel_needs_no_pidfd_and_does_not_write(execution_case, monkeypatch, pidfds):
|
|
tools, _, _ = execution_case
|
|
planned = cases.plan(execution_case)
|
|
tools.launch_run(planned.token)
|
|
# Existing recovery commits cancellation when its authenticated worker died.
|
|
tools.cancel_run(planned.run_id)
|
|
monkeypatch.setattr('obliteratus.mcp_worker.worker_matches', lambda *_: False)
|
|
assert tools.get_run_status(planned.run_id).status == 'cancelled'
|
|
manifest = cases.manifest(execution_case, planned.run_id)
|
|
before = manifest.read_bytes()
|
|
pidfds.opened.clear()
|
|
monkeypatch.delattr(os, 'pidfd_open')
|
|
monkeypatch.delattr(signal, 'pidfd_send_signal')
|
|
assert cancel_guarded(tools.policy.archive_root, planned.run_id)['status'] == 'cancelled'
|
|
assert manifest.read_bytes() == before and not pidfds.opened
|