Skip to content

Commit

Permalink
refactor: Kubernetes JUnit extension is more granular
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jan 10, 2023
1 parent 7d03c3a commit bf4d07f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.junit.jupiter;

import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class KubernetesExtension implements HasKubernetesClient, BeforeAllCallback, BeforeEachCallback {

@Override
public void beforeAll(ExtensionContext context) throws Exception {
for (Field field : extractFields(context, KubernetesClient.class, f -> Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, null, getClient(context).adapt((Class<Client>) field.getType()));
}
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
for (Field field : extractFields(context, KubernetesClient.class, f -> !Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, context.getRequiredTestInstance(), getClient(context).adapt((Class<Client>) field.getType()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* Must be used in conjunction with {@link KubernetesNamespacedTestExtension} to be able to consume a KubernetesClient
* Must be used in conjunction with {@link KubernetesExtension} to be able to consume a KubernetesClient
*/
public class LoadKubernetesManifestsExtension implements HasKubernetesClient, BeforeAllCallback, AfterAllCallback {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
Expand All @@ -35,8 +34,7 @@
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class KubernetesNamespacedTestExtension
implements HasKubernetesClient, BeforeAllCallback, BeforeEachCallback, AfterAllCallback {
public class NamespaceExtension implements HasKubernetesClient, BeforeAllCallback, BeforeEachCallback, AfterAllCallback {

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Expand All @@ -47,19 +45,13 @@ public void beforeAll(ExtensionContext context) throws Exception {
getStore(context).put(KubernetesClient.class,
client.adapt(NamespacedKubernetesClient.class).inNamespace(getKubernetesNamespace(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()));
}
for (Field field : extractFields(context, Namespace.class, f -> Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, null, getKubernetesNamespace(context));
}
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
for (Field field : extractFields(context, KubernetesClient.class, f -> !Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, context.getRequiredTestInstance(), getClient(context).adapt((Class<Client>) field.getType()));
}
for (Field field : extractFields(context, Namespace.class, f -> !Modifier.isStatic(f.getModifiers()))) {
setFieldValue(field, context.getRequiredTestInstance(), getKubernetesNamespace(context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package io.fabric8.junit.jupiter.api;

import io.fabric8.junit.jupiter.KubernetesNamespacedTestExtension;
import io.fabric8.junit.jupiter.KubernetesExtension;
import io.fabric8.junit.jupiter.NamespaceExtension;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -28,7 +29,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Enables and configures the {@link KubernetesNamespacedTestExtension} extension.
* Enables and configures the {@link KubernetesExtension} extension.
*
* <p>
* Creates a {@link KubernetesClient} instance that will
Expand All @@ -48,7 +49,8 @@
*/
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@ExtendWith(KubernetesNamespacedTestExtension.class)
@ExtendWith(NamespaceExtension.class)
@ExtendWith(KubernetesExtension.class)
public @interface KubernetesTest {
/**
* Create an ephemeral Namespace for the test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package io.fabric8.junit.jupiter.api;

import io.fabric8.junit.jupiter.KubernetesNamespacedTestExtension;
import io.fabric8.junit.jupiter.KubernetesExtension;
import io.fabric8.junit.jupiter.LoadKubernetesManifestsExtension;
import io.fabric8.junit.jupiter.NamespaceExtension;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.Retention;
Expand All @@ -36,7 +37,8 @@
*/
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@ExtendWith(KubernetesNamespacedTestExtension.class)
@ExtendWith(NamespaceExtension.class)
@ExtendWith(KubernetesExtension.class)
@ExtendWith(LoadKubernetesManifestsExtension.class)
public @interface LoadKubernetesManifests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.fabric8.junit.jupiter.KubernetesNamespacedTestExtension
io.fabric8.junit.jupiter.NamespaceExtension
io.fabric8.junit.jupiter.KubernetesExtension

0 comments on commit bf4d07f

Please sign in to comment.