Skip to content

Commit

Permalink
Add DeleteLogFileAction (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored Dec 16, 2019
1 parent 668625d commit f394b1b
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.actions;

import hudson.Extension;
import hudson.model.Descriptor;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.actions.Action;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.actions.DeleteLogFileAction;

/**
* Descriptor implementation needed to render UI for {@link DeleteLogFileAction}.
*
* @author Damian Szczepanik (damianszczepanik@github)
*/
@Extension
public class DeleteLogFileActionDescriptor extends Descriptor<Action> {

public DeleteLogFileActionDescriptor() {
super(DeleteLogFileAction.class);
}

@Override
public String getDisplayName() {
return "Delete log file";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.actions;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import hudson.model.Run;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Deletes the build log file.
*
* @author Damian Szczepanik (damianszczepanik@github)
* @see Run#delete()
*/
public class DeleteLogFileAction extends Action {

private static final Logger LOG = Logger.getLogger(DeleteLogFileAction.class.getName());

@DataBoundConstructor
public DeleteLogFileAction() {
// Jenkins stapler requires to have public constructor with @DataBoundConstructor
}

@Override
public void perform(Run<?, ?> run) throws IOException, InterruptedException {
File logFile = run.getLogFile();
if (logFile.exists()) {
boolean wasDeleted = logFile.delete();
if (!wasDeleted) {
LOG.warning(String.format("Log file for build %d could not be deleted", run.getNumber()));
}
// ToDo: consider https://github.com/jenkinsci/delete-log-plugin/blob/master/src/main/java/jenkinsci/plugin/deletelog/LogDelete.java#L47-L51
// for this and other actions
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">

</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h3>Description</h3>
Deletes the build log file.

<h3>Use cases</h3>
To leave builds and delete the log file which eg. might contain sensitive data or are very big.

<h3>Warning!</h3>
Log file should not be deleted for the build which was already removed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.actions;

import static org.assertj.core.api.Assertions.assertThat;

import hudson.model.Descriptor;
import org.junit.Test;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
public class DeleteLogFileActionDescriptorTest {

@Test
public void getDisplayName_ReturnsDescriptorName() {

// given
Descriptor descriptor = new DeleteLogFileActionDescriptor();

// when
String displayName = descriptor.getDisplayName();

// then
assertThat(displayName).isEqualTo("Delete log file");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.actions;

import java.io.IOException;

import org.junit.Test;
import pl.damianszczepanik.jenkins.buildhistorymanager.utils.RunStub;

/**
* @author Damian Szczepanik (damianszczepanik@github)
*/
public class DeleteLogFileActionTest {

@Test
public void perform_OnExistingLogFile_DeletesLogFile() throws IOException, InterruptedException {

// given
Action action = new DeleteLogFileAction();
RunStub run = new RunStub();
run.setLogFile(true);

// when
action.perform(run);

// then
run.assertLogFileWasDeleted();
}


@Test
public void perform_OnMissingLogFile_SkipDeletion() throws IOException, InterruptedException {

// given
Action action = new DeleteLogFileAction();
RunStub run = new RunStub();

// when
action.perform(run);

// then
run.assertLogFileWasNotDeleted();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.File;
import java.io.IOException;

import hudson.model.Job;
Expand All @@ -16,9 +18,11 @@
public class RunStub extends Run {

private Result result;
private boolean isLogFilePresent;

private int deleteArtifactsTimes;
private int deleteTimes;
private int deleteLogFile;

public RunStub(int buildNumber) throws IOException {
this();
Expand Down Expand Up @@ -57,6 +61,10 @@ private void setStartTime(long startTime) {
Deencapsulation.setField(this, "startTime", startTime);
}

public void setLogFile(boolean isPresent) {
isLogFilePresent = isPresent;
}

@Override
public void deleteArtifacts() {
deleteArtifactsTimes++;
Expand All @@ -67,10 +75,28 @@ public void delete() {
deleteTimes++;
}

@Override
public File getLogFile() {
File logFile = mock(File.class);
when(logFile.exists()).thenReturn(isLogFilePresent);
when(logFile.delete()).thenReturn(deleteLogFile(isLogFilePresent));

return logFile;
}

private boolean deleteLogFile(boolean isLogFilePresent) {
deleteLogFile++;
return isLogFilePresent;
}

public void assertBuildWasDeleted() {
assertThat(deleteTimes).isOne();
}

public void assertLogFileWasDeleted() {
assertThat(deleteLogFile).isOne();
}

public void assertBuildIsAvailable() {
assertThat(deleteTimes).isZero();
}
Expand All @@ -83,6 +109,10 @@ public void assertArtifactsAreAvailable() {
assertThat(deleteArtifactsTimes).isZero();
}

public void assertLogFileWasNotDeleted() {
assertThat(deleteLogFile).isOne();
}

@Override
public Result getResult() {
return result;
Expand Down

0 comments on commit f394b1b

Please sign in to comment.