Skip to content
Permalink
Browse files

[JENKINS-46700] Use the latest pipeline plugins for tests

  • Loading branch information
ikedam committed Sep 23, 2017
1 parent 271d158 commit ebf8b83fd4588220a83864913fb1dc1754208215
41 pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.5</version>
<version>2.35</version>
</parent>

<artifactId>copyartifact</artifactId>
@@ -56,7 +56,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.3</version>
<version>1.6</version>
</dependency>
<!-- Used for UI test -->
<dependency>
@@ -97,37 +97,62 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.3</version>
<version>2.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>1.15</version>
<version>2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.10</version>
<version>2.32</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.0</version>
<version>2.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.3</version>
<version>2.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-build-step</artifactId>
<version>2.0</version>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
<version>1.20</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Required by maven-plugin -->
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -26,56 +26,47 @@
import static org.junit.Assert.*;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.model.Cause.UserCause;
import hudson.plugins.copyartifact.testutils.CopyArtifactJenkinsRule;
import hudson.plugins.copyartifact.testutils.CopyArtifactUtil;
import hudson.plugins.copyartifact.testutils.FileWriteBuilder;
import hudson.tasks.ArtifactArchiver;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.After;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

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

/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public class CopyArtifactWorkflowTest {

@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

@After
public void deleteAllJobs() throws Exception {
for (Job<?, ?> job: jenkinsRule.jenkins.getItems(Job.class)) {
job.delete();
}
}
@Rule
public CopyArtifactJenkinsRule jenkinsRule = new CopyArtifactJenkinsRule();

@Test
public void test_simpleUntriggeredCopy() throws Exception {
// create "project_1" with an archived artifact...
WorkflowJob project_1 = createWorkflow("project_1",
WorkflowJob project_1 = jenkinsRule.createWorkflow("project_1",
"writeFile text: 'hello', file: 'hello.txt'; " +
"step([$class: 'ArtifactArchiver', artifacts: 'hello.txt', fingerprint: true])");
WorkflowRun b = jenkinsRule.assertBuildStatusSuccess(project_1.scheduleBuild2(0));
assertArtifactInArchive(b);

// Now lets try copy the artifact from "project_1" to "project_2"
WorkflowJob project_2 = createWorkflow("project_2",
WorkflowJob project_2 = jenkinsRule.createWorkflow("project_2",
"step([$class: 'CopyArtifact', projectName: 'project_1', filter: 'hello.txt']); " +
"step([$class: 'ArtifactArchiver', artifacts: 'hello.txt', fingerprint: true]);");
b = jenkinsRule.assertBuildStatusSuccess(project_2.scheduleBuild2(0));
@@ -88,7 +79,7 @@ public void test_simpleUntriggeredCopy() throws Exception {
@Issue("JENKINS-26694")
@Test
public void testFilterByParametersForWorkflow() throws Exception {
WorkflowJob copiee = createWorkflow("copiee",
WorkflowJob copiee = jenkinsRule.createWorkflow("copiee",
"writeFile text: \"${PARAM}\", file:'artifact.txt';"
+ "archive includes:'artifact.txt';"
);
@@ -140,8 +131,8 @@ public void testFilterByParametersForWorkflow() throws Exception {
@Issue("JENKINS-33577")
@Test
public void copyFromDownstreamBuild() throws Exception {
WorkflowJob us = createWorkflow("us", "step([$class: 'CopyArtifact', projectName: 'ds', selector: [$class: 'SpecificBuildSelector', buildNumber: \"${build('ds').number}\"]]); echo readFile('art')");
WorkflowJob ds = createWorkflow("ds", "writeFile file: 'art', text: env.BUILD_TAG; archive includes: 'art'");
WorkflowJob us = jenkinsRule.createWorkflow("us", "step([$class: 'CopyArtifact', projectName: 'ds', selector: [$class: 'SpecificBuildSelector', buildNumber: \"${build('ds').number}\"]]); echo readFile('art')");
WorkflowJob ds = jenkinsRule.createWorkflow("ds", "writeFile file: 'art', text: env.BUILD_TAG; archive includes: 'art'");
jenkinsRule.assertLogContains("jenkins-ds-1", jenkinsRule.assertBuildStatusSuccess(us.scheduleBuild2(0)));
}

@@ -151,16 +142,9 @@ private void assertArtifactInArchive(Run b) {
Assert.assertEquals("hello.txt", artifacts.get(0).relativePath);
}

private WorkflowJob createWorkflow(String name, String script) throws IOException {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, name);
job.setDefinition(new CpsFlowDefinition("node {" + script + "}", true));
return job;
}

// Tests for symbols.
// Test only that symbols are accessible as expected.
// No tests for features here as they should be tested in other tests.
// The latest pipeline supports calling `copyArtifacts` without `step`.

@Test
public void testDownstreamBuildSelector() throws Exception {
@@ -184,90 +168,102 @@ public void testDownstreamBuildSelector() throws Exception {
jenkinsRule.assertBuildStatusSuccess(upstream.scheduleBuild2(0));
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: downstream(upstreamProjectName: 'upstream', upstreamBuildNumber: '1')));"
"copyArtifacts(projectName: 'copiee', selector: downstream(upstreamProjectName: 'upstream', upstreamBuildNumber: '1'));"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}

@Test
public void testLastCompletedBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: lastCompleted()));"
"copyArtifacts(projectName: 'copiee', selector: lastCompleted());"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}

@Test
public void testParameterizedBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: buildParameter('<StatusBuildSelector/>')));"
"copyArtifacts(projectName: 'copiee', selector: buildParameter(\"${SELECTOR}\"));"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
copier.addProperty(new ParametersDefinitionProperty(
new BuildSelectorParameter(
"SELECTOR",
new StatusBuildSelector(),
""
)
));
// Build with default parameters
JenkinsRule.WebClient wc = jenkinsRule.createAllow405WebClient();
jenkinsRule.submit(wc.getPage(copier, "build").getFormByName("parameters"));
jenkinsRule.waitUntilNoActivity();

jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.getLastBuild()));
}

