Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<spotless.check.skip>false</spotless.check.skip>
<hpi.bundledArtifacts>syslog-java-client</hpi.bundledArtifacts>
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -118,6 +119,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
42 changes: 20 additions & 22 deletions src/test/java/hudson/plugins/audit_trail/AuditTrailFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
package hudson.plugins.audit_trail;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import hudson.Util;
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.regex.Pattern;
import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/**
* Created by Pierre Beitz
* on 18/11/2019.
* @author Pierre Beitz
*/
public class AuditTrailFilterTest {
@WithJenkins
class AuditTrailFilterTest {
public static final int LONG_DELAY = 50000;

@Rule
public JenkinsRule j = new JenkinsRule();

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
Path tmpDir;

@Test
public void cancelItemLogsTheQueryStringAndTheUser() throws Exception {
File logFile = new File(tmpDir.getRoot(), "test.log");
void cancelItemLogsTheQueryStringAndTheUser(JenkinsRule j) throws Exception {
File logFile = tmpDir.resolve("test.log").toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(j, wc);

Expand All @@ -51,18 +49,18 @@ public void cancelItemLogsTheQueryStringAndTheUser() throws Exception {
// see https://issues.jenkins-ci.org/browse/JENKINS-21311
}

String log = Util.loadFile(new File(tmpDir.getRoot(), "test.log.0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve("test.log.0").toFile(), 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());
.matches(),
() -> "logged actions: " + log);
}

@Issue("JENKINS-15731")
@Test
public void createItemLogsTheNewItemName() throws Exception {
File logFile = new File(tmpDir.getRoot(), "create-item.log");
void createItemLogsTheNewItemName(JenkinsRule j) throws Exception {
File logFile = tmpDir.resolve("create-item.log").toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(j, wc);

Expand All @@ -78,11 +76,11 @@ public void createItemLogsTheNewItemName() throws Exception {
wc.waitForBackgroundJavaScript(50);
j.submit(form);

String log = Util.loadFile(new File(tmpDir.getRoot(), "create-item.log.0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve("create-item.log.0").toFile(), StandardCharsets.UTF_8);
assertTrue(
"logged actions: " + log,
Pattern.compile(".*createItem \\(" + jobName + "\\).*by \\QNA from 127.0.0.1\\E.*", Pattern.DOTALL)
.matcher(log)
.matches());
.matches(),
() -> "logged actions: " + log);
}
}
18 changes: 8 additions & 10 deletions src/test/java/hudson/plugins/audit_trail/AuditTrailPluginTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package hudson.plugins.audit_trail;

import static hudson.plugins.audit_trail.AuditTrailPlugin.DEFAULT_PATTERN;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import hudson.ExtensionList;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import org.junit.ClassRule;
import org.junit.Test;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

/**
* Created by Pierre Beitz
* @author Pierre Beitz
*/
public class AuditTrailPluginTest {
@WithJenkinsConfiguredWithCode
class AuditTrailPluginTest {

@ClassRule
public static JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();

@ConfiguredWithCode("security-1846")
@ConfiguredWithCode("security-1846.yaml")
@Issue("SECURITY-1846")
@Test
public void aLegacyDefaultPatternGetsReplacedByTheDefaultPattern() {
void aLegacyDefaultPatternGetsReplacedByTheDefaultPattern(JenkinsConfiguredWithCodeRule r) {
ExtensionList<AuditTrailPlugin> extensionList = r.jenkins.getExtensionList(AuditTrailPlugin.class);
AuditTrailPlugin plugin = extensionList.get(0);
assertEquals(DEFAULT_PATTERN, plugin.getPattern());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@
import hudson.model.labels.LabelAtom;
import java.io.File;
import java.nio.charset.StandardCharsets;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RealJenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;

/**
* Created by Pierre Beitz
* on 16/08/2025.
*/
public class AuditTrailRunListenerFreestyleTest {

@Rule
public RealJenkinsRule rr = new RealJenkinsRule().omitPlugins("workflow-job");
@RegisterExtension
private final RealJenkinsExtension rr = new RealJenkinsExtension().omitPlugins("workflow-job");

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
Path tmpDir;

@Issue("JENKINS-71637")
@Test
public void shouldLogPerStageWorkflowRunAgentNameWithoutPipelineInstalled() throws Throwable {
var logFileName = "shouldLogPerStageWorkflowRunAgentName.log";
var rootDir = tmpDir.getRoot();
var rootDir = tmpDir.toFile();
var logFile = new File(rootDir, logFileName);
rr.startJenkins();
rr.runRemotely(j -> {
Expand All @@ -46,6 +47,7 @@ public void shouldLogPerStageWorkflowRunAgentNameWithoutPipelineInstalled() thro
freestyle.save();

freestyle.scheduleBuild2(0).get();
Thread.sleep(5000);
var log = Util.loadFile(new File(rootDir, logFileName + ".0"), StandardCharsets.UTF_8);
assertThat(log, containsString("slave0"));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hudson.plugins.audit_trail;

import static hudson.plugins.audit_trail.BasicNodeNameRetriever.UNKNOWN_NODE;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import hudson.Util;
import hudson.model.AbstractBuild;
Expand All @@ -18,33 +19,31 @@
import hudson.model.StringParameterDefinition;
import hudson.model.labels.LabelAtom;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.regex.Pattern;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.Mockito;

/**
* Created by Pierre Beitz
* on 31/12/2019.
* @author Pierre Beitz
*/
public class AuditTrailRunListenerTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@WithJenkins
class AuditTrailRunListenerTest {

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
@TempDir
Path tmpDir;

@Issue("JENKINS-12848")
@Test
public void jobParametersAreProperlyLogged() throws Exception {
void jobParametersAreProperlyLogged(JenkinsRule j) throws Exception {
String logFileName = "jobParametersAreProperlyLogged.log";
File logFile = new File(tmpDir.getRoot(), logFileName);
File logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(j, wc);

Expand All @@ -54,40 +53,40 @@ public void jobParametersAreProperlyLogged() throws Exception {
new BooleanParameterDefinition("booleanParam", false, "")));
job.scheduleBuild2(0, new Cause.UserIdCause()).get();

String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);
assertTrue(
"logged actions: " + log,
Pattern.compile(
".*, Parameters:\\[stringParam: \\{value1\\}, booleanParam: \\{false\\}\\].*",
Pattern.DOTALL)
.matcher(log)
.matches());
.matches(),
() -> "logged actions: " + log);
}

@Issue("JENKINS-12848")
@Test
public void jobWithoutParameterIsProperlyLogged() throws Exception {
void jobWithoutParameterIsProperlyLogged(JenkinsRule j) throws Exception {
String logFileName = "jobWithoutParameterIsProperlyLogged.log";
File logFile = new File(tmpDir.getRoot(), logFileName);
File logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(j, wc);

FreeStyleProject job = j.createFreeStyleProject("test-job");
job.scheduleBuild2(0, new Cause.UserIdCause()).get();

String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);
assertTrue(
"logged actions: " + log,
Pattern.compile(".*, Parameters:\\[\\].*", Pattern.DOTALL)
.matcher(log)
.matches());
.matches(),
() -> "logged actions: " + log);
}

@Issue("JENKINS-12848")
@Test
public void jobWithSecretParameterIsProperlyLogged() throws Exception {
void jobWithSecretParameterIsProperlyLogged(JenkinsRule j) throws Exception {
String logFileName = "jobWithSecretParameterIsProperlyLogged.log";
File logFile = new File(tmpDir.getRoot(), logFileName);
File logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(j, wc);

Expand All @@ -96,19 +95,19 @@ public void jobWithSecretParameterIsProperlyLogged() throws Exception {
new ParametersDefinitionProperty(new PasswordParameterDefinition("passParam", "thisIsASecret", "")));
job.scheduleBuild2(0, new Cause.UserIdCause()).get();

String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);
assertTrue(
"logged actions: " + log,
Pattern.compile(".*, Parameters:\\[passParam: \\{\\*\\*\\*\\*\\}\\].*", Pattern.DOTALL)
.matcher(log)
.matches());
.matches(),
() -> "logged actions: " + log);
}

@Issue("JENKINS-62812")
@Test
public void ifSetToNotLogBuildCauseShouldNotLogThem() throws Exception {
void ifSetToNotLogBuildCauseShouldNotLogThem(JenkinsRule j) throws Exception {
String logFileName = "ifSetToNotLogBuildCauseShouldNotLogThem.log";
File logFile = new File(tmpDir.getRoot(), logFileName);
File logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile)
.withLogBuildCause(false)
Expand All @@ -119,13 +118,13 @@ public void ifSetToNotLogBuildCauseShouldNotLogThem() throws Exception {
new ParametersDefinitionProperty(new PasswordParameterDefinition("passParam", "thisIsASecret", "")));
job.scheduleBuild2(0, new Cause.UserIdCause()).get();

String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
String log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);
assertTrue(log.isEmpty());
}

@Issue("JENKINS-71637")
@Test
public void buildNodeNameIsProperlyExtractedFromTheRun() {
void buildNodeNameIsProperlyExtractedFromTheRun(JenkinsRule $) {
var listener = new AuditTrailRunListener();

var notAbstractBuild = Mockito.mock(Run.class);
Expand All @@ -144,9 +143,9 @@ public void buildNodeNameIsProperlyExtractedFromTheRun() {

@Issue("JENKINS-71637")
@Test
public void shouldWorkflowRunAgentName() throws Exception {
public void shouldWorkflowRunAgentName(JenkinsRule j) throws Exception {
var logFileName = "shouldLogWorkflowRunAgentName.log";
var logFile = new File(tmpDir.getRoot(), logFileName);
var logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile)
.withLogBuildCause(true)
Expand Down Expand Up @@ -176,17 +175,17 @@ public void shouldWorkflowRunAgentName() throws Exception {

System.out.println(run.getLog());

var log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
var log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);

// the API creates agents with name slaveN
assertThat(log, containsString("slave0"));
}

@Issue("JENKINS-71637")
@Test
public void shouldLogPerStageWorkflowRunAgentName() throws Exception {
public void shouldLogPerStageWorkflowRunAgentName(JenkinsRule j) throws Exception {
var logFileName = "shouldLogPerStageWorkflowRunAgentName.log";
var logFile = new File(tmpDir.getRoot(), logFileName);
var logFile = tmpDir.resolve(logFileName).toFile();
JenkinsRule.WebClient wc = j.createWebClient();
new SimpleAuditTrailPluginConfiguratorHelper(logFile)
.withLogBuildCause(true)
Expand Down Expand Up @@ -223,7 +222,7 @@ public void shouldLogPerStageWorkflowRunAgentName() throws Exception {
workflowJob.save();
workflowJob.scheduleBuild2(0).get();

var log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8);
var log = Util.loadFile(tmpDir.resolve(logFileName + ".0").toFile(), UTF_8);

assertThat(log, containsString("Built-In Node"));
// the API creates agents with name slaveN
Expand Down
Loading
Loading