Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogRecorder programmatic deletion support #8489

Merged
Merged
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
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