Crates.io | kubernetes-mock |
lib.rs | kubernetes-mock |
version | 0.1.0 |
source | src |
created_at | 2023-10-19 10:19:15.460947 |
updated_at | 2023-10-19 10:19:15.460947 |
description | Mocking framework for interacting with the Kubernetes API |
homepage | |
repository | https://github.com/cisco-open/kubernetes-mock-rs |
max_upload_size | |
id | 1007679 |
size | 45,307 |
Mock Kubernetes client for testing things that interact with the Kubernetes API, including controllers.
View documentation, examples, and more by running make doc
. (link to docs.rs to come when published!)
488 lines of code, but that's 24% doc comments.
v1_25
or v1_21
.
default-features = false
in your Cargo.tomlMinimal compilable example:
use kubernetes_mock::*;
use kube::{Api, api::ListParams};
use k8s_openapi::api::core::v1::Node;
#[tokio::main]
async fn main() {
let (client, mut mocker) = make_mocker();
let api: Api<Node> = Api::all(client);
mocker.expect(|| async {
let nodes = api.list(&ListParams::default()).await;
},
MockReturn::List(&[Node::default()]),
).await.unwrap();
// ...
let handle = tokio::spawn(mocker.run());
api.list(&ListParams::default()).await;
handle.await.unwrap().unwrap(); // Assert tests pass with `unwrap()`.
// Requires two calls to unwrap - one for tokio spawn result, the other for the mocker result
}
Note: unwrap()
is not great for formatting, if you'd like to see errors pretty printed you can do the following:
if let Err(e) = handle.await.unwrap() { // still need one unwrap() for tokio spawn
panic!("{e:#?}"); // debug pretty-print
}
mocker.watch()
- it works but is not tested.
expect()
panicking, etc.expect()
take multiple API calls, meaning people can use one for all API callsFeel free to help, check out CONTRIBUTING.md!
Distributed under Apache 2.0. See LICENSE for more information.