forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for quarkusio#14744: support KibernetesServer test resource for C…
…RUD operations
- Loading branch information
Showing
4 changed files
with
175 additions
and
51 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...lient/src/main/java/io/quarkus/test/kubernetes/client/AbstractKubernetesTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.quarkus.test.kubernetes.client; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.GenericKubernetesClient; | ||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
public abstract class AbstractKubernetesTestResource<T> 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(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); | ||
systemProps.put(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); | ||
systemProps.put(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); | ||
systemProps.put(Config.KUBERNETES_HTTP2_DISABLE, "true"); | ||
|
||
server = createServer(); | ||
initServer(); | ||
|
||
try (GenericKubernetesClient<?> client = getClient()) { | ||
systemProps.put(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl()); | ||
} | ||
|
||
configureServer(); | ||
|
||
return systemProps; | ||
} | ||
|
||
protected abstract GenericKubernetesClient<?> getClient(); | ||
|
||
/** | ||
* Can be used by subclasses in order to | ||
* setup the mock server before the Quarkus application starts | ||
*/ | ||
protected void configureServer() { | ||
} | ||
|
||
protected void initServer() { | ||
} | ||
|
||
protected abstract T createServer(); | ||
|
||
protected boolean useHttps() { | ||
return Boolean.getBoolean("quarkus.kubernetes-client.test.https"); | ||
} | ||
|
||
@Override | ||
public void inject(Object testInstance) { | ||
Class<?> c = testInstance.getClass(); | ||
Class<? extends Annotation> annotation = getInjectionAnnotation(); | ||
Class<?> injectedClass = getInjectedClass(); | ||
while (c != Object.class) { | ||
for (Field f : c.getDeclaredFields()) { | ||
if (f.getAnnotation(annotation) != null) { | ||
if (!injectedClass.isAssignableFrom(f.getType())) { | ||
throw new RuntimeException(annotation + " can only be used on fields of type " + injectedClass); | ||
} | ||
|
||
f.setAccessible(true); | ||
try { | ||
f.set(testInstance, server); | ||
return; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
c = c.getSuperclass(); | ||
} | ||
} | ||
|
||
protected abstract Class<?> getInjectedClass(); | ||
|
||
protected abstract Class<? extends Annotation> getInjectionAnnotation(); | ||
|
||
} |
83 changes: 32 additions & 51 deletions
83
...ent/src/main/java/io/quarkus/test/kubernetes/client/KubernetesMockServerTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,62 @@ | ||
package io.quarkus.test.kubernetes.client; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.lang.annotation.Annotation; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.NamespacedKubernetesClient; | ||
import io.fabric8.kubernetes.client.GenericKubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; | ||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
public class KubernetesMockServerTestResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
private KubernetesMockServer mockServer; | ||
public class KubernetesMockServerTestResource extends AbstractKubernetesTestResource<KubernetesMockServer> { | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
final Map<String, String> systemProps = new HashMap<>(); | ||
systemProps.put(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "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"); | ||
systemProps.put(Config.KUBERNETES_HTTP2_DISABLE, "true"); | ||
|
||
mockServer = createMockServer(); | ||
mockServer.init(); | ||
try (NamespacedKubernetesClient client = mockServer.createClient()) { | ||
systemProps.put(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl()); | ||
} | ||
protected GenericKubernetesClient<?> getClient() { | ||
return server.createClient(); | ||
} | ||
|
||
configureMockServer(mockServer); | ||
@Override | ||
protected void initServer() { | ||
server.init(); | ||
} | ||
|
||
return systemProps; | ||
@Override | ||
protected KubernetesMockServer createServer() { | ||
return createMockServer(); | ||
} | ||
|
||
/** | ||
* @deprecated use {@link #createServer()} | ||
*/ | ||
@Deprecated | ||
protected KubernetesMockServer createMockServer() { | ||
return new KubernetesMockServer(useHttps()); | ||
} | ||
|
||
@Override | ||
protected void configureServer() { | ||
configureMockServer(server); | ||
} | ||
|
||
/** | ||
* Can be used by subclasses of {@code KubernetesMockServerTestResource} in order to | ||
* setup the mock server before the Quarkus application starts | ||
* @deprecated use {@link #configureServer()} | ||
*/ | ||
@Deprecated | ||
public void configureMockServer(KubernetesMockServer mockServer) { | ||
|
||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (mockServer != null) { | ||
mockServer.destroy(); | ||
if (server != null) { | ||
server.destroy(); | ||
server = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void inject(Object testInstance) { | ||
Class<?> c = testInstance.getClass(); | ||
while (c != Object.class) { | ||
for (Field f : c.getDeclaredFields()) { | ||
if (f.getAnnotation(MockServer.class) != null) { | ||
if (!KubernetesMockServer.class.isAssignableFrom(f.getType())) { | ||
throw new RuntimeException("@MockServer can only be used on fields of type KubernetesMockServer"); | ||
} | ||
|
||
f.setAccessible(true); | ||
try { | ||
f.set(testInstance, mockServer); | ||
return; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
c = c.getSuperclass(); | ||
} | ||
protected Class<?> getInjectedClass() { | ||
return KubernetesMockServer.class; | ||
} | ||
|
||
protected boolean useHttps() { | ||
return Boolean.getBoolean("quarkus.kubernetes-client.test.https"); | ||
@Override | ||
protected Class<? extends Annotation> getInjectionAnnotation() { | ||
return MockServer.class; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-client/src/main/java/io/quarkus/test/kubernetes/client/KubernetesServerTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.test.kubernetes.client; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import io.fabric8.kubernetes.client.GenericKubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesServer; | ||
|
||
public class KubernetesServerTestResource extends AbstractKubernetesTestResource<KubernetesServer> { | ||
|
||
@Override | ||
protected GenericKubernetesClient<?> getClient() { | ||
return server.getClient(); | ||
} | ||
|
||
@Override | ||
protected void initServer() { | ||
server.before(); | ||
} | ||
|
||
@Override | ||
protected KubernetesServer createServer() { | ||
return new KubernetesServer(useHttps(), true); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (server != null) { | ||
server.after(); | ||
server = null; | ||
} | ||
} | ||
|
||
@Override | ||
protected Class<?> getInjectedClass() { | ||
return KubernetesServer.class; | ||
} | ||
|
||
@Override | ||
protected Class<? extends Annotation> getInjectionAnnotation() { | ||
return Server.class; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test-framework/kubernetes-client/src/main/java/io/quarkus/test/kubernetes/client/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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; | ||
|
||
/** | ||
* Used to specify that the field should be injected with the mock Kubernetes API server | ||
* Can only be used on type {@link KubernetesServer} | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Server { | ||
|
||
} |