Search in sources :

Example 16 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class RunningFlowActions method createFor.

@Override
public Collection<? extends Action> createFor(FlowExecutionOwner.Executable executable) {
    FlowExecutionOwner owner = executable.asFlowExecutionOwner();
    if (owner != null) {
        FlowExecution exec = owner.getOrNull();
        if (exec instanceof CpsFlowExecution && !exec.isComplete()) {
            CpsFlowExecution e = (CpsFlowExecution) exec;
            List<Action> actions = new ArrayList<>();
            actions.add(new CpsThreadDumpAction(e));
            // TODO cf. comment in CpsFlowExecution#pause
            if (!(executable instanceof AccessControlled) || ((AccessControlled) executable).hasPermission(Item.CANCEL)) {
                actions.add(new PauseUnpauseAction(e));
            }
            return actions;
        }
    }
    return Collections.emptySet();
}
Also used : AccessControlled(hudson.security.AccessControlled) Action(hudson.model.Action) FlowExecutionOwner(org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) ArrayList(java.util.ArrayList)

Example 17 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecutionTest method getCurrentExecutions.

@Test
public void getCurrentExecutions() {
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
            p.setDefinition(new CpsFlowDefinition("echo 'a step'; semaphore 'one'; retry(2) {semaphore 'two'; node {semaphore 'three'}; semaphore 'four'}; semaphore 'five'; " + "parallel a: {node {semaphore 'six'}}, b: {semaphore 'seven'}; semaphore 'eight'", true));
            WorkflowRun b = p.scheduleBuild2(0).waitForStart();
            SemaphoreStep.waitForStart("one/1", b);
            FlowExecution e = b.getExecution();
            assertStepExecutions(e, "semaphore");
            SemaphoreStep.success("one/1", null);
            SemaphoreStep.waitForStart("two/1", b);
            assertStepExecutions(e, "retry {}", "semaphore");
            SemaphoreStep.success("two/1", null);
            SemaphoreStep.waitForStart("three/1", b);
            assertStepExecutions(e, "retry {}", "node {}", "semaphore");
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
            WorkflowRun b = p.getLastBuild();
            CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
            assertTrue(e.isSandbox());
            SemaphoreStep.success("three/1", null);
            SemaphoreStep.waitForStart("four/1", b);
            assertStepExecutions(e, "retry {}", "semaphore");
            SemaphoreStep.failure("four/1", new AbortException("try again"));
            SemaphoreStep.waitForStart("two/2", b);
            assertStepExecutions(e, "retry {}", "semaphore");
            SemaphoreStep.success("two/2", null);
            SemaphoreStep.waitForStart("three/2", b);
            assertStepExecutions(e, "retry {}", "node {}", "semaphore");
            SemaphoreStep.success("three/2", null);
            SemaphoreStep.waitForStart("four/2", b);
            assertStepExecutions(e, "retry {}", "semaphore");
            SemaphoreStep.success("four/2", null);
            SemaphoreStep.waitForStart("five/1", b);
            assertStepExecutions(e, "semaphore");
            SemaphoreStep.success("five/1", null);
            SemaphoreStep.waitForStart("six/1", b);
            SemaphoreStep.waitForStart("seven/1", b);
            assertStepExecutions(e, "parallel {}", "node {}", "semaphore", "semaphore");
            SemaphoreStep.success("six/1", null);
            SemaphoreStep.success("seven/1", null);
            SemaphoreStep.waitForStart("eight/1", b);
            assertStepExecutions(e, "semaphore");
            SemaphoreStep.success("eight/1", null);
            story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
            assertStepExecutions(e);
        }
    });
}
Also used : Statement(org.junit.runners.model.Statement) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) AbortException(hudson.AbortException) Test(org.junit.Test)

Example 18 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method testFullyDurableSurvivesDirtyRestart.

/**
 * Sanity check that fully durable pipelines can survive hard kills.
 */
@Test
public void testFullyDurableSurvivesDirtyRestart() throws Exception {
    final String jobName = "survivesEverything";
    final String[] logStart = new String[1];
    story.addStepWithDirtyShutdown(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Jenkins jenkins = story.j.jenkins;
            WorkflowRun run = createAndRunSleeperJob(story.j.jenkins, jobName, FlowDurabilityHint.MAX_SURVIVABILITY);
            FlowExecution exec = run.getExecution();
            if (exec instanceof CpsFlowExecution) {
                assert ((CpsFlowExecution) exec).getStorage().isPersistedFully();
            }
            logStart[0] = JenkinsRule.getLog(run);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName(jobName, WorkflowJob.class).getLastBuild();
            verifySafelyResumed(story.j, run, false, logStart[0]);
        }
    });
}
Also used : Jenkins(jenkins.model.Jenkins) Statement(org.junit.runners.model.Statement) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 19 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method verifySucceededCleanly.

static void verifySucceededCleanly(Jenkins j, WorkflowRun run) throws Exception {
    Assert.assertEquals(Result.SUCCESS, run.getResult());
    int outputHash = run.getLog().hashCode();
    FlowExecution exec = run.getExecution();
    verifyCompletedCleanly(j, run);
    // Confirm the flow graph is fully navigable and contains the heads with appropriate ending
    DepthFirstScanner scan = new DepthFirstScanner();
    List<FlowNode> allNodes = scan.allNodes(exec);
    FlowNode endNode = exec.getCurrentHeads().get(0);
    Assert.assertEquals(FlowEndNode.class, endNode.getClass());
    assert allNodes.contains(endNode);
    Assert.assertEquals(8, allNodes.size());
    // Graph structure assertions
    Assert.assertEquals(2, scan.filteredNodes(endNode, (Predicate) (Predicates.instanceOf(StepStartNode.class))).size());
    Assert.assertEquals(2, scan.filteredNodes(endNode, (Predicate) (Predicates.instanceOf(StepEndNode.class))).size());
    Assert.assertEquals(1, scan.filteredNodes(endNode, (Predicate) (Predicates.instanceOf(FlowStartNode.class))).size());
    Predicate<FlowNode> sleepOrSemaphoreMatch = Predicates.or(new NodeStepNamePredicate(StepDescriptor.byFunctionName("semaphore").getId()), new NodeStepNamePredicate(StepDescriptor.byFunctionName("sleep").getId()));
    Assert.assertEquals(1, scan.filteredNodes(endNode, sleepOrSemaphoreMatch).size());
    Assert.assertEquals(1, scan.filteredNodes(endNode, new NodeStepNamePredicate(StepDescriptor.byFunctionName("echo").getId())).size());
    for (FlowNode node : (List<FlowNode>) (scan.filteredNodes(endNode, (Predicate) (Predicates.instanceOf(StepNode.class))))) {
        Assert.assertNotNull("Node: " + node.toString() + " does not have a TimingAction", node.getAction(TimingAction.class));
    }
    assertHasTimingAction(run.getExecution());
}
Also used : FlowStartNode(org.jenkinsci.plugins.workflow.graph.FlowStartNode) StepStartNode(org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode) TimingAction(org.jenkinsci.plugins.workflow.actions.TimingAction) StepEndNode(org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode) NodeStepNamePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepNamePredicate) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) StepNode(org.jenkinsci.plugins.workflow.cps.nodes.StepNode) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) List(java.util.List) ArrayList(java.util.ArrayList) CopyOnWriteList(hudson.util.CopyOnWriteList) FlowExecutionList(org.jenkinsci.plugins.workflow.flow.FlowExecutionList) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 20 with FlowExecution

use of org.jenkinsci.plugins.workflow.flow.FlowExecution in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method testFullyDurableSurvivesCleanRestart.

/**
 * Sanity check that fully durable pipelines shutdown and restart cleanly
 */
@Test
public void testFullyDurableSurvivesCleanRestart() throws Exception {
    final String jobName = "survivesEverything";
    final String[] logStart = new String[1];
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Jenkins jenkins = story.j.jenkins;
            WorkflowRun run = createAndRunBasicJob(story.j.jenkins, jobName, FlowDurabilityHint.MAX_SURVIVABILITY);
            FlowExecution exec = run.getExecution();
            if (exec instanceof CpsFlowExecution) {
                assert ((CpsFlowExecution) exec).getStorage().isPersistedFully();
            }
            logStart[0] = JenkinsRule.getLog(run);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName(jobName, WorkflowJob.class).getLastBuild();
            verifySafelyResumed(story.j, run, true, logStart[0]);
        }
    });
}
Also used : Jenkins(jenkins.model.Jenkins) Statement(org.junit.runners.model.Statement) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)28 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)10 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 Statement (org.junit.runners.model.Statement)7 Jenkins (jenkins.model.Jenkins)6 FlowExecutionOwner (org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner)6 AbortException (hudson.AbortException)5 IOException (java.io.IOException)5 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)4 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)4 FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)3 LineTransformationOutputStream (hudson.console.LineTransformationOutputStream)2 StreamBuildListener (hudson.model.StreamBuildListener)2 CopyOnWriteList (hudson.util.CopyOnWriteList)2 BluePipelineStep (io.jenkins.blueocean.rest.model.BluePipelineStep)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2