Add human-friendly kubernetes client fixtures

kubernetes Client Fixtures

Note: The kr8s fixtures (mgmt_cluster_kr8s, cluster_kr8s) are the recommended approach for new tests.

  • mgmt_cluster — a wrapped kubernetes.client connected to the management cluster
  • cluster — a wrapped kubernetes.client connected to the current cluster (based on --cluster-kind)
def test_list_pods(mgmt_cluster, mgmt_cluster_namespace):
    pods = mgmt_cluster.CoreV1Api().list_namespaced_pod(namespace=mgmt_cluster_namespace)
    assert len(pods.items) > 0

kr8s Fixtures

The framework provides kr8s-based fixtures as an alternative to the standard kubernetes client. kr8s is a lightweight Python Kubernetes client with a simpler API.

Available Fixtures

  • mgmt_cluster_kr8s — a kr8s.Api connected to the management cluster
  • cluster_kr8s — a kr8s.Api connected to the current cluster (based on --cluster-kind)

Example

def test_pods_running(cluster_kr8s):
    pods = cluster_kr8s.get("pods", namespace="sylva-system")
    assert len(pods) > 0

Adding Custom CRD Classes

You can register your own CRD classes using kr8s.objects.new_class:

from kr8s.objects import new_class

MyResource = new_class(
    "MyResource",
    version="example.io/v1",
    namespaced=True,
)

def test_my_resource(cluster_kr8s):
    resources = cluster_kr8s.get(MyResource)
    assert len(resources) > 0

The framework also ships pre-defined CRD classes in sylva_test_framework.kr8s_crds. Importing them registers the classes with kr8s's internal registry so they work with cluster_kr8s.get().

Edited by Jonathan GAYVALLET

Merge request reports

Loading