MockKubernetesApi#
- class safir.testing.kubernetes.MockKubernetesApi#
Bases:
object
Mock Kubernetes API for testing.
This object simulates (with almost everything left out) the
CoreV1Api
,CustomObjectApi
, andNetworkingV1Api
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.This mock does not enforce namespace creation before creating objects in a namespace. Creating an object in a namespace will implicitly create that namespace if it doesn’t exist. However, it will not store a
V1Namespace
object, so to verify that a namespace was properly created (although not the order of creation), retrieve all the objects in the namespace withget_namespace_objects_for_test
and one of them will be theV1Namespace
object.Objects stored with
create_*
orreplace_*
methods are NOT copied. The object provided will be stored, so changing that object will change the object returned by subsequent API calls. Likewise, the object returned byread_*
calls will be the same object stored in the mock, and changing it will change the mock’s data. (Sometimes this is the desired behavior, sometimes it isn’t; we had to pick one and this is the approach we picked.)Most APIs do not support watches. The only current exception is
list_namespaced_event
.- initial_pod_phase#
String value to set the status of pods to when created. If this is set to
Running
(the default), a pod start event will also be generated when the pod is created.
- error_callback#
If set, called with the method name and any arguments whenever any Kubernetes API method is called and before it takes any acttion. This can be used for fault injection for testing purposes.
Notes
This class is normally not instantiated directly. Instead, call the
patch_kubernetes
function from a fixture to set up the mock. This is also why it is configurable by setting attributes rather than constructor arguments; the individual test usually doesn’t have control of the constructor.Methods Summary
create_namespace
(body)Create a namespace.
create_namespaced_config_map
(namespace, body)Create a
ConfigMap
object.create_namespaced_custom_object
(group, ...)Create a new custom namespaced object.
create_namespaced_event
(namespace, body)Store a new namespaced event.
create_namespaced_network_policy
(namespace, body)Create a network policy object.
create_namespaced_pod
(namespace, body)Create a pod object.
create_namespaced_resource_quota
(namespace, body)Create a resource quota object.
create_namespaced_secret
(namespace, body)Create a secret object.
create_namespaced_service
(namespace, body)Create a service object.
delete_namespace
(name)Delete a namespace.
delete_namespaced_config_map
(name, namespace)Delete a
ConfigMap
object.delete_namespaced_pod
(name, namespace)Delete a pod object.
get_all_objects_for_test
(kind)Return all objects of a given kind sorted by namespace and name.
get_namespace_objects_for_test
(namespace)Returns all objects in the given namespace.
get_namespaced_custom_object
(group, version, ...)Retrieve a namespaced custom object.
list_cluster_custom_object
(group, version, ...)List all custom objects in the cluster.
List known namespaces.
list_namespaced_event
(namespace, *[, ...])List namespaced events.
list_namespaced_pod
(namespace, *[, ...])List pod objects in a namespace.
List node information.
patch_namespaced_custom_object_status
(group, ...)Patch the status of a namespaced custom object.
patch_namespaced_secret
(name, namespace, body)Patch a secret object.
read_namespace
(name)Return the namespace object for a namespace.
read_namespaced_pod
(name, namespace)Read a pod object.
read_namespaced_pod_status
(name, namespace)Read the status of a pod.
read_namespaced_secret
(name, namespace)Read a secret.
replace_namespaced_custom_object
(group, ...)Replace a custom namespaced object.
replace_namespaced_secret
(name, namespace, body)Replace a secret.
set_nodes_for_test
(nodes)Set the node structures that will be returned by list_node.
Methods Documentation
- async create_namespace(body)#
Create a namespace.
The mock doesn’t truly track namespaces since it autocreates them when an object is created in that namespace (maybe that behavior should be optional). However, this method detects conflicts and stores the
V1Namespace
object so that it can be verified.- Parameters:
body (
V1Namespace
) – Namespace to create.- Raises:
kubernetes_asyncio.client.ApiException – Raised with 409 status if the namespace already exists.
- Return type:
- async create_namespaced_config_map(namespace, body)#
Create a
ConfigMap
object.
- async create_namespaced_custom_object(group, version, namespace, plural, body)#
Create a new custom namespaced object.
- Parameters:
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.
- Return type:
- async create_namespaced_event(namespace, body)#
Store a new namespaced event.
This uses the old core event API, not the new Events API.
- async create_namespaced_network_policy(namespace, body)#
Create a network policy object.
- async create_namespaced_pod(namespace, body)#
Create a pod object.
If
initial_pod_phase
on the mock Kubernetes object is set toRunning
, set the state toRunning
and generate a startup event. Otherwise, the status is set to whateverinitial_pod_phase
is set to, and no event is generated.
- async create_namespaced_resource_quota(namespace, body)#
Create a resource quota object.
- async create_namespaced_secret(namespace, body)#
Create a secret object.
- async create_namespaced_service(namespace, body)#
Create a service object.
- async delete_namespace(name)#
Delete a namespace.
This also immediately removes all objects in the namespace.
- async delete_namespaced_config_map(name, namespace)#
Delete a
ConfigMap
object.
- async delete_namespaced_pod(name, namespace)#
Delete a pod object.
- get_all_objects_for_test(kind)#
Return all objects of a given kind sorted by namespace and name.
- get_namespace_objects_for_test(namespace)#
Returns all objects in the given namespace.
- Parameters:
namespace (
str
) – Name of the namespace.- Returns:
All objects found in that namespace, sorted by kind and then name. Due to how objects are stored in the mock, we can’t distinguish between a missing namespace and a namespace with no objects. In both cases, the empty list is returned.
- Return type:
list of Any
- async get_namespaced_custom_object(group, version, namespace, plural, name)#
Retrieve a namespaced custom object.
- Parameters:
- Returns:
Body of the custom object.
- Return type:
dict of Any
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.
- async list_cluster_custom_object(group, version, plural)#
List all custom objects in the cluster.
- Parameters:
- Returns:
Dictionary with one key,
items
, whose value is a list of all the specified custom objects stored in the cluster.- Return type:
- async list_namespace()#
List known namespaces.
- Returns:
All namespaces, whether implicitly created or not. These will be the actual
V1Namespace
objects if one was stored, otherwise synthesized namespace objects.- Return type:
kubernetes_asyncio.client.V1NamespaceList
- async list_namespaced_event(namespace, *, field_selector=None, resource_version='0', timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)#
List namespaced events.
This uses the old core event API, not the new Events API. It does support watches.
- Parameters:
namespace (
str
) – Namespace to watch for events.field_selector (
Optional
[str
], default:None
) – Which events to retrieve when performing a watch. Currently, this is ignored.resource_version (
str
, default:'0'
) – Where to start in the event stream when performing a watch.timeout_seconds (
Optional
[int
], default:None
) – How long to return events for before exiting when performing a watch.watch (
bool
, default:False
) – Whether to act as a watch._preload_content (
bool
, default:True
) – Verified to be False when performing a watch._request_timeout (
Optional
[int
], default:None
) – Ignored, accepted for compatibility with the watch API.
- Returns:
List of events, when not called as a watch. If called as a watch, returns a mock
aiohttp.Response
with areadline
method that yields the events.- Return type:
kubernetes_asyncio.client.CoreV1EventList or unittest.mock.Mock
- async list_namespaced_pod(namespace, *, field_selector=None)#
List pod objects in a namespace.
- Parameters:
- Returns:
List of pods in that namespace.
- Return type:
kubernetes_asyncio.client.V1PodList
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
AssertionError – Some other
field_selector
was provided.
- async list_node()#
List node information.
- Returns:
The node information previouslyl stored with set_nodes_for_test, if any.
- Return type:
kubernetes_asyncio.client.V1NodeList
- async patch_namespaced_custom_object_status(group, version, namespace, plural, name, body)#
Patch the status of a namespaced custom object.
- Parameters:
group (
str
) – API group for this custom object.version (
str
) – API version for this custom object.namespace (
str
) – Namespace in which to create the object.plural (
str
) – API plural for this custom object.name (
str
) – Name of the object to retrieve.body (
list
[dict
[str
,Any
]]) – Body of the patch. The only patch supported is one withop
ofreplace
andpath
of/status
.
- Returns:
Modified body of the custom object.
- Return type:
dict of Any
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.
AssertionError – Raised if any other type of patch is provided.
- async patch_namespaced_secret(name, namespace, body)#
Patch a secret object.
- Parameters:
- Returns:
Patched secret object.
- Return type:
kubernetes_asyncio.client.V1Secret
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the secret does not exist.
AssertionError – Raised if any other type of patch was provided.
- async read_namespace(name)#
Return the namespace object for a namespace.
- Parameters:
name (
str
) – Name of namespace to retrieve.- Returns:
Corresponding namespace object. If create_namespace has been called, will return the stored object. Otherwise, returns a synthesized
V1Namespace
object if the namespace has been implicitly created.- Return type:
kubernetes_asyncio.client.V1Namespace
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- async read_namespaced_pod(name, namespace)#
Read a pod object.
- async read_namespaced_pod_status(name, namespace)#
Read the status of a pod.
- Parameters:
- Returns:
Pod object. The Kubernetes API returns a
V1Pod
rather than, as expected, aV1PodStatus
. Presumably the acutal API populates only the status portion, but we return the whole pod for testing purposes since it shouldn’t matter.- Return type:
kubernetes_asyncio.client.V1Pod
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the pod was not found.
- async read_namespaced_secret(name, namespace)#
Read a secret.
- async replace_namespaced_custom_object(group, version, namespace, plural, name, body)#
Replace a custom namespaced object.
- Parameters:
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 if the object does not exist.
- Return type:
- async replace_namespaced_secret(name, namespace, body)#
Replace a secret.