diff --git a/test-frame-log-collector/README.md b/test-frame-log-collector/README.md index 872e031..da3d56f 100644 --- a/test-frame-log-collector/README.md +++ b/test-frame-log-collector/README.md @@ -169,43 +169,6 @@ 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. -### 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 `MustGatherController` handlers and configure - -```java -import io.skodjob.testframe.LogCollectorBuilder; -import io.skodjob.testframe.annotations.MustGather; -import io.skodjob.testframe.listeners.MustGatherController; -import io.skodjob.testframe.resources.KubeResourceManager; -import org.junit.jupiter.api.Test; - -@MustGather -class TestClass() { - static { - // Setup global log collector and handlers - MustGatherController.setupMustGatherController(new LogCollectorBuilder() - .withNamespacedResources("sa", "deployment", "configmaps", "secret") - .withClusterWideResources("nodes") - .withKubeClient(KubeResourceManager.getKubeClient()) - .withKubeCmdClient(KubeResourceManager.getKubeCmdClient()) - .withRootFolderPath("/some-path/path/") - .build()); - MustGatherController.setMustGatherCallback(() -> { - MustGatherController.getMustGatherController().collectFromNamespaces("test-namespace", "test-namespace-2"); - MustGatherController.getMustGatherController().collectClusterWideResources(); - }); - } - - @Test - void test() { - ... - } -} -``` - ### Specifying additional folder path In case that you would like to collect the logs to additional sub-directories of your root folder, the `LogCollector` contains diff --git a/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java b/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java deleted file mode 100644 index 4b82bb7..0000000 --- a/test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/MustGather.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright Skodjob authors. - * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). - */ -package io.skodjob.testframe.annotations; - -import io.skodjob.testframe.listeners.MustGatherController; -import org.junit.jupiter.api.extension.ExtendWith; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -/** - * This annotation is used to automatically call log collecting - * when test of prepare and post phase fails in JUnit tests. - * It is applied at the class level. - *
- * It uses the {@link MustGatherController} - */ -@Target(ElementType.TYPE) -@Retention(RUNTIME) -@Inherited -@ExtendWith(MustGatherController.class) -public @interface MustGather { -} diff --git a/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java b/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java deleted file mode 100644 index d6c57c9..0000000 --- a/test-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/MustGatherController.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright Skodjob authors. - * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). - */ -package io.skodjob.testframe.listeners; - -import io.skodjob.testframe.LogCollector; -import io.skodjob.testframe.annotations.MustGather; -import io.skodjob.testframe.interfaces.ThrowableRunner; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler; -import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; - -/** - * Represents global log collector which is automatically called on text fail or error even in setup or post methods - */ -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 MustGatherController() { - // empty constructor - } - - /** - * Handler when test fails - * - * @param extensionContext extension context - * @param throwable throwable - * @throws Throwable original throwable - */ - @Override - public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable { - saveKubeState(extensionContext); - throw throwable; - } - - /** - * Handles beforeAll exception - * - * @param context extensionContext - * @param throwable throwable - * @throws Throwable original throwable - */ - @Override - public void handleBeforeAllMethodExecutionException(ExtensionContext context, Throwable throwable)throws Throwable { - saveKubeState(context); - LifecycleMethodExecutionExceptionHandler.super.handleBeforeAllMethodExecutionException(context, throwable); - } - - /** - * Handles beforeEach exception - * - * @param context extensionContext - * @param throwable throwable - * @throws Throwable original throwable - */ - @Override - public void handleBeforeEachMethodExecutionException(ExtensionContext context, - Throwable throwable) throws Throwable { - saveKubeState(context); - LifecycleMethodExecutionExceptionHandler.super.handleBeforeEachMethodExecutionException(context, throwable); - } - - /** - * Handles afterEach exception - * - * @param context extensionContext - * @param throwable throwable - * @throws Throwable original throwable - */ - @Override - public void handleAfterEachMethodExecutionException(ExtensionContext context, - Throwable throwable) throws Throwable { - saveKubeState(context); - LifecycleMethodExecutionExceptionHandler.super.handleAfterEachMethodExecutionException(context, throwable); - } - - /** - * Handles afterAll exception - * - * @param context extensionContext - * @param throwable throwable - * @throws Throwable original throwable - */ - @Override - public void handleAfterAllMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { - saveKubeState(context); - LifecycleMethodExecutionExceptionHandler.super.handleAfterAllMethodExecutionException(context, throwable); - } - - /** - * Setup globalLogCollector which is automatically used within {@link MustGather} annotation - * - * @param globalLogCollector log collector instance - */ - public static void setupMustGatherController(LogCollector globalLogCollector) { - mustGatherInstance = globalLogCollector; - } - - /** - * Returns globalLogCollector instance - * - * @return global log collector instance - */ - public static LogCollector getMustGatherController() { - if (mustGatherInstance == null) { - throw new NullPointerException("Global log collector is not initialized"); - } - return mustGatherInstance; - } - - /** - * Adds callback for running log collecting - * - * @param callback callback method with log collecting - */ - public static void setMustGatherCallback(ThrowableRunner callback) { - collectCallback = callback; - } - - private void saveKubeState(ExtensionContext context) { - try { - 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 abd4c57..710077b 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 @@ -4,10 +4,6 @@ */ package io.skodjob.testframe.test.integration; -import io.skodjob.testframe.LogCollectorBuilder; -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; import io.skodjob.testframe.annotations.TestVisualSeparator; @@ -16,7 +12,6 @@ import io.skodjob.testframe.resources.NamespaceType; import io.skodjob.testframe.resources.ServiceAccountType; import io.skodjob.testframe.utils.KubeUtils; -import org.junit.jupiter.api.extension.ExtendWith; import java.nio.file.Path; import java.nio.file.Paths; @@ -24,9 +19,7 @@ import java.time.format.DateTimeFormatter; import java.util.concurrent.atomic.AtomicBoolean; -@ExtendWith(GlobalLogCollectorTestHandler.class) // For testing purpose @ResourceManager -@MustGather @TestVisualSeparator public abstract class AbstractIT { static AtomicBoolean isCreateHandlerCalled = new AtomicBoolean(false); @@ -58,19 +51,6 @@ public abstract class AbstractIT { LoggerUtils.logResource("Deleted", r); } }); - - // Setup global log collector and handlers - MustGatherController.setupMustGatherController(new LogCollectorBuilder() - .withNamespacedResources("sa", "deployment", "configmaps", "secret") - .withClusterWideResources("nodes") - .withKubeClient(KubeResourceManager.getKubeClient()) - .withKubeCmdClient(KubeResourceManager.getKubeCmdClient()) - .withRootFolderPath(LOG_DIR.toString()) - .build()); - MustGatherController.setMustGatherCallback(() -> { - MustGatherController.getMustGatherController().collectFromNamespaces("default"); - MustGatherController.getMustGatherController().collectClusterWideResources(); - }); } protected String nsName1 = "test"; diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java deleted file mode 100644 index 10a4f8e..0000000 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MustGatherControllerIT.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright Skodjob authors. - * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). - */ -package io.skodjob.testframe.test.integration; - -import org.apache.commons.io.FileUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; - -import java.io.IOException; - -import static org.junit.jupiter.api.Assertions.fail; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class MustGatherControllerIT extends AbstractIT { - @Test - void testGlobalLogCollector() { - fail("Expected issue"); - } - - @AfterEach - void clean() throws IOException { - FileUtils.deleteDirectory(LOG_DIR.toFile()); - } -} diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/helpers/GlobalLogCollectorTestHandler.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/helpers/GlobalLogCollectorTestHandler.java deleted file mode 100644 index f71e396..0000000 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/helpers/GlobalLogCollectorTestHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright Skodjob authors. - * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). - */ -package io.skodjob.testframe.test.integration.helpers; - -import io.skodjob.testframe.test.integration.AbstractIT; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * Override test failure for expected test issue in GlobalLogCollectorIT test - */ -public class GlobalLogCollectorTestHandler implements TestExecutionExceptionHandler { - - /** - * Check cause message and if it is from GlobalLogCollectorIT check if logs - * are collected and do not mark test as failure - * - * @param context extension context - * @param cause throwable object - * @throws Throwable throwable object - */ - @Override - public void handleTestExecutionException(ExtensionContext context, Throwable cause) throws Throwable { - if (cause.getMessage().contains("Expected issue")) { - assertTrue(AbstractIT.LOG_DIR.toFile().exists()); - assertTrue(AbstractIT.LOG_DIR.resolve("default").toFile().exists()); - assertTrue(AbstractIT.LOG_DIR.resolve("cluster-wide-resources").toFile().exists()); - } else { - throw cause; - } - } -}