Skip to main content

Module testing

Module testing 

Source
Available on crate feature k8s only.
Expand description

Tests against a real API server: a client for a test cluster (kind, k3d, envtest’s kube-apiserver) and a namespace of its own per test, deleted afterwards — what controllers and webhooks need, since a fake API server doesn’t run admission, garbage collection or status subresources.

use rws_k8s::k8s::testing::TestCluster;

#[tokio::test]
async fn my_controller_creates_the_deployment() {
    let Some(cluster) = TestCluster::from_env().await else { return };   // skipped without a cluster
    let ns = cluster.namespace("backup").await.unwrap();
    // … create objects in ns.name(), run the reconcile, assert …
    ns.delete().await;
}

The cluster comes from RWS_TEST_KUBECONFIG (a kubeconfig file). Without it the test is skipped, unless RWS_TEST_REQUIRE_KUBERNETES is set — then it fails, so CI can’t pass by skipping. Never point it at a cluster that matters: the tests create and delete namespaces.

Structs§

TestCluster
A test cluster’s client.
TestNamespace
A namespace of one test. delete it at the end (a failed test leaves it for inspection; they’re labelled rws-test=true).

Functions§

eventually
Wait until check answers Some (polled every 100 ms), at most timeout; for asserting what a controller eventually does.