From e5de3b1e630f736c24377231bfa5104945767a81 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Fri, 3 Nov 2023 16:00:38 +0100 Subject: [PATCH 1/2] tests: include more readable mqtt logs in Get Logs keyword Signed-off-by: Reuben Miller --- .../libraries/ThinEdgeIO/ThinEdgeIO.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py index 92813503d19..157e0d955e0 100644 --- a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py +++ b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py @@ -154,6 +154,12 @@ def get_logs( except Exception as ex: # pylint: disable=broad-except log.warning("Failed to get device managed object. %s", ex) + # Log mqtt messages separately so it is easier to read/debug + try: + self.log_mqtt_messages("#", date_from) + except Exception as ex: + log.warning("Failed to retrieve mqtt logs. %s", ex, exc_info=True) + try: # Get agent log files (if they exist) log.info("tedge agent logs: /var/log/tedge/agent/*") @@ -512,6 +518,18 @@ def _assert_mqtt_topic_messages( return messages + def log_mqtt_messages(self, topic: str = "#", date_from: Union[datetime, float] = None, **kwargs): + items = self.mqtt_match_messages( + topic=topic, + date_from=date_from, + **kwargs, + ) + entries = [ + f'{item["message"]["tst"].replace("+0000", ""):32} {item["message"]["topic"]:70} {bytes.fromhex(item["payload_hex"]).decode("utf8")}' + for item in items + ] + log.info("---- mqtt messages ----\n%s", "\n".join(entries)) + @keyword("Should Have MQTT Messages") def mqtt_should_have_topic( self, From 98b5c9180b31769e781b76a3b74e33a4a39dd744 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 6 Nov 2023 08:48:08 +0100 Subject: [PATCH 2/2] handle any non utf8 payloads just replace unknown utf8 characters to pre preserve the presence of a message Signed-off-by: Reuben Miller --- tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py index 157e0d955e0..b56f51ca804 100644 --- a/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py +++ b/tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py @@ -501,8 +501,7 @@ def _assert_mqtt_topic_messages( ) messages = [ - bytes.fromhex(item["payload_hex"]).decode("utf8") - # item["message"]["payload"] + bytes.fromhex(item["payload_hex"]).decode("utf8", errors="replace") for item in items ] @@ -525,7 +524,7 @@ def log_mqtt_messages(self, topic: str = "#", date_from: Union[datetime, float] **kwargs, ) entries = [ - f'{item["message"]["tst"].replace("+0000", ""):32} {item["message"]["topic"]:70} {bytes.fromhex(item["payload_hex"]).decode("utf8")}' + f'{item["message"]["tst"].replace("+0000", ""):32} {item["message"]["topic"]:70} {bytes.fromhex(item["payload_hex"]).decode("utf8", errors="replace")}' for item in items ] log.info("---- mqtt messages ----\n%s", "\n".join(entries))