⚠️ MOVED Jenvtest was renamed to kube-api-test and moved to be part of Fabric8 Kubernetes Client
jenvtest makes it easy to implement integration tests with Kubernetes API Server in Java. Inspired by envtest in Kubebuilder (Golang).
It runs the API Server binaries directly (without nodes and other components, but with etcd). Linux, Windows, Mac is supported.
See also this blog post regarding the motivation and more.
Docs
See more documentation in docs directory.
Usage
Include dependency:
<dependency> <groupId>io.javaoperatorsdk</groupId> <artifactId>jenvtest</artifactId> <version>[version]</version> <scope>test</scope> </dependency>
In Unit Tests
See sample unit test here
@EnableKubeAPIServer class JUnitExtensionSimpleCaseTest { // Use @KubeConfig annotation to inject kube config yaml to init any client @KubeConfig static String kubeConfigYaml; @Test void simpleTestWithTargetVersion() { var client = new KubernetesClientBuilder() .withConfig(Config.fromKubeconfig(kubeConfigYaml)) .build(); client.resource(TestUtils.testConfigMap()).create(); var cm = client.resource(TestUtils.testConfigMap()).get(); Assertions.assertThat(cm).isNotNull(); } }
Public API
The underlying API can be used directly. See KubeApiServer
See it's usage in a test.
class KubeApiServerTest { @Test void apiServerTest() { var kubeApi = new KubeAPIServer(); kubeApi.start(); var client = new KubernetesClientBuilder() .withConfig(Config.fromKubeconfig(kubeApi.getKubeConfigYaml())) .build(); client.resource(TestUtils.testConfigMap()).create(); var cm = client.resource(TestUtils.testConfigMap()).get(); Assertions.assertThat(cm).isNotNull(); kubeApi.stop(); } }
Fabric8 Kubernetes Client Support
There is dedicated support for Fabric8 Kubernetes Client.
Using dependency:
<dependency> <groupId>io.javaoperatorsdk</groupId> <artifactId>jenvtest-fabric8-client-support</artifactId> <version>[version]</version> <scope>test</scope> </dependency>
The client can be directly injected to the test. See sample test here.
@EnableKubeAPIServer class JUnitFabric8ClientInjectionTest { static KubernetesClient client; // emitted code }
Support for Parallel Execution in Junit5
Parallel test execution is explicitly supported for JUnit5, in fact the project tests are running parallel. Running a new instance for each test case. This speeds up the tests (in our case >75%) in a way that test cases are also fully isolated from each other. See the surefire plugin config.
Testing Mutation and Validation Webhooks
An additional benefits os running K8S API Server this way, is that it makes easy to test Conversion Hooks and/or Dynamic Admission Controllers
In general, it is a best practice to use additional standard frameworks to implement Kubernetes webhooks, like kubernetes-webooks-framework with Quarkus or Spring. However, we demonstrate how it works in this test