Search in sources :

Example 1 with ArgumentsAction

use of org.jenkinsci.plugins.workflow.actions.ArgumentsAction 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 ArgumentsAction

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

the class ArgumentsActionImplTest method testDeserialize.

/**
 * Helper function to test direct file deserialization for an execution
 */
private void testDeserialize(FlowExecution execution) throws Exception {
    if (!(execution instanceof CpsFlowExecution) || !(((CpsFlowExecution) execution).getStorage() instanceof SimpleXStreamFlowNodeStorage)) {
        // Test is unfortunately coupled to the implementation -- otherwise it will simply hit caches
        return;
    }
    SimpleXStreamFlowNodeStorage storage = (SimpleXStreamFlowNodeStorage) (((CpsFlowExecution) execution).getStorage());
    Method getFileM = SimpleXStreamFlowNodeStorage.class.getDeclaredMethod("getNodeFile", String.class);
    getFileM.setAccessible(true);
    List<FlowNode> nodes = new DepthFirstScanner().allNodes(execution.getCurrentHeads());
    Collections.sort(nodes, FlowScanningUtils.ID_ORDER_COMPARATOR);
    Field nodeExecutionF = FlowNode.class.getDeclaredField("exec");
    nodeExecutionF.setAccessible(true);
    // Read each node via deserialization from storage, and sanity check the node, the actions, and the ArgumentsAction read back right
    for (FlowNode f : nodes) {
        XmlFile file = (XmlFile) (getFileM.invoke(storage, f.getId()));
        Object tagObj = file.read();
        Assert.assertNotNull(tagObj);
        // Check actions & node in the Tag object, but without getting at the private Tag class
        Field actionField = tagObj.getClass().getDeclaredField("actions");
        Field nodeField = tagObj.getClass().getDeclaredField("node");
        actionField.setAccessible(true);
        nodeField.setAccessible(true);
        Action[] deserializedActions = (Action[]) actionField.get(tagObj);
        FlowNode deserializedNode = (FlowNode) (nodeField.get(tagObj));
        nodeExecutionF.set(deserializedNode, f.getExecution());
        Assert.assertNotNull(deserializedNode);
        if (f.getActions().size() > 0) {
            Assert.assertNotNull(deserializedActions);
            Assert.assertEquals(f.getActions().size(), deserializedActions.length);
        }
        ArgumentsAction expectedInfoAction = f.getPersistentAction(ArgumentsAction.class);
        if (expectedInfoAction != null) {
            Action deserializedInfoAction = Iterables.getFirst(Iterables.filter(Lists.newArrayList(deserializedActions), Predicates.instanceOf(ArgumentsAction.class)), null);
            Assert.assertNotNull(deserializedInfoAction);
            ArgumentsAction ArgumentsAction = (ArgumentsAction) deserializedInfoAction;
            // Compare original and deserialized step arguments to see if they match
            Assert.assertEquals(ArgumentsAction.getStepArgumentsAsString(f), ArgumentsAction.getStepArgumentsAsString(deserializedNode));
            Map<String, Object> expectedParams = expectedInfoAction.getArguments();
            Map<String, Object> deserializedParams = ArgumentsAction.getArguments();
            Assert.assertEquals(expectedParams.size(), deserializedParams.size());
            for (String s : expectedParams.keySet()) {
                Object expectedVal = expectedParams.get(s);
                Object actualVal = deserializedParams.get(s);
                if (expectedVal instanceof Comparable) {
                    Assert.assertEquals(actualVal, expectedVal);
                }
            }
        }
    }
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) StageAction(org.jenkinsci.plugins.workflow.actions.StageAction) Action(hudson.model.Action) ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) XmlFile(hudson.XmlFile) Method(java.lang.reflect.Method) SimpleXStreamFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) Field(java.lang.reflect.Field) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 3 with ArgumentsAction

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

the class DSLTest method monomorphic.

/**
 * Tests the ability to execute a step with an unnamed monomorphic describable argument.
 */
@Issue("JENKINS-29711")
@Test
public void monomorphic() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "mon");
    p.setDefinition(new CpsFlowDefinition("monomorphStep([firstArg:'one', secondArg:'two'])", true));
    r.assertLogContains("First arg: one, second arg: two", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
    WorkflowRun run = p.getLastBuild();
    LinearScanner scanner = new LinearScanner();
    FlowNode node = scanner.findFirstMatch(run.getExecution().getCurrentHeads(), new NodeStepTypePredicate("monomorphStep"));
    ArgumentsAction argumentsAction = node.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(argumentsAction);
    Assert.assertEquals("one,two", ArgumentsAction.getStepArgumentsAsString(node));
}
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) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 4 with ArgumentsAction

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

the class ArgumentsActionImplTest method testUnusualStepInstantiations.

@Test
public void testUnusualStepInstantiations() throws Exception {
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "unusualInstantiation");
    job.setDefinition(new CpsFlowDefinition(" node('master') { \n" + "   writeFile text: 'hello world', file: 'msg.out'\n" + "   step([$class: 'ArtifactArchiver', artifacts: 'msg.out', fingerprint: false])\n " + // Symbol-based, because withEnv is a metastep; TODO huh? no it is not
    "   withEnv(['CUSTOM=val']) {\n" + "     echo env.CUSTOM\n" + "   }\n" + "}"));
    WorkflowRun run = r.buildAndAssertSuccess(job);
    LinearScanner scan = new LinearScanner();
    FlowNode testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("writeFile"));
    ArgumentsAction act = testNode.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(act);
    Assert.assertEquals("hello world", act.getArgumentValue("text"));
    Assert.assertEquals("msg.out", act.getArgumentValue("file"));
    testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("step"));
    act = testNode.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(act);
    Map<String, Object> delegateMap = ((Map<String, Object>) act.getArgumentValue("delegate"));
    Assert.assertEquals("msg.out", delegateMap.get("artifacts"));
    Assert.assertEquals(Boolean.FALSE, delegateMap.get("fingerprint"));
    // Start node for EnvAction
    testNode = run.getExecution().getNode("7");
    act = testNode.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(act);
    Assert.assertEquals(1, act.getArguments().size());
    Object ob = act.getArguments().get("overrides");
    Assert.assertEquals("CUSTOM=val", (String) ((ArrayList) ob).get(0));
    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) ArrayList(java.util.ArrayList) 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)

Aggregations

ArgumentsAction (org.jenkinsci.plugins.workflow.actions.ArgumentsAction)4 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)3 LinearScanner (org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner)3 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)3 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)3 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)3 Test (org.junit.Test)3 XmlFile (hudson.XmlFile)1 Action (hudson.model.Action)1 ArtifactArchiver (hudson.tasks.ArtifactArchiver)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 StageAction (org.jenkinsci.plugins.workflow.actions.StageAction)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)1 SimpleXStreamFlowNodeStorage (org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage)1 Issue (org.jvnet.hudson.test.Issue)1