Search in sources :

Example 11 with CpsFlowDefinition

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

the class WorkflowJobNonRestartingTest method assertRejected.

private void assertRejected(String script) throws Exception {
    String signature = "staticMethod jenkins.model.Jenkins getInstance";
    ScriptApproval scriptApproval = ScriptApproval.get();
    scriptApproval.denySignature(signature);
    assertEquals(Collections.emptySet(), scriptApproval.getPendingSignatures());
    p.setDefinition(new CpsFlowDefinition(script, true));
    WorkflowRun b = p.scheduleBuild2(0).get();
    jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use " + signature, b);
    jenkins.assertBuildStatus(Result.FAILURE, b);
    Set<ScriptApproval.PendingSignature> pendingSignatures = scriptApproval.getPendingSignatures();
    assertEquals(script, 1, pendingSignatures.size());
    assertEquals(signature, pendingSignatures.iterator().next().signature);
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ScriptApproval(org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun)

Example 12 with CpsFlowDefinition

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

the class WorkflowJobNonRestartingTest method killInfiniteLoop.

@Test
@Issue("JENKINS-25623")
public void killInfiniteLoop() throws Exception {
    p.setDefinition(new CpsFlowDefinition("while(true) { " + WorkflowJobNonRestartingTest.class.getName() + ".going(); }", true));
    QueueTaskFuture<WorkflowRun> f = p.scheduleBuild2(0);
    WorkflowRun b = f.getStartCondition().get(3, TimeUnit.SECONDS);
    // get the buld going, which will loop infinitely
    going.block(3000);
    // abort, abort!
    b.doStop();
    jenkins.assertBuildStatus(Result.ABORTED, jenkins.waitForCompletion(b));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) AbstractCpsFlowTest(org.jenkinsci.plugins.workflow.cps.AbstractCpsFlowTest) Test(org.junit.Test)

Example 13 with CpsFlowDefinition

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

the class WorkflowJobNonRestartingTest method missingContextCheck.

/**
 * Trying to run a step without having the required context should result in a graceful error.
 */
@Test
public void missingContextCheck() throws Exception {
    p.setDefinition(new CpsFlowDefinition("readFile 'true'", true));
    WorkflowRun b = p.scheduleBuild2(0).get();
    // make sure the 'node' is a suggested message. this comes from MissingContextVariableException
    jenkins.assertLogContains("such as: node", b);
    // jenkins.assertLogNotContains("Exception", b)   // haven't figured out how to hide this
    jenkins.assertBuildStatus(Result.FAILURE, b);
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) AbstractCpsFlowTest(org.jenkinsci.plugins.workflow.cps.AbstractCpsFlowTest) Test(org.junit.Test)

Example 14 with CpsFlowDefinition

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

the class WorkflowTest method authentication.

@Test
public void authentication() throws Exception {
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            jenkins().setSecurityRealm(story.j.createDummySecurityRealm());
            jenkins().save();
            QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(Collections.singletonMap("demo", User.get("someone").impersonate())));
            p = jenkins().createProject(WorkflowJob.class, "demo");
            p.setDefinition(new CpsFlowDefinition("checkAuth()"));
            ScriptApproval.get().preapproveAll();
            startBuilding();
            waitForWorkflowToSuspend();
            assertTrue(b.isBuilding());
            story.j.waitForMessage("running as someone", b);
            CheckAuth.finish(false);
            waitForWorkflowToSuspend();
            assertTrue(b.isBuilding());
            story.j.waitForMessage("still running as someone", b);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            assertEquals(JenkinsRule.DummySecurityRealm.class, jenkins().getSecurityRealm().getClass());
            rebuildContext(story.j);
            assertThatWorkflowIsSuspended();
            story.j.waitForMessage("again running as someone", b);
            CheckAuth.finish(true);
            story.j.assertLogContains("finally running as someone", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
        }
    });
}
Also used : MockQueueItemAuthenticator(org.jvnet.hudson.test.MockQueueItemAuthenticator) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) Test(org.junit.Test)

Example 15 with CpsFlowDefinition

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

the class WorkflowTest method invokeBodyLaterAfterRestart.

/**
 * ability to invoke body needs to survive beyond Jenkins restart.
 */
@Test
public void invokeBodyLaterAfterRestart() throws Exception {
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            p = jenkins().createProject(WorkflowJob.class, "demo");
            p.setDefinition(new CpsFlowDefinition("int count=0;\n" + "retry(3) {\n" + "    semaphore 'wait'\n" + // forcing retry
            "    if (count++ < 2) {\n" + "        error 'died'\n" + "    }\n" + "}"));
            startBuilding();
            SemaphoreStep.waitForStart("wait/1", b);
            assertTrue(b.isBuilding());
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            rebuildContext(story.j);
            assertThatWorkflowIsSuspended();
            // resume execution and cause the retry to invoke the body again
            SemaphoreStep.success("wait/1", null);
            SemaphoreStep.success("wait/2", null);
            SemaphoreStep.success("wait/3", null);
            story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
            assertTrue(e.programPromise.get().closures.isEmpty());
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) Test(org.junit.Test)

Aggregations

CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)206 Test (org.junit.Test)196 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)168 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)111 Issue (org.jvnet.hudson.test.Issue)63 Map (java.util.Map)60 List (java.util.List)26 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RunList (hudson.util.RunList)20 ExtensionList (hudson.ExtensionList)19 Statement (org.junit.runners.model.Statement)19 Run (hudson.model.Run)17 URL (java.net.URL)16 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)12 TaskListener (hudson.model.TaskListener)11 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)10 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)9 DescribeChangeSetResult (com.amazonaws.services.cloudformation.model.DescribeChangeSetResult)7 Parameter (com.amazonaws.services.cloudformation.model.Parameter)6