Search in sources :

Example 21 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class TriggeredBuildSelectorTest method getDownstreamAfterOverlappingFlow.

private String getDownstreamAfterOverlappingFlow(boolean allowUpstreamDependencies) throws Exception {
    // 
    // upstream   |   intermediate   |   downstream
    // 
    // Initial build
    // 1     #1 -------.
    // 2               '-----> #1 ---------.
    // 3                                   '-----> #1
    // 
    // Direct trigger of intermediate, then upstream
    // 4                 *---> #2 -------.
    // 5     #2 -----.                   |
    // 6             |                   '-------> #2
    // 7             '-------> #3 -----.
    // 8                               '---------> #3
    // 
    FreeStyleProject upstream = j.createFreeStyleProject("upstream");
    ParameterDefinition paramDef = new StringParameterDefinition("CONTENT", "foo");
    ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
    upstream.addProperty(paramsDef);
    FreeStyleProject intermediate = j.createFreeStyleProject("intermediate");
    FreeStyleProject downstream = j.createFreeStyleProject("downstream");
    upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "${CONTENT}"));
    upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
    upstream.getPublishersList().add(new BuildTrigger(intermediate.getName(), Result.SUCCESS));
    upstream.setQuietPeriod(0);
    intermediate.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(upstream.getName(), "", new TriggeredBuildSelector(true, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting, allowUpstreamDependencies), "artifact.txt", "", false, false, true));
    intermediate.getBuildersList().add(new SleepBuilder(1000));
    intermediate.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
    intermediate.getPublishersList().add(new BuildTrigger(downstream.getName(), Result.SUCCESS));
    intermediate.setQuietPeriod(0);
    downstream.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(upstream.getName(), "", new TriggeredBuildSelector(true, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting, allowUpstreamDependencies), "artifact.txt", "upstream/", false, false, true));
    downstream.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(intermediate.getName(), "", new TriggeredBuildSelector(true, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting, allowUpstreamDependencies), "artifact.txt", "intermediate/", false, false, true));
    downstream.getPublishersList().add(new ArtifactArchiver("upstream/artifact.txt,intermediate/artifact.txt", "", false, false));
    downstream.setQuietPeriod(0);
    upstream.save();
    intermediate.save();
    downstream.save();
    j.jenkins.rebuildDependencyGraph();
    // First (initial) build for each job
    j.assertBuildStatusSuccess(upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "upstreamValue1"))));
    j.waitUntilNoActivity();
    // Trigger directly an 'intermediate#2' build, which depends on 'upstream#1'
    intermediate.scheduleBuild2(0, new Cause.UserCause()).waitForStart();
    // 'intermediate#2' build is running. Meanwhile, a new 'upstream#2' is completing and triggers 'intermediate#3':
    upstream.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("CONTENT", "upstreamValue2")));
    j.waitUntilNoActivity();
    assertEquals("Number of upstream builds", 2, upstream.getBuilds().size());
    assertEquals("Number of intermediate builds", 3, intermediate.getBuilds().size());
    assertEquals("Number of downstream builds", 3, downstream.getBuilds().size());
    // Get the 'downstream#2' build ...
    FreeStyleBuild downstreamBuild2 = downstream.getBuildByNumber(2);
    assertNotNull(downstreamBuild2);
    j.assertBuildStatusSuccess(downstreamBuild2);
    // ... that were triggered by the directly triggered 'intermediate#2' build.
    Cause.UpstreamCause cause = downstreamBuild2.getCause(Cause.UpstreamCause.class);
    assertNotNull(cause);
    assertEquals("intermediate #2", cause.getUpstreamRun().getFullDisplayName());
    // Return artifacts from downstream#2. One of them was copied from 'upstream' that is
    // - either the last successful upstream#2 build
    // - or upstream#1, which is the same that the triggering intermediate#2 depends on
    // depending on the value of allowUpstreamDependencies
    String artifactFromUpstream = FileUtils.readFileToString(new File(downstreamBuild2.getArtifactsDir(), "upstream/artifact.txt"), "UTF-8");
    String artifactFromIntermediate = FileUtils.readFileToString(new File(downstreamBuild2.getArtifactsDir(), "intermediate/artifact.txt"), "UTF-8");
    return artifactFromIntermediate + "," + artifactFromUpstream;
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterValue(hudson.model.StringParameterValue) SleepBuilder(org.jvnet.hudson.test.SleepBuilder) 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) File(java.io.File) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition)

Example 22 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class TriggeredBuildSelectorTest method testUseOldest.

@Test
public void testUseOldest() throws Exception {
    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, TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest, 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)

Example 23 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class TriggeredBuildSelectorTest method testUseNewestByGlobalSetting.

@Test
public void testUseNewestByGlobalSetting() throws Exception {
    TriggeredBuildSelector.DescriptorImpl d = (TriggeredBuildSelector.DescriptorImpl) j.jenkins.getDescriptorOrDie(TriggeredBuildSelector.class);
    d.setGlobalUpstreamFilterStrategy(TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest);
    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, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting, 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("value3", 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 24 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class TriggeredBuildSelectorTest method testUseNewest.

@Test
public void testUseNewest() throws Exception {
    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, TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest, 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("value3", 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 25 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class TriggeredBuildSelectorTest method testUseNewestNested.

@Test
public void testUseNewestNested() 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.UseNewest, 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("value4", 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