Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-20056] Some more tests
  • Loading branch information
olivergondza committed Sep 8, 2014
1 parent 784d5af commit 0af6bfb
Showing 1 changed file with 105 additions and 2 deletions.
107 changes: 105 additions & 2 deletions src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java
Expand Up @@ -23,12 +23,25 @@
*/
package hudson.plugins.ws_cleanup;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.BuildWrapper;
import hudson.tasks.Shell;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
Expand All @@ -44,8 +57,7 @@ public void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exce
final String filename = "\\s! Dozen for 5$ only!";

FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut");
p.getBuildersList().add(new Shell("touch '" + filename + "'"));
j.buildAndAssertSuccess(p); // Populate workspace
populateWorkspace(p, filename);

p.getBuildWrappersList().add(new PreBuildCleanup(Collections.<Pattern>emptyList(), false, null, "rm %s"));
FreeStyleBuild b = j.buildAndAssertSuccess(p);
Expand All @@ -55,4 +67,95 @@ public void doNotTreatFilenameAsRegexReplaceWhenUsingCustomCommand() throws Exce
"Using command: rm " + b.getWorkspace().getRemote() + "/" + filename
));
}

@Test
public void wipeOutWholeWorkspaceBeforeBuild() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut");
populateWorkspace(p, "content.txt");

p.getBuildWrappersList().add(new PreBuildCleanup(Collections.<Pattern>emptyList(), false, null, null));
FreeStyleBuild b = j.buildAndAssertSuccess(p);
assertWorkspaceCleanedUp(b);
}

@Test
public void wipeOutWholeWorkspaceAfterBuild() throws Exception {
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "sut");
p.getBuildersList().add(new Shell("touch content.txt"));

p.getPublishersList().add(wipeoutPublisher());
FreeStyleBuild b = j.buildAndAssertSuccess(p);
assertWorkspaceCleanedUp(b);
}

@Test
public void wipeOutWholeWorkspaceAfterBuildMatrix() throws Exception {
MatrixProject p = j.jenkins.createProject(MatrixProject.class, "sut");
p.setAxes(new AxisList(new TextAxis("name", "a b")));
p.getBuildWrappersList().add(new MatrixWsPopulator());
p.getPublishersList().add(wipeoutPublisher());
MatrixBuild b = j.buildAndAssertSuccess(p);

assertWorkspaceCleanedUp(b);

assertWorkspaceCleanedUp(p.getItem("name=a").getLastBuild());
assertWorkspaceCleanedUp(p.getItem("name=b").getLastBuild());
}

private WsCleanup wipeoutPublisher() {
return new WsCleanup(Collections.<Pattern>emptyList(), false,
true, true, true, true, true, true, true, // run always
null);
}

private void populateWorkspace(FreeStyleProject p, String filename) throws Exception {
p.getBuildersList().add(new Shell("touch '" + filename + "'"));
FreeStyleBuild b = j.buildAndAssertSuccess(p);
p.getBuildersList().clear();
assertFalse("Workspace populated", b.getWorkspace().list().isEmpty());
}

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

List<FilePath> files = workspace.list();
if (files == null) return; // removed

assertTrue("Workspace contains: " + files, files.isEmpty());
}

/**
* Create content in workspace of both master and child builds.
*
* @author ogondza
*/
private static final class MatrixWsPopulator extends BuildWrapper {
@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
build.getWorkspace().child("content.txt").touch(0);
listener.error(build.getWorkspace().toURI().toString());
listener.error(build.getWorkspace().list().toString());
if (build instanceof MatrixRun) {
MatrixBuild mb = ((MatrixRun) build).getParentBuild();
mb.getWorkspace().child("content.txt").touch(0);
listener.error(mb.getWorkspace().toURI().toString());
listener.error(mb.getWorkspace().list().toString());
}

return new Environment() {};
}

@Override
public Descriptor getDescriptor() {
return new Descriptor();
}

private static final class Descriptor extends hudson.model.Descriptor<BuildWrapper> {
@Override
public String getDisplayName() {
return "Matrix workspace populator";
}
}
}
}

0 comments on commit 0af6bfb

Please sign in to comment.