From 583b057f2a76c83fbba5da641f8925312ef558b2 Mon Sep 17 00:00:00 2001 From: Stephane Epardaud Date: Tue, 9 Feb 2021 11:42:44 +0100 Subject: [PATCH] Added `@WithKubernetesTestServer` meta-annotation --- .../client/KubernetesTestServerTest.java | 10 ++---- .../client/KubernetesServerTestResource.java | 35 ++++--------------- .../client/WithKubernetesTestServer.java | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/WithKubernetesTestServer.java diff --git a/integration-tests/kubernetes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesTestServerTest.java b/integration-tests/kubernetes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesTestServerTest.java index 16c043c9177c3..c7733873bb04e 100644 --- a/integration-tests/kubernetes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesTestServerTest.java +++ b/integration-tests/kubernetes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesTestServerTest.java @@ -7,20 +7,14 @@ import org.junit.jupiter.api.Test; import io.fabric8.kubernetes.client.server.mock.KubernetesServer; -import io.quarkus.test.common.QuarkusTestResource; -import io.quarkus.test.common.ResourceArg; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; -import io.quarkus.test.kubernetes.client.KubernetesServerTestResource; import io.quarkus.test.kubernetes.client.KubernetesTestServer; +import io.quarkus.test.kubernetes.client.WithKubernetesTestServer; @TestProfile(KubernetesTestServerTest.MyProfile.class) -@QuarkusTestResource(value = KubernetesServerTestResource.class, initArgs = { - @ResourceArg(name = KubernetesServerTestResource.HTTPS, value = "false"), - @ResourceArg(name = KubernetesServerTestResource.CRUD, value = "false"), - @ResourceArg(name = KubernetesServerTestResource.PORT, value = "10001"), -}) +@WithKubernetesTestServer(https = false, crud = true, port = 10001) @QuarkusTest public class KubernetesTestServerTest { diff --git a/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/KubernetesServerTestResource.java b/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/KubernetesServerTestResource.java index be62d76be9826..97b75ab77a4bd 100644 --- a/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/KubernetesServerTestResource.java +++ b/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/KubernetesServerTestResource.java @@ -3,44 +3,23 @@ import java.lang.annotation.Annotation; import java.net.InetAddress; import java.util.Collections; -import java.util.Map; import io.fabric8.kubernetes.client.GenericKubernetesClient; import io.fabric8.kubernetes.client.server.mock.KubernetesServer; +import io.quarkus.test.common.QuarkusTestResourceConfigurableLifecycleManager; -public class KubernetesServerTestResource extends AbstractKubernetesTestResource { - - /** - * Configure HTTPS usage, defaults to false - */ - public final static String HTTPS = "https"; - /** - * Configure CRUD usage, defaults to true - */ - public final static String CRUD = "crud"; - /** - * Configure the port to use, defaults to 0, for the first available port - */ - public final static String PORT = "port"; +public class KubernetesServerTestResource extends AbstractKubernetesTestResource + implements QuarkusTestResourceConfigurableLifecycleManager { private boolean https = false; private boolean crud = true; private int port = 0; @Override - public void init(Map initArgs) { - String val = initArgs.get(HTTPS); - if (val != null) { - this.https = Boolean.parseBoolean(val); - } - val = initArgs.get(CRUD); - if (val != null) { - this.crud = Boolean.parseBoolean(val); - } - val = initArgs.get(PORT); - if (val != null) { - this.port = Integer.parseInt(val); - } + public void init(WithKubernetesTestServer annotation) { + this.https = annotation.https(); + this.crud = annotation.crud(); + this.port = annotation.port(); } @Override diff --git a/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/WithKubernetesTestServer.java b/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/WithKubernetesTestServer.java new file mode 100644 index 0000000000000..9517b67081354 --- /dev/null +++ b/test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/WithKubernetesTestServer.java @@ -0,0 +1,35 @@ +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.kubernetes.client.server.mock.KubernetesServer; +import io.quarkus.test.common.QuarkusTestResource; + +/** + * Use on your test resource to get a mock {@link KubernetesServer} spawn up, and injectable with {@link KubernetesTestServer}. + * This annotation is only active when used on a test class, and only for this test class. + */ +@QuarkusTestResource(value = KubernetesServerTestResource.class, restrictToAnnotatedTest = true) +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface WithKubernetesTestServer { + + /** + * Start it with HTTPS + */ + boolean https() default false; + + /** + * Start it in CRUD mode + */ + boolean crud() default true; + + /** + * Port to use, defaults to any available port + */ + int port() default 0; + +}