def run(test, params, env): """ Run htm cases: Case one 1) Download unit test suite and configure it 2) Run kvm test on host 3) Check host is still available Case two 1) Download test application in the guest 2) Run it in the guest :param test: QEMU test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ if params["unit_test"] == "yes": error_context.context("Prepare unit test on host", logging.info) cmds = [params["get_htm_dir"], params["compile_htm"]] for cmd in cmds: s, o = process.getstatusoutput(cmd, timeout=3600) if s: test.error("Failed to run cmd '%s', output: %s" % (cmd, o)) error_context.context("Run htm unit test on host", logging.info) s, o = process.getstatusoutput(params["run_htm_test"], timeout=3600) if s: test.fail("Run htm unit test failed, output: %s" % o) # Make sure if host is available by do commands on host status, output = process.getstatusoutput("rm -rf %s" % params["htm_dir"]) if status: test.fail("Please check host's status: %s" % output) utils_misc.verify_dmesg() else: check_exist_cmd = params["check_htm_env"] s, o = process.getstatusoutput(check_exist_cmd) if s: test.error( "Please check htm is supported or not by '%s', output: %s" % (check_exist_cmd, o)) vm = env.get_vm(params["main_vm"]) session = vm.wait_for_login() pkgs = params["depends_pkgs"].split() if not utils_package.package_install(pkgs, session): test.error("Install dependency packages failed") session.cmd(params["get_htm_dir"]) download_htm_demo = params["download_htm_demo"] status = session.cmd_status("wget %s" % download_htm_demo) if status: test.error( "Failed to download test file, please configure it in cfg : %s" % download_htm_demo) else: status, output = session.cmd_status_output( params["test_htm_command"]) if not re.search(params["expected_htm_test_result"], output): test.fail("Test failed and please check : %s" % output) vm.verify_kernel_crash()
def run(test, params, env): """ vpum cpu cycles checking between host and guest: 1) boot guest 2) check cpu cycles for host 3) check cpu cycles for guest and compare with host :param test: QEMU test object. :param params: Dictionary with test parameters. :param env: Dictionary with the test environment. """ tmp_dir = params.get("tmp_dir") timeout = params.get_numeric("login_timeout", 360) test_cmd = params.get("test_cmd") build_cmd = params.get("build_cmd") vm_arch = params["vm_arch_name"] host_arch = arch.ARCH src_dir = os.path.join(data_dir.get_deps_dir(), 'million') src_file = os.path.join(src_dir, "million-%s.s" % host_arch) dst_file = os.path.join(tmp_dir, "million-%s.s" % host_arch) if not utils_package.package_install("perf"): test.error("Install dependency packages failed") vm = env.get_vm(params["main_vm"]) vm.verify_alive() session = vm.wait_for_login(timeout=timeout) error_context.context("build binary file 'million' in host", test.log.info) copyfile(src_file, dst_file) s, o = process.getstatusoutput(build_cmd % host_arch) if s: test.fail("Failed to build test command") error_context.context("running test command in host", test.log.info) s, o = process.getstatusoutput(test_cmd) if s: test.fail("Failed to run test command") host_cpu_cycles = re.findall(r"(\d+) *instructions:u", o, re.M) if not utils_package.package_install("perf", session): test.error("Install dependency packages failed") src_file = os.path.join(src_dir, "million-%s.s" % vm_arch) error_context.context("transfer '%s' to guest('%s')" % (src_file, dst_file), test.log.info) vm.copy_files_to(src_file, tmp_dir, timeout=timeout) error_context.context("build binary file 'million' in guest", test.log.info) session.cmd(build_cmd % vm_arch) error_context.context("running test command in guest", test.log.info) output = session.cmd_output(test_cmd, timeout=timeout) guest_cpu_cycles = re.findall(r"(\d+) *instructions:u", output, re.M) if host_cpu_cycles != guest_cpu_cycles: test.fail("cpu cycles is different between host and guest ")
def _host_config_check(): status = True err_msg = '' if option == "with_negative_config": out = process.getoutput("dmesg") pattern = r"usb (\d-\d(?:.\d)?):.*idVendor=%s, idProduct=%s" pattern = pattern % (vendorid, productid) obj = re.search(pattern, out, re.ASCII) if not obj: status = False err_msg = "Fail to get the USB device info in host dmesg" return (status, err_msg) error_context.context("Make USB device unconfigured", test.log.info) unconfig_value = params["usbredir_unconfigured_value"] cmd = "echo %s > /sys/bus/usb/devices/%s/bConfigurationValue" cmd = cmd % (unconfig_value, obj.group(1)) test.log.info(cmd) s, o = process.getstatusoutput(cmd) if s: status = False err_msg = "Fail to unconfig the USB device, output: %s" % o return (status, err_msg) if backend == 'spicevmc': gui_group = "Server with GUI" out = process.getoutput('yum group list --installed', allow_output_check='stdout', shell=True) obj = re.search(r"(Installed Environment Groups:.*?)^\S", out, re.S | re.M) if not obj or gui_group not in obj.group(1): gui_groupinstall_cmd = "yum groupinstall -y '%s'" % gui_group s, o = process.getstatusoutput(gui_groupinstall_cmd, shell=True) if s: status = False err_msg = "Fail to install '%s' on host, " % gui_group err_msg += "output: %s" % o return (status, err_msg) virt_viewer_cmd = "rpm -q virt-viewer || yum install -y virt-viewer" s, o = process.getstatusoutput(virt_viewer_cmd, shell=True) if s: status = False err_msg = "Fail to install 'virt-viewer' on host, " err_msg += "output: %s" % o return (status, err_msg) elif backend == 'tcp_socket': create_repo() if not utils_package.package_install("usbredir-server"): status = False err_msg = "Fail to install 'usbredir-server' on host" return (status, err_msg) return (status, err_msg)
def check_dump_file(): """ Use crash to check dump file """ process.getstatusoutput("echo bt > %s" % crash_script) process.getstatusoutput("echo quit >> %s" % crash_script) crash_cmd = "crash -i %s /usr/lib/debug/lib/modules/%s/vmlinux " crash_cmd %= (crash_script, host_kernel_version) crash_cmd += dump_file status, output = process.getstatusoutput(crash_cmd) os.remove(crash_script) logging.debug(output) if status != 0 or 'error' in output: test.fail("vmcore corrupt")
def run(test, params, env): """ Run nested relstead tests in kvm-unit-test test suite 1) Start multiple(4) L1 guest vms 2) Clone kvm-unit-tests test suite from repo 3) Compile test suite 4) Run vmx/svm test suite with multiple running vms on host """ vms = env.get_all_vms() for vm in vms: vm.verify_alive() kvm_unit_test_dir = os.path.join(test.logdir, "kvm_unit_tests/") logging.info("kvm_unit_test_dir: %s", kvm_unit_test_dir) clone_cmd = params["clone_cmd"] % kvm_unit_test_dir process.system(clone_cmd) compile_cmd = params["compile_cmd"] % kvm_unit_test_dir process.system(compile_cmd, shell=True) error_context.context("Run kvm_unit_tests on host", logging.info) timeout = params.get_numeric("kvm_unit_test_timeout", 60) run_cmd = params["test_cmd"] % kvm_unit_test_dir logging.info("Run command %s ", run_cmd) status, output = process.getstatusoutput(run_cmd, timeout) if output: test.fail("kvm_unit_tests failed, status: %s, output: %s" % (status, output))
def attach_hook(): """ Check attach hooks. """ # Start a domain with qemu command. disk_src = vm.get_first_disk_devices()['source'] vm_test = "foo" prepare_hook_file(hook_script % (vm_test, hook_log)) qemu_bin = params.get("qemu_bin", "/usr/libexec/qemu-kvm") if "ppc" in platform.machine(): qemu_cmd = ("%s -machine pseries" " -drive file=%s,if=none,bus=0,unit=1" " -monitor unix:/tmp/demo," "server,nowait -name %s" % (qemu_bin, disk_src, vm_test)) else: qemu_cmd = ("%s -drive file=%s,if=none,bus=0,unit=1" " -monitor unix:/tmp/demo," "server,nowait -name %s" % (qemu_bin, disk_src, vm_test)) # After changed above command, qemu-attach failed os.system('%s &' % qemu_cmd) sta, pid = process.getstatusoutput("pgrep qemu-kvm") if not pid: test.fail("Cannot get pid of qemu command") ret = virsh.qemu_attach(pid, **virsh_dargs) if ret.exit_status: utils_misc.kill_process_tree(pid) test.fail("Cannot attach qemu process") else: virsh.destroy(vm_test) hook_str = hook_file + " " + vm_test + " attach begin -" if not check_hooks(hook_str): test.fail("Failed to check attach hooks")
def reload_module(self, force, params): """ Reload module with given parameters. If force=False loading will be skipped when either the module is already loaded with the passed parameters or when the module has not been loaded at all. +----------------------+-+-+-+-+-+-+ |**precondition** | +----------------------+-+-+-+-+-+-+ |module loaded |N|N|Y|Y|Y|Y| +----------------------+-+-+-+-+-+-+ |params already loaded |*|*|Y|N|Y|N| +----------------------+-+-+-+-+-+-+ |force load |Y|N|Y|Y|N|N| +----------------------+-+-+-+-+-+-+ |**result** | +----------------------+-+-+-+-+-+-+ |issue reload |Y|N|Y|Y|N|Y| +----------------------+-+-+-+-+-+-+ :param force: if to force load with params in any case, e.g. True :param params: parameters to load with, e.g. 'key1=param1 ...' """ current_config = self._get_serialized_config() if not force: do_not_load = False if (current_config and all(x in current_config.split() for x in params.split())): logging.debug( "Not reloading module. Current module config" " uration for %s already contains all reques" " ted parameters. Requested: '%s'. Current:" " '%s'. Use force=True to force loading.", self._module_name, params, current_config) do_not_load = True elif not self._was_loaded: logging.debug( "Module %s isn't loaded. Use force=True to force" " loading.", self._module_name) do_not_load = True if do_not_load: return cmds = [] if self._was_loaded: # TODO: Handle cases were module cannot be removed cmds.append('modprobe -r --remove-dependencies %s' % self._module_name) cmd = 'modprobe %s %s' % (self._module_name, params) cmds.append(cmd.strip()) logging.debug("Loading module: %s", cmds) for cmd in cmds: status, output = process.getstatusoutput(cmd, ignore_status=True) if status: raise exceptions.TestError("Couldn't load module %s: %s" % (self._module_name, output)) self._loaded_config = params
def python_install(): """ Install python ansible. """ pip_bin = '' for binary in ['pip', 'pip3', 'pip2']: if process.system("which %s" % binary, ignore_status=True) == 0: pip_bin = binary break if not pip_bin: logging.error("Failed to get available pip binary") return False install_cmd = '%s install ansible' % pip_bin status, output = process.getstatusoutput(install_cmd, verbose=True) if status != 0: logging.error("Install python ansible failed as: %s", output) return False # Install 'sshpass' as it can't be installed automatically as a # dependency of ansible when ansible be installed with pip sshpass_pkg = params.get('sshpass_pkg') if not utils_package.package_install('sshpass'): if not (sshpass_pkg and utils_package.package_install(sshpass_pkg)): logging.error("Failed to install sshpass.") return False return True
def restore(self): """ Restore previous module state. The state will only be restored if the original state was altered. +-------------------+-+-+-+-+ |**precondition** | +-------------------+-+-+-+-+ |module loaded |Y|Y|N|N| +-------------------+-+-+-+-+ |loaded with params |Y|N|Y|N| +-------------------+-+-+-+-+ |**result** | +-------------------+-+-+-+-+ |issue restore |Y|N|Y|N| +-------------------+-+-+-+-+ """ if self.current_config != self._config_backup: # TODO: Handle cases were module cannot be removed holders = self.module_holders for holder in holders: holder.unload_module() self.unload_module() if self._was_loaded: restore_cmd = 'modprobe %s %s' % (self._module_name, self._config_backup) LOG.debug("Restoring module state: %s", restore_cmd) status, output = process.getstatusoutput(restore_cmd) if status: raise KernelModuleRestoreError(self._module_name, output) for holder in holders: holder.restore()
def attach_additional_disk(vm, disksize, targetdev): """ Create a disk with disksize, then attach it to given vm. :param vm: Libvirt VM object. :param disksize: size of attached disk :param targetdev: target of disk device """ LOG.info("Attaching disk...") disk_path = os.path.join(data_dir.get_tmp_dir(), targetdev) cmd = "qemu-img create %s %s" % (disk_path, disksize) status, output = process.getstatusoutput(cmd) if status: return (False, output) # To confirm attached device do not exist. virsh.detach_disk(vm.name, targetdev, extra="--config") attach_result = virsh.attach_disk(vm.name, disk_path, targetdev, extra="--config", debug=True) if attach_result.exit_status: return (False, attach_result) return (True, disk_path)
def setup_remote(self): """ Mount sharing directory to remote host. """ check_mount_dir_cmd = self.ssh_cmd + "'ls -d %s'" % self.mount_dir logging.debug("To check if the %s exists", self.mount_dir) output = process.getoutput(check_mount_dir_cmd) if re.findall("No such file or directory", output, re.M): mkdir_cmd = self.ssh_cmd + "'mkdir -p %s'" % self.mount_dir logging.debug("Prepare to create %s", self.mount_dir) s, o = process.getstatusoutput(mkdir_cmd) if s != 0: raise exceptions.TestFail("Failed to run %s: %s" % (mkdir_cmd, o)) self.mkdir_mount_remote = True if self.params.get("firewall_to_permit_nfs", "yes") == "yes": self.firewall_to_permit_nfs() self.mount_src = "%s:%s" % (self.nfs_server_ip, self.mount_src) logging.debug("Mount %s to %s" % (self.mount_src, self.mount_dir)) mount_cmd = "mount -t nfs %s %s" % (self.mount_src, self.mount_dir) if self.mount_options: mount_cmd += " -o %s" % self.mount_options try: cmd = "%s '%s'" % (self.ssh_cmd, mount_cmd) process.system(cmd, verbose=True) except process.CmdError: raise exceptions.TestFail("Failed to run: %s" % cmd) # Check if the sharing directory is mounted if not self.is_mounted(): raise exceptions.TestFail("Failed to mount from %s to %s" % self.mount_src, self.mount_dir)
def _get_fc_devices(): devs = [] cmd = "lsblk -Spo 'NAME,TRAN' |awk '{if($2==\"fc\") print $1}'" status, output = process.getstatusoutput(cmd) devs_str = output.strip().replace("\n", " ") if devs_str: cmd = "lsblk -Jpo 'NAME,HCTL,SERIAL,TRAN,FSTYPE,WWN' %s" % devs_str status, output = process.getstatusoutput(cmd) devs = copy.deepcopy(json.loads(output)["blockdevices"]) for dev in devs: cmd = "lsscsi -gb %s|awk '{print $3}'" % dev["hctl"] status, output = process.getstatusoutput(cmd) dev["sg_dev"] = output logging.debug(devs) return devs
def test_set_uuid(test, vm, params): """ Test command set_uuid """ add_ref = params.get("gf_add_ref", "disk") readonly = "yes" == params.get("gf_add_readonly") gf = utils_test.libguestfs.GuestfishTools(params) if add_ref == "disk": image_path = params.get("image_path") gf.add_drive_opts(image_path, readonly=readonly) elif add_ref == "domain": vm_name = params.get("main_vm") gf.add_domain(vm_name, readonly=readonly) pv_name = params.get("pv_name") gf.run() mount_point = params.get("mount_point") # set_uuid temp, uuid = process.getstatusoutput("uuidgen") gf.set_uuid(mount_point, uuid) gf_result = gf.vfs_uuid(mount_point) if gf_result.stdout.split()[0] != uuid: gf.close_session() test.fail("set_uuid failed.") gf.close_session()
def _run_cmd(self, option): for line in self.list_of_metric_events: cmd = "perf stat %s %s sleep 1" % (option, line) rc, op = process.getstatusoutput(cmd, ignore_status=True, shell=True, verbose=True) # When the command failed, checking for expected failure or not. if rc: output = op.stdout.decode() + op.stderr.decode() found_imc = False found_hv_24_7 = False for ln in output.splitlines(): if "hv_24x7" in ln: found_hv_24_7 = True break if "imc" in ln: found_imc = True break # IMC errors in PowerVM - Expected # hv_24x7 errors in PowerNV - Expected # IMC failed in PowerNV environment - Fail # HV_24X7 failed in PowerVM environment - Fail if (found_imc and not IS_POWER_NV) or\ (found_hv_24_7 and IS_POWER_NV): self.log.info("%s failed, due to non supporting" " environment" % cmd) else: self.fail_cmd.append(cmd) if self.fail_cmd: self.fail("perf_metric: commands failed are %s" % self.fail_cmd)
def test_set_uuid(test, vm, params): """ Test command set_uuid """ add_ref = params.get("gf_add_ref", "disk") readonly = "yes" == params.get("gf_add_readonly") gf = utils_test.libguestfs.GuestfishTools(params) if add_ref == "disk": image_path = params.get("image_path") gf.add_drive_opts(image_path, readonly=readonly) elif add_ref == "domain": vm_name = params.get("main_vm") gf.add_domain(vm_name, readonly=readonly) pv_name = params.get("pv_name") gf.run() mount_point = params.get("mount_point") # set_uuid temp, uuid = process.getstatusoutput("uuidgen") gf.set_uuid(mount_point, uuid) gf_result = gf.vfs_uuid(mount_point) if gf_result.stdout.split()[0] != uuid: gf.close_session() test.fail("set_uuid failed.") gf.close_session()
def reload_module(self, force, params=""): """ Reload module with given parameters. If force=False loading will be skipped when either the module is already loaded with the passed parameters or when the module has not been loaded at all. +----------------------+-+-+-+-+-+-+ |**precondition** | +----------------------+-+-+-+-+-+-+ |module loaded |N|N|Y|Y|Y|Y| +----------------------+-+-+-+-+-+-+ |params already loaded |*|*|Y|N|Y|N| +----------------------+-+-+-+-+-+-+ |force load |Y|N|Y|Y|N|N| +----------------------+-+-+-+-+-+-+ |**result** | +----------------------+-+-+-+-+-+-+ |issue reload |Y|N|Y|Y|N|Y| +----------------------+-+-+-+-+-+-+ :param force: if to force load with params in any case, e.g. True :param params: parameters to load with, e.g. 'key1=param1 ...' """ current_config = self.current_config if not force: do_not_load = False if (current_config and all(x in current_config.split() for x in params.split())): LOG.debug( "Not reloading module. Current module config" " uration for %s already contains all reques" " ted parameters. Requested: '%s'. Current:" " '%s'. Use force=True to force loading.", self._module_name, params, current_config) do_not_load = True elif not self._was_loaded: LOG.debug( "Module %s isn't loaded. Use force=True to force" " loading.", self._module_name) do_not_load = True if do_not_load: return # TODO: Handle cases were module cannot be removed holders = self.module_holders for holder in holders: holder.unload_module() self.unload_module() reload_cmd = 'modprobe %s %s' % (self._module_name, params) LOG.debug("Reloading module: %s", reload_cmd) status, output = process.getstatusoutput(reload_cmd.strip()) if status: raise KernelModuleReloadError(self._module_name, output) for holder in holders: holder.restore()
def _rv_connection_check(): rv_pid = process.getoutput("pidof %s" % rv_binary) cmd = 'netstat -ptn | grep "^tcp.*127.0.0.1:%s.*ESTABLISHED %s.*"' cmd = cmd % (spice_port, rv_pid) s, o = process.getstatusoutput(cmd) if s: return False test.log.info("netstat output:\n%s", o) return True
def _convert_with_copy_offloading_and_verify(src, tgt): """Verify whether copy_offloading works.""" logging.info("Verify whether copy_offloading works for commit.") cmd = ("strace -e trace=copy_file_range -f qemu-img convert -C -f " "%s %s -O %s %s " % (src.image_format, src.image_filename, tgt.image_format, tgt.image_filename)) sts, text = process.getstatusoutput(cmd, verbose=True) if sts != 0: test.fail("Convert with copy_offloading failed: %s." % text)
def compile_kernel_selftests(): git_cmd = 'git clone --depth=1 {} {} 2>/dev/null'.format(git_repo, dst_dir) if os.path.exists(dst_dir): shutil.rmtree(dst_dir) process.run(git_cmd, timeout=360, shell=True) s, o = process.getstatusoutput(compile_cmd, timeout=180) if s: logging.error('Compile output: %s', o) test.error('Failed to compile the test suite.')
def get_host_scsi_disk(): """ Get scsi disk which emulated by scsi_debug module. """ cmd = "lsblk -S -n -p|grep scsi_debug" status, output = process.getstatusoutput(cmd) if status != 0: test.fail("Can not get scsi_debug disk on host") scsi_disk_info = output.strip().split() return scsi_disk_info[1], scsi_disk_info[0]
def load_vfio_ap(): """ Loads the passthrough module :return: None """ err, out = process.getstatusoutput("modprobe vfio_ap", timeout=CMD_TIMEOUT, verbose=VERBOSE) if err: raise RuntimeError("Couldn't load vfio_ap: %s" % out)
def unload_module(self): """ Unload module and those modules that use it. If there are some modules using this module, they are unloaded first. """ if os.path.exists(self._module_path): unload_cmd = 'rmmod ' + self._module_name LOG.debug("Unloading module: %s", unload_cmd) status, output = process.getstatusoutput(unload_cmd) if status: raise KernelModuleUnloadError(self._module_name, output)
def test_all_group_events(self): failed_events = [] # Run group of events based on PMU for key in self.all_events.keys(): rc, op = process.getstatusoutput("perf stat -e '{%s}' sleep 1" % ','.join(self.all_events[key]), shell=True, verbose=True, ignore_status=True) if rc: failed_events.append(self.all_events[key]) if failed_events: self.fail("Failed with events: %s " % failed_events)
def _get_ioeventfds(ioeventfd_opt): """ Get the number of ioeventfds inside host. """ logging.info('Check the \"%s\" via /proc/$PID/fd/.' % ioeventfd) dst_log = 'off' if 'off' in ioeventfd_opt else 'on' cmd = 'ls -l /proc/$(pgrep qemu-kvm)/fd > /tmp/{0}; cat /tmp/{0}'.format(dst_log) logging.debug('Running \'%s\'' % cmd) s, o = process.getstatusoutput(cmd) logging.debug(o) if s: test.error('Failed to get the number of event fd.\n%s' % o)
def _client_config_check(): status = True err_msg = '' gui_group = "Server with GUI" out = process.getoutput('yum group list --installed', allow_output_check='stdout', shell=True) obj = re.search(r"(Installed Environment Groups:.*?)^\S", out, re.S | re.M) if not obj or gui_group not in obj.group(1): gui_groupinstall_cmd = "yum groupinstall -y '%s'" % gui_group s, o = process.getstatusoutput(gui_groupinstall_cmd, shell=True) if s: status = False err_msg = "Fail to install '%s' on client, " % gui_group err_msg += "output: %s" % o return (status, err_msg) virt_viewer_cmd = "rpm -q virt-viewer || yum install -y virt-viewer" s, o = process.getstatusoutput(virt_viewer_cmd, shell=True) if s: status = False err_msg = "Fail to install 'virt-viewer' on client, " err_msg += "output: %s" % o return (status, err_msg) # unpack fake-smartcard database sc_db = params.get('sc_db_tar', 'fake-smartcard.tar.gz') sc_db_src = os.path.join(data_dir.get_deps_dir('smartcard'), sc_db) unpack_sc_db = 'mkdir -p {0} && tar -zxvf {1} -C {0}' unpack_sc_db = unpack_sc_db.format(sc_db_dst, sc_db_src) s, o = process.getstatusoutput(unpack_sc_db, shell=True) if s: status = False err_msg = "Fail to unpack smartcard database on client, " err_msg += "output: %s" % o return (status, err_msg) return (status, err_msg)
def test_all_events(self): failed_event_list = [] # For each pmu available, run events one by one for pmu in self.pmu_list: for event in self.all_events[pmu]: rc, op = process.getstatusoutput('perf stat -e %s sleep 1' % event, shell=True, ignore_status=True, verbose=True) if rc: failed_event_list.append(event) if failed_event_list: self.fail("Failed events are: %s" % failed_event_list)
def pkg_buffer_test(): logging.info("Compile the script and execute") serial_session.cmd("gcc -o ~/exp ~/exp.c") serial_session.sendline("~/exp") time.sleep(60) s = process.getstatusoutput( "ps -aux|grep /usr/bin/gnome-calculator |grep -v grep", timeout=60, shell=True)[0] if s == 0: test.fail("Virtual machine has security issues") serial_session.send_ctrl("^c") logging.info("send ctrl+c command to exit the current process.") vm.verify_kernel_crash()
def _get_scsi_debug_disk(guest_session=None): """" Get scsi debug disk on host or guest which created as scsi-block. """ cmd = "lsblk -S -n -p|grep scsi_debug" if guest_session: status, output = guest_session.cmd_status_output(cmd) else: status, output = process.getstatusoutput(cmd) if status != 0: test.fail("Can not find scsi_debug disk") return output.split()[0]
def _echo(value, sysfs): """ echoes value into sysfs path :param value: :param sysfs: :raises RuntimeError: if operation fails :return: None """ err, out = process.getstatusoutput("echo %s > %s" % (value, sysfs), timeout=CMD_TIMEOUT, verbose=VERBOSE) if err: raise RuntimeError("Couldn't set value '%s' on '%s': %s" % (value, sysfs, out))
def is_mounted(self): """ Check the NFS is mounted or not. :return: If the src is mounted as expect :rtype: Boolean """ find_mountpoint_cmd = "mount | grep -E '.*%s.*%s.*'" % (self.mount_src, self.mount_dir) cmd = self.ssh_cmd + "'%s'" % find_mountpoint_cmd logging.debug("The command: %s", cmd) status, output = process.getstatusoutput(cmd) if status: logging.debug("The command result: <%s:%s>", status, output) return False return True
def is_mounted(self): """ Check the NFS is mounted or not. :return: If the src is mounted as expect :rtype: Boolean """ find_mountpoint_cmd = "mount | grep -E '.*%s.*%s.*'" % (self.mount_src, self.mount_dir) cmd = self.ssh_cmd + "'%s'" % find_mountpoint_cmd logging.debug("The command: %s", cmd) status, output = process.getstatusoutput(cmd) if status: logging.debug("The command result: <%s:%s>", status, output) return False return True
def cgexec(self, cgroup, cmd, args=""): """ Execute command in desired cgroup :param cgroup: Desired cgroup :param cmd: Executed command :param args: Executed command's parameters """ try: cgexec_cmd = ("cgexec -g %s:%s %s %s" % (self.module, cgroup, cmd, args)) status, output = process.getstatusoutput(cgexec_cmd) return status, output except process.CmdError as detail: raise exceptions.TestFail("Execute %s in cgroup failed!\n%s" % (cmd, detail))
def cgexec(self, cgroup, cmd, args=""): """ Execute command in desired cgroup :param cgroup: Desired cgroup :param cmd: Executed command :param args: Executed command's parameters """ try: cgexec_cmd = ("cgexec -g %s:%s %s %s" % (self.module, cgroup, cmd, args)) status, output = process.getstatusoutput(cgexec_cmd) return status, output except process.CmdError as detail: raise exceptions.TestFail("Execute %s in cgroup failed!\n%s" % (cmd, detail))
def attach_additional_disk(vm, disksize, targetdev): """ Create a disk with disksize, then attach it to given vm. :param vm: Libvirt VM object. :param disksize: size of attached disk :param targetdev: target of disk device """ logging.info("Attaching disk...") disk_path = os.path.join(data_dir.get_tmp_dir(), targetdev) cmd = "qemu-img create %s %s" % (disk_path, disksize) status, output = process.getstatusoutput(cmd) if status: return (False, output) # To confirm attached device do not exist. virsh.detach_disk(vm.name, targetdev, extra="--config") attach_result = virsh.attach_disk(vm.name, disk_path, targetdev, extra="--config", debug=True) if attach_result.exit_status: return (False, attach_result) return (True, disk_path)
def run(test, params, env): """ Test of built-in 'add-xxx' commands in guestfish. 1) Get parameters for test 2) Set options for commands 3) Run key commands: a.add disk or domain with readonly or not b.launch c.mount root device 4) Write a file to help result checking 5) Check result """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) # Get parameters add_ref = params.get("guestfs_add_ref", "domain") add_readonly = "yes" == params.get("guestfs_add_readonly", "no") status_error = "yes" == params.get("status_error", "no") login_to_check = "yes" == params.get("login_to_check_write", "no") start_vm = "yes" == params.get("start_vm", "no") # Any failed info will be recorded in this dict # Result check will rely on it. fail_flag = 0 fail_info = {} if vm.is_alive() and not start_vm: vm.destroy() if add_ref == "domain": disk_or_domain = vm_name elif add_ref == "disk": # Get system disk path of tested domain disks = vm.get_disk_devices() if len(disks): disk = list(disks.values())[0] disk_or_domain = disk['source'] else: # No need to test since getting vm's disk failed. test.fail("Can not get disk of %s" % vm_name) else: # If adding an unknown disk or domain disk_or_domain = add_ref add_ref = "disk" guestfs = lgf.GuestfishPersistent() set_guestfs_args(guestfs) add_error = params.get("guestfs_add_error", "no") # Add tested disk or domain try: add_disk_or_domain(test, guestfs, disk_or_domain, add_ref, add_readonly) except exceptions.TestFail as detail: guestfs.close_session() if add_error: logging.debug("Add failed as expected:%s", str(detail)) return raise # Launch added disk or domain launch_disk(test, guestfs) # Mount root filesystem root = get_root(test, guestfs) mount_filesystem(test, guestfs, root, '/') # Write content to file status, content = process.getstatusoutput("uuidgen") write_result = guestfs.write("/guestfs_temp", content) if write_result.exit_status: fail_flag = 1 fail_info['write_content'] = ("Write content to file failed:" "%s" % write_result) else: logging.debug("Write content to file successfully.") fail_info['write_content'] = "Write content to file successfully." # Check writed file in a new guestfish session guestfs.new_session() set_guestfs_args(guestfs) add_disk_or_domain(test, guestfs, disk_or_domain, add_ref, add_readonly) launch_disk(test, guestfs) mount_filesystem(test, guestfs, root, '/') cat_result = guestfs.cat("/guestfs_temp") if cat_result.exit_status: fail_flag = 1 fail_info['cat_writed'] = ("Cat writed file failed:" "%s" % cat_result) else: guestfs_writed_text = cat_result.stdout if not re.search(content, guestfs_writed_text): fail_flag = 1 fail_info['cat_writed'] = ("Catted text is not match with writed:" "%s" % cat_result) logging.debug("Catted text is not match with writed") else: logging.debug("Cat content of file successfully.") fail_info['cat_writed'] = "Cat content of file successfully." # Start vm and login to check writed file. guestfs.close_session() # Convert sdx in root to vdx for virtio system disk if primary_disk_virtio(vm): root = process.run("echo %s | sed -e 's/sd/vd/g'" % root, ignore_status=True, shell=True).stdout_text.strip() if login_to_check: try: vm.start() session = vm.wait_for_login() session.cmd("mount %s /mnt" % root) try: login_wrote_text = session.cmd_output("cat /mnt/guestfs_temp", timeout=5) except aexpect.ShellTimeoutError as detail: # written content with guestfs.write won't contain line break # Is is a bug of guestfish.write? login_wrote_text = str(detail) if not re.search(content, login_wrote_text): fail_flag = 1 fail_info['login_to_check'] = ("Login to check failed:" "%s" % login_wrote_text) else: logging.debug("Login to check successfully.") fail_info['login_to_check'] = "Login to check successfully." session.close() except aexpect.ShellError as detail: fail_flag = 1 fail_info['login_to_check'] = detail vm.destroy() if status_error: if not fail_flag: test.fail("Expected error is successful:" "%s" % fail_info) else: if fail_flag: test.fail(fail_info)
def run(test, params, env): """ Test command: secret-dumpxml <secret> Output attributes of a secret as an XML dump to stdout. """ # MAIN TEST CODE ### # Process cartesian parameters status_error = ("yes" == params.get("status_error", "no")) secret_ref = params.get("secret_ref") # acl polkit params uri = params.get("virsh_uri") unprivileged_user = params.get('unprivileged_user') if unprivileged_user: if unprivileged_user.count('EXAMPLE'): unprivileged_user = '******' if not libvirt_version.version_compare(1, 1, 1): if params.get('setup_libvirt_polkit') == 'yes': test.cancel("API acl test not supported in current" " libvirt version.") virsh_dargs = {'debug': True} if params.get('setup_libvirt_polkit') == 'yes': virsh_dargs['unprivileged_user'] = unprivileged_user virsh_dargs['uri'] = uri if secret_ref == "secret_valid_uuid": # Generate valid uuid cmd = "uuidgen" status, uuid = process.getstatusoutput(cmd) if status: test.cancel("Failed to generate valid uuid") # Get a full path of tmpfile, the tmpfile need not exist tmp_dir = data_dir.get_tmp_dir() volume_path = os.path.join(tmp_dir, "secret_volume") secret_xml = """ <secret ephemeral='no' private='yes'> <uuid>%s</uuid> <usage type='volume'> <volume>%s</volume> </usage> </secret> """ % (uuid, volume_path) # Write secret xml into a tmpfile tmp_file = tempfile.NamedTemporaryFile(prefix=("secret_xml_"), dir=tmp_dir) xmlfile = tmp_file.name tmp_file.close() with open(xmlfile, 'w') as fd: fd.write(secret_xml) try: virsh.secret_define(xmlfile, debug=True) cmd_result = virsh.secret_dumpxml(uuid, **virsh_dargs) output = cmd_result.stdout.strip() if not status_error and cmd_result.exit_status: test.fail("Dumping the xml of secret object failed") match_string = "<uuid>%s</uuid>" % uuid if not re.search(match_string, output): test.fail("The secret xml is not valid") finally: #Cleanup virsh.secret_undefine(uuid, debug=True) if os.path.exists(xmlfile): os.remove(xmlfile)
def run(test, params, env): """ Test command: virsh secret-define <file> secret-undefine <secret> The testcase is to define or modify a secret from an XML file, then undefine it """ # MAIN TEST CODE ### # Process cartesian parameters secret_ref = params.get("secret_ref") ephemeral = params.get("ephemeral_value", "no") private = params.get("private_value", "no") modify_volume = ("yes" == params.get("secret_modify_volume", "no")) remove_uuid = ("yes" == params.get("secret_remove_uuid", "no")) if secret_ref == "secret_valid_uuid": # Generate valid uuid cmd = "uuidgen" status, uuid = process.getstatusoutput(cmd) if status: test.cancel("Failed to generate valid uuid") elif secret_ref == "secret_invalid_uuid": uuid = params.get(secret_ref) # libvirt acl related params uri = params.get("virsh_uri") unprivileged_user = params.get('unprivileged_user') define_acl = "yes" == params.get("define_acl", "no") undefine_acl = "yes" == params.get("undefine_acl", "no") get_value_acl = "yes" == params.get("get_value_acl", "no") define_error = "yes" == params.get("define_error", "no") undefine_error = "yes" == params.get("undefine_error", "no") get_value_error = "yes" == params.get("get_value_error", "no") define_readonly = "yes" == params.get("secret_define_readonly", "no") undefine_readonly = "yes" == params.get("secret_undefine_readonly", "no") expect_msg = params.get("secret_err_msg", "") if unprivileged_user: if unprivileged_user.count('EXAMPLE'): unprivileged_user = '******' if not libvirt_version.version_compare(1, 1, 1): if params.get('setup_libvirt_polkit') == 'yes': test.cancel("API acl test not supported in current" " libvirt version.") acl_dargs = {'uri': uri, 'unprivileged_user': unprivileged_user, 'debug': True} # Get a full path of tmpfile, the tmpfile need not exist tmp_dir = data_dir.get_tmp_dir() volume_path = os.path.join(tmp_dir, "secret_volume") secret_xml_obj = SecretXML(ephemeral, private) secret_xml_obj.uuid = uuid secret_xml_obj.volume = volume_path secret_xml_obj.usage = "volume" secret_obj_xmlfile = os.path.join(SECRET_DIR, uuid + ".xml") # Run the test try: if define_acl: process.run("chmod 666 %s" % secret_xml_obj.xml, shell=True) cmd_result = virsh.secret_define(secret_xml_obj.xml, **acl_dargs) else: cmd_result = virsh.secret_define(secret_xml_obj.xml, debug=True, readonly=define_readonly) libvirt.check_exit_status(cmd_result, define_error) if cmd_result.exit_status: if define_readonly: if not re.search(expect_msg, cmd_result.stderr.strip()): test.fail("Fail to get expect err msg: %s" % expect_msg) else: logging.info("Get expect err msg: %s", expect_msg) return # Check ephemeral attribute exist = os.path.exists(secret_obj_xmlfile) if (ephemeral == "yes" and exist) or \ (ephemeral == "no" and not exist): test.fail("The ephemeral attribute worked not expected") # Check private attrbute virsh.secret_set_value(uuid, SECRET_BASE64, debug=True) if get_value_acl: cmd_result = virsh.secret_get_value(uuid, **acl_dargs) else: cmd_result = virsh.secret_get_value(uuid, debug=True) libvirt.check_exit_status(cmd_result, get_value_error) status = cmd_result.exit_status err_msg = "The private attribute worked not expected" if private == "yes" and not status: test.fail(err_msg) if private == "no" and status: if not get_value_error: test.fail(err_msg) if modify_volume: volume_path = os.path.join(tmp_dir, "secret_volume_modify") secret_xml_obj.volume = volume_path cmd_result = virsh.secret_define(secret_xml_obj.xml, debug=True) if cmd_result.exit_status == 0: test.fail("Expect fail on redefine after modify " "volume, but success indeed") if remove_uuid: secret_xml_obj2 = SecretXML(ephemeral, private) secret_xml_obj2.volume = volume_path secret_xml_obj2.usage = "volume" cmd_result = virsh.secret_define(secret_xml_obj2.xml, debug=True) if cmd_result.exit_status == 0: test.fail("Expect fail on redefine after remove " "uuid, but success indeed") if undefine_acl: cmd_result = virsh.secret_undefine(uuid, **acl_dargs) else: cmd_result = virsh.secret_undefine(uuid, debug=True, readonly=undefine_readonly) libvirt.check_exit_status(cmd_result, undefine_error) if undefine_readonly: if not re.search(expect_msg, cmd_result.stderr.strip()): test.fail("Fail to get expect err msg: %s" % expect_msg) else: logging.info("Get expect err msg: %s", expect_msg) finally: # cleanup virsh.secret_undefine(uuid, ignore_status=True) if os.path.exists(volume_path): os.unlink(volume_path) if os.path.exists(secret_obj_xmlfile): os.unlink(secret_obj_xmlfile)
def test_formatted_part(test, vm, params): """ 1) Do some necessary check 2) Format additional disk with specific filesystem 3) Try to write a file to mounted device and get md5 4) Login to check writed file and its md5 """ add_device = params.get("gf_additional_device", "/dev/vdb") device_in_lgf = process.run("echo %s | sed -e 's/vd/sd/g'" % add_device, ignore_status=True, shell=True).stdout_text.strip() if utils_test.libguestfs.primary_disk_virtio(vm): device_in_vm = add_device else: device_in_vm = "/dev/vda" device_part = "%s1" % device_in_lgf device_part_in_vm = "%s1" % device_in_vm # Mount specific partition params['special_mountpoints'] = [device_part] vt = utils_test.libguestfs.VirtTools(vm, params) # Create a new vm with additional disk vt.update_vm_disk() # Default vm_ref is oldvm, so switch it. vm_ref = vt.newvm.name # Format disk format_result = vt.format_disk(filesystem="ext3", partition="mbr") if format_result.exit_status: test.fail("Format added disk failed.") logging.info("Format added disk successfully.") # List filesystems detail list_fs_detail = vt.get_filesystems_info(vm_ref) if list_fs_detail.exit_status: test.fail("List filesystems detail failed.") logging.info("List filesystems detail successfully.") content = "This is file for formatted part test." path = params.get("temp_file", "formatted_part") mountpoint = params.get("vt_mountpoint", "/mnt") writes, writeo = vt.write_file_with_guestmount(mountpoint, path, content, vm_ref, cleanup=False) if writes is False: utils_misc.umount("", mountpoint, "") test.fail("Write file to mounted filesystem failed.") logging.info("Create %s successfully.", writeo) # Compute new file's md5 if os.path.isfile(writeo): md5s, md5o = process.getstatusoutput("md5sum %s" % writeo) utils_misc.umount("", mountpoint, "") if md5s: test.fail("Compute %s's md5 failed." % writeo) md5 = md5o.split()[0].strip() logging.debug("%s's md5 in newvm is:%s", writeo, md5) else: utils_misc.umount("", mountpoint, "") test.fail("Can not find %s." % writeo) attached_vm = vt.newvm try: attached_vm.start() session = attached_vm.wait_for_login() except Exception as detail: attached_vm.destroy() test.fail(str(detail)) try: file_path = os.path.join(mountpoint, path) mounts = session.cmd_status("mount %s %s" % (device_part_in_vm, mountpoint), timeout=10) if mounts: logging.error("Mount device in vm failed.") md51 = session.cmd_output("md5sum %s" % file_path) logging.debug(md51) if not re.search(md5o, md51): attached_vm.destroy() attached_vm.wait_for_shutdown() test.fail("Got a different md5.") logging.info("Got matched md5.") session.cmd_status("cat %s" % file_path, timeout=5) attached_vm.destroy() attached_vm.wait_for_shutdown() except (virt_vm.VMError, remote.LoginError, aexpect.ShellError) as detail: if attached_vm.is_alive(): attached_vm.destroy() if not re.search(content, str(detail)): test.fail(str(detail)) logging.info("Check file on guest successfully.")