use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class LastCompletedBuildSelectorTest method testCopyFromBuilds.
@Test
public void testCopyFromBuilds() throws Exception {
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();
upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "${BUILD_ID}"));
upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
downstream.getBuildersList().add(new CopyArtifact(upstream.getFullName(), "", new LastCompletedBuildSelector(), "**/*", "", "", false, false, true));
downstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
// Copy from a job without builds
{
assertNull(upstream.getLastBuild());
j.assertBuildStatus(Result.FAILURE, downstream.scheduleBuild2(0).get());
}
// Copy from a job with a succeeded build
{
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(upstreamBuild);
FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(downstreamBuild);
File artifact = new File(downstreamBuild.getArtifactsDir(), "artifact.txt");
assertEquals(upstreamBuild.getId(), FileUtils.readFileToString(artifact, "UTF-8"));
}
// Copy from a job with a unstable build
{
upstream.getBuildersList().add(new UnstableBuilder());
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
j.assertBuildStatus(Result.UNSTABLE, upstreamBuild);
FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(downstreamBuild);
File artifact = new File(downstreamBuild.getArtifactsDir(), "artifact.txt");
assertEquals(upstreamBuild.getId(), FileUtils.readFileToString(artifact, "UTF-8"));
upstream.getBuildersList().removeAll(UnstableBuilder.class);
}
// Copy from a job with a failed build
{
upstream.getBuildersList().add(new FailureBuilder());
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
j.assertBuildStatus(Result.FAILURE, upstreamBuild);
FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(downstreamBuild);
File artifact = new File(downstreamBuild.getArtifactsDir(), "artifact.txt");
assertEquals(upstreamBuild.getId(), FileUtils.readFileToString(artifact, "UTF-8"));
upstream.getBuildersList().removeAll(FailureBuilder.class);
}
// Copy from a job with an aborted build
{
upstream.getBuildersList().add(new SleepBuilder(60000));
upstream.scheduleBuild2(0);
FreeStyleBuild upstreamBuild = waitForBuildStarts(upstream, 5000);
upstreamBuild.getExecutor().interrupt();
j.waitUntilNoActivity();
j.assertBuildStatus(Result.ABORTED, upstreamBuild);
FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(downstreamBuild);
File artifact = new File(downstreamBuild.getArtifactsDir(), "artifact.txt");
assertEquals(upstreamBuild.getId(), FileUtils.readFileToString(artifact, "UTF-8"));
upstream.getBuildersList().removeAll(SleepBuilder.class);
}
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class ParameterizedBuildSelectorTest method testImmediateValue.
/**
* Also accepts immediate value.
*
* @throws Exception
*/
@Test
public void testImmediateValue() throws Exception {
// Prepare an artifact to be copied.
FreeStyleProject copiee = j.createFreeStyleProject();
copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
j.assertBuildStatusSuccess(copiee.scheduleBuild2(0));
WorkflowJob copier = createWorkflowJob();
ParameterDefinition paramDef = new StringParameterDefinition("SELECTOR", "<StatusBuildSelector><stable>true</stable></StatusBuildSelector>");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
copier.addProperty(paramsDef);
copier.setDefinition(new CpsFlowDefinition(String.format("node {" + "step([$class: 'CopyArtifact'," + "projectName: '%1$s'," + "filter: '**/*'," + "selector: [$class: 'ParameterizedBuildSelector', parameterName: '${SELECTOR}']," + "]);" + "step([$class: 'ArtifactArchiver', artifacts: '**/*']);" + "}", copiee.getFullName()), true));
WorkflowRun b = j.assertBuildStatusSuccess(copier.scheduleBuild2(0, null, new ParametersAction(new StringParameterValue("SELECTOR", "<StatusBuildSelector><stable>true</stable></StatusBuildSelector>"))));
VirtualFile vf = b.getArtifactManager().root().child("artifact.txt");
try (InputStream in = vf.open()) {
assertEquals("foobar", IOUtils.toString(in, "UTF-8"));
}
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class ParameterizedBuildSelectorTest method testWorkflow.
/**
* Also applicable for workflow jobs.
*
* @throws Exception
*/
@Issue("JENKINS-30357")
@Test
public void testWorkflow() throws Exception {
// Prepare an artifact to be copied.
FreeStyleProject copiee = j.createFreeStyleProject();
copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
j.assertBuildStatusSuccess(copiee.scheduleBuild2(0));
WorkflowJob copier = createWorkflowJob();
ParameterDefinition paramDef = new StringParameterDefinition("SELECTOR", "<StatusBuildSelector><stable>true</stable></StatusBuildSelector>");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
copier.addProperty(paramsDef);
copier.setDefinition(new CpsFlowDefinition(String.format("node {" + "step([$class: 'CopyArtifact'," + "projectName: '%1$s'," + "filter: '**/*'," + "selector: [$class: 'ParameterizedBuildSelector', parameterName: 'SELECTOR']," + "]);" + "step([$class: 'ArtifactArchiver', artifacts: '**/*']);" + "}", copiee.getFullName()), true));
WorkflowRun b = j.assertBuildStatusSuccess(copier.scheduleBuild2(0, null, new ParametersAction(new StringParameterValue("SELECTOR", "<StatusBuildSelector><stable>true</stable></StatusBuildSelector>"))));
VirtualFile vf = b.getArtifactManager().root().child("artifact.txt");
try (InputStream in = vf.open()) {
assertEquals("foobar", IOUtils.toString(in, "UTF-8"));
}
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class TriggeredBuildSelectorTest method testUseOldestNested.
@Test
public void testUseOldestNested() throws Exception {
FreeStyleProject upstream = j.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("CONTENT", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
upstream.addProperty(paramsDef);
FreeStyleProject intermediate1 = j.createFreeStyleProject();
FreeStyleProject intermediate2 = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();
upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "${CONTENT}"));
intermediate1.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
intermediate1.setQuietPeriod(5);
upstream.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
intermediate2.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
intermediate2.setQuietPeriod(5);
upstream.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
intermediate1.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
downstream.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(upstream.getName(), "", new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest, false), "artifact.txt", "", false, false, true));
// this allows upstream trigger can be merged.
downstream.setQuietPeriod(5);
upstream.save();
intermediate1.save();
intermediate2.save();
downstream.save();
j.jenkins.rebuildDependencyGraph();
// 2 upstream builds for intermediate1.
upstream.getPublishersList().clear();
upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
upstream.getPublishersList().add(new BuildTrigger(intermediate1.getName(), Result.SUCCESS));
upstream.save();
j.jenkins.rebuildDependencyGraph();
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value1"))));
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value2"))));
// 2 upstream builds for intermediate2.
upstream.getPublishersList().clear();
upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
upstream.getPublishersList().add(new BuildTrigger(intermediate2.getName(), Result.SUCCESS));
upstream.save();
j.jenkins.rebuildDependencyGraph();
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value3"))));
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value4"))));
// wait till downstream will be triggered and completed
j.waitUntilNoActivity();
assertNotNull(intermediate1.getLastBuild());
j.assertBuildStatusSuccess(intermediate1.getLastBuild());
assertEquals(String.format("upstream triggers seem not to be merged into one downstream build. This means quietPeriod of intermediate1 is too short in this environment: %s", intermediate1.getLastBuild().getCauses()), 2, Util.filter(intermediate1.getLastBuild().getCauses(), Cause.UpstreamCause.class).size());
assertNotNull(intermediate2.getLastBuild());
j.assertBuildStatusSuccess(intermediate2.getLastBuild());
assertEquals(String.format("upstream triggers seem not to be merged into one downstream build. This means quietPeriod of intermediate2 is too short in this environment: %s", intermediate2.getLastBuild().getCauses()), 2, Util.filter(intermediate2.getLastBuild().getCauses(), Cause.UpstreamCause.class).size());
FreeStyleBuild b = downstream.getLastBuild();
assertNotNull(b);
j.assertBuildStatusSuccess(b);
assertEquals(String.format("upstream triggers seem not to be merged into one downstream build. This means quietPeriod of downstream is too short in this environment: %s", b.getCauses()), 2, Util.filter(b.getCauses(), Cause.UpstreamCause.class).size());
assertEquals("value1", b.getWorkspace().child("artifact.txt").readToString());
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class TriggeredBuildSelectorTest method testBackwardCompatibility.
@Test
public void testBackwardCompatibility() throws Exception {
TriggeredBuildSelector.DescriptorImpl d = (TriggeredBuildSelector.DescriptorImpl) j.jenkins.getDescriptorOrDie(TriggeredBuildSelector.class);
d.setGlobalUpstreamFilterStrategy(TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest);
FreeStyleProject upstream = j.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("CONTENT", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
upstream.addProperty(paramsDef);
FreeStyleProject downstream = j.createFreeStyleProject();
upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "${CONTENT}"));
upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
upstream.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
downstream.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(upstream.getName(), "", new TriggeredBuildSelector(false, null, false), "artifact.txt", "", false, false, true));
// this allows upstream trigger can be merged.
downstream.setQuietPeriod(5);
upstream.save();
downstream.save();
j.jenkins.rebuildDependencyGraph();
// 3 upstream builds.
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value1"))));
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value2"))));
j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "value3"))));
// wait till downstream will be triggered and completed
j.waitUntilNoActivity();
FreeStyleBuild b = downstream.getLastBuild();
assertNotNull(b);
j.assertBuildStatusSuccess(b);
assertEquals(String.format("upstream triggers seem not to be merged into one downstream build. This means quietPeriod of downstream is too short in this environment: %s", b.getCauses()), 3, Util.filter(b.getCauses(), Cause.UpstreamCause.class).size());
assertEquals("value1", b.getWorkspace().child("artifact.txt").readToString());
}
Aggregations