|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Cleanup status handling through the curated client's real gRPC interceptor.""" |
| 5 | + |
| 6 | +from concurrent import futures |
| 7 | +from contextlib import nullcontext |
| 8 | +from types import SimpleNamespace |
| 9 | + |
| 10 | +import grpc |
| 11 | +import pytest |
| 12 | + |
| 13 | +from openshell._proto import openshell_pb2 |
| 14 | +from openshell.errors import GatewayError |
| 15 | +from openshell.sandbox import Sandbox, SandboxClient |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def cleanup_client(): |
| 20 | + state = SimpleNamespace( |
| 21 | + exists=False, |
| 22 | + code=grpc.StatusCode.NOT_FOUND, |
| 23 | + calls=[], |
| 24 | + closed=[], |
| 25 | + ) |
| 26 | + |
| 27 | + def fail(context): |
| 28 | + context.set_trailing_metadata((("request-id", "cleanup-regression"),)) |
| 29 | + context.abort(state.code, "cleanup status") |
| 30 | + |
| 31 | + def get(request, context): |
| 32 | + state.calls.append("GetSandbox") |
| 33 | + assert request.name == "cleanup-test" |
| 34 | + assert request.workspace_scope.workspace == "default" |
| 35 | + if not state.exists: |
| 36 | + fail(context) |
| 37 | + response = openshell_pb2.SandboxResponse() |
| 38 | + response.sandbox.metadata.id = "sandbox-1" |
| 39 | + response.sandbox.metadata.name = request.name |
| 40 | + response.sandbox.metadata.workspace = "default" |
| 41 | + response.sandbox.status.phase = openshell_pb2.SANDBOX_PHASE_READY |
| 42 | + return response |
| 43 | + |
| 44 | + def delete(request, context): |
| 45 | + state.calls.append("DeleteSandbox") |
| 46 | + assert request.name == "cleanup-test" |
| 47 | + assert request.workspace_scope.workspace == "default" |
| 48 | + fail(context) |
| 49 | + |
| 50 | + server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) |
| 51 | + server.add_generic_rpc_handlers( |
| 52 | + ( |
| 53 | + grpc.method_handlers_generic_handler( |
| 54 | + "openshell.v1.OpenShell", |
| 55 | + { |
| 56 | + "GetSandbox": grpc.unary_unary_rpc_method_handler( |
| 57 | + get, |
| 58 | + request_deserializer=openshell_pb2.GetSandboxRequest.FromString, |
| 59 | + response_serializer=openshell_pb2.SandboxResponse.SerializeToString, |
| 60 | + ), |
| 61 | + "DeleteSandbox": grpc.unary_unary_rpc_method_handler( |
| 62 | + delete, |
| 63 | + request_deserializer=openshell_pb2.DeleteSandboxRequest.FromString, |
| 64 | + response_serializer=openshell_pb2.DeleteSandboxResponse.SerializeToString, |
| 65 | + ), |
| 66 | + }, |
| 67 | + ), |
| 68 | + ) |
| 69 | + ) |
| 70 | + port = server.add_insecure_port("127.0.0.1:0") |
| 71 | + server.start() |
| 72 | + client = SandboxClient( |
| 73 | + f"127.0.0.1:{port}", |
| 74 | + timeout=5, |
| 75 | + _bearer_close=lambda: state.closed.append(True), |
| 76 | + ) |
| 77 | + try: |
| 78 | + yield client, state |
| 79 | + finally: |
| 80 | + client.close() |
| 81 | + server.stop(0).wait() |
| 82 | + |
| 83 | + |
| 84 | +def assert_original_call(error, code): |
| 85 | + assert isinstance(error, GatewayError) |
| 86 | + assert isinstance(error.raw_error, grpc.Call) |
| 87 | + assert error.code() == error.raw_error.code() == code |
| 88 | + assert error.details() == "cleanup status" |
| 89 | + assert ("request-id", "cleanup-regression") in error.trailing_metadata() |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize( |
| 93 | + "code", |
| 94 | + [ |
| 95 | + grpc.StatusCode.NOT_FOUND, |
| 96 | + grpc.StatusCode.PERMISSION_DENIED, |
| 97 | + grpc.StatusCode.UNAVAILABLE, |
| 98 | + ], |
| 99 | +) |
| 100 | +def test_wait_deleted_handles_intercepted_status(cleanup_client, code): |
| 101 | + client, state = cleanup_client |
| 102 | + state.code = code |
| 103 | + # Prove this is the curated intercepted stub, not a fake raw exception. |
| 104 | + with pytest.raises(GatewayError) as observed: |
| 105 | + client.get("cleanup-test", workspace="default") |
| 106 | + assert_original_call(observed.value, code) |
| 107 | + |
| 108 | + expected = ( |
| 109 | + nullcontext() |
| 110 | + if code == grpc.StatusCode.NOT_FOUND |
| 111 | + else pytest.raises(GatewayError) |
| 112 | + ) |
| 113 | + with expected as caught: |
| 114 | + client.wait_deleted("cleanup-test", workspace="default", timeout_seconds=5) |
| 115 | + if caught is not None: |
| 116 | + assert_original_call(caught.value, code) |
| 117 | + assert state.calls == ["GetSandbox", "GetSandbox"] |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize( |
| 121 | + "code", |
| 122 | + [ |
| 123 | + grpc.StatusCode.NOT_FOUND, |
| 124 | + grpc.StatusCode.PERMISSION_DENIED, |
| 125 | + grpc.StatusCode.UNAVAILABLE, |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_context_cleanup_handles_intercepted_status(cleanup_client, monkeypatch, code): |
| 129 | + client, state = cleanup_client |
| 130 | + state.code = code |
| 131 | + state.exists = True |
| 132 | + monkeypatch.setattr( |
| 133 | + SandboxClient, |
| 134 | + "from_active_cluster", |
| 135 | + classmethod(lambda _cls, **_kwargs: client), |
| 136 | + ) |
| 137 | + |
| 138 | + managed = Sandbox(workspace="default", sandbox="cleanup-test") |
| 139 | + expected = ( |
| 140 | + nullcontext() |
| 141 | + if code == grpc.StatusCode.NOT_FOUND |
| 142 | + else pytest.raises(GatewayError) |
| 143 | + ) |
| 144 | + with expected as caught, managed: |
| 145 | + state.exists = False |
| 146 | + if caught is not None: |
| 147 | + assert_original_call(caught.value, code) |
| 148 | + assert state.calls == ["GetSandbox", "GetSandbox", "DeleteSandbox"] |
| 149 | + assert state.closed == [True] |
| 150 | + assert managed._client is None |
| 151 | + assert managed._session is None |
0 commit comments