diff --git a/test-frame-log-collector/README.md b/test-frame-log-collector/README.md index a637707..872e031 100644 --- a/test-frame-log-collector/README.md +++ b/test-frame-log-collector/README.md @@ -169,33 +169,33 @@ the logs path will then look like this: ``` The tree path will look similarly to above examples, there will be folders for Namespaces matching the specified labels. -### Global log collector -`CollectLogs` is an annotation which can handle collecting logs which user want automatically in case of test failure or before/after failure. -It gets configuration passed into `GlobalLogCollector` and call collecting in proper callbacks. +### MustGather +`MustGather` is an annotation which can handle collecting logs which user want automatically in case of test failure or before/after failure. +It gets configuration passed into `MustGatherController` and call collecting in proper callbacks. -Register `GlobalLogCollector` handlers and configure +Register `MustGatherController` handlers and configure ```java import io.skodjob.testframe.LogCollectorBuilder; -import io.skodjob.testframe.annotations.CollectLogs; -import io.skodjob.testframe.listeners.GlobalLogCollector; +import io.skodjob.testframe.annotations.MustGather; +import io.skodjob.testframe.listeners.MustGatherController; import io.skodjob.testframe.resources.KubeResourceManager; import org.junit.jupiter.api.Test; -@CollectLogs +@MustGather class TestClass() { static { // Setup global log collector and handlers - GlobalLogCollector.setupGlobalLogCollector(new LogCollectorBuilder() + MustGatherController.setupMustGatherController(new LogCollectorBuilder() .withNamespacedResources("sa", "deployment", "configmaps", "secret") .withClusterWideResources("nodes") .withKubeClient(KubeResourceManager.getKubeClient()) .withKubeCmdClient(KubeResourceManager.getKubeCmdClient()) .withRootFolderPath("/some-path/path/") .build()); - GlobalLogCollector.addLogCallback(() -> { - GlobalLogCollector.getGlobalLogCollector().collectFromNamespaces("test-namespace", "test-namespace-2"); - GlobalLogCollector.getGlobalLogCollector().collectClusterWideResources(); + MustGatherController.setMustGatherCallback(() -> { + MustGatherController.getMustGatherController().collectFromNamespaces("test-namespace", "test-namespace-2"); + MustGatherController.getMustGatherController().collectClusterWideResources(); }); } diff --git a/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/CollectLogs.java b/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java similarity index 79% rename from test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/CollectLogs.java rename to test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java index adcda8b..4b82bb7 100644 --- a/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/CollectLogs.java +++ b/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java @@ -4,7 +4,7 @@ */ package io.skodjob.testframe.annotations; -import io.skodjob.testframe.listeners.GlobalLogCollector; +import io.skodjob.testframe.listeners.MustGatherController; import org.junit.jupiter.api.extension.ExtendWith; import java.lang.annotation.ElementType; @@ -19,11 +19,11 @@ * when test of prepare and post phase fails in JUnit tests. * It is applied at the class level. *

- * It uses the {@link GlobalLogCollector} + * It uses the {@link MustGatherController} */ @Target(ElementType.TYPE) @Retention(RUNTIME) @Inherited -@ExtendWith(GlobalLogCollector.class) -public @interface CollectLogs { +@ExtendWith(MustGatherController.class) +public @interface MustGather { } diff --git a/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/GlobalLogCollector.java b/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java similarity index 75% rename from test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/GlobalLogCollector.java rename to test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java index 0ada058..d6c57c9 100644 --- a/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/GlobalLogCollector.java +++ b/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java @@ -5,7 +5,7 @@ package io.skodjob.testframe.listeners; import io.skodjob.testframe.LogCollector; -import io.skodjob.testframe.annotations.CollectLogs; +import io.skodjob.testframe.annotations.MustGather; import io.skodjob.testframe.interfaces.ThrowableRunner; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -13,21 +13,18 @@ import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; -import java.util.LinkedList; -import java.util.List; - /** * Represents global log collector which is automatically called on text fail or error even in setup or post methods */ -public class GlobalLogCollector implements TestExecutionExceptionHandler, LifecycleMethodExecutionExceptionHandler { - private static final Logger LOGGER = LogManager.getLogger(GlobalLogCollector.class); - private static LogCollector globalInstance; - private static final List COLLECT_CALLBACKS = new LinkedList<>(); +public class MustGatherController implements TestExecutionExceptionHandler, LifecycleMethodExecutionExceptionHandler { + private static final Logger LOGGER = LogManager.getLogger(MustGatherController.class); + private static LogCollector mustGatherInstance; + private static ThrowableRunner collectCallback; /** * Private constructor */ - private GlobalLogCollector() { + private MustGatherController() { // empty constructor } @@ -40,7 +37,7 @@ private GlobalLogCollector() { */ @Override public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable { - saveKubeState(); + saveKubeState(extensionContext); throw throwable; } @@ -53,7 +50,7 @@ public void handleTestExecutionException(ExtensionContext extensionContext, Thro */ @Override public void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable)throws Throwable { - saveKubeState(); + saveKubeState(context); LifecycleMethodExecutionExceptionHandler.super.handleBeforeAllMethodExecutionException(context, throwable); } @@ -67,7 +64,7 @@ public void handleBeforeAllMethodExecutionException(ExtensionContext context, Th @Override public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { - saveKubeState(); + saveKubeState(context); LifecycleMethodExecutionExceptionHandler.super.handleBeforeEachMethodExecutionException(context, throwable); } @@ -81,7 +78,7 @@ public void handleBeforeEachMethodExecutionException(ExtensionContext context, @Override public void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { - saveKubeState(); + saveKubeState(context); LifecycleMethodExecutionExceptionHandler.super.handleAfterEachMethodExecutionException(context, throwable); } @@ -94,17 +91,17 @@ public void handleAfterEachMethodExecutionException(ExtensionContext context, */ @Override public void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { - saveKubeState(); + saveKubeState(context); LifecycleMethodExecutionExceptionHandler.super.handleAfterAllMethodExecutionException(context, throwable); } /** - * Setup globalLogCollector which is automatically used within {@link CollectLogs} annotation + * Setup globalLogCollector which is automatically used within {@link MustGather} annotation * * @param globalLogCollector log collector instance */ - public static void setupGlobalLogCollector(LogCollector globalLogCollector) { - globalInstance = globalLogCollector; + public static void setupMustGatherController(LogCollector globalLogCollector) { + mustGatherInstance = globalLogCollector; } /** @@ -112,11 +109,11 @@ public static void setupGlobalLogCollector(LogCollector globalLogCollector) { * * @return global log collector instance */ - public static LogCollector getGlobalLogCollector() { - if (globalInstance == null) { + public static LogCollector getMustGatherController() { + if (mustGatherInstance == null) { throw new NullPointerException("Global log collector is not initialized"); } - return globalInstance; + return mustGatherInstance; } /** @@ -124,14 +121,16 @@ public static LogCollector getGlobalLogCollector() { * * @param callback callback method with log collecting */ - public static void addLogCallback(ThrowableRunner callback) { - COLLECT_CALLBACKS.add(callback); + public static void setMustGatherCallback(ThrowableRunner callback) { + collectCallback = callback; } - private void saveKubeState() { + private void saveKubeState(ExtensionContext context) { try { - for (ThrowableRunner runner : COLLECT_CALLBACKS) { - runner.run(); + if (collectCallback != null) { + collectCallback.run(); + } else { + LOGGER.warn("No logCallback defined"); } } catch (Exception ex) { LOGGER.error("Cannot collect all data", ex); diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/AbstractIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/AbstractIT.java index 155e94f..abd4c57 100644 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/AbstractIT.java +++ b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/AbstractIT.java @@ -5,8 +5,8 @@ package io.skodjob.testframe.test.integration; import io.skodjob.testframe.LogCollectorBuilder; -import io.skodjob.testframe.annotations.CollectLogs; -import io.skodjob.testframe.listeners.GlobalLogCollector; +import io.skodjob.testframe.annotations.MustGather; +import io.skodjob.testframe.listeners.MustGatherController; import io.skodjob.testframe.test.integration.helpers.GlobalLogCollectorTestHandler; import io.skodjob.testframe.utils.LoggerUtils; import io.skodjob.testframe.annotations.ResourceManager; @@ -26,7 +26,7 @@ @ExtendWith(GlobalLogCollectorTestHandler.class) // For testing purpose @ResourceManager -@CollectLogs +@MustGather @TestVisualSeparator public abstract class AbstractIT { static AtomicBoolean isCreateHandlerCalled = new AtomicBoolean(false); @@ -60,16 +60,16 @@ public abstract class AbstractIT { }); // Setup global log collector and handlers - GlobalLogCollector.setupGlobalLogCollector(new LogCollectorBuilder() + MustGatherController.setupMustGatherController(new LogCollectorBuilder() .withNamespacedResources("sa", "deployment", "configmaps", "secret") .withClusterWideResources("nodes") .withKubeClient(KubeResourceManager.getKubeClient()) .withKubeCmdClient(KubeResourceManager.getKubeCmdClient()) .withRootFolderPath(LOG_DIR.toString()) .build()); - GlobalLogCollector.addLogCallback(() -> { - GlobalLogCollector.getGlobalLogCollector().collectFromNamespaces("default"); - GlobalLogCollector.getGlobalLogCollector().collectClusterWideResources(); + MustGatherController.setMustGatherCallback(() -> { + MustGatherController.getMustGatherController().collectFromNamespaces("default"); + MustGatherController.getMustGatherController().collectClusterWideResources(); }); } diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/GlobalLogCollectorIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java similarity index 92% rename from test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/GlobalLogCollectorIT.java rename to test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java index a26be4d..10a4f8e 100644 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/GlobalLogCollectorIT.java +++ b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java @@ -14,7 +14,7 @@ import static org.junit.jupiter.api.Assertions.fail; @TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class GlobalLogCollectorIT extends AbstractIT { +public class MustGatherControllerIT extends AbstractIT { @Test void testGlobalLogCollector() { fail("Expected issue");