Search in sources :

Example 21 with FlowExecution

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

the class FlowDurabilityTest method testPauseForcesLowDurabilityToPersist.

/**
 * Verifies that paused pipelines survive dirty restarts
 */
@Test
public void testPauseForcesLowDurabilityToPersist() throws Exception {
    final String jobName = "durableAgainstClean";
    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.PERFORMANCE_OPTIMIZED);
            FlowExecution exec = run.getExecution();
            assertBaseStorageType(exec, BulkFlowNodeStorage.class);
            logStart[0] = JenkinsRule.getLog(run);
            if (run.getExecution() instanceof CpsFlowExecution) {
                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);
                }
            }
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName(jobName, WorkflowJob.class).getLastBuild();
            if (run.getExecution() instanceof CpsFlowExecution) {
                CpsFlowExecution cpsFlow = (CpsFlowExecution) (run.getExecution());
                cpsFlow.pause(false);
                long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS);
                while (System.nanoTime() < timeout && cpsFlow.isPaused()) {
                    Thread.sleep(100L);
                }
            }
            Assert.assertEquals(FlowDurabilityHint.PERFORMANCE_OPTIMIZED, run.getExecution().getDurabilityHint());
            assertBaseStorageType(run.getExecution(), BulkFlowNodeStorage.class);
            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) BulkFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.BulkFlowNodeStorage) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 22 with FlowExecution

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

the class FlowDurabilityTest method testResumeBlocked.

@Test
public void testResumeBlocked() throws Exception {
    final String jobName = "survivesEverything";
    final String[] logStart = new String[1];
    final List<FlowNode> nodesOut = new ArrayList<FlowNode>();
    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);
            run.getParent().setResumeBlocked(true);
            FlowExecution exec = run.getExecution();
            if (exec instanceof CpsFlowExecution) {
                assert ((CpsFlowExecution) exec).getStorage().isPersistedFully();
            }
            logStart[0] = JenkinsRule.getLog(run);
            nodesOut.addAll(new DepthFirstScanner().allNodes(run.getExecution()));
            nodesOut.sort(FlowScanningUtils.ID_ORDER_COMPARATOR);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName(jobName, WorkflowJob.class).getLastBuild();
            verifyFailedCleanly(story.j.jenkins, run);
            assertIncludesNodes(nodesOut, run);
        }
    });
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) Jenkins(jenkins.model.Jenkins) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 23 with FlowExecution

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

the class FlowDurabilityTest method verifySafelyResumed.

/**
 * If it's a {@link SemaphoreStep} we test less rigorously because that blocks async GraphListeners.
 */
static void verifySafelyResumed(JenkinsRule rule, WorkflowRun run, boolean isSemaphore, String logStart) throws Exception {
    assert run.isBuilding();
    FlowExecution exec = run.getExecution();
    // Assert that we have the appropriate flow graph entries
    List<FlowNode> heads = exec.getCurrentHeads();
    Assert.assertEquals(1, heads.size());
    FlowNode node = heads.get(0);
    String name = node.getDisplayFunctionName();
    Assert.assertTrue("Head node not a semaphore step or sleep: " + name, "semaphore".equals(name) || "sleep".equals(name));
    if (!isSemaphore) {
        Assert.assertNotNull(node.getPersistentAction(TimingAction.class));
        Assert.assertNotNull(node.getPersistentAction(ArgumentsAction.class));
        Assert.assertNotNull(node.getAction(LogAction.class));
    } else {
        SemaphoreStep.success("halt/1", Result.SUCCESS);
    }
    assertHasTimingAction(run.getExecution());
    rule.waitForCompletion(run);
    verifySucceededCleanly(rule.jenkins, run);
    rule.assertLogContains(logStart, run);
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) LogAction(org.jenkinsci.plugins.workflow.actions.LogAction) TimingAction(org.jenkinsci.plugins.workflow.actions.TimingAction) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 24 with FlowExecution

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

the class FlowDurabilityTest method verifyCompletedCleanly.

/**
 * Verifies all the universal post-build cleanup was done, regardless of pass/fail state.
 */
static void verifyCompletedCleanly(Jenkins j, WorkflowRun run) throws Exception {
    // Assert that we have the appropriate flow graph entries
    FlowExecution exec = run.getExecution();
    List<FlowNode> heads = exec.getCurrentHeads();
    Assert.assertEquals(1, heads.size());
    verifyNoTasksRunning(j);
    Assert.assertEquals(0, exec.getCurrentExecutions(false).get().size());
    if (exec instanceof CpsFlowExecution) {
        CpsFlowExecution cpsFlow = (CpsFlowExecution) exec;
        assert cpsFlow.getStorage() != null;
        Assert.assertFalse("Should always be able to retrieve script", StringUtils.isEmpty(cpsFlow.getScript()));
        Assert.assertNull("We should have no Groovy shell left or that's a memory leak", cpsFlow.getShell());
        Assert.assertNull("We should have no Groovy shell left or that's a memory leak", cpsFlow.getTrustedShell());
    }
    verifyExecutionRemoved(run);
}
Also used : FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 25 with FlowExecution

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

the class FlowDurabilityTest method verifyFailedCleanly.

static void verifyFailedCleanly(Jenkins j, WorkflowRun run) throws Exception {
    if (run.isBuilding()) {
        // Give the run a little bit of time to see if it can resume or not
        FlowExecution exec = run.getExecution();
        if (exec instanceof CpsFlowExecution) {
            waitForBuildToResumeOrFail(run);
        } else {
            Thread.sleep(4000L);
        }
    }
    if (run.getExecution() instanceof CpsFlowExecution) {
        CpsFlowExecution cfe = (CpsFlowExecution) (run.getExecution());
        assert cfe.isComplete() || (cfe.programPromise != null && cfe.programPromise.isDone());
    }
    assert !run.isBuilding();
    if (run.getExecution() instanceof CpsFlowExecution) {
        Assert.assertEquals(Result.FAILURE, ((CpsFlowExecution) run.getExecution()).getResult());
    }
    Assert.assertEquals(Result.FAILURE, run.getResult());
    assert !run.isBuilding();
    // TODO verify all blocks cleanly closed out, so Block start and end nodes have same counts and FlowEndNode is last node
    verifyCompletedCleanly(j, run);
}
Also used : FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution)

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