]> git.proxmox.com Git - mirror_frr.git/commitdiff
munet: import 0.13.5 w/ nicer cmd logging
authorChristian Hopps <chopps@labn.net>
Wed, 24 May 2023 06:46:58 +0000 (02:46 -0400)
committerChristian Hopps <chopps@labn.net>
Wed, 24 May 2023 06:48:35 +0000 (02:48 -0400)
Signed-off-by: Christian Hopps <chopps@labn.net>
tests/topotests/lib/topotest.py
tests/topotests/munet/base.py

index aeb83d42904547e999318e3c5c1f5c1153745e03..9cfeb8e1decb65826c1d42c261a5fb38b54cc078 100644 (file)
@@ -1100,7 +1100,7 @@ def _sysctl_atleast(commander, variable, min_value):
         else:
             valstr = str(min_value)
         logger.debug("Increasing sysctl %s from %s to %s", variable, cur_val, valstr)
-        commander.cmd_raises('sysctl -w {}="{}"\n'.format(variable, valstr))
+        commander.cmd_raises('sysctl -w {}="{}"'.format(variable, valstr))
 
 
 def _sysctl_assure(commander, variable, value):
index c6ae70e09b21fe5a46efc504e07208c3c491d6b4..06ca4deae2ad51ca92380740e125c37e6ea8023f 100644 (file)
@@ -47,6 +47,9 @@ root_hostname = subprocess.check_output("hostname")
 our_pid = os.getpid()
 
 
+detailed_cmd_logging = False
+
+
 class MunetError(Exception):
     """A generic munet error."""
 
@@ -119,6 +122,27 @@ def cmd_error(rc, o, e):
     return s + o + e
 
 
+def shorten(s):
+    s = s.strip()
+    i = s.find("\n")
+    if i > 0:
+        s = s[: i - 1]
+        if not s.endswith("..."):
+            s += "..."
+    if len(s) > 72:
+        s = s[:69]
+        if not s.endswith("..."):
+            s += "..."
+    return s
+
+
+def comm_result(rc, o, e):
+    s = f"\n\treturncode {rc}" if rc else ""
+    o = "\n\tstdout: " + shorten(o) if o and o.strip() else ""
+    e = "\n\tstderr: " + shorten(e) if e and e.strip() else ""
+    return s + o + e
+
+
 def proc_str(p):
     if hasattr(p, "args"):
         args = p.args if isinstance(p.args, str) else " ".join(p.args)
@@ -477,16 +501,27 @@ class Commander:  # pylint: disable=R0904
                 defaults["preexec_fn"] = os.setsid
             defaults["env"]["PS1"] = "$ "
 
-        self.logger.debug(
-            '%s: %s %s("%s", pre_cmd: "%s" use_pty: %s kwargs: %.120s)',
-            self,
-            "XXX" if method == "_spawn" else "",
-            method,
-            cmd_list,
-            pre_cmd_list if not skip_pre_cmd else "",
-            use_pty,
-            defaults,
-        )
+        if not detailed_cmd_logging:
+            pre_cmd_str = shlex.join(pre_cmd_list) if not skip_pre_cmd else ""
+            if "nsenter" in pre_cmd_str:
+                self.logger.debug('%s("%s")', method, shlex.join(cmd_list))
+            elif pre_cmd_str:
+                self.logger.debug(
+                    '%s("%s") [precmd: %s]', method, shlex.join(cmd_list), pre_cmd_str
+                )
+            else:
+                self.logger.debug('%s("%s") [no precmd]', method, shlex.join(cmd_list))
+        else:
+            self.logger.debug(
+                '%s: %s %s("%s", pre_cmd: "%s" use_pty: %s kwargs: %.120s)',
+                self,
+                "XXX" if method == "_spawn" else "",
+                method,
+                cmd_list,
+                pre_cmd_list if not skip_pre_cmd else "",
+                use_pty,
+                defaults,
+            )
 
         actual_cmd_list = cmd_list if skip_pre_cmd else pre_cmd_list + cmd_list
         return actual_cmd_list, defaults
@@ -873,7 +908,11 @@ class Commander:  # pylint: disable=R0904
     def _cmd_status_finish(self, p, c, ac, o, e, raises, warn):
         rc = p.returncode
         self.last = (rc, ac, c, o, e)
-        if rc:
+        if not rc:
+            resstr = comm_result(rc, o, e)
+            if resstr:
+                self.logger.debug("%s", resstr)
+        else:
             if warn:
                 self.logger.warning("%s: proc failed: %s", self, proc_error(p, o, e))
             if raises: