-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement GlobalLogCollector for automatic collecting logs when test …
…fails Signed-off-by: David Kornel <[email protected]>
- Loading branch information
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
test-frame-log-collector/src/main/java/io/skodjob/testframe/annotations/CollectLogs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.GlobalLogCollector; | ||
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. | ||
* <p> | ||
* It uses the {@link GlobalLogCollector} | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RUNTIME) | ||
@Inherited | ||
@ExtendWith(GlobalLogCollector.class) | ||
public @interface CollectLogs { | ||
} |
140 changes: 140 additions & 0 deletions
140
...-frame-log-collector/src/main/java/io/skodjob/testframe/listeners/GlobalLogCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* 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.CollectLogs; | ||
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); | ||
private static LogCollector globalInstance; | ||
private static final List<ThrowableRunner> COLLECT_CALLBACKS = new LinkedList<>(); | ||
|
||
/** | ||
* Private constructor | ||
*/ | ||
private GlobalLogCollector() { | ||
// 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(); | ||
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(); | ||
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(); | ||
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(); | ||
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(); | ||
LifecycleMethodExecutionExceptionHandler.super.handleAfterAllMethodExecutionException(context, throwable); | ||
} | ||
|
||
/** | ||
* Setup globalLogCollector which is automatically used within {@link CollectLogs} annotation | ||
* | ||
* @param globalLogCollector log collector instance | ||
*/ | ||
public static void setupGlobalLogCollector(LogCollector globalLogCollector) { | ||
globalInstance = globalLogCollector; | ||
} | ||
|
||
/** | ||
* Returns globalLogCollector instance | ||
* | ||
* @return global log collector instance | ||
*/ | ||
public static LogCollector getGlobalLogCollector() { | ||
if (globalInstance == null) { | ||
throw new NullPointerException("Global log collector is not initialized"); | ||
} | ||
return globalInstance; | ||
} | ||
|
||
/** | ||
* Adds callback for running log collecting | ||
* | ||
* @param callback callback method with log collecting | ||
*/ | ||
public static void addLogCallback(ThrowableRunner callback) { | ||
COLLECT_CALLBACKS.add(callback); | ||
} | ||
|
||
private void saveKubeState() { | ||
try { | ||
for (ThrowableRunner runner : COLLECT_CALLBACKS) { | ||
runner.run(); | ||
} | ||
} catch (Exception ex) { | ||
LOGGER.error("Cannot collect all data", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters