Skip to contentSkip to conversation

Improve KVM autotest error reporting

PATCH New Lucas Meneghel Rodrigues 1 participant 6 messages Nov 8, 2011 KVM

lore
Share & tools
lore
#0 lore raw

cover Improve KVM autotest error reporting

This patchset adds some very welcome features:
* Make it possible to fail some tests due to guest screen inactivity (unattended_install, mainly)
* Make monitor errors to not mask qemu segmentation fault errors
* Introduce a mechanism to communicate errors that happen on a background thread to the main test thread.

It was also sent as:

https://github.com/autotest/autotest/pull/62

Lucas Meneghel Rodrigues (5):
virt.kvm_vm: Introduce background error queue virt.virt_env_process: fail tests due to guest screen inactivity virt tests: Introduce inactivity watcher for unattended_install virt.kvm_vm: Introduce vm.verify_userspace_crash() KVM test: Verify qemu process before throwing monitor exceptions

 client/tests/kvm/base.cfg.sample        |    6 ++++
 client/tests/kvm/subtests.cfg.sample    |    2 +
 client/virt/kvm_monitor.py              |   24 +++++++++++++++++-
 client/virt/kvm_vm.py                   |   28 +++++++++++++++++++---
 client/virt/libvirt_vm.py               |    7 +++++
 client/virt/tests/unattended_install.py |    2 +
 client/virt/virt_env_process.py         |   39 +++++++++++++++++++++++++++----
 client/virt/virt_test.py                |   19 ++++++++++++++-
 8 files changed, 115 insertions(+), 12 deletions(-)
Signature · 1 line
1.7.7
#1 lore raw

PATCH 1/5 virt.kvm_vm: Introduce background error queue

Introduce a background error queue to store exceptions happened during virt background tasks (such as aexpect instances and screendump thread). With this queue, it is possible to store exception information useful to fail tests at certain points. Examples:

* We can detect qemu suffered a crash from inside the aexpect thread
* We can detect screen inactivity on the screendump thread and fail the unattended install test without having to wait the entire timeout of the test

The next patches will introduce that functionality.

Implementation consists in a virt test attribute, background_error test, that is a Queue.Queue() (thread safe), background threads that want to make use of it need to catch the exception they want to propagate and store it in the queue using put(sys.exc_info).

Then in every convenient situation to do so, subtests can call the test.verify_bg_errors() method (in the beginning or end of each test loop, where operations such as migrations are executed).

---
 client/virt/virt_test.py |   19 ++++++++++++++++++-
 1 files changed, 18 insertions(+), 1 deletions(-)
client/virt/virt_test.py +18 -1
index e18081b..0ce43c4 100644
@@ -1,4 +1,4 @@
-import os, sys, logging, imp
+import os, sys, logging, imp, Queue
 
 from autotest_lib.client.bin import test
 from autotest_lib.client.common_lib import error
@@ -22,6 +22,22 @@ class virt_test(test.test):
         if params.get("preserve_srcdir", "yes") == "yes":
             self.preserve_srcdir = True
         self.virtdir = os.path.dirname(sys.modules[__name__].__file__)
+        # Queue to store exceptions that happen in bg threads
+        self.background_errors = Queue.Queue()
+
+
+    def verify_background_errors(self):
+        """
+        Verify if there are any errors that happened on background threads.
+
+        @raise Exception: Any exception stored on the background_errors queue.
+        """
+        try:
+            exc = self.background_errors.get(block=False)
+        except Queue.Empty:
+            pass
+        else:
+            raise exc[1], None, exc[2]
 
 
     def run_once(self, params):
@@ -86,6 +102,7 @@ class virt_test(test.test):
                     run_func = getattr(test_module, "run_%s" % t_type)
                     try:
                         run_func(self, params, env)
+                        self.verify_background_errors()
                     finally:
                         env.save()
                     test_passed = True
Signature · 1 line
1.7.7
#2 lore raw

PATCH 2/5 virt.virt_env_process: fail tests due to guest screen inactivity

In reply to Lucas Meneghel Rodrigues

In a very specific test (unattended_install), all OEM installer mechanism will present intense guest screen activity, so screen inactivity for a relatively long time almost always mean something went very wrong and the installer is stuck.

So create a mechanism to detect screen inactivity for any vm that takes place in the test - have a timer for each vm that is reset only if the next screenshot is different than the previous. So, if the vm got stuck somewhere at the install process, for example:

* Wrong Windows CD Key
* Guest OS does not see a disk controller due to lack of drivers
* BIOS got stuck

And other similar cases, we'll have a test failure before the full test timeout (that for Windows may be up to 3 hours). So we save time and know about such situations earlier.

The patch introduces the parameters on base.cfg, subtests.cfg, and also introduces a sensible default for inactivity treshold (30 min).

---
 client/virt/tests/unattended_install.py |    1 +
 client/virt/virt_env_process.py         |   39 +++++++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 5 deletions(-)
client/virt/tests/unattended_install.py +1 -0
index a78e2bc..57161a8 100644
@@ -800,6 +800,7 @@ def run_unattended_install(test, params, env):
             else:
                 raise e
         vm.verify_kernel_crash()
+        test.verify_background_errors()
         if params.get("wait_no_ack", "no") == "no":
             client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             try:
client/virt/virt_env_process.py +34 -5
index 27a60d3..5f33606 100644
@@ -1,4 +1,4 @@
-import os, time, commands, re, logging, glob, threading, shutil
+import os, time, commands, re, logging, glob, threading, shutil, sys
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 import aexpect, virt_utils, kvm_monitor, ppm_utils, virt_test_setup
@@ -16,6 +16,18 @@ _screendump_thread = None
 _screendump_thread_termination_event = None
 
 
+class VMScreenInactiveError(virt_vm.VMError):
+    def __init__(self, vm, inactive_time):
+        virt_vm.VMError.__init__(self)
+        self.vm = vm
+        self.inactive_time = inactive_time
+
+    def __str__(self):
+        msg = ("%s screen is inactive for %d s (%d min)" %
+               (self.vm.name, self.inactive_time, self.inactive_time/60))
+        return msg
+
+
 def ffmpeg_path():
     try:
         return virt_utils.find_command("ffmpeg")
@@ -491,14 +503,19 @@ def _take_screendumps(test, params, env):
                                  virt_utils.generate_random_string(6))
     delay = float(params.get("screendump_delay", 5))
     quality = int(params.get("screendump_quality", 30))
+    inactivity_treshold = float(params.get("inactivity_treshold", 1800))
+    inactivity_watcher = params.get("inactivity_watcher", "log")
 
     cache = {}
     counter = {}
+    inactivity = {}
 
     while True:
         for vm in env.get_all_vms():
             if vm not in counter.keys():
                 counter[vm] = 0
+            if vm not in inactivity.keys():
+                inactivity[vm] = time.time()
             if not vm.is_alive():
                 continue
             try:
@@ -524,17 +541,29 @@ def _take_screendumps(test, params, env):
             counter[vm] += 1
             screendump_filename = os.path.join(screendump_dir, "%04d.jpg" %
                                                counter[vm])
-            hash = utils.hash_file(temp_filename)
-            if hash in cache:
+            image_hash = utils.hash_file(temp_filename)
+            if image_hash in cache:
+                time_inactive = time.time() - inactivity[vm]
+                if time_inactive > inactivity_treshold:
+                    if inactivity_watcher == "error":
+                        try:
+                            raise VMScreenInactiveError(vm, time_inactive)
+                        except VMScreenInactiveError:
+                            test.background_errors.put(sys.exc_info())
+                    else:
+                        logging.debug("%s screen is inactive for more than "
+                                      "%d s (%d min)", vm.name, time_inactive,
+                                      time_inactive/60)
                 try:
-                    os.link(cache[hash], screendump_filename)
+                    os.link(cache[image_hash], screendump_filename)
                 except OSError:
                     pass
             else:
+                inactivity[vm] = time.time()
                 try:
                     image = PIL.Image.open(temp_filename)
                     image.save(screendump_filename, format="JPEG", quality=quality)
-                    cache[hash] = screendump_filename
+                    cache[image_hash] = screendump_filename
                 except NameError:
                     pass
             os.unlink(temp_filename)
Signature · 1 line
1.7.7
#3 lore raw

PATCH 3/5 virt tests: Introduce inactivity watcher for unattended_install

In reply to Lucas Meneghel Rodrigues

With this patch, if the screen got inactive during unattended install for more than 30 minutes, an error will be thrown. For any other tests, we'll just log a debug message, since in other tests we do not expect to see much action going on the screen.

---
 client/tests/kvm/base.cfg.sample     |    6 ++++++
 client/tests/kvm/subtests.cfg.sample |    2 ++
 2 files changed, 8 insertions(+), 0 deletions(-)
client/tests/kvm/base.cfg.sample +6 -0
index 1bda496..48a4539 100644
@@ -111,3 +111,9 @@ login_timeout = 360
 
 # NFS directory of guest images
 images_good = fileserver.foo.com:/autotest/images_good
+
+# Guest screen max inactive time until taking actions (log or throw errors)
+inactivity_treshold = 1800
+
+# Default action if inactivity threshold is reached
+inactivity_watcher = log
client/tests/kvm/subtests.cfg.sample +2 -0
index dc07416..8625b2c 100644
@@ -50,6 +50,8 @@ variants:
         guest_port_unattended_install = 12323
         kernel = vmlinuz
         initrd = initrd.img
+        # Throw errors if guest screen is inactive
+        inactivity_watcher = error
         # Set migrate_background to yes to run migration in parallel
         migrate_background = no
         # Backup image settings
Signature · 1 line
1.7.7
#4 lore raw

PATCH 4/5 virt.kvm_vm: Introduce vm.verify_userspace_crash()

In reply to Lucas Meneghel Rodrigues

In order to capture any qemu crashes, introduce a method that scans qemu output and throws an error if a core dump condition is found.

---
 client/virt/kvm_vm.py                   |   18 ++++++++++++++++++
 client/virt/libvirt_vm.py               |    7 +++++++
 client/virt/tests/unattended_install.py |    1 +
 3 files changed, 26 insertions(+), 0 deletions(-)
client/virt/kvm_vm.py +18 -0
index 4a29721..372cfbc 100644
@@ -10,6 +10,15 @@ from autotest_lib.client.bin import utils
 import virt_utils, virt_vm, virt_test_setup, kvm_monitor, aexpect
 
 
+class QemuSegFaultError(virt_vm.VMError):
+    def __init__(self, crash_message):
+        virt_vm.VMError.__init__(self, crash_message)
+        self.crash_message = crash_message
+
+    def __str__(self):
+        return ("Qemu crashed: %s" % self.crash_message)
+
+
 class VM(virt_vm.BaseVM):
     """
     This class handles all basic VM operations.
@@ -96,6 +105,15 @@ class VM(virt_vm.BaseVM):
             raise virt_vm.VMStatusError("VM status is unexpected")
 
 
+    def verify_userspace_crash(self):
+        """
+        Verify if the userspace component (qemu) crashed.
+        """
+        for line in self.process.get_output().splitlines():
+            if "(core dumped)" in line:
+                raise QemuSegFaultError(line)
+
+
     def clone(self, name=None, params=None, root_dir=None, address_cache=None,
               copy_state=False):
         """
client/virt/libvirt_vm.py +7 -0
index 0c32551..760ccef 100644
@@ -324,6 +324,13 @@ class VM(virt_vm.BaseVM):
         return virsh_is_dead(self.name)
 
 
+    def verify_userspace_crash(self):
+        """
+        Doesn't do anything yet.
+        """
+        pass
+
+
     def clone(self, name=None, params=None, root_dir=None, address_cache=None,
               copy_state=False):
         """
client/virt/tests/unattended_install.py +1 -0
index 57161a8..bc5f29f 100644
@@ -799,6 +799,7 @@ def run_unattended_install(test, params, env):
                 break
             else:
                 raise e
+        vm.verify_userspace_crash()
         vm.verify_kernel_crash()
         test.verify_background_errors()
         if params.get("wait_no_ack", "no") == "no":
Signature · 1 line
1.7.7
#5 lore raw

PATCH 5/5 KVM test: Verify qemu process before throwing monitor exceptions

In reply to Lucas Meneghel Rodrigues

When qemu suffers a segmentation fault, monitor code might still want to issue commands to a VM, masking the segmentation fault.

So, pass the vm object to the monitor code, and before throwing monitor exceptions, verify the state of the qemu process (whether it crashed or not), and throw the QemuSegFaultError if the process crashed:

22:08:44 DEBUG| QemuSegFaultError: Qemu crashed: /bin/sh: line 1: 6847
Aborted (core dumped)

This will make analyzing such failures much easier.

---
 client/virt/kvm_monitor.py |   24 ++++++++++++++++++++++--
 client/virt/kvm_vm.py      |   10 ++++++----
 2 files changed, 28 insertions(+), 6 deletions(-)
client/virt/kvm_monitor.py +22 -2
index efd6aa3..049c28f 100644
@@ -77,6 +77,7 @@ class Monitor:
         try:
             self._socket.connect(filename)
         except socket.error:
+            self.vm.verify_userspace_crash()
             raise MonitorConnectError("Could not connect to monitor socket")
 
 
@@ -124,6 +125,7 @@ class Monitor:
         try:
             return bool(select.select([self._socket], [], [], timeout)[0])
         except socket.error, e:
+            self.vm.verify_userspace_crash()
             raise MonitorSocketError("Verifying data on monitor socket", e)
 
 
@@ -133,6 +135,7 @@ class Monitor:
             try:
                 data = self._socket.recv(1024)
             except socket.error, e:
+                self.vm.verify_userspace_crash()
                 raise MonitorSocketError("Could not receive data from monitor",
                                          e)
             if not data:
@@ -157,7 +160,7 @@ class HumanMonitor(Monitor):
     Wraps "human monitor" commands.
     """
 
-    def __init__(self, name, filename, suppress_exceptions=False):
+    def __init__(self, name, filename, vm, suppress_exceptions=False):
         """
         Connect to the monitor socket and find the (qemu) prompt.
 
@@ -174,10 +177,12 @@ class HumanMonitor(Monitor):
             Monitor.__init__(self, name, filename)
 
             self.protocol = "human"
+            self.vm = vm
 
             # Find the initial (qemu) prompt
             s, o = self._read_up_to_qemu_prompt(20)
             if not s:
+                self.vm.verify_userspace_crash()
                 raise MonitorProtocolError("Could not find (qemu) prompt "
                                            "after connecting to monitor. "
                                            "Output so far: %r" % o)
@@ -190,6 +195,7 @@ class HumanMonitor(Monitor):
             if suppress_exceptions:
                 logging.warn(e)
             else:
+                self.vm.verify_userspace_crash()
                 raise
 
 
@@ -220,6 +226,7 @@ class HumanMonitor(Monitor):
         @raise MonitorSocketError: Raised if a socket error occurs
         """
         if not self._acquire_lock(20):
+            self.vm.verify_userspace_crash()
             raise MonitorLockError("Could not acquire exclusive lock to send "
                                    "monitor command '%s'" % cmd)
 
@@ -227,6 +234,7 @@ class HumanMonitor(Monitor):
             try:
                 self._socket.sendall(cmd + "\n")
             except socket.error, e:
+                self.vm.verify_userspace_crash()
                 raise MonitorSocketError("Could not send monitor command %r" %
                                          cmd, e)
 
@@ -253,6 +261,7 @@ class HumanMonitor(Monitor):
             logging.debug("(monitor %s) Sending command '%s'",
                           self.name, command)
         if not self._acquire_lock(20):
+            self.vm.verify_userspace_crash()
             raise MonitorLockError("Could not acquire exclusive lock to send "
                                    "monitor command '%s'" % command)
 
@@ -283,6 +292,7 @@ class HumanMonitor(Monitor):
             else:
                 msg = ("Could not find (qemu) prompt after command '%s'. "
                        "Output so far: %r" % (command, o))
+                self.vm.verify_userspace_crash()
                 raise MonitorProtocolError(msg)
 
         finally:
@@ -464,7 +474,7 @@ class QMPMonitor(Monitor):
     Wraps QMP monitor commands.
     """
 
-    def __init__(self, name, filename, suppress_exceptions=False):
+    def __init__(self, name, filename, vm, suppress_exceptions=False):
         """
         Connect to the monitor socket, read the greeting message and issue the
         qmp_capabilities command.  Also make sure the json module is available.
@@ -486,6 +496,7 @@ class QMPMonitor(Monitor):
             self.protocol = "qmp"
             self._greeting = None
             self._events = []
+            self.vm = vm
 
             # Make sure json is available
             try:
@@ -505,6 +516,7 @@ class QMPMonitor(Monitor):
                     break
                 time.sleep(0.1)
             else:
+                self.vm.verify_userspace_crash()
                 raise MonitorProtocolError("No QMP greeting message received")
 
             # Issue qmp_capabilities
@@ -515,6 +527,7 @@ class QMPMonitor(Monitor):
             if suppress_exceptions:
                 logging.warn(e)
             else:
+                self.vm.verify_userspace_crash()
                 raise
 
 
@@ -578,6 +591,7 @@ class QMPMonitor(Monitor):
         try:
             self._socket.sendall(data)
         except socket.error, e:
+            self.vm.verify_userspace_crash()
             raise MonitorSocketError("Could not send data: %r" % data, e)
 
 
@@ -623,6 +637,7 @@ class QMPMonitor(Monitor):
             logging.debug("(monitor %s) Sending command '%s'",
                           self.name, cmd)
         if not self._acquire_lock(20):
+            self.vm.verify_userspace_crash()
             raise MonitorLockError("Could not acquire exclusive lock to send "
                                    "QMP command '%s'" % cmd)
 
@@ -635,6 +650,7 @@ class QMPMonitor(Monitor):
             # Read response
             r = self._get_response(id, timeout)
             if r is None:
+                self.vm.verify_userspace_crash()
                 raise MonitorProtocolError("Received no response to QMP "
                                            "command '%s', or received a "
                                            "response with an incorrect id"
@@ -648,6 +664,7 @@ class QMPMonitor(Monitor):
                         logging.debug("(monitor %s)    %s", self.name, l)
                 return r["return"]
             if "error" in r:
+                self.vm.verify_userspace_crash()
                 raise QMPCmdError(cmd, args, r["error"])
 
         finally:
@@ -668,6 +685,7 @@ class QMPMonitor(Monitor):
         @raise MonitorProtocolError: Raised if no response is received
         """
         if not self._acquire_lock(20):
+            self.vm.verify_userspace_crash()
             raise MonitorLockError("Could not acquire exclusive lock to send "
                                    "data: %r" % data)
 
@@ -676,6 +694,7 @@ class QMPMonitor(Monitor):
             self._send(data)
             r = self._get_response(None, timeout)
             if r is None:
+                self.vm.verify_userspace_crash()
                 raise MonitorProtocolError("Received no response to data: %r" %
                                            data)
             return r
@@ -778,6 +797,7 @@ class QMPMonitor(Monitor):
         @raise MonitorLockError: Raised if the lock cannot be acquired
         """
         if not self._acquire_lock(20):
+            self.vm.verify_userspace_crash()
             raise MonitorLockError("Could not acquire exclusive lock to clear "
                                    "QMP event list")
         self._events = []
client/virt/kvm_vm.py +6 -4
index 372cfbc..0f62170 100644
@@ -834,13 +834,15 @@ class VM(virt_vm.BaseVM):
                         if monitor_params.get("monitor_type") == "qmp":
                             # Add a QMP monitor
                             monitor = kvm_monitor.QMPMonitor(
-                                monitor_name,
-                                self.get_monitor_filename(monitor_name))
+                                name=monitor_name,
+                                filename=self.get_monitor_filename(monitor_name),
+                                vm=self)
                         else:
                             # Add a "human" monitor
                             monitor = kvm_monitor.HumanMonitor(
-                                monitor_name,
-                                self.get_monitor_filename(monitor_name))
+                                name=monitor_name,
+                                filename=self.get_monitor_filename(monitor_name),
+                                vm=self)
                         monitor.verify_responsive()
                         break
                     except kvm_monitor.MonitorError, e:
Signature · 1 line
1.7.7