Skip to content

Commit

Permalink
feat: Introduced @WithOpenShiftTestServer annotation
Browse files Browse the repository at this point in the history
Allows to easily run QuarkusTests with OpenShiftMockserver

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jul 22, 2021
1 parent dd1e7a3 commit 394cd7f
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 9 deletions.
14 changes: 14 additions & 0 deletions docs/src/main/asciidoc/kubernetes-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,20 @@ public class OpenShiftClientTest {
...
----

Or by using the `@WithOpenShiftTestServer` similar to the `@WithKubernetesTestServer` explained in the
previous section:

[source, java]
----
@WithOpenShiftTestServer
@QuarkusTest
public class OpenShiftClientTest {
@OpenShiftTestServer
private OpenShiftServer mockOpenShiftServer;
...
----

To use this feature, you have to add a dependency on `quarkus-test-openshift-client`:

[source,xml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static Config createConfig(KubernetesClientBuildConfig buildConfig, TlsCo
.withProxyUsername(buildConfig.proxyUsername.orElse(base.getProxyUsername()))
.withProxyPassword(buildConfig.proxyPassword.orElse(base.getProxyPassword()))
.withNoProxy(buildConfig.noProxy.orElse(base.getNoProxy()))
.withHttp2Disable(base.isHttp2Disable())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.fabric8.kubernetes.client.Config;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.quarkus.arc.DefaultBean;

@Singleton
Expand All @@ -22,7 +23,10 @@ public class OpenShiftClientProducer {
@Singleton
@Produces
public OpenShiftClient openShiftClient(Config config) {
client = new DefaultOpenShiftClient(config);
// TODO - Temporary fix for https://github.com/fabric8io/kubernetes-client/pull/3347 + WithOpenShiftTestServer
final OpenShiftConfig openShiftConfig = new OpenShiftConfig(config);
openShiftConfig.setHttp2Disable(config.isHttp2Disable());
client = new DefaultOpenShiftClient(openShiftConfig);
return client;
}

Expand Down
22 changes: 22 additions & 0 deletions integration-tests/kubernetes-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-config</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-openshift-client</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand All @@ -42,6 +46,11 @@
<artifactId>quarkus-test-kubernetes-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-openshift-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -62,6 +71,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-openshift-client-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.it.kubernetes.client;

import static org.assertj.core.api.Assertions.assertThat;

import javax.inject.Inject;

import org.junit.jupiter.api.Test;

import io.fabric8.openshift.api.model.ProjectBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.server.mock.OpenShiftServer;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kubernetes.client.OpenShiftTestServer;
import io.quarkus.test.kubernetes.client.WithOpenShiftTestServer;

@WithOpenShiftTestServer
@QuarkusTest
class OpenShiftTestServerTest {

@OpenShiftTestServer
private OpenShiftServer mockServer;

@Inject
OpenShiftClient client;

@Test
void testInjectionDefaultsToCrud() {
mockServer.getOpenshiftClient().projects().createOrReplace(new ProjectBuilder()
.withNewMetadata().withName("example-project").addToLabels("project", "crud-is-true").endMetadata()
.build());
assertThat(client)
.isNotSameAs(mockServer.getOpenshiftClient())
.isNotSameAs(mockServer.getOpenshiftClient())
.returns("crud-is-true",
c -> c.projects().withName("example-project").get().getMetadata().getLabels().get("project"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.GenericKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

public abstract class AbstractKubernetesTestResource<T> implements QuarkusTestResourceLifecycleManager {
public abstract class AbstractKubernetesTestResource<T, C extends KubernetesClient>
implements QuarkusTestResourceLifecycleManager {
protected T server;

@Override
public Map<String, String> start() {
final Map<String, String> systemProps = new HashMap<>();
systemProps.put(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
systemProps.put("quarkus.tls.trust-all", "true");
systemProps.put(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
systemProps.put(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
systemProps.put(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
Expand All @@ -37,7 +40,7 @@ public Map<String, String> start() {
return systemProps;
}

protected abstract GenericKubernetesClient<?> getClient();
protected abstract GenericKubernetesClient<C> getClient();

/**
* Can be used by subclasses in order to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import java.lang.annotation.Annotation;

import io.fabric8.kubernetes.client.GenericKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;

/**
* @deprecated use {@link KubernetesServerTestResource}
*/
@Deprecated
public class KubernetesMockServerTestResource extends AbstractKubernetesTestResource<KubernetesMockServer> {
public class KubernetesMockServerTestResource
extends AbstractKubernetesTestResource<KubernetesMockServer, NamespacedKubernetesClient> {

@Override
protected GenericKubernetesClient<?> getClient() {
protected GenericKubernetesClient<NamespacedKubernetesClient> getClient() {
return server.createClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import java.util.function.Consumer;

import io.fabric8.kubernetes.client.GenericKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.quarkus.test.common.QuarkusTestResourceConfigurableLifecycleManager;

public class KubernetesServerTestResource extends AbstractKubernetesTestResource<KubernetesServer>
public class KubernetesServerTestResource extends AbstractKubernetesTestResource<KubernetesServer, NamespacedKubernetesClient>
implements QuarkusTestResourceConfigurableLifecycleManager<WithKubernetesTestServer> {

private boolean https = false;
Expand All @@ -30,7 +31,7 @@ public void init(WithKubernetesTestServer annotation) {
}

@Override
protected GenericKubernetesClient<?> getClient() {
protected GenericKubernetesClient<NamespacedKubernetesClient> getClient() {
return server.getClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
*/
Class<? extends Consumer<KubernetesServer>> setup() default NO_SETUP.class;

static class NO_SETUP implements Consumer<KubernetesServer> {
class NO_SETUP implements Consumer<KubernetesServer> {
@Override
public void accept(KubernetesServer t) {
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import io.fabric8.openshift.client.server.mock.OpenShiftMockServer;

/**
* @deprecated use {@link OpenShiftServerTestResource}
*/
@Deprecated
public class OpenShiftMockServerTestResource extends KubernetesMockServerTestResource {

protected OpenShiftMockServer createMockServer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.quarkus.test.kubernetes.client;

import java.lang.annotation.Annotation;
import java.util.function.Consumer;

import io.fabric8.kubernetes.client.GenericKubernetesClient;
import io.fabric8.openshift.client.NamespacedOpenShiftClient;
import io.fabric8.openshift.client.server.mock.OpenShiftServer;
import io.quarkus.test.common.QuarkusTestResourceConfigurableLifecycleManager;

public class OpenShiftServerTestResource extends AbstractKubernetesTestResource<OpenShiftServer, NamespacedOpenShiftClient>
implements QuarkusTestResourceConfigurableLifecycleManager<WithOpenShiftTestServer> {

private boolean https = false;
private boolean crud = true;
private Consumer<OpenShiftServer> setup;

@Override
public void init(WithOpenShiftTestServer annotation) {
this.https = annotation.https();
this.crud = annotation.crud();
try {
this.setup = annotation.setup().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

@Override
protected GenericKubernetesClient<NamespacedOpenShiftClient> getClient() {
return server.getOpenshiftClient();
}

@Override
protected void initServer() {
server.before();
}

@Override
protected void configureServer() {
if (setup != null)
setup.accept(server);
}

@Override
protected OpenShiftServer createServer() {
return new OpenShiftServer(https, crud);
}

@Override
public void stop() {
if (server != null) {
server.after();
server = null;
}
}

@Override
protected Class<?> getInjectedClass() {
return OpenShiftServer.class;
}

@Override
protected Class<? extends Annotation> getInjectionAnnotation() {
return OpenShiftTestServer.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.test.kubernetes.client;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.fabric8.openshift.client.server.mock.OpenShiftServer;

/**
* Used to specify that the field should be injected with the mock OpenShift API server
* Can only be used on type {@link OpenShiftServer}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface OpenShiftTestServer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.test.kubernetes.client;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Consumer;

import io.fabric8.openshift.client.server.mock.OpenShiftServer;
import io.quarkus.test.common.QuarkusTestResource;

/**
* Use on your test resource to get a mock {@link OpenShiftServer} spawn up, and injectable with {@link OpenShiftTestServer}.
* This annotation is only active when used on a test class, and only for this test class.
*/
@QuarkusTestResource(value = OpenShiftServerTestResource.class, restrictToAnnotatedClass = true)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WithOpenShiftTestServer {

/**
* Start it with HTTPS
*/
boolean https() default false;

/**
* Start it in CRUD mode
*/
boolean crud() default true;

/**
* Setup class to call after the mock server is created, for custom setup.
*/
Class<? extends Consumer<OpenShiftServer>> setup() default NO_SETUP.class;

class NO_SETUP implements Consumer<OpenShiftServer> {
@Override
public void accept(OpenShiftServer t) {
}
}
}

0 comments on commit 394cd7f

Please sign in to comment.