Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

32.4 KB
Raw Blame
# Copyright 2025 The OpenSandbox Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import threading
from unittest.mock import MagicMock, patch

import pytest
from kubernetes.client import ApiClient, ApiException, CoreV1Api

from opensandbox_server.config import KubernetesRuntimeConfig
from opensandbox_server.services.k8s.client import K8sClient
from opensandbox_server.services.k8s.informer import WorkloadInformer

class TestK8sClient:
    
    def test_init_with_kubeconfig_loads_successfully(self, k8s_runtime_config):
        with patch('kubernetes.config.load_kube_config') as mock_load:
            client = K8sClient(k8s_runtime_config)

            assert client.config == k8s_runtime_config
            mock_load.assert_called_once_with(
                config_file=k8s_runtime_config.kubeconfig_path
            )

    def test_init_with_incluster_config_loads_successfully(self):
        config = KubernetesRuntimeConfig(
            kubeconfig_path=None,
            namespace="test-ns"
        )

        with patch('kubernetes.config.load_incluster_config') as mock_load:
            client = K8sClient(config)

            assert client.config == config
            mock_load.assert_called_once()

    def test_insecure_skip_tls_verify_disables_certificate_validation(self):
        """Verify optional TLS verification bypass is applied to the default config."""
        runtime_config = KubernetesRuntimeConfig(
            kubeconfig_path=None,
            insecure_skip_tls_verify=True,
        )
        default_cfg = MagicMock()

        with patch('kubernetes.config.load_incluster_config'), \
             patch('kubernetes.client.Configuration.get_default_copy', return_value=default_cfg), \
             patch('kubernetes.client.Configuration.set_default') as mock_set_default:
            K8sClient(runtime_config)

            assert default_cfg.verify_ssl is False
            mock_set_default.assert_called_once_with(default_cfg)

    def test_init_with_invalid_kubeconfig_raises_exception(self):
        config = KubernetesRuntimeConfig(
            kubeconfig_path="/invalid/path",
            namespace="test-ns"
        )

        with patch('kubernetes.config.load_kube_config') as mock_load:
            mock_load.side_effect = Exception("Config file not found")

            with pytest.raises(Exception) as exc_info:
                K8sClient(config)

            assert "Failed to load Kubernetes configuration" in str(exc_info.value)

    def test_get_core_v1_api_returns_singleton(self, k8s_runtime_config):
        with patch('kubernetes.config.load_kube_config'), \
             patch('kubernetes.client.CoreV1Api') as mock_api_class:

            mock_api_instance = MagicMock()
            mock_api_class.return_value = mock_api_instance

            client = K8sClient(k8s_runtime_config)

            api1 = client.get_core_v1_api()
            api2 = client.get_core_v1_api()

            assert api1 is api2
            assert mock_api_class.call_count == 1

    def test_get_custom_objects_api_returns_singleton(self, k8s_runtime_config):
        with patch('kubernetes.config.load_kube_config'), \
             patch('kubernetes.client.CustomObjectsApi') as mock_api_class:

            mock_api_instance = MagicMock()
            mock_api_class.return_value = mock_api_instance

            client = K8sClient(k8s_runtime_config)

            api1 = client.get_custom_objects_api()
            api2 = client.get_custom_objects_api()

            assert api1 is api2
            assert mock_api_class.call_count == 1
    
    def test_get_core_v1_api_creates_on_first_call(self, k8s_runtime_config):
        """Verify API client is created on first call, not at init time."""
        with patch('kubernetes.config.load_kube_config'), \
             patch('kubernetes.client.CoreV1Api') as mock_api_class:

            client = K8sClient(k8s_runtime_config)

            assert mock_api_class.call_count == 0
            client.get_core_v1_api()
            assert mock_api_class.call_count == 1

    def test_no_rate_limiters_when_qps_is_zero(self, k8s_runtime_config):
        with patch('kubernetes.config.load_kube_config'):
            client = K8sClient(k8s_runtime_config)
            assert client._read_limiter is None
            assert client._write_limiter is None

    def test_read_limiter_created_when_read_qps_set(self):
        config = KubernetesRuntimeConfig(read_qps=10.0, read_burst=20)
        with patch('kubernetes.config.load_incluster_config'):
            client = K8sClient(config)
            assert client._read_limiter is not None
            assert client._write_limiter is None

    def test_write_limiter_created_when_write_qps_set(self):
        config = KubernetesRuntimeConfig(write_qps=5.0, write_burst=10)
        with patch('kubernetes.config.load_incluster_config'):
            client = K8sClient(config)
            assert client._read_limiter is None
            assert client._write_limiter is not None

    def _make_client(self, k8s_runtime_config):
        """Return a K8sClient with mocked kubeconfig and raw API handles."""
        with patch('kubernetes.config.load_kube_config'):
            c = K8sClient(k8s_runtime_config)
        c._custom_objects_api = MagicMock()
        c._core_v1_api = MagicMock()
        c._node_v1_api = MagicMock()
        return c

    def _attach_informer(self, c, informer):
        c._informers[("g", "v1", "foos", "ns")] = informer
        c.config = MagicMock(
            informer_resync_seconds=300,
            informer_watch_timeout_seconds=60,
            read_qps=0.0,
            write_qps=0.0,
        )
        return informer

    def _attach_real_informer(self, c, items, cursor="cursor"):
        informer = WorkloadInformer(
            list_fn=MagicMock(
                return_value={"metadata": {"resourceVersion": cursor}, "items": items}
            ),
            enable_watch=False,
        )
        assert informer._full_resync() is True
        return self._attach_informer(c, informer)

    def test_create_custom_object_delegates_to_api(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        body = {"metadata": {"name": "foo"}}
        c.create_custom_object("g", "v1", "ns", "foos", body)
        c._custom_objects_api.create_namespaced_custom_object.assert_called_once_with(
            group="g", version="v1", namespace="ns", plural="foos", body=body
        )

    def test_create_custom_object_invalidates_informer(self, k8s_runtime_config):
        """A successful create invalidates without writing its direct response."""
        c = self._make_client(k8s_runtime_config)
        created = {"metadata": {"name": "foo-1", "resourceVersion": "11"}}
        c._custom_objects_api.create_namespaced_custom_object.return_value = created
        fake_informer = self._attach_informer(c, MagicMock())
        result = c.create_custom_object("g", "v1", "ns", "foos", {"metadata": {"name": "foo-1"}})
        assert result == created
        fake_informer.invalidate.assert_called_once_with()

    def test_patch_custom_object_invalidates_informer(self, k8s_runtime_config):
        """A successful patch invalidates without writing its direct response."""
        c = self._make_client(k8s_runtime_config)
        patched = {"metadata": {"name": "foo-1", "resourceVersion": "12"}}
        c._custom_objects_api.patch_namespaced_custom_object.return_value = patched
        fake_informer = self._attach_informer(c, MagicMock())
        result = c.patch_custom_object("g", "v1", "ns", "foos", "foo-1", {"spec": {"x": 1}})
        assert result == patched
        fake_informer.invalidate.assert_called_once_with()

    @pytest.mark.parametrize(
        "response",
        [
            {"metadata": {"name": "foo-1", "resourceVersion": "13"}},
            {"kind": "Status", "status": "Success"},
            None,
        ],
    )
    def test_delete_custom_object_invalidates_for_any_response(
        self, k8s_runtime_config, response
    ):
        """Delete response shape never becomes cache state."""
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.delete_namespaced_custom_object.return_value = response
        fake_informer = self._attach_informer(c, MagicMock())
        c.delete_custom_object("g", "v1", "ns", "foos", "foo-1")
        fake_informer.invalidate.assert_called_once_with()

    def test_write_paths_skip_cache_when_no_informer(self, k8s_runtime_config):
        """Write paths must not crash when no informer has been started yet."""
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.create_namespaced_custom_object.return_value = {"metadata": {"name": "x"}}
        c._custom_objects_api.patch_namespaced_custom_object.return_value = {"metadata": {"name": "x"}}
        c.config = MagicMock(read_qps=0.0, write_qps=0.0)
        # No informers registered → _lookup_informer returns None
        c.create_custom_object("g", "v1", "ns", "foos", {"metadata": {"name": "x"}})
        c.patch_custom_object("g", "v1", "ns", "foos", "x", {})
        c.delete_custom_object("g", "v1", "ns", "foos", "x")
        assert c._informers == {}

    @pytest.mark.parametrize("operation", ["create", "patch", "delete"])
    def test_failed_write_does_not_invalidate(self, k8s_runtime_config, operation):
        """Only mutations confirmed successful invalidate the informer."""
        c = self._make_client(k8s_runtime_config)
        fake_informer = self._attach_informer(c, MagicMock())
        api_method = getattr(
            c._custom_objects_api,
            {
                "create": "create_namespaced_custom_object",
                "patch": "patch_namespaced_custom_object",
                "delete": "delete_namespaced_custom_object",
            }[operation],
        )
        api_method.side_effect = ApiException(status=500)

        with pytest.raises(ApiException):
            if operation == "create":
                c.create_custom_object("g", "v1", "ns", "foos", {})
            elif operation == "patch":
                c.patch_custom_object("g", "v1", "ns", "foos", "foo", {})
            else:
                c.delete_custom_object("g", "v1", "ns", "foos", "foo")

        fake_informer.invalidate.assert_not_called()

    def test_get_custom_object_returns_none_on_404(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.get_namespaced_custom_object.side_effect = ApiException(status=404)
        result = c.get_custom_object("g", "v1", "ns", "foos", "foo-1")
        assert result is None

    def test_get_custom_object_returns_object(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        obj = {"metadata": {"name": "foo-1"}}
        c._custom_objects_api.get_namespaced_custom_object.return_value = obj
        result = c.get_custom_object("g", "v1", "ns", "foos", "foo-1")
        assert result == obj

    def test_late_patch_and_get_responses_cannot_revive_watch_deleted_object(
        self, k8s_runtime_config
    ):
        """Direct responses never overwrite LIST/WATCH-owned object state or cursor."""
        c = self._make_client(k8s_runtime_config)
        informer = self._attach_real_informer(
            c,
            [{"metadata": {"name": "foo", "resourceVersion": "rv:12"}}],
            cursor="rv:list",
        )
        informer._handle_event(
            {
                "type": "DELETED",
                "object": {"metadata": {"name": "foo", "resourceVersion": "rv:deleted"}},
            }
        )
        stale = {"metadata": {"name": "foo", "resourceVersion": "rv:11"}}
        c._custom_objects_api.patch_namespaced_custom_object.return_value = stale

        assert c.patch_custom_object("g", "v1", "ns", "foos", "foo", {}) == stale
        assert "foo" not in informer._cache
        assert informer._resource_version == "rv:deleted"
        assert informer.list_if_synced() is None

        generation = informer._invalidation_generation
        c._custom_objects_api.get_namespaced_custom_object.return_value = stale
        assert c.get_custom_object("g", "v1", "ns", "foos", "foo") == stale
        assert "foo" not in informer._cache
        assert informer._resource_version == "rv:deleted"
        assert informer._invalidation_generation == generation

    @pytest.mark.parametrize("operation", ["create", "foreground_delete"])
    def test_mutation_forces_get_and_list_to_live_api(self, k8s_runtime_config, operation):
        """Reads cannot serve the pre-mutation cache until LIST republishes it."""
        c = self._make_client(k8s_runtime_config)
        old = {"metadata": {"name": "foo", "resourceVersion": "old"}}
        informer = self._attach_real_informer(c, [old])
        live = {"metadata": {"name": "foo", "resourceVersion": "live"}}
        if operation == "foreground_delete":
            live["metadata"].update(
                deletionTimestamp="2026-10-09T07:00:00Z",
                finalizers=["foregroundDeletion"],
            )
            c._custom_objects_api.delete_namespaced_custom_object.return_value = live
        c._custom_objects_api.get_namespaced_custom_object.return_value = live
        c._custom_objects_api.list_namespaced_custom_object.return_value = {"items": [live]}

        if operation == "create":
            c.create_custom_object("g", "v1", "ns", "foos", {"metadata": {"name": "bar"}})
        else:
            c.delete_custom_object("g", "v1", "ns", "foos", "foo", propagation_policy="Foreground")
        assert informer.get_if_synced("foo") is None
        assert c.get_custom_object("g", "v1", "ns", "foos", "foo") is live
        assert c.list_custom_objects("g", "v1", "ns", "foos") == [live]
        c._custom_objects_api.get_namespaced_custom_object.assert_called_once()
        c._custom_objects_api.list_namespaced_custom_object.assert_called_once()
        assert informer._cache["foo"] is old

    def test_delete_then_late_patch_response_only_invalidates(self, k8s_runtime_config):
        """A delayed patch response after delete cannot become cache state."""
        c = self._make_client(k8s_runtime_config)
        old = {"metadata": {"name": "foo", "resourceVersion": "rv:10"}}
        informer = self._attach_real_informer(c, [old])
        patch_started = threading.Event()
        release_patch = threading.Event()
        stale_patch = {"metadata": {"name": "foo", "resourceVersion": "rv:11"}}

        def delayed_patch(**_kwargs):
            patch_started.set()
            assert release_patch.wait(timeout=2)
            return stale_patch

        c._custom_objects_api.patch_namespaced_custom_object.side_effect = delayed_patch
        c._custom_objects_api.delete_namespaced_custom_object.return_value = {
            "kind": "Status",
            "status": "Success",
        }
        patch_result = []
        thread = threading.Thread(
            target=lambda: patch_result.append(
                c.patch_custom_object("g", "v1", "ns", "foos", "foo", {})
            )
        )

        thread.start()
        assert patch_started.wait(timeout=2)
        c.delete_custom_object("g", "v1", "ns", "foos", "foo")
        release_patch.set()
        thread.join(timeout=2)

        assert not thread.is_alive()
        assert patch_result == [stale_patch]
        assert informer._cache["foo"] is old
        assert informer._resource_version == "cursor"
        assert informer._invalidation_generation == 2
        assert informer.list_if_synced() is None

    def test_stopped_informer_is_not_restarted_for_read(self, k8s_runtime_config):
        """An existing stopped informer stays stopped and the read falls back live."""
        c = self._make_client(k8s_runtime_config)
        informer = WorkloadInformer(list_fn=MagicMock(), enable_watch=False)
        informer.stop()
        self._attach_informer(c, informer)
        live = {"metadata": {"name": "foo"}}
        c._custom_objects_api.get_namespaced_custom_object.return_value = live

        assert c.get_custom_object("g", "v1", "ns", "foos", "foo") is live
        assert informer._thread is None

    def test_get_custom_object_reraises_non_404(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.get_namespaced_custom_object.side_effect = ApiException(status=500)
        with pytest.raises(ApiException):
            c.get_custom_object("g", "v1", "ns", "foos", "foo-1")

    def test_get_custom_object_returns_cached_when_synced(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        cached_obj = {"metadata": {"name": "foo-1"}}
        fake_informer = self._attach_informer(c, MagicMock())
        fake_informer.get_if_synced.return_value = cached_obj

        result = c.get_custom_object("g", "v1", "ns", "foos", "foo-1")

        assert result is cached_obj
        c._custom_objects_api.get_namespaced_custom_object.assert_not_called()

    def test_get_custom_object_falls_back_to_api_without_synced_cache(
        self, k8s_runtime_config
    ):
        """A lazily started (unsynced) informer never serves stale-free reads."""
        c = self._make_client(k8s_runtime_config)
        obj = {"metadata": {"name": "foo-1"}}
        c._custom_objects_api.get_namespaced_custom_object.return_value = obj
        result = c.get_custom_object("g", "v1", "ns", "foos", "foo-1")
        assert result == obj
        c._custom_objects_api.get_namespaced_custom_object.assert_called_once()

    def test_list_custom_objects_returns_items(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.list_namespaced_custom_object.return_value = {
            "items": [{"metadata": {"name": "a"}}, {"metadata": {"name": "b"}}]
        }
        result = c.list_custom_objects("g", "v1", "ns", "foos")
        assert len(result) == 2

    def test_list_falls_back_to_api_when_cache_unsynced(self, k8s_runtime_config):
        """Selector terms are parsed in memory; an unsynced cache falls back to the API."""
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.list_namespaced_custom_object.return_value = {"items": []}

        assert c.list_custom_objects("g", "v1", "ns", "foos", "team=infra") == []
        c._custom_objects_api.list_namespaced_custom_object.assert_called_once()

    def test_list_custom_objects_returns_empty_on_404(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.list_namespaced_custom_object.side_effect = ApiException(status=404)
        result = c.list_custom_objects("g", "v1", "ns", "foos")
        assert result == []

    def test_list_custom_objects_reraises_non_404(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.list_namespaced_custom_object.side_effect = ApiException(status=500)
        with pytest.raises(ApiException):
            c.list_custom_objects("g", "v1", "ns", "foos")

    def _attach_synced_informer(self, c, items):
        fake_informer = MagicMock()
        fake_informer.list_if_synced.return_value = list(items)
        return self._attach_informer(c, fake_informer)

    def test_list_custom_objects_returns_cached_when_synced(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        items = [
            {"metadata": {"name": "a", "labels": {"opensandbox.io/id": "a"}}},
            {"metadata": {"name": "b", "labels": {"opensandbox.io/id": "b"}}},
        ]
        self._attach_synced_informer(c, items)
        result = c.list_custom_objects("g", "v1", "ns", "foos")
        assert result == items
        c._custom_objects_api.list_namespaced_custom_object.assert_not_called()

    def test_list_custom_objects_filters_cached_by_label_existence(
        self, k8s_runtime_config
    ):
        """Bare-key selector filters cached items in memory without an API call."""
        c = self._make_client(k8s_runtime_config)
        items = [
            {"metadata": {"name": "with-id", "labels": {"opensandbox.io/id": "x"}}},
            {"metadata": {"name": "no-id", "labels": {"other": "y"}}},
        ]
        self._attach_synced_informer(c, items)
        result = c.list_custom_objects(
            "g", "v1", "ns", "foos", label_selector="opensandbox.io/id"
        )
        assert [obj["metadata"]["name"] for obj in result] == ["with-id"]
        c._custom_objects_api.list_namespaced_custom_object.assert_not_called()

    def test_list_custom_objects_filters_cached_by_equality(self, k8s_runtime_config):
        """key=value selector filters cached items in memory without an API call."""
        c = self._make_client(k8s_runtime_config)
        items = [
            {"metadata": {"name": "alpha", "labels": {"team": "infra"}}},
            {"metadata": {"name": "beta", "labels": {"team": "data"}}},
        ]
        self._attach_synced_informer(c, items)
        result = c.list_custom_objects(
            "g", "v1", "ns", "foos", label_selector="team=infra"
        )
        assert [obj["metadata"]["name"] for obj in result] == ["alpha"]
        c._custom_objects_api.list_namespaced_custom_object.assert_not_called()

    def test_list_custom_objects_falls_back_when_informer_unsynced(
        self, k8s_runtime_config
    ):
        c = self._make_client(k8s_runtime_config)
        fake_informer = self._attach_informer(c, MagicMock())
        fake_informer.list_if_synced.return_value = None
        c._custom_objects_api.list_namespaced_custom_object.return_value = {
            "items": [{"metadata": {"name": "z"}}]
        }
        result = c.list_custom_objects("g", "v1", "ns", "foos")
        assert [obj["metadata"]["name"] for obj in result] == ["z"]
        fake_informer.list_if_synced.assert_called_once_with()
        c._custom_objects_api.list_namespaced_custom_object.assert_called_once()

    def test_list_custom_objects_falls_back_on_unsupported_selector(
        self, k8s_runtime_config
    ):
        """Set-based selectors (in/notin) bypass the cache parser and hit the API."""
        c = self._make_client(k8s_runtime_config)
        self._attach_synced_informer(c, [{"metadata": {"name": "x"}}])
        c._custom_objects_api.list_namespaced_custom_object.return_value = {
            "items": [{"metadata": {"name": "from-api"}}]
        }
        result = c.list_custom_objects(
            "g", "v1", "ns", "foos", label_selector="env in (prod, staging)"
        )
        assert [obj["metadata"]["name"] for obj in result] == ["from-api"]
        c._custom_objects_api.list_namespaced_custom_object.assert_called_once()

    @pytest.mark.parametrize("propagation_policy", [None, "Foreground"])
    def test_delete_custom_object_delegates_to_api(
        self, k8s_runtime_config, propagation_policy
    ):
        c = self._make_client(k8s_runtime_config)
        c.delete_custom_object(
            "g", "v1", "ns", "foos", "foo-1",
            grace_period_seconds=0, propagation_policy=propagation_policy,
        )
        c._custom_objects_api.delete_namespaced_custom_object.assert_called_once_with(
            group="g", version="v1", namespace="ns", plural="foos",
            name="foo-1", grace_period_seconds=0, propagation_policy=propagation_policy,
        )

    def test_patch_custom_object_delegates_to_api(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        body = {"spec": {"replicas": 2}}
        c.patch_custom_object("g", "v1", "ns", "foos", "foo-1", body)
        c._custom_objects_api.patch_namespaced_custom_object.assert_called_once_with(
            group="g", version="v1", namespace="ns", plural="foos",
            name="foo-1", body=body
        )

    def test_patch_pvc_uses_supported_strategic_merge_request(
        self, k8s_runtime_config
    ):
        """patch_pvc sends a strategic-merge request accepted by ApiClient."""
        c = self._make_client(k8s_runtime_config)
        api_client = ApiClient()
        c._core_v1_api = CoreV1Api(api_client)
        body = {"metadata": {"ownerReferences": [{"name": "x"}]}}
        try:
            with patch.object(
                api_client, "call_api", return_value="patched"
            ) as mock_call_api:
                result = c.patch_pvc("ns", "pvc-a", body)

            assert result == "patched"
            mock_call_api.assert_called_once()
            args, kwargs = mock_call_api.call_args
            assert args == (
                "/api/v1/namespaces/{namespace}/persistentvolumeclaims/{name}",
                "PATCH",
            )
            assert kwargs["path_params"] == {"namespace": "ns", "name": "pvc-a"}
            assert kwargs["header_params"]["Content-Type"] == (
                "application/strategic-merge-patch+json"
            )
            assert kwargs["body"] == body
            assert kwargs["auth_settings"] == ["BearerToken"]
            assert kwargs["_return_http_data_only"] is True
        finally:
            api_client.close()

    def test_create_secret_delegates_to_api(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        body = {"metadata": {"name": "my-secret"}}
        c.create_secret("ns", body)
        c._core_v1_api.create_namespaced_secret.assert_called_once_with(
            namespace="ns", body=body
        )

    def test_list_pods_returns_items(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_pod = MagicMock()
        c._core_v1_api.list_namespaced_pod.return_value = MagicMock(items=[mock_pod])
        result = c.list_pods("ns", label_selector="app=foo")
        assert result == [mock_pod]
        c._core_v1_api.list_namespaced_pod.assert_called_once_with(
            namespace="ns", label_selector="app=foo"
        )

    def test_list_pods_reraises_exceptions(self, k8s_runtime_config):
        """list_pods re-raises exceptions from the API."""
        c = self._make_client(k8s_runtime_config)
        c._core_v1_api.list_namespaced_pod.side_effect = Exception("network error")
        with pytest.raises(Exception, match="network error"):
            c.list_pods("ns")

    def test_read_pod_returns_pod_and_maps_not_found_to_none(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        pod = MagicMock()
        c._core_v1_api.read_namespaced_pod.return_value = pod

        assert c.read_pod("ns", "sandbox-0") is pod
        c._core_v1_api.read_namespaced_pod.assert_called_once_with(
            namespace="ns",
            name="sandbox-0",
        )

        c._core_v1_api.read_namespaced_pod.side_effect = ApiException(status=404)
        assert c.read_pod("ns", "missing") is None

    def test_read_runtime_class_delegates_to_api(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._node_v1_api.read_runtime_class.return_value = MagicMock(metadata=MagicMock(name="gvisor"))
        result = c.read_runtime_class("gvisor")
        c._node_v1_api.read_runtime_class.assert_called_once_with("gvisor")
        assert result is not None

    def test_write_limiter_called_on_create(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._write_limiter = mock_limiter
        c.create_custom_object("g", "v1", "ns", "foos", {})
        mock_limiter.acquire.assert_called_once()

    def test_write_limiter_called_on_delete(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._write_limiter = mock_limiter
        c.delete_custom_object("g", "v1", "ns", "foos", "foo-1")
        mock_limiter.acquire.assert_called_once()

    def test_write_limiter_called_on_patch(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._write_limiter = mock_limiter
        c.patch_custom_object("g", "v1", "ns", "foos", "foo-1", {})
        mock_limiter.acquire.assert_called_once()

    def test_write_limiter_called_on_create_secret(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._write_limiter = mock_limiter
        c.create_secret("ns", {})
        mock_limiter.acquire.assert_called_once()

    def test_read_limiter_called_on_get(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.get_namespaced_custom_object.return_value = {}
        mock_limiter = MagicMock()
        c._read_limiter = mock_limiter
        c.get_custom_object("g", "v1", "ns", "foos", "foo-1")
        mock_limiter.acquire.assert_called_once()

    def test_read_limiter_called_on_list(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._custom_objects_api.list_namespaced_custom_object.return_value = {"items": []}
        mock_limiter = MagicMock()
        c._read_limiter = mock_limiter
        c.list_custom_objects("g", "v1", "ns", "foos")
        mock_limiter.acquire.assert_called_once()

    def test_read_limiter_called_on_list_pods(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        c._core_v1_api.list_namespaced_pod.return_value = MagicMock(items=[])
        mock_limiter = MagicMock()
        c._read_limiter = mock_limiter
        c.list_pods("ns")
        mock_limiter.acquire.assert_called_once()

    def test_read_limiter_called_on_read_pod(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._read_limiter = mock_limiter
        c.read_pod("ns", "sandbox-0")
        mock_limiter.acquire.assert_called_once()

    def test_read_limiter_called_on_read_runtime_class(self, k8s_runtime_config):
        c = self._make_client(k8s_runtime_config)
        mock_limiter = MagicMock()
        c._read_limiter = mock_limiter
        c.read_runtime_class("gvisor")
        mock_limiter.acquire.assert_called_once()


@pytest.mark.parametrize("provider_kind", ["batchsandbox", "agent-sandbox"])
def test_provider_subscriptions_share_watch_and_respect_namespaces(k8s_runtime_config, provider_kind):
    from opensandbox_server.services.k8s.agent_sandbox_provider import AgentSandboxProvider
    from opensandbox_server.services.k8s.batchsandbox_provider import BatchSandboxProvider

    with patch("kubernetes.config.load_kube_config"), patch(
        "opensandbox_server.services.k8s.client.WorkloadInformer", WorkloadInformer
    ), patch.object(WorkloadInformer, "start") as start:
        client = K8sClient(k8s_runtime_config)
        provider = (BatchSandboxProvider if provider_kind == "batchsandbox" else AgentSandboxProvider)(client)
        sandbox_id = "123-test"
        names = [sandbox_id, "sandbox-123-test"]
        first, second, other_namespace = MagicMock(), MagicMock(), MagicMock()
        remove_first = provider.subscribe_workload(sandbox_id, "ns-a", first)
        remove_second = provider.subscribe_workload(sandbox_id, "ns-a", second)
        remove_other = provider.subscribe_workload(sandbox_id, "ns-b", other_namespace)
        assert start.call_count == 2  # One watch per namespace, not per request.
        informer = client._lookup_informer(provider.group, provider.version, provider.plural, "ns-a")
        assert informer is not None
        for name in names:
            informer._handle_event({
                "type": "MODIFIED",
                "object": {"metadata": {"name": name, "resourceVersion": "2"}},
            })
        first.assert_called_with("MODIFIED", {"metadata": {"name": names[-1], "resourceVersion": "2"}})
        assert first.call_count == 2
        assert second.call_count == 2
        other_namespace.assert_not_called()
        assert remove_first is not None and remove_second is not None and remove_other is not None
        remove_first()
        remove_second()
        remove_other()
        assert informer._subscribers == {}
        client.stop_informers()


def test_subscribe_returns_none_if_informer_cannot_start(k8s_runtime_config):
    with patch("kubernetes.config.load_kube_config"), patch(
        "opensandbox_server.services.k8s.client.WorkloadInformer", WorkloadInformer
    ), patch.object(
        WorkloadInformer, "start", side_effect=RuntimeError("cannot start")
    ):
        client = K8sClient(k8s_runtime_config)
        assert client.subscribe_custom_objects("group", "v1", "ns", "items", ["name"], MagicMock()) is None
        assert client._informers == {}