def test_get_branch(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=2) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch = Branch(source_id=action.id, destination_id=2, condition=condition) action._output = ActionResult(result='aaa', status='Success') workflow = Workflow("helloWorld", action.id, actions=[action, action2], branches=[branch]) result = {'triggered': False} def validate_sent_data(sender, **kwargs): if isinstance(sender, Branch): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.BranchTaken) result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(validate_sent_data) self.assertEqual(workflow.get_branch(action, {}), 2) self.assertTrue(result['triggered'])
def test_add_workflow(self): workflow = Workflow('wf_name', 0) playbook = Playbook('test', [workflow]) playbook.add_workflow(Workflow('test2', 0)) orderless_list_compare( self, [workflow.name for workflow in playbook.workflows], ['wf_name', 'test2'])
def test_branch_with_priority(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=5) action3 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch_one = Branch(source_id=action.id, destination_id=5, condition=condition, priority=5) branch_two = Branch(source_id=action.id, destination_id=1, condition=condition, priority=1) action._output = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action, action2, action3], branches=[branch_one, branch_two]) self.assertEqual(workflow.get_branch(action, {}), 1)
def test_load_playbook(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.json') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) loaded = Loader.load_playbook(filepath) self.assertIsInstance(loaded, Playbook) self.assertEqual(loaded.name, 'test')
def test_load_playbooks_some_invalid_playbooks_in_directory(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) test_invalid_json = 'something not json' filepath = os.path.join(test_data_path, 'test2.playbook') with open(filepath, 'w') as file_out: file_out.write(test_invalid_json) loaded = Loader.load_playbooks(test_data_path) self.assertEqual(len(loaded), 1) self.assertEqual(loaded[0].name, 'test')
def test_branch_counter(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) branch = Branch(source_id=action.id, destination_id=action.id) self.assertEqual(branch._counter, 0) accumulator = {} action._output = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action], branches=[branch]) workflow.get_branch(action, accumulator) self.assertEqual(branch._counter, 1) self.assertIn(branch.id, accumulator) self.assertEqual(accumulator[branch.id], 1)
def test_load_multiple_workflows(self): workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test', workflows=workflows) filepath = os.path.join(test_data_path, 'test.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) workflows = [Workflow('something'), Workflow('something2')] playbook = Playbook('test2', workflows=workflows) filepath = os.path.join(test_data_path, 'test2.playbook') with open(filepath, 'w') as file_out: file_out.write(json.dumps(playbook.read())) loaded = Loader.load_playbooks(test_data_path) self.assertEqual(len(loaded), 2) self.assertSetEqual({playbook.name for playbook in loaded}, {'test', 'test2'})
def test_delete_workflow(self): execution_db_help.standard_load() workflows = [Workflow('wf{}'.format(i), uuid4()) for i in range(2)] for workflow in workflows: self.app.running_context.execution_db.session.add(workflow) target_playbook = Playbook('play1', workflows=workflows) self.app.running_context.execution_db.session.add(target_playbook) self.app.running_context.execution_db.session.flush() workflow_ids = [workflow.id for workflow in workflows] original_num_playbooks = len( self.app.running_context.execution_db.session.query( Playbook).all()) self.delete_with_status_check('/api/workflows/{}'.format( workflow_ids[0]), headers=self.headers, status_code=NO_CONTENT) self.assertEqual(len(list(target_playbook.workflows)), len(workflow_ids) - 1) self.assertNotIn( workflow_ids[0], [workflow.id for workflow in target_playbook.workflows]) self.assertEqual( len( self.app.running_context.execution_db.session.query( Playbook).all()), original_num_playbooks)
def upgrade_workflow(workflow): actions = [] action_map = {} for action in workflow['actions']: actions.append(convert_action(action, action_map)) branches = [] if 'branches' in workflow: for branch in workflow['branches']: branch_obj = convert_branch(branch, action_map) if branch_obj: branches.append(branch_obj) name = workflow['name'] if 'name' in workflow else None if 'start' not in workflow: start = actions[0].id print('WARNING: "start" is now a required field for workflows. Setting start for workflow {0} to {1}'.format( name, start)) else: if workflow['start'] in action_map: start = action_map[workflow['start']] else: start = actions[0].id print('WARNING: "start" field does not refer to a valid action for workflow {0}. ' 'Setting "start" to {1}'.format(name, start)) workflow_obj = Workflow(name=name, actions=actions, branches=branches, start=start, id=workflow.get('uid', None)) return workflow_obj
def test_branch_with_priority(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=5) action3 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch_one = Branch(source_id=action.id, destination_id=5, condition=condition, priority=5) branch_two = Branch(source_id=action.id, destination_id=1, condition=condition, priority=1) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action, action2, action3], branches=[branch_one, branch_two]) wf_ctx = WorkflowExecutionContext(workflow, {}, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action self.assertEqual( Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()), 1)
def test_create_playbook_bad_id_in_workflow(self): workflow = Workflow('wf1', uuid4()) self.app.running_context.execution_db.session.add(workflow) self.app.running_context.execution_db.session.flush() workflow_json = WorkflowSchema().dump(workflow).data workflow_json['id'] = 'garbage' data = {'name': self.add_playbook_name, 'workflows': [workflow_json]} self.post_with_status_check('/api/playbooks', data=json.dumps(data), headers=self.headers, status_code=BAD_REQUEST, content_type="application/json")
def test_get_branch_invalid_action(self): condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch = Branch(source_id=1, destination_id=2, condition=condition) action = Action('HelloWorld', 'helloWorld', 'helloWorld') action._output = ActionResult(result='bbb', status='Success') with self.assertRaises(InvalidExecutionElement): Workflow('test', 1, actions=[action], branches=[branch])
def test_branch_counter(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=1) branch = Branch(source_id=action.id, destination_id=action.id) self.assertEqual(branch._counter, 0) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow('test', 1, actions=[action], branches=[branch]) wf_ctx = WorkflowExecutionContext(workflow, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()) self.assertEqual(branch._counter, 1) self.assertIn(branch.id, wf_ctx.accumulator) self.assertEqual(wf_ctx.accumulator[branch.id], '1')
def test_delete_last_workflow(self): execution_db_help.standard_load() workflow = Workflow('wf', uuid4()) executiondb.execution_db.session.add(workflow) target_playbook = Playbook('play1', workflows=[workflow]) executiondb.execution_db.session.add(target_playbook) executiondb.execution_db.session.flush() original_num_playbooks = len( executiondb.execution_db.session.query(Playbook).all()) self.delete_with_status_check('/api/workflows/{}'.format(workflow.id), headers=self.headers, status_code=NO_CONTENT) self.assertIsNone( executiondb.execution_db.session.query(Playbook).filter_by( name='play1').first()) self.assertEqual( len(executiondb.execution_db.session.query(Playbook).all()), original_num_playbooks - 1)
def test_get_branch(self): action = Action('HelloWorld', 'helloWorld', 'helloWorld', id=10) action2 = Action('HelloWorld', 'helloWorld', 'helloWorld', id=2) condition = ConditionalExpression( 'and', conditions=[ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ]) branch = Branch(source_id=action.id, destination_id=2, condition=condition) action_result = ActionResult(result='aaa', status='Success') workflow = Workflow("helloWorld", action.id, actions=[action, action2], branches=[branch]) wf_ctx = WorkflowExecutionContext(workflow, {}, None, None) wf_ctx.accumulator[action.id] = action_result.result wf_ctx.last_status = action_result.status wf_ctx.executing_action = action result = {'triggered': False} def validate_sent_data(sender, **kwargs): if isinstance(sender, Branch): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.BranchTaken) result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(validate_sent_data) self.assertEqual( Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()), 2) self.assertTrue(result['triggered'])
def test_get_workflow_by_name_no_name(self): workflow = Workflow('wf_name', 0) playbook = Playbook('test', [workflow]) self.assertIsNone(playbook.get_workflow_by_name('invalid'))
def test_get_all_workflow_ids(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) orderless_list_compare(self, playbook.get_all_workflow_ids(), list(workflow.id for workflow in workflows))
def test_rename_workflow_not_found(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) playbook.rename_workflow('invalid', 'new_name') self.assertFalse(playbook.has_workflow_name('invalid'))
def test_rename_workflow(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) playbook.rename_workflow('2', 'new_name') self.assertTrue(playbook.has_workflow_name('new_name')) self.assertFalse(playbook.has_workflow_name('2'))
def test_remove_workflow_by_name_workflow_not_found(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) playbook.remove_workflow_by_name('invalid') self.assertEqual(len(playbook.workflows), 3)
def test_get_branch_no_branches(self): workflow = Workflow('test', 1) self.assertIsNone(workflow.get_branch(None, {}))
def test_remove_workflow_by_name(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) playbook.remove_workflow_by_name('2') self.assertEqual(len(playbook.workflows), 2) self.assertFalse(playbook.has_workflow_name('2'))
def test_get_all_workflow_names(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) orderless_list_compare(self, playbook.get_all_workflow_names(), ['0', '1', '2'])
def test_init_with_workflows(self): workflows = [Workflow(str(i), 0) for i in range(3)] playbook = Playbook('test', workflows) self.assertEqual(playbook.name, 'test') self.assertListEqual(playbook.workflows, workflows)
def test_has_workflow_id(self): workflow = Workflow('wf_name', 0) playbook = Playbook('test', [workflow]) self.assertTrue(playbook.has_workflow_id(workflow.id))
def test_has_workflow_name_no_name(self): workflow = Workflow('wf_name', 0) playbook = Playbook('test', [workflow]) self.assertFalse(playbook.has_workflow_name('invalid'))
def test_get_workflow_by_id(self): workflow = Workflow('wf_name', 0) playbook = Playbook('test', [workflow]) self.assertEqual(playbook.get_workflow_by_id(workflow.id), workflow)
def test_get_branch_no_branches(self): workflow = Workflow('test', 1) wf_ctx = WorkflowExecutionContext(workflow, {}, None, None) self.assertIsNone( Executor.get_branch(wf_ctx, LocalActionExecutionStrategy()))