-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduced @WithOpenShiftTestServer annotation
Allows to easily run QuarkusTests with OpenShiftMockserver Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
13 changed files
with
209 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
41 changes: 41 additions & 0 deletions
41
...ernetes-client/src/test/java/io/quarkus/it/kubernetes/client/OpenShiftTestServerTest.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,41 @@ | ||
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")); | ||
} | ||
} |
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
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
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
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
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
67 changes: 67 additions & 0 deletions
67
...t-client/src/main/java/io/quarkus/test/kubernetes/client/OpenShiftServerTestResource.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,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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...openshift-client/src/main/java/io/quarkus/test/kubernetes/client/OpenShiftTestServer.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,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 { | ||
} |
41 changes: 41 additions & 0 deletions
41
...shift-client/src/main/java/io/quarkus/test/kubernetes/client/WithOpenShiftTestServer.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,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) { | ||
} | ||
} | ||
} |