Skip to content

Commit

Permalink
Merge pull request #15893 from FroMage/15853
Browse files Browse the repository at this point in the history
Fixed documentation for Kubernetes mocking #15853
  • Loading branch information
geoand authored Mar 19, 2021
2 parents 15d6212 + 48b2799 commit de67ef0
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions docs/src/main/asciidoc/kubernetes-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ public class KubernetesClientTest {
final Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
final Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").withNamespace("test").and().build();
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods")
.andReturn(200,
new PodListBuilder().withNewMetadata().withResourceVersion("1").endMetadata().withItems(pod1, pod2)
.build())
.always();
// Set up Kubernetes so that our "pretend" pods are created
mockServer.getClient().pods().create(pod1);
mockServer.getClient().pods().create(pod2);
}
@Test
Expand All @@ -140,6 +138,42 @@ Note that to take advantage of these features, the `quarkus-test-kubernetes-clie
</dependency>
----

By default, the mock server will be in CRUD mode, so you have to use the client to build your state before your application can retrieve it,
but you can also set it up in non-CRUD mode and mock all HTTP requests made to Kubernetes:

[source%nowrap,java]
----
// you can even configure aspects like crud, https and port on this annotation
@WithKubernetesTestServer(crud = false)
@QuarkusTest
public class KubernetesClientTest {
@KubernetesTestServer
KubernetesServer mockServer;
@BeforeEach
public void before() {
final Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
final Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").withNamespace("test").and().build();
// Mock any HTTP request to Kubernetes pods so that our pods are returned
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods")
.andReturn(200,
new PodListBuilder().withNewMetadata().withResourceVersion("1").endMetadata().withItems(pod1, pod2)
.build())
.always();
}
@Test
public void testInteractionWithAPIServer() {
RestAssured.when().get("/pod/test").then()
.body("size()", is(2));
}
}
----


Alternately, you can create a `CustomKubernetesMockServerTestResource.java` to ensure all your `@QuarkusTest` enabled test classes share the same mock server setup:

[source%nowrap,java]
Expand Down

0 comments on commit de67ef0

Please sign in to comment.