def create_iso_image(params, name, prepare=True, file_size=None): """ Creates 'new' iso image with one file on it :param params: parameters for test :param name: name of new iso image file :param preapre: if True then it prepare cd images. :param file_size: Size of iso image in MB :return: path to new iso image file. """ error.context("Creating test iso image '%s'" % name, logging.info) cdrom_cd = params["target_cdrom"] cdrom_cd = params[cdrom_cd] if not os.path.isabs(cdrom_cd): cdrom_cd = utils_misc.get_path(data_dir.get_data_dir(), cdrom_cd) iso_image_dir = os.path.dirname(cdrom_cd) if file_size is None: file_size = 10 file_name = utils_misc.get_path(iso_image_dir, "%s.iso" % (name)) if prepare: cmd = "dd if=/dev/urandom of=%s bs=1M count=%d" utils.run(cmd % (name, file_size)) utils.run("mkisofs -o %s %s" % (file_name, name)) utils.run("rm -rf %s" % (name)) return file_name
def start(self): """ start block device committing job; """ base_image = self.params["base_image"] base_image = utils_misc.get_path(self.data_dir, base_image) top_image = self.params["top_image"] top_image = utils_misc.get_path(self.data_dir, top_image) default_speed = self.params.get("default_speed") backing_file = self.params.get("backing_file", None) if backing_file is not None: backing_file = utils_misc.get_path(self.data_dir, backing_file) if self.vm.monitor.protocol == "qmp": self.vm.monitor.clear_event("BLOCK_JOB_READY") self.vm.monitor.clear_event("BLOCK_JOB_COMPLETED") else: self.test.cancel("hmp command is not supportable.") logging.info("start to commit block device") self.vm.block_commit(self.device, default_speed, base_image, top_image, backing_file) status = self.get_status() if not status: self.test.fail("no active job found") logging.info( "block commit job running, with limited speed {0} B/s".format( default_speed))
def save_image(self, params, filename, root_dir=None): """ Save images to a path for later debugging. :param params: Dictionary containing the test parameters. :param filename: new filename for saved images. :param root_dir: directory for saved images. """ src = self.image_filename if root_dir is None: root_dir = os.path.dirname(src) backup_func = self.copy_data_file if params.get('image_raw_device') == 'yes': ifmt = params.get("image_format", "qcow2") src = utils_misc.get_path(root_dir, ("raw_device.%s" % ifmt)) backup_func = self.copy_data_raw backup_size = 0 if os.path.isfile(src): backup_size = os.path.getsize(src) s = os.statvfs(root_dir) image_dir_free_disk_size = s.f_bavail * s.f_bsize logging.info("Checking disk size on %s.", root_dir) if not self.is_disk_size_enough(backup_size, image_dir_free_disk_size): return backup_func(src, utils_misc.get_path(root_dir, filename))
def start(self): """ start block device committing job; """ base_image = self.params["base_image"] base_image = utils_misc.get_path(self.data_dir, base_image) top_image = self.params["top_image"] top_image = utils_misc.get_path(self.data_dir, top_image) default_speed = self.params.get("default_speed") backing_file = self.params.get("backing_file", None) if backing_file is not None: backing_file = utils_misc.get_path(self.data_dir, backing_file) if self.vm.monitor.protocol == "qmp": self.vm.monitor.clear_event("BLOCK_JOB_READY") self.vm.monitor.clear_event("BLOCK_JOB_COMPLETED") else: self.test.cancel("hmp command is not supportable.") logging.info("start to commit block device") self.vm.block_commit(self.device, default_speed, base_image, top_image, backing_file) status = self.get_status() if not status: self.test.fail("no active job found") logging.info("block commit job running, with limited speed {0} B/s".format(default_speed))
def rebase_test(cmd): """ Subcommand 'qemu-img rebase' test Change the backing file of a snapshot image in "unsafe mode": Assume the previous backing file had missed and we just have to change reference of snapshot to new one. After change the backing file of a snapshot image in unsafe mode, the snapshot should work still. :param cmd: qemu-img base command. """ if not 'rebase' in utils.system_output(cmd + ' --help', ignore_status=True): raise error.TestNAError("Current kvm user space version does not" " support 'rebase' subcommand") sn_fmt = params.get("snapshot_format", "qcow2") sn1 = params["image_name_snapshot1"] sn1 = utils_misc.get_path(data_dir.get_data_dir(), sn1) + ".%s" % sn_fmt base_img = storage.get_image_filename(params, data_dir.get_data_dir()) _create(cmd, sn1, sn_fmt, base_img=base_img, base_img_fmt=image_format) # Create snapshot2 based on snapshot1 sn2 = params["image_name_snapshot2"] sn2 = utils_misc.get_path(data_dir.get_data_dir(), sn2) + ".%s" % sn_fmt _create(cmd, sn2, sn_fmt, base_img=sn1, base_img_fmt=sn_fmt) rebase_mode = params.get("rebase_mode") if rebase_mode == "unsafe": os.remove(sn1) _rebase(cmd, sn2, base_img, image_format, mode=rebase_mode) # Boot snapshot image after rebase img_name, img_format = sn2.split('.') _boot(img_name, img_format) # Check sn2's format and backing_file actual_base_img = _info(cmd, sn2, "backing file") base_img_name = os.path.basename(base_img) if not base_img_name in actual_base_img: raise error.TestFail("After rebase the backing_file of 'sn2' is " "'%s' which is not expected as '%s'" % (actual_base_img, base_img_name)) status, output = _check(cmd, sn2) if not status: raise error.TestFail("Check image '%s' failed after rebase;" "got error: %s" % (sn2, output)) try: os.remove(sn2) os.remove(sn1) except Exception: pass
def rebase_test(cmd): """ Subcommand 'qemu-img rebase' test Change the backing file of a snapshot image in "unsafe mode": Assume the previous backing file had missed and we just have to change reference of snapshot to new one. After change the backing file of a snapshot image in unsafe mode, the snapshot should work still. :param cmd: qemu-img base command. """ if not 'rebase' in utils.system_output(cmd + ' --help', ignore_status=True): raise error.TestNAError("Current kvm user space version does not" " support 'rebase' subcommand") sn_fmt = params.get("snapshot_format", "qcow2") sn1 = params["image_name_snapshot1"] sn1 = utils_misc.get_path( data_dir.get_data_dir(), sn1) + ".%s" % sn_fmt base_img = storage.get_image_filename(params, data_dir.get_data_dir()) _create(cmd, sn1, sn_fmt, base_img=base_img, base_img_fmt=image_format) # Create snapshot2 based on snapshot1 sn2 = params["image_name_snapshot2"] sn2 = utils_misc.get_path( data_dir.get_data_dir(), sn2) + ".%s" % sn_fmt _create(cmd, sn2, sn_fmt, base_img=sn1, base_img_fmt=sn_fmt) rebase_mode = params.get("rebase_mode") if rebase_mode == "unsafe": os.remove(sn1) _rebase(cmd, sn2, base_img, image_format, mode=rebase_mode) # Boot snapshot image after rebase img_name, img_format = sn2.split('.') _boot(img_name, img_format) # Check sn2's format and backing_file actual_base_img = _info(cmd, sn2, "backing file") base_img_name = os.path.basename(base_img) if not base_img_name in actual_base_img: raise error.TestFail("After rebase the backing_file of 'sn2' is " "'%s' which is not expected as '%s'" % (actual_base_img, base_img_name)) status, output = _check(cmd, sn2) if not status: raise error.TestFail("Check image '%s' failed after rebase;" "got error: %s" % (sn2, output)) try: os.remove(sn2) os.remove(sn1) except Exception: pass
def create_iso_image(params, name, prepare=True, file_size=None): """ Creates 'new' iso image with one file on it :param params: parameters for test :param name: name of new iso image file :param preapre: if True then it prepare cd images. :param file_size: Size of iso image in MB :return: path to new iso image file. """ error_context.context("Creating test iso image '%s'" % name, logging.info) cdrom_cd = params["target_cdrom"] cdrom_cd = params[cdrom_cd] if not os.path.isabs(cdrom_cd): cdrom_cd = utils_misc.get_path(data_dir.get_data_dir(), cdrom_cd) iso_image_dir = os.path.dirname(cdrom_cd) if file_size is None: file_size = 10 g_mount_point = tempfile.mkdtemp("gluster") image_params = params.object_params(name) if image_params.get("enable_gluster") == "yes": if params.get("gluster_server"): gluster_server = params.get("gluster_server") else: gluster_server = "localhost" volume_name = params["gluster_volume_name"] g_mount_link = "%s:/%s" % (gluster_server, volume_name) mount_cmd = "mount -t glusterfs %s %s" % (g_mount_link, g_mount_point) process.system(mount_cmd, timeout=60) file_name = os.path.join(g_mount_point, "%s.iso" % name) else: file_name = utils_misc.get_path(iso_image_dir, "%s.iso" % name) if prepare: cmd = "dd if=/dev/urandom of=%s bs=1M count=%d" process.run(cmd % (name, file_size)) process.run("mkisofs -o %s %s" % (file_name, name)) process.run("rm -rf %s" % (name)) if image_params.get("enable_gluster") == "yes": gluster_uri = gluster.create_gluster_uri(image_params) file_name = "%s%s.iso" % (gluster_uri, name) try: umount_cmd = "umount %s" % g_mount_point process.system(umount_cmd, timeout=60) os.rmdir(g_mount_point) except Exception as err: msg = "Fail to clean up %s" % g_mount_point msg += "Error message %s" % err logging.warn(msg) return file_name
def copy_secureboot_pk_kek(self, pk_kek_filename): """ Copy SecureBootPkKek1.oemstr :return SecureBootPkKek1.oemstr path """ pk_kek_filepath = data_dir.get_deps_dir("edk2") pk_kek_src_path = utils_misc.get_path(pk_kek_filepath, pk_kek_filename) pk_kek_dst_path = "images/%s" % pk_kek_filename pk_kek_dst_path = utils_misc.get_path(data_dir.get_data_dir(), pk_kek_dst_path) cp_command = "cp -f %s %s" % (pk_kek_src_path, pk_kek_dst_path) process.system(cp_command) return pk_kek_dst_path
def run_qemu_img(test, params, env): """ 'qemu-img' functions test: 1) Judge what subcommand is going to be tested 2) Run subcommand test @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ cmd = utils_misc.get_path(test.bindir, params.get("qemu_img_binary")) if not os.path.exists(cmd): raise error.TestError("Binary of 'qemu-img' not found") image_format = params.get("image_format") image_size = params.get("image_size", "10G") image_name = storage.get_image_filename(params, data_dir.get_data_dir()) def _check(cmd, img): """ Simple 'qemu-img check' function implementation. @param cmd: qemu-img base command. @param img: image to be checked """ cmd += " check %s" % img logging.info("Checking image '%s'...", img) try: output = utils.system_output(cmd) except error.CmdError, e: if "does not support checks" in str(e): return (True, "") else: return (False, str(e)) return (True, output)
def check_test(cmd): """ Subcommand 'qemu-img check' test. This tests will 'dd' to create a specified size file, and check it. Then convert it to supported image_format in each loop and check again. @param cmd: qemu-img base command. """ test_image = utils_misc.get_path(data_dir.get_data_dir(), params.get("image_name_dd")) print "test_image = %s" % test_image create_image_cmd = params.get("create_image_cmd") create_image_cmd = create_image_cmd % test_image print "create_image_cmd = %s" % create_image_cmd utils.system(create_image_cmd) s, o = _check(cmd, test_image) if not s: raise error.TestFail("Check image '%s' failed with error: %s" % (test_image, o)) for fmt in params.get("supported_image_formats").split(): output_image = test_image + ".%s" % fmt _convert(cmd, fmt, test_image, output_image) s, o = _check(cmd, output_image) if not s: raise error.TestFail("Check image '%s' got error: %s" % (output_image, o)) os.remove(output_image) os.remove(test_image)
def get_image_info(image_name): """ Get the details of the volume(image) from qemu-img info """ qemu_img = utils_misc.get_path(test.bindir, params.get("qemu_img_binary")) if not os.path.exists(qemu_img): raise error.TestError("Binary of 'qemu-img' not found") cmd = "%s info %s" % (qemu_img, image_name) format_vol = utils.system_output(cmd) reg1 = re.compile(r'image:\s+(\S+)') reg2 = re.compile(r'file format:\s+(\S+)') reg3 = re.compile(r'virtual size:\s+(\S+.*)') reg4 = re.compile(r'disk size:\s+(\S+.*)') image_info = {} for line in format_vol.splitlines(): match1 = re.search(reg1, line) match2 = re.search(reg2, line) match3 = re.search(reg3, line) match4 = re.search(reg4, line) if match1 is not None: image_info['name'] = match1.group(1) if match2 is not None: image_info['format'] = match2.group(1) if match3 is not None: image_info['capacity'] = match3.group(1) if match4 is not None: image_info['allocation'] = match4.group(1) return image_info
def get_snapshot_file(self): """ Get path of snapshot file. """ image_format = self.params["image_format"] snapshot_file = "images/%s.%s" % (self.snapshot_file, image_format) return utils_misc.get_path(data_dir.get_data_dir(), snapshot_file)
def run(test, params, env): """ Negative test. Luks image creation with non_utf8_secret: 1. It should be failed to create the image. 2. The error information should be corret. e.g. Data from secret sec0 is not valid UTF-8 :param test: Qemu test object. :param params: Dictionary with the test parameters. :param env: Dictionary with test environment. """ image_stg_name = params["image_name_stg"] root_dir = data_dir.get_data_dir() image_stg_path = utils_misc.get_path(root_dir, image_stg_name) if os.path.exists(image_stg_path): os.remove(image_stg_path) err_info = params["err_info"] tmp_dir = data_dir.get_tmp_dir() non_utf8_secret_file = os.path.join(tmp_dir, "non_utf8_secret") non_utf8_secret = params["echo_non_utf8_secret_cmd"] % non_utf8_secret_file process.run(non_utf8_secret, shell=True) qemu_img_create_cmd = params["qemu_img_create_cmd"] % ( non_utf8_secret_file, image_stg_path) cmd_result = process.run(qemu_img_create_cmd, ignore_status=True, shell=True) if os.path.exists(image_stg_path): test.fail("The image '%s' should not exist. Since created" " it with non_utf8_secret." % image_stg_path) if not re.search(err_info, cmd_result.stderr.decode(), re.I): test.fail("Failed to get error information. The actual error " "information is %s." % cmd_result.stderr.decode())
def check_test(cmd): """ Subcommand 'qemu-img check' test. This tests will 'dd' to create a specified size file, and check it. Then convert it to supported image_format in each loop and check again. @param cmd: qemu-img base command. """ test_image = utils_misc.get_path(data_dir.get_data_dir(), params["image_name_dd"]) create_image_cmd = params["create_image_cmd"] create_image_cmd = create_image_cmd % test_image msg = " Create image %s by command %s" % (test_image, create_image_cmd) error.context(msg, logging.info) utils.system(create_image_cmd, verbose=False) status, output = _check(cmd, test_image) if not status: raise error.TestFail("Check image '%s' failed with error: %s" % (test_image, output)) for fmt in params["supported_image_formats"].split(): output_image = test_image + ".%s" % fmt _convert(cmd, fmt, test_image, output_image) status, output = _check(cmd, output_image) if not status: raise error.TestFail("Check image '%s' got error: %s" % (output_image, output)) os.remove(output_image) os.remove(test_image)
def _get_image_filename(img_name, enable_gluster=False, enable_nvme=False, img_fmt=None): """ Generate an image path. :param image_name: Force name of image. :param enable_gluster: Enable gluster or not. :param enable_nvme: Enable nvme or not. :param image_format: Format for image. """ if enable_gluster: gluster_uri = gluster.create_gluster_uri(params) image_filename = "%s%s" % (gluster_uri, img_name) if img_fmt: image_filename += ".%s" % img_fmt elif enable_nvme: image_filename = image_name else: if img_fmt: img_name = "%s.%s" % (img_name, img_fmt) image_filename = utils_misc.get_path(data_dir.get_data_dir(), img_name) return image_filename
def coredump_exists(mntpnt, files, out_dir): """ Check if there is specified file in the image If hit the files, copy them to output_dir The out_dir is the directory contains debug log :param mntpnt: The mountpoint on the host :param files: The files pattern need be checked :param out_dir: If found coredump files, copy them to the directory :return: Format as Bool, List which contains tuples If the checked file exists, return True, [("checked_file_name", "file_created_time")] If not, return False, [] """ file_exists = False msgs_return = [] for chk_file in files: file_need_check = utils_misc.get_path(mntpnt, chk_file) files_glob = glob.glob(file_need_check) if files_glob: file_exists = True for item in files_glob: file_ctime = time.ctime(os.path.getctime(item)) msgs_return.append((os.path.basename(item), file_ctime)) error.context("copy files %s %s" % (item, out_dir), logging.info) os.system("cp -rf %s %s" % (item, out_dir)) return file_exists, msgs_return
def run(test, params, env): """ Runs CPU rdrand test: :param test: QEMU test object. :param params: Dictionary with test parameters. :param env: Dictionary with the test environment. """ vm = env.get_vm(params["main_vm"]) session = vm.wait_for_login() test_bin = params['test_bin'] source_file = params['source_file'] guest_path = params['guest_path'] host_path = utils_misc.get_path(data_dir.get_deps_dir('rdrand'), source_file) vm.copy_files_to(host_path, '%s%s' % (guest_path, source_file)) if params['os_type'] == 'linux': build_cmd = params.get('build_cmd', 'cd %s; gcc -lrt %s -o %s') error_context.context("build binary file 'rdrand'", logging.info) session.cmd(build_cmd % (guest_path, source_file, test_bin)) s, o = session.cmd_status_output('%s%s' % (guest_path, test_bin)) session.cmd(params['delete_cmd']) if s != 0: test.fail('rdrand failed with status %s' % s) if params['os_type'] == 'linux': try: if not int(float(o)) in range(-101, 101): test.fail('rdrand output is %s, which is not expected' % o) except ValueError as e: test.fail('rdrand should output a float: %s' % str(e))
def clone_image(params, vm_name, image_name, root_dir): """ Clone master image to vm specific file. :param params: Dictionary containing the test parameters. :param vm_name: Vm name. :param image_name: Master image name. :param root_dir: Base directory for relative filenames. """ if not params.get("image_name_%s_%s" % (image_name, vm_name)): m_image_name = params.get("image_name", "image") vm_image_name = params.get("image_name_%s" % vm_name, "%s_%s" % (m_image_name, vm_name)) if params.get("clone_master", "yes") == "yes": image_params = params.object_params(image_name) image_params["image_name"] = vm_image_name master_image = params.get("master_image_name") if master_image: image_format = params.get("image_format", "qcow2") m_image_fn = "%s.%s" % (master_image, image_format) m_image_fn = utils_misc.get_path(root_dir, m_image_fn) else: m_image_fn = get_image_filename(params, root_dir) image_fn = get_image_filename(image_params, root_dir) force_clone = params.get("force_image_clone", "no") if not os.path.exists(image_fn) or force_clone == "yes": logging.info("Clone master image for vms.") process.run( params.get("image_clone_command") % (m_image_fn, image_fn)) params["image_name_%s" % vm_name] = vm_image_name params["image_name_%s_%s" % (image_name, vm_name)] = vm_image_name
def check_test(cmd): """ Subcommand 'qemu-img check' test. This tests will 'dd' to create a specified size file, and check it. Then convert it to supported image_format in each loop and check again. @param cmd: qemu-img base command. """ test_image = utils_misc.get_path(test.bindir, params.get("image_name_dd")) print "test_image = %s" % test_image create_image_cmd = params.get("create_image_cmd") create_image_cmd = create_image_cmd % test_image print "create_image_cmd = %s" % create_image_cmd utils.system(create_image_cmd) s, o = _check(cmd, test_image) if not s: raise error.TestFail("Check image '%s' failed with error: %s" % (test_image, o)) for fmt in params.get("supported_image_formats").split(): output_image = test_image + ".%s" % fmt _convert(cmd, fmt, test_image, output_image) s, o = _check(cmd, output_image) if not s: raise error.TestFail("Check image '%s' got error: %s" % (output_image, o)) os.remove(output_image) os.remove(test_image)
def get_iso_filename(cdrom_params, root_dir, basename=False): """ Generate an iso image path from params and root_dir. :param cdrom_params: Dictionary containing the test parameters. :param root_dir: Base directory for relative iso image. :param basename: True to use only basename of iso image. :return: iso filename """ enable_nvme = cdrom_params.get("enable_nvme") == "yes" enable_nbd = cdrom_params.get("enable_nbd") == "yes" enable_gluster = cdrom_params.get("enable_gluster") == "yes" enable_ceph = cdrom_params.get("enable_ceph") == "yes" enable_iscsi = cdrom_params.get("enable_iscsi") == "yes" enable_curl = cdrom_params.get("enable_curl") == "yes" enable_ssh = cdrom_params.get("enable_ssh") == "yes" if enable_nvme: return None elif any((enable_nbd, enable_gluster, enable_ceph, enable_iscsi, enable_curl, enable_ssh)): return get_image_filename(cdrom_params, None, basename) else: iso = cdrom_params.get("cdrom") if iso: iso = os.path.basename(iso) if basename else utils_misc.get_path( root_dir, iso) return iso
def copy_file_from_nfs(src, dst, mount_point, image_name): logging.info("Test failed before the install process start." " So just copy a good image from nfs for following tests.") utils_misc.mount(src, mount_point, "nfs", perm="ro") image_src = utils_misc.get_path(mount_point, image_name) shutil.copy(image_src, dst) utils_misc.umount(src, mount_point, "nfs")
def save_image(self, params, filename, root_dir=None): """ Save images to a path for later debugging. :param params: Dictionary containing the test parameters. :param filename: new filename for saved images. :param root_dir: directory for saved images. """ src = self.image_filename if root_dir is None: root_dir = os.path.dirname(src) backup_func = self.copy_data_file if self.is_remote_image(): backup_func = self.copy_data_remote elif params.get('image_raw_device') == 'yes': backup_func = self.copy_data_raw backup_size = 0 if os.path.isfile(src): backup_size = os.path.getsize(src) else: # TODO: get the size of block/remote images if self.size: backup_size += int( float( utils_numeric.normalize_data_size( self.size, order_magnitude="B"))) s = os.statvfs(root_dir) image_dir_free_disk_size = s.f_bavail * s.f_bsize logging.info("Checking disk size on %s.", root_dir) if not self.is_disk_size_enough(backup_size, image_dir_free_disk_size): return backup_func(src, utils_misc.get_path(root_dir, filename))
def check_tray_status_test(vm, qemu_cdrom_device, guest_cdrom_device, max_times): """ Test cdrom tray status reporting function. """ error.context("Copy test script to guest") tray_check_src = params.get("tray_check_src") if tray_check_src: tray_check_src = utils_misc.get_path(test.virtdir, "deps/%s" % tray_check_src) vm.copy_files_to(tray_check_src, "/tmp") if is_tray_opened(vm, qemu_cdrom_device) is None: logging.warn("Tray status reporting is not supported by qemu!") logging.warn("cdrom_test_tray_status test is skipped...") return error.context("Eject the cdrom in guest %s times" % max_times, logging.info) session = vm.wait_for_login(timeout=login_timeout) for i in range(1, max_times): session.cmd('eject %s' % guest_cdrom_device) if not is_tray_opened(vm, qemu_cdrom_device): raise error.TestFail("Monitor reports tray closed" " when ejecting (round %s)" % i) session.cmd('dd if=%s of=/dev/null count=1' % guest_cdrom_device) if is_tray_opened(vm, qemu_cdrom_device): raise error.TestFail("Monitor reports tray opened when reading" " cdrom in guest (round %s)" % i) time.sleep(workaround_eject_time)
def check_test(cmd): """ Subcommand 'qemu-img check' test. This tests will 'dd' to create a specified size file, and check it. Then convert it to supported image_format in each loop and check again. :param cmd: qemu-img base command. """ test_image = utils_misc.get_path(data_dir.get_data_dir(), params["image_name_dd"]) create_image_cmd = params["create_image_cmd"] create_image_cmd = create_image_cmd % test_image msg = " Create image %s by command %s" % (test_image, create_image_cmd) error.context(msg, logging.info) utils.system(create_image_cmd, verbose=False) status, output = _check(cmd, test_image) if not status: raise error.TestFail("Check image '%s' failed with error: %s" % (test_image, output)) for fmt in params["supported_image_formats"].split(): output_image = test_image + ".%s" % fmt _convert(cmd, fmt, test_image, output_image) status, output = _check(cmd, output_image) if not status: raise error.TestFail("Check image '%s' got error: %s" % (output_image, output)) os.remove(output_image) os.remove(test_image)
def run_qemu_img(test, params, env): """ 'qemu-img' functions test: 1) Judge what subcommand is going to be tested 2) Run subcommand test @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ cmd = utils_misc.get_path(test.bindir, params.get("qemu_img_binary")) if not os.path.exists(cmd): raise error.TestError("Binary of 'qemu-img' not found") image_format = params.get("image_format") image_size = params.get("image_size", "10G") image_name = storage.get_image_filename(params, test.bindir) def _check(cmd, img): """ Simple 'qemu-img check' function implementation. @param cmd: qemu-img base command. @param img: image to be checked """ cmd += " check %s" % img logging.info("Checking image '%s'...", img) try: output = utils.system_output(cmd) except error.CmdError, e: if "does not support checks" in str(e): return (True, "") else: return (False, str(e)) return (True, output)
def run_test(qemu_src_dir): """ run QEMU I/O test suite :qemu_src_dir: path of qemu source code """ iotests_root = params.get("iotests_root", "tests/qemu-iotests") extra_options = params.get("qemu_io_extra_options", "") image_format = params.get("qemu_io_image_format") result_pattern = params.get("iotests_result_pattern") error_context.context("running qemu-iotests for image format %s" % image_format, logging.info) os.environ["QEMU_PROG"] = utils_misc.get_qemu_binary(params) os.environ["QEMU_IMG_PROG"] = utils_misc.get_qemu_img_binary(params) os.environ["QEMU_IO_PROG"] = utils_misc.get_qemu_io_binary(params) os.environ["QEMU_NBD_PROG"] = utils_misc.get_binary('qemu-nbd', params) os.chdir(os.path.join(qemu_src_dir, iotests_root)) cmd = './check' if extra_options: cmd += " %s" % extra_options cmd += " -%s" % image_format output = process.system_output(cmd, ignore_status=True, shell=True) match = re.search(result_pattern, output, re.I | re.M) if match: iotests_log_file = "qemu_iotests_%s.log" % image_format iotests_log_file = utils_misc.get_path(test.debugdir, iotests_log_file) with open(iotests_log_file, 'w+') as log: log.write(output) log.flush() msg = "Total test %s cases, %s failed" raise exceptions.TestFail(msg % (match.group(2), match.group(1)))
def create_iso_image(params, name, prepare=True, file_size=None): """ Creates 'new' iso image with one file on it :param params: parameters for test :param name: name of new iso image file. It could be the full path of cdrom. :param preapre: if True then it prepare cd images. :param file_size: Size of iso image in MB :return: path to new iso image file. """ error.context("Creating test iso image '%s'" % name, logging.info) if not os.path.isabs(name): cdrom_path = utils_misc.get_path(data_dir.get_data_dir(), name) else: cdrom_path = name if not cdrom_path.endswith(".iso"): cdrom_path = "%s.iso" % cdrom_path name = os.path.basename(cdrom_path) if file_size is None: file_size = 10 if prepare: cmd = "dd if=/dev/urandom of=%s bs=1M count=%d" utils.run(cmd % (name, file_size)) utils.run("mkisofs -o %s %s" % (cdrom_path, name)) utils.run("rm -rf %s" % (name)) return cdrom_path
def copy_uefishell(self): """ Copy uefishell.iso :return uefishell.iso path """ ovmf_path = self.params["ovmf_path"] uefishell_filename = "UefiShell.iso" uefishell_dst_path = "images/%s" % uefishell_filename uefishell_src_path = utils_misc.get_path(ovmf_path, uefishell_filename) uefishell_dst_path = utils_misc.get_path(data_dir.get_data_dir(), uefishell_dst_path) if not os.path.exists(uefishell_dst_path): cp_command = "cp -f %s %s" % (uefishell_src_path, uefishell_dst_path) process.system(cp_command) return uefishell_dst_path
def run(test, params, env): vm = env.get_vm(params.get("vms", "main_vm").split(" ")[0]) vm.verify_alive() steps_filename = params.get("steps") if not steps_filename: image_name = os.path.basename(params["image_name"]) steps_filename = "steps/%s.steps" % image_name steps_filename = utils_misc.get_path(test.virtdir, steps_filename) if not os.path.exists(steps_filename): raise error.TestError("Steps file not found: %s" % steps_filename) sf = open(steps_filename, "r") lines = sf.readlines() sf.close() vm.resume() current_step_num = 0 current_screendump = None skip_current_step = False # Iterate over the lines in the file for line in lines: line = line.strip() if not line: continue logging.info(line) if line.startswith("#"): continue words = line.split() if words[0] == "step": current_step_num += 1 current_screendump = None skip_current_step = False elif words[0] == "screendump": current_screendump = words[1] elif skip_current_step: continue elif words[0] == "sleep": timeout_multiplier = float(params.get("timeout_multiplier") or 1) time.sleep(float(words[1]) * timeout_multiplier) elif words[0] == "key": vm.send_key(words[1]) elif words[0] == "var": if not handle_var(vm, params, words[1]): logging.error("Variable not defined: %s", words[1]) elif words[0] == "barrier_2": if current_screendump: scrdump_filename = os.path.join(ppm_utils.get_data_dir(steps_filename), current_screendump) else: scrdump_filename = None if not barrier_2(vm, words, params, test.debugdir, scrdump_filename, current_step_num): skip_current_step = True else: vm.send_key(words[0])
def get_images(): """ Find the image names under the image directory :return: image names """ return glob.glob(utils_misc.get_path(data_dir.get_data_dir(), "images/*.*"))
def cleanup_cdrom(): """ Remove 'test' cdrom """ logging.info("cleaning up test cdrom") cdrom_test = utils_misc.get_path(data_dir.get_data_dir(), params.get("cdrom_test")) os.remove(cdrom_test)
def change_virtio_media(cdrom_virtio): """ change iso for virtio-win :param cdrom_virtio: iso file """ virtio_iso = utils_misc.get_path(data_dir.get_data_dir(), cdrom_virtio) logging.info("Changing virtio iso image to '%s'" % virtio_iso) vm.change_media("drive_virtio", virtio_iso)
def copy_uefishell(self): """ Copy uefishell.iso :return uefishell.iso path """ ovmf_path = self.params["ovmf_path"] uefishell_filename = "UefiShell.iso" uefishell_dst_path = "images/%s" % uefishell_filename uefishell_src_path = utils_misc.get_path( ovmf_path, uefishell_filename) uefishell_dst_path = utils_misc.get_path( data_dir.get_data_dir(), uefishell_dst_path) if not os.path.exists(uefishell_dst_path): cp_command = "cp -f %s %s" % ( uefishell_src_path, uefishell_dst_path) process.system(cp_command) return uefishell_dst_path
def cleanup_cdroms(): """ Removes created cdrom """ logging.info("cleaning up temp cdrom images") cdrom_test = utils_misc.get_path(data_dir.get_data_dir(), params.get("cdrom_test")) os.remove(cdrom_test)
def run_iometer_windows(test, params, env): """ Run Iometer for Windows on a Windows guest: 1) Boot guest with additional disk 2) Format the additional disk 3) Install and register Iometer 4) Perpare icf to Iometer.exe 5) Run Iometer.exe with icf 6) Copy result to host :param test: kvm test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) # format the target disk utils_test.run_virt_sub_test(test, params, env, "format_disk") error.context("Install Iometer", logging.info) cmd_timeout = int(params.get("cmd_timeout", 360)) ins_cmd = params["install_cmd"] vol_utils = utils_misc.get_winutils_vol(session) if not vol_utils: raise error.TestError("WIN_UTILS CDROM not found") ins_cmd = re.sub("WIN_UTILS", vol_utils, ins_cmd) session.cmd(cmd=ins_cmd, timeout=cmd_timeout) time.sleep(0.5) error.context("Register Iometer", logging.info) reg_cmd = params["register_cmd"] reg_cmd = re.sub("WIN_UTILS", vol_utils, reg_cmd) session.cmd_output(cmd=reg_cmd, timeout=cmd_timeout) error.context("Prepare icf for Iometer", logging.info) icf_name = params["icf_name"] ins_path = params["install_path"] res_file = params["result_file"] icf_file = utils_misc.get_path(test.virtdir, "deps/%s" % icf_name) vm.copy_files_to(icf_file, "%s\\%s" % (ins_path, icf_name)) # Run Iometer error.context("Start Iometer", logging.info) session.cmd("cd %s" % ins_path) logging.info("Change dir to: %s" % ins_path) run_cmd = params["run_cmd"] run_timeout = int(params.get("run_timeout", 1000)) logging.info("Set Timeout: %ss" % run_timeout) run_cmd = run_cmd % (icf_name, res_file) logging.info("Execute Command: %s" % run_cmd) session.cmd(cmd=run_cmd, timeout=run_timeout) error.context("Copy result '%s' to host" % res_file, logging.info) vm.copy_files_from(res_file, test.resultsdir)
def create_backup(self, sync, backup_image_name=""): """ create live backup with qmp command. """ transaction = self.params.get("transaction", "yes") drive_name = self.get_device() bitmap_name = self.bitmap_name backup_format = self.backup_format speed = self.speed mode = "existing" if sync == "full": mode = "absolute-paths" granularity = int(self.params.get("granularity", 65536)) backup_image_name = "images/%s.%s" % (self.image_chain[0], backup_format) backup_image_name = utils_misc.get_path(self.data_dir, backup_image_name) self.trash_files.append(backup_image_name) if transaction == "yes": args_list = [] bitmap_args = { "node": drive_name, "name": bitmap_name, "granularity": granularity } self.transaction_add(args_list, "block-dirty-bitmap-add", bitmap_args) backup_args = { "device": drive_name, "target": backup_image_name, "format": backup_format, "sync": sync, "mode": mode, "speed": speed } self.transaction_add(args_list, "drive-backup", backup_args) error_context.context( "Create bitmap and drive-backup with transaction " "for %s" % drive_name, logging.info) self.vm.monitor.transaction(args_list) if not self.get_status(): raise exceptions.TestFail("full backup job not found") return None error_context.context("Create bitmap for %s" % drive_name, logging.info) self.vm.monitor.operate_dirty_bitmap("add", drive_name, bitmap_name, granularity) if not backup_image_name: raise exceptions.TestError("No backup target provided.") error_context.context("Create %s backup for %s" % (sync, drive_name), logging.info) self.vm.monitor.drive_backup(drive_name, backup_image_name, backup_format, sync, speed, mode, bitmap_name) if not self.get_status(): raise exceptions.TestFail("%s backup job not found" % sync) utils_memory.drop_caches()
def run(test, params, env): """ Test tap device deleted after vm quit with error 1) Boot a with invaild params. 1) Check qemu-kvm quit with error. 2) Check vm tap device delete from ovs bridge. :param test: Kvm test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ def get_ovs_ports(ovs): cmd = "ovs-vsctl list-ports %s" % ovs return set(utils.system_output(cmd).splitlines()) os_dep.command("ovs-vsctl") netdst = params.get("netdst") if netdst not in utils.system_output("ovs-vsctl list-br"): raise error.TestError("%s isn't is openvswith bridge" % netdst) deps_dir = data_dir.get_deps_dir("ovs") nic_script = utils_misc.get_path(deps_dir, params["nic_script"]) nic_downscript = utils_misc.get_path(deps_dir, params["nic_downscript"]) params["nic_script"] = nic_script params["nic_downscript"] = nic_downscript params["qemu_command_prefix"] = "export SHELL=/usr/bin/bash;" params["start_vm"] = "yes" params["nettype"] = "bridge" params["nic_model"] = "virtio-net-pci" try: ports = get_ovs_ports(netdst) env_process.preprocess_vm(test, params, env, params["main_vm"]) env.get_vm(params["main_vm"]) except virt_vm.VMStartError: ports = get_ovs_ports(netdst) - ports if ports: for p in ports: utils.system("ovs-vsctl del-if %s %s" % (netdst, p)) raise error.TestFail("%s not delete after qemu quit." % ports) else: raise error.TestFail("Qemu should quit with error")
def run_qemu_iotests(test, params, env): """ Fetch from git and run qemu-iotests using the qemu binaries under test. 1) Fetch qemu-io from git 3) Run test for the file format detected 4) Report any errors found to autotest @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ # First, let's get qemu-io std = "git://git.kernel.org/pub/scm/linux/kernel/git/hch/qemu-iotests.git" uri = params.get("qemu_io_uri", std) branch = params.get("qemu_io_branch", 'master') lbranch = params.get("qemu_io_lbranch", 'master') commit = params.get("qemu_io_commit", None) base_uri = params.get("qemu_io_base_uri", None) destination_dir = os.path.join(test.srcdir, "qemu_io_tests") git.get_repo(uri=uri, branch=branch, lbranch=lbranch, commit=commit, destination_dir=destination_dir, base_uri=base_uri) # Then, set the qemu paths for the use of the testsuite os.environ["QEMU_PROG"] = utils_misc.get_path( test.bindir, params.get("qemu_binary", "qemu")) os.environ["QEMU_IMG_PROG"] = utils_misc.get_path( test.bindir, params.get("qemu_img_binary", "qemu-img")) os.environ["QEMU_IO_PROG"] = utils_misc.get_path( test.bindir, params.get("qemu_io_binary", "qemu-io")) os.chdir(destination_dir) image_format = params.get("qemu_io_image_format") extra_options = params.get("qemu_io_extra_options", "") cmd = './check' if extra_options: cmd += extra_options error.context("running qemu-iotests for image format %s" % image_format) utils.system("%s -%s" % (cmd, image_format))
def run(test, params, env): """ live_snapshot_base test: 1). Boot up guest 2). Create a file on host and record md5 3). Copy the file to guest 3). Create live snapshot 4). Copy the file from guest,then check md5 :param test: Kvm test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 3600)) session = vm.wait_for_login(timeout=timeout) dd_timeout = params.get("dd_timeoout", 600) copy_timeout = params.get("copy_timeoout", 600) base_file = storage.get_image_filename(params, data_dir.get_data_dir()) device = vm.get_block({"file": base_file}) snapshot_file = "images/%s" % params.get("snapshot_file") snapshot_file = utils_misc.get_path(data_dir.get_data_dir(), snapshot_file) snapshot_format = params.get("snapshot_format", "qcow2") tmp_name = utils_misc.generate_random_string(5) src = dst = "/tmp/%s" % tmp_name if params.get("os_type") != "linux": dst = "c:\\users\\public\\%s" % tmp_name try: error_context.context("create file on host, copy it to guest", logging.info) cmd = params.get("dd_cmd") % src process.system(cmd, timeout=dd_timeout, shell=True) md5 = crypto.hash_file(src, algorithm="md5") vm.copy_files_to(src, dst, timeout=copy_timeout) process.system("rm -f %s" % src) error_context.context("create live snapshot", logging.info) if vm.live_snapshot(base_file, snapshot_file, snapshot_format) != device: test.fail("Fail to create snapshot") backing_file = vm.monitor.get_backingfile(device) if backing_file != base_file: logging.error("backing file: %s, base file: %s", backing_file, base_file) test.fail("Got incorrect backing file") error_context.context("copy file to host, check content not changed", logging.info) vm.copy_files_from(dst, src, timeout=copy_timeout) if md5 and (md5 != crypto.hash_file(src, algorithm="md5")): test.fail("diff md5 before/after create snapshot") session.cmd(params.get("alive_check_cmd", "dir")) finally: if session: session.close() process.system("rm -f %s %s" % (snapshot_file, src))
def run(test, params, env): """ live_snapshot_base test: 1). Boot up guest 2). Create a file on host and record md5 3). Copy the file to guest 3). Create live snapshot 4). Copy the file from guest,then check md5 :param test: Kvm test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 3600)) session = vm.wait_for_login(timeout=timeout) dd_timeout = params.get("dd_timeoout", 600) copy_timeout = params.get("copy_timeoout", 600) base_file = storage.get_image_filename(params, data_dir.get_data_dir()) device = vm.get_block({"file": base_file}) snapshot_file = "images/%s" % params.get("snapshot_name") snapshot_file = utils_misc.get_path(data_dir.get_data_dir(), snapshot_file) snapshot_format = params.get("snapshot_format", "qcow2") tmp_name = utils_misc.generate_random_string(5) src = dst = "/tmp/%s" % tmp_name if params.get("os_type") != "linux": dst = "c:\\users\\public\\%s" % tmp_name try: error_context.context("create file on host, copy it to guest", logging.info) cmd = params.get("dd_cmd") % src process.system(cmd, timeout=dd_timeout, shell=True) md5 = crypto.hash_file(src, algorithm="md5") vm.copy_files_to(src, dst, timeout=copy_timeout) process.system("rm -f %s" % src) error_context.context("create live snapshot", logging.info) if vm.live_snapshot(base_file, snapshot_file, snapshot_format) != device: test.fail("Fail to create snapshot") backing_file = vm.monitor.get_backingfile(device) if backing_file != base_file: logging.error( "backing file: %s, base file: %s", backing_file, base_file) test.fail("Got incorrect backing file") error_context.context("copy file to host, check content not changed", logging.info) vm.copy_files_from(dst, src, timeout=copy_timeout) if md5 and (md5 != crypto.hash_file(src, algorithm="md5")): test.fail("diff md5 before/after create snapshot") session.cmd(params.get("alive_check_cmd", "dir")) finally: if session: session.close() process.system("rm -f %s %s" % (snapshot_file, src))
def dlink_preprcess(download_link): """ Preprocess the download link """ if not download_link: raise error.TestNAError("Can not get the netperf download_link") if not utils.is_url(download_link): download_link = utils_misc.get_path(data_dir.get_deps_dir(), download_link) return download_link
def copy_and_install_rpm(session, url, upgrade=False): rpm_name = os.path.basename(url) if url.startswith("http"): download_cmd = "wget %s" % url utils.system_output(download_cmd) rpm_src = rpm_name else: rpm_src = utils_misc.get_path(test.bindir, url) vm.copy_files_to(rpm_src, "/tmp/%s" % rpm_name) install_rpm(session, "/tmp/%s" % rpm_name, upgrade)
def create_cdroms(): """ Create 'test' cdrom with one file on it """ logging.info("creating test cdrom") cdrom_test = params.get("cdrom_test") cdrom_test = utils_misc.get_path(data_dir.get_data_dir(), cdrom_test) utils.run("dd if=/dev/urandom of=test bs=10M count=1") utils.run("mkisofs -o %s test" % cdrom_test) utils.run("rm -f test")
def run_qemu_iotests(test, params, env): """ Fetch from git and run qemu-iotests using the qemu binaries under test. 1) Fetch qemu-io from git 3) Run test for the file format detected 4) Report any errors found to autotest @param test: QEMU test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ # First, let's get qemu-io std = "git://git.kernel.org/pub/scm/linux/kernel/git/hch/qemu-iotests.git" uri = params.get("qemu_io_uri", std) branch = params.get("qemu_io_branch", 'master') lbranch = params.get("qemu_io_lbranch", 'master') commit = params.get("qemu_io_commit", None) base_uri = params.get("qemu_io_base_uri", None) destination_dir = os.path.join(test.srcdir, "qemu_io_tests") git.get_repo(uri=uri, branch=branch, lbranch=lbranch, commit=commit, destination_dir=destination_dir, base_uri=base_uri) # Then, set the qemu paths for the use of the testsuite os.environ["QEMU_PROG"] = utils_misc.get_path(test.bindir, params.get("qemu_binary", "qemu")) os.environ["QEMU_IMG_PROG"] = utils_misc.get_path(test.bindir, params.get("qemu_img_binary", "qemu-img")) os.environ["QEMU_IO_PROG"] = utils_misc.get_path(test.bindir, params.get("qemu_io_binary", "qemu-io")) os.chdir(destination_dir) image_format = params.get("qemu_io_image_format") extra_options = params.get("qemu_io_extra_options", "") cmd = './check' if extra_options: cmd += extra_options error.context("running qemu-iotests for image format %s" % image_format) utils.system("%s -%s" % (cmd, image_format))
def create_test(cmd): """ Subcommand 'qemu-img create' test. @param cmd: qemu-img base command. """ image_large = params.get("image_name_large") img = utils_misc.get_path(test.bindir, image_large) img += '.' + image_format _create(cmd, img_name=img, fmt=image_format, img_size=params.get("image_size_large")) os.remove(img)
def check_imagefile(self): """ verify current image file is expected image file """ params = self.parser_test_args() exp_img_file = params["expected_image_file"] exp_img_file = utils_misc.get_path(self.data_dir, exp_img_file) logging.info("Check image file is '%s'" % exp_img_file) img_file = self.get_image_file() if exp_img_file != img_file: msg = "Excepted image file: %s," % exp_img_file msg += "Actual image file: %s" % img_file self.test.fail(msg)
def run_stepmaker(test, params, env): vm = env.get_vm(params["main_vm"]) vm.verify_alive() steps_filename = params.get("steps") if not steps_filename: image_name = os.path.basename(params["image_name"]) steps_filename = "steps/%s.steps" % image_name steps_filename = utils_misc.get_path(test.virtdir, steps_filename) StepMaker(vm, steps_filename, test.debugdir, params) gtk.main()