@Test
public void testPermalinkBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: permalink('lastStableBuild')));"
"copyArtifacts(projectName: 'copiee', selector: permalink('lastStableBuild'));"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}

@Test
public void testSavedBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0)).keepLog();

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: latestSavedBuild()));"
"copyArtifacts(projectName: 'copiee', selector: latestSavedBuild());"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}

@Test
public void testSpecificBuildSelector() throws Exception {
WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: specific(\"${build('copiee').number}\")));"
"copyArtifacts(projectName: 'copiee', selector: specific(\"${build('copiee').number}\"));"
+ "echo readFile('artifact.txt');"
);
createWorkflow(
jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
@@ -277,33 +273,33 @@ public void testSpecificBuildSelector() throws Exception {

@Test
public void testStatusBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: lastSuccessful()));"
"copyArtifacts(projectName: 'copiee', selector: lastSuccessful());"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}

@Test
public void testTriggeredBuildSelector() throws Exception {
WorkflowJob copiee = createWorkflow(
WorkflowJob copiee = jenkinsRule.createWorkflow(
"copiee",
"writeFile text: 'foobar', file: 'artifact.txt';"
+ "archive includes: 'artifact.txt';"
+ "build('copier');"
);

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: upstream()));"
"copyArtifacts(projectName: 'copiee', selector: upstream());"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));
@@ -312,15 +308,15 @@ public void testTriggeredBuildSelector() throws Exception {

@Test
public void testWorkspaceBuildSelector() throws Exception {
// workspace is useless for pipeline jobs
// workspace can copy only from `AbstractProject`s
FreeStyleProject copiee = jenkinsRule.createFreeStyleProject("copiee");
copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));

WorkflowJob copier = createWorkflow(
WorkflowJob copier = jenkinsRule.createWorkflow(
"copier",
"step(copyArtifacts(projectName: 'copiee', selector: workspace()));"
"copyArtifacts(projectName: 'copiee', selector: workspace());"
+ "echo readFile('artifact.txt');"
);
jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));

0 comments on commit ebf8b83

Please sign in to comment.