def get_sent_messages(rabbitmq_container): """Read a file in a container. Args: rabbitmq_container (conu.DockerContainer): The RabbitMQ container. Returns: list(Message): A list of Message instance that were sent over the broker. """ with tempfile.TemporaryDirectory() as tempdir: destfile = os.path.join(tempdir, "file") rabbitmq_container.copy_from("/var/log/fedora-messaging/messages.log", destfile) with open(destfile, "r") as fh: serialized_messages = fh.read().replace("\n", ",") serialized_messages = "[%s]" % serialized_messages[:-1] return message.loads(serialized_messages)
def test_proper_json(self): """Assert loading message from json work.""" message_json = ( '[{"topic": "test topic", "headers": {"fedora_messaging_schema": "base.message", ' '"fedora_messaging_severity": 30}, "id": "test id", "body": ' '{"test_key": "test_value"}, "queue": "test queue"}]') messages = message.loads(message_json) test_message = messages[0] self.assertEqual(len(messages), 1) self.assertIsInstance(test_message, message.Message) self.assertEqual("test topic", test_message.topic) self.assertEqual("test id", test_message.id) self.assertEqual({"test_key": "test_value"}, test_message.body) self.assertEqual("test queue", test_message.queue) self.assertEqual(message.WARNING, test_message._headers["fedora_messaging_severity"]) self.assertEqual("base.message", test_message._headers["fedora_messaging_schema"])
def test_missing_queue(self): """Assert message without queue is returned and warning is printed.""" message_json = ( '[{"topic": "test topic", "headers": {"fedora_messaging_schema": ' '"base.message", "fedora_messaging_severity": 30}, "id": "test id", ' '"body": {"test_key": "test_value"}}]') with mock.patch("fedora_messaging.message._log") as mock_log: messages = message.loads(message_json) mock_log.warning.assert_called_once_with( "Message saved without queue.") test_message = messages[0] self.assertEqual(len(messages), 1) self.assertEqual("test topic", test_message.topic) self.assertEqual("test id", test_message.id) self.assertEqual({"test_key": "test_value"}, test_message.body) self.assertEqual(None, test_message.queue) self.assertEqual(message.WARNING, test_message._headers["fedora_messaging_severity"]) self.assertEqual("base.message", test_message._headers["fedora_messaging_schema"])