Skip to content

Commit

Permalink
Rename GlobalLogCollector to MustGather and use only one callback
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <kornys@outlook.com>
  • Loading branch information
kornys committed Jul 11, 2024
1 parent 1c7e032 commit 7eaf016
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
14 changes: 7 additions & 7 deletions test-frame-log-collector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,25 @@ Register `GlobalLogCollector` 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.setupGlobalLogCollector(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.setLogCallback(() -> {
MustGatherController.getGlobalLogCollector().collectFromNamespaces("test-namespace", "test-namespace-2");
MustGatherController.getGlobalLogCollector().collectClusterWideResources();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,11 +19,11 @@
* when test of prepare and post phase fails in JUnit tests.
* It is applied at the class level.
* <p>
* 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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@
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;
import org.junit.jupiter.api.extension.ExtensionContext;
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);
public class MustGatherController implements TestExecutionExceptionHandler, LifecycleMethodExecutionExceptionHandler {
private static final Logger LOGGER = LogManager.getLogger(MustGatherController.class);
private static LogCollector globalInstance;
private static final List<ThrowableRunner> COLLECT_CALLBACKS = new LinkedList<>();
private static ThrowableRunner collectCallback;

/**
* Private constructor
*/
private GlobalLogCollector() {
private MustGatherController() {
// empty constructor
}

Expand All @@ -40,7 +37,7 @@ private GlobalLogCollector() {
*/
@Override
public void handleTestExecutionException(ExtensionContext extensionContext, Throwable throwable) throws Throwable {
saveKubeState();
saveKubeState(extensionContext);
throw throwable;
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -94,12 +91,12 @@ 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
*/
Expand All @@ -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 setLogCallback(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@

@ExtendWith(GlobalLogCollectorTestHandler.class) // For testing purpose
@ResourceManager
@CollectLogs
@MustGather
@TestVisualSeparator
public abstract class AbstractIT {
static AtomicBoolean isCreateHandlerCalled = new AtomicBoolean(false);
Expand Down Expand Up @@ -60,16 +60,16 @@ public abstract class AbstractIT {
});

// Setup global log collector and handlers
GlobalLogCollector.setupGlobalLogCollector(new LogCollectorBuilder()
MustGatherController.setupGlobalLogCollector(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.setLogCallback(() -> {
MustGatherController.getGlobalLogCollector().collectFromNamespaces("default");
MustGatherController.getGlobalLogCollector().collectClusterWideResources();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 7eaf016

Please sign in to comment.