Skip to content

Commit

Permalink
feat: @KubernetesTest/KubernetesNamespacedTestExtension creates an ep…
Browse files Browse the repository at this point in the history
…hemeral Namespace optionally (can opt-out)

Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jan 5, 2023
1 parent c934dc6 commit 40fa963
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Fix #4694: [java-generator] Option to override the package name of the generated code.
* Fix #4720: interceptors close any response body if the response is not a 2xx response.
* Fix #4734: @KubernetesTest annotation can be used in base test classes
* Fix #4734: @KubernetesTest creates an ephemeral Namespace optionally (can opt-out)

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.junit.jupiter;

import io.fabric8.junit.jupiter.api.KubernetesTest;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.ObjectReference;
Expand Down Expand Up @@ -45,9 +46,12 @@ public ExtensionContext.Namespace getNamespace() {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
final KubernetesClient client = new KubernetesClientBuilder().build();
getStore(context).put(Namespace.class, initNamespace(client));
getStore(context).put(KubernetesClient.class,
client.adapt(NamespacedKubernetesClient.class).inNamespace(getNamespace(context).getMetadata().getName()));
getStore(context).put(KubernetesClient.class, client);
if (shouldCreateNamespace(context)) {
getStore(context).put(Namespace.class, initNamespace(client));
getStore(context).put(KubernetesClient.class,
client.adapt(NamespacedKubernetesClient.class).inNamespace(getNamespace(context).getMetadata().getName()));
}
for (Field field : extractFields(context, KubernetesClient.class, f -> Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, null, getClient(context).adapt((Class<Client>) field.getType()));
}
Expand All @@ -69,7 +73,9 @@ public void beforeEach(ExtensionContext context) throws Exception {
@Override
public void afterAll(ExtensionContext context) {
final KubernetesClient client = getClient(context);
client.resource(getNamespace(context)).withGracePeriod(0L).delete();
if (shouldCreateNamespace(context)) {
client.resource(getNamespace(context)).withGracePeriod(0L).delete();
}
client.close();
}

Expand Down Expand Up @@ -109,6 +115,11 @@ private static Namespace initNamespace(KubernetesClient client) {
return namespace;
}

private boolean shouldCreateNamespace(ExtensionContext context) {
final KubernetesTest annotation = context.getRequiredTestClass().getAnnotation(KubernetesTest.class);
return annotation == null || annotation.createEphemeralNamespace();
}

private Namespace getNamespace(ExtensionContext context) {
final Namespace namespace = getStore(context).get(Namespace.class, Namespace.class);
if (namespace == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
* Enables and configures the {@link KubernetesNamespacedTestExtension} extension.
*
* <p>
* Creates a namespace and configures a {@link KubernetesClient} instance that will
* Creates a {@link KubernetesClient} instance that will
* be automatically injected into tests.
*
* <p>
* Optionally, creates a Namespace for the tests and configures the client to use it. The Namespace
* is deleted after the test suite execution.
*
* <pre>{@code
* &#64;KubernetesTest
* class MyTest {
Expand All @@ -46,4 +50,8 @@
@Retention(RUNTIME)
@ExtendWith(KubernetesNamespacedTestExtension.class)
public @interface KubernetesTest {
/**
* Create an ephemeral Namespace for the test.
*/
boolean createEphemeralNamespace() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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

@KubernetesTest
@KubernetesTest(createEphemeralNamespace = false)
// Due to the way the extensions interact with the test, this test class can't be parameterized
class KubernetesTestTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import io.fabric8.kubernetes.client.KubernetesClient;

@KubernetesTest
@KubernetesTest(createEphemeralNamespace = false)
public class KubernetesTestWithSuperClass {

static KubernetesClient staticSuperClient;
Expand Down

0 comments on commit 40fa963

Please sign in to comment.