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