Ejemplo n.º 1
0
    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'])
Ejemplo n.º 2
0
 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'])
Ejemplo n.º 3
0
    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')
Ejemplo n.º 6
0
    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'})
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
 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")
Ejemplo n.º 12
0
 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])
Ejemplo n.º 13
0
    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')
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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'])
Ejemplo n.º 16
0
 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'))
Ejemplo n.º 17
0
 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))
Ejemplo n.º 18
0
 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'))
Ejemplo n.º 19
0
 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'))
Ejemplo n.º 20
0
 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)
Ejemplo n.º 21
0
 def test_get_branch_no_branches(self):
     workflow = Workflow('test', 1)
     self.assertIsNone(workflow.get_branch(None, {}))
Ejemplo n.º 22
0
 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'))
Ejemplo n.º 23
0
 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'])
Ejemplo n.º 24
0
 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)
Ejemplo n.º 25
0
 def test_has_workflow_id(self):
     workflow = Workflow('wf_name', 0)
     playbook = Playbook('test', [workflow])
     self.assertTrue(playbook.has_workflow_id(workflow.id))
Ejemplo n.º 26
0
 def test_has_workflow_name_no_name(self):
     workflow = Workflow('wf_name', 0)
     playbook = Playbook('test', [workflow])
     self.assertFalse(playbook.has_workflow_name('invalid'))
Ejemplo n.º 27
0
 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)
Ejemplo n.º 28
0
 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()))