FIX: Pylint reduce
[csit.git] / resources / libraries / python / DUTSetup.py
index 3f8c407..712da63 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2020 Cisco and/or its affiliates.
+# Copyright (c) 2021 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
@@ -33,11 +33,11 @@ class DUTSetup:
         :type node: dict
         :type service: str
         """
-        command = u"echo $(< /tmp/*supervisor*.log)"\
+        command = u"cat /tmp/*supervisor*.log"\
             if DUTSetup.running_in_container(node) \
-            else f"journalctl --no-pager --unit={service} " \
-            f"--since=\"$(echo `systemctl show -p ActiveEnterTimestamp " \
-            f"{service}` | awk \'{{print $2 $3}}\')\""
+            else f"journalctl --no-pager _SYSTEMD_INVOCATION_ID=$(systemctl " \
+            f"show -p InvocationID --value {service})"
+
         message = f"Node {node[u'host']} failed to get logs from unit {service}"
 
         exec_cmd_no_error(
@@ -175,24 +175,24 @@ class DUTSetup:
         else:
             shell_cmd = f"ip netns exec {namespace} sh -c"
 
-        pgrep_cmd = f"{shell_cmd} \'pgrep {program}\'"
-        ret_code, _, _ = exec_cmd(node, pgrep_cmd, timeout=cmd_timeout,
-                                  sudo=True)
-        if ret_code == 0:
+        pgrep_cmd = f"{shell_cmd} \'pgrep -c {program}\'"
+        _, stdout, _ = exec_cmd(node, pgrep_cmd, timeout=cmd_timeout,
+                                sudo=True)
+        if int(stdout) == 0:
             logger.trace(f"{program} is not running on {host}")
             return
-        ret_code, _, _ = exec_cmd(node, f"{shell_cmd} \'pkill {program}\'",
-                                  timeout=cmd_timeout, sudo=True)
+        exec_cmd(node, f"{shell_cmd} \'pkill {program}\'",
+                 timeout=cmd_timeout, sudo=True)
         for attempt in range(5):
-            ret_code, _, _ = exec_cmd(node, pgrep_cmd, timeout=cmd_timeout,
-                                      sudo=True)
-            if ret_code != 0:
+            _, stdout, _ = exec_cmd(node, pgrep_cmd, timeout=cmd_timeout,
+                                    sudo=True)
+            if int(stdout) == 0:
                 logger.trace(f"Attempt {attempt}: {program} is dead on {host}")
                 return
             sleep(1)
         logger.trace(f"SIGKILLing {program} on {host}")
-        ret_code, _, _ = exec_cmd(node, f"{shell_cmd} \'pkill -9 {program}\'",
-                                  timeout=cmd_timeout, sudo=True)
+        exec_cmd(node, f"{shell_cmd} \'pkill -9 {program}\'",
+                 timeout=cmd_timeout, sudo=True)
 
     @staticmethod
     def verify_program_installed(node, program):
@@ -429,6 +429,18 @@ class DUTSetup:
             node, command, timeout=120, sudo=True, message=message
         )
 
+    @staticmethod
+    def pci_driver_unbind_list(node, *pci_addrs):
+        """Unbind PCI devices from current driver on node.
+
+        :param node: DUT node.
+        :param pci_addrs: PCI device addresses.
+        :type node: dict
+        :type pci_addrs: list
+        """
+        for pci_addr in pci_addrs:
+            DUTSetup.pci_driver_unbind(node, pci_addr)
+
     @staticmethod
     def pci_driver_bind(node, pci_addr, driver):
         """Bind PCI device to driver on node.