Search in sources :

Example 1 with CpsFlowDefinition

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

the class PipelineApiTest method getPipelineJobActivities.

@Test
public void getPipelineJobActivities() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build1'); " + "   echo ('Building'); " + "   stage ('Test1'); " + "   sleep 10000      " + "   echo ('Testing'); " + "}"));
    job1.setConcurrentBuild(false);
    WorkflowRun r = job1.scheduleBuild2(0).waitForStart();
    job1.scheduleBuild2(0);
    List l = request().get("/organizations/jenkins/pipelines/pipeline1/activities").build(List.class);
    Assert.assertEquals(2, l.size());
    Assert.assertEquals("io.jenkins.blueocean.service.embedded.rest.QueueItemImpl", ((Map) l.get(0)).get("_class"));
    Assert.assertEquals("io.jenkins.blueocean.rest.impl.pipeline.PipelineRunImpl", ((Map) l.get(1)).get("_class"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 2 with CpsFlowDefinition

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

the class AbstractRunImplTest method replayRunTest.

// Disabled, see JENKINS-36453
@Test
@Ignore
public void replayRunTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    j.createOnlineSlave(Label.get("remote"));
    job1.setDefinition(new CpsFlowDefinition("node('remote') {\n" + "    ws {\n" + "        git($/" + sampleRepo + "/$)\n" + "    }\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    sampleRepo.write("file1", "");
    sampleRepo.git("add", "file1");
    sampleRepo.git("commit", "--message=init");
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b2);
    Assert.assertNotEquals(new PipelineRunImpl(b1, null, null).getCommitId(), new PipelineRunImpl(b2, null, null).getCommitId());
    request().post("/organizations/jenkins/pipelines/pipeline1/runs/1/replay").build(String.class);
    j.waitForCompletion(job1.getLastBuild());
    Map r = request().get("/organizations/jenkins/pipelines/pipeline1/runs/3/").build(Map.class);
    assertEquals(r.get("commitId"), new PipelineRunImpl(b2, null, null).getCommitId());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with CpsFlowDefinition

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

the class ArgumentsActionImplTest method testReallyUnusualStepInstantiations.

@Test
public void testReallyUnusualStepInstantiations() throws Exception {
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "unusualInstantiation");
    job.setDefinition(new CpsFlowDefinition(" node() {\n" + "   writeFile text: 'hello world', file: 'msg.out'\n" + // note, not whitelisted
    "   step(new hudson.tasks.ArtifactArchiver('msg.out'))\n" + "}", false));
    WorkflowRun run = r.buildAndAssertSuccess(job);
    LinearScanner scan = new LinearScanner();
    FlowNode testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("step"));
    ArgumentsAction act = testNode.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(act);
    Object delegate = act.getArgumentValue("delegate");
    Assert.assertThat(delegate, instanceOf(ArtifactArchiver.class));
    Assert.assertEquals("msg.out", ((ArtifactArchiver) delegate).getArtifacts());
    Assert.assertFalse(((ArtifactArchiver) delegate).isFingerprint());
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) ArtifactArchiver(hudson.tasks.ArtifactArchiver) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 4 with CpsFlowDefinition

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

the class ArgumentsActionImplTest method simpleSemaphoreStep.

@Test
public void simpleSemaphoreStep() throws Exception {
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "p");
    job.setDefinition(new CpsFlowDefinition("semaphore 'wait'"));
    WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
    SemaphoreStep.waitForStart("wait/1", run);
    FlowNode semaphoreNode = run.getExecution().getCurrentHeads().get(0);
    CpsThread thread = CpsThread.current();
    SemaphoreStep.success("wait/1", null);
    r.waitForCompletion(run);
    testDeserialize(run.getExecution());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) CpsThread(org.jenkinsci.plugins.workflow.cps.CpsThread) Test(org.junit.Test)

Example 5 with CpsFlowDefinition

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

the class ArgumentsActionImplTest method testBasicCredentials.

@Test
public void testBasicCredentials() throws Exception {
    String username = "bob";
    String password = "s3cr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "test", "sample", username, password);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "credentialed");
    job.setDefinition(new CpsFlowDefinition("node{ withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'test',\n" + "                usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n" + "    //available as an env variable, but will be masked if you try to print it out any which way\n" + "    echo \"$PASSWORD'\" \n" + "    echo \"${env.USERNAME}\"\n" + "    echo \"bob\"\n" + "} }\n" + "withCredentials([usernamePassword(credentialsId: 'test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + "  echo \"${env.USERNAME} ${env.PASSWORD}\"\n" + "}"));
    WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
    r.waitForCompletion(run);
    FlowExecution exec = run.getExecution();
    String log = r.getLog(run);
    ForkScanner scanner = new ForkScanner();
    List<FlowNode> filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(BindingStep.DescriptorImpl.class));
    // Check the binding step is OK
    Assert.assertEquals(8, filtered.size());
    FlowNode node = Collections2.filter(filtered, FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)).iterator().next();
    ArgumentsActionImpl act = node.getPersistentAction(ArgumentsActionImpl.class);
    Assert.assertNotNull(act.getArgumentValue("bindings"));
    Assert.assertNotNull(act.getArguments().get("bindings"));
    // Test that masking really does mask bound credentials appropriately
    filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(EchoStep.DescriptorImpl.class));
    for (FlowNode f : filtered) {
        act = f.getPersistentAction(ArgumentsActionImpl.class);
        Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, act.getArguments().get("message"));
        Assert.assertNull(ArgumentsAction.getStepArgumentsAsString(f));
    }
    List<FlowNode> allStepped = scanner.filteredNodes(run.getExecution().getCurrentHeads(), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class));
    // One ArgumentsActionImpl per block or atomic step
    Assert.assertEquals(6, allStepped.size());
    testDeserialize(exec);
}
Also used : DescriptorMatchPredicate(org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate) EchoStep(org.jenkinsci.plugins.workflow.steps.EchoStep) ForkScanner(org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) 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