MockKubernetesApi#
- class safir.testing.kubernetes.MockKubernetesApi#
Bases:
object
Mock Kubernetes API for testing.
This object simulates (with almost everything left out) the
BatchV1Api
,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 current exceptions are
list_namespaced_event
,list_namespaced_ingress
,list_namespaced_job
,list_namespaced_pod
, andlist_namespaced_service
.- 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_ingress
(namespace, body)Create an ingress object.
create_namespaced_job
(namespace, body)Create a job object.
create_namespaced_network_policy
(namespace, body)Create a network policy object.
Create a persistent volume claim.
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_custom_object
(group, ...)Delete a custom namespaced object.
delete_namespaced_ingress
(name, namespace, *)Delete an ingress object.
delete_namespaced_job
(name, namespace, *[, ...])Delete a job object.
delete_namespaced_pod
(name, namespace, *[, ...])Delete a pod object.
delete_namespaced_service
(name, namespace, *)Delete a service 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_ingress
(namespace, *[, ...])List ingress objects in a namespace.
list_namespaced_job
(namespace, *[, ...])List job objects in a namespace.
list_namespaced_pod
(namespace, *[, ...])List pod objects in a namespace.
list_namespaced_service
(namespace, *[, ...])List service objects in a namespace.
List node information.
patch_namespaced_custom_object_status
(group, ...)Patch the status of a namespaced custom object.
patch_namespaced_ingress_status
(name, ...)Patch the status of an ingress object.
patch_namespaced_pod_status
(name, namespace, ...)Patch the status of a pod object.
patch_namespaced_secret
(name, namespace, body)Patch a secret object.
read_namespace
(name)Return the namespace object for a namespace.
read_namespaced_config_map
(name, namespace)Read a config map object.
read_namespaced_ingress
(name, namespace)Read a ingress object.
read_namespaced_job
(name, namespace)Read a job object.
read_namespaced_pod
(name, namespace)Read a pod object.
read_namespaced_pod_status
(name, namespace)Read the status of a pod.
read_namespaced_resource_quota
(name, namespace)Read a resource quota object.
read_namespaced_secret
(name, namespace)Read a secret.
read_namespaced_service
(name, namespace)Read a service.
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_ingress(namespace, body)#
Create an ingress object.
In real life, it usually takes some time for the Ingress controller to set the
status.load_balancer.ingress
field. It appears that status.load_balancer is created when the Ingress is but is empty, so that’s what we do too.Use
patch_namespaced_ingress_status()
to update the field to indicate that the ingress is ready.
- async create_namespaced_job(namespace, body)#
Create a job object.
A pod corresponding to this job will also be created. The pod will have a label
job-name
set to the name of the Job object. Its name will be the job’s name prepended to-abcde
. Ifinitial_pod_phase
on the mock is set toRunning
, thestatus.active
field of the job will be set to 1.
- async create_namespaced_network_policy(namespace, body)#
Create a network policy object.
- async create_namespaced_persistent_volume_claim(namespace, body)#
Create a persistent volume claim.
- 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, *, grace_period_seconds=None, body=None)#
Delete a namespace.
This also immediately removes all objects in the namespace.
- Parameters:
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- Return type:
- async delete_namespaced_config_map(name, namespace, *, grace_period_seconds=None, body=None)#
Delete a
ConfigMap
object.- Parameters:
- Returns:
Success status if object was deleted.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.
- async delete_namespaced_custom_object(group, version, namespace, plural, name, *, grace_period_seconds=None, body=None)#
Delete a custom namespaced object.
- Parameters:
group (
str
) – API group for this custom object.version (
str
) – API version for this custom object.namespace (
str
) – Namespace in which to delete the object.plural (
str
) – API plural for this custom object.name (
str
) – Custom object to delete.grace_period_seconds (
Optional
[int
], default:None
) – Grace period for object deletion (currently ignored).body (
Optional
[V1DeleteOptions
], default:None
) – Delete options (currently ignored).
- Returns:
Success status if object was deleted.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.
- async delete_namespaced_ingress(name, namespace, *, grace_period_seconds=None, body=None)#
Delete an ingress object.
- Parameters:
- Returns:
Success status.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress was not found.
- async delete_namespaced_job(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None)#
Delete a job object.
Will also propagate to pods.
- Parameters:
name (
str
) – Name of job to delete.namespace (
str
) – Namespace of job to delete.grace_period_seconds (
Optional
[int
], default:None
) – Grace period for object deletion (currently ignored).propagation_policy (
str
, default:'Foreground'
) – Propagation policy for deletion. Must beForeground
if specified, and has no effect on the behavior of the mock.body (
Optional
[V1DeleteOptions
], default:None
) – Delete options (currently ignored).
- Returns:
Success status.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
AssertionError – Raised if the propagation policy is not
Foreground
.kubernetes_asyncio.client.ApiException – Raised with 404 status if the job was not found.
- async delete_namespaced_pod(name, namespace, *, grace_period_seconds=None, body=None)#
Delete a pod object.
- Parameters:
- Returns:
Success status.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the pod was not found.
- async delete_namespaced_service(name, namespace, *, grace_period_seconds=None, body=None)#
Delete a service object.
- Parameters:
- Returns:
Success status.
- Return type:
kubernetes_asyncio.client.V1Status
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the service was not found.
- 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=None, 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 (
Optional
[str
], default:None
) – 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_ingress(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)#
List ingress objects in a namespace.
This does support watches.
- Parameters:
namespace (
str
) – Namespace of ingresss to list.field_selector (
Optional
[str
], default:None
) – Onlymetadata.name=...
is supported. It is parsed to find the ingress name and only ingresss matching that name will be returned.label_selector (
Optional
[str
], default:None
) – Which events to retrieve when performing a watch. All labels must match.resource_version (
Optional
[str
], default:None
) – Where to start in the event stream when performing a watch. If None, starts with the next change.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 ingresss in that namespace, when not called as a watch. If called as a watch, returns a mock
aiohttp.Response
with areadline
metehod that yields the events.- Return type:
kubernetes_asyncio.client.V1IngressList or unittest.mock.Mock
- Raises:
AssertionError – Some other
field_selector
was provided.kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- async list_namespaced_job(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)#
List job objects in a namespace.
This does support watches.
- Parameters:
namespace (
str
) – Namespace of jobs to list.field_selector (
Optional
[str
], default:None
) – Onlymetadata.name=...
is supported. It is parsed to find the job name and only jobs matching that name will be returned.label_selector (
Optional
[str
], default:None
) – Which events to retrieve when performing a watch. All labels must match.resource_version (
Optional
[str
], default:None
) – Where to start in the event stream when performing a watch. If None, starts with the next change.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 jobs in that namespace, when not called as a watch. If called as a watch, returns a mock
aiohttp.Response
with areadline
metehod that yields the events.- Return type:
kubernetes_asyncio.client.V1JobList or unittest.mock.Mock
- Raises:
AssertionError – Some other
field_selector
was provided.kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- async list_namespaced_pod(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)#
List pod objects in a namespace.
This does support watches.
- Parameters:
namespace (
str
) – Namespace of pods to list.field_selector (
Optional
[str
], default:None
) – Onlymetadata.name=...
is supported. It is parsed to find the pod name and only pods matching that name will be returned.label_selector (
Optional
[str
], default:None
) – Only pods with labels matching all of these will be returned.resource_version (
Optional
[str
], default:None
) – Where to start in the event stream when performing a watch. If None, starts with the next change.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 pods in that namespace, when not called as a watch. If called as a watch, returns a mock
aiohttp.Response
with areadline
metehod that yields the events.- Return type:
kubernetes_asyncio.client.V1PodList or unittest.mock.Mock
- Raises:
AssertionError – Some other
field_selector
was provided.kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- async list_namespaced_service(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)#
List service objects in a namespace.
This does support watches.
- Parameters:
namespace (
str
) – Namespace of services to list.field_selector (
Optional
[str
], default:None
) – Onlymetadata.name=...
is supported. It is parsed to find the service name and only services matching that name will be returned.label_selector (
Optional
[str
], default:None
) – Which events to retrieve when performing a watch. All labels must match.resource_version (
Optional
[str
], default:None
) – Where to start in the event stream when performing a watch. If None, starts with the next change.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 services in that namespace, when not called as a watch. If called as a watch, returns a mock
aiohttp.Response
with areadline
metehod that yields the events.- Return type:
kubernetes_asyncio.client.V1ServiceList or unittest.mock.Mock
- Raises:
AssertionError – Some other
field_selector
was provided.kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.
- 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_ingress_status(name, namespace, body)#
Patch the status of an ingress object.
- Parameters:
- Returns:
Corresponding object.
- Return type:
kubernetes_asyncio.client.V1Ingress
- Raises:
kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress does not exist.
AssertionError – Raised if any other type of patch was provided.
- async patch_namespaced_pod_status(name, namespace, body)#
Patch the status of a pod object.
- Parameters:
- Returns:
Patched pod object.
- Return type:
kubernetes_asyncio.client.V1Pod
- 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 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_config_map(name, namespace)#
Read a config map object.
- async read_namespaced_ingress(name, namespace)#
Read a ingress object.
- async read_namespaced_job(name, namespace)#
Read a job object.
- 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_resource_quota(name, namespace)#
Read a resource quota object.
- async read_namespaced_secret(name, namespace)#
Read a secret.
- async read_namespaced_service(name, namespace)#
Read a service.
- 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.