Skip to contentSkip to conversation

KVM test: migration test: destroy dest_vm if test fails

PATCH New Michael Goldish 1 participant 4 messages Sep 14, 2009 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 KVM test: migration test: destroy dest_vm if test fails

---
 client/tests/kvm/kvm_tests.py |   94 ++++++++++++++++++++++-------------------
 1 files changed, 50 insertions(+), 44 deletions(-)
client/tests/kvm/kvm_tests.py +49 -43
index b61d98c..446b415 100644
@@ -129,46 +129,54 @@ def run_migration(test, params, env):
     dest_vm = vm.clone()
     dest_vm.create(for_migration=True)
 
-    # Define the migration command
-    cmd = "migrate -d tcp:localhost:%d" % dest_vm.migration_port
-    logging.debug("Migration command: %s" % cmd)
-
-    # Migrate
-    s, o = vm.send_monitor_cmd(cmd)
-    if s:
-        logging.error("Migration command failed (command: %r, output: %r)" %
-                      (cmd, o))
-        raise error.TestFail("Migration command failed")
-
-    # Define some helper functions
-    def mig_finished():
-        s, o = vm.send_monitor_cmd("info migrate")
-        return s == 0 and not "Migration status: active" in o
-
-    def mig_succeeded():
-        s, o = vm.send_monitor_cmd("info migrate")
-        return s == 0 and "Migration status: completed" in o
-
-    def mig_failed():
-        s, o = vm.send_monitor_cmd("info migrate")
-        return s == 0 and "Migration status: failed" in o
-
-    # Wait for migration to finish
-    if not kvm_utils.wait_for(mig_finished, 90, 2, 2,
-                              "Waiting for migration to finish..."):
-        raise error.TestFail("Timeout elapsed while waiting for migration to "
-                             "finish")
-
-    # Report migration status
-    if mig_succeeded():
-        logging.info("Migration finished successfully")
-    elif mig_failed():
-        raise error.TestFail("Migration failed")
-    else:
-        raise error.TestFail("Migration ended with unknown status")
+    try:
+        # Define the migration command
+        cmd = "migrate -d tcp:localhost:%d" % dest_vm.migration_port
+        logging.debug("Migration command: %s" % cmd)
+
+        # Migrate
+        s, o = vm.send_monitor_cmd(cmd)
+        if s:
+            logging.error("Migration command failed (command: %r, output: %r)"
+                          % (cmd, o))
+            raise error.TestFail("Migration command failed")
+
+        # Define some helper functions
+        def mig_finished():
+            s, o = vm.send_monitor_cmd("info migrate")
+            return s == 0 and not "Migration status: active" in o
+
+        def mig_succeeded():
+            s, o = vm.send_monitor_cmd("info migrate")
+            return s == 0 and "Migration status: completed" in o
+
+        def mig_failed():
+            s, o = vm.send_monitor_cmd("info migrate")
+            return s == 0 and "Migration status: failed" in o
+
+        # Wait for migration to finish
+        if not kvm_utils.wait_for(mig_finished, 90, 2, 2,
+                                  "Waiting for migration to finish..."):
+            raise error.TestFail("Timeout elapsed while waiting for migration "
+                                 "to finish")
+
+        # Report migration status
+        if mig_succeeded():
+            logging.info("Migration finished successfully")
+        elif mig_failed():
+            raise error.TestFail("Migration failed")
+        else:
+            raise error.TestFail("Migration ended with unknown status")
 
-    # Kill the source VM
-    vm.destroy(gracefully=False)
+        # Kill the source VM
+        vm.destroy(gracefully=False)
+
+        # Replace the source VM with the new cloned VM
+        kvm_utils.env_register_vm(env, params.get("main_vm"), dest_vm)
+
+    except:
+        dest_vm.destroy(gracefully=False)
+        raise
 
     # Log into guest and get the output of migration_test_command
     logging.info("Logging into guest after migration...")
@@ -189,13 +197,11 @@ def run_migration(test, params, env):
         logging.info("Command: %s" % params.get("migration_test_command"))
         logging.info("Output before:" +
                      kvm_utils.format_str_for_message(reference_output))
-        logging.info("Output after:" + kvm_utils.format_str_for_message(output))
+        logging.info("Output after:" +
+                     kvm_utils.format_str_for_message(output))
         raise error.TestFail("Command produced different output before and "
                              "after migration")
 
-    # Replace the main VM with the new cloned VM
-    kvm_utils.env_register_vm(env, params.get("main_vm"), dest_vm)
-
 
 def run_autotest(test, params, env):
     """
Signature · 1 line
1.5.4.1
#1 lore raw

PATCH 2/4 KVM test: wait_for_login(): include the VM's name in log messages

---
 client/tests/kvm/kvm_test_utils.py |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
client/tests/kvm/kvm_test_utils.py +2 -2
index 39e92b9..9924232 100644
@@ -52,10 +52,10 @@ def wait_for_login(vm, nic_index=0, timeout=240):
     @param timeout: Time to wait before giving up.
     @return: A shell session object.
     """
-    logging.info("Waiting for guest to be up...")
+    logging.info("Waiting for guest '%s' to be up..." % vm.name)
     session = kvm_utils.wait_for(lambda: vm.remote_login(nic_index=nic_index),
                                  timeout, 0, 2)
     if not session:
-        raise error.TestFail("Could not log into guest")
+        raise error.TestFail("Could not log into guest '%s'" % vm.name)
     logging.info("Logged in")
     return session
Signature · 1 line
1.5.4.1
#2 lore raw

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

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

---
 client/tests/kvm/kvm_vm.py |   84 ++++++++++++++++++++++----------------------
 1 files changed, 42 insertions(+), 42 deletions(-)
client/tests/kvm/kvm_vm.py +42 -42
index f728104..89a31df 100755
@@ -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):
Signature · 1 line
1.5.4.1
#3 lore raw

PATCH 4/4 KVM test: boot test: change the scope of the try-finally statement

It's possible for the test to fail when there's no shell session to close, and then session.close() in the finally clause raises a TypeError.

---
 client/tests/kvm/kvm_tests.py |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
client/tests/kvm/kvm_tests.py +7 -8
index 446b415..43a6291 100644
@@ -44,18 +44,17 @@ def run_boot(test, params, env):
                                   120, 0, 1):
             raise error.TestFail("Guest refuses to go down")
 
+    finally:
         session.close()
 
-        logging.info("Guest is down; waiting for it to go up again...")
-
-        session = kvm_utils.wait_for(vm.remote_login, 240, 0, 2)
-        if not session:
-            raise error.TestFail("Could not log into guest after reboot")
+    logging.info("Guest is down; waiting for it to go up again...")
 
-        logging.info("Guest is up again")
+    session = kvm_utils.wait_for(vm.remote_login, 240, 0, 2)
+    if not session:
+        raise error.TestFail("Could not log into guest after reboot")
+    session.close()
 
-    finally:
-        session.close()
+    logging.info("Guest is up again")
 
 
 def run_shutdown(test, params, env):
Signature · 1 line
1.5.4.1