-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create log matcher for operator (#64)
Signed-off-by: Jakub Stejskal <[email protected]>
- Loading branch information
Showing
3 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/io/odh/test/framework/matchers/LogHasNoUnexpectedErrors.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,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
21
src/main/java/io/odh/test/framework/matchers/Matchers.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,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(); | ||
} | ||
} |
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