Search in sources :

Example 16 with FileWriteBuilder

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);
    }
}
Also used : FailureBuilder(org.jvnet.hudson.test.FailureBuilder) ArtifactArchiver(hudson.tasks.ArtifactArchiver) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) SleepBuilder(org.jvnet.hudson.test.SleepBuilder) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) File(java.io.File) UnstableBuilder(org.jvnet.hudson.test.UnstableBuilder) Test(org.junit.Test)

Example 17 with FileWriteBuilder

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"));
    }
}
Also used : VirtualFile(jenkins.util.VirtualFile) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterDefinition(hudson.model.StringParameterDefinition) InputStream(java.io.InputStream) StringParameterValue(hudson.model.StringParameterValue) FreeStyleProject(hudson.model.FreeStyleProject) ParametersAction(hudson.model.ParametersAction) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) StringParameterDefinition(hudson.model.StringParameterDefinition) ParameterDefinition(hudson.model.ParameterDefinition) Test(org.junit.Test)

Example 18 with FileWriteBuilder

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"));
    }
}
Also used : VirtualFile(jenkins.util.VirtualFile) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterDefinition(hudson.model.StringParameterDefinition) InputStream(java.io.InputStream) StringParameterValue(hudson.model.StringParameterValue) FreeStyleProject(hudson.model.FreeStyleProject) ParametersAction(hudson.model.ParametersAction) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) StringParameterDefinition(hudson.model.StringParameterDefinition) ParameterDefinition(hudson.model.ParameterDefinition) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 19 with FileWriteBuilder

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());
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterValue(hudson.model.StringParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) ParametersAction(hudson.model.ParametersAction) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) Cause(hudson.model.Cause) BuildTrigger(hudson.tasks.BuildTrigger) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Example 20 with FileWriteBuilder

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());
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterValue(hudson.model.StringParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) ParametersAction(hudson.model.ParametersAction) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) Cause(hudson.model.Cause) BuildTrigger(hudson.tasks.BuildTrigger) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Aggregations

FileWriteBuilder (hudson.plugins.copyartifact.testutils.FileWriteBuilder)37 ArtifactArchiver (hudson.tasks.ArtifactArchiver)37 Test (org.junit.Test)32 FreeStyleProject (hudson.model.FreeStyleProject)26 FreeStyleBuild (hudson.model.FreeStyleBuild)17 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)15 StringParameterDefinition (hudson.model.StringParameterDefinition)15 ParametersAction (hudson.model.ParametersAction)13 StringParameterValue (hudson.model.StringParameterValue)13 ParameterDefinition (hudson.model.ParameterDefinition)11 BuildTrigger (hudson.tasks.BuildTrigger)11 MockAuthorizationStrategy (org.jvnet.hudson.test.MockAuthorizationStrategy)11 Cause (hudson.model.Cause)10 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)9 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)6 CopyArtifact (hudson.plugins.copyartifact.CopyArtifact)4 File (java.io.File)4 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)4 Axis (hudson.matrix.Axis)3 AxisList (hudson.matrix.AxisList)3