Search in sources :

Example 11 with DepthFirstScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method testPipelineFinishesFlowGraph.

/**
 * Verify that if we bomb out because we cannot resume, we at least try to finish the flow graph if we have something to work with.
 */
@Test
// Can be fleshed out later if we have a valid need for it.
@Ignore
public void testPipelineFinishesFlowGraph() throws Exception {
    final String[] logStart = new String[1];
    final List<FlowNode> nodesOut = new ArrayList<FlowNode>();
    story.addStepWithDirtyShutdown(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = createAndRunSleeperJob(story.j.jenkins, "durableAgainstClean", FlowDurabilityHint.PERFORMANCE_OPTIMIZED);
            Assert.assertEquals(FlowDurabilityHint.PERFORMANCE_OPTIMIZED, run.getExecution().getDurabilityHint());
            logStart[0] = JenkinsRule.getLog(run);
            if (run.getExecution() instanceof CpsFlowExecution) {
                // Pause and unPause to force persistence
                CpsFlowExecution cpsFlow = (CpsFlowExecution) (run.getExecution());
                cpsFlow.pause(true);
                long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS);
                while (System.nanoTime() < timeout && !cpsFlow.isPaused()) {
                    Thread.sleep(100L);
                }
                nodesOut.addAll(new DepthFirstScanner().allNodes(run.getExecution()));
                nodesOut.sort(FlowScanningUtils.ID_ORDER_COMPARATOR);
                cpsFlow.pause(false);
                timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS);
                while (System.nanoTime() < timeout && cpsFlow.isPaused()) {
                    Thread.sleep(100L);
                }
                // Ensures we're marked as can-not-resume
                cpsFlow.persistedClean = false;
                cpsFlow.saveOwner();
            }
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName("durableAgainstClean", WorkflowJob.class).getLastBuild();
            verifyFailedCleanly(story.j.jenkins, run);
            story.j.assertLogContains(logStart[0], run);
            assertIncludesNodes(nodesOut, run);
        }
    });
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with DepthFirstScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner in project workflow-cps-plugin by jenkinsci.

the class StepNodeTest method metastepConsoleShellClass.

@Test
public void metastepConsoleShellClass() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("node {\n" + "  wrap([$class: 'ConfigFileBuildWrapper', managedFiles: []]) {\n" + "    writeFile text: '''<testsuite name='a'><testcase name='c'><error>failed</error></testcase></testsuite>''', file: 'x.xml'\n" + "    step([$class: 'JUnitResultArchiver', testResults: 'x.xml'])\n" + "  }\n" + "}", true));
    WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0));
    List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
    assertThat(coreStepNodes, hasSize(1));
    assertEquals("junit", coreStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(JUnitResultArchiver.class).getDisplayName(), coreStepNodes.get(0).getDisplayName());
    List<FlowNode> coreWrapperStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), Predicates.and(new NodeStepTypePredicate("wrap"), new Predicate<FlowNode>() {

        @Override
        public boolean apply(FlowNode n) {
            return n instanceof StepStartNode && !((StepStartNode) n).isBody();
        }
    }));
    assertThat(coreWrapperStepNodes, hasSize(1));
    assertEquals("configFileProvider", coreWrapperStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(ConfigFileBuildWrapper.class).getDisplayName() + " : Start", coreWrapperStepNodes.get(0).getDisplayName());
    r.assertLogContains("[Pipeline] junit", b);
    r.assertLogContains("[Pipeline] configFileProvider", b);
    r.assertLogContains("[Pipeline] // configFileProvider", b);
}
Also used : NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ConfigFileBuildWrapper(org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Aggregations

FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)12 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)12 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)5 Predicate (com.google.common.base.Predicate)4 ConfigFileBuildWrapper (org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)4 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)4 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)4 FlowDurabilityHint (org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint)3 Statement (org.junit.runners.model.Statement)3 Action (hudson.model.Action)2 List (java.util.List)2 Jenkins (jenkins.model.Jenkins)2 Ignore (org.junit.Ignore)2 Issue (org.jvnet.hudson.test.Issue)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 XmlFile (hudson.XmlFile)1