From e5f0449aaef4f5842c2f0308ddc1e66dec63b458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kr=C3=A1l?= <53821852+im-konge@users.noreply.github.com> Date: Tue, 5 Mar 2024 10:13:28 +0100 Subject: [PATCH] Add logger (logback) to test-frame (#27) Signed-off-by: Lukas Kral --- pom.xml | 8 +++++ test-frame-common/pom.xml | 6 ++++ .../resources/KubeResourceManager.java | 2 +- .../java/io/skodjob/testframe/wait/Wait.java | 8 ++--- .../src/main/resources/logback.xml | 31 +++++++++++++++++++ test-frame-test/pom.xml | 6 ++++ .../testframe/test/unit/UnitTests.java | 5 ++- .../src/test/resources/logback.xml | 31 +++++++++++++++++++ 8 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 test-frame-common/src/main/resources/logback.xml create mode 100644 test-frame-test/src/test/resources/logback.xml diff --git a/pom.xml b/pom.xml index 072ae1d..f07bff6 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,7 @@ 17 6.10.0 + 1.4.12 4.7.3.4 3.3.0 @@ -171,6 +172,13 @@ maven-failsafe-plugin ${maven.surefire.version} + + + ch.qos.logback + logback-classic + runtime + ${logback.version} + diff --git a/test-frame-common/pom.xml b/test-frame-common/pom.xml index 7f65cb8..d23a220 100644 --- a/test-frame-common/pom.xml +++ b/test-frame-common/pom.xml @@ -85,5 +85,11 @@ org.junit.jupiter junit-jupiter-api + + + ch.qos.logback + logback-classic + runtime + \ No newline at end of file diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java b/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java index bf5943e..f047705 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/resources/KubeResourceManager.java @@ -271,7 +271,7 @@ public final boolean waitResourceCondition(T resource, R ResourceType type = findResourceType(resource); boolean[] resourceReady = new boolean[1]; - Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s", + Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s", condition.getConditionName(), resource.getKind(), resource.getMetadata().getName()), TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, TestFrameConstants.GLOBAL_TIMEOUT, () -> { diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/wait/Wait.java b/test-frame-common/src/main/java/io/skodjob/testframe/wait/Wait.java index 0356e38..e22725e 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/wait/Wait.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/wait/Wait.java @@ -58,7 +58,7 @@ public static void until(String description, long pollIntervalMs, long timeoutMs */ public static void until(String description, long pollIntervalMs, long timeoutMs, BooleanSupplier ready, Runnable onTimeout) { - System.out.println("Waiting for " + description); + LOGGER.info("Waiting for: {}", description); long deadline = System.currentTimeMillis() + timeoutMs; String exceptionMessage = null; @@ -82,7 +82,7 @@ public static void until(String description, long pollIntervalMs, long timeoutMs if (++exceptionCount == exceptionAppearanceCount && exceptionMessage != null && exceptionMessage.equals(previousExceptionMessage)) { - System.out.println("While waiting for " + description + " exception occurred: " + exceptionMessage); + LOGGER.info("While waiting for: {} exception occurred: {}", description, exceptionMessage); // log the stacktrace e.printStackTrace(new PrintWriter(stackTraceError)); } else if (exceptionMessage != null && !exceptionMessage.equals(previousExceptionMessage) @@ -98,11 +98,11 @@ public static void until(String description, long pollIntervalMs, long timeoutMs } if (timeLeft <= 0) { if (exceptionCount > 1) { - System.out.println("Exception waiting for " + description + ", " + exceptionMessage); + LOGGER.error("Exception waiting for: {}, {}", description, exceptionMessage); if (!stackTraceError.toString().isEmpty()) { // printing handled stacktrace - System.out.println(stackTraceError); + LOGGER.error(String.valueOf(stackTraceError)); } } onTimeout.run(); diff --git a/test-frame-common/src/main/resources/logback.xml b/test-frame-common/src/main/resources/logback.xml new file mode 100644 index 0000000..839a83a --- /dev/null +++ b/test-frame-common/src/main/resources/logback.xml @@ -0,0 +1,31 @@ + + + + + + + + + %d{yyyy-MM-dd'T'HH:mm:ss,GMT} %highlight(%-5p) [%c{0}:%L] %m%n + + + + + ${LOG_DIR}/testFile.log + true + true + + %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z',GMT} [%thread] %-5p [%c{0}:%L] %m%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/test-frame-test/pom.xml b/test-frame-test/pom.xml index efb6f76..2a80921 100644 --- a/test-frame-test/pom.xml +++ b/test-frame-test/pom.xml @@ -73,6 +73,12 @@ maven-failsafe-plugin test + + + ch.qos.logback + logback-classic + test + diff --git a/test-frame-test/src/test/java/io/skodjob/testframe/test/unit/UnitTests.java b/test-frame-test/src/test/java/io/skodjob/testframe/test/unit/UnitTests.java index e90ca80..246cec2 100644 --- a/test-frame-test/src/test/java/io/skodjob/testframe/test/unit/UnitTests.java +++ b/test-frame-test/src/test/java/io/skodjob/testframe/test/unit/UnitTests.java @@ -8,15 +8,18 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; //TODO Implement mock kube to be able to work with RM @EnableKubernetesMockClient(crud = true) public class UnitTests { private KubernetesClient kubernetesClient; private KubernetesMockServer server; + private static final Logger LOGGER = LoggerFactory.getLogger(UnitTests.class); @Test void tmpTest() { - System.out.println("Placeholder for test"); + LOGGER.info("Placeholder for test"); } } diff --git a/test-frame-test/src/test/resources/logback.xml b/test-frame-test/src/test/resources/logback.xml new file mode 100644 index 0000000..839a83a --- /dev/null +++ b/test-frame-test/src/test/resources/logback.xml @@ -0,0 +1,31 @@ + + + + + + + + + %d{yyyy-MM-dd'T'HH:mm:ss,GMT} %highlight(%-5p) [%c{0}:%L] %m%n + + + + + ${LOG_DIR}/testFile.log + true + true + + %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z',GMT} [%thread] %-5p [%c{0}:%L] %m%n + + + + + + + + + + + + + \ No newline at end of file