Search in sources :

Example 31 with Jenkins

use of jenkins.model.Jenkins 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 32 with Jenkins

use of jenkins.model.Jenkins 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)

Example 33 with Jenkins

use of jenkins.model.Jenkins 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 34 with Jenkins

use of jenkins.model.Jenkins 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 35 with Jenkins

use of jenkins.model.Jenkins in project workflow-cps-plugin by jenkinsci.

the class FlowDurabilityTest method fuzzTimingDurable.

/**
 * Test interrupting build by randomly dying at unpredictable times.
 */
@Test
// Too long to run as part of main suite
@Ignore
@TimedRepeatRule.RepeatForTime(repeatMillis = 170_000)
public void fuzzTimingDurable() throws Exception {
    final String jobName = "NestedParallelDurableJob";
    final String[] logStart = new String[1];
    final List<FlowNode> nodesOut = new ArrayList<FlowNode>();
    // Create thread that eventually interrupts Jenkins with a hard shutdown at a random time interval
    story.addStepWithDirtyShutdown(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Jenkins jenkins = story.j.jenkins;
            WorkflowJob job = jenkins.getItemByFullName(jobName, WorkflowJob.class);
            if (job == null) {
                // Job may already have been created
                job = jenkins.createProject(WorkflowJob.class, jobName);
                FlowDurabilityHint hint = FlowDurabilityHint.MAX_SURVIVABILITY;
                TestDurabilityHintProvider provider = Jenkins.getInstance().getExtensionList(TestDurabilityHintProvider.class).get(0);
                provider.registerHint(job.getFullName(), hint);
                job.setDefinition(new CpsFlowDefinition("echo 'first'\n" + "def steps = [:]\n" + "steps['1'] = {\n" + "    echo 'do 1 stuff'\n" + "}\n" + "steps['2'] = {\n" + "    echo '2a'\n" + "    echo '2b'\n" + "    def nested = [:]\n" + "    nested['2-1'] = {\n" + "        echo 'do 2-1'\n" + "    } \n" + "    nested['2-2'] = {\n" + "        sleep 1\n" + "        echo '2 section 2'\n" + "    }\n" + "    parallel nested\n" + "}\n" + "parallel steps\n" + "echo 'final'"));
            }
            story.j.buildAndAssertSuccess(job);
            long millisDuration = job.getLastBuild().getDuration();
            int time = new Random().nextInt((int) millisDuration);
            WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
            Thread.sleep(time);
            logStart[0] = JenkinsRule.getLog(run);
            nodesOut.clear();
            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();
            if (run.isBuilding()) {
                story.j.waitForCompletion(run);
                Assert.assertEquals(Result.SUCCESS, run.getResult());
            } else {
                verifyCompletedCleanly(story.j.jenkins, run);
            }
            assertIncludesNodes(nodesOut, run);
            story.j.assertLogContains(logStart[0], run);
        }
    });
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) Jenkins(jenkins.model.Jenkins) Random(java.util.Random) TestDurabilityHintProvider(org.jenkinsci.plugins.workflow.TestDurabilityHintProvider) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Jenkins (jenkins.model.Jenkins)73 Test (org.junit.Test)22 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)13 IOException (java.io.IOException)10 File (java.io.File)9 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Statement (org.junit.runners.model.Statement)7 CheckForNull (javax.annotation.CheckForNull)6 FilePath (hudson.FilePath)5 Computer (hudson.model.Computer)5 URL (java.net.URL)5 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)5 Issue (org.jvnet.hudson.test.Issue)5 Item (hudson.model.Item)4 Node (hudson.model.Node)4 Date (java.util.Date)4