Skip to content

Commit

Permalink
[FIXED JENKINS-26250] Asynchronous delete of workspace does not detec…
Browse files Browse the repository at this point in the history
…t fail to rename
  • Loading branch information
olivergondza committed Jan 26, 2015
1 parent 478158f commit ab81960
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/hudson/plugins/ws_cleanup/Wipeout.java
Expand Up @@ -25,6 +25,8 @@

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

import hudson.FilePath;
import hudson.FilePath.FileCallable;
Expand All @@ -51,6 +53,16 @@
protected void perform(FilePath workspace) throws IOException, InterruptedException {
final FilePath deleteMe = workspace.withSuffix("_ws-cleanup_" + System.currentTimeMillis());
workspace.renameTo(deleteMe);

if (!deleteMe.exists()) {
LOGGER.log(
Level.WARNING,
"Cleaning workspace synchronously. Failed to rename {0} to {1}.",
new Object[] { workspace.getRemote(), deleteMe.getName() }
);
workspace.act(COMMAND);
}

deleteMe.actAsync(COMMAND);
}

Expand All @@ -61,4 +73,6 @@ public Object invoke(File f, VirtualChannel channel) throws IOException, Interru
return null;
}
}

private static final Logger LOGGER = Logger.getLogger(Wipeout.class.getName());
}
23 changes: 22 additions & 1 deletion src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java
Expand Up @@ -25,7 +25,9 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
Expand All @@ -51,6 +53,7 @@

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;

public class CleanupTest {
Expand Down Expand Up @@ -150,6 +153,24 @@ public void deleteWorkspaceWithNonAsciiCharacters() throws Exception {
assertWorkspaceCleanedUp(build);
}

@Test @Bug(26250)
public void doNotFailToWipeoutWhenRenameFails() throws Exception {
assumeTrue(!Functions.isWindows()); // chmod does not work here

FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut");
populateWorkspace(p, "content.txt");
p.getPublishersList().add(wipeoutPublisher());

FilePath workspace = p.getLastBuild().getWorkspace();
workspace.getParent().chmod(0555); // Remove write for parent dir so rename will fail

workspace.renameTo(workspace.withSuffix("2"));
assertTrue("Rename operation should fail", workspace.exists());

FreeStyleBuild build = j.buildAndAssertSuccess(p);
assertWorkspaceCleanedUp(build);
}

private WsCleanup wipeoutPublisher() {
return new WsCleanup(Collections.<Pattern>emptyList(), false,
true, true, true, true, true, true, true, // run always
Expand All @@ -163,7 +184,7 @@ private void populateWorkspace(FreeStyleProject p, String filename) throws Excep
assertFalse("Workspace populated", b.getWorkspace().list().isEmpty());
}

private void assertWorkspaceCleanedUp(AbstractBuild b) throws Exception {
private void assertWorkspaceCleanedUp(AbstractBuild<?, ?> b) throws Exception {
final FilePath workspace = b.getWorkspace();
if (workspace == null) return; // removed

Expand Down

0 comments on commit ab81960

Please sign in to comment.