Skip to content

Added documentation for unit tests with the envtest crate#92

Open
emilianomaccaferri wants to merge 1 commit into
kube-rs:mainfrom
emilianomaccaferri:mock-testing-with-envtest-crate
Open

Added documentation for unit tests with the envtest crate#92
emilianomaccaferri wants to merge 1 commit into
kube-rs:mainfrom
emilianomaccaferri:mock-testing-with-envtest-crate

Conversation

@emilianomaccaferri
Copy link
Copy Markdown
Contributor

@emilianomaccaferri emilianomaccaferri commented Mar 7, 2026

I added a few lines that document how to test a simple reconciler using this crate.
Keep up the great work you're doing with the kube-rs ecosystem!

Edit: lol I didn't notice that @Danil-Grigorev was a kube-rs maintainer

Signed-off-by: Emiliano Maccaferri <inbox@emilianomaccaferri.com>
@emilianomaccaferri emilianomaccaferri changed the title Add unit tests with the envtest crate Added documentation for unit tests with the envtest crate Mar 7, 2026
async fn test_namespace_creation() {
initialize_ring();
// this assumes you have a `crd.yaml` in your `src` folder
let env = envtest_with_crds(vec![current_dir().unwrap().join("src/crd.yaml")]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let env = envtest_with_crds(vec![current_dir().unwrap().join("src/crd.yaml")]);
let env = Environment::default().with_crds(Test::crd());

Should work as well, which can remove envtest_with_crds

Here is an example - https://github.com/kube-rs/controller-rs/blob/main/src/controller.rs#L309, though passing vec! is not necessary.

// this assumes you have a `crd.yaml` in your `src` folder
let env = envtest_with_crds(vec![current_dir().unwrap().join("src/crd.yaml")]);
let apiserver = env.create().unwrap(); // this starts the api server!
let (client, ctx) = client_and_ctx(apiserver.kubeconfig().unwrap()).await;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let (client, ctx) = client_and_ctx(apiserver.kubeconfig().unwrap()).await;
let client = apiserver.client();
let ctx = KubeContext{client: client.clone()};

Should do it, assuming you have enabled kube feature https://docs.rs/crate/envtest/0.1.2/features#kube. It should be enabled by default.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! i'll check these tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants