def test_pause_and_resume_workflow_breakpoint(self):
        from gevent import monkey
        monkey.patch_all()
        self.controller.load_workflows_from_file(path=path.join(
            config.test_workflows_path, 'pauseWorkflowTest.playbook'))

        waiter = Event()

        def step_2_finished_listener(sender, **kwargs):
            if sender.name == '2':
                waiter.set()

        def pause_resume_thread():
            self.controller.add_workflow_breakpoint_steps(
                'pauseWorkflowTest', 'pauseWorkflow', ['2'])
            gevent.sleep(1.5)
            self.controller.resume_breakpoint_step('pauseWorkflowTest',
                                                   'pauseWorkflow')

        def step_1_about_to_begin_listener(sender, **kwargs):
            if sender.name == '1':
                gevent.spawn(pause_resume_thread)

        FunctionExecutionSuccess.connect(step_2_finished_listener)
        StepInputValidated.connect(step_1_about_to_begin_listener)

        start = default_timer()
        self.controller.execute_workflow('pauseWorkflowTest', 'pauseWorkflow')
        waiter.wait(timeout=5)
        duration = default_timer() - start
        self.assertTrue(2.5 < duration < 5)
Exemplo n.º 2
0
    def test_trigger_execute_tag(self):
        condition = {"flag": 'regMatch', "args": [{'name': 'regex', 'value': '(.*)'}], "filters": []}
        data = {"playbook": "test",
                "workflow": self.test_trigger_workflow,
                "conditional": json.dumps([condition]),
                "tag": "wrong_tag"}
        self.put_with_status_check('/execution/listener/triggers/{0}'.format(self.test_trigger_name),
                                   headers=self.headers, data=data, status_code=OBJECT_CREATED)

        data["tag"] = "execute_tag"

        self.put_with_status_check('/execution/listener/triggers/{0}'.format("execute_one"),
                                   headers=self.headers, data=data, status_code=OBJECT_CREATED)
        self.put_with_status_check('/execution/listener/triggers/{0}'.format("execute_two"),
                                   headers=self.headers, data=data, status_code=OBJECT_CREATED)

        result = {'value': 0}

        def step_finished_listener(sender, **kwargs):
            result['value'] += 1

        FunctionExecutionSuccess.connect(step_finished_listener)

        data = {"data": "hellohellohello"}

        response = self.post_with_status_check('/execution/listener/execute?tags=execute_tag',
                                               headers=self.headers, data=data, status_code=SUCCESS_ASYNC)
        with server.running_context.flask_app.app_context():
            server.running_context.shutdown_threads()

        self.assertEqual(2, result['value'])
        self.assertEqual(len(response['executed']), 2)
        executed_names = {executed['name'] for executed in response['executed']}
        self.assertSetEqual(executed_names, {'execute_one', 'execute_two'})
        self.assertEqual(0, len(response["errors"]))
Exemplo n.º 3
0
    def test_trigger_execute_change_input(self):
        condition = {"flag": 'regMatch', "args": [{'name': 'regex', 'value': '(.*)'}], "filters": []}
        data = {"playbook": "test",
                "workflow": self.test_trigger_workflow,
                "conditional": json.dumps([condition])}
        self.put_with_status_check('/execution/listener/triggers/{0}'.format(self.test_trigger_name),
                                   headers=self.headers, data=data, status_code=OBJECT_CREATED)

        result = {'value': None}

        def step_finished_listener(sender, **kwargs):
            result['value'] = kwargs['data']

        FunctionExecutionSuccess.connect(step_finished_listener)

        data = {"data": "hellohellohello",
                "input": json.dumps({"call": "CHANGE INPUT"})}

        response = self.post_with_status_check('/execution/listener/execute',
                                               headers=self.headers, data=data, status_code=SUCCESS_ASYNC)
        self.assertSetEqual(set(response.keys()), {'errors', 'executed'})
        self.assertEqual(len(response['executed']), 1)
        self.assertIn('id', response['executed'][0])
        self.assertEqual(response['executed'][0]['name'], 'testTrigger')
        self.assertListEqual(response['errors'], [])
        step_input = {'result': 'REPEATING: CHANGE INPUT'}
        self.assertDictEqual(json.loads(result['value']), step_input)
    def test_pause_and_resume_workflow(self):
        self.c.load_workflows_from_file(path=path.join(
            config.test_workflows_path, 'pauseWorkflowTest.workflow'))

        waiter = Event()

        def step_2_finished_listener(sender, **kwargs):
            if sender.name == '2':
                waiter.set()

        def pause_resume_thread():
            uuid = self.c.pause_workflow('pauseWorkflowTest', 'pauseWorkflow')
            gevent.sleep(1.5)
            self.c.resume_workflow('pauseWorkflowTest', 'pauseWorkflow', uuid)

        def step_1_about_to_begin_listener(sender, **kwargs):
            if sender.name == '1':
                gevent.spawn(pause_resume_thread)

        FunctionExecutionSuccess.connect(step_2_finished_listener)
        StepInputValidated.connect(step_1_about_to_begin_listener)

        start = default_timer()
        self.c.execute_workflow('pauseWorkflowTest', 'pauseWorkflow')
        waiter.wait(timeout=5)
        duration = default_timer() - start
        self.assertTrue(2.5 < duration < 5)
