Search in sources :

Example 1 with SimpleXStreamFlowNodeStorage

use of org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage 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 SimpleXStreamFlowNodeStorage

use of org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method createStorage.

private TimingFlowNodeStorage createStorage() throws IOException {
    FlowNodeStorage wrappedStorage;
    FlowDurabilityHint hint = getDurabilityHint();
    wrappedStorage = (hint.isPersistWithEveryStep()) ? new SimpleXStreamFlowNodeStorage(this, getStorageDir()) : new BulkFlowNodeStorage(this, getStorageDir());
    return new TimingFlowNodeStorage(wrappedStorage);
}
Also used : BulkFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.BulkFlowNodeStorage) SimpleXStreamFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage) FlowDurabilityHint(org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint) BulkFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.BulkFlowNodeStorage) FlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.FlowNodeStorage) SimpleXStreamFlowNodeStorage(org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage)

Aggregations

SimpleXStreamFlowNodeStorage (org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage)2 XmlFile (hudson.XmlFile)1 Action (hudson.model.Action)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArgumentsAction (org.jenkinsci.plugins.workflow.actions.ArgumentsAction)1 StageAction (org.jenkinsci.plugins.workflow.actions.StageAction)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 FlowDurabilityHint (org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint)1 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)1 DepthFirstScanner (org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner)1 BulkFlowNodeStorage (org.jenkinsci.plugins.workflow.support.storage.BulkFlowNodeStorage)1 FlowNodeStorage (org.jenkinsci.plugins.workflow.support.storage.FlowNodeStorage)1