-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_support.py
More file actions
62 lines (47 loc) · 1.74 KB
/
Copy pathtest_support.py
File metadata and controls
62 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations
import socket
import threading
from pathlib import Path
from typing import Any
import pytest
def can_bind_loopback() -> bool:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('127.0.0.1', 0))
except PermissionError:
return False
finally:
sock.close()
return True
def can_start_threads(count: int = 1) -> bool:
threads: list[threading.Thread] = []
release = threading.Event()
try:
for _ in range(count):
thread = threading.Thread(target=release.wait)
thread.start()
threads.append(thread)
except RuntimeError as exc:
if "can't start new thread" in str(exc):
return False
raise
finally:
release.set()
for thread in threads:
thread.join(timeout=1.0)
return True
def skip_if_socket_bind_is_blocked() -> None:
"""Skip tests that require a loopback TCP listener when the environment forbids it."""
if not can_bind_loopback():
pytest.skip('sandbox forbids socket.bind() on loopback')
def skip_if_thread_start_is_blocked(count: int = 1) -> None:
"""Skip tests that require spawning worker threads when the environment forbids it."""
if not can_start_threads(count):
pytest.skip('environment has thread resource limits')
def mcp_test_governance(root: str | Path, permission_mode: str = 'prompt') -> Any:
"""Build a governed MCP server context (approval policy + run log) under ``root``."""
from teaagent.mcp_server import MCPGovernance
from teaagent.policy import PermissionMode
return MCPGovernance.for_workspace(
root, permission_mode=PermissionMode(permission_mode), transport='test'
)