Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
132 lines (104 loc) · 4.44 KB
/
Copy pathconftest.py
File metadata and controls
132 lines (104 loc) · 4.44 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import fcntl
import os
import time
from typing import TYPE_CHECKING
import grpc
import pytest
from openshell import Sandbox, SandboxClient, WorkspaceClient
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line(
"markers",
"exclusive_gateway_config: hold an exclusive lock on gateway-global "
"config so no other xdist worker observes a transient global setting",
)
@pytest.fixture(autouse=True)
def _gateway_config_guard(
request: pytest.FixtureRequest,
tmp_path_factory: pytest.TempPathFactory,
) -> Iterator[None]:
"""Readers-writer guard over gateway-global config mutations.
The e2e gateway is shared across xdist workers, so a test that flips a
gateway-global setting can leak that transient value into another worker's
sandbox creation. Every test holds a shared lock by default; a test marked
``exclusive_gateway_config`` holds an exclusive lock, so while it mutates and
restores the setting no other worker is mid-test and none can observe the
transient value. The lock file lives in the run's shared base temp dir
(``getbasetemp().parent``), which is common to all xdist workers.
"""
lock_path = tmp_path_factory.getbasetemp().parent / "gateway-config.lock"
exclusive = (
request.node.get_closest_marker("exclusive_gateway_config") is not None
)
with lock_path.open("w") as lock_file:
fcntl.flock(lock_file, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
yield
@pytest.fixture(scope="session")
def cluster_name() -> str | None:
return os.environ.get("OPENSHELL_GATEWAY")
@pytest.fixture(scope="session")
def sandbox_client(cluster_name: str | None) -> Iterator[SandboxClient]:
# Direct deletes can wait out the Kubernetes pod grace period.
with SandboxClient.from_active_cluster(
cluster=cluster_name, timeout=120.0
) as client:
yield client
@pytest.fixture(scope="session", autouse=True)
def ensure_sandbox_persistence_ready(sandbox_client: SandboxClient) -> None:
for _ in range(60):
try:
sandbox_client.list_ids(workspace="default", page_size=1)
return
except grpc.RpcError as exc:
details = exc.details() or ""
if exc.code() == grpc.StatusCode.UNAVAILABLE:
time.sleep(2)
continue
if (
exc.code() == grpc.StatusCode.INTERNAL
and "no such table: objects" in details
):
time.sleep(1)
continue
raise
pytest.fail(
"openshell-server persistence is not initialized (missing sqlite objects table); "
"redeploy the active cluster and rerun e2e sandbox tests"
)
@pytest.fixture
def sandbox(cluster_name: str | None) -> Callable[..., Sandbox]:
def _create(*, spec: object | None = None, delete_on_exit: bool = True) -> Sandbox:
return Sandbox(
workspace="default",
cluster=cluster_name,
spec=spec,
delete_on_exit=delete_on_exit,
# Allow time to pull an explicitly supplied workload fixture.
ready_timeout_seconds=300.0,
# Kubernetes holds DeleteSandbox open until the pod terminates
# (30s grace plus API headroom), which can exceed the 30s default.
delete_timeout_seconds=120.0,
)
return _create
@pytest.fixture(scope="session")
def workspace_client(sandbox_client: SandboxClient) -> WorkspaceClient:
return WorkspaceClient.from_sandbox_client(sandbox_client)
@pytest.fixture(scope="session")
def _worker_suffix(worker_id: str) -> str:
"""Return a suffix for worker-unique resource names.
Uses the built-in ``worker_id`` fixture from pytest-xdist which returns
``"gw0"``, ``"gw1"``, etc. for workers, or ``"master"`` for non-xdist runs.
"""
if worker_id == "master":
return ""
return f"-{worker_id}"
@pytest.fixture
def run_python() -> Callable[[Sandbox, str], tuple[int, str, str]]:
def _run(sandbox: Sandbox, code: str) -> tuple[int, str, str]:
result = sandbox.exec(["python", "-c", code], timeout_seconds=20)
return result.exit_code, result.stdout, result.stderr
return _run