Search in sources :

Example 1 with DepthFirstScanner

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

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

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

use of org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner 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)

Example 5 with DepthFirstScanner

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

the class FlowDurabilityTest method testResumeBlockedAddedAfterRunStart.

@Test
@Issue("JENKINS-49961")
public void testResumeBlockedAddedAfterRunStart() throws Exception {
    final String jobName = "survivesEverything";
    final String[] logStart = new String[1];
    final List<FlowNode> nodesOut = new ArrayList<FlowNode>();
    story.addStepWithDirtyShutdown(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Jenkins jenkins = story.j.jenkins;
            WorkflowRun run = createAndRunSleeperJob(story.j.jenkins, jobName, FlowDurabilityHint.MAX_SURVIVABILITY);
            run.getParent().setResumeBlocked(false);
            FlowExecution exec = run.getExecution();
            if (exec instanceof CpsFlowExecution) {
                assert ((CpsFlowExecution) exec).getStorage().isPersistedFully();
            }
            logStart[0] = JenkinsRule.getLog(run);
            nodesOut.addAll(new DepthFirstScanner().allNodes(run.getExecution()));
            nodesOut.sort(FlowScanningUtils.ID_ORDER_COMPARATOR);
            run.getParent().setResumeBlocked(true);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowRun run = story.j.jenkins.getItemByFullName(jobName, WorkflowJob.class).getLastBuild();
            verifyFailedCleanly(story.j.jenkins, run);
            assertIncludesNodes(nodesOut, run);
        }
    });
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) DepthFirstScanner(org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner) Jenkins(jenkins.model.Jenkins) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)12 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)12 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)5 Predicate (com.google.common.base.Predicate)4 ConfigFileBuildWrapper (org.jenkinsci.plugins.configfiles.buildwrapper.ConfigFileBuildWrapper)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)4 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)4 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)4 FlowDurabilityHint (org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint)3 Statement (org.junit.runners.model.Statement)3 Action (hudson.model.Action)2 List (java.util.List)2 Jenkins (jenkins.model.Jenkins)2 Ignore (org.junit.Ignore)2 Issue (org.jvnet.hudson.test.Issue)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 XmlFile (hudson.XmlFile)1