From 2f7a571759808002a419447731411f88a19da855 Mon Sep 17 00:00:00 2001 From: Frederic Martinsons Date: Tue, 6 Jun 2023 07:21:56 +0200 Subject: [PATCH 1/2] tools/tests: add ability to log to a file That has been proven useful for debugging the tool during tests Signed-off-by: Frederic Martinsons --- tools/test-networkmanager-service.py | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/test-networkmanager-service.py b/tools/test-networkmanager-service.py index 9002f26b36..528422d15a 100755 --- a/tools/test-networkmanager-service.py +++ b/tools/test-networkmanager-service.py @@ -288,8 +288,15 @@ class Util: return (Util.ip_addr_ntop(a, family), family) @staticmethod - def eprint(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) + def log(message): + if gl.log_file: + try: + gl.log_file.write(message + "\n") + gl.log_file.flush() + except Exception: + pass + else: + print(message, file=sys.stderr) @staticmethod def variant_from_dbus(val): @@ -2884,6 +2891,26 @@ class ObjectManager(dbus.service.Object): return managed_objects +def setup_log_file(): + """ + Get environment variable for the log file , if any + We accept a %p placeholder to replace with the pid of the current + process + """ + try: + log_file_name = os.environ["NM_TEST_NETWORKMANAGER_SERVICE_LOGFILE"] + except KeyError: + return None + if "%p" in log_file_name: + log_file_name = log_file_name.replace("%p", str(os.getpid())) + try: + log_file = open(log_file_name, "w") + except Exception: + log_file = None + + return log_file + + ############################################################################### @@ -2895,6 +2922,7 @@ def main(): global gl gl = Global() + gl.log_file = setup_log_file() gl.mainloop = GLib.MainLoop() gl.bus = dbus.SessionBus() gl.force_activation_failure = {} @@ -2927,7 +2955,8 @@ def main(): gl.settings.unexport() gl.manager.unexport() gl.object_manager.remove_from_connection() - + if gl.log_file: + gl.log_file.close() sys.exit(0) From ae408fe4ab8761c90d61feaa0de0eeabc33409ca Mon Sep 17 00:00:00 2001 From: Frederic Martinsons Date: Tue, 6 Jun 2023 07:23:04 +0200 Subject: [PATCH 2/2] tools/tests: correct variant parsing Seen in NM 1.42.6 where there is now a ipv4.dns-data key which have as signature: dbus.Array([dbus.String('a.b.c.d')], signature=dbus.Signature('s'), variant_level=1) This lead to the following exception: Cannot convert array element to type 's': Must be string, not Variant Moreover, the exception TypeError has no message field so it raised another expcetion which gave me trouble to find what's going on. Hence the addition of a log file from the previous commit Signed-off-by: Frederic Martinsons --- tools/test-networkmanager-service.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/test-networkmanager-service.py b/tools/test-networkmanager-service.py index 528422d15a..8221804143 100755 --- a/tools/test-networkmanager-service.py +++ b/tools/test-networkmanager-service.py @@ -315,9 +315,9 @@ class Util: if isinstance(val, dbus.Array): try: if val.signature == "s": - return GLib.Variant("as", [Util.variant_from_dbus(x) for x in val]) + return GLib.Variant("as", [str(x) for x in val]) if val.signature == "b": - return GLib.Variant("ab", [Util.variant_from_dbus(x) for x in val]) + return GLib.Variant("ab", [bool(x) for x in val]) if val.signature == "y": return GLib.Variant("ay", [int(x) for x in val]) if val.signature == "u": @@ -349,8 +349,7 @@ class Util: ) except Exception as e: raise Exception( - "Cannot convert array element to type '%s': %s" - % (val.signature, e.message) + "Cannot convert array element to type '%s': %s" % (val.signature, e) ) if isinstance(val, dbus.Dictionary): if val.signature == "ss":