Search in sources :

Example 1 with FlowGraphWalker

use of org.jenkinsci.plugins.workflow.graph.FlowGraphWalker in project workflow-cps-plugin by jenkinsci.

the class CpsScriptTest method dumpError.

/**
 * Picks up any errors recorded in {@link #exec}.
 */
private String dumpError() {
    StringBuilder msg = new StringBuilder();
    FlowGraphWalker walker = new FlowGraphWalker(exec);
    for (FlowNode n : walker) {
        ErrorAction e = n.getAction(ErrorAction.class);
        if (e != null) {
            msg.append(Functions.printThrowable(e.getError()));
        }
    }
    return msg.toString();
}
Also used : ErrorAction(org.jenkinsci.plugins.workflow.actions.ErrorAction) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 2 with FlowGraphWalker

use of org.jenkinsci.plugins.workflow.graph.FlowGraphWalker in project workflow-cps-plugin by jenkinsci.

the class CpsBodyExecutionTest method synchronousExceptionInBody.

/**
 * When the body of a step is synchronous and explodes, the failure should be recorded and the pipeline job
 * should move on.
 *
 * But instead, this hangs because CpsBodyExecution has a bug in how it handles this case.
 * It tries to launch the body (in this case the 'bodyBlock' method) in a separate CPS thread,
 * and puts the parent CPS thread on hold. Yet when the child CPS thread ends with an exception,
 * it fails to record this result correctly, and it gets into the eternal sleep in which
 * the parent CPS thread expects to be notified of the outcome of the child CPS thread, which
 * never arrives.
 */
@Test
public void synchronousExceptionInBody() throws Exception {
    WorkflowJob p = jenkins.createProject(WorkflowJob.class);
    p.setDefinition(new CpsFlowDefinition("synchronousExceptionInBody()", true));
    WorkflowRun b = jenkins.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0));
    jenkins.assertLogContains(EmperorHasNoClothes.class.getName(), b);
    {
        // assert the shape of FlowNodes
        FlowGraphWalker w = new FlowGraphWalker(b.getExecution());
        List<String> nodes = new ArrayList<>();
        for (FlowNode n : w) {
            String s = n.getClass().getSimpleName();
            if (n instanceof StepNode) {
                StepNode sn = (StepNode) n;
                s += ":" + sn.getDescriptor().getFunctionName();
            }
            if (n instanceof StepEndNode) {
                // this should have recorded a failure
                ErrorAction e = n.getAction(ErrorAction.class);
                assertEquals(EmperorHasNoClothes.class, e.getError().getClass());
            }
            nodes.add(s);
        }
        assertEquals(Arrays.asList("FlowEndNode", // this for the end of invoking a body
        "StepEndNode:synchronousExceptionInBody", // this for invoking a body
        "StepStartNode:synchronousExceptionInBody", // letting steps take over the FlowNode creation that should solve this.
        "StepAtomNode:synchronousExceptionInBody", "FlowStartNode"), nodes);
    }
}
Also used : ErrorAction(org.jenkinsci.plugins.workflow.actions.ErrorAction) StepNode(org.jenkinsci.plugins.workflow.cps.nodes.StepNode) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) ArrayList(java.util.ArrayList) List(java.util.List) StepEndNode(org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 3 with FlowGraphWalker

use of org.jenkinsci.plugins.workflow.graph.FlowGraphWalker in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method basics.

@Test
public void basics() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    CpsScmFlowDefinition def = new CpsScmFlowDefinition(new SingleFileSCM("flow.groovy", "echo 'hello from SCM'"), "flow.groovy");
    // currently the default, but just to be clear that we do rely on that in this test
    def.setLightweight(false);
    p.setDefinition(def);
    WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
    // TODO currently the log text is in Run.log, but not on FlowStartNode/LogAction, so not visible from Workflow Steps etc.
    r.assertLogContains("hello from SCM", b);
    r.assertLogContains("Staging flow.groovy", b);
    r.assertLogNotContains("Retrying after 10 seconds", b);
    FlowGraphWalker w = new FlowGraphWalker(b.getExecution());
    int workspaces = 0;
    for (FlowNode n : w) {
        if (n.getAction(WorkspaceAction.class) != null) {
            workspaces++;
        }
    }
    assertEquals(1, workspaces);
}
Also used : WorkspaceAction(org.jenkinsci.plugins.workflow.actions.WorkspaceAction) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) SingleFileSCM(org.jvnet.hudson.test.SingleFileSCM) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 4 with FlowGraphWalker

use of org.jenkinsci.plugins.workflow.graph.FlowGraphWalker in project workflow-job-plugin by jenkinsci.

the class FlowGraphAction method getNodes.

@Exported
public Collection<? extends FlowNode> getNodes() {
    FlowExecution exec = run.getExecution();
    if (exec == null) {
        return Collections.emptySet();
    }
    List<FlowNode> nodes = new ArrayList<>();
    FlowGraphWalker walker = new FlowGraphWalker(exec);
    for (FlowNode n : walker) {
        nodes.add(n);
    }
    Collections.reverse(nodes);
    return nodes;
}
Also used : FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) ArrayList(java.util.ArrayList) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Exported(org.kohsuke.stapler.export.Exported)

Example 5 with FlowGraphWalker

use of org.jenkinsci.plugins.workflow.graph.FlowGraphWalker in project workflow-job-plugin by jenkinsci.

the class GraphVizAction method writeDot.

private void writeDot(PrintWriter w) throws IOException {
    try {
        w.println("digraph G {");
        FlowGraphWalker walker = new FlowGraphWalker(run.getExecution());
        for (FlowNode n : walker) {
            for (FlowNode p : n.getParents()) {
                w.printf("%s -> %s%n", p.getId(), n.getId());
            }
            if (n instanceof BlockStartNode) {
                BlockStartNode sn = (BlockStartNode) n;
                w.printf("%s [shape=trapezium]%n", n.getId());
            } else if (n instanceof BlockEndNode) {
                BlockEndNode sn = (BlockEndNode) n;
                w.printf("%s [shape=invtrapezium]%n", n.getId());
                w.printf("%s -> %s [style=dotted]%n", sn.getStartNode().getId(), n.getId());
            }
            w.printf("%s [label=\"%s: %s\"]%n", n.getId(), n.getId(), n.getDisplayName());
        }
        w.println("}");
    } finally {
        w.close();
    }
}
Also used : BlockStartNode(org.jenkinsci.plugins.workflow.graph.BlockStartNode) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) BlockEndNode(org.jenkinsci.plugins.workflow.graph.BlockEndNode) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Aggregations

FlowGraphWalker (org.jenkinsci.plugins.workflow.graph.FlowGraphWalker)6 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)6 ArrayList (java.util.ArrayList)2 ErrorAction (org.jenkinsci.plugins.workflow.actions.ErrorAction)2 BlockEndNode (org.jenkinsci.plugins.workflow.graph.BlockEndNode)2 BlockStartNode (org.jenkinsci.plugins.workflow.graph.BlockStartNode)2 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)2 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)2 Test (org.junit.Test)2 Action (hudson.model.Action)1 List (java.util.List)1 JSONArray (net.sf.json.JSONArray)1 JSONObject (net.sf.json.JSONObject)1 WorkspaceAction (org.jenkinsci.plugins.workflow.actions.WorkspaceAction)1 StepEndNode (org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode)1 StepNode (org.jenkinsci.plugins.workflow.cps.nodes.StepNode)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 StepNode (org.jenkinsci.plugins.workflow.graph.StepNode)1 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)1 SingleFileSCM (org.jvnet.hudson.test.SingleFileSCM)1