def test_marathon_event(self): """Test that we can process at least one kind of event.""" payload = { "eventType": "status_update_event", "slaveId": "slave-01", "taskId": "task-01", "taskStatus": "TASK_RUNNING", "message": "Some message", "appId": "/foo/bar", "host": "host-01", "ipAddresses": [ { "ip_address": "127.0.0.1", "protocol": "tcp" }, { "ip_address": "127.0.0.1", "protocol": "udp" }, ], "ports": [0, 1], "version": "1234", "timestamp": 12345, } factory = EventFactory() event = factory.process(payload) expected_event = MarathonStatusUpdateEvent( event_type="status_update_event", timestamp=12345, slave_id="slave-01", task_id="task-01", task_status="TASK_RUNNING", message="Some message", app_id="/foo/bar", host="host-01", ports=[0, 1], version="1234", ) expected_event.ip_addresses = [ MarathonIpAddress(ip_address="127.0.0.1", protocol="tcp"), MarathonIpAddress(ip_address="127.0.0.1", protocol="udp"), ] self.assertEqual(event.to_json(), expected_event.to_json())
class TestEventSubscription(unittest.TestCase): """ This test will generate an event from Marathon's EventBus and then test the creation of a Marathon EventObject. It will spawn a separate server process for capturing Marathon Callbacks. """ def setUp(self): self.app = get_app() self.factory = EventFactory() self.client = MarathonClient(servers=[MARATHON_SERVER]) self.client.create_event_subscription(url=MARATHON_CALLBACK_URL) def test_event_factory(self): queue = Queue() # Use a Multiprocess Queue for communication with subprocess. receive = Process(target=listen_for_events, args=[queue]) receive.start() # Start the server. self.client.create_event_subscription(url='192.0.2.1') # Generate an event (subscribe_event). while True: o = self.factory.process(queue.get(timeout=120)) # Raise queueEmpty if the test exceeds 2 minutes. if isinstance(o, MarathonSubscribeEvent): print(o, dir(o), o.__dict__) self.assertEqual(o.event_type, 'subscribe_event') break receive.terminate() def tearDown(self): self.client.delete_event_subscription(url='192.0.2.1')
def test_marathon_event(self): """Test that we can process at least one kind of event.""" payload = { "eventType": "status_update_event", "slaveId": "slave-01", "taskId": "task-01", "taskStatus": "TASK_RUNNING", "message": "Some message", "appId": "/foo/bar", "host": "host-01", "ipAddresses": [ {"ip_address": "127.0.0.1", "protocol": "tcp"}, {"ip_address": "127.0.0.1", "protocol": "udp"}, ], "ports": [0, 1], "version": "1234", "timestamp": 12345, } factory = EventFactory() event = factory.process(payload) expected_event = MarathonStatusUpdateEvent( event_type="status_update_event", timestamp=12345, slave_id="slave-01", task_id="task-01", task_status="TASK_RUNNING", message="Some message", app_id="/foo/bar", host="host-01", ports=[0, 1], version="1234", ) expected_event.ip_addresses = [ MarathonIpAddress(ip_address="127.0.0.1", protocol="tcp"), MarathonIpAddress(ip_address="127.0.0.1", protocol="udp"), ] self.assertEqual(event.to_json(), expected_event.to_json())
async def _stream_events(self, events_tx, event_types): ef = EventFactory() async with self._client() as client: headers = {"Accept": "text/event-stream"} params = {} if event_types: params["event_type"] = event_types async with client.stream( "GET", "/v2/events", headers=headers, params=params, timeout=60 ) as response: async for line in response.aiter_lines(): _data = line.split(":", 1) if _data[0] == "data": event_data = json.loads(_data[1].strip()) if "eventType" not in event_data: raise MarathonError("Invalid event data received.") event = ef.process(event_data) if event_types and event.event_type not in event_types: # We're filtering events, but got an unwanted one # anyway. Ignore it and move on. continue self.log.info(f"Received marathon event: {event}") await events_tx.send(event)
def setUp(self): self.app = get_app() self.factory = EventFactory() self.client = MarathonClient(servers=[MARATHON_SERVER]) self.client.create_event_subscription(url=MARATHON_CALLBACK_URL)