Example #1
0
def run_virsh_uri(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                               "default") )

    option = params.get("options")
    target_uri = params.get("target_uri")
    if target_uri:
        if target_uri.count('EXAMPLE.COM'):
            raise error.TestError('target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    logging.info("The command: %s", cmd)
    try:
        uri_test = virsh.canonical_uri(option, uri=connect_uri,
                             ignore_status=False,
                             debug=True)
        status = 0 # good
    except error.CmdError:
        status = 1 # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise error.TestFail("Command: %s  succeeded "
                                 "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if cmp(target_uri, uri_test) != 0:
            raise error.TestFail("Virsh cmd uri %s != %s." %
                                 (uri_test, target_uri))
        if status != 0:
            raise error.TestFail("Command: %s  failed "
                                 "(correct command)" % cmd)
Example #2
0
def run_virsh_domuuid(test, params, env):
    """
    Test command: virsh domuuid.
    """
    vm_name = params.get("main_vm", "vm1")
    vm = env.get_vm(vm_name)
    vm.verify_alive()

    # Get parameters
    vm_ref = params.get("domuuid_vm_ref", "domname")
    vm_state = params.get("domuuid_vm_state", "running")
    addition_arg = params.get("domuuid_addition_arg")
    libvirtd = params.get("libvirtd", "on")
    status_error = params.get("status_error", "no")

    domid = vm.get_id()
    vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm_name)
    xml_domuuid = vmxml.uuid
    logging.debug("UUID in XML is:\n%s", xml_domuuid)

    if vm_state == "shutoff":
        vm.destroy()

    # Prepare options
    if vm_ref == "domid":
        vm_ref = domid
    elif vm_ref == "hex_id":
        vm_ref = hex(int(domid))
    elif vm_ref == "domname":
        vm_ref = vm_name

    # Add additional argument
    if vm_ref and addition_arg:
        vm_ref = "%s %s" % (vm_ref, addition_arg)

    # Prepare libvirtd state
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    result = virsh.domuuid(vm_ref)
    logging.debug(result)
    status = result.exit_status
    output = result.stdout.strip()

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    if status_error == "yes":
        if status == 0:
            raise error.TestFail("Run successfully with wrong command!")
    elif status_error == "no":
        if status != 0:
            raise error.TestFail("Run failed with right command.")
        elif xml_domuuid != output:
            raise error.TestFail("UUID from virsh command is not expected.")
Example #3
0
def run_virsh_nodeinfo(test, params, env):
    """
    Test the command virsh nodeinfo

    (1) Call virsh nodeinfo
    (2) Call virsh nodeinfo with an unexpected option
    (3) Call virsh nodeinfo with libvirtd service stop
    """
    def _check_nodeinfo(nodeinfo_output, verify_str, column):
        cmd = "echo \"%s\" | grep \"%s\" | awk '{print $%s}'" % (nodeinfo_output, verify_str, column)
        cmd_result = utils.run(cmd, ignore_status=True)
        stdout = cmd_result.stdout.strip()
        logging.debug("Info %s on nodeinfo output:%s" %  (verify_str, stdout))
        return stdout


    def output_check(nodeinfo_output):
        # Check CPU model
        cpu_model_nodeinfo = _check_nodeinfo(nodeinfo_output, "CPU model", 3)
        cpu_model_os = utils.get_current_kernel_arch()
        if not re.match(cpu_model_nodeinfo, cpu_model_os):
            raise error.TestFail("Virsh nodeinfo output didn't match CPU model")

        # Check number of CPUs
        cpus_nodeinfo = _check_nodeinfo(nodeinfo_output, "CPU(s)", 2)
        cpus_os = utils.count_cpus()
        if  int(cpus_nodeinfo) != cpus_os:
            raise error.TestFail("Virsh nodeinfo output didn't match number of "
                                 "CPU(s)")

        # Check CPU frequency
        cpu_frequency_nodeinfo = _check_nodeinfo(nodeinfo_output, 'CPU frequency', 3)
        cmd = ("cat /proc/cpuinfo | grep 'cpu MHz' | head -n1 | "
               "awk '{print $4}' | awk -F. '{print $1}'")
        cmd_result = utils.run(cmd, ignore_status=True)
        cpu_frequency_os = cmd_result.stdout.strip()
        print cpu_frequency_os
        if not re.match(cpu_frequency_nodeinfo, cpu_frequency_os):
            raise error.TestFail("Virsh nodeinfo output didn't match CPU "
                                 "frequency")

        # Check CPU socket(s)
        cpu_sockets_nodeinfo = int(_check_nodeinfo(nodeinfo_output, 'CPU socket(s)', 3))
        cmd = "grep 'physical id' /proc/cpuinfo | uniq | sort | uniq |wc -l"
        cmd_result = utils.run(cmd, ignore_status=True)
        cpu_NUMA_nodeinfo = _check_nodeinfo(nodeinfo_output, 'NUMA cell(s)', 3)
        cpu_sockets_os = int(cmd_result.stdout.strip())/int(cpu_NUMA_nodeinfo)
        if cpu_sockets_os != cpu_sockets_nodeinfo:
            raise error.TestFail("Virsh nodeinfo output didn't match CPU "
                                 "socket(s)")

        # Check Core(s) per socket
        cores_per_socket_nodeinfo = _check_nodeinfo(nodeinfo_output, 'Core(s) per socket', 4)
        cmd = "grep 'cpu cores' /proc/cpuinfo | head -n1 | awk '{print $4}'"
        cmd_result = utils.run(cmd, ignore_status=True)
        cores_per_socket_os = cmd_result.stdout.strip()
        if not re.match(cores_per_socket_nodeinfo, cores_per_socket_os):
            raise error.TestFail("Virsh nodeinfo output didn't match Core(s) "
                                 "per socket")

        # Check Memory size
        memory_size_nodeinfo = int(_check_nodeinfo(nodeinfo_output, 'Memory size', 3))
        memory_size_os = utils.memtotal()
        if memory_size_nodeinfo != memory_size_os:
            raise error.TestFail("Virsh nodeinfo output didn't match "
                                 "Memory size")


    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    option = params.get("virsh_node_options")
    cmd_result = virsh.nodeinfo(ignore_status=True, extra=option)
    logging.info("Output:\n%s", cmd_result.stdout.strip())
    logging.info("Status: %d", cmd_result.exit_status)
    logging.error("Error: %s", cmd_result.stderr.strip())
    output = cmd_result.stdout.strip()
    status = cmd_result.exit_status


    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail("Command 'virsh nodeinfo' succeeded "
                                     "with libvirtd service stopped, incorrect")
            else:
                raise error.TestFail("Command 'virsh nodeinfo %s' succeeded"
                                     "(incorrect command)" % option)
    elif status_error == "no":
        output_check(output)
        if status != 0:
            raise error.TestFail("Command 'virsh nodeinfo %s' failed "
                                 "(correct command)" % option)
Example #4
0
def run_virsh_numatune(test, params, env):
    """
    Test numa tuning

    1) Positive testing
       1.1) get the current numa parameters for a running/shutoff guest
       1.2) set the current numa parameters for a running/shutoff guest
           1.2.1) set valid 'mode' parameters
           1.2.2) set valid 'nodeset' parameters
    2) Negative testing
       2.1) get numa parameters
           2.1.1) invalid options
           2.1.2) stop cgroup service
       2.2) set numa parameters
           2.2.1) invalid 'mode' parameters
           2.2.2) invalid 'nodeset' parameters
           2.2.3) change 'mode' for a running guest and 'mode' is not 'strict'
           2.2.4) change 'nodeset' for running guest with mode of 'interleave'
                  'interleave' or 'preferred' numa mode
           2.2.5) stop cgroup service
    """

    # Run test case
    vm_name = params.get("vms")
    vm = env.get_vm(vm_name)
    status_error = params.get("status_error", "no")
    libvirtd = params.get("libvirtd", "on")
    cgconfig = params.get("cgconfig", "on")
    start_vm = params.get("start_vm", "no")
    change_parameters = params.get("change_parameters", "no")

    ########## positive and negative testing #########

    if status_error == "no":
        if change_parameters == "no":
            get_numa_parameter(params)
        else:
            set_numa_parameter(params)

    if cgconfig == "off":
        # Need to shutdown a running guest before stopping cgconfig service
        # and will start the guest after restarting libvirtd service
        if vm.is_alive():
            vm.destroy()
        if utils_cgroup.service_cgconfig_control("status"):
            utils_cgroup.service_cgconfig_control("stop")

    # Refresh libvirtd service to get latest cgconfig service change
    if libvirtd == "restart":
        libvirt_vm.service_libvirtd_control("restart")

    # Recover previous running guest
    if cgconfig == "off" and libvirtd == "restart" \
        and not vm.is_alive() and start_vm == "yes":
        vm.start()

    if status_error == "yes":
        if change_parameters == "no":
            get_numa_parameter(params)
        else:
            set_numa_parameter(params)

    # Recover cgconfig and libvirtd service
    if not utils_cgroup.service_cgconfig_control("status"):
        utils_cgroup.service_cgconfig_control("start")
        libvirt_vm.service_libvirtd_control("restart")
Example #5
0
def run_virsh_freecell(test, params, env):
    """
    Test the command virsh freecell

    (1) Call virsh freecell
    (2) Call virsh freecell --all
    (3) Call virsh freecell with a numeric argument
    (4) Call virsh freecell xyz
    (5) Call virsh freecell with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                               "default") )
    option = params.get("virsh_freecell_options")

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    cmd_result = virsh.freecell(ignore_status=True, extra=option,
                                uri=connect_uri, debug=True)
    output = cmd_result.stdout.strip()
    status = cmd_result.exit_status

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check the output
    if virsh.has_help_command('numatune'):
        OLD_LIBVIRT = False
    else:
        OLD_LIBVIRT = True
        if option == '--all':
            raise error.TestNAError("Older libvirt virsh freecell "
                                    "doesn't support --all option")

    def output_check(freecell_output):
        if not re.search("ki?B", freecell_output, re.IGNORECASE):
            raise error.TestFail("virsh freecell output invalid: " + freecell_output)

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail("Command 'virsh freecell' succeeded "
                                     "with libvirtd service stopped, incorrect")
            else:
                # newer libvirt
                if not OLD_LIBVIRT:
                    raise error.TestFail("Command 'virsh freecell %s' succeeded"
                                         "(incorrect command)" % option)
                else: # older libvirt
                    raise error.TestNAError('Older libvirt virsh freecell '
                                            'incorrectly processes extranious'
                                            'command-line options')
    elif status_error == "no":
        output_check(output)
        if status != 0:
            raise error.TestFail("Command 'virsh freecell %s' failed "
                                 "(correct command)" % option)
Example #6
0
def run_virsh_list(test, params, env):
    """
    Test command: virsh list.

    1) Filt parameters according libvirtd's version
    2) Prepare domain's exist state:transient,managed-save.
    3) Prepare libvirt's status.
    4) Execute list command.
    5) Result check.
    """
    def list_local_domains_on_remote(options_ref, remote_ip, remote_passwd, local_ip):
        """
        Create a virsh list command and execute it on remote host.
        It will list local domains on remote host.

        @param options_ref:options in virsh list command.
        @param remote_ip:remote host's ip.
        @param remote_passwd:remote host's password.
        @param local_ip:local ip, to create uri in virsh list.
        @return:return status and output of the virsh list command.
        """
        command_on_remote = "virsh -c qemu+ssh://%s/system list %s" % (local_ip, options_ref)
        session = remote.remote_login("ssh", remote_ip, "22", "root", remote_passwd, "#")
        time.sleep(5)
        status, output = session.cmd_status_output(command_on_remote, internal_timeout=5)
        time.sleep(5)
        session.close()
        return int(status), output

    vm_name = params.get("main_vm", "vm1")
    vm = env.get_vm(params["main_vm"])

    options_ref = params.get("options_ref", "")
    list_ref = params.get("list_ref", "")
    vm_ref = params.get("vm_ref", "")

    #Some parameters are not supported on old libvirt, skip them.
    help_info = virsh.command("help list").stdout.strip()
    if vm_ref and not re.search(vm_ref, help_info):
        raise error.TestNAError("This version do not support vm type:%s"
                                 % vm_ref)
    if list_ref and not re.search(list_ref, help_info):
        raise error.TestNAError("This version do not support list type:%s"
                                 % list_ref)

    status_error = params.get("status_error", "no")
    addition_status_error = params.get("addition_status_error", "no")
    domuuid = vm.get_uuid().strip()
    # If a transient domain is destroyed, it will disappear.
    if vm_ref == "transient" and options_ref == "inactive":
        logging.info("Set addition_status_error to yes")
        logging.info("because transient domain will disappear after destroyed.")
        addition_status_error = "yes"

    if vm_ref == "transient":
        tmp_xml = vm.backup_xml()
        vm.undefine()
    elif vm_ref == "managed-save":
        virsh.managedsave(vm_name, ignore_status=True, print_info=True)

    #Prepare libvirtd status
    libvirtd = params.get("libvirtd", "on")
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    #run test case
    if list_ref == "--uuid":
        result_expected = domuuid
        logging.info("%s's uuid is: %s", vm_name, domuuid)
    else:
        result_expected = vm_name
        logging.info("domain's name is: %s", vm_name)

    if options_ref == "vm_id":
        domid = vm.get_id().strip()
        logging.info("%s's running-id is: %s", vm_name, domid)
        options_ref = "%s %s" % (domid, list_ref)
    elif options_ref == "vm_uuid":
        logging.info("%s's uuid is: %s", vm_name, domuuid)
        options_ref = "%s %s" % (domuuid, list_ref)
    elif options_ref == "inactive":
        vm.destroy()
        options_ref = "--inactive %s" % list_ref
    elif options_ref == "vm_name":
        options_ref = "%s %s" % (vm_name, list_ref)
    elif options_ref == "all":
        options_ref = "--all %s" % list_ref
    elif options_ref == "":
        options_ref = "%s" % list_ref

    remote_ref = params.get("remote_ref", "local")
    if remote_ref == "remote":
        remote_ip = params.get("remote_ip", "none")
        remote_passwd = params.get("remote_passwd", "none")
        local_ip = params.get("local_ip", "none")
        logging.info("Execute virsh command on remote host %s.", remote_ip)
        status, output = list_local_domains_on_remote(options_ref, remote_ip, remote_passwd, local_ip)
        logging.info("Status:%s", status)
        logging.info("Output:\n%s", output)
    else:
        if vm_ref:
            options_ref = "%s --%s" % (options_ref, vm_ref)
        result = virsh.dom_list(options_ref, ignore_status=True, print_info=True)
        status = result.exit_status
        output = result.stdout.strip()

    #Recover libvirtd service status
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    #Recover of domain
    if vm_ref == "transient":
        vm.define(tmp_xml)
    elif vm_ref == "managed-save":
        #Recover saved guest.
        virsh.managedsave_remove(vm_name, ignore_status=True, print_info=True)

    #Check result
    status_error = (status_error == "no") and (addition_status_error == "no")
    if vm_ref == "managed-save":
        saved_output = re.search(vm_name+"\s+saved", output)
        if saved_output:
            output = saved_output.group(0)
        else:
            output = ""

    if not status_error:
        if status == 0 and re.search(result_expected, output):
            raise error.TestFail("Run successful with wrong command!")
    else:
        if status != 0:
            raise error.TestFail("Run failed with right command.")
        if not re.search(result_expected, output):
            raise error.TestFail("Run successful but result is not expected.")
Example #7
0
def run_virsh_capabilities(test, params, env):
    """
    Test the command virsh capabilities

    (1) Call virsh capabilities
    (2) Call virsh capabilities with an unexpected option
    (3) Call virsh capabilities with libvirtd service stop
    """
    def compare_capabilities_xml(source):
        dom = parseString(source)
        host = dom.getElementsByTagName('host')[0]
        # check that host has a non-empty UUID tag.
        uuid = host.getElementsByTagName('uuid')[0]
        host_uuid_output = uuid.firstChild.data
        logging.info("Host uuid (capabilities_xml):%s", host_uuid_output)
        if host_uuid_output == "":
            raise error.TestFail("The host uuid in capabilities_xml is none!")

        # check the host arch.
        arch = host.getElementsByTagName('arch')[0]
        host_arch_output = arch.firstChild.data
        logging.info("Host arch (capabilities_xml):%s", host_arch_output)
        cmd_result = utils.run("arch", ignore_status=True)
        if cmp(host_arch_output, cmd_result.stdout.strip()) != 0:
            raise error.TestFail("The host arch in capabilities_xml is wrong!")

        # check the host cpus num.
        cpus = dom.getElementsByTagName('cpus')[0]
        host_cpus_output = cpus.getAttribute('num')
        logging.info("Host cpus num (capabilities_xml):%s",
                      host_cpus_output)
        cmd = "less /proc/cpuinfo | grep processor | wc -l"
        cmd_result = utils.run(cmd, ignore_status=True)
        if cmp(host_cpus_output, cmd_result.stdout.strip()) != 0:
            raise error.TestFail("Host cpus num (capabilities_xml) is "
                                 "wrong")

        # check the arch of guest supported.
        cmd = "/usr/libexec/qemu-kvm  --cpu ? | grep qemu"
        cmd_result = utils.run(cmd, ignore_status=True)
        guest_wordsize_array = dom.getElementsByTagName('wordsize')
        length = len(guest_wordsize_array)
        for i in range(length):
            element = guest_wordsize_array[i]
            guest_wordsize = element.firstChild.data
            logging.info("Arch of guest supported (capabilities_xml):%s",
                         guest_wordsize)
            if not re.search(guest_wordsize, cmd_result.stdout.strip()):
                raise error.TestFail("The capabilities_xml gives an extra arch "
                                     "of guest to support!")

        # check the type of hyperviosr.
        guest_domain_type = dom.getElementsByTagName('domain')[0]
        guest_domain_type_output = guest_domain_type.getAttribute('type')
        logging.info("Hypervisor (capabilities_xml):%s",
                     guest_domain_type_output)
        cmd_result = utils.run("virsh uri", ignore_status=True)
        if not re.search(guest_domain_type_output, cmd_result.stdout.strip()):
            raise error.TestFail("The capabilities_xml gives an different "
                                 "hypervisor")


    connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                               "default") )

    # Prepare libvirtd service
    if params.has_key("libvirtd"):
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    option = params.get("virsh_cap_options")
    try:
        output = virsh.capabilities(option, uri=connect_uri,
                                    ignore_status=False, debug=True)
        status = 0 # good
    except error.CmdError:
        status = 1 # bad
        output = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail("Command 'virsh capabilities' succeeded "
                                     "with libvirtd service stopped, incorrect")
            else:
                raise error.TestFail("Command 'virsh capabilities %s' succeeded "
                                     "(incorrect command)" % option)
    elif status_error == "no":
        compare_capabilities_xml(output)
        if status != 0:
            raise error.TestFail("Command 'virsh capabilities %s' failed "
                                 "(correct command)" % option)
Example #8
0
def run_virsh_nodememstats(test, params, env):
    """
    Test the command virsh nodememstats

    (1) Call the virsh nodememstats command
    (2) Get the output
    (3) Check the against /proc/meminfo output
    (4) Call the virsh nodememstats command with an unexpected option
    (5) Call the virsh nodememstats command with libvirtd service stop
    """

    # Initialize the variables
    expected = {}
    actual = {}
    deltas = []
    name_stats = ['total', 'free', 'buffers', 'cached']
    itr = int(params.get("itr"))

    def virsh_check_nodememtats(actual_stats, expected_stats, delta):
        """
        Check the nodememstats output value with /proc/meminfo value
        """

        delta_stats = {}
        for name in name_stats:
            delta_stats[name] = abs(actual_stats[name] - expected_stats[name])
            if 'total' in name:
                if not delta_stats[name] == 0:
                    raise error.TestFail("Command 'virsh nodememstats' not"
                                         " succeeded as the value for %s is "
                                         "deviated by %d\nThe total memory "
                                         "value is deviating-check"
                                            % (name, delta_stats[name]))
            else:
                if delta_stats[name] > delta:
                    raise error.TestFail("Command 'virsh nodememstats' not "
                                         "succeeded as the value for %s"
                                          " is deviated by %d"
                                            % (name, delta_stats[name]))
        return delta_stats


    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Get the option for the test case
    option = params.get("virsh_nodememstats_options")

    # Run test case for 10 iterations
    # (default can be changed in subtests.cfg file)
    # and print the final statistics
    for i in range(itr):
        output = virsh.nodememstats(option)

        # Get the status of the virsh command executed
        status = output.exit_status

        # Get status_error option for the test case
        status_error = params.get("status_error")
        if status_error == "yes":
            if status == 0:
                if libvirtd == "off":
                    libvirt_vm.service_libvirtd_control("start")
                    raise error.TestFail("Command 'virsh nodememstats' "
                                         "succeeded with libvirtd service"
                                         " stopped, incorrect")
                else:
                    raise error.TestFail("Command 'virsh nodememstats %s' "
                                         "succeeded (incorrect command)"
                                         % option)

        elif status_error == "no":
            if status == 0:
                # From the beginning of a line, group 1 is one or
                # more word-characters, followed by zero or more
                # whitespace characters and a ':', then one or
                # more whitespace characters, followed by group 2,
                # which is one or more digit characters,
                # then one or more whitespace characters followed by
                # a literal 'kB' or 'KiB' sequence, e.g as below
                # total  :              3809340 kB
                # total  :              3809340 KiB
                # Normalise the value to MBs
                regex_obj = re.compile(r"^(\w+)\s*:\s+(\d+)\s\w+")
                expected = {}

                for line in output.stdout.split('\n'):
                    match_obj = regex_obj.search(line)
                    # Due to the extra space in the list
                    if match_obj is not None:
                        name = match_obj.group(1)
                        value = match_obj.group(2)
                        expected[name] = int(value) / 1024

                # Get the actual value from /proc/meminfo and normalise to MBs
                actual['total'] = int(utils.memtotal()) / 1024
                actual['free'] = int(utils.freememtotal()) / 1024
                actual['buffers'] = int(utils.read_from_meminfo('Buffers'))/1024
                actual['cached'] = int(utils.read_from_meminfo('Cached')) / 1024

                # Currently the delta value is kept at 200 MB this can be
                # tuned based on the accuracy
                # Check subtests.cfg for more details
                delta = int(params.get("delta"))
                output = virsh_check_nodememtats(actual, expected, delta)
                deltas.append(output)

            else:
                raise error.TestFail("Command virsh nodememstats %s not "
                                     "succeeded:\n%s" % (option, status))

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Print the deviated values for all iterations
    if status_error == "no":
        logging.debug("The following is the deviations from "
                      "the actual(/proc/meminfo) and expected"
                      " value(output of virsh nodememstats)")

        for i in range(itr):
            logging.debug("iteration %d:", i)
            for index, name in enumerate(name_stats):
                logging.debug("%19s : %d", name, deltas[i][name])
Example #9
0
def run_virsh_dump(test, params, env):
    """
    Test command: virsh dump.

    This command can dump the core of a domain to a file for analysis.
    1. Positive testing
        1.1 Dump domain with valid options.
        1.2 Avoid file system cache when dumping.
        1.3 Compress the dump images to valid/invalid formats.
    2. Negative testing
        2.1 Dump domain to a non-exist directory.
        2.2 Dump domain with invalid option.
        2.3 Dump a shut-off domain.
    """

    vm_name = params.get("main_vm", "vm1")
    vm = env.get_vm(params["main_vm"])
    options = params.get("dump_options")
    dump_file = params.get("dump_file")
    dump_image_format = params.get("dump_image_format")
    start_vm = params.get("start_vm")
    status_error = params.get("status_error", "no")
    qemu_conf = "/etc/libvirt/qemu.conf"

    # prepare the vm state
    if vm.is_alive() and start_vm == "no":
        vm.destroy()

    if vm.is_dead() and start_vm == "yes":
        vm.start()

    def check_domstate(actual, options):
        """
        Check the domain status according to dump options.
        """

        if options.find('live') >= 0:
            domstate = "running"
            if options.find('crash') >= 0 or options.find('reset') >0:
                domstate = "running"
        elif options.find('crash') >=0:
            domstate = "shut off"
            if options.find('reset') >= 0:
                domstate = "running"
        elif options.find('reset') >= 0:
            domstate = "running"
        else:
            domstate = "running"

        if start_vm == "no":
            domstate = "shut off"

        logging.debug("Domain should %s after run dump %s", domstate, options)

        if domstate == actual:
            return True
        else:
            return False

    def check_dump_format(dump_image_format, dump_file):
        """
        Check the format of dumped file.

        If 'dump_image_format' is not specified or invalid in qemu.conf, then
        the file shoule be normal raw file, otherwise it shoud be compress to
        specified format, the supported compress format including: lzop, gzip,
        bzip2, and xz.
        """

        valid_format = ["lzop", "gzip", "bzip2", "xz"]
        if len(dump_image_format) == 0 or dump_image_format not in valid_format:
            logging.debug("No need check the dumped file format")
            return True
        else:
            file_cmd = "file %s" % dump_file
            (status, output) = commands.getstatusoutput(file_cmd)
            if status == 0:
                logging.debug("Run file %s output: %s", dump_file, output)
                actual_format = output.split(" ")[1]
                if actual_format == dump_image_format:
                    if dump_image_format in valid_format:
                        logging.info("Compress dumped file to %s successfully",
                                     dump_image_format)
                    return True
                else:
                    logging.error("Compress dumped file to %s fail",
                                  dump_image_format)
                    return False
            else:
                logging.error("Fail to check dumped file %s", dump_file)
                return False


    # Configure dump_image_format in /etc/libvirt/qemu.conf.
    if len(dump_image_format) != 0:
        conf_cmd = ("echo dump_image_format = \\\"%s\\\" >> %s" %
                    (dump_image_format, qemu_conf))
        if os.system(conf_cmd):
            logging.error("Config dump_image_format to %s fail",
                          dump_image_format)
        libvirt_vm.service_libvirtd_control("restart")

    # Deal with bypass-cache option
    if options.find('bypass-cache') >= 0:
        thread.start_new_thread(check_bypass,(dump_file,))
        # Guarantee check_bypass function has run before dump
        time.sleep(5)

    # Run virsh command
    cmd_result = virsh.dump(vm_name, dump_file, options,
                            ignore_status=True, debug=True)
    status = cmd_result.exit_status

    # Check libvirtd status
    if libvirt_vm.service_libvirtd_control("status"):
        if check_domstate(vm.state(), options):
            if status_error == "yes":
                if status == 0:
                    raise error.TestFail("Expect fail, but run successfully")
            if status_error == "no":
                if status != 0:
                    raise error.TestFail("Expect succeed, but run fail")
                else:
                    if os.path.exists(dump_file):
                        if check_dump_format(dump_image_format, dump_file):
                            logging.info("Successfully dump domain to %s",
                                         dump_file)
                        else:
                            raise error.TestFail("The format of dumped file "
                                                 "is wrong.")
                    else:
                        raise error.TestFail("Fail to find domain dumped file.")

        else:
            raise error.TestFail("Domain status check fail.")
    else:
        raise error.TestFail("Libvirtd service is dead.")

    if os.path.exists(dump_file):
        os.remove(dump_file)

    if len(dump_image_format) != 0:
        clean_qemu_conf = "sed -i '$d' %s " % qemu_conf
        if os.system(clean_qemu_conf):
            raise error.TestFail("Fail to recover %s", qemu_conf)
Example #10
0
def run_virsh_numatune(test, params, env):
    """
    Test numa tuning

    1) Positive testing
       1.1) get the current numa parameters for a running/shutoff guest
       1.2) set the current numa parameters for a running/shutoff guest
           1.2.1) set valid 'mode' parameters
           1.2.2) set valid 'nodeset' parameters
    2) Negative testing
       2.1) get numa parameters
           2.1.1) invalid options
           2.1.2) stop cgroup service
       2.2) set numa parameters
           2.2.1) invalid 'mode' parameters
           2.2.2) invalid 'nodeset' parameters
           2.2.3) change 'mode' for a running guest and 'mode' is not 'strict'
           2.2.4) change 'nodeset' for running guest with mode of 'interleave'
                  'interleave' or 'preferred' numa mode
           2.2.5) stop cgroup service
    """

    # Run test case
    vm_name = params.get("vms")
    vm = env.get_vm(vm_name)
    original_vm_xml = libvirt_xml.VMXML.new_from_dumpxml(vm_name)
    status_error = params.get("status_error", "no")
    libvirtd = params.get("libvirtd", "on")
    cgconfig = params.get("cgconfig", "on")
    start_vm = params.get("start_vm", "no")
    change_parameters = params.get("change_parameters", "no")

    # Make sure vm is down if start not requested
    if start_vm == "no" and vm.is_alive():
        vm.destroy()

    ########## positive and negative testing #########

    try:
        if status_error == "no":
            if change_parameters == "no":
                get_numa_parameter(params)
            else:
                set_numa_parameter(params)
        if cgconfig == "off":
            # Need to shutdown a running guest before stopping cgconfig service
            # and will start the guest after restarting libvirtd service
            if vm.is_alive():
                vm.destroy()
            if utils_cgroup.service_cgconfig_control("status"):
                utils_cgroup.service_cgconfig_control("stop")
        # Refresh libvirtd service to get latest cgconfig service change
        if libvirtd == "restart":
            libvirt_vm.service_libvirtd_control("restart")
        # Recover previous running guest
        if cgconfig == "off" and libvirtd == "restart" \
            and not vm.is_alive() and start_vm == "yes":
            vm.start()
        if status_error == "yes":
            if change_parameters == "no":
                get_numa_parameter(params)
            else:
                set_numa_parameter(params)
        # Recover cgconfig and libvirtd service
        if not utils_cgroup.service_cgconfig_control("status"):
            utils_cgroup.service_cgconfig_control("start")
            libvirt_vm.service_libvirtd_control("restart")
    finally:
        vm.destroy()
        # Restore guest, first remove existing
        original_vm_xml.undefine()
        # Recover based on original XML
        original_vm_xml.define()
Example #11
0
def run_virsh_uri(test, params, env):
    """
    Test the command virsh uri

    (1) Call virsh uri
    (2) Call virsh -c remote_uri uri
    (3) Call virsh uri with an unexpected option
    (4) Call virsh uri with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri(
        params.get("connect_uri", "default"))

    option = params.get("options")
    target_uri = params.get("target_uri")
    if target_uri:
        if target_uri.count('EXAMPLE.COM'):
            raise error.TestError(
                'target_uri configuration set to sample value')
        logging.info("The target_uri: %s", target_uri)
        cmd = "virsh -c %s uri" % target_uri
    else:
        cmd = "virsh uri %s" % option

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    logging.info("The command: %s", cmd)
    try:
        uri_test = virsh.canonical_uri(option,
                                       uri=connect_uri,
                                       ignore_status=False,
                                       debug=True)
        status = 0  # good
    except error.CmdError:
        status = 1  # bad
        uri_test = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            raise error.TestFail("Command: %s  succeeded "
                                 "(incorrect command)" % cmd)
        else:
            logging.info("command: %s is a expected error", cmd)
    elif status_error == "no":
        if cmp(target_uri, uri_test) != 0:
            raise error.TestFail("Virsh cmd uri %s != %s." %
                                 (uri_test, target_uri))
        if status != 0:
            raise error.TestFail("Command: %s  failed "
                                 "(correct command)" % cmd)
Example #12
0
def run_virsh_nodecpustats(test, params, env):
    """
    Test the command virsh nodecpustats

    (1) Call the virsh nodecpustats command for all cpu host cpus
        separately
    (2) Get the output
    (3) Check the against /proc/stat output(o) for respective cpu
        user: o[0] + o[1]
        system: o[2] + o[5] + o[6]
        idle: o[3]
        iowait: o[4]
    (4) Call the virsh nodecpustats command with an unexpected option
    (5) Call the virsh nodecpustats command with libvirtd service stop
    """
    def virsh_check_nodecpustats_percpu(actual_stats):
        """
        Check the acual nodecpustats output value
        total time <= system uptime
        """

        # Normalise to seconds from nano seconds
        total = float((actual_stats['system'] + actual_stats['user'] + \
                    actual_stats['idle'] + actual_stats['iowait'])/(10 ** 9))
        uptime = float(utils.get_uptime())
        if not total <= uptime:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                 " as total time: %f is more"
                                 " than uptime: %f" % (total, uptime))
        return True

    def virsh_check_nodecpustats(actual_stats, cpu_count):
        """
        Check the acual nodecpustats output value
        total time <= system uptime
        """

        # Normalise to seconds from nano seconds and get for one cpu
        total = float(((actual_stats['system'] + actual_stats['user'] + \
                actual_stats['idle'] + actual_stats['iowait'])/(10 ** 9))/(
                cpu_count))
        uptime = float(utils.get_uptime())
        if not total <= uptime:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                 " as total time: %f is more"
                                 " than uptime: %f" % (total, uptime))
        return True

    def virsh_check_nodecpustats_percentage(actual_per):
        """
        Check the actual nodecpustats percentage adds upto 100%
        """

        total = int(round(actual_per['user'] + actual_per['system'] + \
                    actual_per['idle'] + actual_per['iowait']))

        if not total == 100:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                 " as the total percentage value: %d"
                                 " is not equal 100" % total)

    def parse_output(output):
        """
        To get the output parsed into a dictionary
        @param: virsh command output

        @return: dict of user,system,idle,iowait times
        """

        # From the beginning of a line, group 1 is one or more word-characters,
        # followed by zero or more whitespace characters and a ':',
        # then one or more whitespace characters,
        # followed by group 2, which is one or more digit characters,
        # e.g as below
        # user:                  6163690000000
        #
        regex_obj = re.compile(r"^(\w+)\s*:\s+(\d+)")
        actual = {}

        for line in output.stdout.split('\n'):
            match_obj = regex_obj.search(line)
            # Due to the extra space in the list
            if match_obj is not None:
                name = match_obj.group(1)
                value = match_obj.group(2)
                actual[name] = int(value)
        return actual

    def parse_percentage_output(output):
        """
        To get the output parsed into a dictionary
        @param: virsh command output

        @return: dict of user,system,idle,iowait times
        """

        # From the beginning of a line, group 1 is one or more word-characters,
        # followed by zero or more whitespace characters and a ':',
        # then one or more whitespace characters,
        # followed by group 2, which is one or more digit characters,
        # e.g as below
        # user:             1.5%
        #
        regex_obj = re.compile(r"^(\w+)\s*:\s+(\d+.\d+)")
        actual_percentage = {}

        for line in output.stdout.split('\n'):
            match_obj = regex_obj.search(line)
            # Due to the extra space in the list
            if match_obj is not None:
                name = match_obj.group(1)
                value = match_obj.group(2)
                actual_percentage[name] = float(value)
        return actual_percentage

    # Initialize the variables
    itr = int(params.get("inner_test_iterations"))
    option = params.get("virsh_cpunodestats_options")
    status_error = params.get("status_error")
    libvirtd = params.get("libvirtd", "on")

    # Prepare libvirtd service
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    # Get the host cpu count
    host_cpu_count = utils.count_cpus()

    # Run test case for 5 iterations default can be changed in subtests.cfg file
    for i in range(itr):

        if status_error == "yes":
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                if libvirtd == "off":
                    libvirt_vm.service_libvirtd_control("start")
                    raise error.TestFail("Command 'virsh nodecpustats' "
                                         "succeeded with libvirtd service "
                                         "stopped, incorrect")
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s' "
                                         "succeeded (incorrect command)" %
                                         option)

        elif status_error == "no":
            # Run the testcase for each cpu to get the cpu stats
            for cpu in range(host_cpu_count):
                option = "--cpu %d" % cpu
                output = virsh.nodecpustats(ignore_status=True, option=option)
                status = output.exit_status

                if status == 0:
                    actual_value = parse_output(output)
                    virsh_check_nodecpustats_percpu(actual_value)
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s'"
                                         "not succeeded" % option)

            # Run the test case for each cpu to get the cpu stats in percentage
            for cpu in range(host_cpu_count):
                option = "--cpu %d --percent" % cpu
                output = virsh.nodecpustats(ignore_status=True, option=option)
                status = output.exit_status

                if status == 0:
                    actual_value = parse_percentage_output(output)
                    virsh_check_nodecpustats_percentage(actual_value)
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s'"
                                         " not succeeded" % option)

            option = ''
            # Run the test case for total cpus to get the cpus stats
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                actual_value = parse_output(output)
                virsh_check_nodecpustats(actual_value, host_cpu_count)
            else:
                raise error.TestFail("Command 'virsh nodecpustats %s'"
                                     " not succeeded" % option)

            # Run the test case for the total cpus to get the stats in percentage
            option = "--percent"
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                actual_value = parse_percentage_output(output)
                virsh_check_nodecpustats_percentage(actual_value)
            else:
                raise error.TestFail("Command 'virsh nodecpustats %s'"
                                     " not succeeded" % option)

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")
Example #13
0
def run_virsh_freecell(test, params, env):
    """
    Test the command virsh freecell

    (1) Call virsh freecell
    (2) Call virsh freecell --all
    (3) Call virsh freecell with a numeric argument
    (4) Call virsh freecell xyz
    (5) Call virsh freecell with libvirtd service stop
    """

    connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                               "default") )
    option = params.get("virsh_freecell_options")

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    cmd_result = virsh.freecell(ignore_status=True, extra=option,
                                uri=connect_uri, debug=True)
    output = cmd_result.stdout.strip()
    status = cmd_result.exit_status

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check the output
    if virsh.has_help_command('numatune'):
        OLD_LIBVIRT = False
    else:
        OLD_LIBVIRT = True
        if option == '--all':
            raise error.TestNAError("Older libvirt virsh freecell "
                                    "doesn't support --all option")

    def output_check(freecell_output):
        if not re.search("ki?B", freecell_output, re.IGNORECASE):
            raise error.TestFail("virsh freecell output invalid: " + freecell_output)

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail("Command 'virsh freecell' succeeded "
                                     "with libvirtd service stopped, incorrect")
            else:
                # newer libvirt
                if not OLD_LIBVIRT:
                    raise error.TestFail("Command 'virsh freecell %s' succeeded"
                                         "(incorrect command)" % option)
                else: # older libvirt
                    raise error.TestNAError('Older libvirt virsh freecell '
                                            'incorrectly processes extranious'
                                            'command-line options')
    elif status_error == "no":
        output_check(output)
        if status != 0:
            raise error.TestFail("Command 'virsh freecell %s' failed "
                                 "(correct command)" % option)
Example #14
0
def run_virsh_setmem(test, params, env):
    """
    Test command: virsh setmem.

    1) Prepare vm environment.
    2) Handle params
    3) Prepare libvirtd status.
    4) Run test command and wait for current memory's stable.
    5) Recover environment.
    4) Check result.
    TODO: support new libvirt with more options.
    """

    def vm_proc_meminfo(session):
        proc_meminfo = session.cmd_output("cat /proc/meminfo")
        # verify format and units are expected
        return int(re.search(r'MemTotal:\s+(\d+)\s+kB', proc_meminfo).group(1))


    def make_domref(domarg, vm_ref, domid, vm_name, domuuid):
        # Specify domain as argument or parameter
        if domarg == "yes":
            dom_darg_key = "domainarg"
        else:
            dom_darg_key = "domain"

        # How to reference domain
        if vm_ref == "domid":
            dom_darg_value = domid
        elif vm_ref == "domname":
            dom_darg_value = vm_name
        elif vm_ref == "domuuid":
            dom_darg_value = domuuid
        elif vm_ref == "none":
            dom_darg_value = None
        elif vm_ref == "emptystring":
            dom_darg_value = '""'
        else: # stick in value directly
            dom_darg_value = vm_ref

        return {dom_darg_key:dom_darg_value}


    def make_sizeref(sizearg, mem_ref, original_mem):
        if sizearg == "yes":
            size_darg_key = "sizearg"
        else:
            size_darg_key = "size"

        if mem_ref == "halfless":
            size_darg_value = "%d" % (original_mem / 2)
        elif mem_ref == "halfmore":
            size_darg_value = "%d" % int(original_mem * 1.5) # no fraction
        elif mem_ref == "same":
            size_darg_value = "%d" % original_mem
        elif mem_ref == "emptystring":
            size_darg_value = '""'
        elif mem_ref == "zero":
            size_darg_value = "0"
        elif mem_ref == "toosmall":
            size_darg_value = "1024"
        elif mem_ref == "toobig":
            size_darg_value = "1099511627776" # (KiB) One Petabyte
        elif mem_ref == "none":
            size_darg_value = None
        else: # stick in value directly
            size_darg_value = mem_ref

        return {size_darg_key:size_darg_value}


    def is_in_range(actual, expected, error_percent):
        deviation = 100 - (100 * (float(actual) / float(expected)))
        logging.debug("Deviation: %0.2f%%" % float(deviation))
        return float(deviation) <= float(error_percent)


    def is_old_libvirt():
        regex = r'\s+\[--size\]\s+'
        return bool( not virsh.has_command_help_match('setmem', regex) )


    def print_debug_stats(original_inside_mem, original_outside_mem,
                          test_inside_mem, test_outside_mem,
                          expected_mem, delta_percentage):
        dbgmsg = ("Original inside mem  : %d KiB\n"
                  "Expected inside mem  : %d KiB\n"
                  "Actual inside mem    : %d KiB\n"
                  "Inside mem deviation : %0.2f%%\n"
                  "Original outside mem : %d KiB\n"
                  "Expected outside mem : %d KiB\n"
                  "Actual outside mem   : %d KiB\n"
                  "Outside mem deviation: %0.2f%%\n"
                  "Acceptable deviation %0.2f%%" % (
                  original_inside_mem,
                  expected_mem,
                  test_inside_mem,
                  100 - (100 * (float(test_inside_mem) / float(expected_mem))),
                  original_outside_mem,
                  expected_mem,
                  test_outside_mem,
                  100 - (100 * (float(test_outside_mem) / float(expected_mem))),
                  float(delta_percentage) ))
        for dbgline in dbgmsg.splitlines():
            logging.debug(dbgline)


    ### MAIN TEST CODE ###


    # Process cartesian parameters
    vm_ref = params.get("setmem_vm_ref", "")
    mem_ref = params.get("setmem_mem_ref", "")
    flags = params.get("setmem_flags", "")
    status_error = params.get("status_error", "no")
    old_libvirt_fail = params.get("setmem_old_libvirt_fail", "no")
    quiesce_delay = int(params.get("setmem_quiesce_delay", "1"))
    domarg = params.get("setmem_domarg", "no")
    sizearg = params.get("setmem_sizearg", "no")
    libvirt = params.get("libvirt", "on")
    delta_percentage = float(params.get("setmem_delta_per", "10"))
    start_vm = params.get("start_vm", "yes")
    vm_name = params.get("main_vm")
    paused_after_start_vm = "yes" == params.get("paused_after_start_vm", "no")

    # Gather environment parameters
    vm = env.get_vm(params["main_vm"])
    if start_vm == "yes":
        if paused_after_start_vm:
            vm.resume()
        session = vm.wait_for_login()
        original_inside_mem = vm_proc_meminfo(session)
        session.close()
        if paused_after_start_vm:
            vm.pause()
    else:
        session = None
        # Retrieve known mem value, convert into kilobytes
        original_inside_mem = int(params.get("mem", "1024")) * 1024
    original_outside_mem = vm.get_used_mem()
    domid = vm.get_id()
    domuuid = vm.get_uuid()
    uri = vm.connect_uri

    old_libvirt = is_old_libvirt()
    if old_libvirt:
        logging.info("Running test on older libvirt")
        use_kilobytes = True
    else:
        logging.info("Running test on newer libvirt")
        use_kilobytes = False

    # Argument pattern is complex, build with dargs
    dargs = {'flagstr':flags,
             'use_kilobytes':use_kilobytes,
             'uri':uri, 'ignore_status':True, "debug":True}
    dargs.update( make_domref(domarg, vm_ref, domid, vm_name, domuuid) )
    dargs.update( make_sizeref(sizearg, mem_ref, original_outside_mem) )

    # Prepare libvirtd status
    if libvirt == "off":
        libvirt_vm.service_libvirtd_control("stop")
    else: # make sure it's running
        libvirt_vm.service_libvirtd_control("restart")

    if status_error == "yes" or old_libvirt_fail == "yes":
        logging.info("Error Test: Expecting an error to occur!")

    result = virsh.setmem(**dargs)
    status = result.exit_status

    # Recover libvirtd status
    if libvirt == "off":
        libvirt_vm.service_libvirtd_control("start")

    if status is 0:
        logging.info("Waiting %d seconds for VM memory to settle", quiesce_delay)
        # It takes time for kernel to settle on new memory
        # and current clean pages is not predictable. Therefor,
        # extremely difficult to determine quiescence, so
        # sleep one second per error percent is reasonable option.
        time.sleep(quiesce_delay)

    # Gather stats if not running error test
    if status_error == "no" and old_libvirt_fail == "no":
        if vm.state() == "shut off":
            vm.start()
        # Make sure it's never paused
        vm.resume()
        session = vm.wait_for_login()

        # Actual results
        test_inside_mem = vm_proc_meminfo(session)
        session.close()
        test_outside_mem = vm.get_used_mem()

        # Expected results for both inside and outside
        if sizearg == "yes":
            expected_mem = int(dargs["sizearg"])
        else:
            expected_mem = int(dargs["size"])

        print_debug_stats(original_inside_mem, original_outside_mem,
                          test_inside_mem, test_outside_mem,
                          expected_mem, delta_percentage)


    if status is 0: # Restore original memory
        restore_status = virsh.setmem(domainarg=vm_name,
                                      sizearg=original_inside_mem,
                                      ignore_status=True).exit_status
        if restore_status is not 0:
            logging.warning("Failed to restore VM's original memory to %s KiB"
                            % original_inside_mem)
    else:
        # virsh setmem failed, no need to restore
        pass


    # Don't care about memory comparison on error test
    if status_error == "no" and old_libvirt_fail == "no":
        outside_in_range = is_in_range(test_outside_mem, expected_mem,
                                       delta_percentage)
        inside_in_range = is_in_range(test_inside_mem, expected_mem,
                                      delta_percentage)
        if status is not 0 or not outside_in_range or not inside_in_range:
            msg = "test conditions not met: "
            if status is not 0:
                msg += "Non-zero virsh setmem exit code. " # maybe multiple
            if not outside_in_range:                       # errors
                msg += "Outside memory deviated. "
            if not inside_in_range:
                msg += "Inside memory deviated. "
            raise error.TestFail(msg)

        return # Normal test passed

    else: # Verify an error test resulted in error
        if status is 0:
            raise error.TestFail("Error test did not result in an error")
        else: # status != 0
            if not old_libvirt: # new libvirt should not have returnd error
                raise error.TestFail("Newer libvirt failed when it should not")
            else:
                # Test passes for old_libvirt == True
                pass
Example #15
0
def run_virsh_net_create(test, params, env):
    """
    Test command: virsh net-create.

    1) Gather test parameters
    2) Store current libvirt host network state
    3) Call virsh net create on possibly modified network XML
    4) Recover original libvirtd and network.
    5) Check result.
    """

    # Gather test parameters
    uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri",
                                                       "default"))
    status_error = "yes" == params.get("status_error", "no")
    # libvirtd also controls use of Virsh or VirshPersistent
    libvirtd = "on" == params.get("libvirtd", "on")
    net_name = params.get("net_create_net_name", "") # default is tested
    net_uuid = params.get("net_create_net_uuid", "") # default is tested
    options_ref = params.get("net_create_options_ref", "") # default is tested
    extra = params.get("net_create_options_extra", "") # extra cmd-line params.
    corrupt = "yes" == params.get("net_create_corrupt_xml", "no")
    remove_existing = "yes" == params.get("net_create_remove_existing", "yes")
    # Dictionary or None value
    bridge = eval(params.get("net_create_bridge", "None"),
                  {'__builtins__':None}, {})
    # make easy to maintain
    virsh_dargs = {'uri':uri, 'debug':False, 'ignore_status':False}
    vrsh = virsh.VirshPersistent(**virsh_dargs)

    # Prepare environment and record current net_state_dict
    backup = libvirt_xml.NetworkXML.new_all_networks_dict(vrsh)
    backup_state = vrsh.net_state_dict()
    logging.debug("Backed up network(s): %s", backup_state)

    # Make some XML to use for testing, for now we just copy 'default'
    test_xml = xml_utils.TempXMLFile() # temporary file
    try:
        # LibvirtXMLBase.__str__ returns XML content
        test_xml.write(str(backup['default']))
        test_xml.flush()
    except (KeyError, AttributeError):
        raise error.TestNAError("Test requires default network to exist")
    if corrupt:
        # find file size
        test_xml.seek(0, 2) # end
        # write garbage at middle of file
        test_xml.seek(test_xml.tell() / 2)
        test_xml.write('"<network><<<BAD>>><\'XML</network\>'
                       '!@#$%^&*)>(}>}{CORRUPTE|>!')
        test_xml.flush()
        # Assume next user might want to read
        test_xml.seek(0)

    if remove_existing:
        for netxml in backup.values():
            netxml.orbital_nuclear_strike()

    #Run test case

    # Prepare libvirtd status
    if not libvirtd: # TODO: Support stop on remote system
        vrsh.close_session() # all virsh commands will probably fail
        libvirt_vm.service_libvirtd_control("stop")

    # Be nice to user
    if status_error:
        logging.info("The following is expected to fail...")

    try:
        # Determine depth of test - if low-level calls are needed
        if (options_ref or extra or corrupt):
            logging.debug("Performing low-level net-create test")
            # vrsh will act like it's own virsh-dargs, i.e. it is dict-like
            test_passed = do_low_level_test(vrsh, test_xml, options_ref, extra)
        else: # high-level test
            logging.debug("Performing high-level net-create test")
            # vrsh will act like it's own virsh-dargs, i.e. it is dict-like
            test_passed = do_high_level_test(vrsh, test_xml, net_name,
                          net_uuid, bridge)
    finally:
        # Be nice to user
        if status_error:
            # In case test itself has errors, warn they are real.
            logging.info("The following is NOT expected to fail...")

        # Recover libvirtd service start
        if not libvirtd: # TODO: Support start on remote system
            libvirt_vm.service_libvirtd_control("start")
            vrsh.new_session() # Virsh instance shared with backup netxml values

        # Done with file, cleanup
        del test_xml

        # Recover environment
        leftovers = libvirt_xml.NetworkXML.new_all_networks_dict(vrsh)
        for netxml in leftovers.values():
            netxml.orbital_nuclear_strike()

        # Recover from backup
        for netxml in backup.values():
            netxml.create()
            # autostart = True requires persistent = True first!
            for state in ['active', 'persistent', 'autostart']:
                netxml[state] = backup_state[netxml.name][state]

        # Close down persistent virsh session (including for all netxml copies)
        if hasattr(vrsh, 'close_session'):
            vrsh.close_session()

    # Check Result
    if status_error: # An error was expected
        if test_passed: # Error was not produced
            raise error.TestFail("Error test did not fail!")
    else: # no error expected
        if not test_passed:
            raise error.TestFail("Normal test returned failure")
Example #16
0
def run_virsh_setmem(test, params, env):
    """
    Test command: virsh setmem.

    1) Prepare vm environment.
    2) Handle params
    3) Prepare libvirtd status.
    4) Run test command and wait for current memory's stable.
    5) Recover environment.
    4) Check result.
    TODO: support new libvirt with more options.
    """
    def vm_proc_meminfo(session):
        proc_meminfo = session.cmd_output("cat /proc/meminfo")
        # verify format and units are expected
        return int(re.search(r'MemTotal:\s+(\d+)\s+kB', proc_meminfo).group(1))

    def make_domref(domarg, vm_ref, domid, vm_name, domuuid):
        # Specify domain as argument or parameter
        if domarg == "yes":
            dom_darg_key = "domainarg"
        else:
            dom_darg_key = "domain"

        # How to reference domain
        if vm_ref == "domid":
            dom_darg_value = domid
        elif vm_ref == "domname":
            dom_darg_value = vm_name
        elif vm_ref == "domuuid":
            dom_darg_value = domuuid
        elif vm_ref == "none":
            dom_darg_value = None
        elif vm_ref == "emptystring":
            dom_darg_value = '""'
        else:  # stick in value directly
            dom_darg_value = vm_ref

        return {dom_darg_key: dom_darg_value}

    def make_sizeref(sizearg, mem_ref, original_mem):
        if sizearg == "yes":
            size_darg_key = "sizearg"
        else:
            size_darg_key = "size"

        if mem_ref == "halfless":
            size_darg_value = "%d" % (original_mem / 2)
        elif mem_ref == "halfmore":
            size_darg_value = "%d" % int(original_mem * 1.5)  # no fraction
        elif mem_ref == "same":
            size_darg_value = "%d" % original_mem
        elif mem_ref == "emptystring":
            size_darg_value = '""'
        elif mem_ref == "zero":
            size_darg_value = "0"
        elif mem_ref == "toosmall":
            size_darg_value = "1024"
        elif mem_ref == "toobig":
            size_darg_value = "1099511627776"  # (KiB) One Petabyte
        elif mem_ref == "none":
            size_darg_value = None
        else:  # stick in value directly
            size_darg_value = mem_ref

        return {size_darg_key: size_darg_value}

    def is_in_range(actual, expected, error_percent):
        deviation = 100 - (100 * (float(actual) / float(expected)))
        logging.debug("Deviation: %0.2f%%" % float(deviation))
        return float(deviation) <= float(error_percent)

    def is_old_libvirt():
        regex = r'\s+\[--size\]\s+'
        return bool(not virsh.has_command_help_match('setmem', regex))

    def print_debug_stats(original_inside_mem, original_outside_mem,
                          test_inside_mem, test_outside_mem, expected_mem,
                          delta_percentage):
        dbgmsg = ("Original inside mem  : %d KiB\n"
                  "Expected inside mem  : %d KiB\n"
                  "Actual inside mem    : %d KiB\n"
                  "Inside mem deviation : %0.2f%%\n"
                  "Original outside mem : %d KiB\n"
                  "Expected outside mem : %d KiB\n"
                  "Actual outside mem   : %d KiB\n"
                  "Outside mem deviation: %0.2f%%\n"
                  "Acceptable deviation %0.2f%%" %
                  (original_inside_mem, expected_mem, test_inside_mem, 100 -
                   (100 * (float(test_inside_mem) / float(expected_mem))),
                   original_outside_mem, expected_mem, test_outside_mem, 100 -
                   (100 * (float(test_outside_mem) / float(expected_mem))),
                   float(delta_percentage)))
        for dbgline in dbgmsg.splitlines():
            logging.debug(dbgline)

    ### MAIN TEST CODE ###

    # Process cartesian parameters
    vm_ref = params.get("setmem_vm_ref", "")
    mem_ref = params.get("setmem_mem_ref", "")
    flags = params.get("setmem_flags", "")
    status_error = params.get("status_error", "no")
    old_libvirt_fail = params.get("setmem_old_libvirt_fail", "no")
    quiesce_delay = int(params.get("setmem_quiesce_delay", "1"))
    domarg = params.get("setmem_domarg", "no")
    sizearg = params.get("setmem_sizearg", "no")
    libvirt = params.get("libvirt", "on")
    delta_percentage = float(params.get("setmem_delta_per", "10"))
    start_vm = params.get("start_vm", "yes")
    vm_name = params.get("main_vm")

    # Gather environment parameters
    vm = env.get_vm(params["main_vm"])
    if start_vm == "yes":
        session = vm.wait_for_login()
        original_inside_mem = vm_proc_meminfo(session)
        session.close()
    else:
        session = None
        # Retrieve known mem value, convert into kilobytes
        original_inside_mem = int(params.get("mem", "1024")) * 1024
    original_outside_mem = vm.get_used_mem()
    domid = vm.get_id()
    domuuid = vm.get_uuid()
    uri = vm.connect_uri

    old_libvirt = is_old_libvirt()
    if old_libvirt:
        logging.info("Running test on older libvirt")
        use_kilobytes = True
    else:
        logging.info("Running test on newer libvirt")
        use_kilobytes = False

    # Argument pattern is complex, build with dargs
    dargs = {
        'flagstr': flags,
        'use_kilobytes': use_kilobytes,
        'uri': uri,
        'ignore_status': True,
        "debug": True
    }
    dargs.update(make_domref(domarg, vm_ref, domid, vm_name, domuuid))
    dargs.update(make_sizeref(sizearg, mem_ref, original_outside_mem))

    # Prepare libvirtd status
    if libvirt == "off":
        libvirt_vm.service_libvirtd_control("stop")
    else:  # make sure it's running
        libvirt_vm.service_libvirtd_control("restart")

    if status_error == "yes" or old_libvirt_fail == "yes":
        logging.info("Error Test: Expecting an error to occur!")

    result = virsh.setmem(**dargs)
    status = result.exit_status

    # Recover libvirtd status
    if libvirt == "off":
        libvirt_vm.service_libvirtd_control("start")

    if status is 0:
        logging.info("Waiting %d seconds for VM memory to settle",
                     quiesce_delay)
        # It takes time for kernel to settle on new memory
        # and current clean pages is not predictable. Therefor,
        # extremely difficult to determine quiescence, so
        # sleep one second per error percent is reasonable option.
        time.sleep(quiesce_delay)

    # Gather stats if not running error test
    if status_error == "no" and old_libvirt_fail == "no":
        if vm.state() == "shut off":
            vm.start()
        elif vm.state() == "paused":
            vm.resume()
        session = vm.wait_for_login()

        # Actual results
        test_inside_mem = vm_proc_meminfo(session)
        session.close()
        test_outside_mem = vm.get_used_mem()

        # Expected results for both inside and outside
        if sizearg == "yes":
            expected_mem = int(dargs["sizearg"])
        else:
            expected_mem = int(dargs["size"])

        print_debug_stats(original_inside_mem, original_outside_mem,
                          test_inside_mem, test_outside_mem, expected_mem,
                          delta_percentage)

    if status is 0:  # Restore original memory
        restore_status = virsh.setmem(domainarg=vm_name,
                                      sizearg=original_inside_mem,
                                      ignore_status=True).exit_status
        if restore_status is not 0:
            logging.warning(
                "Failed to restore VM's original memory to %s KiB" %
                original_inside_mem)
    else:
        # virsh setmem failed, no need to restore
        pass

    # Don't care about memory comparison on error test
    if status_error == "no" and old_libvirt_fail == "no":
        outside_in_range = is_in_range(test_outside_mem, expected_mem,
                                       delta_percentage)
        inside_in_range = is_in_range(test_inside_mem, expected_mem,
                                      delta_percentage)
        if status is not 0 or not outside_in_range or not inside_in_range:
            msg = "test conditions not met: "
            if status is not 0:
                msg += "Non-zero virsh setmem exit code. "  # maybe multiple
            if not outside_in_range:  # errors
                msg += "Outside memory deviated. "
            if not inside_in_range:
                msg += "Inside memory deviated. "
            raise error.TestFail(msg)

        return  # Normal test passed

    else:  # Verify an error test resulted in error
        if status is 0:
            raise error.TestFail("Error test did not result in an error")
        else:  # status != 0
            if not old_libvirt:  # new libvirt should not have returnd error
                raise error.TestFail("Newer libvirt failed when it should not")
            else:
                # Test passes for old_libvirt == True
                pass
Example #17
0
            raise error.TestFail("Command virsh autostart is failed:\n%s" %
                                 result.stdout)
        else:
            logging.debug("Pool: %s marked for autostart successfully",
                          pool_name)

        # Step (7)
        if not check_list_state(pool_name, "active"):
            raise error.TestFail("State of pool: %s marked as inactive"
                                 "instead of active" % pool_name)
        if not check_list_autostart(pool_name, "yes"):
            raise error.TestFail("Autostart of pool: %s marked as no"
                                 "instead of yes" % pool_name)

        # Step (7.a)
        libvirt_vm.service_libvirtd_control("stop")
        # TODO: Add more negative cases after libvirtd stopped
        libvirt_vm.service_libvirtd_control("start")

        # Step (7.b)
        if not check_list_state(pool_name, "active"):
            raise error.TestFail("State of pool: %s marked as inactive"
                                 "instead of active" % pool_name)
        if not check_list_autostart(pool_name, "yes"):
            raise error.TestFail("Autostart of pool: %s marked as no"
                                 "instead of yes" % pool_name)

        # Step (8)
        result = virsh.pool_undefine(pool_name, ignore_status=True)
        if result.exit_status == 0:
            raise error.TestFail("Command virsh pool-undefine succeeded"
Example #18
0
def run_virsh_capabilities(test, params, env):
    """
    Test the command virsh capabilities

    (1) Call virsh capabilities
    (2) Call virsh capabilities with an unexpected option
    (3) Call virsh capabilities with libvirtd service stop
    """
    def compare_capabilities_xml(source):
        dom = parseString(source)
        host = dom.getElementsByTagName('host')[0]
        # check that host has a non-empty UUID tag.
        uuid = host.getElementsByTagName('uuid')[0]
        host_uuid_output = uuid.firstChild.data
        logging.info("Host uuid (capabilities_xml):%s", host_uuid_output)
        if host_uuid_output == "":
            raise error.TestFail("The host uuid in capabilities_xml is none!")

        # check the host arch.
        arch = host.getElementsByTagName('arch')[0]
        host_arch_output = arch.firstChild.data
        logging.info("Host arch (capabilities_xml):%s", host_arch_output)
        cmd_result = utils.run("arch", ignore_status=True)
        if cmp(host_arch_output, cmd_result.stdout.strip()) != 0:
            raise error.TestFail("The host arch in capabilities_xml is wrong!")

        # check the host cpus num.
        cpus = dom.getElementsByTagName('cpus')[0]
        host_cpus_output = cpus.getAttribute('num')
        logging.info("Host cpus num (capabilities_xml):%s", host_cpus_output)
        cmd = "less /proc/cpuinfo | grep processor | wc -l"
        cmd_result = utils.run(cmd, ignore_status=True)
        if cmp(host_cpus_output, cmd_result.stdout.strip()) != 0:
            raise error.TestFail("Host cpus num (capabilities_xml) is "
                                 "wrong")

        # check the arch of guest supported.
        cmd = "/usr/libexec/qemu-kvm  --cpu ? | grep qemu"
        cmd_result = utils.run(cmd, ignore_status=True)
        guest_wordsize_array = dom.getElementsByTagName('wordsize')
        length = len(guest_wordsize_array)
        for i in range(length):
            element = guest_wordsize_array[i]
            guest_wordsize = element.firstChild.data
            logging.info("Arch of guest supported (capabilities_xml):%s",
                         guest_wordsize)
            if not re.search(guest_wordsize, cmd_result.stdout.strip()):
                raise error.TestFail(
                    "The capabilities_xml gives an extra arch "
                    "of guest to support!")

        # check the type of hyperviosr.
        guest_domain_type = dom.getElementsByTagName('domain')[0]
        guest_domain_type_output = guest_domain_type.getAttribute('type')
        logging.info("Hypervisor (capabilities_xml):%s",
                     guest_domain_type_output)
        cmd_result = utils.run("virsh uri", ignore_status=True)
        if not re.search(guest_domain_type_output, cmd_result.stdout.strip()):
            raise error.TestFail("The capabilities_xml gives an different "
                                 "hypervisor")

    connect_uri = libvirt_vm.normalize_connect_uri(
        params.get("connect_uri", "default"))

    # Prepare libvirtd service
    if params.has_key("libvirtd"):
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    option = params.get("virsh_cap_options")
    try:
        output = virsh.capabilities(option,
                                    uri=connect_uri,
                                    ignore_status=False,
                                    debug=True)
        status = 0  # good
    except error.CmdError:
        status = 1  # bad
        output = ''

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail(
                    "Command 'virsh capabilities' succeeded "
                    "with libvirtd service stopped, incorrect")
            else:
                raise error.TestFail(
                    "Command 'virsh capabilities %s' succeeded "
                    "(incorrect command)" % option)
    elif status_error == "no":
        compare_capabilities_xml(output)
        if status != 0:
            raise error.TestFail("Command 'virsh capabilities %s' failed "
                                 "(correct command)" % option)
Example #19
0
def run_virsh_domname(test, params, env):
    """
    Test command: virsh domname <id/uuid>.

    1) Prepare libvirtd status and test environment.
    2) Try to get domname through valid and invalid command.
    3) Recover libvirtd service and test environment.
    4) Check result.
    """
    vm_name = params.get("main_vm", "vm1")
    vm = env.get_vm(params["main_vm"])

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    connect_uri = vm.connect_uri

    #run test case
    options_ref = params.get("domname_options_ref", "id")
    addition_status_error = params.get("addition_status_error", "no")
    status_error = params.get("status_error", "no")
    options = params.get("domname_options", "%s")
    options_suffix = params.get("domname_options_suffix", "")
    if options_ref == "id":
        options_ref = domid
        if options_ref == "-":
            options = "%s"
        else:
            options_ref = int(domid)
    elif options_ref == "uuid":
        options_ref = domuuid
        # UUID can get domain name in any state.
        logging.warning("Reset addition_status_error to NO for uuid test!")
        addition_status_error = "no"
    elif options_ref == "name":
        options_ref = vm_name

    if options:
        options = (options % options_ref)
    if options_suffix:
        options = options + " " + options_suffix

    #Prepare libvirtd status
    libvirtd = params.get("libvirtd", "on")
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    result = virsh.domname(options,
                           ignore_status=True,
                           debug=True,
                           uri=connect_uri)

    #Recover libvirtd service to start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")
        addition_status_error = "yes"

    #check status_error
    status_error = (status_error == "no") and (addition_status_error == "no")
    if status_error:
        if result.exit_status != 0 or result.stdout.strip() != vm_name:
            raise error.TestFail("Run failed because unexpected result.")
    else:
        if result.exit_status == 0 and result.stdout.strip() != vm_name:
            raise error.TestFail("Run passed but result is unexpected.")
Example #20
0
def run_virsh_nodecpustats(test, params, env):
    """
    Test the command virsh nodecpustats

    (1) Call the virsh nodecpustats command for all cpu host cpus
        separately
    (2) Get the output
    (3) Check the against /proc/stat output(o) for respective cpu
        user: o[0] + o[1]
        system: o[2] + o[5] + o[6]
        idle: o[3]
        iowait: o[4]
    (4) Call the virsh nodecpustats command with an unexpected option
    (5) Call the virsh nodecpustats command with libvirtd service stop
    """


    def virsh_check_nodecpustats_percpu(actual_stats):
        """
        Check the acual nodecpustats output value
        total time <= system uptime
        """

        # Normalise to seconds from nano seconds
        total = float((actual_stats['system'] + actual_stats['user'] + \
                    actual_stats['idle'] + actual_stats['iowait'])/(10 ** 9))
        uptime = float(utils.get_uptime())
        if not total <= uptime:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                 " as total time: %f is more"
                                 " than uptime: %f" % (total, uptime))
        return True


    def virsh_check_nodecpustats(actual_stats, cpu_count):
        """
        Check the acual nodecpustats output value
        total time <= system uptime
        """

        # Normalise to seconds from nano seconds and get for one cpu
        total = float(((actual_stats['system'] + actual_stats['user'] + \
                actual_stats['idle'] + actual_stats['iowait'])/(10 ** 9))/(
                cpu_count))
        uptime = float(utils.get_uptime())
        if not total <= uptime:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                 " as total time: %f is more"
                                 " than uptime: %f" % (total, uptime))
        return True


    def virsh_check_nodecpustats_percentage(actual_per):
        """
        Check the actual nodecpustats percentage adds upto 100%
        """

        total = int(round(actual_per['user'] + actual_per['system'] + \
                    actual_per['idle'] + actual_per['iowait']))

        if not total == 100:
            raise error.TestFail("Commands 'virsh nodecpustats' not succeeded"
                                  " as the total percentage value: %d"
                                  " is not equal 100" % total)


    def parse_output(output):
        """
        To get the output parsed into a dictionary
        @param: virsh command output

        @return: dict of user,system,idle,iowait times
        """

        # From the beginning of a line, group 1 is one or more word-characters,
        # followed by zero or more whitespace characters and a ':',
        # then one or more whitespace characters,
        # followed by group 2, which is one or more digit characters,
        # e.g as below
        # user:                  6163690000000
        #
        regex_obj = re.compile(r"^(\w+)\s*:\s+(\d+)")
        actual = {}

        for line in output.stdout.split('\n'):
            match_obj = regex_obj.search(line)
            # Due to the extra space in the list
            if match_obj is not None:
                name = match_obj.group(1)
                value = match_obj.group(2)
                actual[name] = int(value)
        return actual


    def parse_percentage_output(output):
        """
        To get the output parsed into a dictionary
        @param: virsh command output

        @return: dict of user,system,idle,iowait times
        """

        # From the beginning of a line, group 1 is one or more word-characters,
        # followed by zero or more whitespace characters and a ':',
        # then one or more whitespace characters,
        # followed by group 2, which is one or more digit characters,
        # e.g as below
        # user:             1.5%
        #
        regex_obj = re.compile(r"^(\w+)\s*:\s+(\d+.\d+)")
        actual_percentage = {}

        for line in output.stdout.split('\n'):
            match_obj = regex_obj.search(line)
            # Due to the extra space in the list
            if match_obj is not None:
                name = match_obj.group(1)
                value = match_obj.group(2)
                actual_percentage[name] = float(value)
        return actual_percentage


    # Initialize the variables
    itr = int(params.get("inner_test_iterations"))
    option = params.get("virsh_cpunodestats_options")
    status_error = params.get("status_error")
    libvirtd = params.get("libvirtd", "on")

    # Prepare libvirtd service
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    # Get the host cpu count
    host_cpu_count = utils.count_cpus()

    # Run test case for 5 iterations default can be changed in subtests.cfg file
    for i in range(itr):

        if status_error == "yes":
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                if libvirtd == "off":
                    libvirt_vm.service_libvirtd_control("start")
                    raise error.TestFail("Command 'virsh nodecpustats' "
                                     "succeeded with libvirtd service "
                                     "stopped, incorrect")
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s' "
                                     "succeeded (incorrect command)" % option)

        elif status_error == "no":
            # Run the testcase for each cpu to get the cpu stats
            for cpu in range(host_cpu_count):
                option = "--cpu %d" % cpu
                output = virsh.nodecpustats(ignore_status=True, option=option)
                status = output.exit_status


                if status == 0:
                    actual_value = parse_output(output)
                    virsh_check_nodecpustats_percpu(actual_value)
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s'"
                                         "not succeeded" % option)

            # Run the test case for each cpu to get the cpu stats in percentage
            for cpu in range(host_cpu_count):
                option = "--cpu %d --percent" % cpu
                output = virsh.nodecpustats(ignore_status=True, option=option)
                status = output.exit_status

                if status == 0:
                    actual_value = parse_percentage_output(output)
                    virsh_check_nodecpustats_percentage(actual_value)
                else:
                    raise error.TestFail("Command 'virsh nodecpustats %s'"
                                         " not succeeded" % option)

            option = ''
            # Run the test case for total cpus to get the cpus stats
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                actual_value = parse_output(output)
                virsh_check_nodecpustats(actual_value, host_cpu_count)
            else:
                raise error.TestFail("Command 'virsh nodecpustats %s'"
                                     " not succeeded" % option)

            # Run the test case for the total cpus to get the stats in percentage
            option = "--percent"
            output = virsh.nodecpustats(ignore_status=True, option=option)
            status = output.exit_status

            if status == 0:
                actual_value = parse_percentage_output(output)
                virsh_check_nodecpustats_percentage(actual_value)
            else:
                raise error.TestFail("Command 'virsh nodecpustats %s'"
                                     " not succeeded" % option)

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")
Example #21
0
def run_virsh_domname(test, params, env):
    """
    Test command: virsh domname <id/uuid>.

    1) Prepare libvirtd status and test environment.
    2) Try to get domname through valid and invalid command.
    3) Recover libvirtd service and test environment.
    4) Check result.
    """
    vm_name = params.get("main_vm", "vm1")
    vm = env.get_vm(params["main_vm"])

    domid = vm.get_id()
    domuuid = vm.get_uuid()
    connect_uri = vm.connect_uri

    #run test case
    options_ref = params.get("options_ref", "id")
    addition_status_error = params.get("addition_status_error", "no")
    status_error = params.get("status_error", "no")
    options = params.get("options", "%s")
    options_suffix = params.get("options_suffix", "")
    if options_ref == "id":
        options_ref = domid
        if options_ref == "-":
            options = "%s"
        else:
            options_ref = int(domid)
    elif options_ref == "uuid":
        options_ref = domuuid
        # UUID can get domain name in any state.
        logging.warning("Reset addition_status_error to NO for uuid test!")
        addition_status_error = "no"
    elif options_ref == "name":
        options_ref = vm_name

    if options:
        options = (options % options_ref)
    if options_suffix:
        options = options + " " + options_suffix

    #Prepare libvirtd status
    libvirtd = params.get("libvirtd", "on")
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("stop")

    result = virsh.domname(options, ignore_status=True, debug=True,
                           uri=connect_uri)

    #Recover libvirtd service to start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")
        addition_status_error = "yes"

    #check status_error
    status_error = (status_error == "no") and (addition_status_error == "no")
    if status_error:
        if result.exit_status != 0 or result.stdout.strip() != vm_name:
            raise error.TestFail("Run failed because unexpected result.")
    else:
        if result.exit_status == 0 and result.stdout.strip() != vm_name:
            raise error.TestFail("Run passed but result is unexpected.")
