Java Code Examples for org.jenkinsci.plugins.workflow.job.WorkflowRun#getExecution()

The following examples show how to use org.jenkinsci.plugins.workflow.job.WorkflowRun#getExecution() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PipelineNodeGraphVisitor.java    From blueocean-plugin with MIT License 6 votes vote down vote up
public PipelineNodeGraphVisitor(WorkflowRun run) {
    this.run = run;
    this.inputAction = run.getAction(InputAction.class);
    this.pipelineActions = new HashSet<>();
    this.pendingActionsForBranches = new HashMap<>();
    declarative = run.getAction(ExecutionModelAction.class) != null;
    FlowExecution execution = run.getExecution();
    if (execution != null) {
        try {
            ForkScanner.visitSimpleChunks(execution.getCurrentHeads(), this, new StageChunkFinder());
        } catch (final Throwable t) {
            // Log run ID, because the eventual exception handler (probably Stapler) isn't specific enough to do so
            logger.error("Caught a " + t.getClass().getSimpleName() +
                             " traversing the graph for run " + run.getExternalizableId());
            throw t;
        }
    } else {
        logger.debug("Could not find execution for run " + run.getExternalizableId());
    }
}
 
Example 2
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Issue("JENKINS-47158")
@Test
public void syntheticParallelFlowNodeNotSaved() throws Exception {
    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    p.setDefinition(new CpsFlowDefinition("parallel a: {\n" +
                                              "    node {\n" +
                                              "        echo 'a'\n" +
                                              "    }\n" +
                                              "}, b: {\n" +
                                              "    node {\n" +
                                              "        echo 'b'\n" +
                                              "    }\n" +
                                              "}\n", true));
    WorkflowRun b = j.buildAndAssertSuccess(p);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    FlowExecution rawExec = b.getExecution();
    assert (rawExec instanceof CpsFlowExecution);
    CpsFlowExecution execution = (CpsFlowExecution) rawExec;
    File storage = execution.getStorageDir();

    // Nodes 5 and 6 are the parallel branch start nodes. Make sure no "5-parallel-synthetic.xml" and "6..." files
    // exist in the storage directory, showing we haven't saved them.
    assertFalse(new File(storage, "5-parallel-synthetic.xml").exists());
    assertFalse(new File(storage, "6-parallel-synthetic.xml").exists());
}
 
Example 3
Source File: JUnitResultsStepTest.java    From junit-plugin with MIT License 5 votes vote down vote up
@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()));
}
 
Example 4
Source File: JUnitResultsStepTest.java    From junit-plugin with MIT License 5 votes vote down vote up
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());
    }
}
 
Example 5
Source File: JUnitResultsStepTest.java    From junit-plugin with MIT License 5 votes vote down vote up
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);
}
 
Example 6
Source File: PipelineHelper.java    From jenkins-build-monitor-plugin with MIT License 5 votes vote down vote up
public static List<String> getPipelines(Run<?, ?> run) {
    WorkflowRun currentBuild = (WorkflowRun) run;
    FlowExecution execution = currentBuild.getExecution();
    if (execution != null) {
        WorkflowNodeTraversal traversal = new WorkflowNodeTraversal();
        traversal.start(execution.getCurrentHeads());
        return traversal.getStages();
    }
    return Collections.emptyList();
}
 
Example 7
Source File: LockStepTestBase.java    From lockable-resources-plugin with MIT License 5 votes vote down vote up
protected void isPaused(WorkflowRun run, int count, int effectivePauses) {
  int pauseActions = 0, pausedActions = 0;
  for (FlowNode node : new FlowGraphWalker(run.getExecution())) {
    for (PauseAction pauseAction : PauseAction.getPauseActions(node)) {
      ++pauseActions;
      if (pauseAction.isPaused()) {
        ++pausedActions;
      }
    }
  }
  assertEquals(count, pauseActions);
  assertEquals(effectivePauses, pausedActions);
}
 
