HDDS-235. Fix TestOzoneAuditLogger#verifyDefaultLogLevel.

Contributed by Xiaoyu Yao.
This commit is contained in:
Anu Engineer 2018-07-08 11:27:54 -07:00
parent 2f51cd60ef
commit 790c563511
1 changed files with 31 additions and 15 deletions

View File

@ -100,7 +100,7 @@ public class TestOzoneAuditLogger {
AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR); AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR);
AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR, AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR,
new Exception("test")); new Exception("test"));
verifyLog(null); verifyNoLog();
} }
/** /**
@ -110,22 +110,38 @@ public class TestOzoneAuditLogger {
public void notLogDebugEvents() throws IOException { public void notLogDebugEvents() throws IOException {
AUDIT.logWriteSuccess(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap(), Level.DEBUG); AUDIT.logWriteSuccess(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap(), Level.DEBUG);
AUDIT.logReadSuccess(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.DEBUG); AUDIT.logReadSuccess(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.DEBUG);
verifyLog(null); verifyNoLog();
} }
public void verifyLog(String expected) throws IOException { private void verifyLog(String expected) throws IOException {
File file = new File("audit.log"); File file = new File("audit.log");
List<String> lines = FileUtils.readLines(file, (String)null); List<String> lines = FileUtils.readLines(file, (String)null);
if(expected == null){ final int retry = 5;
// When no log entry is expected, the log file must be empty int i = 0;
assertTrue(lines.size() == 0); while (lines.isEmpty() && i < retry) {
} else { lines = FileUtils.readLines(file, (String)null);
try {
Thread.sleep( 500 * (i + 1));
} catch(InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
i++;
}
// When log entry is expected, the log file will contain one line and // When log entry is expected, the log file will contain one line and
// that must be equal to the expected string // that must be equal to the expected string
assertTrue(lines.size() != 0);
assertTrue(expected.equalsIgnoreCase(lines.get(0))); assertTrue(expected.equalsIgnoreCase(lines.get(0)));
//empty the file //empty the file
lines.remove(0); lines.remove(0);
FileUtils.writeLines(file, lines, false); FileUtils.writeLines(file, lines, false);
} }
private void verifyNoLog() throws IOException {
File file = new File("audit.log");
List<String> lines = FileUtils.readLines(file, (String)null);
// When no log entry is expected, the log file must be empty
assertTrue(lines.size() == 0);
} }
} }