Search in sources :

Example 51 with CpsFlowDefinition

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

the class WorkflowRunTest method globalNodePropertiesInEnv.

@Test
@Issue("JENKINS-43396")
public void globalNodePropertiesInEnv() throws Exception {
    DescribableList<NodeProperty<?>, NodePropertyDescriptor> original = r.jenkins.getGlobalNodeProperties();
    EnvironmentVariablesNodeProperty envProp = new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry("KEY", "VALUE"));
    original.add(envProp);
    WorkflowJob j = r.jenkins.createProject(WorkflowJob.class, "envVars");
    j.setDefinition(new CpsFlowDefinition("echo \"KEY is ${env.KEY}\"", true));
    WorkflowRun b = r.assertBuildStatusSuccess(j.scheduleBuild2(0));
    r.assertLogContains("KEY is " + envProp.getEnvVars().get("KEY"), b);
}
Also used : NodePropertyDescriptor(hudson.slaves.NodePropertyDescriptor) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) NodeProperty(hudson.slaves.NodeProperty) EnvironmentVariablesNodeProperty(hudson.slaves.EnvironmentVariablesNodeProperty) EnvironmentVariablesNodeProperty(hudson.slaves.EnvironmentVariablesNodeProperty) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 52 with CpsFlowDefinition

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

the class WorkflowRunTest method funnyParameters.

@Test
public void funnyParameters() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("echo \"a.b=${params['a.b']}\"", true));
    p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("a.b", null)));
    WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("a.b", "v"))));
    r.assertLogContains("a.b=v", b);
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Test(org.junit.Test)

Example 53 with CpsFlowDefinition

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

the class WorkflowRunTest method failedToStartRun.

@Test
@Issue("JENKINS-29221")
public void failedToStartRun() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("{{stage 'dev'\n" + "def hello = new HelloWorld()\n" + "public class HelloWorld()\n" + "{ // <- invalid class definition }\n" + "}}"));
    QueueTaskFuture<WorkflowRun> workflowRunQueueTaskFuture = p.scheduleBuild2(0);
    WorkflowRun run = r.assertBuildStatus(Result.FAILURE, workflowRunQueueTaskFuture.get());
    // The issue was that the WorkflowRun's execution instance got initialised even though the script
    // was bad and the execution never started. The fix is to ensure that it only gets initialised
    // if the execution instance starts successfully i.e. in the case of this test, that it doesn't
    // get initialised.
    assertNull(run.getExecution());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 54 with CpsFlowDefinition

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

the class WorkflowRunTest method interruptCause.

@Issue("JENKINS-41276")
@Test
public void interruptCause() throws Exception {
    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    // TODO should probably be @Whitelisted
    ScriptApproval.get().approveSignature("method org.jenkinsci.plugins.workflow.steps.FlowInterruptedException getCauses");
    // ditto
    ScriptApproval.get().approveSignature("method jenkins.model.CauseOfInterruption$UserInterruption getUser");
    p.setDefinition(new CpsFlowDefinition("@NonCPS def users(e) {e.causes*.user}; try {semaphore 'wait'} catch (e) {echo(/users=${users(e)}/); throw e}", true));
    final WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
    SemaphoreStep.waitForStart("wait/1", b1);
    ACL.impersonate(User.get("dev").impersonate(), new Runnable() {

        @Override
        public void run() {
            b1.getExecutor().doStop();
        }
    });
    r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b1));
    r.assertLogContains("users=[dev]", b1);
    InterruptedBuildAction iba = b1.getAction(InterruptedBuildAction.class);
    assertNotNull(iba);
    assertEquals(Collections.singletonList(new CauseOfInterruption.UserInterruption("dev")), iba.getCauses());
    String log = JenkinsRule.getLog(b1);
    assertEquals(log, 1, StringUtils.countMatches(log, jenkins.model.Messages.CauseOfInterruption_ShortDescription("dev")));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 55 with CpsFlowDefinition

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

the class WorkflowRunTest method interruptWithResult.

@Test
public void interruptWithResult() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("sleep 1; semaphore 'hang'", true));
    WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
    // TODO sleeps should not be necessary but seems to randomly fail to receive interrupt otherwise
    Thread.sleep(500);
    SemaphoreStep.waitForStart("hang/1", b1);
    Thread.sleep(500);
    Executor ex = b1.getExecutor();
    assertNotNull(ex);
    ex.interrupt(Result.NOT_BUILT, new CauseOfInterruption.UserInterruption("bob"));
    r.assertBuildStatus(Result.NOT_BUILT, r.waitForCompletion(b1));
    InterruptedBuildAction iba = b1.getAction(InterruptedBuildAction.class);
    assertNotNull(iba);
    assertEquals(Collections.singletonList(new CauseOfInterruption.UserInterruption("bob")), iba.getCauses());
    Thread.sleep(500);
    WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
    assertEquals(2, b2.getNumber());
    Thread.sleep(500);
    SemaphoreStep.waitForStart("hang/2", b2);
    Thread.sleep(500);
    ex = b2.getExecutor();
    assertNotNull(ex);
    ex.interrupt();
    r.assertBuildStatus(Result.ABORTED, r.waitForCompletion(b2));
    iba = b2.getAction(InterruptedBuildAction.class);
    assertNull(iba);
}
Also used : CauseOfInterruption(jenkins.model.CauseOfInterruption) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) Test(org.junit.Test)

Aggregations

CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)220 Test (org.junit.Test)210 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)182 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)124 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