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);
}
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));
}
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);
}
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)));
}
});
}
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());
}
});
}
Aggregations