Skip to content

Commit

Permalink
Create log matcher for operator (#64)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Stejskal <[email protected]>
  • Loading branch information
Frawless authored Jan 17, 2024
1 parent 638332e commit b82fa4a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> {

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;
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/odh/test/framework/matchers/Matchers.java
Original file line number Diff line number Diff line change
@@ -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<String> logHasNoUnexpectedErrors() {
return new LogHasNoUnexpectedErrors();
}
}
9 changes: 3 additions & 6 deletions src/main/java/io/odh/test/utils/UpgradeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit b82fa4a

Please sign in to comment.