|
@@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
@@ -36,10 +37,29 @@ import static org.junit.Assert.assertTrue;
|
|
|
*/
|
|
|
public class TestOzoneAuditLogger {
|
|
|
|
|
|
- private static final Logger LOG = LoggerFactory.getLogger
|
|
|
- (TestOzoneAuditLogger.class.getName());
|
|
|
- private static AuditLogger AUDIT = new AuditLogger(AuditLoggerType.OMLOGGER);
|
|
|
- public DummyEntity auditableObj = new DummyEntity();
|
|
|
+ private static final Logger LOG =
|
|
|
+ LoggerFactory.getLogger(TestOzoneAuditLogger.class.getName());
|
|
|
+
|
|
|
+ private static final AuditLogger AUDIT =
|
|
|
+ new AuditLogger(AuditLoggerType.OMLOGGER);
|
|
|
+
|
|
|
+ private static final String SUCCESS = AuditEventStatus.SUCCESS.name();
|
|
|
+ private static final String FAILURE = AuditEventStatus.FAILURE.name();
|
|
|
+
|
|
|
+ private static final Map<String, String> PARAMS =
|
|
|
+ new DummyEntity().toAuditMap();
|
|
|
+
|
|
|
+ private static final AuditMessage WRITE_FAIL_MSG = new AuditMessage("john",
|
|
|
+ "192.168.0.1", DummyAction.CREATE_VOLUME.name(), PARAMS, FAILURE);
|
|
|
+
|
|
|
+ private static final AuditMessage WRITE_SUCCESS_MSG = new AuditMessage("john",
|
|
|
+ "192.168.0.1", DummyAction.CREATE_VOLUME.name(), PARAMS, SUCCESS);
|
|
|
+
|
|
|
+ private static final AuditMessage READ_FAIL_MSG = new AuditMessage("john",
|
|
|
+ "192.168.0.1", DummyAction.READ_VOLUME.name(), PARAMS, FAILURE);
|
|
|
+
|
|
|
+ private static final AuditMessage READ_SUCCESS_MSG = new AuditMessage("john",
|
|
|
+ "192.168.0.1", DummyAction.READ_VOLUME.name(), PARAMS, SUCCESS);
|
|
|
|
|
|
@BeforeClass
|
|
|
public static void setUp(){
|
|
@@ -48,13 +68,13 @@ public class TestOzoneAuditLogger {
|
|
|
|
|
|
@AfterClass
|
|
|
public static void tearDown() {
|
|
|
- File file = new File("audit.log");
|
|
|
- if (FileUtils.deleteQuietly(file)) {
|
|
|
- LOG.info(file.getName() +
|
|
|
- " has been deleted as all tests have completed.");
|
|
|
- } else {
|
|
|
- LOG.info("audit.log could not be deleted.");
|
|
|
- }
|
|
|
+ File file = new File("audit.log");
|
|
|
+ if (FileUtils.deleteQuietly(file)) {
|
|
|
+ LOG.info(file.getName() +
|
|
|
+ " has been deleted as all tests have completed.");
|
|
|
+ } else {
|
|
|
+ LOG.info("audit.log could not be deleted.");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -62,20 +82,31 @@ public class TestOzoneAuditLogger {
|
|
|
*/
|
|
|
@Test
|
|
|
public void logInfoWriteSuccess() throws IOException {
|
|
|
- AUDIT.logWriteSuccess(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap(), Level.INFO);
|
|
|
- String expected = "[INFO ] OMAudit - CREATE_VOLUME [ key1=\"value1\" " +
|
|
|
- "key2=\"value2\"] SUCCESS";
|
|
|
+ AUDIT.logWriteSuccess(Level.INFO, WRITE_SUCCESS_MSG);
|
|
|
+ String expected =
|
|
|
+ "[INFO ] OMAudit - " + WRITE_SUCCESS_MSG.getFormattedMessage();
|
|
|
+ verifyLog(expected);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test to verify default log level is INFO when logging success events.
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void verifyDefaultLogLevelForSuccess() throws IOException {
|
|
|
+ AUDIT.logWriteSuccess(WRITE_SUCCESS_MSG);
|
|
|
+ String expected =
|
|
|
+ "[INFO ] OMAudit - " + WRITE_SUCCESS_MSG.getFormattedMessage();
|
|
|
verifyLog(expected);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Test to verify default log level is INFO
|
|
|
+ * Test to verify default log level is ERROR when logging failure events.
|
|
|
*/
|
|
|
@Test
|
|
|
- public void verifyDefaultLogLevel() throws IOException {
|
|
|
- AUDIT.logWriteSuccess(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap());
|
|
|
- String expected = "[INFO ] OMAudit - CREATE_VOLUME [ key1=\"value1\" " +
|
|
|
- "key2=\"value2\"] SUCCESS";
|
|
|
+ public void verifyDefaultLogLevelForFailure() throws IOException {
|
|
|
+ AUDIT.logWriteFailure(WRITE_FAIL_MSG);
|
|
|
+ String expected =
|
|
|
+ "[ERROR] OMAudit - " + WRITE_FAIL_MSG.getFormattedMessage();
|
|
|
verifyLog(expected);
|
|
|
}
|
|
|
|
|
@@ -84,9 +115,9 @@ public class TestOzoneAuditLogger {
|
|
|
*/
|
|
|
@Test
|
|
|
public void logErrorWriteFailure() throws IOException {
|
|
|
- AUDIT.logWriteFailure(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap(), Level.ERROR);
|
|
|
- String expected = "[ERROR] OMAudit - CREATE_VOLUME [ key1=\"value1\" " +
|
|
|
- "key2=\"value2\"] FAILURE";
|
|
|
+ AUDIT.logWriteFailure(Level.ERROR, WRITE_FAIL_MSG);
|
|
|
+ String expected =
|
|
|
+ "[ERROR] OMAudit - " + WRITE_FAIL_MSG.getFormattedMessage();
|
|
|
verifyLog(expected);
|
|
|
}
|
|
|
|
|
@@ -95,11 +126,10 @@ public class TestOzoneAuditLogger {
|
|
|
*/
|
|
|
@Test
|
|
|
public void notLogReadEvents() throws IOException {
|
|
|
- AUDIT.logReadSuccess(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.INFO);
|
|
|
- AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.INFO);
|
|
|
- AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR);
|
|
|
- AUDIT.logReadFailure(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.ERROR,
|
|
|
- new Exception("test"));
|
|
|
+ AUDIT.logReadSuccess(Level.INFO, READ_SUCCESS_MSG);
|
|
|
+ AUDIT.logReadFailure(Level.INFO, READ_FAIL_MSG);
|
|
|
+ AUDIT.logReadFailure(Level.ERROR, READ_FAIL_MSG);
|
|
|
+ AUDIT.logReadFailure(Level.ERROR, READ_FAIL_MSG, new Exception("test"));
|
|
|
verifyNoLog();
|
|
|
}
|
|
|
|
|
@@ -108,34 +138,34 @@ public class TestOzoneAuditLogger {
|
|
|
*/
|
|
|
@Test
|
|
|
public void notLogDebugEvents() throws IOException {
|
|
|
- AUDIT.logWriteSuccess(DummyAction.CREATE_VOLUME, auditableObj.toAuditMap(), Level.DEBUG);
|
|
|
- AUDIT.logReadSuccess(DummyAction.READ_VOLUME, auditableObj.toAuditMap(), Level.DEBUG);
|
|
|
+ AUDIT.logWriteSuccess(Level.DEBUG, WRITE_SUCCESS_MSG);
|
|
|
+ AUDIT.logReadSuccess(Level.DEBUG, READ_SUCCESS_MSG);
|
|
|
verifyNoLog();
|
|
|
}
|
|
|
|
|
|
private void verifyLog(String expected) throws IOException {
|
|
|
File file = new File("audit.log");
|
|
|
List<String> lines = FileUtils.readLines(file, (String)null);
|
|
|
- final int retry = 5;
|
|
|
- int i = 0;
|
|
|
- while (lines.isEmpty() && i < retry) {
|
|
|
- lines = FileUtils.readLines(file, (String)null);
|
|
|
- try {
|
|
|
- Thread.sleep( 500 * (i + 1));
|
|
|
- } catch(InterruptedException ie) {
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- break;
|
|
|
- }
|
|
|
- i++;
|
|
|
+ final int retry = 5;
|
|
|
+ int i = 0;
|
|
|
+ while (lines.isEmpty() && i < retry) {
|
|
|
+ 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
|
|
|
- // that must be equal to the expected string
|
|
|
- assertTrue(lines.size() != 0);
|
|
|
- assertTrue(expected.equalsIgnoreCase(lines.get(0)));
|
|
|
- //empty the file
|
|
|
- lines.remove(0);
|
|
|
- FileUtils.writeLines(file, lines, false);
|
|
|
+ // When log entry is expected, the log file will contain one line and
|
|
|
+ // that must be equal to the expected string
|
|
|
+ assertTrue(lines.size() != 0);
|
|
|
+ assertTrue(expected.equalsIgnoreCase(lines.get(0)));
|
|
|
+ //empty the file
|
|
|
+ lines.remove(0);
|
|
|
+ FileUtils.writeLines(file, lines, false);
|
|
|
}
|
|
|
|
|
|
private void verifyNoLog() throws IOException {
|