protected void setUp() { super.setUp(); testFramework = getFrameworkInstance(); testnode = testFramework.getFrameworkNodeName(); final FrameworkProject frameworkProject = testFramework.getFrameworkProjectMgr().createFrameworkProject(TEST_PROJECT); File resourcesfile = new File(frameworkProject.getNodesResourceFilePath()); // copy test nodes to resources file try { FileUtils.copyFileStreams( new File("src/test/resources/com/dtolabs/rundeck/core/common/test-nodes1.xml"), resourcesfile); } catch (IOException e) { throw new RuntimeException("Caught Setup exception: " + e.getMessage(), e); } }
public void testExecuteWorkflow() throws Exception { { final FrameworkProject frameworkProject = testFramework.getFrameworkProjectMgr().getFrameworkProject(TEST_PROJECT); final INodeSet nodes = frameworkProject.getNodeSet(); assertNotNull(nodes); assertEquals(2, nodes.getNodes().size()); } { // test empty workflow final NodeSet nodeset = new NodeSet(); final WorkflowImpl workflow = new WorkflowImpl(new ArrayList<ExecutionItem>(), 1, false, WorkflowStrategy.STEP_FIRST); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .nodeSelector(nodeset) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); interpreterService.registerInstance("exec", interpreterMock); interpreterService.registerInstance("script", interpreterMock); interpreterService.registerInstance( WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock); interpreterService.registerInstance( WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock); // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, // interpreterMock); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertNull("threw exception: " + result.getException(), result.getException()); assertTrue(result.isSuccess()); assertEquals(0, interpreterMock.executionItemList.size()); } { // test undefined workflow item final NodeSet nodeset = new NodeSet(); final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>(); commands.add(new testWorkflowCmdItem()); final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .nodeSelector(nodeset.nodeSelectorWithDefaultAll()) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); interpreterService.registerInstance("exec", interpreterMock); interpreterService.registerInstance("script", interpreterMock); interpreterService.registerInstance( WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock); interpreterService.registerInstance( WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock); // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, // interpreterMock); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertFalse(result.isSuccess()); assertEquals(0, interpreterMock.executionItemList.size()); assertNotNull("threw exception: " + result.getException(), result.getException()); assertTrue( "threw exception: " + result.getException(), result.getException() instanceof WorkflowStepFailureException); assertEquals( "threw exception: " + result.getException(), "Step 1 of the workflow threw an exception: Failed dispatching to node test1: provider name was null for Service: CommandInterpreter", result.getException().getMessage()); } { // test script exec item final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName()); final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>(); final ExecutionItem testWorkflowCmdItem = new ScriptFileCommandBase() { @Override public String getScript() { return "a command"; } }; commands.add(testWorkflowCmdItem); final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .nodeSelector(nodeset) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); testInterpreter failMock = new testInterpreter(); failMock.shouldThrowException = true; interpreterService.registerInstance("exec", failMock); interpreterService.registerInstance("script", interpreterMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock); // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock); // set resturn result interpreterMock.resultList.add( new InterpreterResult() { public boolean isSuccess() { return true; } }); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertNull("threw exception: " + result.getException(), result.getException()); assertTrue(result.isSuccess()); assertEquals(1, interpreterMock.executionItemList.size()); final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0); assertTrue( "wrong class: " + executionItem1.getClass().getName(), executionItem1 instanceof ScriptFileCommandExecutionItem); ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) executionItem1; assertEquals("a command", scriptItem.getScript()); assertNull(scriptItem.getScriptAsStream()); assertNull(scriptItem.getServerScriptFilePath()); assertEquals(1, interpreterMock.executionContextList.size()); final ExecutionContext executionContext = interpreterMock.executionContextList.get(0); assertEquals(TEST_PROJECT, executionContext.getFrameworkProject()); assertNull(executionContext.getArgs()); assertNull(executionContext.getDataContext()); assertEquals(0, executionContext.getLoglevel()); assertEquals("user1", executionContext.getUser()); assertEquals( "expected " + nodeset + ", but was " + executionContext.getNodeSelector(), nodeset, executionContext.getNodeSelector()); } { // test command exec item final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName()); final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>(); final ExecutionItem testWorkflowCmdItem = new ExecCommandBase() { @Override public String[] getCommand() { return new String[] {"a", "command"}; } }; commands.add(testWorkflowCmdItem); final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .nodeSelector(nodeset) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); testInterpreter failMock = new testInterpreter(); failMock.shouldThrowException = true; interpreterService.registerInstance("exec", interpreterMock); interpreterService.registerInstance("script", failMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock); // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock); // set resturn result interpreterMock.resultList.add( new InterpreterResult() { public boolean isSuccess() { return true; } }); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertNull("threw exception: " + result.getException(), result.getException()); assertTrue(result.isSuccess()); assertEquals(1, interpreterMock.executionItemList.size()); final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0); assertTrue( "wrong class: " + executionItem1.getClass().getName(), executionItem1 instanceof ExecCommandExecutionItem); ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1; assertNotNull(execItem.getCommand()); assertEquals(2, execItem.getCommand().length); assertEquals("a", execItem.getCommand()[0]); assertEquals("command", execItem.getCommand()[1]); assertEquals(1, interpreterMock.executionContextList.size()); final ExecutionContext executionContext = interpreterMock.executionContextList.get(0); assertEquals(TEST_PROJECT, executionContext.getFrameworkProject()); assertNull(executionContext.getArgs()); assertNull(executionContext.getDataContext()); assertEquals(0, executionContext.getLoglevel()); assertEquals("user1", executionContext.getUser()); assertEquals(nodeset, executionContext.getNodeSelector()); } { // test workflow of three successful items final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName()); final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>(); final ExecutionItem testWorkflowCmdItem = new ExecCommandBase() { @Override public String[] getCommand() { return new String[] {"a", "2", "command"}; } }; commands.add(testWorkflowCmdItem); final ExecutionItem testWorkflowCmdItemScript = new ScriptFileCommandBase() { @Override public String getScript() { return "a command"; } @Override public String[] getArgs() { return new String[] {"-testargs", "1"}; } }; commands.add(testWorkflowCmdItemScript); final ExecutionItem testWorkflowCmdItemScript2 = new ScriptFileCommandBase() { @Override public String getServerScriptFilePath() { return "/some/file/path"; } @Override public String[] getArgs() { return new String[] {"-testargs", "2"}; } }; commands.add(testWorkflowCmdItemScript2); final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .args(new String[] {"test", "args"}) .nodeSelector(nodeset) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); testInterpreter failMock = new testInterpreter(); failMock.shouldThrowException = true; // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, // interpreterMock); interpreterService.registerInstance("exec", interpreterMock); interpreterService.registerInstance("script", interpreterMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock); // set resturn results interpreterMock.resultList.add(new testResult(true, 0)); interpreterMock.resultList.add(new testResult(true, 1)); interpreterMock.resultList.add(new testResult(true, 2)); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertNull("threw exception: " + result.getException(), result.getException()); assertTrue(result.isSuccess()); assertEquals(1, result.getResultSet().size()); assertNotNull(result.getResultSet()); assertNotNull( "missing key " + testnode + ": " + result.getResultSet().keySet(), result.getResultSet().get(testnode)); final List<StatusResult> test1 = result.getResultSet().get(testnode); assertEquals(3, test1.size()); for (final int i : new int[] {0, 1, 2}) { final StatusResult interpreterResult = test1.get(i); assertTrue(interpreterResult instanceof testResult); testResult val = (testResult) interpreterResult; assertTrue(val.isSuccess()); assertEquals(i, val.flag); } assertEquals(3, interpreterMock.executionItemList.size()); final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0); assertTrue( "wrong class: " + executionItem1.getClass().getName(), executionItem1 instanceof ExecCommandExecutionItem); ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1; assertNotNull(execItem.getCommand()); assertEquals(3, execItem.getCommand().length); assertEquals("a", execItem.getCommand()[0]); assertEquals("2", execItem.getCommand()[1]); assertEquals("command", execItem.getCommand()[2]); final ExecutionItem item2 = interpreterMock.executionItemList.get(1); assertTrue( "wrong class: " + item2.getClass().getName(), item2 instanceof ScriptFileCommandExecutionItem); ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2; assertEquals("a command", scriptItem.getScript()); assertNull(scriptItem.getScriptAsStream()); assertNull(scriptItem.getServerScriptFilePath()); final ExecutionItem item3 = interpreterMock.executionItemList.get(2); assertTrue( "wrong class: " + item3.getClass().getName(), item2 instanceof ScriptFileCommandExecutionItem); ScriptFileCommandExecutionItem scriptItem2 = (ScriptFileCommandExecutionItem) item3; assertNull(scriptItem2.getScript()); assertNull(scriptItem2.getScriptAsStream()); assertEquals("/some/file/path", scriptItem2.getServerScriptFilePath()); assertNotNull(scriptItem2.getArgs()); assertEquals(2, scriptItem2.getArgs().length); assertEquals("-testargs", scriptItem2.getArgs()[0]); assertEquals("2", scriptItem2.getArgs()[1]); assertEquals(3, interpreterMock.executionContextList.size()); for (final int i : new int[] {0, 1, 2}) { final ExecutionContext executionContext = interpreterMock.executionContextList.get(i); assertEquals("item " + i, TEST_PROJECT, executionContext.getFrameworkProject()); assertNull("item " + i, executionContext.getDataContext()); assertEquals("item " + i, 0, executionContext.getLoglevel()); assertEquals("item " + i, "user1", executionContext.getUser()); assertEquals("item " + i, nodeset, executionContext.getNodeSelector()); assertNotNull("item " + i, executionContext.getArgs()); assertEquals("item " + i, 2, executionContext.getArgs().length); assertEquals("item " + i, "test", executionContext.getArgs()[0]); assertEquals("item " + i, "args", executionContext.getArgs()[1]); } } { // test a workflow with a failing item (1), with keepgoing=false final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName()); final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>(); final ExecutionItem testWorkflowCmdItem = new ExecCommandBase() { @Override public String[] getCommand() { return new String[] {"a", "2", "command"}; } }; commands.add(testWorkflowCmdItem); final ExecutionItem testWorkflowCmdItemScript = new ScriptFileCommandBase() { @Override public String getScript() { return "a command"; } @Override public String[] getArgs() { return new String[] {"-testargs", "1"}; } }; commands.add(testWorkflowCmdItemScript); final ExecutionItem testWorkflowCmdItemScript2 = new ScriptFileCommandBase() { @Override public String getServerScriptFilePath() { return "/some/file/path"; } @Override public String[] getArgs() { return new String[] {"-testargs", "2"}; } }; commands.add(testWorkflowCmdItemScript2); final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST); workflow.setKeepgoing(false); final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow); final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework); final com.dtolabs.rundeck.core.execution.ExecutionContext context = new ExecutionContextImpl.Builder() .frameworkProject(TEST_PROJECT) .user("user1") .nodeSelector(nodeset) .executionListener(new testListener()) .framework(testFramework) .build(); // setup testInterpreter for all command types final CommandInterpreterService interpreterService = CommandInterpreterService.getInstanceForFramework(testFramework); testInterpreter interpreterMock = new testInterpreter(); testInterpreter failMock = new testInterpreter(); failMock.shouldThrowException = true; // interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, // interpreterMock); interpreterService.registerInstance("exec", interpreterMock); interpreterService.registerInstance("script", interpreterMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock); interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock); // set resturn results, fail on second item interpreterMock.resultList.add(new testResult(true, 0)); interpreterMock.resultList.add(new testResult(false, 1)); interpreterMock.resultList.add(new testResult(true, 2)); final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem); assertNotNull(result); if (!result.isSuccess() && null != result.getException()) { result.getException().printStackTrace(System.err); } assertFalse(result.isSuccess()); assertNotNull("threw exception: " + result.getException(), result.getException()); assertTrue( "threw exception: " + result.getException(), result.getException() instanceof WorkflowStepFailureException); WorkflowStepFailureException wfsfe = (WorkflowStepFailureException) result.getException(); assertEquals(2, wfsfe.getWorkflowStep()); assertNotNull(wfsfe.getExecutionResult()); final ExecutionResult executionResult = wfsfe.getExecutionResult(); assertNotNull(executionResult.getResultObject()); assertNotNull(executionResult.getResultObject().getResults()); assertEquals(1, executionResult.getResultObject().getResults().size()); assertNotNull(executionResult.getResultObject().getResults().get(testnode)); final StatusResult testnode1 = executionResult.getResultObject().getResults().get(testnode); assertNotNull(testnode1); assertTrue(testnode1 instanceof testResult); testResult failResult = (testResult) testnode1; assertEquals(1, failResult.flag); assertEquals(1, result.getResultSet().size()); assertNotNull(result.getResultSet()); assertNotNull( "missing key" + testnode + ": " + result.getResultSet().keySet(), result.getResultSet().get(testnode)); final List<StatusResult> test1 = result.getResultSet().get(testnode); assertEquals(2, test1.size()); for (final int i : new int[] {0, 1}) { final StatusResult interpreterResult = test1.get(i); assertTrue(interpreterResult instanceof testResult); testResult val = (testResult) interpreterResult; assertEquals(i, val.flag); if (0 == i) { assertTrue(val.isSuccess()); } else { assertFalse(val.isSuccess()); } } assertEquals(2, interpreterMock.executionItemList.size()); final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0); assertTrue( "wrong class: " + executionItem1.getClass().getName(), executionItem1 instanceof ExecCommandExecutionItem); ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1; assertNotNull(execItem.getCommand()); assertEquals(3, execItem.getCommand().length); assertEquals("a", execItem.getCommand()[0]); assertEquals("2", execItem.getCommand()[1]); assertEquals("command", execItem.getCommand()[2]); final ExecutionItem item2 = interpreterMock.executionItemList.get(1); assertTrue( "wrong class: " + item2.getClass().getName(), item2 instanceof ScriptFileCommandExecutionItem); ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2; assertEquals("a command", scriptItem.getScript()); assertNull(scriptItem.getScriptAsStream()); assertNull(scriptItem.getServerScriptFilePath()); assertNotNull(scriptItem.getArgs()); assertEquals(2, scriptItem.getArgs().length); assertEquals("-testargs", scriptItem.getArgs()[0]); assertEquals("1", scriptItem.getArgs()[1]); assertEquals(2, interpreterMock.executionContextList.size()); for (final int i : new int[] {0, 1}) { final ExecutionContext executionContext = interpreterMock.executionContextList.get(i); assertEquals(TEST_PROJECT, executionContext.getFrameworkProject()); assertNull(executionContext.getDataContext()); assertEquals(0, executionContext.getLoglevel()); assertEquals("user1", executionContext.getUser()); assertEquals(nodeset, executionContext.getNodeSelector()); assertNull(executionContext.getArgs()); } } }