async def receive(request): Log.debug(f"Received GET request for component " \ f"{request.rel_url.query['component']}") try: component = request.rel_url.query['component'] EventMessage.subscribe(component=component) alert = EventMessage.receive() except EventMessageError as e: status_code = e.rc error_message = e.desc Log.error(f"Unable to receive event message for component: " \ f"{component}, status code: {status_code}," \ f" error: {error_message}") response_obj = {'error_code': status_code, 'exception': \ ['EventMessageError', {'message': error_message}]} except Exception as e: exception_key = type(e).__name__ exception = RestServerError(exception_key).http_error() status_code = exception[0] error_message = exception[1] Log.error(f"Internal error while receiving event messages for " \ f"component: {component}, status code: " \ f"{status_code}, error: {error_message}") response_obj = {'error_code': status_code, 'exception': \ [exception_key, {'message': error_message}]} raise EventMessageError(status_code, error_message) from e else: status_code = 200 # No exception, Success response_obj = {'alert': alert} Log.debug(f"GET method finished with status code: {status_code}" \ f"for component {component} and received event message " \ f"alert info. - {alert['iem']['info']}.") finally: return web.Response(text=json.dumps(response_obj), \ status=status_code)
def init(args): EventMessage.init(component='sspl', source='S') # Check existing iem alert present in MessageBus EventMessage.subscribe(component='sspl') while True: msg = EventMessage.receive() if msg is None: break
def receive(self, component): """Receive the incoming message.""" EventMessage.subscribe(component) try: alert = EventMessage.receive() self._logger.DEBUG("Received message --> ", alert) except Exception as exception: self._logger.ERROR("Exception : ", exception) return False return True
def test_bulk_verify_receive(self): """ Test bulk receive alerts """ EventMessage.init(component='cmp', source='H', receiver=True) count = 0 while True: alert = EventMessage.receive() if alert is None: break self.assertIs(type(alert), dict) count += 1 self.assertEqual(count, 1000)
def test_validate_without_optional_params(self): """ Validate without optional params of send attributes """ EventMessage.send(module='mod', event_id='500', severity='B', \ message_blob={'input': 'This is message'}) alert = EventMessage.receive() self.assertEqual(alert['iem']['location']['site_id'], \ alert['iem']['source']['site_id']) self.assertEqual(alert['iem']['location']['node_id'], \ alert['iem']['source']['node_id']) self.assertEqual(alert['iem']['location']['rack_id'], \ alert['iem']['source']['rack_id'])
def test_bulk_verify_receive(self): """ Test bulk receive alerts """ EventMessage.subscribe(component='cmp', \ message_server_endpoints=TestMessage._message_server_endpoints) count = 0 while True: alert = EventMessage.receive() if alert is None: break self.assertIs(type(alert), dict) if 'test_bulk_message' in alert['iem']['contents']['message']: count += 1 self.assertEqual(count, 1000)
def test_iem_alerts(self): """Test iem 'ipmitool' fault alert receive.""" check_sspl_ll_is_running() Iem().iem_fault("IPMITOOL_ERROR") time.sleep(10) EventMessage.subscribe(component='sspl') fault_alert = EventMessage.receive() print(f"IEM Received:{fault_alert}") assert (fault_alert is not None) assert (fault_alert["iem"]["info"]["severity"] is not None) assert (fault_alert["iem"]["info"]["type"] is not None) assert (fault_alert["iem"]["info"]["event_time"] is not None) assert (fault_alert["iem"]["source"]["module"] is not None) assert (fault_alert["iem"]["contents"]["event"] is not None)
def test_receive_without_send(self): """ Receive message without send """ EventMessage.subscribe(component='cmp', \ message_server_endpoints=TestMessage._message_server_endpoints) alert = EventMessage.receive() self.assertIsNone(alert)
def test_alert_fail_receive(self): """ Receive message without subscribing """ with self.assertRaises(EventMessageError): EventMessage.receive()
def test_alert_verify_receive(self): """ Test receive alerts """ EventMessage.subscribe(component='cmp', \ message_server_endpoints=TestMessage._message_server_endpoints) alert = EventMessage.receive() self.assertIs(type(alert), dict)
def test_json_verify_receive(self): """ Test receive json as message description """ EventMessage.init(component='cmp', source='H', receiver=True) alert = EventMessage.receive() self.assertIs(type(alert), dict)
def test_receive_without_send(self): """ Receive message without send """ EventMessage.init(component='cmp', source='H', receiver=True) alert = EventMessage.receive() self.assertIsNone(alert)
def test_receive_fail(self): """ Receive message with receiver as False """ EventMessage.init(component='cmp', source='H') with self.assertRaises(KeyError): EventMessage.receive()
def test_alert_verify_receive(self): """ Test receive alerts """ EventMessage.init(component='cmp', source='H', receiver=True) alert = EventMessage.receive() self.assertIs(type(alert), dict)
def test_receive_without_send(self): """ Receive message without send """ EventMessage.subscribe(component='cmp') alert = EventMessage.receive() self.assertIsNone(alert)
def test_alert_verify_receive(self): """ Test receive alerts """ EventMessage.subscribe(component='cmp') alert = EventMessage.receive() self.assertIs(type(alert), dict)
def test_json_verify_receive(self): """ Test receive json as message description """ EventMessage.subscribe(component='cmp') alert = EventMessage.receive() self.assertIs(type(alert), dict)