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 Nov 8, 2016
1 parent 4751cd2 commit 00232be
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 16 deletions.
22 changes: 18 additions & 4 deletions pom.xml
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 Down Expand Up @@ -45,6 +48,11 @@
<version>0.3</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 @@ -54,19 +62,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>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
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 @@ -253,7 +254,8 @@ public boolean needsToRunAfterFinalized() {
public boolean isMatrixProject(Object o) {
return o instanceof MatrixProject;
}


@Symbol("cleanWs")
@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
Expand Up @@ -27,6 +27,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.*;

Expand Down Expand Up @@ -54,6 +55,7 @@
import java.util.concurrent.Future;

import hudson.util.DescribableList;
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 @@ -64,6 +66,9 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

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

public class CleanupTest {

@Rule public JenkinsRule j = new JenkinsRule();
Expand Down Expand Up @@ -215,9 +220,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 @@ -236,14 +240,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 @@ -266,10 +266,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" +
" cleanWs() \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" +
" cleanWs 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" +
" cleanWs 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));
}

public static WsCleanup wipeoutPublisher() {
Expand Down

0 comments on commit 00232be

Please sign in to comment.