|
@@ -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))); |
|
|