def test_split_subscriptions_no_controller(self): self.assertTupleEqual( split_subscriptions([ Subscription(1, ['a', 'b']), Subscription(2, ['b', 'c', 'd', 'e']) ]), ([ Subscription(1, ['a', 'b']), Subscription(2, ['b', 'c', 'd', 'e']) ], None))
def test_update_case_erases_old_subs(self): self.subs.add_subscriptions( 1, [Subscription(self.ids[0], ['e1', 'e2', 'e3'])]) self.subs.add_subscriptions( 2, [Subscription(self.ids[0], ['e1', 'e2', 'e4'])]) self.subs.update_subscriptions( 1, [Subscription(self.ids[0], ['e1', 'e2'])]) self.assert_events_have_cases(self.ids[0], ['e1', 'e2'], {1, 2}) self.assert_events_have_cases(self.ids[0], ['e4'], {2}) self.assertNotIn('e3', self.subs._subscriptions[self.ids[0]])
def test_convert_subscriptions(self): self.assertEqual( convert_subscriptions([{ 'id': 1, 'events': ['a', 'b'] }, { 'id': 2, 'events': ['b', 'c', 'd', 'e'] }]), [ Subscription(1, ['a', 'b']), Subscription(2, ['b', 'c', 'd', 'e']) ])
def test_pause_and_resume_workflow(self): execution_id = None result = {status: False for status in ('paused', 'resumed', 'called')} workflow = execution_db_help.load_workflow('pauseResumeWorkflowFixed', 'pauseResumeWorkflow') workflow_id = workflow.id case = Case(name='name') self.app.running_context.case_db.session.add(case) self.app.running_context.case_db.session.commit() subscriptions = [Subscription( id=str(workflow_id), events=[WalkoffEvent.WorkflowPaused.signal_name])] self.app.running_context.executor.create_case(case.id, subscriptions) self.app.running_context.case_logger.add_subscriptions(case.id, [ Subscription(str(workflow_id), [WalkoffEvent.WorkflowResumed.signal_name])]) def pause_resume_thread(): self.app.running_context.executor.pause_workflow(execution_id) return @WalkoffEvent.WorkflowPaused.connect def workflow_paused_listener(sender, **kwargs): result['paused'] = True wf_status = self.app.running_context.execution_db.session.query(WorkflowStatus).filter_by( execution_id=sender['execution_id']).first() wf_status.paused() self.app.running_context.execution_db.session.commit() self.app.running_context.executor.resume_workflow(execution_id) @WalkoffEvent.WorkflowResumed.connect def workflow_resumed_listener(sender, **kwargs): result['resumed'] = True @WalkoffEvent.WorkflowExecutionStart.connect def workflow_started_listener(sender, **kwargs): self.assertEqual(sender['id'], str(workflow_id)) result['called'] = True execution_id = self.app.running_context.executor.execute_workflow(workflow_id) while True: self.app.running_context.execution_db.session.expire_all() workflow_status = self.app.running_context.execution_db.session.query(WorkflowStatus).filter_by( execution_id=execution_id).first() if workflow_status and workflow_status.status == WorkflowStatusEnum.running: threading.Thread(target=pause_resume_thread).start() time.sleep(0) break self.app.running_context.executor.wait_and_reset(1) for status in ('called', 'paused', 'resumed'): self.assertTrue(result[status])
def _format_case_message_data(message): if message.type == CaseControl.CREATE: return CaseCommunicationMessageData( CaseCommunicationMessageType.create, message.id, [Subscription(sub.id, sub.events) for sub in message.subscriptions]) elif message.type == CaseControl.UPDATE: return CaseCommunicationMessageData( CaseCommunicationMessageType.update, message.id, [Subscription(sub.id, sub.events) for sub in message.subscriptions]) elif message.type == CaseControl.DELETE: return CaseCommunicationMessageData(CaseCommunicationMessageType.delete, message.id, None)
def test_import_cases(self): with patch.object(current_app.running_context.executor, 'create_case') as mock_create: subscription = {'id': 'id1', 'events': ['a', 'b', 'c']} data = { 'name': 'case1', 'note': 'Test', 'subscriptions': [subscription] } path = os.path.join(walkoff.config.Config.APPS_PATH, 'case.json') with open(path, 'w') as f: f.write(json.dumps(data, indent=4, sort_keys=True)) files = {'file': (path, open(path, 'r'), 'application/json')} case = self.post_with_status_check( '/api/cases', headers=self.headers, status_code=OBJECT_CREATED, data=files, content_type='multipart/form-data') case.pop('id', None) self.assertDictEqual(case, data) subscriptions = [Subscription('id1', ['a', 'b', 'c'])] mock_create.assert_called_once_with(1, subscriptions)
def test_split_subscriptions_with_controller(self): self.assertTupleEqual( split_subscriptions([ Subscription(1, ['a']), Subscription('controller', ['d']), Subscription(2, ['b', 'c']) ]), ([Subscription(1, ['a']), Subscription(2, ['b', 'c'])], Subscription('controller', ['d'])))
def send_all_cases_to_workers(): from walkoff.serverdb.casesubscription import CaseSubscription from walkoff.case.database import Case from walkoff.case.subscription import Subscription current_app.logger.info('Sending existing cases to workers') for case_subscription in CaseSubscription.query.all(): subscriptions = [ Subscription(sub['id'], sub['events']) for sub in case_subscription.subscriptions ] case = current_app.running_context.case_db.session.query(Case).filter( Case.name == case_subscription.name).first() if case is not None: current_app.running_context.executor.update_case( case.id, subscriptions)
def setUp(self): self.subs = SubscriptionCache() self.ids = [uuid4() for _ in range(4)] self.case1 = [ Subscription(self.ids[0], ['e1', 'e2', 'e3']), Subscription(self.ids[1], ['e1']) ] self.case2 = [Subscription(self.ids[0], ['e2', 'e3'])] self.case3 = [ Subscription(self.ids[2], ['e', 'b', 'c']), Subscription(self.ids[3], ['d']) ] self.case4 = [Subscription(self.ids[0], ['a', 'b'])]
def test_send_cases_to_workers(self): with patch.object(current_app.running_context.executor, 'update_case') as mock_update: from walkoff.case.database import Case from walkoff.serverdb.casesubscription import CaseSubscription from walkoff.extensions import db from walkoff.server.blueprints.root import send_all_cases_to_workers ids = [str(uuid4()) for _ in range(4)] case1_subs = [{ 'id': ids[0], 'events': ['e1', 'e2', 'e3'] }, { 'id': ids[1], 'events': ['e1'] }] case2_subs = [{'id': ids[0], 'events': ['e2', 'e3']}] case3_subs = [{ 'id': ids[2], 'events': ['e', 'b', 'c'] }, { 'id': ids[3], 'events': ['d'] }] case4_subs = [{'id': ids[0], 'events': ['a', 'b']}] expected = [] for i, case_subs in enumerate( (case1_subs, case2_subs, case3_subs, case4_subs)): name = 'case{}'.format(i) new_case_subs = CaseSubscription(name, subscriptions=case_subs) db.session.add(new_case_subs) case = Case(name=name) current_app.running_context.case_db.session.add(case) current_app.running_context.case_db.session.commit() call_subs = [ Subscription(sub['id'], sub['events']) for sub in case_subs ] expected.append(call(case.id, call_subs)) current_app.running_context.case_db.session.commit() send_all_cases_to_workers() mock_update.assert_has_calls(expected)
'events': ['a', 'b', 'c'] }] }) cases = [ case.name for case in current_app.running_context.case_db.session.query( case_database.Case).all() ] expected_cases = ['case1'] orderless_list_compare(self, cases, expected_cases) cases_config = CaseSubscription.query.all() self.assertEqual(len(cases_config), 1) orderless_list_compare(self, [case.name for case in cases_config], ['case1']) mock_create.assert_called_once_with( 1, [Subscription(uid, ['a', 'b', 'c'])]) # @patch.object(current_app.running_context.executor, 'create_case') def test_create_case_with_subscriptions_with_controller(self): with patch.object(current_app.running_context.executor, 'create_case') as mock_create: uid = str(uuid4()) subscriptions = [{ 'id': uid, 'events': ['a', 'b', 'c'] }, { 'id': 'controller', 'events': ['a'] }] data = {
def setUpClass(cls): initialize_test_config() cls.subscriptions = [Subscription(str(uuid4()), ['a', 'b', 'c']), Subscription(str(uuid4()), ['b'])] cls.cache = MockRedisCacheAdapter() cls.controller = WorkflowExecutionController(cls.cache) setup_dbs()
def convert_subscriptions(subscriptions): return [ Subscription(subscription['id'], subscription['events']) for subscription in subscriptions ]
def test_update_subscriptions(self, mock_update_subs): subs = [Subscription('id', ['e1'])] self.logger.update_subscriptions('case1', subs) self.assert_mock_called_once_with(mock_update_subs, 'case1', subs)