lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 1 | import sys, os, time, commands, re, logging, signal, glob, threading, shutil |
lmr | 47a853b | 2010-02-04 13:56:48 +0000 | [diff] [blame] | 2 | from autotest_lib.client.bin import test, utils |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 3 | from autotest_lib.client.common_lib import error |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 4 | import kvm_vm, kvm_utils, kvm_subprocess, kvm_monitor, ppm_utils |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 5 | try: |
| 6 | import PIL.Image |
| 7 | except ImportError: |
| 8 | logging.warning('No python imaging library installed. PPM image ' |
| 9 | 'conversion to JPEG disabled. In order to enable it, ' |
| 10 | 'please install python-imaging or the equivalent for your ' |
| 11 | 'distro.') |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 12 | |
| 13 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 14 | _screendump_thread = None |
| 15 | _screendump_thread_termination_event = None |
| 16 | |
| 17 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 18 | def preprocess_image(test, params): |
| 19 | """ |
| 20 | Preprocess a single QEMU image according to the instructions in params. |
| 21 | |
| 22 | @param test: Autotest test object. |
| 23 | @param params: A dict containing image preprocessing parameters. |
| 24 | @note: Currently this function just creates an image if requested. |
| 25 | """ |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 26 | image_filename = kvm_vm.get_image_filename(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 27 | |
| 28 | create_image = False |
| 29 | |
| 30 | if params.get("force_create_image") == "yes": |
| 31 | logging.debug("'force_create_image' specified; creating image...") |
| 32 | create_image = True |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 33 | elif (params.get("create_image") == "yes" and not |
| 34 | os.path.exists(image_filename)): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 35 | logging.debug("Creating image...") |
| 36 | create_image = True |
| 37 | |
lmr | 0294de3 | 2009-09-09 22:44:28 +0000 | [diff] [blame] | 38 | if create_image and not kvm_vm.create_image(params, test.bindir): |
| 39 | raise error.TestError("Could not create image") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def preprocess_vm(test, params, env, name): |
| 43 | """ |
| 44 | Preprocess a single VM object according to the instructions in params. |
| 45 | Start the VM if requested and get a screendump. |
| 46 | |
| 47 | @param test: An Autotest test object. |
| 48 | @param params: A dict containing VM preprocessing parameters. |
| 49 | @param env: The environment (a dict-like object). |
| 50 | @param name: The name of the VM object. |
| 51 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 52 | logging.debug("Preprocessing VM '%s'..." % name) |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 53 | vm = env.get_vm(name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 54 | if vm: |
| 55 | logging.debug("VM object found in environment") |
| 56 | else: |
| 57 | logging.debug("VM object does not exist; creating it") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 58 | vm = kvm_vm.VM(name, params, test.bindir, env.get("address_cache")) |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 59 | env.register_vm(name, vm) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 60 | |
| 61 | start_vm = False |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 62 | |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 63 | migration_mode = params.get("migration_mode", None) |
| 64 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 65 | if params.get("restart_vm") == "yes": |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 66 | logging.debug("'restart_vm' specified; (re)starting VM...") |
| 67 | start_vm = True |
| 68 | elif params.get("start_vm") == "yes": |
| 69 | if not vm.is_alive(): |
| 70 | logging.debug("VM is not alive; starting it...") |
| 71 | start_vm = True |
| 72 | elif vm.make_qemu_command() != vm.make_qemu_command(name, params, |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 73 | test.bindir): |
lmr | a496762 | 2009-07-23 01:36:32 +0000 | [diff] [blame] | 74 | logging.debug("VM's qemu command differs from requested one; " |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 75 | "restarting it...") |
| 76 | start_vm = True |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 77 | elif migration_mode is not None: |
| 78 | logging.debug("Starting VM on migration incoming mode...") |
| 79 | start_vm = True |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 80 | |
lmr | e9a9cf3 | 2010-06-18 12:15:25 +0000 | [diff] [blame] | 81 | if start_vm: |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 82 | if migration_mode is not None: |
| 83 | if not vm.create(name, params, test.bindir, |
| 84 | migration_mode=migration_mode): |
| 85 | raise error.TestError("Could not start VM for migration") |
| 86 | else: |
| 87 | # Start the VM (or restart it if it's already up) |
| 88 | if not vm.create(name, params, test.bindir): |
| 89 | raise error.TestError("Could not start VM") |
lmr | e9a9cf3 | 2010-06-18 12:15:25 +0000 | [diff] [blame] | 90 | else: |
| 91 | # Don't start the VM, just update its params |
| 92 | vm.params = params |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 93 | |
| 94 | scrdump_filename = os.path.join(test.debugdir, "pre_%s.ppm" % name) |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 95 | try: |
| 96 | if vm.monitor: |
| 97 | vm.monitor.screendump(scrdump_filename) |
| 98 | except kvm_monitor.MonitorError, e: |
| 99 | logging.warn(e) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def postprocess_image(test, params): |
| 103 | """ |
| 104 | Postprocess a single QEMU image according to the instructions in params. |
| 105 | Currently this function just removes an image if requested. |
| 106 | |
| 107 | @param test: An Autotest test object. |
| 108 | @param params: A dict containing image postprocessing parameters. |
| 109 | """ |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 110 | if params.get("remove_image") == "yes": |
lmr | 90b9fd5 | 2009-08-17 20:48:18 +0000 | [diff] [blame] | 111 | kvm_vm.remove_image(params, test.bindir) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | def postprocess_vm(test, params, env, name): |
| 115 | """ |
| 116 | Postprocess a single VM object according to the instructions in params. |
| 117 | Kill the VM if requested and get a screendump. |
| 118 | |
| 119 | @param test: An Autotest test object. |
| 120 | @param params: A dict containing VM postprocessing parameters. |
| 121 | @param env: The environment (a dict-like object). |
| 122 | @param name: The name of the VM object. |
| 123 | """ |
| 124 | logging.debug("Postprocessing VM '%s'..." % name) |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 125 | vm = env.get_vm(name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 126 | if vm: |
| 127 | logging.debug("VM object found in environment") |
| 128 | else: |
| 129 | logging.debug("VM object does not exist in environment") |
| 130 | return |
| 131 | |
| 132 | scrdump_filename = os.path.join(test.debugdir, "post_%s.ppm" % name) |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 133 | try: |
| 134 | if vm.monitor: |
| 135 | vm.monitor.screendump(scrdump_filename) |
| 136 | except kvm_monitor.MonitorError, e: |
| 137 | logging.warn(e) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 138 | |
| 139 | if params.get("kill_vm") == "yes": |
lmr | adbfb1a | 2009-10-13 13:44:14 +0000 | [diff] [blame] | 140 | kill_vm_timeout = float(params.get("kill_vm_timeout", 0)) |
| 141 | if kill_vm_timeout: |
| 142 | logging.debug("'kill_vm' specified; waiting for VM to shut down " |
| 143 | "before killing it...") |
| 144 | kvm_utils.wait_for(vm.is_dead, kill_vm_timeout, 0, 1) |
| 145 | else: |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 146 | logging.debug("'kill_vm' specified; killing VM...") |
| 147 | vm.destroy(gracefully = params.get("kill_vm_gracefully") == "yes") |
| 148 | |
| 149 | |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 150 | def process_command(test, params, env, command, command_timeout, |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 151 | command_noncritical): |
| 152 | """ |
| 153 | Pre- or post- custom commands to be executed before/after a test is run |
| 154 | |
| 155 | @param test: An Autotest test object. |
| 156 | @param params: A dict containing all VM and image parameters. |
| 157 | @param env: The environment (a dict-like object). |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 158 | @param command: Command to be run. |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 159 | @param command_timeout: Timeout for command execution. |
| 160 | @param command_noncritical: If True test will not fail if command fails. |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 161 | """ |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 162 | # Export environment vars |
lmr | 0ce407d | 2010-03-23 15:46:30 +0000 | [diff] [blame] | 163 | for k in params: |
lmr | 0bc6efc | 2009-07-27 13:27:42 +0000 | [diff] [blame] | 164 | os.putenv("KVM_TEST_%s" % k, str(params[k])) |
lmr | b1ef847 | 2010-02-05 11:21:13 +0000 | [diff] [blame] | 165 | # Execute commands |
lmr | 12b98a7 | 2010-02-09 11:13:57 +0000 | [diff] [blame] | 166 | try: |
| 167 | utils.system("cd %s; %s" % (test.bindir, command)) |
| 168 | except error.CmdError, e: |
lmr | 1084553 | 2010-06-18 12:54:46 +0000 | [diff] [blame] | 169 | if command_noncritical: |
| 170 | logging.warn(e) |
| 171 | else: |
| 172 | raise |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 173 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 174 | def process(test, params, env, image_func, vm_func): |
| 175 | """ |
| 176 | Pre- or post-process VMs and images according to the instructions in params. |
| 177 | Call image_func for each image listed in params and vm_func for each VM. |
| 178 | |
| 179 | @param test: An Autotest test object. |
| 180 | @param params: A dict containing all VM and image parameters. |
| 181 | @param env: The environment (a dict-like object). |
| 182 | @param image_func: A function to call for each image. |
| 183 | @param vm_func: A function to call for each VM. |
| 184 | """ |
| 185 | # Get list of VMs specified for this test |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 186 | for vm_name in params.objects("vms"): |
| 187 | vm_params = params.object_params(vm_name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 188 | # Get list of images specified for this VM |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 189 | for image_name in vm_params.objects("images"): |
| 190 | image_params = vm_params.object_params(image_name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 191 | # Call image_func for each image |
| 192 | image_func(test, image_params) |
| 193 | # Call vm_func for each vm |
| 194 | vm_func(test, vm_params, env, vm_name) |
| 195 | |
| 196 | |
| 197 | def preprocess(test, params, env): |
| 198 | """ |
| 199 | Preprocess all VMs and images according to the instructions in params. |
| 200 | Also, collect some host information, such as the KVM version. |
| 201 | |
| 202 | @param test: An Autotest test object. |
| 203 | @param params: A dict containing all VM and image parameters. |
| 204 | @param env: The environment (a dict-like object). |
| 205 | """ |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 206 | # Start tcpdump if it isn't already running |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 207 | if "address_cache" not in env: |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 208 | env["address_cache"] = {} |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 209 | if "tcpdump" in env and not env["tcpdump"].is_alive(): |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 210 | env["tcpdump"].close() |
| 211 | del env["tcpdump"] |
lmr | b1f7646 | 2010-05-25 23:44:03 +0000 | [diff] [blame] | 212 | if "tcpdump" not in env and params.get("run_tcpdump", "yes") == "yes": |
lmr | 3eb39c7 | 2010-07-08 23:34:05 +0000 | [diff] [blame] | 213 | cmd = "%s -npvi any 'dst port 68'" % kvm_utils.find_command("tcpdump") |
| 214 | logging.debug("Starting tcpdump (%s)...", cmd) |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 215 | env["tcpdump"] = kvm_subprocess.Tail( |
lmr | 3eb39c7 | 2010-07-08 23:34:05 +0000 | [diff] [blame] | 216 | command=cmd, |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 217 | output_func=_update_address_cache, |
| 218 | output_params=(env["address_cache"],)) |
| 219 | if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(), |
| 220 | 0.1, 0.1, 1.0): |
| 221 | logging.warn("Could not start tcpdump") |
| 222 | logging.warn("Status: %s" % env["tcpdump"].get_status()) |
| 223 | logging.warn("Output:" + kvm_utils.format_str_for_message( |
| 224 | env["tcpdump"].get_output())) |
| 225 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 226 | # Destroy and remove VMs that are no longer needed in the environment |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 227 | requested_vms = params.objects("vms") |
lmr | 0ce407d | 2010-03-23 15:46:30 +0000 | [diff] [blame] | 228 | for key in env.keys(): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 229 | vm = env[key] |
| 230 | if not kvm_utils.is_vm(vm): |
| 231 | continue |
| 232 | if not vm.name in requested_vms: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 233 | logging.debug("VM '%s' found in environment but not required for " |
| 234 | "test; removing it..." % vm.name) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 235 | vm.destroy() |
| 236 | del env[key] |
| 237 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 238 | # Get the KVM kernel module version and write it as a keyval |
| 239 | logging.debug("Fetching KVM module version...") |
| 240 | if os.path.exists("/dev/kvm"): |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 241 | try: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 242 | kvm_version = open("/sys/module/kvm/version").read().strip() |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 243 | except: |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 244 | kvm_version = os.uname()[2] |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 245 | else: |
| 246 | kvm_version = "Unknown" |
| 247 | logging.debug("KVM module not loaded") |
| 248 | logging.debug("KVM version: %s" % kvm_version) |
| 249 | test.write_test_keyval({"kvm_version": kvm_version}) |
| 250 | |
| 251 | # Get the KVM userspace version and write it as a keyval |
| 252 | logging.debug("Fetching KVM userspace version...") |
lmr | 52800ba | 2009-08-17 20:49:58 +0000 | [diff] [blame] | 253 | qemu_path = kvm_utils.get_path(test.bindir, params.get("qemu_binary", |
| 254 | "qemu")) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 255 | version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 256 | matches = re.findall("[Vv]ersion .*?,", version_line) |
| 257 | if matches: |
| 258 | kvm_userspace_version = " ".join(matches[0].split()[1:]).strip(",") |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 259 | else: |
| 260 | kvm_userspace_version = "Unknown" |
| 261 | logging.debug("Could not fetch KVM userspace version") |
| 262 | logging.debug("KVM userspace version: %s" % kvm_userspace_version) |
| 263 | test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) |
| 264 | |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 265 | # Execute any pre_commands |
| 266 | if params.get("pre_command"): |
| 267 | process_command(test, params, env, params.get("pre_command"), |
| 268 | int(params.get("pre_command_timeout", "600")), |
| 269 | params.get("pre_command_noncritical") == "yes") |
| 270 | |
| 271 | # Preprocess all VMs and images |
| 272 | process(test, params, env, preprocess_image, preprocess_vm) |
| 273 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 274 | # Start the screendump thread |
| 275 | if params.get("take_regular_screendumps") == "yes": |
| 276 | logging.debug("Starting screendump thread") |
| 277 | global _screendump_thread, _screendump_thread_termination_event |
| 278 | _screendump_thread_termination_event = threading.Event() |
| 279 | _screendump_thread = threading.Thread(target=_take_screendumps, |
| 280 | args=(test, params, env)) |
| 281 | _screendump_thread.start() |
| 282 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 283 | |
| 284 | def postprocess(test, params, env): |
| 285 | """ |
| 286 | Postprocess all VMs and images according to the instructions in params. |
| 287 | |
| 288 | @param test: An Autotest test object. |
| 289 | @param params: Dict containing all VM and image parameters. |
| 290 | @param env: The environment (a dict-like object). |
| 291 | """ |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 292 | # Postprocess all VMs and images |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 293 | process(test, params, env, postprocess_image, postprocess_vm) |
| 294 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 295 | # Terminate the screendump thread |
| 296 | global _screendump_thread, _screendump_thread_termination_event |
| 297 | if _screendump_thread: |
| 298 | logging.debug("Terminating screendump thread...") |
| 299 | _screendump_thread_termination_event.set() |
| 300 | _screendump_thread.join(10) |
lmr | 327729b | 2010-06-14 17:14:00 +0000 | [diff] [blame] | 301 | _screendump_thread = None |
| 302 | _screendump_thread_termination_event = None |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 303 | |
lmr | 665975c | 2009-12-27 20:01:41 +0000 | [diff] [blame] | 304 | # Warn about corrupt PPM files |
| 305 | for f in glob.glob(os.path.join(test.debugdir, "*.ppm")): |
| 306 | if not ppm_utils.image_verify_ppm_file(f): |
| 307 | logging.warn("Found corrupt PPM file: %s", f) |
| 308 | |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 309 | # Should we convert PPM files to PNG format? |
| 310 | if params.get("convert_ppm_files_to_png") == "yes": |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 311 | logging.debug("'convert_ppm_files_to_png' specified; converting PPM " |
| 312 | "files to PNG format...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 313 | try: |
| 314 | for f in glob.glob(os.path.join(test.debugdir, "*.ppm")): |
lmr | 15c4486 | 2009-09-10 03:23:45 +0000 | [diff] [blame] | 315 | if ppm_utils.image_verify_ppm_file(f): |
| 316 | new_path = f.replace(".ppm", ".png") |
| 317 | image = PIL.Image.open(f) |
| 318 | image.save(new_path, format='PNG') |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 319 | except NameError: |
| 320 | pass |
lmr | 0ee0a9c | 2009-07-24 19:23:29 +0000 | [diff] [blame] | 321 | |
| 322 | # Should we keep the PPM files? |
| 323 | if params.get("keep_ppm_files") != "yes": |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 324 | logging.debug("'keep_ppm_files' not specified; removing all PPM files " |
| 325 | "from debug dir...") |
lmr | e9f528e | 2009-08-12 15:18:10 +0000 | [diff] [blame] | 326 | for f in glob.glob(os.path.join(test.debugdir, '*.ppm')): |
| 327 | os.unlink(f) |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 328 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 329 | # Should we keep the screendump dirs? |
| 330 | if params.get("keep_screendumps") != "yes": |
| 331 | logging.debug("'keep_screendumps' not specified; removing screendump " |
| 332 | "dirs...") |
| 333 | for d in glob.glob(os.path.join(test.debugdir, "screendumps_*")): |
| 334 | if os.path.isdir(d) and not os.path.islink(d): |
| 335 | shutil.rmtree(d, ignore_errors=True) |
lmr | 86d1ea5 | 2009-06-15 20:34:39 +0000 | [diff] [blame] | 336 | |
lmr | 588f497 | 2009-10-13 13:43:10 +0000 | [diff] [blame] | 337 | # Kill all unresponsive VMs |
| 338 | if params.get("kill_unresponsive_vms") == "yes": |
| 339 | logging.debug("'kill_unresponsive_vms' specified; killing all VMs " |
| 340 | "that fail to respond to a remote login request...") |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 341 | for vm in env.get_all_vms(): |
lmr | 588f497 | 2009-10-13 13:43:10 +0000 | [diff] [blame] | 342 | if vm.is_alive(): |
| 343 | session = vm.remote_login() |
| 344 | if session: |
| 345 | session.close() |
| 346 | else: |
| 347 | vm.destroy(gracefully=False) |
| 348 | |
lmr | ee4338e | 2010-07-08 23:40:10 +0000 | [diff] [blame] | 349 | # Kill all kvm_subprocess tail threads |
| 350 | kvm_subprocess.kill_tail_threads() |
lmr | a419700 | 2009-08-13 05:00:51 +0000 | [diff] [blame] | 351 | |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 352 | # Terminate tcpdump if no VMs are alive |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 353 | living_vms = [vm for vm in env.get_all_vms() if vm.is_alive()] |
lmr | 8a47ce3 | 2010-03-23 15:27:12 +0000 | [diff] [blame] | 354 | if not living_vms and "tcpdump" in env: |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 355 | env["tcpdump"].close() |
| 356 | del env["tcpdump"] |
| 357 | |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 358 | # Execute any post_commands |
| 359 | if params.get("post_command"): |
| 360 | process_command(test, params, env, params.get("post_command"), |
| 361 | int(params.get("post_command_timeout", "600")), |
| 362 | params.get("post_command_noncritical") == "yes") |
| 363 | |
lmr | 6f669ce | 2009-05-31 19:02:42 +0000 | [diff] [blame] | 364 | |
| 365 | def postprocess_on_error(test, params, env): |
| 366 | """ |
| 367 | Perform postprocessing operations required only if the test failed. |
| 368 | |
| 369 | @param test: An Autotest test object. |
| 370 | @param params: A dict containing all VM and image parameters. |
| 371 | @param env: The environment (a dict-like object). |
| 372 | """ |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 373 | params.update(params.object_params("on_error")) |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 374 | |
| 375 | |
| 376 | def _update_address_cache(address_cache, line): |
| 377 | if re.search("Your.IP", line, re.IGNORECASE): |
| 378 | matches = re.findall(r"\d*\.\d*\.\d*\.\d*", line) |
| 379 | if matches: |
| 380 | address_cache["last_seen"] = matches[0] |
| 381 | if re.search("Client.Ethernet.Address", line, re.IGNORECASE): |
| 382 | matches = re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) |
| 383 | if matches and address_cache.get("last_seen"): |
| 384 | mac_address = matches[0].lower() |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 385 | if time.time() - address_cache.get("time_%s" % mac_address, 0) > 5: |
| 386 | logging.debug("(address cache) Adding cache entry: %s ---> %s", |
| 387 | mac_address, address_cache.get("last_seen")) |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 388 | address_cache[mac_address] = address_cache.get("last_seen") |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 389 | address_cache["time_%s" % mac_address] = time.time() |
lmr | 965bcd2 | 2009-08-13 04:12:19 +0000 | [diff] [blame] | 390 | del address_cache["last_seen"] |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 391 | |
| 392 | |
| 393 | def _take_screendumps(test, params, env): |
| 394 | global _screendump_thread_termination_event |
| 395 | temp_dir = test.debugdir |
| 396 | if params.get("screendump_temp_dir"): |
| 397 | temp_dir = kvm_utils.get_path(test.bindir, |
| 398 | params.get("screendump_temp_dir")) |
| 399 | try: |
| 400 | os.makedirs(temp_dir) |
| 401 | except OSError: |
| 402 | pass |
| 403 | temp_filename = os.path.join(temp_dir, "scrdump-%s.ppm" % |
| 404 | kvm_utils.generate_random_string(6)) |
| 405 | delay = float(params.get("screendump_delay", 5)) |
| 406 | quality = int(params.get("screendump_quality", 30)) |
| 407 | |
| 408 | cache = {} |
| 409 | |
| 410 | while True: |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 411 | for vm in env.get_all_vms(): |
lmr | 7fa9fe7 | 2010-06-22 01:46:47 +0000 | [diff] [blame] | 412 | if not vm.is_alive(): |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 413 | continue |
lmr | 9e964a0 | 2010-06-18 03:46:21 +0000 | [diff] [blame] | 414 | try: |
| 415 | vm.monitor.screendump(temp_filename) |
| 416 | except kvm_monitor.MonitorError, e: |
| 417 | logging.warn(e) |
| 418 | continue |
lmr | 0e4056d | 2010-04-01 02:58:01 +0000 | [diff] [blame] | 419 | if not os.path.exists(temp_filename): |
| 420 | logging.warn("VM '%s' failed to produce a screendump", vm.name) |
| 421 | continue |
| 422 | if not ppm_utils.image_verify_ppm_file(temp_filename): |
| 423 | logging.warn("VM '%s' produced an invalid screendump", vm.name) |
| 424 | os.unlink(temp_filename) |
| 425 | continue |
| 426 | screendump_dir = os.path.join(test.debugdir, |
| 427 | "screendumps_%s" % vm.name) |
| 428 | try: |
| 429 | os.makedirs(screendump_dir) |
| 430 | except OSError: |
| 431 | pass |
| 432 | screendump_filename = os.path.join(screendump_dir, |
| 433 | "%s_%s.jpg" % (vm.name, |
| 434 | time.strftime("%Y-%m-%d_%H-%M-%S"))) |
| 435 | hash = utils.hash_file(temp_filename) |
| 436 | if hash in cache: |
| 437 | try: |
| 438 | os.link(cache[hash], screendump_filename) |
| 439 | except OSError: |
| 440 | pass |
| 441 | else: |
| 442 | try: |
| 443 | image = PIL.Image.open(temp_filename) |
| 444 | image.save(screendump_filename, format="JPEG", quality=quality) |
| 445 | cache[hash] = screendump_filename |
| 446 | except NameError: |
| 447 | pass |
| 448 | os.unlink(temp_filename) |
| 449 | if _screendump_thread_termination_event.isSet(): |
| 450 | break |
| 451 | _screendump_thread_termination_event.wait(delay) |