use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition in project workflow-cps-plugin by jenkinsci.
the class DynamicEnvironmentExpanderTest method dynamics.
@Issue("JENKINS-26163")
@Test
public void dynamics() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("dynamicEnv {echo \"initially ${env.DYNVAR}\"; semaphore 'wait'; echo \"subsequently ${env.DYNVAR}\"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("wait/1", b);
story.j.waitForMessage("initially one", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
SemaphoreStep.success("wait/1", null);
WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
story.j.assertLogContains("subsequently two", story.j.waitForCompletion(b));
}
});
}
use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition in project workflow-cps-plugin by jenkinsci.
the class SerializationTest method stepExecutionFailsToPersist.
/**
* When wokflow execution runs into a serialization problem, can we handle that situation gracefully?
*/
@Test
public void stepExecutionFailsToPersist() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join("node {", " persistenceProblem()", "}")));
startBuilding();
waitForWorkflowToSuspend();
// TODO: let the ripple effect of a failure run to the completion.
while (b.isBuilding()) try {
waitForWorkflowToSuspend();
} catch (Exception x) {
// ignore persistence failure
String message = x.getMessage();
if (message == null || !message.contains("Failed to persist")) {
throw x;
}
}
story.j.assertBuildStatus(Result.FAILURE, b);
story.j.assertLogContains("java.lang.RuntimeException: testing the forced persistence failure behaviour", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
rebuildContext(story.j);
story.j.assertBuildStatus(Result.FAILURE, b);
}
});
}
use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition in project workflow-cps-plugin by jenkinsci.
the class SubtypeInjectingStepTest method contextInjectionOfSubParameters.
@Test
@Issue("JENKINS-25630")
public void contextInjectionOfSubParameters() throws Exception {
// see SubtypeInjectingStep
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node('master') { injectSubtypesAsContext() }"));
r.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition in project workflow-cps-plugin by jenkinsci.
the class RestartingLoadStepTest method persistenceOfLoadedScripts.
/**
* Makes sure that loaded scripts survive persistence.
*/
@Test
public void persistenceOfLoadedScripts() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = jenkins.createProject(WorkflowJob.class, "p");
jenkins.getWorkspaceFor(p).child("test.groovy").write("def answer(i) { return i*2; }\n" + "def foo(body) {\n" + " def i = body()\n" + " semaphore 'watchA'\n" + " return answer(i);\n" + "}\n" + "return this;", null);
p.setDefinition(new CpsFlowDefinition("node {\n" + " println 'started'\n" + " def o = load 'test.groovy'\n" + " println 'o=' + o.foo({21})\n" + "}"));
// get the build going
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
// wait until the executor gets assigned and the execution pauses
SemaphoreStep.waitForStart("watchA/1", b);
assertTrue(JenkinsRule.getLog(b), b.isBuilding());
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(1);
// resume from where it left off
SemaphoreStep.success("watchA/1", null);
story.j.waitForCompletion(b);
story.j.assertBuildStatusSuccess(b);
story.j.assertLogContains("o=42", b);
}
});
}
use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition in project workflow-cps-plugin by jenkinsci.
the class RestartingLoadStepTest method accessToSiblingScripts.
@Issue("JENKINS-36372")
@Test
public void accessToSiblingScripts() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = jenkins.createProject(WorkflowJob.class, "p");
jenkins.getWorkspaceFor(p).child("a.groovy").write("def call(arg) {echo \"a ran on ${arg}\"}; this", null);
ScriptApproval.get().approveSignature("method groovy.lang.Binding getVariables");
jenkins.getWorkspaceFor(p).child("b.groovy").write("def m(arg) {echo \"${this} binding=${binding.variables}\"; a(\"${arg} from b\")}; this", null);
// Control case:
// TODO if you enable sandbox here, build fails: NoSuchMethodError: No such DSL method 'a' found among […]
// SandboxInterceptor.onMethodCall is given Script2 as the receiver and "a" as the method, when really it should be asked about onGetProperty(Script2.a) followed by onMethodCall(Script1.call).
// Works fine if you use a.call(…) rather than a(…).
p.setDefinition(new CpsFlowDefinition("a = 0; node {a = load 'a.groovy'}; def b; node {b = load 'b.groovy'}; echo \"${this} binding=${binding.variables}\"; b.m('value')", false));
story.j.assertLogContains("a ran on value from b", story.j.assertBuildStatusSuccess(p.scheduleBuild2(0)));
// Test case:
p.setDefinition(new CpsFlowDefinition("a = 0; node {a = load 'a.groovy'}; semaphore 'wait'; def b; node {b = load 'b.groovy'}; echo \"${this} binding=${binding.variables}\"; b.m('value')", /* TODO ditto */
false));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("wait/1", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(2);
SemaphoreStep.success("wait/1", null);
story.j.assertLogContains("a ran on value from b", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
// Better case:
jenkins.getWorkspaceFor(p).child("b.groovy").write("def m(a, arg) {a(\"${arg} from b\")}; this", null);
p.setDefinition(new CpsFlowDefinition("def a; def b; node {a = load 'a.groovy'; b = load 'b.groovy'}; b.m(a, 'value')", true));
story.j.assertLogContains("a ran on value from b", story.j.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}
});
}
Aggregations