Example #22
0
def run_virsh_nodeinfo(test, params, env):
    """
    Test the command virsh nodeinfo

    (1) Call virsh nodeinfo
    (2) Call virsh nodeinfo with an unexpected option
    (3) Call virsh nodeinfo with libvirtd service stop
    """
    def _check_nodeinfo(nodeinfo_output, verify_str, column):
        cmd = "echo \"%s\" | grep \"%s\" | awk '{print $%s}'" % (
            nodeinfo_output, verify_str, column)
        cmd_result = utils.run(cmd, ignore_status=True)
        stdout = cmd_result.stdout.strip()
        logging.debug("Info %s on nodeinfo output:%s" % (verify_str, stdout))
        return stdout

    def output_check(nodeinfo_output):
        # Check CPU model
        cpu_model_nodeinfo = _check_nodeinfo(nodeinfo_output, "CPU model", 3)
        cpu_model_os = utils.get_current_kernel_arch()
        if not re.match(cpu_model_nodeinfo, cpu_model_os):
            raise error.TestFail(
                "Virsh nodeinfo output didn't match CPU model")

        # Check number of CPUs
        cpus_nodeinfo = _check_nodeinfo(nodeinfo_output, "CPU(s)", 2)
        cpus_os = utils.count_cpus()
        if int(cpus_nodeinfo) != cpus_os:
            raise error.TestFail(
                "Virsh nodeinfo output didn't match number of "
                "CPU(s)")

        # Check CPU frequency
        cpu_frequency_nodeinfo = _check_nodeinfo(nodeinfo_output,
                                                 'CPU frequency', 3)
        cmd = ("cat /proc/cpuinfo | grep 'cpu MHz' | head -n1 | "
               "awk '{print $4}' | awk -F. '{print $1}'")
        cmd_result = utils.run(cmd, ignore_status=True)
        cpu_frequency_os = cmd_result.stdout.strip()
        print cpu_frequency_os
        if not re.match(cpu_frequency_nodeinfo, cpu_frequency_os):
            raise error.TestFail("Virsh nodeinfo output didn't match CPU "
                                 "frequency")

        # Check CPU socket(s)
        cpu_sockets_nodeinfo = int(
            _check_nodeinfo(nodeinfo_output, 'CPU socket(s)', 3))
        cmd = "grep 'physical id' /proc/cpuinfo | uniq | sort | uniq |wc -l"
        cmd_result = utils.run(cmd, ignore_status=True)
        cpu_NUMA_nodeinfo = _check_nodeinfo(nodeinfo_output, 'NUMA cell(s)', 3)
        cpu_sockets_os = int(
            cmd_result.stdout.strip()) / int(cpu_NUMA_nodeinfo)
        if cpu_sockets_os != cpu_sockets_nodeinfo:
            raise error.TestFail("Virsh nodeinfo output didn't match CPU "
                                 "socket(s)")

        # Check Core(s) per socket
        cores_per_socket_nodeinfo = _check_nodeinfo(nodeinfo_output,
                                                    'Core(s) per socket', 4)
        cmd = "grep 'cpu cores' /proc/cpuinfo | head -n1 | awk '{print $4}'"
        cmd_result = utils.run(cmd, ignore_status=True)
        cores_per_socket_os = cmd_result.stdout.strip()
        if not re.match(cores_per_socket_nodeinfo, cores_per_socket_os):
            raise error.TestFail("Virsh nodeinfo output didn't match Core(s) "
                                 "per socket")

        # Check Memory size
        memory_size_nodeinfo = int(
            _check_nodeinfo(nodeinfo_output, 'Memory size', 3))
        memory_size_os = utils.memtotal()
        if memory_size_nodeinfo != memory_size_os:
            raise error.TestFail("Virsh nodeinfo output didn't match "
                                 "Memory size")

    # Prepare libvirtd service
    check_libvirtd = params.has_key("libvirtd")
    if check_libvirtd:
        libvirtd = params.get("libvirtd")
        if libvirtd == "off":
            libvirt_vm.service_libvirtd_control("stop")

    # Run test case
    option = params.get("virsh_node_options")
    cmd_result = virsh.nodeinfo(ignore_status=True, extra=option)
    logging.info("Output:\n%s", cmd_result.stdout.strip())
    logging.info("Status: %d", cmd_result.exit_status)
    logging.error("Error: %s", cmd_result.stderr.strip())
    output = cmd_result.stdout.strip()
    status = cmd_result.exit_status

    # Recover libvirtd service start
    if libvirtd == "off":
        libvirt_vm.service_libvirtd_control("start")

    # Check status_error
    status_error = params.get("status_error")
    if status_error == "yes":
        if status == 0:
            if libvirtd == "off":
                raise error.TestFail(
                    "Command 'virsh nodeinfo' succeeded "
                    "with libvirtd service stopped, incorrect")
            else:
                raise error.TestFail("Command 'virsh nodeinfo %s' succeeded"
                                     "(incorrect command)" % option)
    elif status_error == "no":
        output_check(output)
        if status != 0:
            raise error.TestFail("Command 'virsh nodeinfo %s' failed "
                                 "(correct command)" % option)