def test_init(self): arg = Argument() self.assertIsNone(arg.key) self.assertEqual(arg.format, 'str') self.assertIsNone(arg.templated) self.assertEqual(arg.value, 'None') arg = Argument(key='test') self.assertEqual(arg.key, 'test') self.assertEqual(arg.format, 'str') self.assertIsNone(arg.templated) self.assertEqual(arg.value, 'None') arg = Argument(key='test', value='val') self.assertEqual(arg.key, 'test') self.assertEqual(arg.format, 'str') self.assertIsNone(arg.templated) self.assertEqual(arg.value, 'val') arg = Argument(key='test', format='int') self.assertEqual(arg.key, 'test') self.assertEqual(arg.format, 'int') self.assertIsNone(arg.templated) self.assertEqual(arg.value, None) arg = Argument(key='test', format='int', value='6') self.assertEqual(arg.key, 'test') self.assertEqual(arg.format, 'int') self.assertIsNone(arg.templated) self.assertEqual(arg.value, 6)
def test_convert_to_int(self): self.assertIsNone(Argument.convert(conversion_type='int')) int_convert_attempts = ((6, 6), (4.5, 4), ('6', 6), ('4.5', '4.5')) for int_type, expected in int_convert_attempts: self.assertEqual( Argument.convert(value=int_type, conversion_type='int'), expected)
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_convert_to_string(self): self.assertEqual(Argument.convert(), 'None') string_types = ['str', 'string', 'unicode'] string_converted_attempts = (6, 'a', None, 4.56, {6: "this_string"}, ['a', 'b']) for string_type in string_types: self.assertEqual(Argument.convert(conversion_type=string_type), 'None') for string_type in string_types: for attempt in string_converted_attempts: self.assertEqual(Argument.convert(value=attempt, conversion_type=string_type), str(attempt))
def test_to_from_json(self): input_output = {Argument(): {'key': 'None', 'value': 'None', 'format': 'str'}, Argument(key='test'): {'key': 'test', 'value': 'None', 'format': 'str'}, Argument(key='test', value='val'): {'key': 'test', 'value': 'val', 'format': 'str'}, Argument(key='test', format='int'): {'key': 'test', 'value': 'None', 'format': 'int'}, Argument(key='test', format='int', value='6'): {'key': 'test', 'value': '6', 'format': 'int'}} for arg, expected_json in input_output.items(): original_json = arg.as_json() self.assertDictEqual(original_json, expected_json) self.assertDictEqual(Argument.from_json(original_json).as_json(), expected_json) arg = Argument(key='test', format='int', value='6') arg2 = Argument.from_json(arg.as_json()) self.assertEqual(arg2.value, 6)
def test_validate_args(self): flag = Flag() self.assertTrue(flag.validate_args()) flag = Flag(action='count') self.assertTrue(flag.validate_args()) flag = Flag(action='junkName') self.assertTrue(flag.validate_args()) flag = Flag( args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name1']['args'] }) self.assertTrue(flag.validate_args()) flag = Flag(action='func_name1', args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name1']['args'] }) self.assertTrue(flag.validate_args()) flag = Flag(action='junkName', args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name1']['args'] }) self.assertTrue(flag.validate_args()) flag = Flag(action='func_name2', args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name2']['args'] }) self.assertTrue(flag.validate_args()) flag = Flag(action='junkName', args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name2']['args'] }) with self.assertRaises(KeyError): flag.validate_args()
def test_create_flag(self): def test_help(_next_step, _expected): self.assertEqual(len(_next_step.flags), len(_expected)) self.assertListEqual([flag.action for flag in _next_step.flags], [flag['action'] for flag in _expected]) for flag, expected_flag in zip(_next_step.flags, _expected): self.assertDictEqual(flag.as_json(), expected_flag) self.assertEqual(flag.parent_name, 'name') expected_ancestry = list(_next_step.ancestry) expected_ancestry.append(flag.name) self.assertListEqual(flag.ancestry, expected_ancestry) next_step = NextStep(name='name') next_step.create_flag('1') expected = [Flag(action='1').as_json()] test_help(next_step, expected) filters = [Filter(action='test_filter_action'), Filter()] next_step.create_flag('2', filters=filters) expected.append(Flag(action='2', filters=filters).as_json()) test_help(next_step, expected) args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } next_step.create_flag('3', filters=filters, args=args) expected.append(Flag(action='3', filters=filters, args=args).as_json()) test_help(next_step, expected)
def execute(data_in, input_in): triggers = Triggers.query.all() from server.flaskserver import running_context for trigger in triggers: conditionals = json.loads(trigger.condition) if all( Triggers.__execute_trigger(conditional, data_in) for conditional in conditionals): workflow_to_be_executed = running_context.controller.get_workflow( trigger.playbook, trigger.workflow) if workflow_to_be_executed: if input_in: input = { arg['key']: Argument(key=arg['key'], value=arg['value'], format=arg.get('format', 'str')) for arg in input_in } workflow_to_be_executed.execute(input=input) else: workflow_to_be_executed.execute() return {"status": "success"} else: return {"status": "error: workflow could not be found"} return {"status": "warning: no trigger found valid for data in"}
def test_to_from_xml(self): filters = [Filter(action='test_filter_action'), Filter()] args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } flags = [ Flag(), Flag(parent_name='test_parent'), Flag(action='test_action'), Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b']), Flag(action='test_action', filters=filters), Flag(action='test_action', args=args), Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b'], filters=filters, args=args) ] for flag in flags: original_flag = flag.as_json() derived_flag = Flag(xml=flag.to_xml()).as_json() self.assertDictEqual(derived_flag, original_flag)
def test_convert_to_unspecified_type(self): unknown_types = ['list', 'dict', 'float', 'tuple', 'set', 'junk'] attempts = (6, 'a', None, 4.56, {6: "this_string"}, ['a', 'b']) for input_type in unknown_types: for attempt in attempts: self.assertEqual( Argument.convert(value=attempt, conversion_type=input_type), attempt)
def test_call_invalid_flag(self): flag = Flag(action='junkName', args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['func_name2']['args'] }) self.assertIsNone(flag()) self.assertIsNone(flag(output=6))
def __execute_trigger(conditional, data_in): flag_args = { arg['key']: Argument(key=arg['key'], value=arg['value'], format=arg.get('format', 'str')) for arg in conditional['args'] } filters = [ Filter(action=filter_element['action'], args={ arg['key']: Argument(key=arg['key'], value=arg['value'], format=arg.get('format', 'str')) for arg in filter_element['args'] }) for filter_element in conditional['filters'] ] return Flag(action=conditional['flag'], args=flag_args, filters=filters)(data_in)
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_get_next_step_with_errors(self): flags1 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }) ] flags2 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }), Flag( action='regMatch', args={'regex': Argument(key='regex', value='a', format='str')}) ] next_step1 = NextStep(name='name1', flags=flags1) next_step2 = NextStep(name='name2', flags=flags2) step1 = Step(next_steps=[next_step2, next_step1], errors=[]) step1.output = 'aaaa' self.assertEqual(step1.get_next_step(), next_step2.name) self.assertEqual(step1.next_up, next_step2.name) step1.output = 'aaa' self.assertIsNone(step1.get_next_step(error=True)) self.assertEqual(step1.next_up, next_step2.name) step2 = Step(next_steps=[next_step1], errors=[next_step2]) step2.output = 'bbbb' self.assertEqual(step2.get_next_step(), next_step1.name) step2.output = 'aaa' self.assertEqual(step2.get_next_step(error=True), next_step2.name) self.assertEqual(step2.next_up, next_step2.name) step3 = Step(next_steps=[], errors=[next_step2, next_step1]) self.assertIsNone(step3.get_next_step()) step3.output = 'bbbbb' self.assertEqual(step3.get_next_step(error=True), next_step1.name) self.assertEqual(step3.next_up, next_step1.name) step3.output = 'aaa' self.assertEqual(step3.get_next_step(error=True), next_step2.name) self.assertEqual(step3.next_up, next_step2.name)
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_init(self): flag = Flag() self.__compare_init(flag, '', '', ['', ''], [], {}) flag = Flag(parent_name='test_parent') self.__compare_init(flag, '', 'test_parent', ['test_parent', ''], [], {}) flag = Flag(action='test_action') self.__compare_init(flag, 'test_action', '', ['', 'test_action'], [], {}) flag = Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b']) self.__compare_init(flag, 'test_action', 'test_parent', ['a', 'b', 'test_action'], [], {}) filters = [Filter(action='test_filter_action'), Filter()] flag = Flag(action='test_action', filters=filters) self.__compare_init(flag, 'test_action', '', ['', 'test_action'], filters, {}) args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } expected_arg_json = { arg_name: arg_value.as_json() for arg_name, arg_value in args.items() } flag = Flag(action='test_action', args=args) self.__compare_init(flag, 'test_action', '', ['', 'test_action'], [], expected_arg_json) flag = Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b'], filters=filters, args=args) self.__compare_init(flag, 'test_action', 'test_parent', ['a', 'b', 'test_action'], filters, expected_arg_json)
def test_set_non_existent(self): filters = [Filter(action='test_filter_action'), Filter()] args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } flag = Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b'], filters=filters, args=args) flag.set('junkfield', 'junk') self.assertEqual(flag.junkfield, 'junk')
def test_validate(self): for action, args in self.test_funcs.items(): for arg_pair in args['args']: arg = Argument(key=arg_pair['name'], format=arg_pair['type']) self.assertTrue(arg.validate(action=action)) test_funcs = {'func_name2': {'args': []}, 'func_name3': {'args': [{'name': 'junk_name1', 'type': 'junk_type1'}, {'name': 'junk_name2', 'type': 'junk_type2'}]}} for action, args in test_funcs.items(): for arg_pair in args['args']: arg = Argument(key=arg_pair['name'], format=arg_pair['type']) self.assertFalse(arg.validate(action=action))
def test_set(self): filters = [Filter(action='test_filter_action'), Filter()] args = {'arg1': 'a', 'arg2': 3, 'arg3': u'abc'} args = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in args.items() } flag = Flag(parent_name='test_parent', action='test_action', ancestry=['a', 'b'], filters=filters, args=args) flag.set('action', 'renamed_action') self.assertEqual(flag.action, 'renamed_action') flag.set('filters', []) self.assertListEqual(flag.filters, []) flag.set('args', {}) self.assertDictEqual(flag.args, {})
def test_validate_args(self): actions = ['func_name1', 'func_name2', 'func_name3', 'invalid_name'] for action in actions: for arg_action, args in self.test_funcs['flags'].items(): flag = Flag( action=action, args={ arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in self.test_funcs['flags'][arg_action]['args'] }) if action == 'invalid_name': self.assertFalse(flag.validate_args()) elif action == arg_action or not self.test_funcs['flags'][ action]['args']: self.assertTrue(flag.validate_args()) else: self.assertFalse(flag.validate_args())
def test_validate_input(self): apps = ['app1', 'app2', 'app3', 'invalid_app'] actions = ['func_name1', 'func_name2', 'func_name3', 'invalid_action'] for app in apps: for action in actions: for arg_action, args in self.test_funcs.items(): test_args = { arg['name']: Argument(key=arg['name'], format=arg['type']) for arg in args['args'] } step = Step(app=app, action=action, inputs=test_args) if app == 'invalid_app' or action == 'invalid_action': self.assertFalse(step.validate_input()) elif action == arg_action or not self.test_funcs[action][ 'args']: self.assertTrue(step.validate_input()) else: self.assertFalse(step.validate_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_call(self): flags1 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }) ] flags2 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }), Flag( action='regMatch', args={'regex': Argument(key='regex', value='a', format='str')}) ] flags3 = [ Flag(action='invalidName', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }) ] flags4 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }), Flag(action='invalidName', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }) ] inputs = [('name1', [], 'aaaa', True), ('name2', flags1, 'anyString', True), ('name3', flags2, 'anyString', True), ('name4', flags2, 'bbbb', False), ('name5', flags3, 'anyString', False), ('name6', flags4, 'anyString', False)] for name, flags, input_str, expect_name in inputs: next_step = NextStep(name=name, flags=flags) if expect_name: expected_name = next_step.name self.assertEqual(next_step(input_str), expected_name) else: self.assertIsNone(next_step(input_str))
class JobInspectCLI: ''' This module will submit job to the queue system ''' # mutually exclusive group option is for possible extension mutually_exclusive_groups_name = [] mutually_exclusive_groups = {} args = { Argument(name=('-s', '--status'), help='STATUS of the SUBMITTED JOBS to the QUEUE SYSTEM.', action='store_true', default=True), # Argument( # name=('-c', '--cancel'), # help='Cancel job/jobs', # action='store_true', # default=True # ), } def __init__(self, *, subparsers): self.parser = subparsers.add_parser( 'inspect', help= 'INSPECT JOBS submitted to the QUEUE SYSTEM of the CLUSTER where the SCRIPT is being RUN' ) self.__add_mutually_exclusive_groups() self.__add_arguments() def __add_arguments(self): for arg in self.args: if arg.action in ['store_true', 'store_false']: self.parser.add_argument( *arg.name, help=arg.help, action=arg.action, required=arg.required, default=arg.default, ) else: self.parser.add_argument( *arg.name, help=arg.help, choices=arg.choices, action=arg.action, type=arg.type, nargs=arg.nargs, required=arg.required, metavar=arg.metavar, default=arg.default, ) def __add_mutually_exclusive_groups(self): for group_name in self.mutually_exclusive_groups_name: self.mutually_exclusive_groups[ group_name] = self.parser.add_mutually_exclusive_group() print('Group : {0} has been added to {1}'.format( group_name, self.__class__.__name__))
class JobSubmitCLI: ''' This module will submit job to the queue system ''' # mutually exclusive group option is for possible extension mutually_exclusive_groups_name = [] mutually_exclusive_groups = {} args = { Argument( name=('-d', '--directories'), help= 'DIRECTORIES where to look for job script to submit to the QUEUE system. ' + 'DIRECTORIES can be provided using ABSOLUTE PATH or RELATIVE to the CURRENT WORKING DIRECTORY. ' + f'(default : [\'{settings.DEFAULT_WORKDIR}\',] ).', nargs='+', default=[ settings.DEFAULT_WORKDIR, ]), Argument( name=('-r', '--recursive'), help= f'RECURSIVE looking for JOB SCRIPT within the DIRECTORY named as the CLUSTER name ' + ' starting at provided value by option -d/--directories.', action='store_true', default=False), Argument( name=('-s', '--suffix'), help= f'SUFFIX to be considered for job script. ( default : {settings.DEFAULT_SCRIPT_SUFFIX} ).', default=settings.DEFAULT_SCRIPT_SUFFIX), Argument( name=('-o', '--override'), help= 'Providing this flag means, if script folder contain any file or folder except job script, \ will be removed. If not provided, will ask for permission at program runtime before running each job script.', action='store_true', default=False), } def __init__(self, *, subparsers): self.parser = subparsers.add_parser( 'submit', help= 'SUBMIT JOB SCRIPT to the QUEUE SYSTEM of the CLUSTER where the SCRIPT is being RUN.' ) self.__add_mutually_exclusive_groups() self.__add_arguments() def __add_arguments(self): for arg in self.args: if arg.action in ['store_true', 'store_false']: self.parser.add_argument( *arg.name, help=arg.help, action=arg.action, required=arg.required, default=arg.default, ) else: self.parser.add_argument( *arg.name, help=arg.help, choices=arg.choices, action=arg.action, type=arg.type, nargs=arg.nargs, required=arg.required, metavar=arg.metavar, default=arg.default, ) def __add_mutually_exclusive_groups(self): for group_name in self.mutually_exclusive_groups_name: self.mutually_exclusive_groups[ group_name] = self.parser.add_mutually_exclusive_group() print('Group : {0} has been added to {1}'.format( group_name, self.__class__.__name__))
class SessionCLI: sub_commands = [ Command(name='open', help='open a saved session', description='This module will open a saved session', args=[ Argument(name=( '-n', '--name', ), type=str, help='Later', required=True), Argument(name=( '-l', '--links', ), type=str, help='Saves links', action='store_true', group='apps'), Argument(name=( '-s', '--shells', ), type=str, help='Saves all opening shell process', action='store_true', group='apps'), ], mutually_exclusive_group={'apps': None}), Command(name='view', help='view a saved session', description= 'This module display saved session using the session name', args=[ Argument( name=( '-n', '--name', ), type=str, help='Display all records for this session name.', group='view_option', ), Argument( name=('--sessions', ), help='Display sessions from the database.', action='store_true', group='view_option', ), Argument(name=( '-l', '--links', ), help='Consider links table', action='store_true', group='apps'), Argument(name=( '-s', '--shells', ), help='Consider shells table', action='store_true', group='apps'), Argument(name=( '-a', '--all', ), help='Consider all tables', action='store_true', group='apps'), ], mutually_exclusive_group={ 'apps': None, 'view_option': None }), Command(name='edit', help='edit a saved session', description='This module helps edit a saved session', args=[ Argument(name=( '-n', '--name', ), type=str, help='Name of the Session to edit', required=True), Argument(name=( '-l', '--links', ), help='Choose links list to be Edited.', action='store_true', group='apps'), Argument(name=( '-s', '--shells', ), help='Choose Shell list to be Edited.', action='store_true', group='apps'), Argument(name=( '-a', '--add', ), help='Add an entry', action='store_true', group='edit_option'), Argument(name=( '-d', '--delete', ), help='Delete an entry', action='store_true', group='edit_option'), Argument(name=('--delete-all', ), help='Delete all', action='store_true', group='edit_option'), ], mutually_exclusive_group={ 'edit_option': None, 'apps': None, }), Command(name='save', help='saves current session', description='This module will save the current session', args=[ Argument(name=('-n', '--name'), type=str, help='Later', required=True), Argument(name=( '-l', '--links', ), type=str, help='Saves links', action='store_true', group='save_option'), Argument(name=( '-s', '--shell', ), type=str, help='Saves all opening shell process', action='store_true', group='save_option'), Argument(name=( '-a', '--all', ), type=str, help='Saves all opening process', action='store_true', group='save_option'), ], mutually_exclusive_group={'save_option': None}), ] def __init__(self, *, sub_parsers, name='session'): self.parser = sub_parsers.add_parser( name, description='This is the description for the {0} command'.format( name), help='{0} module'.format(name), ) self.sub_parsers = self.parser.add_subparsers( title='sub-commands for {0} module'.format(name), description='Specify what to do with {0} module'.format(name), help='open/view/edit/save') self.add_subparsers() def add_subparsers(self): for command in self.sub_commands: parser = self.sub_parsers.add_parser( command.name, description=command.description, help=command.help) # adding mutually exclusive group if command.mutually_exclusive_group: command.add_mutually_exclusive_groups(parser) for arg in command.args: if arg.group and arg.group in command.mutually_exclusive_group: parser = command.mutually_exclusive_group[arg.group] if arg.action in ['store_true', 'store_false']: parser.add_argument(*arg.name, help=arg.help, action=arg.action) else: parser.add_argument(*arg.name, help=arg.help, type=arg.type, choices=arg.choices, action=arg.action, required=arg.required)
def test_init(self): step = Step() self.__compare_init(step, '', '', '', '', '', {}, [], [], ['', ''], []) step = Step(name='name') self.__compare_init(step, 'name', '', '', '', '', {}, [], [], ['', 'name'], []) step = Step(name='name', parent_name='parent_name') self.__compare_init(step, 'name', 'parent_name', '', '', '', {}, [], [], ['parent_name', 'name'], []) step = Step(name='name', parent_name='parent_name', action='action') self.__compare_init(step, 'name', 'parent_name', 'action', '', '', {}, [], [], ['parent_name', 'name'], []) step = Step(name='name', parent_name='parent_name', action='action', app='app') self.__compare_init(step, 'name', 'parent_name', 'action', 'app', '', {}, [], [], ['parent_name', 'name'], []) step = Step(name='name', parent_name='parent_name', action='action', app='app', risk=100) self.__compare_init(step, 'name', 'parent_name', 'action', 'app', '', {}, [], [], ['parent_name', 'name'], [], risk=100) step = Step(name='name', parent_name='parent_name', action='action', app='app', risk=-100) self.__compare_init(step, 'name', 'parent_name', 'action', 'app', '', {}, [], [], ['parent_name', 'name'], [], risk=-100) widgets = [('aaa', 'bbb'), ('ccc', 'ddd'), ('eee', 'fff')] step = Step(name='name', parent_name='parent_name', action='action', app='app', widgets=widgets) self.__compare_init(step, 'name', 'parent_name', 'action', 'app', '', {}, [], [], ['parent_name', 'name'], widgets) step = Step(name='name', parent_name='parent_name', action='action', app='app', device='device') self.__compare_init(step, 'name', 'parent_name', 'action', 'app', 'device', {}, [], [], ['parent_name', 'name'], []) inputs = {'in1': 'a', 'in2': 3, 'in3': u'abc'} inputs = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in inputs.items() } step = Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs) self.__compare_init( step, 'name', 'parent_name', 'action', 'app', 'device', {key: input_.as_json() for key, input_ in inputs.items()}, [], [], ['parent_name', 'name'], []) flags = [Flag(), Flag(action='action')] nexts = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=flags, ancestry=['a', 'b']) ] step = Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=nexts) self.__compare_init( step, 'name', 'parent_name', 'action', 'app', 'device', {key: input_.as_json() for key, input_ in inputs.items()}, [next_.as_json() for next_ in nexts], [], ['parent_name', 'name'], []) error_flags = [Flag(), Flag(action='action'), Flag()] errors = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=error_flags, ancestry=['a', 'b']) ] step = Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=nexts, errors=errors) self.__compare_init( step, 'name', 'parent_name', 'action', 'app', 'device', {key: input_.as_json() for key, input_ in inputs.items()}, [next_.as_json() for next_ in nexts], [error.as_json() for error in errors], ['parent_name', 'name'], [])
def test_get_next_step(self): flags1 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }) ] flags2 = [ Flag(action='regMatch', args={ 'regex': Argument(key='regex', value='(.*)', format='str') }), Flag( action='regMatch', args={'regex': Argument(key='regex', value='a', format='str')}) ] next_step1 = NextStep(name='name1', flags=[]) next_step2 = NextStep(name='name2', flags=flags1) next_step3 = NextStep(name='name3', flags=flags2) step1 = Step(next_steps=[next_step1]) self.assertEqual(step1.get_next_step(), next_step1.name) self.assertEqual(step1.next_up, next_step1.name) step1.output = 'aaaa' self.assertEqual(step1.get_next_step(), next_step1.name) self.assertEqual(step1.next_up, next_step1.name) step2 = Step(next_steps=[next_step2]) step2.output = 'aaaa' self.assertEqual(step2.get_next_step(), next_step2.name) self.assertEqual(step2.next_up, next_step2.name) step3 = Step(next_steps=[next_step3]) step3.output = 'aaaa' self.assertEqual(step3.get_next_step(), next_step3.name) self.assertEqual(step3.next_up, next_step3.name) step3.output = None self.assertIsNone(step3.get_next_step()) self.assertEqual(step3.next_up, next_step3.name) step4 = Step(next_steps=[next_step1, next_step2]) step4.output = 'aaaa' self.assertEqual(step4.get_next_step(), next_step1.name) self.assertEqual(step4.next_up, next_step1.name) step4.output = None self.assertEqual(step4.get_next_step(), next_step1.name) self.assertEqual(step4.next_up, next_step1.name) step4 = Step(next_steps=[next_step2, next_step1]) step4.output = 6 self.assertEqual(step4.get_next_step(), next_step2.name) self.assertEqual(step4.next_up, next_step2.name) step5 = Step(next_steps=[next_step3, next_step2]) step5.output = 6 self.assertEqual(step5.get_next_step(), next_step2.name) self.assertEqual(step5.next_up, next_step2.name) step5.output = 'aaa' self.assertEqual(step5.get_next_step(), next_step3.name) self.assertEqual(step5.next_up, next_step3.name)
def execute(data_in, input_in, trigger_name=None, tags=None): """Tries to match the data_in against the conditionals of all the triggers registered in the database. Args: data_in (str): Data to be used to match against the conditionals input_in (str): The input to the first step of the workflow trigger_name (str): The name of the specific trigger to execute tags (array): A list of tags to find the specific triggers to execute Returns: Dictionary of {"status": <status string>} """ triggers = set() if trigger_name: t = Triggers.query.filter_by(name=trigger_name).first() if t: triggers.add(t) if tags: for tag in tags: if len(Triggers.query.filter_by(tag=tag).all()) > 1: for t in Triggers.query.filter_by(tag=tag): triggers.add(t) elif len(Triggers.query.filter_by(tag=tag).all()) == 1: triggers.add(Triggers.query.filter_by(tag=tag).first()) if not (trigger_name or tags): triggers = Triggers.query.all() returned_json = dict() returned_json["executed"] = [] returned_json["errors"] = [] from server.flaskserver import running_context for trigger in triggers: conditionals = json.loads(trigger.condition) if all( Triggers.__execute_trigger(conditional, data_in) for conditional in conditionals): workflow_to_be_executed = running_context.controller.get_workflow( trigger.playbook, trigger.workflow) if workflow_to_be_executed: if input_in: input_args = { arg['key']: Argument(key=arg['key'], value=arg['value'], format=arg.get('format', 'str')) for arg in input_in } workflow_to_be_executed.execute(start_input=input_args) logger.info( 'Workflow {0} executed with input {1}'.format( workflow_to_be_executed.name, input_args)) else: workflow_to_be_executed.execute() logger.info( 'Workflow {0} executed with no input'.format( workflow_to_be_executed.name)) returned_json["executed"].append(trigger.name) else: logger.error( 'Workflow associated with trigger is not in controller' ) returned_json["errors"].append( {trigger.name: "Workflow could not be found."}) if not (returned_json["executed"] or returned_json["errors"]): logging.debug('No trigger matches data input') return returned_json
def test_to_from_xml(self): inputs = {'in1': 'a', 'in2': 3, 'in3': u'abc'} inputs = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in inputs.items() } flags = [Flag(), Flag(action='action')] next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=flags, ancestry=['a', 'b']) ] error_flags = [Flag(), Flag(action='action'), Flag()] errors = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=error_flags, ancestry=['a', 'b']) ] widgets = [('aaa', 'bbb'), ('ccc', 'ddd'), ('eee', 'fff')] steps = [ Step(), Step(name='name'), Step(name='name', parent_name='parent_name'), Step(name='name', parent_name='parent_name', action='action'), Step(name='name', parent_name='parent_name', action='action', app='app'), Step(name='name', parent_name='parent_name', action='action', app='app', device='device'), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }, risk=10), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }, risk=-10), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, widgets=widgets) ] for step in steps: original_json = step.as_json() new_step = Step(xml=step.to_xml()) new_json = new_step.as_json() self.assertDictEqual(new_json, original_json)