Skip to content

Commit

Permalink
[JENKINS-37054] Add symbol annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsomai committed Sep 3, 2016
1 parent 1bcd38c commit 7411ce3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 17 deletions.
23 changes: 18 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<properties>
<jenkins.version>1.609</jenkins.version>
<java.level>6</java.level>
<workflow.version>1.4.3</workflow.version>
<workflow-job.version>1.4.3</workflow-job.version>
<workflow-cps.version>1.4.3</workflow-cps.version>
<workflow-basic-steps.version>1.4.3</workflow-basic-steps.version>
<workflow-durable-task-step.version>1.4.3</workflow-durable-task-step.version>
</properties>

<developers>
Expand All @@ -43,6 +46,11 @@
<artifactId>matrix-project</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand All @@ -58,19 +66,25 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<version>${workflow-job.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<version>${workflow-cps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow.version}</version>
<version>${workflow-basic-steps.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>${workflow-durable-task-step.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -97,4 +111,3 @@
</pluginRepository>
</pluginRepositories>
</project>

4 changes: 3 additions & 1 deletion src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -251,7 +252,8 @@ public boolean needsToRunAfterFinalized() {
public boolean isMatrixProject(Object o) {
return o instanceof MatrixProject;
}


@Symbol("wsCleanup")
@Extension(ordinal=-9999)
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

Expand Down
125 changes: 114 additions & 11 deletions src/test/java/hudson/plugins/ws_cleanup/CleanupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
import static org.hamcrest.Matchers.*;
import hudson.FilePath;
Expand All @@ -47,6 +48,7 @@
import java.util.List;
import java.util.concurrent.Future;

import hudson.util.VersionNumber;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand All @@ -56,6 +58,9 @@
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class CleanupTest {

@Rule public JenkinsRule j = new JenkinsRule();
Expand Down Expand Up @@ -207,9 +212,8 @@ public void pipelineWorkspaceCleanup() throws Exception {
" } \n" +
" } \n" +
"}"));
WorkflowRun build = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", build);
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

assertThat(ws.getRoot().listFiles(), nullValue());
}
Expand All @@ -228,14 +232,10 @@ public void pipelineWorkspaceCleanupUsingPattern() throws Exception {
" } \n" +
" } \n" +
"}"));
WorkflowRun build = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", build);
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

File[] files = ws.getRoot().listFiles();
assertThat(files, notNullValue());
assertThat(files, arrayWithSize(1));
assertThat(files[0].getName(), is("foo.txt"));
verifyFileExists("foo.txt");
}

@Test @Issue("JENKINS-28454")
Expand All @@ -258,10 +258,113 @@ public void pipelineWorkspaceCleanupUnlessBuildFails() throws Exception {
j.assertBuildStatus(Result.FAILURE, build);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] Skipped based on build state FAILURE", build);

verifyFileExists("foo.txt");
}

@Test
@Issue("JENKINS-37054")
public void symbolAnnotationWorkspaceCleanup() throws Exception {
assumeSymbolDependencies();

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "') { \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'foobar' \n" +
" } finally { \n" +
" wsCleanup() \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

assertThat(ws.getRoot().listFiles(), nullValue());
}

@Test
@Issue("JENKINS-37054")
public void symbolWorkspaceCleanupAnnotationUsingPattern() throws Exception {
assumeSymbolDependencies();

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "') { \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'first file' \n" +
" writeFile file: 'bar.txt', text: 'second file' \n" +
" } finally { \n" +
" wsCleanup patterns: [[pattern: 'bar.*', type: 'INCLUDE']] \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done", run);

verifyFileExists("foo.txt");
}

@Test
@Issue("JENKINS-37054")
public void symbolAnnotationWorkspaceCleanupUnlessBuildFails() throws Exception {
assumeSymbolDependencies();

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node { \n" +
" ws ('" + ws.getRoot() + "'){ \n" +
" try { \n" +
" writeFile file: 'foo.txt', text: 'foobar' \n" +
" throw new Exception() \n" +
" } catch (err) { \n" +
" currentBuild.result = 'FAILURE' \n" +
" } finally { \n" +
" wsCleanup cleanWhenFailure: false \n" +
" } \n" +
" } \n" +
"}"));
WorkflowRun run = p.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, run);
j.assertLogContains("[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] Skipped based on build state FAILURE", run);

verifyFileExists("foo.txt");
}

/**
* To use the @Symbol annotation in tests, minimum workflow-cps version 2.10 is required.
* This dependency comes with other dependency version requirements, as stated by this method.
* To run tests restricted by this method, type
* <pre>
* mvn clean install -Djenkins.version=1.642.1 -Djava.level=7 -Dworkflow-job.version=2.4 -Dworkflow-basic-steps.version=2.1 -Dworkflow-cps.version=2.10 -Dworkflow-durable-task-step.version=2.4
* </pre>
*/
private static void assumeSymbolDependencies() {
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("jenkins.version"), "1.642.1");
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("java.level"), "7");
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("workflow-job.version"), "2.4");
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("workflow-basic-steps.version"), "2.1");
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("workflow-cps.version"), "2.10");
assumePropertyIsGreaterThanOrEqualTo(System.getProperty("workflow-durable-task-step.version"), "2.4");
}

/**
* Checks if the given property is not null, and if it's greater than or equal to the given version.
*
* @param property the property to be checked
* @param version the version on which the property is checked against
*/
private static void assumePropertyIsGreaterThanOrEqualTo(@CheckForNull String property, @Nonnull String version) {
assumeThat(property, notNullValue());
assumeThat(new VersionNumber(property).compareTo(new VersionNumber(version)), is(greaterThanOrEqualTo(0)));
}

private void verifyFileExists(String fileName) {
File[] files = ws.getRoot().listFiles();
assertThat(files, notNullValue());
assertThat(files, arrayWithSize(1));
assertThat(files[0].getName(), is("foo.txt"));
assertThat(files[0].getName(), is(fileName));
}

private WsCleanup wipeoutPublisher() {
Expand Down

0 comments on commit 7411ce3

Please sign in to comment.