MockKubernetesApi¶
- class safir.testing.kubernetes.MockKubernetesApi(*args, **kw)¶
Bases:
unittest.mock.Mock
Mock Kubernetes API for testing.
This object simulates (with almost everything left out) the
CoreV1Api
andCustomObjectApi
client objects while keeping simple internal state. It is intended to be used as a mock inside tests.Methods ending with
_for_test
are outside of the API and are intended for use by the test suite.If
error_callback
is set to a callable, it will be called with the method name and any arguments whenever any Kubernetes API method is called and before it takes any action. This can be used to inject exceptions for test purposes.Notes
This class is normally not instantiated directly. Instead, call the
patch_kubernetes
function from a fixture to set up the mock.Attributes Summary
Methods Summary
__call__
(*args, **kwargs)Call self as a function.
assert_any_call
(*args, **kwargs)assert the mock has been called with the specified arguments.
assert that the mock was called at least once
assert that the mock was called only once.
assert_called_once_with
(*args, **kwargs)assert that the mock was called exactly once and that that call was with the specified arguments.
assert_called_with
(*args, **kwargs)assert that the last call was made with the specified arguments.
assert_has_calls
(calls[, any_order])assert the mock has been called with the specified calls.
assert that the mock was never called.
attach_mock
(mock, attribute)Attach a mock as an attribute of this one, replacing its name and parent.
configure_mock
(**kwargs)Set attributes on the mock through keyword arguments.
create_namespaced_config_map
(namespace, ...)create_namespaced_custom_object
(group, ...)create_namespaced_pod
(namespace, pod)create_namespaced_secret
(namespace, secret)delete_namespaced_config_map
(name, namespace)delete_namespaced_pod
(name, namespace)get_all_objects_for_test
(kind)Return all objects of a given kind sorted by namespace and name.
get_namespaced_custom_object
(group, version, ...)list_cluster_custom_object
(group, version, ...)mock_add_spec
(spec[, spec_set])Add a spec to a mock.
patch_namespaced_custom_object_status
(group, ...)patch_namespaced_secret
(name, namespace, body)read_namespaced_pod
(name, namespace)read_namespaced_secret
(name, namespace)replace_namespaced_custom_object
(group, ...)replace_namespaced_secret
(name, namespace, ...)reset_mock
([visited, return_value, side_effect])Restore the mock object to its initial state.
Attributes Documentation
- call_args¶
- call_args_list¶
- call_count¶
- called¶
- mock_calls¶
- return_value¶
- side_effect¶
Methods Documentation
- __call__(*args, **kwargs)¶
Call self as a function.
- assert_any_call(*args, **kwargs)¶
assert the mock has been called with the specified arguments.
The assert passes if the mock has ever been called, unlike
assert_called_with
andassert_called_once_with
that only pass if the call is the most recent one.
- assert_called()¶
assert that the mock was called at least once
- assert_called_once()¶
assert that the mock was called only once.
- assert_called_once_with(*args, **kwargs)¶
assert that the mock was called exactly once and that that call was with the specified arguments.
- assert_called_with(*args, **kwargs)¶
assert that the last call was made with the specified arguments.
Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.
- assert_has_calls(calls, any_order=False)¶
assert the mock has been called with the specified calls. The
mock_calls
list is checked for the calls.If
any_order
is False (the default) then the calls must be sequential. There can be extra calls before or after the specified calls.If
any_order
is True then the calls can be in any order, but they must all appear inmock_calls
.
- assert_not_called()¶
assert that the mock was never called.
- attach_mock(mock, attribute)¶
Attach a mock as an attribute of this one, replacing its name and parent. Calls to the attached mock will be recorded in the
method_calls
andmock_calls
attributes of this one.
- configure_mock(**kwargs)¶
Set attributes on the mock through keyword arguments.
Attributes plus return values and side effects can be set on child mocks using standard dot notation and unpacking a dictionary in the method call:
>>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError} >>> mock.configure_mock(**attrs)
- async create_namespaced_config_map(namespace: str, config_map: kubernetes_asyncio.client.models.v1_config_map.V1ConfigMap) None ¶
- async create_namespaced_custom_object(group: str, version: str, namespace: str, plural: str, body: Dict[str, Any]) None ¶
- async create_namespaced_pod(namespace: str, pod: kubernetes_asyncio.client.models.v1_pod.V1Pod) None ¶
- async create_namespaced_secret(namespace: str, secret: kubernetes_asyncio.client.models.v1_secret.V1Secret) None ¶
- async delete_namespaced_config_map(name: str, namespace: str) kubernetes_asyncio.client.models.v1_status.V1Status ¶
- async delete_namespaced_pod(name: str, namespace: str) kubernetes_asyncio.client.models.v1_status.V1Status ¶
- get_all_objects_for_test(kind: str) List[Any] ¶
Return all objects of a given kind sorted by namespace and name.
- Parameters
kind (
str
) – The Kubernetes kind, such asSecret
orPod
. This is case-sensitive.- Returns
objects – All objects of that kind found in the mock, sorted by namespace and then name.
- Return type
List[
typing.Any
]
- async get_namespaced_custom_object(group: str, version: str, namespace: str, plural: str, name: str) Dict[str, Any] ¶
- async list_cluster_custom_object(group: str, version: str, plural: str) Dict[str, List[Dict[str, Any]]] ¶
- mock_add_spec(spec, spec_set=False)¶
Add a spec to a mock.
spec
can either be an object or a list of strings. Only attributes on thespec
can be fetched as attributes from the mock.If
spec_set
is True then only attributes on the spec can be set.
- async patch_namespaced_custom_object_status(group: str, version: str, namespace: str, plural: str, name: str, body: Dict[str, Any]) Dict[str, Any] ¶
- async patch_namespaced_secret(name: str, namespace: str, body: List[Dict[str, Any]]) kubernetes_asyncio.client.models.v1_secret.V1Secret ¶
- async read_namespaced_pod(name: str, namespace: str) kubernetes_asyncio.client.models.v1_pod.V1Pod ¶
- async read_namespaced_secret(name: str, namespace: str) kubernetes_asyncio.client.models.v1_secret.V1Secret ¶
- async replace_namespaced_custom_object(group: str, version: str, namespace: str, plural: str, name: str, body: Dict[str, Any]) None ¶
- async replace_namespaced_secret(name: str, namespace: str, secret: kubernetes_asyncio.client.models.v1_secret.V1Secret) None ¶
- reset_mock(visited=None, *, return_value=False, side_effect=False)¶
Restore the mock object to its initial state.