Skip to content

Commit

Permalink
Expanded the LogFileAuditLoggerTest with more tests and some minor ty…
Browse files Browse the repository at this point in the history
…pography changes in AuditTrailFilterTest
  • Loading branch information
chikitulfo committed Dec 15, 2022
1 parent ba9ea47 commit 0f8d507
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
import java.util.Collection;
import java.util.regex.Pattern;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;

/**
* Created by Pierre Beitz
* on 18/11/2019.
Expand Down Expand Up @@ -60,7 +56,7 @@ public void cancelItemLogsTheQueryStringAndTheUser() throws Exception {
}

String log = Util.loadFile(new File(tmpDir.getRoot(), "test.log.0"), StandardCharsets.UTF_8);
assertTrue("logged actions: " + log, Pattern.compile(".*id=1.*job/test-job.*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
Assert.assertTrue("logged actions: " + log, Pattern.compile(".*id=1.*job/test-job.*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
}

@Issue("JENKINS-15731")
Expand All @@ -83,7 +79,7 @@ public void createItemLogsTheNewItemName() throws Exception {
j.submit(form);

String log = Util.loadFile(new File(tmpDir.getRoot(), "create-item.log.0"), StandardCharsets.UTF_8);
assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
Assert.assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
}

@Test
Expand Down Expand Up @@ -113,11 +109,11 @@ public void createItemLogsTheNewItemNameWithRotateDaily() throws Exception {

// Check that the action was logged in the file
String log = Util.loadFile(new File(tmpDir.getRoot(), logRotateComputedName), StandardCharsets.UTF_8);
assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());
Assert.assertTrue("logged actions: " + log, Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL).matcher(log).matches());

// Check that there is only one daily log file in the directory
String directoryPath = logFile.getParent();
Collection<File> directoryFiles = FileUtils.listFiles(new File(directoryPath), new RegexFileFilter(".*" + logFile.getName() + LogFileAuditLogger.DAILY_ROTATING_FILE_REGEX_PATTERN), DirectoryFileFilter.DIRECTORY);
assertThat(directoryFiles.size(), is(1));
Assert.assertEquals(directoryFiles.size(), 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;

/**
* Created by Pierre Beitz
Expand Down Expand Up @@ -69,4 +68,64 @@ public void configuringAFileLoggerWithDailyRotationAndNonExistingParents() {
Assert.assertFalse(logFile.toFile().exists());
Assert.assertTrue(logFileRotating.toFile().exists());
}

/**
* A test that ensures that the logger reuses the same file if restarted within the same day
*/
@Test
public void logFileIsReusedIfRestartedWithDailyRotation() throws IOException {
Path logFile = folder.getRoot().toPath().resolve("file");
LogFileAuditLogger logFileAuditLogger = new LogFileAuditLogger(logFile.toString(), 0, 1, null, true);
logFileAuditLogger.log("configuringAFileLoggerRotatingDaily - line1");
Path logFileRotating = folder.getRoot().toPath().resolve(logFileAuditLogger.computePattern());
Assert.assertTrue(logFileRotating.toFile().exists());
logFileAuditLogger.cleanUp();
LogFileAuditLogger logFileAuditLogger2 = new LogFileAuditLogger(logFile.toString(), 0, 1, null, true);
logFileAuditLogger2.log("configuringAFileLoggerRotatingDaily - line2");

String directoryPath = logFile.toFile().getParent();
Collection<File> directoryFiles = FileUtils.listFiles(new File(directoryPath), new RegexFileFilter(".*" + logFile.toFile().getName() + LogFileAuditLogger.DAILY_ROTATING_FILE_REGEX_PATTERN), DirectoryFileFilter.DIRECTORY);
Assert.assertEquals(directoryFiles.size(), 1);

String log = Util.loadFile(logFileRotating.toFile(), StandardCharsets.UTF_8);
Assert.assertTrue(log.contains("configuringAFileLoggerRotatingDaily - line1"));
Assert.assertTrue(log.contains("configuringAFileLoggerRotatingDaily - line2"));
}

/**
* A test that ensures that the log file rotates in the next day
*/
@Test
public void logFileProperlyRotatingInNextDayWithDailyRotation() throws IOException {
ZonedDateTime zonedDateTimeA = ZonedDateTime.now().plusDays(1);
MockedStatic<ZonedDateTime> mockedLocalDateTime = Mockito.mockStatic(ZonedDateTime.class, Mockito.withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS));

// Check that the log file is created with the corresponded format (Today date)
Path logFile = folder.getRoot().toPath().resolve("file");
LogFileAuditLogger logFileAuditLogger = new LogFileAuditLogger(logFile.toString(), 0, 2, null, true);
logFileAuditLogger.log("configuringAFileLoggerRotatingDaily - line1");
Path logFileRotating = folder.getRoot().toPath().resolve(logFileAuditLogger.computePattern());
Assert.assertTrue(logFileRotating.toFile().exists());

// Check that there is ONLY one file generated at this point (Today date)
String directoryPath = logFile.toFile().getParent();
Collection<File> directoryFiles = FileUtils.listFiles(new File(directoryPath), new RegexFileFilter(".*" + logFile.toFile().getName() + LogFileAuditLogger.DAILY_ROTATING_FILE_REGEX_PATTERN), DirectoryFileFilter.DIRECTORY);
Assert.assertEquals(directoryFiles.size(),1);

// Log something and check it appears in the logger file
String log = Util.loadFile(logFileRotating.toFile(), StandardCharsets.UTF_8);
Assert.assertTrue(log.contains("configuringAFileLoggerRotatingDaily - line1"));

// Increase +1 day
mockedLocalDateTime.when(ZonedDateTime::now).thenReturn(zonedDateTimeA);

// Log something else
logFileAuditLogger.log("configuringAFileLoggerRotatingDaily - line2");

// Check that the corresponded is the ONLY one which appear on this file (Today +1)
logFileRotating = folder.getRoot().toPath().resolve(logFileAuditLogger.computePattern());
log = Util.loadFile(logFileRotating.toFile(), StandardCharsets.UTF_8);
Assert.assertTrue(log.contains("configuringAFileLoggerRotatingDaily - line2"));
Assert.assertFalse(log.contains("configuringAFileLoggerRotatingDaily - line1"));
}
}

0 comments on commit 0f8d507

Please sign in to comment.