Search in sources :

Example 26 with CpsFlowDefinition

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));
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 27 with CpsFlowDefinition

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);
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) Test(org.junit.Test)

Example 28 with CpsFlowDefinition

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));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 29 with CpsFlowDefinition

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);
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 30 with CpsFlowDefinition

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)));
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Statement(org.junit.runners.model.Statement) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) 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