def test_execute_action_which_raises_exception(self): from tests.apps.HelloWorld.exceptions import CustomException step = Step(app='HelloWorld', action='Buggy') instance = Instance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(CustomException): step.execute(instance.instance, {})
def test_accumulated_risk_with_error(self): workflow = Workflow(name='workflow') 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 = Instance.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)
def __execute(self, start='start', input=""): instances = {} total_steps = [] steps = self.__steps(start=start) first = True for step in steps: while self.is_paused: _ = yield if step: if step.name in self.breakpoint_steps: _ = yield callbacks.NextStepFound.send(self) if step.device not in instances: instances[step.device] = Instance.create(step.app, step.device) callbacks.AppInstanceCreated.send(self) step.render_step(steps=total_steps) if first: if input: step.input = input first = False error_flag = self.__execute_step(step, instances[step.device]) total_steps.append(step) steps.send(error_flag) self.__shutdown(instances) yield
def test_execute_no_args(self): step = Step(app='HelloWorld', action='helloWorld') instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertDictEqual(step.execute(instance.instance, {}), {'message': 'HELLO WORLD'}) self.assertDictEqual(step.output, {'message': 'HELLO WORLD'})
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 = Instance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, {}) self.assertIsInstance(result, tuple) self.assertAlmostEqual(result.result, 11.0) self.assertEqual(result.status, 'Success') self.assertTupleEqual(step.output, result)
def __execute(self, start, start_input): instances = {} total_steps = [] steps = self.__steps(start=start) first = True for step in steps: logger.debug('Executing step {0} of workflow {1}'.format( step, self.ancestry)) while self.is_paused: _ = yield if step is not None: if step.name in self.breakpoint_steps: _ = yield callbacks.NextStepFound.send(self) if step.device not in instances: instances[step.device] = Instance.create( step.app, step.device) callbacks.AppInstanceCreated.send(self) logger.debug( 'Created new app instance: App {0}, device {1}'.format( step.app, step.device)) step.render_step(steps=total_steps) if first: first = False if start_input: self.__swap_step_input(step, start_input) error_flag = self.__execute_step(step, instances[step.device]) total_steps.append(step) steps.send(error_flag) self.accumulator[step.name] = step.output self.__shutdown(instances) yield
def test_accumulated_risk_with_error(self): workflow = controller.wf.Workflow(name='workflow') workflow.create_step(name="stepOne", app='HelloWorld', action='invalid_name', risk=1) workflow.steps["stepOne"].inputs = { 'call': Argument(key='call', value='HelloWorld', format='str') } workflow.create_step(name="stepTwo", app='HelloWorld', action='repeatBackToMe', risk=2) workflow.steps["stepTwo"].inputs = { 'number': Argument(key='number', value='6', format='str') } workflow.create_step(name="stepThree", app='HelloWorld', action='returnPlusOne', risk=3) workflow.steps["stepThree"].inputs = {} instance = Instance.create(app_name='HelloWorld', device_name='test_device_name') workflow._Workflow__execute_step(workflow.steps["stepOne"], instance=instance()) self.assertEqual(workflow.accumulated_risk, 1.0 / 6.0) workflow._Workflow__execute_step(workflow.steps["stepTwo"], instance=instance()) self.assertEqual(workflow.accumulated_risk, (1.0 / 6.0) + (2.0 / 6.0)) workflow._Workflow__execute_step(workflow.steps["stepThree"], instance=instance()) self.assertEqual(workflow.accumulated_risk, 1.0)
def test_load_app_function(self): app = 'HelloWorld' with server.running_context.flask_app.app_context(): instance = Instance.create(app, 'default_device_name') existing_actions = {'helloWorld': instance().helloWorld, 'repeatBackToMe': instance().repeatBackToMe, 'returnPlusOne': instance().returnPlusOne} for action, function in existing_actions.items(): self.assertEqual(load_app_function(instance(), action), function)
def test_as_json_after_executed(self): step = Step(app='HelloWorld', action='helloWorld') instance = Instance.create(app_name='HelloWorld', device_name='device1') step.execute(instance.instance, {}) step_json = step.as_json() self.assertDictEqual( step_json['output'], ActionResult({ 'message': 'HELLO WORLD' }, 'Success').as_json())
def test_execute_with_args(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertAlmostEqual(step.execute(instance.instance, {}), 8.9) self.assertAlmostEqual(step.output, 8.9)
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 = Instance.create(app_name='HelloWorld', device_name='device1') with self.assertRaises(InvalidInput): step.execute(instance.instance, accumulator)
def test_execute_with_accumulator_with_extra_steps(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '@1', 'num2': '@step2', 'num3': '10.2' }) accumulator = {'1': '-5.6', 'step2': '4.3', '3': '45'} instance = Instance.create(app_name='HelloWorld', device_name='device1') self.assertAlmostEqual(step.execute(instance.instance, accumulator), 8.9) self.assertAlmostEqual(step.output, 8.9)
def test_execute_with_args(self): step = Step(app='HelloWorld', action='Add Three', inputs={ 'num1': '-5.6', 'num2': '4.3', 'num3': '10.2' }) instance = Instance.create(app_name='HelloWorld', device_name='device1') result = step.execute(instance.instance, {}) self.assertIsInstance(result, tuple) self.assertAlmostEqual(result.result, 8.9) self.assertEqual(result.status, 'Success') self.assertTupleEqual(step.output, result)
def test_execute_invalid_inputs(self): app = 'HelloWorld' actions = [('invalid_name', { 'call': Argument(key='call', value='HelloWorld', format='str') }), ('repeatBackToMe', { 'number': Argument(key='number', value='6', format='str') }), ('returnPlusOne', {})] for action, inputs in actions: step = Step(app=app, action=action, inputs=inputs) with server.running_context.flask_app.app_context(): instance = Instance.create(app_name=app, device_name='test_device_name') with self.assertRaises(InvalidStepInputError): step.execute(instance=instance())
def test_execute_event(self): step = Step(app='HelloWorld', action='Sample Event', inputs={'arg1': 1}) instance = Instance.create(app_name='HelloWorld', device_name='device1') import time from tests.apps.HelloWorld.events import event1 def sender(): gevent.sleep(0.1) event1.trigger(3) start = time.time() gevent.spawn(sender) result = step.execute(instance.instance, {}) end = time.time() self.assertTupleEqual(result, (4, 'Success')) self.assertTrue((end - start) > 0.1)
def test_execute(self): app = 'HelloWorld' with server.running_context.flask_app.app_context(): instance = Instance.create(app_name=app, device_name='test_device_name') actions = [('helloWorld', {}, { "message": "HELLO WORLD" }), ('repeatBackToMe', { 'call': Argument(key='call', value='HelloWorld', format='str') }, "REPEATING: HelloWorld"), ('returnPlusOne', { 'number': Argument(key='number', value='6', format='str') }, '7')] for action, inputs, output in actions: step = Step(app=app, action=action, inputs=inputs) self.assertEqual(step.execute(instance=instance()), output) self.assertEqual(step.output, output)
def test_load_app_function_invalid_function(self): with server.running_context.flask_app.app_context(): instance = Instance.create('HelloWorld', 'default_device_name') self.assertIsNone(load_app_function(instance(), 'JunkFunctionName'))