def test_basic_submit(self): initialize_message_system() send_message('hello world', 'test') self.assertEquals(len(saq.MESSAGE_SYSTEM.systems), 1) start_message_system() system = saq.MESSAGE_SYSTEM.systems['test'] self.assertTrue(system.message_dispatched.wait(5)) self.assertEquals(len(system.messages_received), 1) stop_message_system() self.assertIsNone(saq.db.query(Message).first())
def test_multi_route_submit(self): # a single message type is routed to two different destinations on the same system saq.CONFIG['message_routing']['test'] = 'test:test_destination,test:test_destination_2' initialize_message_system() send_message('hello world', 'test') start_message_system() system = saq.MESSAGE_SYSTEM.systems['test'] wait_for(lambda: len(system.messages_received) == 2, 1, 3) self.assertEquals(len(system.messages_received), 2) stop_message_system() self.assertIsNone(saq.db.query(Message).first())
def test_multi_route_default_type(self): # a single message is routed to all available routes saq.CONFIG['messaging_system_test_2'] = {} saq.CONFIG['messaging_system_test_2']['enabled'] = 'yes' saq.CONFIG['messaging_system_test_2']['module'] = 'saq.messaging.test' saq.CONFIG['messaging_system_test_2']['class'] = 'TestMessageDispatchSystem' saq.CONFIG['messaging_system_test_2']['route'] = 'test_2' saq.CONFIG['message_routing']['test'] = 'test:test_destination,test_2:test_destination_2' initialize_message_system() send_message('hello world') start_message_system() self.assertTrue(saq.MESSAGE_SYSTEM.systems['test'].message_dispatched.wait(5)) self.assertTrue(saq.MESSAGE_SYSTEM.systems['test_2'].message_dispatched.wait(5)) stop_message_system() self.assertIsNone(saq.db.query(Message).first())
def execute(self, remediation): # execute this remediation try: remediation_result = self.execute_request(remediation) if remediation_result is None: raise RuntimeError( "forgot to return remediation object in execute_request") logging.info(f"completed remediation item {remediation}") if remediation_result.successful and self.message_on_success: try: send_message( f"remediation for {remediation_result.key} completed: {remediation_result.result}", MESSAGE_TYPE_REMEDIATION_SUCCESS) except Exception as e: logging.error(f"unable to send completed message: {e}") elif not remediation_result.successful and self.message_on_error: try: send_message( f":rotating_light: remediation for {remediation_result.key} failed:\n{remediation_result.result}", MESSAGE_TYPE_REMEDIATION_FAILURE) except Exception as e: logging.error(f"unable to send completed message: {e}") saq.db.execute(Remediation.__table__.update().values( status=REMEDIATION_STATUS_COMPLETED, successful=remediation_result.successful, result=remediation_result.result).where( Remediation.id == remediation_result.id)) saq.db.commit() except Exception as e: logging.error( f"unable to execute remediation item {remediation.id}: {e}") report_exception() try: saq.db.execute(Remediation.__table__.update().values( status=REMEDIATION_STATUS_COMPLETED, successful=False, result=str(e))\ .where(Remediation.id == remediation.id)) saq.db.commit() if self.message_on_error: send_message( f":rotating_light: attempt to execute remediation {remediation.key} failed:\n{e}", MESSAGE_TYPE_REMEDIATION_FAILURE) except Exception as e: logging.error( f"unable to record error for remediation item {remediation.id}: {e}" ) report_exception()