From b82fa4aaf191a6c1e7c3282e51c90d883dd95dd9 Mon Sep 17 00:00:00 2001 From: Jakub Stejskal Date: Wed, 17 Jan 2024 08:21:27 +0100 Subject: [PATCH] Create log matcher for operator (#64) Signed-off-by: Jakub Stejskal --- .../matchers/LogHasNoUnexpectedErrors.java | 79 +++++++++++++++++++ .../odh/test/framework/matchers/Matchers.java | 21 +++++ .../java/io/odh/test/utils/UpgradeUtils.java | 9 +-- 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/odh/test/framework/matchers/LogHasNoUnexpectedErrors.java create mode 100644 src/main/java/io/odh/test/framework/matchers/Matchers.java diff --git a/src/main/java/io/odh/test/framework/matchers/LogHasNoUnexpectedErrors.java b/src/main/java/io/odh/test/framework/matchers/LogHasNoUnexpectedErrors.java new file mode 100644 index 00000000..2bcff2ce --- /dev/null +++ b/src/main/java/io/odh/test/framework/matchers/LogHasNoUnexpectedErrors.java @@ -0,0 +1,79 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.odh.test.framework.matchers; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * A LogHasNoUnexpectedErrors is custom matcher to check log form kubernetes client + * doesn't have any unexpected errors. + */ +public class LogHasNoUnexpectedErrors extends BaseMatcher { + + private static final Logger LOGGER = LoggerFactory.getLogger(LogHasNoUnexpectedErrors.class); + + @Override + public boolean matches(Object actualValue) { + if (!"".equals(actualValue)) { + if (actualValue.toString().contains("Unhandled Exception")) { + return false; + } + // This pattern is used for split each log ine with stack trace if it's there from some reasons + // It's match start of the line which contains date in format yyyy-mm-dd hh:mm:ss + String logLineSplitPattern = "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"; + for (String line : ((String) actualValue).split(logLineSplitPattern)) { + if (line.contains("DEBUG") || line.contains("WARN") || line.contains("INFO")) { + continue; + } + if (line.startsWith("java.lang.NullPointerException")) { + return false; + } + if (line.contains("NullPointer")) { + return false; + } + String lineLowerCase = line.toLowerCase(Locale.ENGLISH); + if (lineLowerCase.contains("error") || lineLowerCase.contains("exception")) { + boolean ignoreListResult = false; + for (LogIgnoreList value : LogIgnoreList.values()) { + Matcher m = Pattern.compile(value.name).matcher(line); + if (m.find()) { + ignoreListResult = true; + break; + } + } + if (!ignoreListResult) { + LOGGER.error(line); + return false; + } + } + } + return true; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("The log should not contain unexpected errors."); + } + + enum LogIgnoreList { + // This should be removed when https://issues.redhat.com/browse/RHOAIENG-1742 will be done + MISSING_SERVICE_MESH("servicemeshcontrolplanes.maistra.io\" not found"); + + final String name; + + LogIgnoreList(String name) { + this.name = name; + } + } +} diff --git a/src/main/java/io/odh/test/framework/matchers/Matchers.java b/src/main/java/io/odh/test/framework/matchers/Matchers.java new file mode 100644 index 00000000..89584228 --- /dev/null +++ b/src/main/java/io/odh/test/framework/matchers/Matchers.java @@ -0,0 +1,21 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.odh.test.framework.matchers; + +import org.hamcrest.Matcher; + +public class Matchers { + + private Matchers() { + } + + /** + * A matcher checks that log doesn't have unexpected errors + * @return The matcher. + */ + public static Matcher logHasNoUnexpectedErrors() { + return new LogHasNoUnexpectedErrors(); + } +} diff --git a/src/main/java/io/odh/test/utils/UpgradeUtils.java b/src/main/java/io/odh/test/utils/UpgradeUtils.java index ca19041b..8620ddba 100644 --- a/src/main/java/io/odh/test/utils/UpgradeUtils.java +++ b/src/main/java/io/odh/test/utils/UpgradeUtils.java @@ -6,19 +6,16 @@ import io.odh.test.framework.manager.ResourceManager; +import static io.odh.test.framework.matchers.Matchers.logHasNoUnexpectedErrors; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.core.IsNot.not; public class UpgradeUtils { public static void deploymentLogIsErrorEmpty(String namespace, String deploymentName) { - // Check that operator doesn't contains errors in logs + // Check that operator doesn't contain errors in logs String operatorLog = ResourceManager.getClient().getClient().apps().deployments() .inNamespace(namespace).withName(deploymentName).getLog(); - assertThat(operatorLog, not(containsString("error"))); - assertThat(operatorLog, not(containsString("Error"))); - assertThat(operatorLog, not(containsString("ERROR"))); + assertThat(operatorLog, logHasNoUnexpectedErrors()); } }