MockKubernetesApi#

class safir.testing.kubernetes.MockKubernetesApi#

Bases: object

Mock Kubernetes API for testing.

This object simulates (with almost everything left out) the CoreV1Api and CustomObjectApi 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.

Methods Summary

create_namespaced_config_map(namespace, ...)

param namespace:

create_namespaced_custom_object(group, ...)

param group:

create_namespaced_pod(namespace, pod)

param namespace:

create_namespaced_secret(namespace, secret)

param namespace:

delete_namespaced_config_map(name, namespace)

param name:

delete_namespaced_pod(name, namespace)

param name:

get_all_objects_for_test(kind)

Return all objects of a given kind sorted by namespace and name.

get_namespaced_custom_object(group, version, ...)

param group:

list_cluster_custom_object(group, version, ...)

param group:

list_namespaced_pod(namespace, *, field_selector)

param namespace:

patch_namespaced_custom_object_status(group, ...)

param group:

patch_namespaced_secret(name, namespace, body)

param name:

read_namespaced_pod(name, namespace)

param name:

read_namespaced_secret(name, namespace)

param name:

replace_namespaced_custom_object(group, ...)

param group:

replace_namespaced_secret(name, namespace, ...)

param name:

Methods Documentation

async create_namespaced_config_map(namespace, config_map)#
Parameters:
  • namespace (str) –

  • config_map (V1ConfigMap) –

Return type:

None

async create_namespaced_custom_object(group, version, namespace, plural, body)#
Parameters:
Return type:

None

async create_namespaced_pod(namespace, pod)#
Parameters:
  • namespace (str) –

  • pod (V1Pod) –

Return type:

None

async create_namespaced_secret(namespace, secret)#
Parameters:
  • namespace (str) –

  • secret (V1Secret) –

Return type:

None

async delete_namespaced_config_map(name, namespace)#
Parameters:
  • name (str) –

  • namespace (str) –

Return type:

V1Status

async delete_namespaced_pod(name, namespace)#
Parameters:
  • name (str) –

  • namespace (str) –

Return type:

V1Status

get_all_objects_for_test(kind)#

Return all objects of a given kind sorted by namespace and name.

Parameters:

kind (str) – The Kubernetes kind, such as Secret or Pod. 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, version, namespace, plural, name)#
Parameters:
  • group (str) –

  • version (str) –

  • namespace (str) –

  • plural (str) –

  • name (str) –

Return type:

Dict[str, Any]

async list_cluster_custom_object(group, version, plural)#
Parameters:
  • group (str) –

  • version (str) –

  • plural (str) –

Return type:

Dict[str, List[Dict[str, Any]]]

async list_namespaced_pod(namespace, *, field_selector)#
Parameters:
  • namespace (str) –

  • field_selector (str) –

Return type:

V1PodList

async patch_namespaced_custom_object_status(group, version, namespace, plural, name, body)#
Parameters:
Return type:

Dict[str, Any]

async patch_namespaced_secret(name, namespace, body)#
Parameters:
Return type:

V1Secret

async read_namespaced_pod(name, namespace)#
Parameters:
  • name (str) –

  • namespace (str) –

Return type:

V1Pod

async read_namespaced_secret(name, namespace)#
Parameters:
  • name (str) –

  • namespace (str) –

Return type:

V1Secret

async replace_namespaced_custom_object(group, version, namespace, plural, name, body)#
Parameters:
Return type:

None

async replace_namespaced_secret(name, namespace, secret)#
Parameters:
  • name (str) –

  • namespace (str) –

  • secret (V1Secret) –

Return type:

None