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());
}
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);
}
}
}
}
}
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));
}
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());
}
Aggregations