Java源码示例:org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner
示例1
@Override
public List<BluePipelineStep> getPipelineNodeSteps(final String nodeId, Link parent) {
FlowExecution execution = run.getExecution();
if (execution == null) {
logger.debug(String.format("Pipeline %s, runid %s has null execution", run.getParent().getName(), run.getId()));
return Collections.emptyList();
}
DepthFirstScanner depthFirstScanner = new DepthFirstScanner();
//If blocked scope, get the end node
FlowNode n = depthFirstScanner
.findFirstMatch(execution.getCurrentHeads(),
input -> (input != null
&& input.getId().equals(nodeId)
&& (PipelineNodeUtil.isStage(input) || PipelineNodeUtil.isParallelBranch(input))));
if (n == null) { //if no node found or the node is not stage or parallel we return empty steps
return Collections.emptyList();
}
PipelineStepVisitor visitor = new PipelineStepVisitor(run, n);
ForkScanner.visitSimpleChunks(execution.getCurrentHeads(), visitor, new StageChunkFinder());
return visitor.getSteps()
.stream()
.map(node -> new PipelineStepImpl(node, parent))
.collect(Collectors.toList());
}
示例2
@Issue({"JENKINS-41758", "JENKINS-57827", "JENKINS-60886"})
@Test
public void declarative() throws Exception {
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
FlowNode podTemplateNode = new DepthFirstScanner().findFirstMatch(b.getExecution(), Predicates.and(new NodeStepTypePredicate("podTemplate"), FlowScanningUtils.hasActionPredicate(ArgumentsAction.class)));
assertNotNull("recorded arguments for podTemplate", podTemplateNode);
Map<String, Object> arguments = podTemplateNode.getAction(ArgumentsAction.class).getArguments();
@SuppressWarnings("unchecked")
List<UninstantiatedDescribable> containers = (List<UninstantiatedDescribable>) arguments.get("containers");
assertNotNull(containers);
assertFalse("no junk in arguments: " + arguments, containers.get(0).getArguments().containsKey("alwaysPullImage"));
FlowNode containerNode = new DepthFirstScanner().findFirstMatch(b.getExecution(), Predicates.and(new NodeStepTypePredicate("container"), FlowScanningUtils.hasActionPredicate(ArgumentsAction.class)));
assertNotNull("recorded arguments for container", containerNode);
// JENKINS-60886
UninstantiatedDescribable podRetention = (UninstantiatedDescribable) arguments.get("podRetention");
assertNotNull(podRetention);
assertTrue(podRetention.getModel().getType().equals(OnFailure.class));
}
示例3
/**
* Verifies that when publishIssues marks the build as unstable it also marks the step with
* WarningAction so that visualizations can display the step as unstable rather than just
* the whole build.
*
* @see <a href="http://issues.jenkins-ci.org/browse/JENKINS-39203">Issue 39203</a>
*/
@Test @org.jvnet.hudson.test.Issue("JENKINS-39203")
public void publishIssuesShouldMarkStepWithWarningAction() {
WorkflowJob job = createPipelineWithWorkspaceFiles("javac.txt");
job.setDefinition(asStage(createScanForIssuesStep(new Java(), "java"),
"publishIssues(issues:[java], qualityGates: [[threshold: 1, type: 'TOTAL', unstable: true]])"));
WorkflowRun run = (WorkflowRun)buildWithResult(job, Result.UNSTABLE);
FlowNode publishIssuesNode = new DepthFirstScanner().findFirstMatch(run.getExecution(),
node -> "publishIssues".equals(Objects.requireNonNull(node).getDisplayFunctionName()));
assertThat(publishIssuesNode).isNotNull();
WarningAction warningAction = publishIssuesNode.getPersistentAction(WarningAction.class);
assertThat(warningAction).isNotNull();
assertThat(warningAction.getMessage()).isEqualTo("Some quality gates have been missed: overall result is UNSTABLE");
}
示例4
/**
* Verifies that when recordIssues marks the build as unstable it also marks the step with
* WarningAction so that visualizations can display the step as unstable rather than just
* the whole build.
*
* @see <a href="http://issues.jenkins-ci.org/browse/JENKINS-39203">Issue 39203</a>
*/
@Test @org.jvnet.hudson.test.Issue("JENKINS-39203")
public void recordIssuesShouldMarkStepWithWarningAction() {
WorkflowJob job = createPipelineWithWorkspaceFiles("javac.txt");
job.setDefinition(asStage("recordIssues(tool: java(pattern:'**/*issues.txt', reportEncoding:'UTF-8'),"
+ "qualityGates: [[threshold: 1, type: 'TOTAL', unstable: true]])"));
WorkflowRun run = (WorkflowRun)buildWithResult(job, Result.UNSTABLE);
FlowNode publishIssuesNode = new DepthFirstScanner().findFirstMatch(run.getExecution(),
node -> "recordIssues".equals(Objects.requireNonNull(node).getDisplayFunctionName()));
assertThat(publishIssuesNode).isNotNull();
WarningAction warningAction = publishIssuesNode.getPersistentAction(WarningAction.class);
assertThat(warningAction).isNotNull();
assertThat(warningAction.getMessage()).isEqualTo("Some quality gates have been missed: overall result is UNSTABLE");
}
示例5
static Result resultForStage(FlowNode startNode, FlowNode endNode) {
Result errorResult = Result.SUCCESS;
DepthFirstScanner scanner = new DepthFirstScanner();
if (scanner.setup(endNode, Collections.singletonList(startNode))) {
WarningAction warningAction = StreamSupport.stream(scanner.spliterator(), false)
.map(node -> node.getPersistentAction(WarningAction.class))
.filter(Objects::nonNull)
.max(Comparator.comparing(warning -> warning.getResult().ordinal))
.orElse(null);
if (warningAction != null) {
errorResult = warningAction.getResult();
}
}
return errorResult;
}
示例6
private void testExpressions(List<TestDataBuilder.Data> testData, Result expectedResult) throws Exception {
WorkflowJob job = j.createProject(WorkflowJob.class);
StringBuilder str = new StringBuilder();
for (TestDataBuilder.Data data : testData) {
str.append(data.expression()).append('\n');
}
job.setDefinition(new CpsFlowDefinition(str.toString(), true));
run = j.buildAndAssertStatus(expectedResult, job);
DepthFirstScanner scanner = new DepthFirstScanner();
assertThat(scanner.filteredNodes(run.getExecution(), new NodeStepTypePredicate("compareVersions")), hasSize(testData.size()));
}
示例7
@Issue("JENKINS-48250")
@Test
public void emptyFails() throws Exception {
WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "emptyFails");
j.setDefinition(new CpsFlowDefinition("stage('first') {\n" +
" node {\n" +
(Functions.isWindows() ?
" bat 'echo hi'\n" :
" sh 'echo hi'\n") +
" junit('*.xml')\n" +
" }\n" +
"}\n", true));
WorkflowRun r = j.scheduleBuild2(0).waitForStart();
rule.assertBuildStatus(Result.FAILURE, rule.waitForCompletion(r));
rule.assertLogContains("ERROR: " + Messages.JUnitResultArchiver_NoTestReportFound(), r);
FlowExecution execution = r.getExecution();
DepthFirstScanner scanner = new DepthFirstScanner();
FlowNode f = scanner.findFirstMatch(execution, new Predicate<FlowNode>() {
@Override
public boolean apply(@Nullable FlowNode input) {
return input instanceof StepAtomNode &&
((StepAtomNode) input).getDescriptor() instanceof JUnitResultsStep.DescriptorImpl;
}
});
assertNotNull(f);
LogAction logAction = f.getPersistentAction(LogAction.class);
assertNotNull(logAction);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
logAction.getLogText().writeRawLogTo(0, baos);
String log = baos.toString();
assertThat(log, CoreMatchers.containsString(Messages.JUnitResultArchiver_NoTestReportFound()));
}
示例8
public static void assertBranchResults(WorkflowRun run, int suiteCount, int testCount, int failCount, String branchName, String stageName,
String innerStageName) {
FlowExecution execution = run.getExecution();
DepthFirstScanner scanner = new DepthFirstScanner();
BlockStartNode aBranch = (BlockStartNode)scanner.findFirstMatch(execution, branchForName(branchName));
assertNotNull(aBranch);
TestResult branchResult = assertBlockResults(run, suiteCount, testCount, failCount, aBranch);
String namePrefix = stageName + " / " + branchName;
if (innerStageName != null) {
namePrefix += " / " + innerStageName;
}
for (CaseResult c : branchResult.getPassedTests()) {
assertEquals(namePrefix + " / " + c.getTransformedTestName(), c.getDisplayName());
}
}
示例9
public static void assertStageResults(WorkflowRun run, int suiteCount, int testCount, int failCount, String stageName) {
FlowExecution execution = run.getExecution();
DepthFirstScanner scanner = new DepthFirstScanner();
BlockStartNode aStage = (BlockStartNode)scanner.findFirstMatch(execution, stageForName(stageName));
assertNotNull(aStage);
assertBlockResults(run, suiteCount, testCount, failCount, aStage);
}
示例10
private static List<FlowNode> findJUnitSteps(BlockStartNode blockStart) {
return new DepthFirstScanner().filteredNodes(
Collections.singletonList(blockStart.getEndNode()),
Collections.singletonList(blockStart),
node -> node instanceof StepAtomNode &&
((StepAtomNode) node).getDescriptor() instanceof JUnitResultsStep.DescriptorImpl
);
}