Exemplo n.º 5
0
    def test_trigger_execute_multiple_tags(self):
        condition = {
            "flag": 'regMatch',
            "args": [{
                "key": "regex",
                "value": '(.*)'
            }],
            "filters": []
        }
        data = {
            "playbook": "test",
            "workflow": self.test_trigger_workflow,
            "conditional": json.dumps([condition]),
            "tag": "wrong_tag"
        }
        self.put_with_status_check('/execution/listener/triggers/{0}'.format(
            self.test_trigger_name),
                                   headers=self.headers,
                                   data=data,
                                   status_code=OBJECT_CREATED)

        data["tag"] = "execute_tag"

        self.put_with_status_check(
            '/execution/listener/triggers/{0}'.format("execute_one"),
            headers=self.headers,
            data=data,
            status_code=OBJECT_CREATED)

        data["tag"] = "execute_tag_two"
        self.put_with_status_check(
            '/execution/listener/triggers/{0}'.format("execute_two"),
            headers=self.headers,
            data=data,
            status_code=OBJECT_CREATED)

        result = {'value': 0}

        def step_finished_listener(sender, **kwargs):
            result['value'] += 1

        FunctionExecutionSuccess.connect(step_finished_listener)

        data = {"data": "hellohellohello"}

        response = self.post_with_status_check(
            '/execution/listener/execute?tags=execute_tag&tags=execute_tag_two',
            headers=self.headers,
            data=data)
        self.assertEqual(2, result['value'])
        self.assertIn("execute_one", response["executed"])
        self.assertIn("execute_two", response["executed"])
        self.assertEqual(2, len(response["executed"]))
        self.assertEqual(0, len(response["errors"]))
    def test_change_step_input(self):
        import json

        input_list = [{'key': 'call', 'value': 'CHANGE INPUT'}]

        input_arg = {arg['key']: arg['value'] for arg in input_list}

        result = {'value': None}

        def step_finished_listener(sender, **kwargs):
            result['value'] = kwargs['data']

        FunctionExecutionSuccess.connect(step_finished_listener)

        self.testWorkflow.execute(start_input=input_arg)
        self.assertDictEqual(json.loads(result['value']),
                             {"result": "REPEATING: CHANGE INPUT"})
Exemplo n.º 7
0
    def test_trigger_execute_change_input(self):
        condition = {
            "flag": 'regMatch',
            "args": [{
                "key": "regex",
                "value": '(.*)'
            }],
            "filters": []
        }
        data = {
            "playbook": "test",
            "workflow": self.test_trigger_workflow,
            "conditional": json.dumps([condition])
        }
        self.put_with_status_check('/execution/listener/triggers/{0}'.format(
            self.test_trigger_name),
                                   headers=self.headers,
                                   data=data,
                                   status_code=OBJECT_CREATED)

        result = {'value': None}

        def step_finished_listener(sender, **kwargs):
            result['value'] = kwargs['data']

        FunctionExecutionSuccess.connect(step_finished_listener)

        data = {
            "data": "hellohellohello",
            "input": json.dumps([{
                "key": "call",
                "value": "CHANGE INPUT"
            }])
        }

        self.post_with_status_check('/execution/listener/execute',
                                    headers=self.headers,
                                    data=data)

        input = {'result': 'REPEATING: CHANGE INPUT'}
        self.assertDictEqual(json.loads(result['value']), input)
    def test_change_step_input(self):

        input = [{"key": "call", "value": "CHANGE INPUT"}]

        input_arg = {
            arg['key']: Argument(key=arg['key'],
                                 value=arg['value'],
                                 format=arg.get('format', 'str'))
            for arg in input
        }

        result = {'value': None}

        def step_finished_listener(sender, **kwargs):
            result['value'] = kwargs['data']

        FunctionExecutionSuccess.connect(step_finished_listener)

        self.testWorkflow.execute(input=input_arg)
        self.assertDictEqual(json.loads(result['value']),
                             {"result": "REPEATING: CHANGE INPUT"})
    def test_change_step_input(self):
        self.controller.initialize_threading()
        input_list = [{'key': 'call', 'value': 'CHANGE INPUT'}]

        input_arg = {arg['key']: arg['value'] for arg in input_list}

        result = {'value': None}

        def step_finished_listener(sender, **kwargs):
            result['value'] = kwargs['data']

        FunctionExecutionSuccess.connect(step_finished_listener)

        self.controller.execute_workflow('simpleDataManipulationWorkflow',
                                         'helloWorldWorkflow',
                                         start_input=input_arg)
        self.controller.shutdown_pool(1)
        self.assertDictEqual(result['value'], {
            'result': {
                'result': 'REPEATING: CHANGE INPUT',
                'status': 'Success'
            }
        })
Exemplo n.º 10
0
    # TODO: This identifier should be replaced by step id when that happens
    __action_tmp[(sender.app, sender.action)] = datetime.utcnow()


def __action_ended_callback(sender, **kwargs):
    __update_success_action_tracker(sender.app, sender.action)


def __action_ended_error_callback(sender, **kwargs):
    step = json.loads(kwargs['data'])['step']
    __update_error_action_tracker(step['app'], step['action'])


StepInputValidated.connect(__action_started_callback)
StepInputInvalid.connect(__action_started_callback)
FunctionExecutionSuccess.connect(__action_ended_callback)
StepExecutionError.connect(__action_ended_error_callback)


def __update_success_action_tracker(app, action):
    __update_action_tracker('success', app, action)


def __update_error_action_tracker(app, action):
    __update_action_tracker('error', app, action)


def __update_action_tracker(form, app, action):
    if (app, action) in __action_tmp:
        execution_time = datetime.utcnow() - __action_tmp[(app, action)]
        if app not in app_metrics: