Ejemplo n.º 1
0
def get_domiftune_parameter(params):
    """
    Get the domiftune parameters
    :params: the parameter dictionary
    """
    vm_name = params.get("vms")
    options = params.get("options")
    interface = params.get("iface_dev")

    result = virsh.domiftune(vm_name, interface, options=options)
    status = result.exit_status

    # Check status_error
    status_error = params.get("status_error", "no")

    if status_error == "yes":
        if status:
            logging.info("It's an expected error: %s", result.stderr)
        else:
            raise error.TestFail("%d not a expected command "
                                 "return value", status)
    elif status_error == "no":
        if status:
            raise error.TestFail(result.stderr)
        else:
            logging.info(result.stdout)
Ejemplo n.º 2
0
def set_domiftune_parameter(params):
    """
    Set the domiftune parameters
    :params: the parameter dictionary
    """
    vm_name = params.get("vms")
    inbound = params.get("inbound")
    outbound = params.get("outbound")
    options = params.get("options", None)
    interface = params.get("iface_dev")

    result = virsh.domiftune(vm_name, interface, options, inbound, outbound)
    status = result.exit_status

    # Check status_error
    status_error = params.get("status_error", "no")

    if status_error == "yes":
        if status:
            logging.info("It's an expected error: %s", result.stderr)
        else:
            raise error.TestFail("%d not a expected command "
                                 "return value", status)
    elif status_error == "no":
        if status:
            raise error.TestFail(result.stderr)
        else:
            if check_domiftune(params):
                logging.info(result.stdout)
            else:
                error.TestFail("The 'inbound' or/and 'outbound' are"
                               " inconsistent with domiftune XML"
                               " and/or virsh command output")
Ejemplo n.º 3
0
def check_domiftune(params):
    """
    Compare inbound and outbound value with guest XML configuration
    and virsh command output.
    @params: the parameter dictionary
    """
    vm_name = params.get("vms")
    vm = params.get("vm")
    interface = params.get("iface_dev")
    options = params.get("options")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    inbound_from_cmd_output = None
    outbound_from_cmd_output = None
    domiftune_params = {}
    if vm and vm.is_alive():
        result = virsh.domiftune(vm_name, interface, options=options)
        dicts = {}
        o = result.stdout.strip().split("\n")
        for l in o:
            if l and l.find(':'):
                k, v = l.split(':')
                dicts[k.strip()] = v.strip()

        logging.debug(dicts)
        inbound_from_cmd_output = dicts['inbound.average']
        outbound_from_cmd_output = dicts['outbound.average']

    virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)

    if options == "config" and vm and vm.is_alive():
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name, "--inactive")
    elif vm and not vm.is_alive():
        logging.debug("The guest %s isn't running!", vm_name)
        return True
    else:
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name)

    inbound_from_xml = domiftune_params.get("inbound")
    outbound_from_xml = domiftune_params.get("outbound")

    if vm and vm.is_alive() and options != "config":
        if inbound and inbound != inbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", inbound,
                          inbound_from_cmd_output)
            return False
        if outbound and outbound != outbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", outbound,
                          outbound_from_cmd_output)
            return False
        if inbound and inbound_from_xml and inbound != inbound_from_xml:
            logging.error("To expect outbound %s: %s", inbound,
                          inbound_from_xml)
            return False
        if outbound and outbound_from_xml and outbound != outbound_from_xml:
            logging.error("To expect outbound %s: %s", outbound,
                          outbound_from_xml)
            return False

    return True
Ejemplo n.º 4
0
def get_domiftune_parameter(params, test):
    """
    Get the domiftune parameters
    :params: the parameter dictionary
    """
    vm_name = params.get("main_vm")
    options = params.get("options")
    interface = params.get("iface_dev")

    result = virsh.domiftune(vm_name, interface, options=options)
    status = result.exit_status

    # Check status_error
    status_error = params.get("status_error", "no")

    if status_error == "yes":
        if status:
            logging.info("It's an expected error: %s", result.stderr)
        else:
            test.fail("%d not a expected command return value" %
                      status)
    elif status_error == "no":
        if status:
            test.fail("Unexpected result, get domiftune: %s" %
                      result.stderr)
        else:
            logging.info("getdominfo return succesfully")
Ejemplo n.º 5
0
def run(test, params, env):
    """
    Test domiftune tuning

    1) Positive testing
       1.1) get the current domiftune parameters for a running guest
       1.2) set the current domiftune parameters for a running guest
    2) Negative testing
       2.1) get domiftune parameters
       2.2) set domiftune parameters
    """

    # Run test case
    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)
    status_error = params.get("status_error", "no")
    start_vm = params.get("start_vm", "yes")
    change_parameters = params.get("change_parameters", "no")
    interface_ref = params.get("interface_ref", "name")

    interface = []

    if vm and not vm.is_alive():
        vm.start()

    if vm and vm.is_alive():
        virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)
        interface = virt_xml_obj.get_iface_dev(vm_name)
        if_mac = interface[0]
        # Get interface name
        vmxml = virt_xml_obj.new_from_dumpxml(vm_name)
        if_node = vmxml.get_iface_all().get(if_mac)
        if_name = if_node.find('target').get('dev')

    if interface_ref == "name":
        interface = if_name

    if interface_ref == "mac":
        interface = if_mac

    logging.debug("the interface is %s", interface)

    test_dict = dict(params)
    test_dict['vm'] = vm
    if interface:
        test_dict['iface_dev'] = interface

    if start_vm == "no" and vm and vm.is_alive():
        vm.destroy()

    # positive and negative testing #########
    if change_parameters == "no":
        get_domiftune_parameter(test_dict, test)
    else:
        set_domiftune_parameter(test_dict, test)
        ret = virsh.domiftune(vm_name, interface, 'current', '0', '0')
        libvirt.check_exit_status(ret)
Ejemplo n.º 6
0
def set_domiftune_parameter(params):
    """
    Set the domiftune parameters
    :params: the parameter dictionary
    """
    vm_name = params.get("main_vm")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    options = params.get("options", None)
    interface = params.get("iface_dev")
    check_clear = params.get("check_clear", "no")
    status_error = params.get("status_error", "no")

    test_clear = False
    if check_clear == "yes":
        # In libvirt 1.2.3, commit id '14973382' it will now be possible
        # to pass a zero (0) as an inbound or outbound parameter to virsh
        # domiftune in order to clear all the settings found. So we'll
        # handle that difference here
        if libvirt_version.version_compare(1, 2, 3):
            test_clear = True
            # Although the .cfg file has "0" for these that will
            # not test whether we can clear the value. So let's
            # set it to "1", then after we are sure we can set it
            # we will clear it and check that it's clear
            if inbound:
                save_inbound = inbound
                # average,peak,burst
                inbound = "1,4,6"
                params['inbound'] = "1"
            if outbound:
                save_outbound = outbound
                # average,peak,burst
                outbound = "1,4,6"
                params['outbound'] = "1"
        else:
            # Prior to libvirt 1.2.3 this would be an error
            # So let's just treat it as such. Leaving the
            # inbound/outbound as zero should result in an
            # error on the following set, but a pass for
            # the test since the error is expected.
            status_error = "yes"

    result = virsh.domiftune(vm_name, interface, options, inbound, outbound)
    status = result.exit_status

    if status_error == "yes":
        if status:
            logging.info("It's an expected error: %s", result.stderr)
        else:
            raise error.TestFail("%d not a expected command return value" %
                                 status)
    elif status_error == "no":
        if status:
            raise error.TestFail("Unexpected set domiftune error: %s" %
                                 result.stderr)
        else:
            if not check_domiftune(params, False):
                raise error.TestFail("The 'inbound' or/and 'outbound' are"
                                     " inconsistent with domiftune XML"
                                     " and/or virsh command output")

    # If supported, then here's where we reset the inbound/outbound
    # back to what they were input as and then run the same domiftune
    # command.  That should result in a successful return and should
    # clear the parameter.
    if test_clear:
        if inbound:
            inbound = save_inbound
            params['inbound'] = save_inbound
        if outbound:
            outbound = save_outbound
            params['outbound'] = save_outbound
        result = virsh.domiftune(vm_name, interface, options, inbound, outbound)
        status = result.exit_status
        if status:
            raise error.TestFail("Unexpected failure when clearing: %s" %
                                 result.stderr)
        else:
            if not check_domiftune(params, True):
                raise error.TestFail("The 'inbound' or/and 'outbound' were "
                                     "not cleared.")
Ejemplo n.º 7
0
def check_domiftune(params, test_clear):
    """
    Compare inbound and outbound value with guest XML configuration
    and virsh command output.
    :params: the parameter dictionary
    """
    vm_name = params.get("main_vm")
    vm = params.get("vm")
    interface = params.get("iface_dev")
    options = params.get("options")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    inbound_from_cmd_output = None
    outbound_from_cmd_output = None
    peak_in_from_cmd_output = None
    peak_out_from_cmd_output = None
    burst_in_from_cmd_output = None
    burst_out_from_cmd_output = None
    domiftune_params = {}

    logging.debug("Checking inbound=%s outbound=%s", inbound, outbound)
    if vm and vm.is_alive():
        result = virsh.domiftune(vm_name, interface, options=options)
        dicts = {}
        o = result.stdout.strip().split("\n")
        for l in o:
            if l and l.find(':'):
                k, v = l.split(':')
                dicts[k.strip()] = v.strip()

        logging.debug(dicts)
        inbound_from_cmd_output = dicts['inbound.average']
        outbound_from_cmd_output = dicts['outbound.average']
        logging.debug("inbound_from_cmd_output=%s, outbound_from_cmd_output=%s",
                      inbound_from_cmd_output, outbound_from_cmd_output)
        peak_in_from_cmd_output = dicts['inbound.peak']
        peak_out_from_cmd_output = dicts['outbound.peak']
        burst_in_from_cmd_output = dicts['inbound.peak']
        burst_out_from_cmd_output = dicts['outbound.peak']

    virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)

    if options == "config" and vm and vm.is_alive():
        domiftune_params = virt_xml_obj.get_iftune_params(
            vm_name, "--inactive")
    elif vm and not vm.is_alive():
        logging.debug("The guest %s isn't running!", vm_name)
        return True
    else:
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name)

    inbound_from_xml = domiftune_params.get("inbound")
    outbound_from_xml = domiftune_params.get("outbound")
    logging.debug("inbound_from_xml=%s, outbound_from_xml=%s",
                  inbound_from_xml, outbound_from_cmd_output)

    if vm and vm.is_alive() and options != "config":
        if test_clear and inbound:
            if inbound_from_xml is not None or \
               inbound_from_cmd_output is not "0" or \
               peak_in_from_cmd_output is not "0" or \
               burst_in_from_cmd_output is not "0":
                logging.error("Inbound was not cleared, xml=%s "
                              "avg=%s peak=%s burst=%s",
                              inbound_from_xml,
                              inbound_from_cmd_output,
                              peak_in_from_cmd_output,
                              burst_in_from_cmd_output)
                return False
        if test_clear and outbound:
            if outbound_from_xml is not None or \
               outbound_from_cmd_output is not "0" or \
               peak_out_from_cmd_output is not "0" or \
               burst_out_from_cmd_output is not "0":
                logging.error("Outbound was not cleared, xml=%s "
                              "avg=%s peak=%s burst=%s",
                              outbound_from_xml,
                              outbound_from_cmd_output,
                              peak_out_from_cmd_output,
                              burst_out_from_cmd_output)
        if test_clear:
            return True
        if inbound and inbound != inbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", inbound,
                          inbound_from_cmd_output)
            return False
        if outbound and outbound != outbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", outbound,
                          outbound_from_cmd_output)
            return False
        if inbound and inbound_from_xml and inbound != inbound_from_xml:
            logging.error("To expect outbound %s: %s", inbound,
                          inbound_from_xml)
            return False
        if outbound and outbound_from_xml and outbound != outbound_from_xml:
            logging.error("To expect outbound %s: %s", outbound,
                          outbound_from_xml)
            return False

    return True
Ejemplo n.º 8
0
def check_domiftune(params, test_clear):
    """
    Compare inbound and outbound value with guest XML configuration
    and virsh command output.
    :params: the parameter dictionary
    """
    vm_name = params.get("main_vm")
    vm = params.get("vm")
    interface = params.get("iface_dev")
    options = params.get("options")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    set_clear = "yes" == params.get("set_clear", "no")
    test_outbound = "yes" == params.get("test_outbound", "no")
    test_inbound = "yes" == params.get("test_inbound", "no")
    status_error = 'yes' == params.get("status_error", "no")
    average_inbound_from_cmd_output = None
    average_outbound_from_cmd_output = None
    peak_inbound_from_cmd_output = None
    peak_outbound_from_cmd_output = None
    burst_inbound_from_cmd_output = None
    burst_outbound_from_cmd_output = None
    average_inbound_from_xml = None
    average_outbound_from_xml = None
    peak_inbound_from_xml = None
    peak_outbound_from_xml = None
    burst_inbound_from_xml = None
    burst_outbound_from_xml = None

    logging.debug("Checking inbound=%s outbound= %s", inbound, outbound)
    # get inbound and outbound parameters from virsh cmd setting
    if not status_error and test_inbound:
        in_list = re.findall(r'[0-9]+', inbound)
        inbound_average = int(in_list[0])
        if len(in_list) == 3:
            inbound_peak = int(in_list[1])
            inbound_burst = int(in_list[2])
    if not status_error and test_outbound:
        out_list = re.findall(r'[0-9]+', outbound)
        outbound_average = int(out_list[0])
        if len(out_list) == 3:
            outbound_peak = int(out_list[1])
            outbound_burst = int(out_list[2])

    # --config affect next boot
    if options == "config":
        vm.destroy()
        vm.start()

    # get inbound and outbound parameters from virsh cmd output
    if vm and vm.is_alive():
        result = virsh.domiftune(vm_name, interface, options=options)
        dicts = {}
        o = result.stdout.strip().split("\n")
        for l in o:
            if l and l.find(':'):
                k, v = l.split(':')
                dicts[k.strip()] = v.strip()

        logging.debug(dicts)
        average_inbound_from_cmd_output = int(dicts['inbound.average'])
        average_outbound_from_cmd_output = int(dicts['outbound.average'])
        peak_inbound_from_cmd_output = int(dicts['inbound.peak'])
        peak_outbound_from_cmd_output = int(dicts['outbound.peak'])
        burst_inbound_from_cmd_output = int(dicts['inbound.burst'])
        burst_outbound_from_cmd_output = int(dicts['outbound.burst'])

        logging.debug("inbound and outbound from cmd output:")
        logging.debug("inbound: %s,%s,%s; outbound: %s,%s,%s"
                      % (average_inbound_from_cmd_output, peak_inbound_from_cmd_output,
                         burst_inbound_from_cmd_output, average_outbound_from_cmd_output,
                         peak_outbound_from_cmd_output, burst_outbound_from_cmd_output))

    # get inbound and outbound parameters from vm xml
    virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)

    if options == "config" and vm and vm.is_alive():
        domiftune_params = virt_xml_obj.get_iftune_params(
            vm_name, "--inactive")
    elif vm and not vm.is_alive():
        logging.debug("The guest %s isn't running!", vm_name)
        return True
    else:
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name)

    try:
        logging.debug("test inbound is %s, test outbound is %s" %
                      (test_inbound, test_outbound))
        if test_inbound:
            average_inbound_from_xml = int(domiftune_params.get("inbound").get("average"))
            peak_inbound_from_xml = int(domiftune_params.get("inbound").get("peak"))
            burst_inbound_from_xml = int(domiftune_params.get("inbound").get("burst"))
            logging.debug("inbound from xml:")
            logging.debug("%s, %s, %s" % (average_inbound_from_xml, peak_inbound_from_xml,
                                          burst_inbound_from_xml))
        if test_outbound:
            average_outbound_from_xml = int(domiftune_params.get("outbound").get("average"))
            peak_outbound_from_xml = int(domiftune_params.get("outbound").get("peak"))
            burst_outbound_from_xml = int(domiftune_params.get("outbound").get("burst"))
            logging.debug("outbound from xml:")
            logging.debug("%s, %s, %s" % (average_outbound_from_xml, peak_outbound_from_xml,
                                          burst_outbound_from_xml))
    except AttributeError as details:
        logging.error("Error in get inbound/outbound average: %s", details)
    logging.debug("average_inbound_from_xml=%s, average_outbound_from_xml=%s",
                  average_inbound_from_xml, average_outbound_from_xml)

    if vm and vm.is_alive():
        if test_clear and set_clear and test_inbound:
            if average_inbound_from_xml is not None or \
               average_inbound_from_cmd_output != 0 or \
               peak_inbound_from_cmd_output != 0 or \
               burst_inbound_from_cmd_output != 0:
                logging.error("Inbound was not cleared, xml=%s "
                              "avg=%s peak=%s burst=%s",
                              average_inbound_from_xml,
                              average_inbound_from_cmd_output,
                              peak_inbound_from_cmd_output,
                              burst_inbound_from_cmd_output)
                return False
            else:
                return True
        if test_clear and set_clear and test_outbound:
            if average_outbound_from_xml is not None or \
               average_outbound_from_cmd_output != 0 or \
               peak_outbound_from_cmd_output != 0 or \
               burst_outbound_from_cmd_output != 0:
                logging.error("Outbound was not cleared, xml=%s "
                              "avg=%s peak=%s burst=%s",
                              average_outbound_from_xml,
                              average_outbound_from_cmd_output,
                              peak_outbound_from_cmd_output,
                              burst_outbound_from_cmd_output)
                return False
            else:
                return True
        if test_inbound and (inbound_average != average_inbound_from_cmd_output
                             or inbound_peak != peak_inbound_from_cmd_output
                             or inbound_burst != burst_inbound_from_cmd_output):
            logging.error("To expect inbound %s: but got {average: %s, peak:"
                          " %s, burst: %s} from cmd output", inbound,
                          average_inbound_from_cmd_output, peak_inbound_from_cmd_output,
                          burst_inbound_from_cmd_output)
            return False
        if test_inbound and (inbound_average != average_inbound_from_xml
                             or inbound_peak != peak_inbound_from_xml
                             or inbound_burst != burst_inbound_from_xml):
            logging.error("To expect inbound %s: but got {average: %s, peak:"
                          " %s, burst: %s} from xml", inbound,
                          average_inbound_from_xml, peak_inbound_from_xml,
                          burst_inbound_from_xml)
            return False
        if test_outbound and (outbound_average != average_outbound_from_cmd_output
                              or outbound_peak != peak_outbound_from_cmd_output
                              or outbound_burst != burst_outbound_from_cmd_output):
            logging.error("To expect outbound %s: but got {average: %s, peak:"
                          " %s, burst: %s} from cmd output", outbound,
                          average_outbound_from_cmd_output, peak_outbound_from_cmd_output,
                          burst_outbound_from_cmd_output)
            return False
        if test_outbound and (outbound_average != average_outbound_from_xml or
                              outbound_peak != peak_outbound_from_xml or
                              outbound_burst != burst_outbound_from_xml):
            logging.error("To expect outbound %s: but got {average: %s, peak:"
                          " %s, burst: %s} from xml", outbound,
                          average_outbound_from_xml, peak_outbound_from_xml,
                          burst_outbound_from_xml)
            return False

    return True
Ejemplo n.º 9
0
def check_domiftune(params, test_clear):
    """
    Compare inbound and outbound value with guest XML configuration
    and virsh command output.
    :params: the parameter dictionary
    """
    vm_name = params.get("main_vm")
    vm = params.get("vm")
    interface = params.get("iface_dev")
    options = params.get("options")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    set_clear = "yes" == params.get("set_clear", "no")
    test_outbound = "yes" == params.get("test_outbound", "no")
    test_inbound = "yes" == params.get("test_inbound", "no")
    status_error = 'yes' == params.get("status_error", "no")
    later_start = 'yes' == params.get("later_start", "no")
    average_inbound_from_cmd_output = None
    average_outbound_from_cmd_output = None
    peak_inbound_from_cmd_output = None
    peak_outbound_from_cmd_output = None
    burst_inbound_from_cmd_output = None
    burst_outbound_from_cmd_output = None
    floor_inbound_from_cmd_output = None
    average_inbound_from_xml = None
    average_outbound_from_xml = None
    peak_inbound_from_xml = None
    peak_outbound_from_xml = None
    burst_inbound_from_xml = None
    burst_outbound_from_xml = None
    floor_inbound_from_xml = None
    netfloor = params.get("netfloor")

    logging.debug("Checking inbound=%s outbound= %s", inbound, outbound)
    # get inbound and outbound parameters from virsh cmd setting
    if not status_error and test_inbound:
        in_list = re.findall(r'[0-9]+', inbound)
        inbound_average = int(in_list[0])
        if len(in_list) >= 3:
            inbound_peak = int(in_list[1])
            inbound_burst = int(in_list[2])
            if netfloor:
                inbound_floor = int(in_list[3])
    if not status_error and test_outbound:
        out_list = re.findall(r'[0-9]+', outbound)
        outbound_average = int(out_list[0])
        if len(out_list) == 3:
            outbound_peak = int(out_list[1])
            outbound_burst = int(out_list[2])

    # --config affect next boot
    if options == "config":
        vm.destroy()
        vm.start()

    # setting for shutoff guest need start to check
    if later_start:
        vm.start()

    # get inbound and outbound parameters from virsh cmd output
    if vm and vm.is_alive():
        result = virsh.domiftune(vm_name, interface, options=options)
        dicts = {}
        bws = result.stdout.strip().split("\n")
        for bw in bws:
            if bw and bw.find(':'):
                k, v = bw.split(':')
                dicts[k.strip()] = v.strip()

        logging.debug(dicts)
        average_inbound_from_cmd_output = int(dicts['inbound.average'])
        average_outbound_from_cmd_output = int(dicts['outbound.average'])
        peak_inbound_from_cmd_output = int(dicts['inbound.peak'])
        peak_outbound_from_cmd_output = int(dicts['outbound.peak'])
        burst_inbound_from_cmd_output = int(dicts['inbound.burst'])
        burst_outbound_from_cmd_output = int(dicts['outbound.burst'])
        floor_inbound_from_cmd_output = int(dicts['inbound.floor'])

        logging.debug("inbound and outbound from cmd output:")
        logging.debug(
            "inbound: %s,%s,%s; outbound: %s,%s,%s",
            average_inbound_from_cmd_output, peak_inbound_from_cmd_output,
            burst_inbound_from_cmd_output, average_outbound_from_cmd_output,
            peak_outbound_from_cmd_output, burst_outbound_from_cmd_output)
        if netfloor:
            logging.debug("inbound floor: %s", floor_inbound_from_cmd_output)

    # get inbound and outbound parameters from vm xml
    virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)

    if options == "config" and vm and vm.is_alive():
        domiftune_params = virt_xml_obj.get_iftune_params(
            vm_name, "--inactive")
    elif vm and not vm.is_alive():
        logging.debug("The guest %s isn't running!", vm_name)
        return True
    else:
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name)

    try:
        logging.debug("test inbound is %s, test outbound is %s", test_inbound,
                      test_outbound)
        if test_inbound:
            average_inbound_from_xml = int(
                domiftune_params.get("inbound").get("average"))
            peak_inbound_from_xml = int(
                domiftune_params.get("inbound").get("peak"))
            burst_inbound_from_xml = int(
                domiftune_params.get("inbound").get("burst"))
            logging.debug("inbound from xml:")
            logging.debug("%s, %s, %s", average_inbound_from_xml,
                          peak_inbound_from_xml, burst_inbound_from_xml)
            if netfloor:
                floor_inbound_from_xml = int(
                    domiftune_params.get("inbound").get("floor"))
                logging.debug("inbound floor from xml: %s",
                              floor_inbound_from_xml)
        if test_outbound:
            average_outbound_from_xml = int(
                domiftune_params.get("outbound").get("average"))
            peak_outbound_from_xml = int(
                domiftune_params.get("outbound").get("peak"))
            burst_outbound_from_xml = int(
                domiftune_params.get("outbound").get("burst"))
            logging.debug("outbound from xml:")
            logging.debug("%s, %s, %s", average_outbound_from_xml,
                          peak_outbound_from_xml, burst_outbound_from_xml)
    except AttributeError as details:
        logging.error("Error in get inbound/outbound average: %s", details)
    logging.debug("average_inbound_from_xml=%s, average_outbound_from_xml=%s",
                  average_inbound_from_xml, average_outbound_from_xml)

    if vm and vm.is_alive():
        if test_clear and set_clear and test_inbound:
            if average_inbound_from_xml is not None or \
               average_inbound_from_cmd_output != 0 or \
               peak_inbound_from_cmd_output != 0 or \
               burst_inbound_from_cmd_output != 0:
                logging.error(
                    "Inbound was not cleared, xml=%s "
                    "avg=%s peak=%s burst=%s", average_inbound_from_xml,
                    average_inbound_from_cmd_output,
                    peak_inbound_from_cmd_output,
                    burst_inbound_from_cmd_output)
                return False
            else:
                return True
        if test_clear and set_clear and test_outbound:
            if average_outbound_from_xml is not None or \
               average_outbound_from_cmd_output != 0 or \
               peak_outbound_from_cmd_output != 0 or \
               burst_outbound_from_cmd_output != 0:
                logging.error(
                    "Outbound was not cleared, xml=%s "
                    "avg=%s peak=%s burst=%s", average_outbound_from_xml,
                    average_outbound_from_cmd_output,
                    peak_outbound_from_cmd_output,
                    burst_outbound_from_cmd_output)
                return False
            else:
                return True
        if test_inbound and (inbound_average != average_inbound_from_cmd_output
                             or inbound_peak != peak_inbound_from_cmd_output or
                             inbound_burst != burst_inbound_from_cmd_output):
            logging.error(
                "To expect inbound %s: but got {average: %s, peak:"
                " %s, burst: %s} from cmd output", inbound,
                average_inbound_from_cmd_output, peak_inbound_from_cmd_output,
                burst_inbound_from_cmd_output)
            return False
        if netfloor and inbound_floor != floor_inbound_from_cmd_output:
            logging.error(
                "To expect inbound floor %s, but got %s from cmd output",
                inbound_floor, floor_inbound_from_cmd_output)
            return False
        if test_inbound and (inbound_average != average_inbound_from_xml
                             or inbound_peak != peak_inbound_from_xml
                             or inbound_burst != burst_inbound_from_xml):
            logging.error(
                "To expect inbound %s: but got {average: %s, peak:"
                " %s, burst: %s} from xml", inbound, average_inbound_from_xml,
                peak_inbound_from_xml, burst_inbound_from_xml)
            return False
        if netfloor and inbound_floor != floor_inbound_from_xml:
            logging.error("To expect inbound floor %s, but got %s from xml",
                          inbound_floor, floor_inbound_from_xml)
            return False
        if test_outbound and (
                outbound_average != average_outbound_from_cmd_output
                or outbound_peak != peak_outbound_from_cmd_output
                or outbound_burst != burst_outbound_from_cmd_output):
            logging.error(
                "To expect outbound %s: but got {average: %s, peak:"
                " %s, burst: %s} from cmd output", outbound,
                average_outbound_from_cmd_output,
                peak_outbound_from_cmd_output, burst_outbound_from_cmd_output)
            return False
        if test_outbound and (outbound_average != average_outbound_from_xml
                              or outbound_peak != peak_outbound_from_xml
                              or outbound_burst != burst_outbound_from_xml):
            logging.error(
                "To expect outbound %s: but got {average: %s, peak:"
                " %s, burst: %s} from xml", outbound,
                average_outbound_from_xml, peak_outbound_from_xml,
                burst_outbound_from_xml)
            return False

    return True
Ejemplo n.º 10
0
def check_domiftune(params, test_clear):
    """
    Compare inbound and outbound value with guest XML configuration
    and virsh command output.
    :params: the parameter dictionary
    """
    vm_name = params.get("vms")
    vm = params.get("vm")
    interface = params.get("iface_dev")
    options = params.get("options")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    inbound_from_cmd_output = None
    outbound_from_cmd_output = None
    peak_in_from_cmd_output = None
    peak_out_from_cmd_output = None
    burst_in_from_cmd_output = None
    burst_out_from_cmd_output = None
    domiftune_params = {}

    logging.debug("Checking inbound=%s outbound=%s", inbound, outbound)
    if vm and vm.is_alive():
        result = virsh.domiftune(vm_name, interface, options=options)
        dicts = {}
        o = result.stdout.strip().split("\n")
        for l in o:
            if l and l.find(':'):
                k, v = l.split(':')
                dicts[k.strip()] = v.strip()

        logging.debug(dicts)
        inbound_from_cmd_output = dicts['inbound.average']
        outbound_from_cmd_output = dicts['outbound.average']
        logging.debug(
            "inbound_from_cmd_output=%s, outbound_from_cmd_output=%s",
            inbound_from_cmd_output, outbound_from_cmd_output)
        peak_in_from_cmd_output = dicts['inbound.peak']
        peak_out_from_cmd_output = dicts['outbound.peak']
        burst_in_from_cmd_output = dicts['inbound.peak']
        burst_out_from_cmd_output = dicts['outbound.peak']

    virt_xml_obj = vm_xml.VMXML(virsh_instance=virsh)

    if options == "config" and vm and vm.is_alive():
        domiftune_params = virt_xml_obj.get_iftune_params(
            vm_name, "--inactive")
    elif vm and not vm.is_alive():
        logging.debug("The guest %s isn't running!", vm_name)
        return True
    else:
        domiftune_params = virt_xml_obj.get_iftune_params(vm_name)

    inbound_from_xml = domiftune_params.get("inbound")
    outbound_from_xml = domiftune_params.get("outbound")
    logging.debug("inbound_from_xml=%s, outbound_from_xml=%s",
                  inbound_from_xml, outbound_from_cmd_output)

    if vm and vm.is_alive() and options != "config":
        if test_clear and inbound:
            if inbound_from_xml is not None or \
               inbound_from_cmd_output is not "0" or \
               peak_in_from_cmd_output is not "0" or \
               burst_in_from_cmd_output is not "0":
                logging.error(
                    "Inbound was not cleared, xml=%s "
                    "avg=%s peak=%s burst=%s", inbound_from_xml,
                    inbound_from_cmd_output, peak_in_from_cmd_output,
                    burst_in_from_cmd_output)
                return False
        if test_clear and outbound:
            if outbound_from_xml is not None or \
               outbound_from_cmd_output is not "0" or \
               peak_out_from_cmd_output is not "0" or \
               burst_out_from_cmd_output is not "0":
                logging.error(
                    "Outbound was not cleared, xml=%s "
                    "avg=%s peak=%s burst=%s", outbound_from_xml,
                    outbound_from_cmd_output, peak_out_from_cmd_output,
                    burst_out_from_cmd_output)
        if test_clear:
            return True
        if inbound and inbound != inbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", inbound,
                          inbound_from_cmd_output)
            return False
        if outbound and outbound != outbound_from_cmd_output:
            logging.error("To expect inbound %s: %s", outbound,
                          outbound_from_cmd_output)
            return False
        if inbound and inbound_from_xml and inbound != inbound_from_xml:
            logging.error("To expect outbound %s: %s", inbound,
                          inbound_from_xml)
            return False
        if outbound and outbound_from_xml and outbound != outbound_from_xml:
            logging.error("To expect outbound %s: %s", outbound,
                          outbound_from_xml)
            return False

    return True
Ejemplo n.º 11
0
def set_domiftune_parameter(params):
    """
    Set the domiftune parameters
    :params: the parameter dictionary
    """
    vm_name = params.get("vms")
    inbound = params.get("inbound", "")
    outbound = params.get("outbound", "")
    options = params.get("options", None)
    interface = params.get("iface_dev")
    check_clear = params.get("check_clear", "no")
    status_error = params.get("status_error", "no")

    test_clear = False
    if check_clear == "yes":
        # In libvirt 1.2.3, commit id '14973382' it will now be possible
        # to pass a zero (0) as an inbound or outbound parameter to virsh
        # domiftune in order to clear all the settings found. So we'll
        # handle that difference here
        if libvirt_version.version_compare(1, 2, 3):
            test_clear = True
            # Although the .cfg file has "0" for these that will
            # not test whether we can clear the value. So let's
            # set it to "1", then after we are sure we can set it
            # we will clear it and check that it's clear
            if inbound:
                save_inbound = inbound
                # average,peak,burst
                inbound = "1,4,6"
                params['inbound'] = "1"
            if outbound:
                save_outbound = outbound
                # average,peak,burst
                outbound = "1,4,6"
                params['outbound'] = "1"
        else:
            # Prior to libvirt 1.2.3 this would be an error
            # So let's just treat it as such. Leaving the
            # inbound/outbound as zero should result in an
            # error on the following set, but a pass for
            # the test since the error is expected.
            status_error = "yes"

    result = virsh.domiftune(vm_name, interface, options, inbound, outbound)
    status = result.exit_status

    if status_error == "yes":
        if status:
            logging.info("It's an expected error: %s", result.stderr)
        else:
            raise error.TestFail("%d not a expected command return value" %
                                 status)
    elif status_error == "no":
        if status:
            raise error.TestFail("Unexpected set domiftune error: %s" %
                                 result.stderr)
        else:
            if not check_domiftune(params, False):
                raise error.TestFail("The 'inbound' or/and 'outbound' are"
                                     " inconsistent with domiftune XML"
                                     " and/or virsh command output")

    # If supported, then here's where we reset the inbound/outbound
    # back to what they were input as and then run the same domiftune
    # command.  That should result in a successful return and should
    # clear the parameter.
    if test_clear:
        if inbound:
            inbound = save_inbound
            params['inbound'] = save_inbound
        if outbound:
            outbound = save_outbound
            params['outbound'] = save_outbound
        result = virsh.domiftune(vm_name, interface, options, inbound,
                                 outbound)
        status = result.exit_status
        if status:
            raise error.TestFail("Unexpected failure when clearing: %s" %
                                 result.stderr)
        else:
            if not check_domiftune(params, True):
                raise error.TestFail("The 'inbound' or/and 'outbound' were "
                                     "not cleared.")
Ejemplo n.º 12
0
def run(test, params, env):
    """
    Test domiftune tuning

    1) Positive testing
       1.1) get the current domiftune parameters for a running guest
       1.2) set the current domiftune parameters for a running guest
    2) Negative testing
       2.1) get domiftune parameters
       2.2) set domiftune parameters
    """

    # Run test case
    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)
    status_error = params.get("status_error", "no")
    start_vm = params.get("start_vm", "yes")
    change_parameters = params.get("change_parameters", "no")
    interface_ref = params.get("interface_ref", "name")
    netfloor = params.get("netfloor")
    pre_vmstate = params.get("pre_vmstate")
    interface = []
    change_net = False

    def set_netbw(netfloor, if_net, netxml):
        """
        Set network bandwidth as required.

        :param netfloor: action on network bandwidth, 'need' or 'delete'
        :param if_net: network name acquired from guest interface
        :param netxml: the network xml
        """
        if netfloor == 'delete':
            netxml.del_element('/bandwidth')
        elif netfloor == 'need':
            netxml.bandwidth_inbound = {
                'average': 100,
                'peak': 200,
                'burst': 128
            }
            netxml.bandwidth_outbound = {
                'average': 50,
                'peak': 100,
                'burst': 128
            }
        netxml.sync()
        virsh.net_dumpxml(if_net, debug=True)

    def get_iface(vm_name, if_mac):
        """
        Get the first interface dev from guest xml

        :param vm_name: guest name
        :param if_mac: the interface mac
        """
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        if_node = vmxml.get_iface_all().get(if_mac)
        return if_node

    if pre_vmstate == "shutoff" and vm.is_alive():
        vm.destroy()

    if_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
    if netfloor:
        if_node = get_iface(vm_name, if_mac)
        if_net = if_node.find('source').get('network')
        netxml = network_xml.NetworkXML.new_from_net_dumpxml(if_net)
        netxml_backup = netxml.copy()
        if netfloor == 'delete' and 'bandwidth' in str(netxml) or \
                netfloor == 'need' and 'bandwidth' not in str(netxml):
            set_netbw(netfloor, if_net, netxml)
            change_net = True

    if vm and not vm.is_alive():
        vm.start()

    if vm and vm.is_alive():
        if_node = get_iface(vm_name, if_mac)
        if_name = if_node.find('target').get('dev')

    if interface_ref == "name":
        interface = if_name

    if interface_ref == "mac":
        interface = if_mac

    logging.debug("the interface is %s", interface)

    test_dict = dict(params)
    test_dict['vm'] = vm
    if interface:
        test_dict['iface_dev'] = interface

    if start_vm == "no" and vm and vm.is_alive():
        vm.destroy()

    # positive and negative testing #########
    libvirtd = utils_libvirtd.Libvirtd()
    if change_parameters == "no":
        get_domiftune_parameter(test_dict, test, libvirtd)
    else:
        set_domiftune_parameter(test_dict, test, libvirtd)

    if change_parameters != "no":
        if change_net:
            netxml_backup.sync()
            if vm.is_alive():
                vm.destroy()
                vm.start()
        if not status_error and interface_ref == "mac":
            opt = 'config'
        else:
            opt = 'current'
        ret = virsh.domiftune(vm_name, if_mac, opt, '0', '0', debug=True)
        libvirt.check_exit_status(ret)
Ejemplo n.º 13
0
def run(test, params, env):
    """
    Test openvswitch support for network.

    1.Prepare test environment,destroy or suspend a VM.
    2.Edit xml and start the domain.
    3.Perform test operation.
    4.Recover test environment.
    5.Confirm the test result.
    """
    vm_name = params.get("main_vm")
    vm = env.get_vm(vm_name)

    # install openvswitch on host.
    if distro.detect().name == 'Ubuntu':
        pkg = "openvswitch-switch"
    else:
        pkg = "openvswitch"

    ovs_service = service.Factory.create_service(pkg)

    if not shutil.which('ovs-vsctl') and not utils_package.package_install(
            pkg):
        test.cancel("Failed to install dependency package %s" " on host" % pkg)
    if not ovs_service.status():
        logging.debug("Restart %s service.." % pkg)
        ovs_service.restart()

    def check_ovs_port(ifname, brname):
        """
        Check OVS port that created by libvirt
        """
        pg_name = params.get("porgroup_name", "").split()
        pg_vlan = params.get("portgroup_vlan", "").split()
        if_source = eval(iface_source)
        port_vlan = {}
        if "portgroup" in if_source:
            pg = if_source["portgroup"]
            for (name, vlan) in zip(pg_name, pg_vlan):
                if pg == name:
                    port_vlan = eval(vlan)
        # Check bridge name by port name
        _, bridge = utils_net.find_current_bridge(ifname)
        assert bridge == brname
        # Get port info from ovs-vsctl output
        cmd = "ovs-vsctl list port %s" % ifname
        output = process.run(cmd, shell=True).stdout_text
        logging.debug("ovs port output: %s", output)
        for line in output.splitlines():
            if line.count("tag"):
                tag_info = line.rsplit(':')
                if ("id" in port_vlan and tag_info[0] == "tag"):
                    assert port_vlan["id"] == tag_info[1]
            elif line.count("vlan_mode"):
                mode_info = line.rsplit(':')
                if ("nativeMode" in port_vlan and mode_info[0] == "vlan_mode"):
                    assert (port_vlan["nativeMode"] == "native-%s" %
                            mode_info[1])

    start_error = "yes" == params.get("start_error", "no")

    # network specific attributes.
    net_name = params.get("net_name", "default")
    net_bridge = params.get("net_bridge", "{'name':'virbr0'}")
    iface_source = params.get("iface_source", "{}")
    create_network = "yes" == params.get("create_network", "no")
    change_iface_option = "yes" == params.get("change_iface_option", "no")
    test_ovs_port = "yes" == params.get("test_ovs_port", "no")
    iface_inbound = params.get("iface_bandwidth_inbound")
    iface_outbound = params.get("iface_bandwidth_outbound")
    test_qos = "yes" == params.get("test_qos", "no")
    hotplug = "yes" == params.get("hotplug", "no")
    iface_type = params.get("iface_type", "bridge")
    iface_model = params.get("iface_model", "virtio")
    iface_virtualport = params.get("iface_virtualport")
    live_add_qos = "yes" == params.get("live_add_qos", 'no')

    libvirt_version.is_libvirt_feature_supported(params)
    # Destroy the guest first
    if vm.is_alive():
        vm.destroy(gracefully=False)

    # Back up xml file.
    netxml_backup = NetworkXML.new_from_net_dumpxml("default")
    iface_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
    params["guest_mac"] = iface_mac
    vmxml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)

    bridge_name = eval(net_bridge)['name']
    # Build the xml and run test.
    try:
        # Edit the network xml or create a new one.
        if create_network:
            # Try to add ovs bridge first
            if not utils_net.ovs_br_exists(bridge_name):
                utils_net.add_ovs_bridge(bridge_name)
            netxml = libvirt.create_net_xml(net_name, params)
            netxml.sync()
        # Edit the interface xml.
        if change_iface_option:
            # Try to add bridge if needed
            source = eval(iface_source)
            if source:
                if "bridge" in source:
                    if not utils_net.ovs_br_exists(source["bridge"]):
                        utils_net.add_ovs_bridge(source["bridge"])
            iface_dict = {
                'type': iface_type,
                'model': iface_model,
                'source': iface_source,
                'virtualport_type': iface_virtualport,
                'inbound': iface_inbound,
                'outbound': iface_outbound
            }
            if live_add_qos:
                iface_dict.pop('inbound')
                iface_dict.pop('outbound')
            if not hotplug:
                libvirt.modify_vm_iface(vm_name, 'update_iface', iface_dict)
            else:
                iface_attach_xml = os.path.join(data_dir.get_data_dir(),
                                                "iface_attach.xml")
                shutil.copyfile(
                    libvirt.modify_vm_iface(vm_name, 'get_xml', iface_dict),
                    iface_attach_xml)
                libvirt_vmxml.remove_vm_devices_by_type(vm, 'interface')

        try:
            # Start the VM.
            vm.start()
            if start_error:
                test.fail("VM started unexpectedly")
            if hotplug:
                virsh.attach_device(vm_name,
                                    iface_attach_xml,
                                    debug=True,
                                    ignore_status=False)
            iface_name = libvirt.get_ifname_host(vm_name, iface_mac)
            if test_ovs_port:
                check_ovs_port(iface_name, bridge_name)
            if live_add_qos:
                inbound_opt = ",".join(re.findall(r'[0-9]+', iface_inbound))
                outbount_opt = ",".join(re.findall(r'[0-9]+', iface_outbound))
                virsh.domiftune(vm_name,
                                iface_name,
                                inbound=inbound_opt,
                                outbound=outbount_opt,
                                debug=True,
                                ignore_status=False)
            if test_qos:
                iface_mac = vm_xml.VMXML.get_first_mac_by_name(vm_name)
                tap_name = libvirt.get_ifname_host(vm_name, iface_mac)
                logging.info("Test inbound:")
                res1 = utils_net.check_class_rules(
                    tap_name, "1:1", ast.literal_eval(iface_inbound))
                logging.info("Test outbound:")
                res2 = utils_net.check_filter_rules(
                    tap_name, ast.literal_eval(iface_outbound))
                if not res1 or not res2:
                    test.fail("Qos test fail!")

        except virt_vm.VMStartError as details:
            logging.info(str(details))
            if not start_error:
                test.fail('VM failed to start:\n%s' % details)

    finally:
        # Recover VM.
        if vm.is_alive():
            vm.destroy(gracefully=False)
        logging.info("Restoring network...")
        if net_name == "default":
            netxml_backup.sync()
        else:
            # Destroy and undefine new created network
            virsh.net_destroy(net_name)
            virsh.net_undefine(net_name)
        # Try to recovery ovs bridge
        if utils_net.ovs_br_exists(bridge_name):
            utils_net.del_ovs_bridge(bridge_name)
        if "iface_attach_xml" in locals():
            os.remove(iface_attach_xml)
        vmxml_backup.sync()