def get_speed(exp_migrate_speed): """ Get migration speed and compare with value in exp_migrate_speed :params exp_migrate_speed: the dict of expected migration speed :raise: test.fail if speed does not match """ if exp_migrate_speed.get("precopy_speed"): output = virsh.migrate_getspeed(vm_name, **virsh_args).stdout_text.strip() if exp_migrate_speed['precopy_speed'] != output: virsh.migrate_getspeed(vm_name, extra="--postcopy", **virsh_args) test.fail("Migration speed is expected to be '%s MiB/s', but " "'%s MiB/s' found!" % (exp_migrate_speed['precopy_speed'], output)) if exp_migrate_speed.get("postcopy_speed"): output = virsh.migrate_getspeed(vm_name, extra="--postcopy", **virsh_args).stdout_text.strip() if exp_migrate_speed['postcopy_speed'] != output: test.fail("Prostcopy migration speed is expected to be '%s " "MiB/s', but '%s MiB/s' found!" % (exp_migrate_speed['postcopy_speed'], output))
def control_migrate_speed(to_speed=1, opts=""): """ Control migration duration :param to_speed: the speed value in Mbps to be set for migration :return int: the new migration speed after setting """ virsh_args.update({"ignore_status": False}) old_speed = virsh.migrate_getspeed(vm_name, extra=opts, **virsh_args) logging.debug("Current migration speed is %s MiB/s\n", old_speed.stdout.strip()) logging.debug("Set migration speed to %d MiB/s\n", to_speed) cmd_result = virsh.migrate_setspeed(vm_name, to_speed, extra=opts, **virsh_args) actual_speed = virsh.migrate_getspeed(vm_name, extra=opts, **virsh_args) logging.debug("New migration speed is %s MiB/s\n", actual_speed.stdout.strip()) return int(actual_speed.stdout.strip())
def control_migrate_speed(to_speed=1): """ Control migration duration :param to_speed: the speed value in Mbps to be set for migration :return int: the new migration speed after setting """ virsh_args.update({"ignore_status": False}) old_speed = virsh.migrate_getspeed(vm_name, **virsh_args) logging.debug("Current migration speed is %s MiB/s\n", old_speed.stdout.strip()) logging.debug("Set migration speed to %d MiB/s\n", to_speed) cmd_result = virsh.migrate_setspeed(vm_name, to_speed, "", **virsh_args) actual_speed = virsh.migrate_getspeed(vm_name, **virsh_args) logging.debug("New migration speed is %s MiB/s\n", actual_speed.stdout.strip()) return int(actual_speed.stdout.strip())
def set_get_speed(vm_name, expected_value, status_error=False, options_extra="", **virsh_dargs): """Set speed and check its result""" result = virsh.migrate_setspeed(vm_name, expected_value, options_extra, **virsh_dargs) status = result.exit_status err = result.stderr.strip() # Check status_error if status_error: if status == 0 or err == "": # Without code for bz1083483 applied, this will succeed # when it shouldn't be succeeding. if bz1083483 and not libvirt_version.version_compare(1, 2, 4): raise error.TestNAError("bz1083483 should result in fail") else: raise error.TestFail("Expect fail, but run successfully!") # no need to perform getspeed if status_error is true return else: if status != 0 or err != "": raise error.TestFail("Run failed with right " "virsh migrate-setspeed command") result = virsh.migrate_getspeed(vm_name, **virsh_dargs) status = result.exit_status actual_value = result.stdout.strip() err = result.stderr.strip() if status != 0 or err != "": raise error.TestFail("Run failed with virsh migrate-getspeed") logging.info("The expected bandwidth is %s MiB/s, " "the actual bandwidth is %s MiB/s" % (expected_value, actual_value)) if int(actual_value) != int(expected_value): raise error.TestFail("Bandwidth value from getspeed " "is different from expected value " "set by setspeed")
def _set_speed(extra_option=''): """ Inner function to set migration speed :param extra_option: str, it might include '--postcopy' or not """ virsh_args = {"ignore_status": False} old_speed = virsh.migrate_getspeed(vm_name, extra=extra_option, **virsh_args) logging.debug("Current %s migration speed is %s " "MiB/s\n", extra_option, old_speed.stdout_text.strip()) logging.debug("Set %s migration speed to %d " "MiB/s\n", extra_option, to_speed) virsh.migrate_setspeed(vm_name, to_speed, extra=extra_option, **virsh_args)
def run(test, params, env): """ Test command: virsh migrate-setspeed <domain> <bandwidth> virsh migrate-getspeed <domain>. 1) Prepare test environment. 2) Try to set the maximum migration bandwidth (in MiB/s) for a domain through valid and invalid command. 3) Recover test environment. 4) Check result. """ # MAIN TEST CODE ### # Process cartesian parameters vm_name = params.get("main_vm") bandwidth = params.get("bandwidth", "default") options_extra = params.get("options_extra", "") status_error = "yes" == params.get("status_error", "yes") virsh_dargs = {'debug': True} # Checking uris for migration twice_migration = "yes" == params.get("twice_migration", "no") if twice_migration: src_uri = params.get("migrate_src_uri", "qemu+ssh://EXAMPLE/system") dest_uri = params.get("migrate_dest_uri", "qemu+ssh://EXAMPLE/system") if src_uri.count('///') or src_uri.count('EXAMPLE'): raise error.TestNAError("The src_uri '%s' is invalid" % src_uri) if dest_uri.count('///') or dest_uri.count('EXAMPLE'): raise error.TestNAError("The dest_uri '%s' is invalid" % dest_uri) bz1083483 = False if bandwidth == "zero": expected_value = 0 elif bandwidth == "one": expected_value = 1 elif bandwidth == "negative": expected_value = -1 bz1083483 = True elif bandwidth == "default": expected_value = DEFAULT elif bandwidth == "UINT32_MAX": expected_value = UINT32_MiB elif bandwidth == "INT64_MAX": expected_value = INT64_MiB elif bandwidth == "UINT64_MAX": expected_value = UINT64_MiB bz1083483 = True elif bandwidth == "INVALID_VALUE": expected_value = INT64_MiB + 1 bz1083483 = True else: expected_value = bandwidth orig_value = virsh.migrate_getspeed(vm_name).stdout.strip() def set_get_speed(vm_name, expected_value, status_error=False, options_extra="", **virsh_dargs): """Set speed and check its result""" result = virsh.migrate_setspeed(vm_name, expected_value, options_extra, **virsh_dargs) status = result.exit_status err = result.stderr.strip() # Check status_error if status_error: if status == 0 or err == "": # Without code for bz1083483 applied, this will succeed # when it shouldn't be succeeding. if bz1083483 and not libvirt_version.version_compare(1, 2, 4): raise error.TestNAError("bz1083483 should result in fail") else: raise error.TestFail("Expect fail, but run successfully!") # no need to perform getspeed if status_error is true return else: if status != 0 or err != "": raise error.TestFail("Run failed with right " "virsh migrate-setspeed command") result = virsh.migrate_getspeed(vm_name, **virsh_dargs) status = result.exit_status actual_value = result.stdout.strip() err = result.stderr.strip() if status != 0 or err != "": raise error.TestFail("Run failed with virsh migrate-getspeed") logging.info("The expected bandwidth is %s MiB/s, " "the actual bandwidth is %s MiB/s" % (expected_value, actual_value)) if int(actual_value) != int(expected_value): raise error.TestFail("Bandwidth value from getspeed " "is different from expected value " "set by setspeed") def verify_migration_speed(test, params, env): """ Check if migration speed is effective with twice migration. """ vms = env.get_all_vms() src_uri = params.get("migrate_src_uri", "qemu+ssh://EXAMPLE/system") dest_uri = params.get("migrate_dest_uri", "qemu+ssh://EXAMPLE/system") if src_uri.count('///') or src_uri.count('EXAMPLE'): raise error.TestNAError("The src_uri '%s' is invalid", src_uri) if dest_uri.count('///') or dest_uri.count('EXAMPLE'): raise error.TestNAError("The dest_uri '%s' is invalid", dest_uri) remote_host = params.get("migrate_dest_host") username = params.get("migrate_dest_user", "root") password = params.get("migrate_dest_pwd") # Config ssh autologin for remote host ssh_key.setup_ssh_key(remote_host, username, password, port=22) # Check migrated vms' state for vm in vms: if vm.is_dead(): vm.start() load_vm_names = params.get("load_vms").split() # vms for load load_vms = [] for vm_name in load_vm_names: load_vms.append(libvirt_vm.VM(vm_name, params, test.bindir, env.get("address_cache"))) bandwidth = int(params.get("bandwidth", "4")) stress_type = params.get("stress_type", "load_vms_booting") migration_type = params.get("migration_type", "orderly") thread_timeout = int(params.get("thread_timeout", "60")) delta = float(params.get("allowed_delta", "0.1")) virsh_migrate_timeout = int(params.get("virsh_migrate_timeout", 60)) # virsh migrate options virsh_migrate_options = "--live --timeout %s", virsh_migrate_timeout # Migrate vms to remote host mig_first = utlv.MigrationTest() virsh_dargs = {"debug": True} for vm in vms: set_get_speed(vm.name, bandwidth, virsh_dargs=virsh_dargs) vm.wait_for_login() utils_test.load_stress(stress_type, vms, params) mig_first.do_migration(vms, src_uri, dest_uri, migration_type, options=virsh_migrate_options, thread_timeout=thread_timeout) for vm in vms: mig_first.cleanup_dest_vm(vm, None, dest_uri) # Keep it clean for second migration if vm.is_alive(): vm.destroy() # Migrate vms again with new bandwidth second_bandwidth = params.get("second_bandwidth", "times") if second_bandwidth == "half": second_bandwidth = bandwidth / 2 speed_times = 2 elif second_bandwidth == "times": second_bandwidth = bandwidth * 2 speed_times = 0.5 elif second_bandwidth == "same": second_bandwidth = bandwidth speed_times = 1 # Migrate again for vm in vms: if vm.is_dead(): vm.start() vm.wait_for_login() set_get_speed(vm.name, second_bandwidth, virsh_dargs=virsh_dargs) utils_test.load_stress(stress_type, vms, params) mig_second = utlv.MigrationTest() mig_second.do_migration(vms, src_uri, dest_uri, migration_type, options="--live", thread_timeout=thread_timeout) for vm in vms: mig_second.cleanup_dest_vm(vm, None, dest_uri) fail_info = [] # Check whether migration failed if len(fail_info): raise error.TestFail(fail_info) for vm in vms: first_time = mig_first.mig_time[vm.name] second_time = mig_second.mig_time[vm.name] logging.debug("Migration time for %s:\n" "Time with Bandwidth '%s' first: %s\n" "Time with Bandwidth '%s' second: %s", vm.name, bandwidth, first_time, second_bandwidth, second_time) shift = float(abs(first_time * speed_times - second_time)) / float(second_time) logging.debug("Shift:%s", shift) if delta < shift: fail_info.append("Spent time for migrating %s is intolerable." % vm.name) # Check again for speed result if len(fail_info): raise error.TestFail(fail_info) # Run test case try: set_get_speed(vm_name, expected_value, status_error, options_extra, **virsh_dargs) if twice_migration: verify_migration_speed(test, params, env) else: set_get_speed(vm_name, expected_value, status_error, options_extra, **virsh_dargs) finally: #restore bandwidth to default virsh.migrate_setspeed(vm_name, orig_value) if twice_migration: for vm in env.get_all_vms(): utlv.MigrationTest().cleanup_dest_vm(vm, src_uri, dest_uri) if vm.is_alive(): vm.destroy(gracefully=False)
def run(test, params, env): """ Test command: virsh domjobabort. The command can abort the currently running domain job. 1.Prepare test environment,destroy or suspend a VM. 2.Do action to get a subprocess(dump, save, managedsave). 3.Perform virsh domjobabort operation to abort VM's job. 4.Recover the VM's status and wait for the subprocess over. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) start_vm = params.get("start_vm") pre_vm_state = params.get("pre_vm_state", "start") if start_vm == "no" and vm.is_alive(): vm.destroy() # Instead of "paused_after_start_vm", use "pre_vm_state". # After start the VM, wait for some time to make sure the job # can be created on this domain. if start_vm == "yes": vm.wait_for_login() if params.get("pre_vm_state") == "suspend": vm.pause() domid = vm.get_id() domuuid = vm.get_uuid() original_speed = virsh.migrate_getspeed(vm_name).stdout.strip() def get_subprocess(action, vm_name, file, remote_uri=None): """ Execute background virsh command, return subprocess w/o waiting for exit() :param cmd : virsh command. :param guest_name : VM's name :param file_source : virsh command's file option. """ args = "" if action == "managedsave": file = "" elif action == "migrate": # Slow down migration for domjobabort virsh.migrate_setspeed(vm_name, "1") file = remote_uri args = "--unsafe" command = "virsh %s %s %s %s" % (action, vm_name, file, args) logging.debug("Action: %s", command) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return p action = params.get("jobabort_action", "dump") dump_opt = params.get("dump_opt", None) status_error = params.get("status_error", "no") job = params.get("jobabort_job", "yes") tmp_file = os.path.join(data_dir.get_tmp_dir(), "domjobabort.tmp") tmp_pipe = os.path.join(data_dir.get_tmp_dir(), "domjobabort.fifo") vm_ref = params.get("jobabort_vm_ref") remote_uri = params.get("jobabort_remote_uri") remote_host = params.get("migrate_dest_host") remote_user = params.get("migrate_dest_user", "root") remote_pwd = params.get("migrate_dest_pwd") saved_data = None # Build job action if dump_opt: action = "dump --crash" if action == "managedsave": tmp_pipe = '/var/lib/libvirt/qemu/save/%s.save' % vm.name if action == "restore": virsh.save(vm_name, tmp_file, ignore_status=True) if action == "migrate": if remote_host.count("EXAMPLE"): test.cancel("Remote host should be configured " "for migrate.") else: # Config ssh autologin for remote host ssh_key.setup_ssh_key(remote_host, remote_user, remote_pwd, port=22) if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = vm_name # Get the subprocess of VM. # The command's effect is to abort the currently running domain job. # So before do "domjobabort" action, we must create a job on the domain. process = None if job == "yes" and start_vm == "yes" and status_error == "no": if os.path.exists(tmp_pipe): os.unlink(tmp_pipe) os.mkfifo(tmp_pipe) process = get_subprocess(action, vm_name, tmp_pipe, remote_uri) saved_data = None if action == "restore": with open(tmp_file, 'r') as tmp_f: saved_data = tmp_f.read(10 * 1024 * 1024) f = open(tmp_pipe, 'w') f.write(saved_data[:1024 * 1024]) elif action == "migrate": f = None else: f = open(tmp_pipe, 'rb') dummy = f.read(1024 * 1024).decode(locale.getpreferredencoding(), 'ignore') # Give enough time for starting job t = 0 while t < 5: jobtype = vm.get_job_type() if "None" == jobtype: t += 1 time.sleep(1) continue elif jobtype is False: logging.error("Get job type failed.") break else: logging.debug("Job started: %s", jobtype) break ret = virsh.domjobabort(vm_ref, ignore_status=True, debug=True) status = ret.exit_status if process and f: if saved_data: f.write(saved_data[1024 * 1024:]) else: dummy = f.read() f.close() try: os.unlink(tmp_pipe) except OSError as detail: logging.info("Can't remove %s: %s", tmp_pipe, detail) try: os.unlink(tmp_file) except OSError as detail: logging.info("Cant' remove %s: %s", tmp_file, detail) # Recover the environment. if pre_vm_state == "suspend": vm.resume() if process: if process.poll(): try: process.kill() except OSError: pass if action == "migrate": # Recover migration speed virsh.migrate_setspeed(vm_name, original_speed) utlv.MigrationTest().cleanup_dest_vm(vm, None, remote_uri) # check status_error if status_error == "yes": if status == 0: test.fail("Run successfully with wrong command!") elif status_error == "no": if status != 0: test.fail("Run failed with right command")
def run(test, params, env): """ Test command: virsh migrate-setspeed <domain> <bandwidth> virsh migrate-getspeed <domain>. 1) Prepare test environment. 2) Try to set the maximum migration bandwidth (in MiB/s) for a domain through valid and invalid command. 3) Recover test environment. 4) Check result. """ # MAIN TEST CODE ### # Process cartesian parameters vm_name = params.get("migrate_main_vm") bandwidth = params.get("bandwidth", "default") options_extra = params.get("options_extra", "") status_error = "yes" == params.get("status_error", "yes") virsh_dargs = {'debug': True} # Checking uris for migration twice_migration = "yes" == params.get("twice_migration", "no") if twice_migration: src_uri = params.get("migrate_src_uri", "qemu+ssh://EXAMPLE/system") dest_uri = params.get("migrate_dest_uri", "qemu+ssh://EXAMPLE/system") if src_uri.count('///') or src_uri.count('EXAMPLE'): raise error.TestNAError("The src_uri '%s' is invalid" % src_uri) if dest_uri.count('///') or dest_uri.count('EXAMPLE'): raise error.TestNAError("The dest_uri '%s' is invalid" % dest_uri) bz1083483 = False if bandwidth == "zero": expected_value = 0 elif bandwidth == "one": expected_value = 1 elif bandwidth == "negative": expected_value = -1 bz1083483 = True elif bandwidth == "default": expected_value = DEFAULT elif bandwidth == "UINT32_MAX": expected_value = UINT32_MiB elif bandwidth == "INT64_MAX": expected_value = INT64_MiB elif bandwidth == "UINT64_MAX": expected_value = UINT64_MiB bz1083483 = True elif bandwidth == "INVALID_VALUE": expected_value = INT64_MiB + 1 bz1083483 = True else: expected_value = bandwidth orig_value = virsh.migrate_getspeed(vm_name).stdout.strip() def set_get_speed(vm_name, expected_value, status_error=False, options_extra="", **virsh_dargs): """Set speed and check its result""" result = virsh.migrate_setspeed(vm_name, expected_value, options_extra, **virsh_dargs) status = result.exit_status err = result.stderr.strip() # Check status_error if status_error: if status == 0 or err == "": # Without code for bz1083483 applied, this will succeed # when it shouldn't be succeeding. if bz1083483 and not libvirt_version.version_compare(1, 2, 4): raise error.TestNAError("bz1083483 should result in fail") else: raise error.TestFail("Expect fail, but run successfully!") # no need to perform getspeed if status_error is true return else: if status != 0 or err != "": raise error.TestFail("Run failed with right " "virsh migrate-setspeed command") result = virsh.migrate_getspeed(vm_name, **virsh_dargs) status = result.exit_status actual_value = result.stdout.strip() err = result.stderr.strip() if status != 0 or err != "": raise error.TestFail("Run failed with virsh migrate-getspeed") logging.info("The expected bandwidth is %s MiB/s, " "the actual bandwidth is %s MiB/s" % (expected_value, actual_value)) if int(actual_value) != int(expected_value): raise error.TestFail("Bandwidth value from getspeed " "is different from expected value " "set by setspeed") def verify_migration_speed(test, params, env): """ Check if migration speed is effective with twice migration. """ vms = env.get_all_vms() src_uri = params.get("migrate_src_uri", "qemu+ssh://EXAMPLE/system") dest_uri = params.get("migrate_dest_uri", "qemu+ssh://EXAMPLE/system") if not len(vms): raise error.TestNAError("Please provide migrate_vms for test.") if src_uri.count('///') or src_uri.count('EXAMPLE'): raise error.TestNAError("The src_uri '%s' is invalid" % src_uri) if dest_uri.count('///') or dest_uri.count('EXAMPLE'): raise error.TestNAError("The dest_uri '%s' is invalid" % dest_uri) remote_host = params.get("migrate_dest_host") username = params.get("migrate_dest_user", "root") password = params.get("migrate_dest_pwd") # Config ssh autologin for remote host ssh_key.setup_ssh_key(remote_host, username, password, port=22) # Check migrated vms' state for vm in vms: if vm.is_dead(): vm.start() load_vm_names = params.get("load_vms").split() # vms for load load_vms = [] for vm_name in load_vm_names: load_vms.append( libvirt_vm.VM(vm_name, params, test.bindir, env.get("address_cache"))) params["load_vms"] = load_vms bandwidth = int(params.get("bandwidth", "4")) stress_type = params.get("stress_type", "load_vms_booting") migration_type = params.get("migration_type", "orderly") thread_timeout = int(params.get("thread_timeout", "60")) delta = float(params.get("allowed_delta", "0.1")) virsh_migrate_timeout = int(params.get("virsh_migrate_timeout", "60")) # virsh migrate options virsh_migrate_options = "--live --unsafe --timeout %s" % virsh_migrate_timeout # Migrate vms to remote host mig_first = utlv.MigrationTest() virsh_dargs = {"debug": True} for vm in vms: set_get_speed(vm.name, bandwidth, virsh_dargs=virsh_dargs) vm.wait_for_login() utils_test.load_stress(stress_type, vms, params) mig_first.do_migration(vms, src_uri, dest_uri, migration_type, options=virsh_migrate_options, thread_timeout=thread_timeout) for vm in vms: mig_first.cleanup_dest_vm(vm, None, dest_uri) # Keep it clean for second migration if vm.is_alive(): vm.destroy() # Migrate vms again with new bandwidth second_bandwidth = params.get("second_bandwidth", "times") if second_bandwidth == "half": second_bandwidth = bandwidth / 2 speed_times = 2 elif second_bandwidth == "times": second_bandwidth = bandwidth * 2 speed_times = 0.5 elif second_bandwidth == "same": second_bandwidth = bandwidth speed_times = 1 # Migrate again for vm in vms: if vm.is_dead(): vm.start() vm.wait_for_login() set_get_speed(vm.name, second_bandwidth, virsh_dargs=virsh_dargs) utils_test.load_stress(stress_type, vms, params) mig_second = utlv.MigrationTest() mig_second.do_migration(vms, src_uri, dest_uri, migration_type, options=virsh_migrate_options, thread_timeout=thread_timeout) for vm in vms: mig_second.cleanup_dest_vm(vm, None, dest_uri) fail_info = [] # Check whether migration failed if len(fail_info): raise error.TestFail(fail_info) for vm in vms: first_time = mig_first.mig_time[vm.name] second_time = mig_second.mig_time[vm.name] logging.debug( "Migration time for %s:\n" "Time with Bandwidth '%s' first: %s\n" "Time with Bandwidth '%s' second: %s", vm.name, bandwidth, first_time, second_bandwidth, second_time) shift = float(abs(first_time * speed_times - second_time)) / float(second_time) logging.debug("Shift:%s", shift) if delta < shift: fail_info.append( "Spent time for migrating %s is intolerable." % vm.name) # Check again for speed result if len(fail_info): raise error.TestFail(fail_info) # Run test case try: set_get_speed(vm_name, expected_value, status_error, options_extra, **virsh_dargs) if twice_migration: verify_migration_speed(test, params, env) else: set_get_speed(vm_name, expected_value, status_error, options_extra, **virsh_dargs) finally: #restore bandwidth to default virsh.migrate_setspeed(vm_name, orig_value) if twice_migration: for vm in env.get_all_vms(): utlv.MigrationTest().cleanup_dest_vm(vm, src_uri, dest_uri) if vm.is_alive(): vm.destroy(gracefully=False)
def run(test, params, env): """ Test command: virsh migrate-setspeed <domain> <bandwidth> virsh migrate-getspeed <domain>. 1) Prepare test environment. 2) Try to set the maximum migration bandwidth (in MiB/s) for a domain through valid and invalid command. 3) Recover test environment. 4) Check result. """ # MAIN TEST CODE ### # Process cartesian parameters vm_name = params.get("main_vm") bandwidth = params.get("bandwidth", "default") options_extra = params.get("options_extra", "") status_error = "yes" == params.get("status_error", "yes") virsh_dargs = {'debug': True} if bandwidth == "zero": expected_value = 0 elif bandwidth == "one": expected_value = 1 elif bandwidth == "default": expected_value = DEFAULT elif bandwidth == "UINT32_MAX": expected_value = UINT32_MAX elif bandwidth == "INT64_MAX": expected_value = INT64_MAX elif bandwidth == "UINT64_MAX": expected_value = UINT64_MAX elif bandwidth == "INVALID_VALUE": expected_value = UINT64_MAX + 1 else: expected_value = bandwidth orig_value = virsh.migrate_getspeed(vm_name).stdout.strip() # Run test case try: result = virsh.migrate_setspeed(vm_name, expected_value, options_extra, **virsh_dargs) status = result.exit_status err = result.stderr.strip() # Check status_error if status_error: if status == 0 or err == "": raise error.TestFail("Expect fail, but run successfully!") # no need to perform getspeed if status_error is true return else: if status != 0 or err != "": raise error.TestFail("Run failed with right " "virsh migrate-setspeed command") result = virsh.migrate_getspeed(vm_name, **virsh_dargs) status = result.exit_status actual_value = result.stdout.strip() err = result.stderr.strip() if status != 0 or err != "": raise error.TestFail("Run failed with virsh migrate-getspeed") logging.info("The expected bandwidth is %s MiB/s, " "the actual bandwidth is %s MiB/s" % (expected_value, actual_value)) if int(actual_value) != int(expected_value): raise error.TestFail("Bandwidth value from getspeed " "is different from expected value " "set by setspeed") finally: #restore bandwidth to default virsh.migrate_setspeed(vm_name, orig_value)
def run(test, params, env): """ Test command: virsh domjobabort. The command can abort the currently running domain job. 1.Prepare test environment,destroy or suspend a VM. 2.Do action to get a subprocess(dump, save, managedsave). 3.Perform virsh domjobabort operation to abort VM's job. 4.Recover the VM's status and wait for the subprocess over. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) start_vm = params.get("start_vm") pre_vm_state = params.get("pre_vm_state", "start") if start_vm == "no" and vm.is_alive(): vm.destroy() # Instead of "paused_after_start_vm", use "pre_vm_state". # After start the VM, wait for some time to make sure the job # can be created on this domain. if start_vm == "yes": vm.wait_for_login() if params.get("pre_vm_state") == "suspend": vm.pause() domid = vm.get_id() domuuid = vm.get_uuid() original_speed = virsh.migrate_getspeed(vm_name).stdout.strip() def get_subprocess(action, vm_name, file, remote_uri=None): """ Execute background virsh command, return subprocess w/o waiting for exit() :param cmd : virsh command. :param guest_name : VM's name :param file_source : virsh command's file option. """ if action == "managedsave": file = "" elif action == "migrate": # Slow down migration for domjobabort virsh.migrate_setspeed(vm_name, "1") file = remote_uri command = "virsh %s %s %s --unsafe" % (action, vm_name, file) logging.debug("Action: %s", command) p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return p action = params.get("jobabort_action", "dump") status_error = params.get("status_error", "no") job = params.get("jobabort_job", "yes") tmp_file = os.path.join(test.tmpdir, "domjobabort.tmp") tmp_pipe = os.path.join(test.tmpdir, "domjobabort.fifo") vm_ref = params.get("jobabort_vm_ref") remote_uri = params.get("jobabort_remote_uri") remote_host = params.get("migrate_dest_host") remote_user = params.get("migrate_dest_user", "root") remote_pwd = params.get("migrate_dest_pwd") saved_data = None if action == "managedsave": tmp_pipe = '/var/lib/libvirt/qemu/save/%s.save' % vm.name if action == "restore": virsh.save(vm_name, tmp_file, ignore_status=True) if action == "migrate": if remote_host.count("EXAMPLE"): raise error.TestNAError("Remote host should be configured " "for migrate.") else: # Config ssh autologin for remote host ssh_key.setup_ssh_key(remote_host, remote_user, remote_pwd, port=22) if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = vm_name # Get the subprocess of VM. # The command's effect is to abort the currently running domain job. # So before do "domjobabort" action, we must create a job on the domain. process = None if job == "yes" and start_vm == "yes" and status_error == "no": if os.path.exists(tmp_pipe): os.unlink(tmp_pipe) os.mkfifo(tmp_pipe) process = get_subprocess(action, vm_name, tmp_pipe, remote_uri) saved_data = None if action == "restore": saved_data = file(tmp_file, 'r').read(10 * 1024 * 1024) f = open(tmp_pipe, 'w') f.write(saved_data[:1024 * 1024]) elif action == "migrate": f = None else: f = open(tmp_pipe, 'r') dummy = f.read(1024 * 1024) # Give enough time for starting job t = 0 while t < 5: jobtype = vm.get_job_type() if "None" == jobtype: t += 1 time.sleep(1) continue elif jobtype is False: logging.error("Get job type failed.") break else: logging.debug("Job started: %s", jobtype) break ret = virsh.domjobabort(vm_ref, ignore_status=True, debug=True) status = ret.exit_status if process and f: if saved_data: f.write(saved_data[1024 * 1024:]) else: dummy = f.read() f.close() if os.path.exists(tmp_pipe): os.unlink(tmp_pipe) if os.path.exists(tmp_file): os.unlink(tmp_file) # Recover the environment. if pre_vm_state == "suspend": vm.resume() if process: if process.poll(): try: process.kill() except OSError: pass if action == "migrate": # Recover migration speed virsh.migrate_setspeed(vm_name, original_speed) utlv.MigrationTest().cleanup_dest_vm(vm, None, remote_uri) # check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")