patch_kubernetes¶
- safir.testing.kubernetes.patch_kubernetes()¶
Replace the Kubernetes API with a mock class.
- Returns:
The mock Kubernetes API object.
- Return type:
Notes
This function will mock out the Kuberentes library configuration API in a manner compatible with
safir.kubernetes.initialize_kubernetes, ensuring that it does nothing during test. It will replace the KubernetesApiClientobject with aMagicMockand then redirectCoreV1ApiandCustomObjectsApito aMockKubernetesApiinstance.To use this mock successfully, you must not import
ApiClient,CoreV1Api, orCustomObjectsApidirectly into the local namespace, or they will not be correctly patched. Instead, use:from kubernetes_asyncio import client
and then use
client.ApiClientand so forth.Examples
Normally this should be called from a fixture in
tests/conftest.pysuch as the following:from safir.testing.kubernetes import MockKubernetesApi, patch_kubernetes @pytest.fixture def mock_kubernetes() -> Iterator[MockKubernetesApi]: yield from patch_kubernetes()