public void waitForWorkflowToSuspend(CpsFlowExecution e) throws Exception { e.waitForSuspension(); }
/* for tests */ CpsThreadDump threadDumpSynchronous() throws InterruptedException, ExecutionException { execution.waitForSuspension(); return execution.getThreadDump(); }
public void assertThatWorkflowIsSuspended(WorkflowRun b, CpsFlowExecution e) throws Exception { e.waitForSuspension(); // it should be in the suspended state assert b.isBuilding(); }
/** * The code getting evaluated must also get sandbox transformed. */ @Test public void evaluateShallSandbox() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("evaluate('Jenkins.getInstance()')", true); createExecution(flow); exec.start(); exec.waitForSuspension(); String msg = dumpError(); // execution should have failed with error, pointing that Jenkins.getInstance() is not allowed from sandbox assertTrue(exec.isComplete()); assertEquals(Result.FAILURE, exec.getResult()); assertTrue(msg, msg.contains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance")); }
/** Need to be careful that internal method names in {@link CpsScript} are not likely identifiers in user scripts. */ @Test public void methodNameClash() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("def build() {20}; def initialize() {10}; def env() {10}; def getShell() {2}; assert build() + initialize() + env() + shell == 42", false); createExecution(flow); exec.start(); while (!exec.isComplete()) { exec.waitForSuspension(); } assertEquals(dumpError(), Result.SUCCESS, exec.getResult()); }
/** * Test the 'evaluate' method call. * The first test case. */ @Test public void evaluate() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("assert evaluate('1+2+3')==6", false); createExecution(flow); exec.start(); exec.waitForSuspension(); assertTrue(exec.isComplete()); assertEquals(dumpError(), Result.SUCCESS, exec.getResult()); }
@Issue("JENKINS-38167") @Test public void bindingDuringConstructor() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("@groovy.transform.Field def opt = (binding.hasVariable('opt')) ? opt : 'default'", false); createExecution(flow); exec.start(); while (!exec.isComplete()) { exec.waitForSuspension(); } assertEquals(dumpError(), Result.SUCCESS, exec.getResult()); }
/** * The code getting evaluated must also get CPS transformation. */ @Ignore("TODO usually future == null, perhaps because CpsThread.resume is intended to be @CpsVmThreadOnly (so assumes that the promise is just set is not cleared by runNextChunk) yet we are calling it from the test thread; extremely dubious test design, should probably be using SemaphoreStep to be more realistic") @Test public void evaluateShallBeCpsTransformed() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("evaluate('1+com.cloudbees.groovy.cps.Continuable.suspend(2+3)')", false); createExecution(flow); exec.start(); exec.waitForSuspension(); // TODO: can't we assert that the suspend() ended with value 5? // this should have paused at suspend, so we are going to resume it by having it return a value we control assertFalse(dumpError(), exec.isComplete()); ListenableFuture<CpsThreadGroup> pp = exec.programPromise; assertNotNull(pp); Future<Object> future = pp.get().getThread(0).resume(new Outcome(7,null)); assertNotNull(future); assertEquals(8, future.get()); exec.waitForSuspension(); assertTrue(dumpError(), exec.isComplete()); assertEquals(dumpError(), Result.SUCCESS, exec.getResult()); }
@Override public void evaluate() throws Throwable { WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("semaphore 'wait'; echo 'I am done'", true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("wait/1", b); story.j.jenkins.doQuietDown(true, 0); SemaphoreStep.success("wait/1", null); ((CpsFlowExecution) b.getExecution()).waitForSuspension(); assertTrue(b.isBuilding()); } });
@Issue("JENKINS-48379") @Test public void simpleInput() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "simpleInput"); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources("simpleInput"), true)); // get the build going, and wait until workflow pauses QueueTaskFuture<WorkflowRun> q = p.scheduleBuild2(0); WorkflowRun b = q.getStartCondition().get(); CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get(); while (b.getAction(InputAction.class)==null) { e.waitForSuspension(); } // make sure we are pausing at the right state that reflects what we wrote in the program InputAction a = b.getAction(InputAction.class); assertEquals(1, a.getExecutions().size()); InputStepExecution is = a.getExecution("Foo"); assertEquals("Continue?", is.getInput().getMessage()); assertEquals(0, is.getInput().getParameters().size()); assertNull(is.getInput().getSubmitter()); JenkinsRule.WebClient wc = j.createWebClient(); HtmlPage page = wc.getPage(b, a.getUrlName()); j.submit(page.getFormByName(is.getId()), "proceed"); assertEquals(0, a.getExecutions().size()); q.get(); j.assertBuildStatusSuccess(j.waitForCompletion(b)); j.assertLogContains("hello", b); }
@Issue("JENKINS-50880") @Test public void whenBeforeInputTrue() throws Exception { String whenFile = "whenBeforeInputTrue"; WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, whenFile); p.setDefinition(new CpsFlowDefinition(pipelineSourceFromResources(whenFile), true)); // get the build going, and wait until workflow pauses QueueTaskFuture<WorkflowRun> q = p.scheduleBuild2(0); WorkflowRun b = q.getStartCondition().get(); CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get(); while (b.getAction(InputAction.class)==null) { e.waitForSuspension(); } // make sure we are pausing at the right state that reflects what we wrote in the program InputAction a = b.getAction(InputAction.class); assertEquals(1, a.getExecutions().size()); InputStepExecution is = a.getExecution("Simple-input"); assertEquals("Continue?", is.getInput().getMessage()); assertNull(is.getInput().getSubmitter()); JenkinsRule.WebClient wc = j.createWebClient(); HtmlPage page = wc.getPage(b, a.getUrlName()); j.submit(page.getFormByName(is.getId()), "proceed"); assertEquals(0, a.getExecutions().size()); q.get(); j.assertBuildStatusSuccess(j.waitForCompletion(b)); j.assertLogContains("X-SHOW-X", b); }
/** * I should be able to have DSL call into async step and then bring it to the completion. */ @Test public void suspendExecutionAndComeBack() throws Exception { CpsFlowDefinition flow = new CpsFlowDefinition("semaphore 'watch'\nprintln 'Yo'", false); // get this going... createExecution(flow); exec.start(); SemaphoreStep.waitForStart("watch/1", null); assertFalse("Expected the execution to be suspended but it has completed", exec.isComplete()); FlowExecutionOwner owner = exec.getOwner(); exec = roundtripXStream(exec); // poor man's simulation of Jenkins restart exec.onLoad(owner); // now resume workflow execution SemaphoreStep.success("watch/1", null); exec.waitForSuspension(); assertTrue(exec.isComplete()); }