[KVM-AUTOTEST PATCH 3/4] KVM test: kvm_vm.py: wrap VM.destroy() in a try-finally block

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This makes the function a little shorter or at least a little cleaner.

Signed-off-by: Michael Goldish <mgoldish@xxxxxxxxxx>
---
 client/tests/kvm/kvm_vm.py |   84 ++++++++++++++++++++++----------------------
 1 files changed, 42 insertions(+), 42 deletions(-)

diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index f728104..89a31df 100755
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -502,55 +502,55 @@ class VM:
                 using a shell command before trying to end the qemu process
                 with a 'quit' or a kill signal.
         """
-        # Is it already dead?
-        if self.is_dead():
-            logging.debug("VM is already down")
-            if self.process:
-                self.process.close()
-            return
+        try:
+            # Is it already dead?
+            if self.is_dead():
+                logging.debug("VM is already down")
+                return
 
-        logging.debug("Destroying VM with PID %d..." % self.process.get_pid())
+            logging.debug("Destroying VM with PID %d..." %
+                          self.process.get_pid())
 
-        if gracefully and self.params.get("shutdown_command"):
-            # Try to destroy with shell command
-            logging.debug("Trying to shutdown VM with shell command...")
-            session = self.remote_login()
-            if session:
-                try:
-                    # Send the shutdown command
-                    session.sendline(self.params.get("shutdown_command"))
-                    logging.debug("Shutdown command sent; waiting for VM to go "
-                                  "down...")
-                    if kvm_utils.wait_for(self.is_dead, 60, 1, 1):
-                        logging.debug("VM is down")
-                        self.process.close()
-                        return
-                finally:
-                    session.close()
-
-        # Try to destroy with a monitor command
-        logging.debug("Trying to kill VM with monitor command...")
-        (status, output) = self.send_monitor_cmd("quit", block=False)
-        # Was the command sent successfully?
-        if status == 0:
+            if gracefully and self.params.get("shutdown_command"):
+                # Try to destroy with shell command
+                logging.debug("Trying to shutdown VM with shell command...")
+                session = self.remote_login()
+                if session:
+                    try:
+                        # Send the shutdown command
+                        session.sendline(self.params.get("shutdown_command"))
+                        logging.debug("Shutdown command sent; waiting for VM "
+                                      "to go down...")
+                        if kvm_utils.wait_for(self.is_dead, 60, 1, 1):
+                            logging.debug("VM is down")
+                            return
+                    finally:
+                        session.close()
+
+            # Try to destroy with a monitor command
+            logging.debug("Trying to kill VM with monitor command...")
+            status, output = self.send_monitor_cmd("quit", block=False)
+            # Was the command sent successfully?
+            if status == 0:
+                # Wait for the VM to be really dead
+                if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5):
+                    logging.debug("VM is down")
+                    return
+
+            # If the VM isn't dead yet...
+            logging.debug("Cannot quit normally; sending a kill to close the "
+                          "deal...")
+            kvm_utils.kill_process_tree(self.process.get_pid(), 9)
             # Wait for the VM to be really dead
             if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5):
                 logging.debug("VM is down")
-                self.process.close()
                 return
 
-        # If the VM isn't dead yet...
-        logging.debug("Cannot quit normally; sending a kill to close the "
-                      "deal...")
-        kvm_utils.kill_process_tree(self.process.get_pid(), 9)
-        # Wait for the VM to be really dead
-        if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5):
-            logging.debug("VM is down")
-            self.process.close()
-            return
-
-        logging.error("Process %s is a zombie!" % self.process.get_pid())
-        self.process.close()
+            logging.error("Process %s is a zombie!" % self.process.get_pid())
+
+        finally:
+            if self.process:
+                self.process.close()
 
 
     def is_alive(self):
-- 
1.5.4.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux