Example #1
0
def test_upload_offset(test, vm, params):
    """
    Test command upload_offset
    """
    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)

    gf.run()
    gf.do_mount("/")

    filename = "%s/src.txt" % data_dir.get_tmp_dir()
    string = "Hello World"
    process.getoutput("echo %s > %s" % (string, filename))

    gf.upload_offset(filename, "/dest.txt", 0)
    content = gf.cat("/dest.txt").stdout.strip()
    gf.close_session()
    process.getoutput("rm %s" % filename)

    if content != string:
        test.fail("Content is not correct")
Example #2
0
 def check_ksm(mem, stress=False):
     """
     :param mem: Boot guest with given memory, in KB
     :param stress: Load stress or not
     """
     params['mem'] = mem // 1024
     params['start_vm'] = 'yes'
     vm_name = params['main_vm']
     env_process.preprocess_vm(test, params, env, vm_name)
     vm = env.get_vm(vm_name)
     vm.wait_for_login()
     if stress:
         params['stress_args'] = ('--cpu 4 --io 4 --vm 2 --vm-bytes %sM' %
                                  (int(params['mem']) // 2))
         stress_test = VMStress(vm, "stress", params)
         stress_test.load_stress_tool()
         time.sleep(30)
     qemu_pid = vm.get_pid()
     qemu_used_page = utils_misc.normalize_data_size(process.getoutput(
         params['cmd_get_qemu_used_mem'] % qemu_pid, shell=True) + 'K', 'B')
     pagesize = utils_memory.getpagesize()
     qemu_used_mem = int(float(qemu_used_page)) * pagesize
     free_mem_host = utils_memory.freememtotal()
     ksm_status = process.getoutput(params['cmd_check_ksm_status'])
     vm.destroy()
     logging.info('The ksm threshold is %s, the memory allocated by qemu is'
                  ' %s, and the total free memory on host is %s.' %
                  (ksm_thres, qemu_used_mem, free_mem_host))
     if free_mem_host >= ksm_thres:
         if ksm_status != '0':
             test.fail('Ksm should not start.')
         if stress:
             test.error('The host resource is not consumed as expected.')
     elif ksm_status == '0':
         test.fail('Ksm should start but it does not.')
Example #3
0
 def get_user_ugid(username):
     """
     return user uid and gid as a list
     """
     user_uid = process.getoutput("id -u %s" % username).split()
     user_gid = process.getoutput("id -g %s" % username).split()
     return (user_uid, user_gid)
Example #4
0
 def _start_usbredir_server():
     process.getoutput("killall usbredirserver")
     usbredir_server = utils_misc.get_binary('usbredirserver', params)
     usbredirserver_args = usbredir_server + " -p %s " % free_port
     usbredirserver_args += " %s:%s" % (vendorid, productid)
     usbredirserver_args += " > /dev/null 2>&1"
     rv_thread = utils_misc.InterruptedThread(os.system,
                                              (usbredirserver_args, ))
     rv_thread.start()
 def create_path_disk():
     """Create a passthrough disk with scsi_debug """
     process.getoutput(params["pre_command"], shell=True)
     disks_old = process.getoutput("ls -1d /dev/sd*", shell=True).split()
     process.system_output(params["create_command"],
                           timeout=300,
                           shell=True,
                           verbose=False)
     disks_new = process.getoutput("ls -1d /dev/sd*", shell=True).split()
     return list(set(disks_new) - set(disks_old))[0]
Example #6
0
    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)
Example #7
0
 def list_mount_devices():
     """
     Lists mounted file systems and swap on devices.
     """
     # list mounted file systems
     devices = [
         line.split()[0]
         for line in process.getoutput('mount').splitlines()
     ]
     # list mounted swap devices
     swaps = process.getoutput('swapon -s').splitlines()
     devices.extend(
         [line.split()[0] for line in swaps if line.startswith('/')])
     return devices
    def virsh_convxml(guest_args):
        """
        Put dumpxml vm'infomation to a file

        :param guest_args : File which will save config information.
        """
        pid = vm.get_pid()
        cmdline = process.run("cat -v /proc/%d/cmdline" % pid).stdout_text
        cmdline = re.sub(r'\^@', ' ', cmdline)
        cmdline_tmp = re.sub(r'\s-drive\s[^\s]+', '\s', cmdline)

        # Libvirt requires the binary path for domxml-from-native to succeed
        # /proc/pid/cmdline would give qemu cmdline with binary without
        # absolute path
        qemu_bin = ['qemu-system-ppc64', 'qemu-system-x86_64', 'qemu-kvm', 'kvm']
        change_bin = ''
        qemu_binary = ''
        for each_bin in qemu_bin:
            if each_bin in cmdline_tmp:
                qemu_binary = each_bin
                break
        for each_opt in cmdline_tmp.split():
            if qemu_binary in each_opt:
                change_bin = each_opt
                break
        exec_str = "export PATH=$PATH:/usr/libexec;which %s"
        qemu_binary = process.getoutput(exec_str % qemu_binary, shell=True)
        if change_bin.strip() != qemu_binary.strip():
            cmdline_tmp = re.sub(change_bin, qemu_binary, cmdline_tmp)

        with open(guest_args, 'w') as guest_file:
            guest_file.write(cmdline_tmp)
Example #9
0
def generate_mem_dump(test, params, vm):
    """
    Generate the Memory.dmp file through qemu side.

    :param test: kvm test object
    :param params: the dict used for parameters.
    :param vm: VM object.
    """
    tmp_dir = params["tmp_dir"]
    if not os.path.isdir(tmp_dir):
        process.system("mkdir %s" % tmp_dir)
    dump_name = utils_misc.generate_random_string(4) + "Memory.dmp"
    dump_file = tmp_dir + "/" + dump_name

    output = vm.monitor.human_monitor_cmd('dump-guest-memory -w %s' %
                                          dump_file)
    if output:
        test.fail("Save dump file failed as: %s" % output)
    else:
        cmd = "ls -l %s | awk '{print $5}'" % dump_file
        dump_size = int(process.getoutput(cmd))
        if dump_size == 0:
            test.fail("The size of dump file is %d" % dump_size)

    dump_name_zip = "%s.zip" % dump_name
    process.system("cd %s && zip %s %s" % (tmp_dir, dump_name_zip, dump_name),
                   shell=True)
    dump_file_zip = tmp_dir + "/" + dump_name_zip
    return dump_file, dump_file_zip
Example #10
0
def run(test, params, env):
    """
    Boot guest after disable ept/npt:
    1) Create lvm with read-only permission
    2) Boot up guest with lvm
    3) Unplug the disk
    4) Hotplug blockdev node with auto-read-only

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """

    params["start_vm"] = 'yes'
    params["pv_name"] = process.getoutput(params["get_devname_command"])
    lvm = LVM(params)
    lvm.setup()
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params['main_vm'])
    session = vm.wait_for_login()
    qmp_port = vm.monitor
    qdev = vm.devices
    device = qdev.get_by_params({"id": 'stg0'})[0]
    qdev.simple_unplug(device, qmp_port)
    image_name = params["data_tag"]
    image_params = params.object_params(image_name)
    devs = qdev.images_define_by_params(image_name, image_params, 'disk')
    for dev in devs:
        qdev.simple_hotplug(dev, qmp_port)
Example #11
0
def run(test, params, env):
    """
    acpi description of serial and parallel ports incorrect
    with -chardev/-device:
    1) Check device resources(io port and irq) on host
    2) Boot guest A with isa-serial with tty chardev backend
    3) Check device resources inside guest A
    4) Boot guest B with -serial /dev/ttyS0
    5) Check device resources inside guest B
    6) Check if the result are same for host, A and B.
    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    outputs = []
    check_cmd = params['check_cmd']
    host_output = process.getoutput(check_cmd)[:35]
    outputs.append(host_output)
    for x in range(2):
        if x >= 1:
            params['serials'] = " ".join(params['serials'].split()[:-1])
            params['extra_params'] = params.get('extra_params',
                                                '') + ' -serial /dev/ttyS0'
            env_process.preprocess(test, params, env)
        vm = env.get_vm(params["main_vm"])
        session = vm.wait_for_login()
        vm_output = session.cmd_status_output(check_cmd)[1][:35]
        outputs.append(vm_output)
        vm.destroy()
    assert outputs.count(outputs[0]) == len(outputs), \
        "Host: {} and VM 1: {} and VM 2: {} are not the same".\
        format(outputs[0], outputs[1], outputs[2])
Example #12
0
 def get_image_size(self, image):
     """
     Get image size for given image.
     :param image: image file.
     :return: image size.
     """
     force_share = False
     pause_vm = False
     params = self.params.object_params(image)
     qemu_image = qemu_storage.QemuImg(params, self.data_dir, image)
     if self.vm:
         pids = process.getoutput(
             "lsof %s |grep -v PID|awk '{print $2}'" % qemu_image.image_filename)
         force_share = str(self.vm.get_pid()) in pids
         if force_share and not self.vm.is_paused():
             self.vm.pause()
             pause_vm = True
     image_info = qemu_image.info(force_share=force_share)
     if self.vm and pause_vm:
         self.vm.resume()
     if not image_info:
         self.test.error("Get image info failed.")
     image_size = re.findall(r"disk size: (\d\.?\d*?.*)", image_info)[0]
     image_size = int(float(utils_misc.normalize_data_size(image_size, "B")))
     logging.info("Image size of %s is %s", image, image_size)
     return image_size
Example #13
0
 def check_qemu_used_mem(qemu_pid, mem):
     qemu_used_page = process.getoutput(get_qemu_used_mem % qemu_pid,
                                        shell=True)
     qemu_used_mem = float(qemu_used_page) * pagesize
     if qemu_used_mem < mem * mem_thres:
         return False
     return True
Example #14
0
def test_download_offset(test, vm, params):
    """
    Test command download-offset
    """
    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)

    gf.run()
    gf.do_mount("/")

    string = "Hello World"

    gf.write("/src.txt", string)
    src_size = gf.filesize("/src.txt").stdout.strip()

    dest = "%s/dest.txt" % data_dir.get_tmp_dir()
    gf.download_offset("/src.txt", "%s" % dest, 0, len(string))
    gf.close_session()

    content = process.getoutput("cat %s" % dest)
    process.system("rm %s" % dest)

    if content != "Hello World":
        test.fail("Content or filesize is not match")
Example #15
0
def check_snap_in_image(vm_name, snap_name):
    """
    check the snapshot info in image

    :params: vm_name: VM name
    :params: snap_name: Snapshot name
    """

    domxml = virsh.dumpxml(vm_name).stdout.strip()
    xtf_dom = xml_utils.XMLTreeFile(domxml)
    # Check whether qemu-img need add -U suboption since locking feature was added afterwards qemu-2.10
    qemu_img_locking_feature_support = libvirt_storage.check_qemu_image_lock_support(
    )

    cmd = "qemu-img info " + xtf_dom.find("devices/disk/source").get("file")
    if qemu_img_locking_feature_support:
        cmd = "qemu-img info -U " + xtf_dom.find("devices/disk/source").get(
            "file")
    img_info = process.getoutput(cmd).strip()

    if re.search(snap_name, img_info):
        logging.info("Find snapshot info in image")
        return True
    else:
        return False
Example #16
0
    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)
Example #17
0
 def list_mount_points():
     """
     Lists the mount points.
     """
     return [
         line.split()[2]
         for line in process.getoutput('mount').splitlines()
     ]
Example #18
0
    def get_ovs_ports(ovs):
        '''
        get the ovs bridge all Interface list.

        :param ovs: Ovs bridge name
        '''
        cmd = "ovs-vsctl list-ports %s" % ovs
        return process.getoutput(cmd, shell=True)
Example #19
0
 def get_wwpn(self, fc_host):
     '''
     find and return the wwpn of a fc_host
     '''
     cmd = 'cat /sys/class/fc_host/%s/port_name' % fc_host
     wwpn = process.getoutput(cmd)[2:]
     wwpn = ':'.join([wwpn[i:i+2] for i in range(0, len(wwpn), 2)])
     return wwpn
Example #20
0
    def get_ovs_ports(ovs):
        '''
        get the ovs bridge all Interface list.

        :param ovs: Ovs bridge name
        '''
        cmd = "ovs-vsctl list-ports %s" % ovs
        return process.getoutput(cmd, shell=True)
Example #21
0
 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 is_mpath_flushed(self):
     '''
     returns True if multipath is flushed else false
     '''
     process.system("multipath -F", ignore_status=True)
     cmd = "lsmod | grep -i ^%s" % self.module
     if process.getoutput(cmd).split(" ")[-1] == '0':
         return True
     return False
 def get_fc_host(self, path):
     '''
     find and returns fc_host for given disk
     '''
     cmd = 'ls -l /sys/block/ | grep -i %s' % path
     out = process.getoutput(cmd)
     for line in out.split("/"):
         if "host" in line:
             return line
Example #24
0
def run(test, params, env):
    """
    Test cpu flag intel-pt.
    1) Check if current flags are in the supported lists, if no, cancel test
    2) Otherwise, set pt_mode = 1 on host at first
    3) Boot guest with cpu model 'host' without intel-pt.
    4) Check cpu flags in guest(only for linux guest)
    5) For q35
       5.1) boot guest with cpu model with intel-pt
    6) For pc
       6.1) boot guest with intel-pt and min-level=0x14
    7) Check cpu flags in guest(only for linux guest)
    8) Restore pt_mode value on host at last

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    flags = params["flags"]
    check_cpu_flags(params, flags, test)

    set_pt_mode = params.get("set_pt_mode")
    get_pt_mode = params.get("get_pt_mode")
    origin_value = process.getoutput(get_pt_mode).strip()

    try:
        if origin_value != '1':
            process.system(set_pt_mode % '1', shell=True)
        pt_mode = process.getoutput(get_pt_mode).strip()
        if pt_mode != '1':
            test.cancel("pt_mode can't be set to 1")

        params["start_vm"] = "yes"
        env_process.preprocess(test, params, env)
        vm = env.get_vm(params["main_vm"])
        error_context.context("Try to log into guest", test.log.info)
        session = vm.wait_for_login()
        if params["os_type"] == "linux":
            check_cpu_flags(params, flags, test, session)
        vm.verify_kernel_crash()
        session.close()
        utils_misc.wait_for(vm.destroy, 240)
    finally:
        process.system(set_pt_mode % origin_value, shell=True)
Example #25
0
def run(test, params, env):
    """
    Timer device tscwrite test:

    1) Check for an appropriate clocksource on host.
    2) Boot the guest.
    3) Download and compile the newest msr-tools.
    4) Execute cmd in guest.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    error_context.context("Check for an appropriate clocksource on host",
                          logging.info)
    host_cmd = "cat /sys/devices/system/clocksource/"
    host_cmd += "clocksource0/current_clocksource"
    if "tsc" not in process.getoutput(host_cmd):
        test.cancel("Host must use 'tsc' clocksource")

    error_context.context("Boot the guest", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error_context.context("Download and compile the newest msr-tools",
                          logging.info)
    tarball = params["tarball"]
    compile_cmd = params["compile_cmd"]
    msr_name = params["msr_name"]
    tarball = os.path.join(data_dir.get_deps_dir(), tarball)
    msr_dir = "/tmp/"
    vm.copy_files_to(tarball, msr_dir)
    session.cmd("cd %s && tar -zxvf %s" % (msr_dir, os.path.basename(tarball)))
    session.cmd("cd %s && %s" % (msr_name, compile_cmd))

    error_context.context("Execute cmd in guest", logging.info)
    cmd = "dmesg -c > /dev/null"
    session.cmd(cmd)

    date_cmd = "strace date 2>&1 | egrep 'clock_gettime|gettimeofday' | wc -l"
    output = session.cmd(date_cmd)
    if '0' not in output:
        test.fail("Test failed before run msr tools. Output: '%s'" % output)

    msr_tools_cmd = params["msr_tools_cmd"]
    session.cmd(msr_tools_cmd)

    cmd = "dmesg"
    session.cmd(cmd)

    output = session.cmd(date_cmd)
    if "1" not in output:
        test.fail("Test failed after run msr tools. Output: '%s'" % output)
def run(test, params, env):
    """
    Test long time virtio serial guest file transfer.

    Steps:
    1) Boot up a VM with virtio serial device.
    2) Create a large file in guest or host(sender).
    3) In 'repeat_time',  repeatedly transfer the file between guest and host
    4) Kill the data transfer process
    5) Check guest running well, no crash

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """

    os_type = params["os_type"]
    sender = params['file_sender']
    file_size = int(params.get("filesize", 100))
    continue_time = int(params.get("continue_transfer_time", 600))
    vm = env.get_vm(params["main_vm"])
    session = vm.wait_for_login()
    host_dir = data_dir.get_tmp_dir()
    guest_dir = params.get("tmp_dir", '/var/tmp/')
    host_file_size, guest_file_size, _, _ =\
        get_command_options(sender, file_size)
    host_file_name = generate_data_file(host_dir, host_file_size)
    guest_file_name = generate_data_file(
        guest_dir, guest_file_size, session)

    check_pid_cmd = 'ps aux | grep "%s"| grep -v "grep"'
    host_script = params['host_script']
    guest_script = params["guest_script"]
    logging.info('Transfer data from %s', sender)
    try:
        test_time = time.time() + continue_time
        while time.time() < test_time:
            transfer_data(
                params, vm, host_file_name, guest_file_name, sender, False)
        host_proc = process.getoutput(check_pid_cmd % host_script, shell=True)
        guest_proc = session.cmd_output(check_pid_cmd % guest_script)
        if host_proc:
            host_pid = host_proc.split()[1]
            logging.info("Kill serial process on host")
            os.kill(int(host_pid), signal.SIGINT)
        if guest_proc:
            guest_pid = guest_proc.split()[1]
            logging.info("Kill serial process on guest")
            session.cmd('kill -9 %s' % guest_pid)
    finally:
        clean_cmd = params['clean_cmd']
        session.cmd('%s %s' % (clean_cmd, guest_file_name))
        os.remove(host_file_name)
        session.close()
        vm.verify_kernel_crash()
        vm.destroy()
def get_abstract_address(hostfile):
    """
    Only for unix socket

    :param hostfile: the unix socket path in command
    :return: Abstract hostfile address for unix socket
    """
    find_cmd = "cat /proc/net/unix | grep '%s'" % hostfile
    abstract_hostfile = process.getoutput(find_cmd).strip().split(' ')[-1]
    return abstract_hostfile
Example #28
0
def get_mpath_status(mpath):
    """
    Get the status of mpathX of multipaths.

    :param mpath: mpath names. Example: mpatha, mpathb.
    :return: state of mpathX eg: Active, Suspend, None
    """
    cmd = f'multipathd -k"show maps status" | grep -i {mpath}'
    mpath_status = process.getoutput(cmd).split()[-2]
    return mpath_status
 def get_paths(self, fc_host):
     '''
     returns the list of paths coressponding to the given fc_host
     '''
     paths = []
     cmd = 'ls -l /sys/block/ | grep -i %s' % fc_host
     for line in process.getoutput(cmd):
         if "/%s/" % fc_host in line:
             paths.append(line.split("/")[-1])
     return paths
Example #30
0
 def built_in_module(self, module):
     """
     checking whether the given module is built_in module or not
     """
     path = "/lib/modules/%s/modules.builtin" % self.uname
     for each in genio.read_all_lines(path):
         out = process.getoutput(each.split('/')[-1])
         if module == out.split('.'[0]):
             return True
         return False
Example #31
0
 def get_microcode_ver(cmd, session=None):
     """
     Get microcde version in guest or host
     """
     if session:
         output = session.cmd_output(cmd)
     else:
         output = process.getoutput(cmd, shell=True)
     ver = re.findall(r":\s*(0x[0-9A-Fa-f]+)", output)[0]
     return ver
Example #32
0
def run(test, params, env):
    """
    Timer device tscwrite test:

    1) Check for an appropriate clocksource on host.
    2) Boot the guest.
    3) Download and compile the newest msr-tools.
    4) Execute cmd in guest.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    error_context.context("Check for an appropriate clocksource on host",
                          logging.info)
    host_cmd = "cat /sys/devices/system/clocksource/"
    host_cmd += "clocksource0/current_clocksource"
    if "tsc" not in process.getoutput(host_cmd):
        test.cancel("Host must use 'tsc' clocksource")

    error_context.context("Boot the guest", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error_context.context("Download and compile the newest msr-tools",
                          logging.info)
    msr_tools_install_cmd = params["msr_tools_install_cmd"]
    session.cmd(msr_tools_install_cmd)

    error_context.context("Execute cmd in guest", logging.info)
    cmd = "dmesg -c > /dev/null"
    session.cmd(cmd)

    date_cmd = "strace date 2>&1 | egrep 'clock_gettime|gettimeofday' | wc -l"
    output = session.cmd(date_cmd)
    if '0' not in output:
        test.fail("Test failed before run msr tools. Output: '%s'" % output)

    msr_tools_cmd = params["msr_tools_cmd"]
    session.cmd(msr_tools_cmd)

    cmd = "dmesg"
    session.cmd(cmd)

    output = session.cmd(date_cmd)
    if "1" not in output:
        test.fail("Test failed after run msr tools. Output: '%s'" % output)
def execute(cmd, timeout=360, session=None):
    """
    Execute command in guest or host, if session is not None return
    command output in guest else return command ouput in host

    :param cmd: Shell commands
    :param timeout: Timeout to execute command
    :param session: ShellSession or None

    :return: Command output string
    """
    if session:
        ret = session.cmd_output_safe(cmd, timeout=timeout)
    else:
        ret = process.getoutput(cmd)
    target = 'guest' if session else 'host'
    logging.debug("(%s) Execute command('%s')" % (target, cmd))
    return ret
Example #34
0
def netcf_trans_control(test, command="status"):
    """
    Control current network configuration
    :param: command: it may be 'status', 'snapshot-dir', restart,
            change-begin, etc, Note that is the netcf-libs is required
    :returns: return command result
    """
    try:
        # For OS using systemd, this command usually located in /usr/libexec/.
        cmd = utils_path.find_command("netcf-transaction.sh")
    except utils_path.CmdNotFoundError:
        # This is the default location for sysV init.
        old_path = "/etc/rc.d/init.d/netcf-transaction"
        if os.path.isfile(old_path):
            cmd = old_path
        else:
            test.cancel("Cannot find netcf-transaction! "
                        "Make sure you have netcf-libs installed!")
    logging.debug(cmd)

    return process.getoutput("%s %s" % (cmd, command), shell=True)
def check_snap_in_image(vm_name, snap_name):
    """
    check the snapshot info in image

    :params: vm_name: VM name
    :params: snap_name: Snapshot name
    """

    domxml = virsh.dumpxml(vm_name).stdout.strip()
    xtf_dom = xml_utils.XMLTreeFile(domxml)
    # Check whether qemu-img need add -U suboption since locking feature was added afterwards qemu-2.10
    qemu_img_locking_feature_support = libvirt_storage.check_qemu_image_lock_support()

    cmd = "qemu-img info " + xtf_dom.find("devices/disk/source").get("file")
    if qemu_img_locking_feature_support:
        cmd = "qemu-img info -U " + xtf_dom.find("devices/disk/source").get("file")
    img_info = process.getoutput(cmd).strip()

    if re.search(snap_name, img_info):
        logging.info("Find snapshot info in image")
        return True
    else:
        return False
Example #36
0
def run(test, params, env):
    """
    Test virsh create command including parameters except --pass-fds
    because it is used in lxc.

    Basic test scenarios:
    1. --console with all combination(with other options)
    2. --autodestroy with left combination
    3. --paused itself
    """
    vm_name = params.get("main_vm")
    options = params.get("create_options", "")
    status_error = ("yes" == params.get("status_error", "no"))
    c_user = params.get("create_login_user", "root")
    readonly = params.get("readonly", False)
    if c_user == "root":
        c_passwd = params.get("password")
    else:
        c_passwd = params.get("create_login_password_nonroot")

    vm = env.get_vm(vm_name)
    if vm.exists():
        if vm.is_alive():
            vm.destroy()
        xmlfile = vm.backup_xml()
        vm.undefine()
    else:
        xmlfile = params.get("create_domain_xmlfile")
        if xmlfile is None:
            test.fail("Please provide domain xml file for create or"
                      " existing domain name with main_vm = xx")
        #get vm name from xml file
        xml_cut = process.getoutput("grep '<name>.*</name>' %s" % xmlfile, shell=True)
        vm_name = xml_cut.strip(' <>').strip("name").strip("<>/")
        logging.debug("vm_name is %s", vm_name)
        vm = env.get_vm(vm_name)

    try:
        def create_status_check(vm):
            """
            check guest status
            1. if have options --paused: check status and resume
            2. check if guest is running after 1
            """
            if "--paused" in options:
                # make sure guest is paused
                ret = utils_misc.wait_for(lambda: vm.is_paused(), 30)
                if not ret:
                    test.fail("Guest status is not paused with"
                              "options %s, state is %s" %
                              (options, vm.state()))
                else:
                    logging.info("Guest status is paused.")
                vm.resume()

            # make sure guest is running
            ret = utils_misc.wait_for(lambda: vm.state() == "running", 30)
            if ret:
                logging.info("Guest is running now.")
            else:
                test.fail("Fail to create guest, guest state is %s"
                          % vm.state())

        def create_autodestroy_check(vm):
            """
            check if guest will disappear with --autodestroy
            """
            # make sure guest is auto destroyed
            ret = utils_misc.wait_for(lambda: not vm.exists(), 30)
            if not ret:
                test.fail("Guest still exist with options %s" %
                          options)
            else:
                logging.info("Guest does not exist after session closed.")

        try:
            if status_error:
                output = virsh.create(xmlfile, options, readonly=readonly)
                if output.exit_status:
                    logging.info("Fail to create guest as expect:%s",
                                 output.stderr)
                if vm.state() == "running":
                    test.fail("Expect fail, but succeed indeed")
            elif "--console" in options:
                # Use session for console
                command = "virsh create %s %s" % (xmlfile, options)
                session = aexpect.ShellSession(command)
                # check domain status including paused and running
                create_status_check(vm)
                status = utils_test.libvirt.verify_virsh_console(
                    session, c_user, c_passwd, timeout=90, debug=True)
                if not status:
                    test.fail("Fail to verify console")

                session.close()
                # check if domain exist after session closed
                if "--autodestroy" in options:
                    create_autodestroy_check(vm)

            elif "--autodestroy" in options:
                # Use session for virsh interactive mode because
                # guest will be destroyed after virsh exit
                command = "virsh"
                session = aexpect.ShellSession(command)
                while True:
                    match, text = session.read_until_any_line_matches(
                        [r"Domain \S+ created from %s" % xmlfile, r"virsh # "],
                        timeout=10, internal_timeout=1)
                    if match == -1:
                        logging.info("Run create %s %s", xmlfile, options)
                        command = "create %s %s" % (xmlfile, options)
                        session.sendline(command)
                    elif match == -2:
                        logging.info("Domain created from %s", xmlfile)
                        break
                create_status_check(vm)
                logging.info("Close session!")
                session.close()
                # check if domain exist after session closed
                create_autodestroy_check(vm)
            else:
                # have --paused option or none options
                output = virsh.create(xmlfile, options)
                if output.exit_status:
                    test.fail("Fail to create domain:%s" %
                              output.stderr)
                create_status_check(vm)

        except (aexpect.ShellError, aexpect.ExpectError) as detail:
            log = session.get_output()
            session.close()
            vm.define(xmlfile)
            test.fail("Verify create failed:\n%s\n%s" %
                      (detail, log))
    finally:
        #Guest recovery
        vm.define(xmlfile)
Example #37
0
def run(test, params, env):
    """
    Virsh create test with --pass-fds for container
    """
    fds_options = params.get("create_lxc_fds_options", "")
    other_options = params.get("create_lxc_other_options", "")
    uri = params.get("connect_uri", "lxc:///")
    vm_name = params.get("vms")
    vcpu = params.get("create_lxc_vcpu", 1)
    max_mem = params.get("create_lxc_maxmem", 500000)
    cur_mem = params.get("create_lxc_curmem", 500000)
    dom_type = params.get("create_lxc_domtype", "lxc")
    os_type = params.get("create_lxc_ostype", "exe")
    os_arch = params.get("create_lxc_osarch", "x86_64")
    os_init = params.get("create_lxc_osinit", "/bin/sh")
    emulator_path = params.get("create_lxc_emulator",
                               "/usr/libexec/libvirt_lxc")
    tmpfile1 = params.get("create_lxc_tmpfile1", "/tmp/foo")
    tmpfile2 = params.get("create_lxc_tmpfile2", "/tmp/bar")
    tmpfile3 = params.get("create_lxc_tmpfile3", "/tmp/wizz")

    def container_xml_generator():
        """
        Generate container xml
        """
        vmxml = vm_xml.VMXML(dom_type)
        vmxml.vm_name = vm_name
        vmxml.max_mem = max_mem
        vmxml.current_mem = cur_mem
        vmxml.vcpu = vcpu
        osxml = vmxml.os
        osxml.type = os_type
        osxml.arch = os_arch
        osxml.init = os_init
        vmxml.os = osxml
        # Generate emulator
        emulator = Emulator()
        emulator.path = emulator_path
        # Generate console
        console = Console()
        # Add emulator and console in devices
        devices = vm_xml.VMXMLDevices()
        devices.append(emulator)
        devices.append(console)
        logging.debug("device is %s", devices)
        vmxml.set_devices(devices)
        return vmxml

    fd1 = open(tmpfile1, 'w')
    fd2 = open(tmpfile2, 'w')
    fd3 = open(tmpfile3, 'w')

    try:
        options = "%s %s,%s,%s %s" % (fds_options, fd1.fileno(), fd2.fileno(),
                                      fd3.fileno(), other_options)
        vmxml = container_xml_generator()
        logging.debug("xml is %s", process.getoutput("cat %s" % vmxml.xml))
        if "--console" not in options:
            output = virsh.create(vmxml.xml, options, uri=uri)
            if output.exit_status:
                test.fail("Create %s domain failed:%s" %
                          (dom_type, output.stderr))
            logging.info("Domain %s created, will check with console", vm_name)
            command = "virsh -c %s console %s" % (uri, vm_name)
        else:
            command = "virsh -c %s create %s %s" % (uri, vmxml.xml, options)

        session = aexpect.ShellSession(command)
        time.sleep(2)
        for i in (tmpfile1, tmpfile2, tmpfile3):
            lsofcmd = "lsof|grep '^sh.*%s'" % i
            cmd_status, cmd_output = session.cmd_status_output(lsofcmd)
            if cmd_status != 0:
                test.fail("Can not find file %s in container" % i)
            else:
                logging.info("Find open file in guest: %s", cmd_output)

        session.close()
        vm = env.get_vm(vm_name)
        if "--autodestroy" in options:
            if vm.is_alive():
                test.fail("Guest still exist after close session "
                          "with option --autodestroy")
            logging.info("Guest already destroyed after session closed")
        elif not vm.is_alive():
            test.fail("Guest is not running after close session!")
        else:
            logging.info("Guest still exist after session closed")

    finally:
        fd1.close()
        fd2.close()
        fd3.close()
        os.remove(tmpfile1)
        os.remove(tmpfile2)
        os.remove(tmpfile3)
def run(test, params, env):
    """
    Timer device check clock frequency offset using chrony on CPU starved guest:

    1) Check for an appropriate clocksource on host.
    2) Boot the guest.
    3) Copy time-warp-test.c to guest.
    4) Compile the time-warp-test.c.
    5) Stop chronyd and apply load on guest.
    6) Pin every vcpu to a physical cpu.
    7) Verify each vcpu is pinned on host.
    8) Run time-warp-test on guest.
    9) Start chronyd on guest.
    10) Check the drift in /var/lib/chrony/drift file on guest after hours
        of running.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    def _drift_file_exist():
        try:
            session.cmd("test -f /var/lib/chrony/drift")
            return True
        except Exception:
            return False

    error_context.context("Check for an appropriate clocksource on host",
                          logging.info)
    host_cmd = "cat /sys/devices/system/clocksource/"
    host_cmd += "clocksource0/current_clocksource"
    if "tsc" not in process.getoutput(host_cmd):
        test.cancel("Host must use 'tsc' clocksource")

    error_context.context("Boot the guest", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    sess_guest_load = vm.wait_for_login(timeout=timeout)

    error_context.context("Copy time-warp-test.c to guest", logging.info)
    src_file_name = os.path.join(data_dir.get_deps_dir(), "tsc_sync",
                                 "time-warp-test.c")
    vm.copy_files_to(src_file_name, "/tmp")

    error_context.context("Compile the time-warp-test.c", logging.info)
    cmd = "cd /tmp/;"
    cmd += " yum install -y popt-devel;"
    cmd += " rm -f time-warp-test;"
    cmd += " gcc -Wall -o time-warp-test time-warp-test.c -lrt"
    sess_guest_load.cmd(cmd)

    error_context.context("Stop chronyd and apply load on guest", logging.info)
    sess_guest_load.cmd("systemctl stop chronyd")
    load_cmd = "for ((I=0; I<`grep 'processor id' /proc/cpuinfo| wc -l`; I++));"
    load_cmd += " do taskset $(( 1 << $I )) /bin/bash -c 'for ((;;)); do X=1; done &';"
    load_cmd += " done"
    sess_guest_load.cmd(load_cmd)

    error_context.context("Pin every vcpu to a physical cpu", logging.info)
    host_cpu_cnt_cmd = params["host_cpu_cnt_cmd"]
    host_cpu_num = process.system_output(host_cpu_cnt_cmd, shell=True).strip()
    host_cpu_list = (_ for _ in range(int(host_cpu_num)))
    cpu_pin_list = list(zip(vm.vcpu_threads, host_cpu_list))
    if len(cpu_pin_list) < len(vm.vcpu_threads):
        test.cancel("There isn't enough physical cpu to pin all the vcpus")
    for vcpu, pcpu in cpu_pin_list:
        process.system("taskset -p %s %s" % (1 << pcpu, vcpu))

    error_context.context("Verify each vcpu is pinned on host", logging.info)

    error_context.context("Run time-warp-test", logging.info)
    session = vm.wait_for_login(timeout=timeout)
    cmd = "/tmp/time-warp-test > /dev/null &"
    session.cmd(cmd)

    error_context.context("Start chronyd on guest", logging.info)
    cmd = "systemctl start chronyd; sleep 1; echo"
    session.cmd(cmd)

    error_context.context("Check if the drift file exists on guest",
                          logging.info)
    test_run_timeout = float(params["test_run_timeout"])
    try:
        utils_misc.wait_for(_drift_file_exist, test_run_timeout, step=5)
    except aexpect.ShellCmdError as detail:
        test.error("Failed to wait for the creation of"
                   " /var/lib/chronyd/drift file. Detail: '%s'" % detail)

    error_context.context("Verify the drift file content on guest",
                          logging.info)
    output = session.cmd("cat /var/lib/chrony/drift").strip().split()[0]
    if int(abs(float(output))) > 20:
        test.fail("Failed to check the chrony drift. Output: '%s'" % output)
Example #39
0
def run(test, params, env):
    """
    Test the RX jumbo frame function of vnics:

    1) Boot the VM.
    2) Change the MTU of guest nics and host taps depending on the NIC model.
    3) Add the static ARP entry for guest NIC.
    4) Wait for the MTU ok.
    5) Verify the path MTU using ping.
    6) Ping the guest with large frames.
    7) Increment size ping.
    8) Flood ping the guest with large frames.
    9) Verify the path MTU.
    10) Recover the MTU.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def get_ovs_ports(ovs):
        '''
        get the ovs bridge all Interface list.

        :param ovs: Ovs bridge name
        '''
        cmd = "ovs-vsctl list-ports %s" % ovs
        return process.getoutput(cmd, shell=True)

    netdst = params.get("netdst", "switch")
    host_bridges = utils_net.find_bridge_manager(netdst)
    if not isinstance(host_bridges, utils_net.Bridge):
        ovs = host_bridges
        host_hw_interface = get_ovs_ports(netdst)
        tmp_ports = re.findall(r"t[0-9]{1,}-[a-zA-Z0-9]{6}", host_hw_interface)
        if tmp_ports:
            for p in tmp_ports:
                process.system_output("ovs-vsctl del-port %s %s" %
                                      (netdst, p))

    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params["main_vm"])

    timeout = int(params.get("login_timeout", 360))
    mtu_default = 1500
    mtu = params.get("mtu", "1500")
    def_max_icmp_size = int(mtu) - 28
    max_icmp_pkt_size = int(params.get("max_icmp_pkt_size",
                                       def_max_icmp_size))
    flood_time = params.get("flood_time", "300")
    os_type = params.get("os_type")
    os_variant = params.get("os_variant")

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session = vm.wait_for_login(timeout=timeout)
    session_serial = vm.wait_for_serial_login(timeout=timeout)

    ifname = vm.get_ifname(0)
    guest_ip = vm.get_address(0)
    if guest_ip is None:
        test.error("Could not get the guest ip address")

    host_mtu_cmd = "ifconfig %s mtu %s"
    if not isinstance(host_bridges, utils_net.Bridge):
        target_ifaces = set(get_ovs_ports(netdst).splitlines())
    else:
        br_in_use = host_bridges.list_br()
        ifaces_in_use = host_bridges.list_iface()
        target_ifaces = set(ifaces_in_use) - set(br_in_use)

    error_context.context("Change all Bridge NICs MTU to %s" %
                          mtu, logging.info)
    for iface in target_ifaces:
        process.run(host_mtu_cmd % (iface, mtu), shell=True)

    try:
        error_context.context("Changing the MTU of guest", logging.info)
        # Environment preparation
        mac = vm.get_mac_address(0)
        if os_type == "linux":
            ethname = utils_net.get_linux_ifname(session, mac)
            guest_mtu_cmd = "ifconfig %s mtu %s" % (ethname, mtu)
        else:
            connection_id = utils_net.get_windows_nic_attribute(
                session, "macaddress", mac, "netconnectionid")

            index = utils_net.get_windows_nic_attribute(
                session, "netconnectionid", connection_id, "index")
            if os_variant == "winxp":
                pnpdevice_id = utils_net.get_windows_nic_attribute(
                    session, "netconnectionid", connection_id, "pnpdeviceid")
                cd_num = utils_misc.get_winutils_vol(session)
                copy_cmd = r"xcopy %s:\devcon\wxp_x86\devcon.exe c:\ " % cd_num
                session.cmd(copy_cmd)

            reg_set_mtu_pattern = params.get("reg_mtu_cmd")
            mtu_key_word = params.get("mtu_key", "MTU")
            reg_set_mtu = reg_set_mtu_pattern % (int(index), mtu_key_word,
                                                 int(mtu))
            guest_mtu_cmd = "%s " % reg_set_mtu

        session.cmd(guest_mtu_cmd)
        if os_type == "windows":
            mode = "netsh"
            if os_variant == "winxp":
                connection_id = pnpdevice_id.split("&")[-1]
                mode = "devcon"
            utils_net.restart_windows_guest_network(session_serial,
                                                    connection_id,
                                                    mode=mode)

        error_context.context("Chaning the MTU of host tap ...", logging.info)
        host_mtu_cmd = "ifconfig %s mtu %s"
        # Before change macvtap mtu, must set the base interface mtu
        if params.get("nettype") == "macvtap":
            base_if = utils_net.get_macvtap_base_iface(params.get("netdst"))
            process.run(host_mtu_cmd % (base_if, mtu), shell=True)
        process.run(host_mtu_cmd % (ifname, mtu), shell=True)

        error_context.context("Add a temporary static ARP entry ...",
                              logging.info)
        arp_add_cmd = "arp -s %s %s -i %s" % (guest_ip, mac, ifname)
        process.run(arp_add_cmd, shell=True)

        def is_mtu_ok():
            status, _ = utils_test.ping(guest_ip, 1,
                                        packetsize=max_icmp_pkt_size,
                                        hint="do", timeout=2)
            return status == 0

        def verify_mtu():
            logging.info("Verify the path MTU")
            status, output = utils_test.ping(guest_ip, 10,
                                             packetsize=max_icmp_pkt_size,
                                             hint="do", timeout=15)
            if status != 0:
                logging.error(output)
                test.fail("Path MTU is not as expected")
            if utils_test.get_loss_ratio(output) != 0:
                logging.error(output)
                test.fail("Packet loss ratio during MTU "
                          "verification is not zero")

        def flood_ping():
            logging.info("Flood with large frames")
            utils_test.ping(guest_ip,
                            packetsize=max_icmp_pkt_size,
                            flood=True, timeout=float(flood_time))

        def large_frame_ping(count=100):
            logging.info("Large frame ping")
            _, output = utils_test.ping(guest_ip, count,
                                        packetsize=max_icmp_pkt_size,
                                        timeout=float(count) * 2)
            ratio = utils_test.get_loss_ratio(output)
            if ratio != 0:
                test.fail("Loss ratio of large frame ping is %s" % ratio)

        def size_increase_ping(step=random.randrange(90, 110)):
            logging.info("Size increase ping")
            for size in range(0, max_icmp_pkt_size + 1, step):
                logging.info("Ping %s with size %s", guest_ip, size)
                status, output = utils_test.ping(guest_ip, 1,
                                                 packetsize=size,
                                                 hint="do", timeout=1)
                if status != 0:
                    status, output = utils_test.ping(guest_ip, 10,
                                                     packetsize=size,
                                                     adaptive=True,
                                                     hint="do",
                                                     timeout=20)

                    fail_ratio = int(params.get("fail_ratio", 50))
                    if utils_test.get_loss_ratio(output) > fail_ratio:
                        test.fail("Ping loss ratio is greater "
                                  "than 50% for size %s" % size)

        logging.info("Waiting for the MTU to be OK")
        wait_mtu_ok = 10
        if not utils_misc.wait_for(is_mtu_ok, wait_mtu_ok, 0, 1):
            logging.debug(process.getoutput("ifconfig -a",
                                            verbose=False,
                                            ignore_status=True,
                                            shell=True))
            test.error("MTU is not as expected even after %s "
                       "seconds" % wait_mtu_ok)

        # Functional Test
        error_context.context("Checking whether MTU change is ok",
                              logging.info)
        verify_mtu()
        large_frame_ping()
        size_increase_ping()

        # Stress test
        flood_ping()
        verify_mtu()

    finally:
        # Environment clean
        if session:
            session.close()
        grep_cmd = "grep '%s.*%s' /proc/net/arp" % (guest_ip, ifname)
        if process.system(grep_cmd, shell=True) == '0':
            process.run("arp -d %s -i %s" % (guest_ip, ifname),
                        shell=True)
            logging.info("Removing the temporary ARP entry successfully")

        logging.info("Change back Bridge NICs MTU to %s" % mtu_default)
        for iface in target_ifaces:
            process.run(host_mtu_cmd % (iface, mtu_default), shell=True)
def run(test, params, env):
    """
    Timer device check TSC synchronity after change host clocksource:

    1) Check for an appropriate clocksource on host.
    2) Boot the guest.
    3) Check the guest is using vsyscall.
    4) Copy time-warp-test.c to guest.
    5) Compile the time-warp-test.c.
    6) Switch host to hpet clocksource.
    6) Run time-warp-test.
    7) Check the guest is not using vsyscall.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    error_context.context("Check for an appropriate clocksource on host",
                          logging.info)
    host_cmd = "cat /sys/devices/system/clocksource/"
    host_cmd += "clocksource0/current_clocksource"
    if "tsc" not in process.getoutput(host_cmd):
        test.cancel("Host must use 'tsc' clocksource")

    error_context.context("Boot the guest with one cpu socket", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error_context.context("Check the guest is using vsyscall", logging.info)
    date_cmd = "strace date 2>&1|egrep 'clock_gettime|gettimeofday'|wc -l"
    output = session.cmd(date_cmd)
    if '0' not in output:
        test.fail("Failed to check vsyscall. Output: '%s'" % output)

    error_context.context("Copy time-warp-test.c to guest", logging.info)
    src_file_name = os.path.join(data_dir.get_deps_dir(), "tsc_sync",
                                 "time-warp-test.c")
    vm.copy_files_to(src_file_name, "/tmp")

    error_context.context("Compile the time-warp-test.c", logging.info)
    cmd = "cd /tmp/;"
    cmd += " yum install -y popt-devel;"
    cmd += " rm -f time-warp-test;"
    cmd += " gcc -Wall -o time-warp-test time-warp-test.c -lrt"
    session.cmd(cmd)

    error_context.context("Run time-warp-test", logging.info)
    test_run_timeout = int(params.get("test_run_timeout", 10))
    session.sendline("$(sleep %d; pkill time-warp-test) &" % test_run_timeout)
    cmd = "/tmp/time-warp-test"
    output = session.cmd_status_output(cmd, timeout=(test_run_timeout + 60))[1]

    re_str = r"fail:(\d+).*?fail:(\d+).*fail:(\d+)"
    fail_cnt = re.findall(re_str, output)
    if not fail_cnt:
        test.error("Could not get correct test output. Output: '%s'" % output)

    tsc_cnt, tod_cnt, clk_cnt = [int(_) for _ in fail_cnt[-1]]
    if tsc_cnt or tod_cnt or clk_cnt:
        msg = output.splitlines()[-5:]
        test.fail("Get error when running time-warp-test."
                  " Output (last 5 lines): '%s'" % msg)

    try:
        error_context.context("Switch host to hpet clocksource", logging.info)
        cmd = "echo hpet > /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        process.system(cmd, shell=True)

        error_context.context("Run time-warp-test after change the host"
                              " clock source", logging.info)
        cmd = "$(sleep %d; pkill time-warp-test) &"
        session.sendline(cmd % test_run_timeout)
        cmd = "/tmp/time-warp-test"
        output = session.cmd_status_output(cmd,
                                           timeout=(test_run_timeout + 60))[1]

        fail_cnt = re.findall(re_str, output)
        if not fail_cnt:
            test.error("Could not get correct test output."
                       " Output: '%s'" % output)

        tsc_cnt, tod_cnt, clk_cnt = [int(_) for _ in fail_cnt[-1]]
        if tsc_cnt or tod_cnt or clk_cnt:
            msg = output.splitlines()[-5:]
            test.fail("Get error when running time-warp-test."
                      " Output (last 5 lines): '%s'" % msg)

        output = session.cmd(date_cmd)
        if "1" not in output:
            test.fail("Failed to check vsyscall. Output: '%s'" % output)
    finally:
        error_context.context("Restore host to tsc clocksource", logging.info)
        cmd = "echo tsc > /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        try:
            process.system(cmd, shell=True)
        except Exception as detail:
            logging.error("Failed to restore host clocksource."
                          "Detail: %s" % detail)
def run(test, params, env):
    """
    Timer device check TSC synchronity for long time test:

    1) Check for an appropriate clocksource on host.
    2) Check host has more than one cpu socket.
    3) Boot the guest with specified cpu socket.
    4) Copy time-warp-test.c to guest.
    5) Compile the time-warp-test.c.
    6) Run time-warp-test for minimum 4 hours.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    error_context.context("Check for an appropriate clocksource on host",
                          logging.info)
    host_cmd = "cat /sys/devices/system/clocksource/"
    host_cmd += "clocksource0/current_clocksource"
    if "tsc" not in process.getoutput(host_cmd):
        test.cancel("Host must use 'tsc' clocksource")

    error_context.context("Check host has more than one cpu socket",
                          logging.info)
    host_socket_cnt_cmd = params["host_socket_cnt_cmd"]
    if process.system_output(host_socket_cnt_cmd, shell=True).strip() == "1":
        test.cancel("Host must have more than 1 socket")

    error_context.context("Boot the guest with one cpu socket", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error_context.context("Copy time-warp-test.c to guest", logging.info)
    src_file_name = os.path.join(data_dir.get_deps_dir(), "tsc_sync",
                                 "time-warp-test.c")
    vm.copy_files_to(src_file_name, "/tmp")

    error_context.context("Compile the time-warp-test.c", logging.info)
    cmd = "cd /tmp/;"
    cmd += " yum install -y popt-devel;"
    cmd += " rm -f time-warp-test;"
    cmd += " gcc -Wall -o time-warp-test time-warp-test.c -lrt"
    session.cmd(cmd)

    error_context.context("Run time-warp-test for minimum 4 hours",
                          logging.info)
    test_run_timeout = int(params.get("test_run_timeout", 14400))
    session.sendline("$(sleep %d; pkill time-warp-test) &" % test_run_timeout)
    cmd = "/tmp/time-warp-test"
    output = session.cmd_status_output(cmd, timeout=(test_run_timeout + 60))[1]

    re_str = r"fail:(\d+).*?fail:(\d+).*fail:(\d+)"
    fail_cnt = re.findall(re_str, output)
    if not fail_cnt:
        test.error("Could not get correct test output. Output: '%s'" % output)

    tsc_cnt, tod_cnt, clk_cnt = [int(_) for _ in fail_cnt[-1]]
    if tsc_cnt or tod_cnt or clk_cnt:
        msg = output.splitlines()[-5:]
        test.fail("Get error when running time-warp-test."
                  " Output (last 5 lines): '%s'" % msg)
Example #42
0
def run(test, params, env):
    """
    Test virsh reset command
    """

    if not virsh.has_help_command('reset'):
        test.cancel("This version of libvirt does not support "
                    "the reset test")

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    vm_ref = params.get("reset_vm_ref")
    readonly = params.get("readonly", False)
    status_error = ("yes" == params.get("status_error", "no"))
    start_vm = ("yes" == params.get("start_vm"))

    vm = env.get_vm(vm_name)
    domid = vm.get_id()
    domuuid = vm.get_uuid()
    bef_pid = process.getoutput("pidof -s qemu-kvm")

    if vm_ref == 'id':
        vm_ref = domid
    elif vm_ref == 'uuid':
        vm_ref = domuuid
    else:
        vm_ref = vm_name

    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.")

    # change the disk cache to default
    vmxml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)

    def change_cache(vmxml, mode):
        """
        Change the cache mode

        :param vmxml: instance of VMXML
        :param mode: cache mode you want to change
        """
        devices = vmxml.devices
        disk_index = devices.index(devices.by_device_tag('disk')[0])
        disk = devices[disk_index]
        disk_driver = disk.driver
        disk_driver['cache'] = mode
        disk.driver = disk_driver
        vmxml.devices = devices
        vmxml.define()

    try:
        change_cache(vmxml_backup.copy(), "default")

        tmpfile = "/home/%s" % utils_misc.generate_random_string(6)
        logging.debug("tmpfile is %s", tmpfile)
        if start_vm:
            session = vm.wait_for_login()
            session.cmd("rm -rf %s && sync" % tmpfile)
            status = session.get_command_status("touch %s && ls %s" %
                                                (tmpfile, tmpfile))
            if status == 0:
                logging.info("Succeed generate file %s", tmpfile)
            else:
                test.fail("Touch command failed!")

        # record the pid before reset for compare
        output = virsh.reset(vm_ref, readonly=readonly,
                             unprivileged_user=unprivileged_user,
                             uri=uri, ignore_status=True, debug=True)
        if output.exit_status != 0:
            if status_error:
                logging.info("Failed to reset guest as expected, Error:%s.",
                             output.stderr)
                return
            else:
                test.fail("Failed to reset guest, Error:%s." %
                          output.stderr)
        elif status_error:
            test.fail("Expect fail, but succeed indeed.")

        session.close()
        time.sleep(5)
        session = vm.wait_for_login()
        status = session.get_command_status("ls %s" % tmpfile)
        if status == 0:
            test.fail("Fail to reset guest, tmpfile still exist!")
        else:
            aft_pid = process.getoutput("pidof -s qemu-kvm")
            if bef_pid == aft_pid:
                logging.info("Succeed to check reset, tmpfile is removed.")
            else:
                test.fail("Domain pid changed after reset!")
        session.close()

    finally:
        vmxml_backup.sync()