diff mbox

[KVM-AUTOTEST,05/14] KVM test: kvm_vm.py: make get_pid() return the pid of the VM itself

Message ID 1276439625-32472-5-git-send-email-mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish June 13, 2010, 2:33 p.m. UTC
None
diff mbox

Patch

diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index 67a13d7..9d3539f 100755
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -594,8 +594,7 @@  class VM:
                 self.destroy()
                 return False
 
-            logging.debug("VM appears to be alive with PID %d",
-                          self.process.get_pid())
+            logging.debug("VM appears to be alive with PID %s", self.get_pid())
             return True
 
         finally:
@@ -704,8 +703,7 @@  class VM:
                 logging.debug("VM is already down")
                 return
 
-            logging.debug("Destroying VM with PID %d..." %
-                          self.process.get_pid())
+            logging.debug("Destroying VM with PID %s...", self.get_pid())
 
             if gracefully and self.params.get("shutdown_command"):
                 # Try to destroy with shell command
@@ -853,7 +851,25 @@  class VM:
 
     def get_pid(self):
         """
-        Return the VM's PID.
+        Return the VM's PID.  If the VM is dead return None.
+
+        @note: This works under the assumption that self.process.get_pid()
+        returns the PID of the parent shell process.
+        """
+        try:
+            children = commands.getoutput("ps --ppid=%d -o pid=" %
+                                          self.process.get_pid()).split()
+            return int(children[0])
+        except (TypeError, IndexError, ValueError):
+            return None
+
+
+    def get_shell_pid(self):
+        """
+        Return the PID of the parent shell process.
+
+        @note: This works under the assumption that self.process.get_pid()
+        returns the PID of the parent shell process.
         """
         return self.process.get_pid()
 
diff --git a/client/tests/kvm/tests/timedrift.py b/client/tests/kvm/tests/timedrift.py
index 9cb7489..bb23830 100644
--- a/client/tests/kvm/tests/timedrift.py
+++ b/client/tests/kvm/tests/timedrift.py
@@ -80,7 +80,7 @@  def run_timedrift(test, params, env):
 
     try:
         # Set the VM's CPU affinity
-        prev_affinity = set_cpu_affinity(vm.get_pid(), cpu_mask)
+        prev_affinity = set_cpu_affinity(vm.get_shell_pid(), cpu_mask)
 
         try:
             # Open shell sessions with the guest