Example 8
Source File: LinkResolverTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void resolveNodeLink() throws Exception {
    {
        WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
        job1.setDefinition(new CpsFlowDefinition("stage \"Build\"\n" +
            "    node {\n" +
            "       sh \"echo here\"\n" +
            "    }\n" +
            "\n" +
            "stage \"Test\"\n" +
            "    parallel (\n" +
            "        \"Firefox\" : {\n" +
            "            node {\n" +
            "                sh \"echo ffox\"\n" +
            "            }\n" +
            "        },\n" +
            "        \"Chrome\" : {\n" +
            "            node {\n" +
            "                sh \"echo chrome\"\n" +
            "            }\n" +
            "        }\n" +
            "    )\n" +
            "\n" +
            "stage \"CrashyMcgee\"\n" +
            "  parallel (\n" +
            "    \"SlowButSuccess\" : {\n" +
            "        node {\n" +
            "            echo 'This is time well spent.'\n" +
            "        }\n" +
            "    },\n" +
            "    \"DelayThenFail\" : {\n" +
            "        node {\n" +
            "            echo 'Not yet.'\n" +
            "        }\n" +
            "    }\n" +
            "  )\n" +
            "\n" +
            "\n" +
            "stage \"Deploy\"\n" +
            "    node {\n" +
            "        sh \"echo deploying\"\n" +
            "    }"));

        WorkflowRun b1 = job1.scheduleBuild2(0).get();
        j.assertBuildStatusSuccess(b1);

        FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
        nodeGraphTable.build();
        List<FlowNode> nodes = getStages(nodeGraphTable);
        List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);

        Assert.assertEquals(String.format("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/%s/nodes/%s/",
            b1.getId(),nodes.get(0).getId()),
            LinkResolver.resolveLink(nodes.get(0)).getHref());

        Assert.assertEquals(String.format("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/%s/nodes/%s/",
            b1.getId(),parallelNodes.get(0).getId()),
            LinkResolver.resolveLink(parallelNodes.get(0)).getHref());

        NodeGraphBuilder graphBuilder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);

        List<BluePipelineStep> steps = graphBuilder.getPipelineNodeSteps(new Link(String.format("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/%s/steps/", b1.getId())));

        Assert.assertEquals(String.format("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/%s/steps/%s/",
            b1.getId(),steps.get(0).getId()),
            LinkResolver.resolveLink(steps.get(0)).getHref());

    }
}
 
Example 9
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getPipelineStepsTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");


    job1.setDefinition(new CpsFlowDefinition("stage ('build') {\n" +
                                                 "    node{\n" +
                                                 "        sh \"echo Building...\"\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('test') {\n" +
                                                 "    parallel 'unit':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"Unit testing...\"\n" +
                                                 "            sh \"echo Tests running\"\n" +
                                                 "            sh \"echo Tests completed\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'integration':{\n" +
                                                 "        node{\n" +
                                                 "            echo \"Integration testing...\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'ui':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"UI testing...\"\n" +
                                                 "        }\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "node{\n" +
                                                 "  echo \"Done Testing\"\n" +
                                                 "}\n" +
                                                 "stage ('deploy') {\n" +
                                                 "    node{\n" +
                                                 "        echo \"Deploying\"\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('deployToProd') {\n" +
                                                 "    node{\n" +
                                                 "        echo \"Deploying to production\"\n" +
                                                 "    }\n" +
                                                 "}", false
    ));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);

    FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
    nodeGraphTable.build();
    List<FlowNode> nodes = getStages(nodeGraphTable);
    List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);

    Assert.assertEquals(7, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());

    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + nodes.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(6, resp.size());

    Map step = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + parallelNodes.get(0).getId() + "/steps/" + resp.get(0).get("id"), Map.class);

    assertNotNull(step);

    String stepLog = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + parallelNodes.get(0).getId() + "/steps/" + resp.get(0).get("id") + "/log", String.class);
    assertNotNull(stepLog);
}
 
Example 10
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getPipelineWihNodesAllStepsTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");


    job1.setDefinition(new CpsFlowDefinition("stage ('build') {\n" +
                                                 "    node{\n" +
                                                 "        sh \"echo Building...\"\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('test') {\n" +
                                                 "    parallel 'unit':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"Unit testing...\"\n" +
                                                 "            sh \"echo Tests running\"\n" +
                                                 "            sh \"echo Tests completed\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'integration':{\n" +
                                                 "        node{\n" +
                                                 "            echo \"Integration testing...\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'ui':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"UI testing...\"\n" +
                                                 "        }\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "node{\n" +
                                                 "  echo \"Done Testing\"\n" +
                                                 "}\n" +
                                                 "stage ('deploy') {\n" +
                                                 "    node{\n" +
                                                 "        echo \"Deploying\"\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('deployToProd') {\n" +
                                                 "    node{\n" +
                                                 "        echo \"Deploying to production\"\n" +
                                                 "    }\n" +
                                                 "}", false
    ));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);

    FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
    nodeGraphTable.build();
    List<FlowNode> nodes = getStages(nodeGraphTable);
    List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);

    Assert.assertEquals(7, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());

    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals(9, resp.size());
}
 
Example 11
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getPipelineJobRunNodeTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");

    // same
    job1.setDefinition(new CpsFlowDefinition(
        "stage ('build') {\n" +
            "    node {\n" +
            "        echo \"Building...\"\n" +
            "    }\n" +
            "}\n" +
            "stage ('test') {\n" +
            "    parallel 'unit':{\n" +
            "        node {\n" +
            "            echo \"Unit testing...\"\n" +
            "        }\n" +
            "    },\n" +
            "    'integration':{\n" +
            "        node {\n" +
            "            echo \"Integration testing...\"\n" +
            "        }\n" +
            "    },\n" +
            "    'ui':{\n" +
            "        node {\n" +
            "            echo \"UI testing...\"\n" +
            "        }\n" +
            "    }\n" +
            "}\n" +
            "stage ('deploy') {\n" +
            "    node {\n" +
            "        echo \"Deploying\"\n" +
            "    }\n" +
            "}", false));

    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
    nodeGraphTable.build();
    List<FlowNode> nodes = getStages(nodeGraphTable);
    List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);

    Assert.assertEquals(6, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());

    // get all nodes for pipeline1
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(nodes.size(), resp.size());

    //Get a node detail
    FlowNode n = nodes.get(0);

    Map node = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + n.getId());

    List<Map> edges = (List<Map>) node.get("edges");

    Assert.assertEquals(n.getId(), node.get("id"));
    Assert.assertEquals(getNodeName(n), node.get("displayName"));
    Assert.assertEquals("SUCCESS", node.get("result"));
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(nodes.get(1).getId(), edges.get(0).get("id"));


    //Get a parallel node detail
    node = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + parallelNodes.get(0).getId());

    n = parallelNodes.get(0);
    edges = (List<Map>) node.get("edges");

    Assert.assertEquals(n.getId(), node.get("id"));
    Assert.assertEquals(getNodeName(n), node.get("displayName"));
    Assert.assertEquals("SUCCESS", node.get("result"));
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(nodes.get(nodes.size() - 1).getId(), edges.get(0).get("id"));
}
 
Example 12
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getPipelineJobRunNodeLogTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");

    // same
    job1.setDefinition(new CpsFlowDefinition("stage ('build') {\n" +
                                                 "    node {\n" +
                                                 "        echo \"Building...\"\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('test') {\n" +
                                                 "    parallel 'unit':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"Unit testing...\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'integration':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"Integration testing...\"\n" +
                                                 "        }\n" +
                                                 "    },\n" +
                                                 "    'ui':{\n" +
                                                 "        node {\n" +
                                                 "            echo \"UI testing...\"\n" +
                                                 "        }\n" +
                                                 "    }\n" +
                                                 "}\n" +
                                                 "stage ('deploy') {\n" +
                                                 "    node {\n" +
                                                 "        echo \"Deploying\"\n" +
                                                 "    }\n" +
                                                 "}", false));

    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
    nodeGraphTable.build();
    List<FlowNode> nodes = getStages(nodeGraphTable);
    List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);

    Assert.assertEquals(6, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());

    String output = get("/organizations/jenkins/pipelines/pipeline1/runs/1/log", String.class);
    assertNotNull(output);
    System.out.println(output);
}