Skip to content

Commit

Permalink
LogRecorder programmatic deletion support (#8489)
Browse files Browse the repository at this point in the history
* Add programmatic deletion support for LogRecorder.
* Also added event handling through SaveableListener whenever it gets deleted.
  • Loading branch information
Vlatombe authored Sep 20, 2023
1 parent b7920ec commit 7d28571
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/main/java/hudson/logging/LogRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ public int hashCode() {
*/
@RequirePOST
public synchronized void doDoDelete(StaplerResponse rsp) throws IOException, ServletException {
delete();
rsp.sendRedirect2("..");
}

/**
* Deletes this log recorder.
* @throws IOException In case anything went wrong while deleting the configuration file.
* @since TODO
*/
public void delete() throws IOException {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);

getConfigFile().delete();
Expand All @@ -544,7 +554,7 @@ public synchronized void doDoDelete(StaplerResponse rsp) throws IOException, Ser
loggers.forEach(Target::disable);

getParent().getRecorders().forEach(logRecorder -> logRecorder.getLoggers().forEach(Target::enable));
rsp.sendRedirect2("..");
SaveableListener.fireOnChange(this, getConfigFile());
}

/**
Expand Down
34 changes: 34 additions & 0 deletions test/src/test/java/hudson/logging/LogRecorderManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import hudson.XmlFile;
import hudson.model.Computer;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import java.io.IOException;
Expand All @@ -52,6 +57,7 @@
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.Url;

/**
Expand Down Expand Up @@ -204,6 +210,34 @@ public void addingLogRecorderToListAddsToLegacyRecordersMap() throws IOException
assertThat(log.getRecorders().size(), is(1));
}

@Test
public void deletingLogRecorder() throws IOException {
LogRecorderManager log = j.jenkins.getLog();
assertThat(log.getRecorders(), empty());
LogRecorder logRecorder = new LogRecorder("dummy");
logRecorder.getLoggers().add(new LogRecorder.Target("dummy", Level.ALL));
log.getRecorders().add(logRecorder);
logRecorder.save();
assertThat(log.getRecorders(), hasSize(1));
logRecorder.delete();
assertThat(log.getRecorders(), empty());
assertTrue(DeletingLogRecorderListener.recordDeletion);
}

@TestExtension("deletingLogRecorder")
public static class DeletingLogRecorderListener extends SaveableListener {
private static boolean recordDeletion;

@Override
public void onChange(Saveable o, XmlFile file) {
if (o instanceof LogRecorder && "dummy".equals(((LogRecorder) o).getName())) {
if (!file.exists()) {
recordDeletion = true;
}
}
}
}

private static final class Log extends MasterToSlaveCallable<Boolean, Error> {
private final Level level;
private final String logger;
Expand Down

0 comments on commit 7d28571

Please sign in to comment.