|
| 1 | +package com.cloudbees.plugins.flow |
| 2 | + |
| 3 | +import com.gargoylesoftware.htmlunit.html.HtmlPage |
| 4 | +import hudson.model.Cause |
| 5 | +import hudson.model.CauseAction |
| 6 | +import org.junit.Rule |
| 7 | +import org.junit.Test |
| 8 | +import org.jvnet.hudson.test.JenkinsRule |
| 9 | + |
| 10 | +import static org.junit.Assert.assertTrue |
| 11 | + |
| 12 | +class FlowCauseTest { |
| 13 | + |
| 14 | + @Rule |
| 15 | + public JenkinsRule j = new JenkinsRule() |
| 16 | + |
| 17 | + protected BuildFlow createFlow(String name, String dsl) { |
| 18 | + def job = j.jenkins.createProject(BuildFlow, name) |
| 19 | + job.dsl = dsl |
| 20 | + job.save() |
| 21 | + return job |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Given a pipeline A --> FLOW --> B, then the build for a B build must display Flow <-- A. |
| 26 | + */ |
| 27 | + @Test |
| 28 | + void 'Flow cause not blocking the upstream causes'() { |
| 29 | + // Jobs |
| 30 | + def a = createFlow('A', 'build("B")') |
| 31 | + def b = createFlow('B', 'build("C")') |
| 32 | + def c = createFlow('C', '') |
| 33 | + |
| 34 | + // Triggers A |
| 35 | + j.assertBuildStatusSuccess(a.scheduleBuild2(0, new Cause.UserIdCause())) |
| 36 | + |
| 37 | + // Checks the builds |
| 38 | + j.assertBuildStatusSuccess(b.builds.lastBuild) |
| 39 | + |
| 40 | + // Last build in the sequence |
| 41 | + def build = c.builds.lastBuild |
| 42 | + j.assertBuildStatusSuccess(build) |
| 43 | + |
| 44 | + // Checks the cause tree |
| 45 | + def causeAction = build.actions.find { it instanceof CauseAction } as CauseAction |
| 46 | + assert causeAction != null: "A cause action must be associated with the build" |
| 47 | + assert causeAction.causes.size() == 1 |
| 48 | + def cause = causeAction.causes.first() as Cause.UpstreamCause |
| 49 | + assert cause.shortDescription == 'Started by build flow B#1' |
| 50 | + def upstreamCause = cause.upstreamCauses.first() as Cause.UpstreamCause |
| 51 | + assert upstreamCause.shortDescription == 'Started by upstream project "A" build number 1' |
| 52 | + |
| 53 | + // Goes to the build status page of C |
| 54 | + HtmlPage page = j.createWebClient().goTo(c.builds.lastBuild.url) |
| 55 | + assert page.titleText == 'C #1 [Jenkins]' |
| 56 | + |
| 57 | + // Just checking the different causes are in the page (a bit primitive) |
| 58 | + def bPosition = page.body.textContent.indexOf('Started by build flow B') |
| 59 | + assertTrue(bPosition > 0) |
| 60 | + assertTrue(page.body.textContent.indexOf('Started by upstream project A build number 1') > bPosition) |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments