Search in sources :

Example 1 with NodeStepTypePredicate

use of org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate 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 2 with NodeStepTypePredicate

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

the class ArgumentsActionImplTest method testArgumentDescriptions.

@Test
public void testArgumentDescriptions() throws Exception {
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "argumentDescription");
    job.setDefinition(new CpsFlowDefinition("echo 'test' \n " + " node('master') { \n" + "   retry(3) {\n" + "     if (isUnix()) { \n" + "       sh 'whoami' \n" + "     } else { \n" + "       bat 'echo %USERNAME%' \n" + "     }\n" + "   } \n" + "}"));
    WorkflowRun run = r.buildAndAssertSuccess(job);
    LinearScanner scan = new LinearScanner();
    // Argument test
    FlowNode echoNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("echo"));
    Assert.assertEquals("test", echoNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
    Assert.assertEquals("test", ArgumentsAction.getStepArgumentsAsString(echoNode));
    if (Functions.isWindows()) {
        FlowNode batchNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("bat"));
        Assert.assertEquals("echo %USERNAME%", batchNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
        Assert.assertEquals("echo %USERNAME%", ArgumentsAction.getStepArgumentsAsString(batchNode));
    } else {
        // Unix
        FlowNode shellNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("sh"));
        Assert.assertEquals("whoami", shellNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
        Assert.assertEquals("whoami", ArgumentsAction.getStepArgumentsAsString(shellNode));
    }
    FlowNode nodeNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), Predicates.and(Predicates.instanceOf(StepStartNode.class), new NodeStepTypePredicate("node"), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)));
    Assert.assertEquals("master", nodeNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
    Assert.assertEquals("master", ArgumentsAction.getStepArgumentsAsString(nodeNode));
    testDeserialize(run.getExecution());
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) 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 3 with NodeStepTypePredicate

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

the class StepNodeTest method metastepConsoleNotStoredArgument.

@Ignore("TODO ArgumentsAction.getResolvedArguments does not yet handle NotStoredReason sensibly")
@Test
public void metastepConsoleNotStoredArgument() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    // cf. ArgumentsAction.MAX_RETAINED_LENGTH
    String spaces = StringUtils.repeat(" ", 1025);
    p.setDefinition(new CpsFlowDefinition("node {\n" + "  configFileProvider([]) {\n" + "    writeFile text: '''<testsuite name='a'><testcase name='c'><error>failed</error></testcase></testsuite>''', file: 'x.xml'\n" + "    junit 'x.xml," + spaces + "'\n" + "  }\n" + "}", true));
    WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0));
    List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
    assertThat(coreStepNodes, hasSize(1));
    assertEquals("junit", coreStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(JUnitResultArchiver.class).getDisplayName(), coreStepNodes.get(0).getDisplayName());
    List<FlowNode> coreWrapperStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), Predicates.and(new NodeStepTypePredicate("wrap"), new Predicate<FlowNode>() {

        @Override
        public boolean apply(FlowNode n) {
            return n instanceof StepStartNode && !((StepStartNode) n).isBody();
        }
    }));
    assertThat(coreWrapperStepNodes, hasSize(1));
    assertEquals("configFileProvider", coreWrapperStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(ConfigFileBuildWrapper.class).getDisplayName() + " : Start", coreWrapperStepNodes.get(0).getDisplayName());
    r.assertLogContains("[Pipeline] junit", b);
    r.assertLogContains("[Pipeline] configFileProvider", b);
    r.assertLogContains("[Pipeline] // configFileProvider", b);
}
Also used : NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ConfigFileBuildWrapper(org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) Predicate(com.google.common.base.Predicate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with NodeStepTypePredicate

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

the class StepNodeTest method metastepConsoleRaw.

@Test
public void metastepConsoleRaw() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("node {\n" + "  wrap(new org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper([])) {\n" + "    writeFile text: '''<testsuite name='a'><testcase name='c'><error>failed</error></testcase></testsuite>''', file: 'x.xml'\n" + "    step(new hudson.tasks.junit.JUnitResultArchiver('x.xml'))\n" + "  }\n" + "}", false));
    WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0));
    List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
    assertThat(coreStepNodes, hasSize(1));
    assertEquals("junit", coreStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(JUnitResultArchiver.class).getDisplayName(), coreStepNodes.get(0).getDisplayName());
    List<FlowNode> coreWrapperStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), Predicates.and(new NodeStepTypePredicate("wrap"), new Predicate<FlowNode>() {

        @Override
        public boolean apply(FlowNode n) {
            return n instanceof StepStartNode && !((StepStartNode) n).isBody();
        }
    }));
    assertThat(coreWrapperStepNodes, hasSize(1));
    assertEquals("configFileProvider", coreWrapperStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(ConfigFileBuildWrapper.class).getDisplayName() + " : Start", coreWrapperStepNodes.get(0).getDisplayName());
    r.assertLogContains("[Pipeline] junit", b);
    r.assertLogContains("[Pipeline] configFileProvider", b);
    r.assertLogContains("[Pipeline] // configFileProvider", b);
}
Also used : NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ConfigFileBuildWrapper(org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 5 with NodeStepTypePredicate

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

the class StepNodeTest method metastepConsole.

@Issue("JENKINS-45109")
@Test
public void metastepConsole() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("node {\n" + "  configFileProvider([]) {\n" + "    writeFile text: '''<testsuite name='a'><testcase name='c'><error>failed</error></testcase></testsuite>''', file: 'x.xml'\n" + "    junit 'x.xml'\n" + "  }\n" + "}", true));
    WorkflowRun b = r.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0));
    List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), new NodeStepTypePredicate("step"));
    assertThat(coreStepNodes, hasSize(1));
    assertEquals("junit", coreStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(JUnitResultArchiver.class).getDisplayName(), coreStepNodes.get(0).getDisplayName());
    List<FlowNode> coreWrapperStepNodes = new DepthFirstScanner().filteredNodes(b.getExecution(), Predicates.and(new NodeStepTypePredicate("wrap"), new Predicate<FlowNode>() {

        @Override
        public boolean apply(FlowNode n) {
            return n instanceof StepStartNode && !((StepStartNode) n).isBody();
        }
    }));
    assertThat(coreWrapperStepNodes, hasSize(1));
    assertEquals("configFileProvider", coreWrapperStepNodes.get(0).getDisplayFunctionName());
    assertEquals(r.jenkins.getDescriptor(ConfigFileBuildWrapper.class).getDisplayName() + " : Start", coreWrapperStepNodes.get(0).getDisplayName());
    r.assertLogContains("[Pipeline] junit", b);
    r.assertLogContains("[Pipeline] configFileProvider", b);
    r.assertLogContains("[Pipeline] // configFileProvider", b);
}
Also used : NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ConfigFileBuildWrapper(org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) Predicate(com.google.common.base.Predicate) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)9 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)9 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)9 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)9 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 Test (org.junit.Test)9 LinearScanner (org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner)5 Predicate (com.google.common.base.Predicate)4 ConfigFileBuildWrapper (org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper)4 ArgumentsAction (org.jenkinsci.plugins.workflow.actions.ArgumentsAction)4 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)4 Issue (org.jvnet.hudson.test.Issue)3 ArtifactArchiver (hudson.tasks.ArtifactArchiver)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1