Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaults for kubernetes mock server #12360

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/src/main/asciidoc/kubernetes-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ Note that to take advantage of these features, the `quarkus-test-kubernetes-clie
</dependency>
----

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

[source%nowrap,java]
----
public class CustomKubernetesMockServerTestResource extends KubernetesMockServerTestResource {

@Override
public void configureMockServer(KubernetesMockServer mockServer) {
mockServer.expect().get().withPath("/api/v1/namespaces/test/pods")
.andReturn(200, new PodList())
.always();
}
}
----

and use this in your other test classes like this:
[source%nowrap,java]
----
@QuarkusTestResource(KubernetesMockServerTestResource.class)
@QuarkusTest
public class KubernetesClientTest {

//tests will now use the configured server...
}
----

Furthermore, to get a mock server that replies with empty lists per default (instead of getting 404 from the kubernetes api),
you can use the `EmptyDefaultKubernetesMockServerTestResource.class` instead of
`KubernetesMockServerTestResource.class`.


== Note on implementing the Watcher interface

Due to the restrictions imposed by GraalVM, extra care needs to be taken when implementing a `io.fabric8.kubernetes.client.Watcher` if the application is intended to work in native mode.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.quarkus.test.kubernetes.client;

import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.api.model.EventList;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.ServiceAccountList;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.api.model.networking.v1beta1.IngressList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;

/**
* Kubernetes mock server, which responds to most of the basic api calls.
*/
public class EmptyDefaultKubernetesMockServerTestResource extends KubernetesMockServerTestResource {

@Override
public void configureMockServer(KubernetesMockServer mockServer) {
final String ns = Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY;
final String basePath = "/api/v1/namespaces/" + ns;

mockServer.expect().get().withPath(basePath + "/configmaps")
.andReturn(200, new ConfigMapList())
.always();
mockServer.expect().get().withPath(basePath + "/configmaps?watch=true")
.andReturn(200, new ConfigMapList())
.always();

mockServer.expect().get().withPath(basePath + "/deployments")
.andReturn(200, new DeploymentList())
.always();
mockServer.expect().get().withPath(basePath + "/deployments?watch=true")
.andReturn(200, new DeploymentList())
.always();

mockServer.expect().get().withPath(basePath + "/events")
.andReturn(200, new EventList())
.always();
mockServer.expect().get().withPath(basePath + "/events?watch=true")
.andReturn(200, new EventList())
.always();

mockServer.expect().get().withPath("/apis/extensions/v1beta1/namespaces/" + ns + "/ingresses")
.andReturn(200, new IngressList())
.always();
mockServer.expect().get().withPath("/apis/extensions/v1beta1/namespaces/" + ns + "/ingresses?watch=true")
.andReturn(200, new IngressList())
.always();

mockServer.expect().get().withPath(basePath + "/pods")
.andReturn(200, new PodList())
.always();
mockServer.expect().get().withPath(basePath + "/pods?watch=true")
.andReturn(200, new PodList())
.always();

mockServer.expect().get().withPath(basePath + "/serviceaccounts")
.andReturn(200, new ServiceAccountList())
.always();
mockServer.expect().get().withPath(basePath + "/serviceaccounts?watch=true")
.andReturn(200, new ServiceAccountList())
.always();

mockServer.expect().get().withPath(basePath + "/services")
.andReturn(200, new ServiceList())
.always();
mockServer.expect().get().withPath(basePath + "/services?watch=true")
.andReturn(200, new ServiceList())
.always();
}

}