def test_accumulated_risk_with_error(self):
        workflow = Workflow(name='workflow')
        workflow._execution_uid = 'some_uid'
        step1 = Step(name="step_one", app='HelloWorld', action='Buggy', risk=1)
        step2 = Step(name="step_two", app='HelloWorld', action='Buggy', risk=2)
        step3 = Step(name="step_three",
                     app='HelloWorld',
                     action='Buggy',
                     risk=3.5)
        workflow.steps = {
            'step_one': step1,
            'step_two': step2,
            'step_three': step3
        }
        workflow._total_risk = 6.5

        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='test_device_name')

        workflow._Workflow__execute_step(workflow.steps["step_one"], instance)
        self.assertAlmostEqual(workflow.accumulated_risk, 1.0 / 6.5)
        workflow._Workflow__execute_step(workflow.steps["step_two"], instance)
        self.assertAlmostEqual(workflow.accumulated_risk,
                               (1.0 / 6.5) + (2.0 / 6.5))
        workflow._Workflow__execute_step(workflow.steps["step_three"],
                                         instance)
        self.assertAlmostEqual(workflow.accumulated_risk, 1.0)
Пример #2
0
    def test_execute_multiple_triggers(self):
        triggers = [
            Condition('HelloWorld',
                      action_name='regMatch',
                      arguments=[Argument('regex', value='aaa')])
        ]
        action = Action(app_name='HelloWorld',
                        action_name='helloWorld',
                        triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        action.send_data_to_trigger({"data_in": {"data": 'a'}})

        trigger_taken = {'triggered': 0}
        trigger_not_taken = {'triggered': 0}

        def callback_is_sent(sender, **kwargs):
            if kwargs['event'] == WalkoffEvent.TriggerActionTaken:
                trigger_taken['triggered'] += 1
            elif kwargs['event'] == WalkoffEvent.TriggerActionNotTaken:
                action.send_data_to_trigger({"data_in": {"data": 'aaa'}})
                trigger_not_taken['triggered'] += 1

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})
        self.assertEqual(trigger_taken['triggered'], 1)
        self.assertEqual(trigger_not_taken['triggered'], 1)
Пример #3
0
 def __setup_app_instance(self, instances, step):
     device_id = (step.app, step.device)
     if device_id not in instances:
         instances[device_id] = AppInstance.create(step.app, step.device)
         data_sent.send(self, callback_name="App Instance Created", object_type="Workflow")
         logger.debug('Created new app instance: App {0}, device {1}'.format(step.app, step.device))
     return device_id
Пример #4
0
 def test_execute_with_complex_inputs(self):
     step = Step(app='HelloWorld',
                 action='Json Sample',
                 inputs={
                     'json_in': {
                         'a':
                         '-5.6',
                         'b': {
                             'a': '4.3',
                             'b': 5.3
                         },
                         'c': ['1', '2', '3'],
                         'd': [{
                             'a': '',
                             'b': 3
                         }, {
                             'a': '',
                             'b': -1.5
                         }, {
                             'a': '',
                             'b': -0.5
                         }]
                     }
                 })
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = step.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 11.0)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(step._output, result)
Пример #5
0
 def test_execute_generates_uid(self):
     step = Step(app='HelloWorld', action='helloWorld')
     original_execution_uid = step.get_execution_uid()
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     step.execute(instance.instance, {})
     self.assertNotEqual(step.get_execution_uid(), original_execution_uid)
Пример #6
0
    def test_execute_with_accumulator_missing_step_callbacks(self):
        step = Step(app='HelloWorld',
                    action='Add Three',
                    inputs={
                        'num1': '@1',
                        'num2': '@step2',
                        'num3': '10.2'
                    })
        accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        @callbacks.data_sent.connect
        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Step):
                self.assertIs(sender, step)
                self.assertIn('callback_name', kwargs)
                self.assertIn(kwargs['callback_name'],
                              ('Step Started', 'Step Input Invalid'))
                self.assertIn('object_type', kwargs)
                self.assertEqual(kwargs['object_type'], 'Step')
                if kwargs['callback_name'] == 'Step Started':
                    result['started_triggered'] = True
                else:
                    result['result_triggered'] = True

        with self.assertRaises(InvalidInput):
            step.execute(instance.instance, accumulator)

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Пример #7
0
    def test_execute_with_accumulator_missing_action_callbacks(self):
        action = Action(app_name='HelloWorld',
                        action_name='Add Three',
                        arguments=[
                            Argument('num1', reference='1'),
                            Argument('num2', reference='action2'),
                            Argument('num3', value='10.2')
                        ])
        accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionArgumentsInvalid))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                else:
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)
        action.execute(instance.instance, accumulator)

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Пример #8
0
 def test_execute_with_complex_args(self):
     action = Action(app_name='HelloWorld',
                     action_name='Json Sample',
                     arguments=[
                         Argument('json_in',
                                  value={
                                      'a':
                                      '-5.6',
                                      'b': {
                                          'a': '4.3',
                                          'b': 5.3
                                      },
                                      'c': ['1', '2', '3'],
                                      'd': [{
                                          'a': '',
                                          'b': 3
                                      }, {
                                          'a': '',
                                          'b': -1.5
                                      }, {
                                          'a': '',
                                          'b': -0.5
                                      }]
                                  })
                     ])
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 11.0)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Пример #9
0
 def test_execute_action_which_raises_exception(self):
     from tests.testapps.HelloWorld.exceptions import CustomException
     step = Step(app='HelloWorld', action='Buggy')
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     with self.assertRaises(CustomException):
         step.execute(instance.instance, {})
Пример #10
0
    def test_execute_sends_callbacks(self):
        action = Action(app_name='HelloWorld',
                        action_name='Add Three',
                        arguments=[
                            Argument('num1', value='-5.6'),
                            Argument('num2', value='4.3'),
                            Argument('num3', value='10.2')
                        ])
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionExecutionSuccess))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                else:
                    self.assertIn('data', kwargs)
                    data = kwargs['data']
                    self.assertEqual(data['status'], 'Success')
                    self.assertAlmostEqual(data['result'], 8.9)
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})
        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Пример #11
0
 def test_execute_generates_uid(self):
     action = Action(app_name='HelloWorld', action_name='helloWorld')
     original_execution_uid = action.get_execution_uid()
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, {})
     self.assertNotEqual(action.get_execution_uid(), original_execution_uid)
Пример #12
0
 def test_execute_no_args(self):
     step = Step(app='HelloWorld', action='helloWorld')
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     self.assertEqual(step.execute(instance.instance, {}),
                      ActionResult({'message': 'HELLO WORLD'}, 'Success'))
     self.assertEqual(step._output,
                      ActionResult({'message': 'HELLO WORLD'}, 'Success'))
Пример #13
0
 def test_execute_global_action(self):
     action = Action(app_name='HelloWorld',
                     action_name='global2',
                     arguments=[Argument('arg1', value='something')])
     instance = AppInstance.create(app_name='HelloWorld', device_name='')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 'something')
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Пример #14
0
 def test_execute_global_action(self):
     step = Step(app='HelloWorld',
                 action='global2',
                 inputs={'arg1': 'something'})
     instance = AppInstance.create(app_name='HelloWorld', device_name='')
     result = step.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 'something')
     self.assertEqual(result.status, 'Success')
     self.assertEqual(step._output, result)
Пример #15
0
 def __setup_app_instance(self, instances, action):
     device_id = (action.app_name, action.device_id)
     if device_id not in instances:
         instances[device_id] = AppInstance.create(action.app_name,
                                                   action.device_id)
         WalkoffEvent.CommonWorkflowSignal.send(
             self, event=WalkoffEvent.AppInstanceCreated)
         logger.debug(
             'Created new app instance: App {0}, device {1}'.format(
                 action.app_name, action.device_id))
     return device_id
Пример #16
0
 def test_execute_with_accumulator_missing_action(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', reference='1'),
                         Argument('num2', reference='action2'),
                         Argument('num3', value='10.2')
                     ])
     accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, accumulator)
Пример #17
0
 def test_execute_with_accumulator_missing_step(self):
     step = Step(app='HelloWorld',
                 action='Add Three',
                 inputs={
                     'num1': '@1',
                     'num2': '@step2',
                     'num3': '10.2'
                 })
     accumulator = {'1': '-5.6', 'missing': '4.3', '3': '45'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     with self.assertRaises(InvalidInput):
         step.execute(instance.instance, accumulator)
Пример #18
0
 def test_execute_with_args(self):
     step = Step(app='HelloWorld',
                 action='Add Three',
                 inputs={
                     'num1': '-5.6',
                     'num2': '4.3',
                     'num3': '10.2'
                 })
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = step.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(step._output, result)
Пример #19
0
 def test_execute_with_args(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', value='-5.6'),
                         Argument('num2', value='4.3'),
                         Argument('num3', value='10.2')
                     ])
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, {})
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Пример #20
0
 def test_execute_with_accumulator_with_extra_actions(self):
     action = Action(app_name='HelloWorld',
                     action_name='Add Three',
                     arguments=[
                         Argument('num1', reference='1'),
                         Argument('num2', reference='action2'),
                         Argument('num3', value='10.2')
                     ])
     accumulator = {'1': '-5.6', 'action2': '4.3', '3': '45'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = action.execute(instance.instance, accumulator)
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(action._output, result)
Пример #21
0
 def test_execute_with_accumulator_with_conversion(self):
     step = Step(app='HelloWorld',
                 action='Add Three',
                 inputs={
                     'num1': '@1',
                     'num2': '@step2',
                     'num3': '10.2'
                 })
     accumulator = {'1': '-5.6', 'step2': '4.3'}
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     result = step.execute(instance.instance, accumulator)
     self.assertAlmostEqual(result.result, 8.9)
     self.assertEqual(result.status, 'Success')
     self.assertEqual(step._output, result)
Пример #22
0
    def test_execute_with_triggers(self):
        triggers = [Flag(action='regMatch', args={'regex': 'aaa'})]
        step = Step(app='HelloWorld', action='helloWorld', triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        step.send_data_to_trigger({"data_in": {"data": 'aaa'}})

        result = {'triggered': False}

        @callbacks.data_sent.connect
        def callback_is_sent(sender, **kwargs):
            if kwargs['callback_name'] == "Trigger Step Taken":
                result['triggered'] = True

        step.execute(instance.instance, {})
        self.assertTrue(result['triggered'])
Пример #23
0
    def test_execute_multiple_triggers(self):
        triggers = [Flag(action='regMatch', args={'regex': 'aaa'})]
        step = Step(app='HelloWorld', action='helloWorld', triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        step.send_data_to_trigger({"data_in": {"data": 'a'}})

        trigger_taken = {'triggered': 0}
        trigger_not_taken = {'triggered': 0}

        @callbacks.data_sent.connect
        def callback_is_sent(sender, **kwargs):
            if kwargs['callback_name'] == "Trigger Step Taken":
                trigger_taken['triggered'] += 1
            elif kwargs['callback_name'] == "Trigger Step Not Taken":
                step.send_data_to_trigger({"data_in": {"data": 'aaa'}})
                trigger_not_taken['triggered'] += 1

        step.execute(instance.instance, {})
        self.assertEqual(trigger_taken['triggered'], 1)
        self.assertEqual(trigger_not_taken['triggered'], 1)
Пример #24
0
    def test_execute_action_which_raises_exception_sends_callbacks(self):
        from tests.testapps.HelloWorld.exceptions import CustomException
        step = Step(app='HelloWorld', action='Buggy')
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False}

        @callbacks.data_sent.connect
        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Step):
                self.assertIs(sender, step)
                self.assertIn('callback_name', kwargs)
                self.assertEqual(kwargs['callback_name'], 'Step Started')
                self.assertIn('object_type', kwargs)
                self.assertEqual(kwargs['object_type'], 'Step')
                result['started_triggered'] = True

        with self.assertRaises(CustomException):
            step.execute(instance.instance, {})

        self.assertTrue(result['started_triggered'])
Пример #25
0
    def test_execute_with_triggers(self):
        triggers = [
            Condition('HelloWorld',
                      action_name='regMatch',
                      arguments=[Argument('regex', value='aaa')])
        ]
        action = Action(app_name='HelloWorld',
                        action_name='helloWorld',
                        triggers=triggers)
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')
        action.send_data_to_trigger({"data_in": {"data": 'aaa'}})

        result = {'triggered': False}

        def callback_is_sent(sender, **kwargs):
            if kwargs['event'] == WalkoffEvent.TriggerActionTaken:
                result['triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)
        action.execute(instance.instance, {})
        self.assertTrue(result['triggered'])
Пример #26
0
    def test_execute_event(self):
        step = Step(app='HelloWorld',
                    action='Sample Event',
                    inputs={'arg1': 1})
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        import time
        from tests.testapps.HelloWorld.events import event1
        import threading

        def sender():
            time.sleep(0.1)
            event1.trigger(3)

        thread = threading.Thread(target=sender)
        start = time.time()
        thread.start()
        result = step.execute(instance.instance, {})
        end = time.time()
        thread.join()
        self.assertEqual(result, ActionResult(4, 'Success'))
        self.assertGreater((end - start), 0.1)
Пример #27
0
    def test_execute_action_which_raises_exception_sends_callbacks(self):
        action = Action(app_name='HelloWorld', action_name='Buggy')
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Action):
                self.assertIn('event', kwargs)
                self.assertIn(kwargs['event'],
                              (WalkoffEvent.ActionStarted,
                               WalkoffEvent.ActionExecutionError))
                if kwargs['event'] == WalkoffEvent.ActionStarted:
                    result['started_triggered'] = True
                elif kwargs['event'] == WalkoffEvent.ActionExecutionError:
                    result['result_triggered'] = True

        WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent)

        action.execute(instance.instance, {})

        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Пример #28
0
    def test_execute_sends_callbacks(self):
        step = Step(app='HelloWorld',
                    action='Add Three',
                    inputs={
                        'num1': '-5.6',
                        'num2': '4.3',
                        'num3': '10.2'
                    })
        instance = AppInstance.create(app_name='HelloWorld',
                                      device_name='device1')

        result = {'started_triggered': False, 'result_triggered': False}

        @callbacks.data_sent.connect
        def callback_is_sent(sender, **kwargs):
            if isinstance(sender, Step):
                self.assertIs(sender, step)
                self.assertIn('callback_name', kwargs)
                self.assertIn(kwargs['callback_name'],
                              ('Step Started', 'Function Execution Success'))
                self.assertIn('object_type', kwargs)
                self.assertEqual(kwargs['object_type'], 'Step')
                if kwargs['callback_name'] == 'Step Started':
                    result['started_triggered'] = True
                else:
                    self.assertIn('data', kwargs)
                    data = json.loads(kwargs['data'])
                    self.assertIn('result', data)
                    data = data['result']
                    self.assertEqual(data['status'], 'Success')
                    self.assertAlmostEqual(data['result'], 8.9)
                    result['result_triggered'] = True

        step.execute(instance.instance, {})
        self.assertTrue(result['started_triggered'])
        self.assertTrue(result['result_triggered'])
Пример #29
0
 def test_execute_action_which_raises_exception(self):
     action = Action(app_name='HelloWorld', action_name='Buggy')
     instance = AppInstance.create(app_name='HelloWorld',
                                   device_name='device1')
     action.execute(instance.instance, {})
     self.assertIsNotNone(action.get_output())