Skip to content

Commit 3b57b15

Browse files
authored
test(e2e): port the SDK E2E suite from core
Ports the four core e2e/python files whose subject is the SDK, from v0.1.7: sandbox_api, workspace_api, sandbox_exec_python and exec_admission. They keep their calls to client._stub, because users depend on it too. The conftest keeps only the fixtures these tests use and skips every test when OPENSHELL_GATEWAY isn't set. tests/e2e/Dockerfile.workload is a copy of core's sandbox image with the client's Python and cloudpickle, which exec_python needs. mise run e2e:image builds it, and conformance:check now also fails when it drifts from core at the pin. mise run test ignores tests/e2e; mise run e2e runs it against the gateway named in OPENSHELL_GATEWAY. Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
1 parent f8d3b3f commit 3b57b15

7 files changed

Lines changed: 889 additions & 3 deletions

File tree

‎mise.toml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ fi
6565
"""
6666

6767
[tasks."conformance:check"]
68-
description = "Fail if vendored fixtures differ from core at the pinned ref"
68+
description = "Fail if vendored fixtures or the E2E workload Dockerfile differ from core at the pinned ref"
6969
run = """
7070
#!/usr/bin/env bash
7171
set -euo pipefail
7272
core=$(mktemp -d)
7373
trap 'rm -rf "$core"' EXIT
74-
scripts/fetch-core.sh "$OPENSHELL_CORE_REF" "$core" sdk/conformance
74+
scripts/fetch-core.sh "$OPENSHELL_CORE_REF" "$core" sdk/conformance e2e/python/Dockerfile.workload
7575
for fixture in oauth-client-credentials.json oauth-client-credentials.json.license; do
7676
diff -u "$core/sdk/conformance/$fixture" "tests/fixtures/$fixture"
7777
done
78+
diff -u "$core/e2e/python/Dockerfile.workload" tests/e2e/Dockerfile.workload
7879
"""
7980

8081
[tasks.lint]
@@ -100,7 +101,18 @@ run = "uv run --frozen ty check {{vars.python_paths}}"
100101
[tasks.test]
101102
description = "Run unit tests (set UV_PYTHON to pick the interpreter)"
102103
depends = ["sync", "proto:fetch"]
103-
run = "uv run --frozen pytest -n auto"
104+
run = "uv run --frozen pytest -n auto --ignore=tests/e2e"
105+
106+
[tasks."e2e:image"]
107+
description = "Build the E2E sandbox image: Python from .python-version plus cloudpickle, for exec_python"
108+
run = """
109+
docker build --file tests/e2e/Dockerfile.workload --build-arg "PYTHON_VERSION=$(cat .python-version)" --tag openshell/e2e-python:dev tests/e2e
110+
"""
111+
112+
[tasks.e2e]
113+
description = "Run the E2E suite against the gateway named in OPENSHELL_GATEWAY (see scripts/e2e/start-gateway.sh)"
114+
depends = ["sync"]
115+
run = "uv run --frozen pytest tests/e2e -n ${E2E_PARALLEL:-5}"
104116

105117
[tasks.check]
106118
description = "Run formatting, lint, type, and drift checks"

‎tests/e2e/Dockerfile.workload‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
ARG WORKLOAD_BASE=nvcr.io/nvidia/base/ubuntu:24.04
5+
FROM ghcr.io/astral-sh/uv:0.12.17@sha256:10787c682e4184e4f290de1171fd4703dc63de99221f10fe1c99002ce7fa9acc AS uv
6+
FROM ${WORKLOAD_BASE}
7+
8+
COPY --from=uv /uv /uvx /usr/local/bin/
9+
# Supplied from .python-version by the build task for cloudpickle compatibility.
10+
ARG PYTHON_VERSION
11+
# curl and openssl also support the Rust Docker tests sharing this fixture.
12+
RUN apt-get update \
13+
&& apt-get install -y --no-install-recommends \
14+
bash ca-certificates curl git openssl python3 python3-pip python3-venv python-is-python3 \
15+
&& rm -rf /var/lib/apt/lists/* \
16+
&& if getent passwd ubuntu >/dev/null; then userdel ubuntu; fi \
17+
&& if getent group ubuntu >/dev/null; then groupdel ubuntu; fi \
18+
&& groupadd --gid 1000 sandbox \
19+
&& useradd --uid 1000 --gid sandbox --create-home --shell /bin/bash sandbox \
20+
&& UV_PYTHON_INSTALL_DIR=/sandbox/.uv/python uv python install "${PYTHON_VERSION}" \
21+
&& UV_PYTHON_INSTALL_DIR=/sandbox/.uv/python uv venv --python "${PYTHON_VERSION}" --seed /sandbox/.venv \
22+
&& uv pip install --python /sandbox/.venv/bin/python cloudpickle==3.1.2 google-auth==2.40.3 requests==2.32.5 \
23+
&& chown -R sandbox:sandbox /sandbox \
24+
&& uv cache clean
25+
26+
ENV VIRTUAL_ENV=/sandbox/.venv
27+
ENV PATH="/sandbox/.venv/bin:${PATH}"
28+
WORKDIR /sandbox
29+
USER sandbox:sandbox

‎tests/e2e/conftest.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Fixtures for the E2E suite.
5+
6+
The suite runs against the gateway named in ``OPENSHELL_GATEWAY``, for example
7+
the one ``scripts/e2e/start-gateway.sh`` registers as ``e2e``. Without it,
8+
every test is skipped. See ``mise run e2e``.
9+
"""
10+
11+
from __future__ import annotations
12+
13+
import os
14+
import time
15+
from typing import TYPE_CHECKING
16+
17+
import grpc
18+
import pytest
19+
20+
from openshell import Sandbox, SandboxClient, WorkspaceClient
21+
22+
if TYPE_CHECKING:
23+
from collections.abc import Callable, Iterator
24+
25+
from openshell._proto import openshell_pb2
26+
27+
28+
@pytest.fixture(scope="session")
29+
def cluster_name() -> str:
30+
name = os.environ.get("OPENSHELL_GATEWAY")
31+
if not name:
32+
pytest.skip("OPENSHELL_GATEWAY not set")
33+
return name
34+
35+
36+
@pytest.fixture(scope="session")
37+
def sandbox_client(cluster_name: str) -> Iterator[SandboxClient]:
38+
with SandboxClient.from_active_cluster(cluster=cluster_name) as client:
39+
yield client
40+
41+
42+
@pytest.fixture(scope="session", autouse=True)
43+
def ensure_sandbox_persistence_ready(sandbox_client: SandboxClient) -> None:
44+
for _ in range(60):
45+
try:
46+
sandbox_client.list_ids(workspace="default", page_size=1)
47+
return
48+
except grpc.RpcError as exc:
49+
details = exc.details() or ""
50+
if exc.code() == grpc.StatusCode.UNAVAILABLE:
51+
time.sleep(2)
52+
continue
53+
if (
54+
exc.code() == grpc.StatusCode.INTERNAL
55+
and "no such table: objects" in details
56+
):
57+
time.sleep(1)
58+
continue
59+
raise
60+
61+
pytest.fail(
62+
"openshell-server persistence is not initialized (missing sqlite objects table); "
63+
"restart the gateway and rerun the E2E suite"
64+
)
65+
66+
67+
@pytest.fixture
68+
def sandbox(cluster_name: str) -> Callable[..., Sandbox]:
69+
def _create(
70+
*, spec: openshell_pb2.SandboxSpec | None = None, delete_on_exit: bool = True
71+
) -> Sandbox:
72+
return Sandbox(
73+
workspace="default",
74+
cluster=cluster_name,
75+
spec=spec,
76+
delete_on_exit=delete_on_exit,
77+
# Allow time to pull an explicitly supplied workload fixture.
78+
ready_timeout_seconds=300.0,
79+
)
80+
81+
return _create
82+
83+
84+
@pytest.fixture(scope="session")
85+
def workspace_client(sandbox_client: SandboxClient) -> WorkspaceClient:
86+
return WorkspaceClient.from_sandbox_client(sandbox_client)

‎tests/e2e/exec_admission_test.py‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
import threading
7+
import time
8+
import uuid
9+
from typing import TYPE_CHECKING
10+
11+
import grpc
12+
import pytest
13+
from google.protobuf import duration_pb2
14+
15+
from openshell._proto import datamodel_pb2, openshell_pb2
16+
from openshell.errors import from_grpc_error
17+
18+
if TYPE_CHECKING:
19+
from collections.abc import Callable
20+
21+
from openshell import Sandbox, SandboxClient
22+
23+
24+
def assert_blocked(stream, reason: str) -> None:
25+
with pytest.raises(grpc.RpcError) as caught:
26+
list(stream)
27+
error = from_grpc_error(caught.value)
28+
assert error.code() == grpc.StatusCode.FAILED_PRECONDITION
29+
assert error.error_info is not None
30+
assert error.error_info.reason == reason
31+
32+
33+
@pytest.mark.parametrize("interactive", [False, True])
34+
def test_exec_request_id_never_relaunches_or_replays_output(
35+
sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient, interactive: bool
36+
) -> None:
37+
with sandbox(delete_on_exit=True) as sb:
38+
path = f"/sandbox/exec-admission-{uuid.uuid4().hex}"
39+
request = openshell_pb2.ExecSandboxRequest(
40+
sandbox=sb.sandbox.name,
41+
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
42+
command=[
43+
"/bin/sh",
44+
"-c",
45+
f"printf x >> {path}; printf started; "
46+
f"while [ ! -e {path}.release ]; do sleep 0.1; done; "
47+
"printf finished; exit 7",
48+
],
49+
request_id=str(uuid.uuid4()),
50+
)
51+
done = threading.Event()
52+
53+
def invoke(value):
54+
if not interactive:
55+
return sandbox_client._stub.ExecSandbox(value, timeout=30)
56+
57+
def inputs():
58+
yield openshell_pb2.ExecSandboxInput(start=value)
59+
done.wait(timeout=30)
60+
61+
return sandbox_client._stub.ExecSandboxInteractive(inputs(), timeout=30)
62+
63+
stream = invoke(request)
64+
try:
65+
first = next(stream)
66+
assert first.WhichOneof("payload") == "stdout"
67+
assert b"started" in first.stdout.data
68+
assert_blocked(invoke(request), "REQUEST_OUTCOME_UNCERTAIN")
69+
changed = openshell_pb2.ExecSandboxRequest()
70+
changed.CopyFrom(request)
71+
changed.command.append("changed-payload")
72+
assert_blocked(invoke(changed), "REQUEST_ID_PAYLOAD_MISMATCH")
73+
assert sb.exec(["touch", f"{path}.release"]).exit_code == 0
74+
events = list(stream)
75+
assert events[-1].WhichOneof("payload") == "exit"
76+
assert events[-1].exit.exit_code == 7
77+
assert_blocked(invoke(request), "REQUEST_STREAM_UNAVAILABLE")
78+
result = sb.exec(["cat", path])
79+
assert result.exit_code == 0
80+
assert result.stdout == "x"
81+
finally:
82+
done.set()
83+
stream.cancel()
84+
85+
86+
@pytest.mark.parametrize("interactive", [False, True])
87+
def test_exec_request_timeout_keeps_launch_unresolved(
88+
sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient, interactive: bool
89+
) -> None:
90+
with sandbox(delete_on_exit=True) as sb:
91+
path = f"/sandbox/exec-timeout-{uuid.uuid4().hex}"
92+
request = openshell_pb2.ExecSandboxRequest(
93+
sandbox=sb.sandbox.name,
94+
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
95+
command=["/bin/sh", "-c", f"printf x >> {path}; sleep 5"],
96+
execution_timeout=duration_pb2.Duration(seconds=1),
97+
request_id=str(uuid.uuid4()),
98+
)
99+
done = threading.Event()
100+
101+
def invoke():
102+
if not interactive:
103+
return sandbox_client._stub.ExecSandbox(request, timeout=30)
104+
105+
def inputs():
106+
yield openshell_pb2.ExecSandboxInput(start=request)
107+
done.wait(timeout=30)
108+
109+
return sandbox_client._stub.ExecSandboxInteractive(inputs(), timeout=30)
110+
111+
stream = invoke()
112+
try:
113+
events = []
114+
for event in stream:
115+
events.append(event)
116+
if event.WhichOneof("payload") == "exit":
117+
# A synthetic timeout leaves the interactive input task
118+
# alive until the client closes its side of the stream.
119+
done.set()
120+
assert events[-1].exit.exit_code == 124
121+
assert_blocked(invoke(), "REQUEST_OUTCOME_UNCERTAIN")
122+
assert sb.exec(["cat", path]).stdout == "x"
123+
finally:
124+
done.set()
125+
stream.cancel()
126+
127+
128+
def test_exec_client_cancellation_does_not_clear_launch_admission(
129+
sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient
130+
) -> None:
131+
with sandbox(delete_on_exit=True) as sb:
132+
path = f"/sandbox/exec-cancel-{uuid.uuid4().hex}"
133+
request = openshell_pb2.ExecSandboxRequest(
134+
sandbox=sb.sandbox.name,
135+
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
136+
command=[
137+
"/bin/sh",
138+
"-c",
139+
f"printf x >> {path}; printf started; sleep 1; printf finished",
140+
],
141+
request_id=str(uuid.uuid4()),
142+
)
143+
stream = sandbox_client._stub.ExecSandbox(request, timeout=30)
144+
assert b"started" in next(stream).stdout.data
145+
stream.cancel()
146+
deadline = time.monotonic() + 15
147+
while True:
148+
with pytest.raises(grpc.RpcError) as caught:
149+
list(sandbox_client._stub.ExecSandbox(request, timeout=30))
150+
error = from_grpc_error(caught.value)
151+
assert error.error_info is not None
152+
if error.error_info.reason == "REQUEST_STREAM_UNAVAILABLE":
153+
break
154+
assert error.error_info.reason == "REQUEST_OUTCOME_UNCERTAIN"
155+
assert time.monotonic() < deadline
156+
time.sleep(0.1)
157+
assert sb.exec(["cat", path]).stdout == "x"

0 commit comments

Comments
 (0)