Пример #1
0
 def create_interface():
     """
         Call different function to create interface according to the type
     """
     new_iface = Interface('network')
     if vf_type == "vf":
         new_iface = create_hostdev_interface(vf_addr, managed, model)
     if vf_type == "vf_pool":
         netxml = create_hostdev_network()
         virsh.net_define(netxml.xml, ignore_status=True)
         if not inactive_pool:
             virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     if vf_type == "macvtap":
         new_iface = Interface('direct')
         new_iface.source = {"dev": vf_name, "mode": "passthrough"}
         new_iface.mac_address = utils_net.generate_mac_address_simple()
         new_iface.model = "virtio"
         if vlan_id:
             new_iface.vlan = new_iface.new_vlan(**vlan_id)
     if vf_type == "macvtap_network":
         netxml = create_macvtap_network()
         result = virsh.net_define(netxml.xml, ignore_status=True)
         virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     return new_iface
Пример #2
0
    def create_iface(iface_model, iface_source, **kwargs):
        """
        Create an interface to be attached to vm

        :param iface_model: model of the interface device
        :param iface_source: source of the interface device
        :param kwargs: other k-w args that needed to create device
        :return: the newly created interface object
        """
        iface = Interface('network')
        iface.model = iface_model
        iface.source = eval(iface_source)

        if 'mac' in kwargs:
            iface.mac_address = kwargs['mac']
        else:
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac

        if 'address' in kwargs:
            iface.address = iface.new_iface_address(
                attrs=eval(kwargs['address']))

        logging.debug('iface: %s', iface)
        return iface
Пример #3
0
 def create_iface_xml(iface_mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     source = ast.literal_eval(iface_source)
     if source:
         iface.source = source
     iface.model = iface_model if iface_model else "virtio"
     iface.mac_address = iface_mac
     driver_dict = {}
     driver_host = {}
     driver_guest = {}
     if iface_driver:
         driver_dict = ast.literal_eval(iface_driver)
     if iface_driver_host:
         driver_host = ast.literal_eval(iface_driver_host)
     if iface_driver_guest:
         driver_guest = ast.literal_eval(iface_driver_guest)
     iface.driver = iface.new_driver(driver_attr=driver_dict,
                                     driver_host=driver_host,
                                     driver_guest=driver_guest)
     if test_target:
         iface.target = {"dev": target_dev}
     logging.debug("Create new interface xml: %s", iface)
     return iface
Пример #4
0
 def create_iface(iface_type, **kwargs):
     """
     Create a interface to be attached to vm
     """
     m_iface = Interface(iface_type)
     m_iface.mac_address = utils_net.generate_mac_address_simple()
     if 'base_if' in kwargs:
         m_iface.source = {'dev': kwargs['base_if'], 'mode': 'vepa'}
     if 'source_net' in kwargs:
         m_iface.source = {'network': kwargs['source_net']}
     if 'mtu' in kwargs:
         m_iface.mtu = {'size': kwargs['mtu']}
     if 'model_net' in kwargs:
         m_iface.model = kwargs['model_net']
     logging.debug(m_iface.get_xml())
     logging.debug(m_iface)
     return m_iface
Пример #5
0
 def create_network_interface(name):
     """
         Create network type interface xml.
     """
     new_iface = Interface('network')
     new_iface.source = {'network': name}
     new_iface.model = "virtio"
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     return new_iface
Пример #6
0
 def create_iface_xml():
     """
     Create interface xml file
     """
     iface = Interface("bridge")
     iface.source = eval("{'bridge':'virbr0'}")
     iface.model = "virtio"
     logging.debug("Create new interface xml: %s", iface)
     return iface
Пример #7
0
 def create_iface(iface_type, **kwargs):
     """
     Create a interface to be attached to vm
     """
     m_iface = Interface(iface_type)
     m_iface.mac_address = utils_net.generate_mac_address_simple()
     if 'base_if' in kwargs:
         m_iface.source = {'dev': kwargs['base_if'],
                           'mode': 'vepa'}
     if 'source_net' in kwargs:
         m_iface.source = {'network': kwargs['source_net']}
     if 'mtu' in kwargs:
         m_iface.mtu = {'size': kwargs['mtu']}
     if 'model_net' in kwargs:
         m_iface.model = kwargs['model_net']
     logging.debug(m_iface.get_xml())
     logging.debug(m_iface)
     return m_iface
Пример #8
0
 def create_network_interface(name):
     """
         Create network type interface xml.
     """
     new_iface = Interface('network')
     new_iface.source = {'network': name}
     new_iface.model = "virtio"
     new_iface.mac_address = utils_net.generate_mac_address_simple()
     return new_iface
Пример #9
0
 def create_iface_xml(mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     iface.source = iface_source
     iface.model = iface_model if iface_model else "virtio"
     if iface_target:
         iface.target = {'dev': iface_target}
     iface.mac_address = mac
     logging.debug("Create new interface xml: %s", iface)
     return iface
Пример #10
0
 def create_iface_xml(mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     iface.source = iface_source
     iface.model = iface_model if iface_model else "virtio"
     if iface_target:
         iface.target = {'dev': iface_target}
     iface.mac_address = mac
     logging.debug("Create new interface xml: %s", iface)
     return iface
Пример #11
0
        def create_iface_xml(mac):
            """
            Create interface xml file

            :param mac: The mac address of nic device
            """
            iface = Interface(type_name='network')
            iface.source = iface_source
            iface.model = iface_model
            iface.mac_address = mac
            logging.debug("Create new interface xml: %s", iface)
            return iface
Пример #12
0
 def prepare_iface_xml(iface_bus, iface_slot):
     """
     Create interface xml file
     """
     iface_xml = Interface(type_name='bridge')
     iface_xml.source = {'bridge': 'virbr0'}
     iface_xml.model = "virtio"
     addr_dict = {
         'bus': iface_bus,
         'slot': iface_slot,
         'domain': '0x0000',
         'function': '0x0'
     }
     iface_xml.address = iface_xml.new_iface_address(type_name='pci',
                                                     **{"attrs": addr_dict})
     return iface_xml
Пример #13
0
    def modify_iface_xml(br_name, nwfilter, vm_name):
        """
        Modify interface xml with the new bridge and the nwfilter

        :param br_name: bridge name
        :param nwfilter: nwfilter name
        :param vm_name: vm name
        """
        vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
        iface_xml = vmxml.get_devices('interface')[0]
        vmxml.del_device(iface_xml)
        iface_xml = Interface(type_name='bridge')
        iface_xml.source = {'bridge': br_name}
        iface_xml.model = 'virtio'
        iface_xml.filterref = iface_xml.new_filterref(name=nwfilter)
        logging.debug("new interface xml is: %s" % iface_xml)
        vmxml.add_device(iface_xml)
        vmxml.sync()
Пример #14
0
 def create_interface():
     """
         Call different function to create interface according to the type
     """
     new_iface = Interface('network')
     if vf_type == "vf":
         new_iface = create_hostdev_interface(vf_addr, managed, model)
     if vf_type == "vf_pool":
         netxml = create_hostdev_network()
         virsh.net_define(netxml.xml, ignore_status=True)
         if not inactive_pool:
             virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     if vf_type == "macvtap":
         new_iface = Interface('direct')
         new_iface.source = {"dev": vf_name, "mode": "passthrough"}
         new_iface.mac_address = utils_net.generate_mac_address_simple()
     if vf_type == "macvtap_network":
         netxml = create_macvtap_network()
         result = virsh.net_define(netxml.xml, ignore_status=True)
         virsh.net_start(netxml.name)
         new_iface = create_network_interface(netxml.name)
     return new_iface
Пример #15
0
 def generate_container_xml():
     """
     Generate container xml
     """
     vmxml = vm_xml.VMXML(dom_type)
     vmxml.vm_name = vm_name
     vmxml.max_mem = max_mem
     vmxml.current_mem = current_mem
     vmxml.vcpu = vcpu
     # Generate os
     vm_os = vm_xml.VMOSXML()
     vm_os.type = os_type
     vm_os.arch = os_arch
     vm_os.init = os_init
     vmxml.os = vm_os
     # Generate emulator
     emulator = Emulator()
     emulator.path = emulator_path
     # Generate console
     console = Console()
     filesystem = Filesystem()
     filesystem.accessmode = fs_accessmode
     filesystem.source = {'dir': install_root}
     filesystem.target = {'dir': fs_target}
     # Add emulator and console in devices
     devices = vm_xml.VMXMLDevices()
     devices.append(emulator)
     devices.append(console)
     devices.append(filesystem)
     # Add network device
     network = Interface(type_name=interface_type)
     network.mac_address = utils_net.generate_mac_address_simple()
     network.source = {interface_type: net_name}
     devices.append(network)
     vmxml.set_devices(devices)
     return vmxml
Пример #16
0
    def create_iface(iface_model, iface_source, **kwargs):
        """
        Create an interface to be attached to vm

        :param iface_model: model of the interface device
        :param iface_source: source of the interface device
        :param kwargs: other k-w args that needed to create device
        :return: the newly created interface object
        """
        iface = Interface('network')
        iface.model = iface_model
        iface.source = eval(iface_source)

        if 'mac' in kwargs:
            iface.mac_address = kwargs['mac']
        else:
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac

        if 'address' in kwargs:
            iface.address = iface.new_iface_address(attrs=eval(kwargs['address']))

        logging.debug('iface: %s', iface)
        return iface
Пример #17
0
 def generate_container_xml():
     """
     Generate container xml
     """
     vmxml = vm_xml.VMXML(dom_type)
     vmxml.vm_name = vm_name
     vmxml.max_mem = max_mem
     vmxml.current_mem = current_mem
     vmxml.vcpu = vcpu
     # Generate os
     vm_os = vm_xml.VMOSXML()
     vm_os.type = os_type
     vm_os.arch = os_arch
     vm_os.init = os_init
     vmxml.os = vm_os
     # Generate emulator
     emulator = Emulator()
     emulator.path = emulator_path
     # Generate console
     console = Console()
     filesystem = Filesystem()
     filesystem.accessmode = fs_accessmode
     filesystem.source = {'dir': install_root}
     filesystem.target = {'dir': fs_target}
     # Add emulator and console in devices
     devices = vm_xml.VMXMLDevices()
     devices.append(emulator)
     devices.append(console)
     devices.append(filesystem)
     # Add network device
     network = Interface(type_name=interface_type)
     network.mac_address = utils_net.generate_mac_address_simple()
     network.source = {interface_type: net_name}
     devices.append(network)
     vmxml.set_devices(devices)
     return vmxml
Пример #18
0
 def create_iface_xml(iface_mac):
     """
     Create interface xml file
     """
     iface = Interface(type_name=iface_type)
     source = ast.literal_eval(iface_source)
     if source:
         iface.source = source
     iface.model = iface_model if iface_model else "virtio"
     iface.mac_address = iface_mac
     driver_dict = {}
     driver_host = {}
     driver_guest = {}
     if iface_driver:
         driver_dict = ast.literal_eval(iface_driver)
     if iface_driver_host:
         driver_host = ast.literal_eval(iface_driver_host)
     if iface_driver_guest:
         driver_guest = ast.literal_eval(iface_driver_guest)
     iface.driver = iface.new_driver(driver_attr=driver_dict,
                                     driver_host=driver_host,
                                     driver_guest=driver_guest)
     logging.debug("Create new interface xml: %s", iface)
     return iface
Пример #19
0
def run(test, params, env):
    """
    cpu, memory, network, disk limit test:
    1) prepare the guest with given topology, memory and if any devices
    2) Start and login to the guest
    3) Check if the guest functional
    4) if given run some stress test

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    TODO: 1. Add multiple pci-bridge and pci devices respectively.
          2. Get no. VM interfaces at test start and use for validation.
    """

    failures = {
        "Failed to allocate KVM HPT of order":
        "Host does no have enough cma to boot, try increasing \
                kvm_cma_resv_ratio in host boot",
        "unable to map backing store for guest RAM":
        "Not enough memory in the host to boot the guest"
    }
    vm_name = params.get("main_vm")
    max_vcpu = current_vcpu = int(params.get("max_vcpu", 240))
    vm_cores = int(params.get("limit_vcpu_cores", 240))
    vm_threads = int(params.get("limit_vcpu_threads", 1))
    vm_sockets = int(params.get("limit_vcpu_sockets", 1))
    usermaxmem = params.get("usermaxmem", '')
    default_mem = int(params.get("default_mem", 8))
    maxmem = params.get("maxmem", "no") == "yes"
    swap_setup = params.get("swap_setup", "yes") == "yes"
    blk_partition = params.get("blk_part", '')
    graphic = params.get("graphics", "no") == "yes"
    vm = env.get_vm(vm_name)
    guestmemory = None
    max_network = params.get("max_network", "no") == "yes"
    max_disk = params.get("max_disk", "no") == "yes"
    num_network = int(params.get("num_network", 16))
    num_disk = int(params.get("num_disk", 16))
    drive_format = params.get("drive_format", "scsi")
    disk_format = params.get("disk_format", "qcow2")
    netdst = params.get("netdst", "virbr0")
    memunit = params.get("memunit", 'G')
    failed = False
    vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    org_xml = vmxml.copy()
    # Destroy the vm
    vm.destroy()
    try:
        # Setup swap
        if swap_setup:
            if not blk_partition:
                test.cancel("block partition is not given")
            # Check if valid partition or raise
            cmd = "blkid /dev/%s|awk -F" " '{print $2}'" % blk_partition
            output = process.system_output(cmd, shell=True)
            if "UUID" not in output:
                test.cancel("Not a valid partition given for swap creation")
            # Create swap partition
            cmd = "mkswap /dev/%s" % blk_partition
            process.system(cmd, shell=True)
            cmd = "swapon /dev/%s" % blk_partition
            process.system(cmd, shell=True)

        # Check for host memory and cpu levels and validate against
        # requested limits, allow to max of 10X for CPU and 2.5X for memory
        host_memory = int(memory.rounded_memtotal())
        if maxmem:
            if usermaxmem:
                guestmemory = usermaxmem
            else:
                # Normalize to GB
                guestmemory = int(2.5 * host_memory / (1024 * 1024))
        else:
            pass
        if not guestmemory:
            # assign default memory
            guestmemory = default_mem

        # Set the current and max memory params
        vmxml.current_mem_unit = memunit
        vmxml.max_mem_unit = memunit
        vmxml.current_mem = int(guestmemory)
        vmxml.max_mem = int(guestmemory)
        vmxml.sync()

        # Set vcpu and topology
        libvirt_xml.VMXML.set_vm_vcpus(vm_name, max_vcpu, current_vcpu,
                                       vm_sockets, vm_cores, vm_threads)
        vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)

        # Set vnc display as needed
        graphics = vmxml.get_device_class('graphics')()
        if graphic:
            if not vmxml.get_graphics_devices("vnc"):
                graphics.add_graphic(vm_name, graphic="vnc")
        else:
            if vmxml.get_graphics_devices("vnc"):
                graphics.del_graphic(vm_name)
        vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)

        network_str = None
        disk_str = None
        # Set network devices
        if max_network:
            network_str = "ip link|grep ^[1-9]|wc -l"
            for idx in range(num_network):
                network = Interface(type_name="bridge")
                network.mac_address = utils_net.generate_mac_address_simple()
                network.source = {"bridge": netdst}
                vmxml.add_device(network)

        # Set disk devices
        if max_disk:
            for idx in range(num_disk):
                disk_str = "lsblk|grep ^[s,v]|grep 1G|wc -l"
                disk = Disk()
                disk_path = os.path.join(data_dir.get_data_dir(), "images",
                                         "%s.qcow2" % idx)
                if "scsi" in drive_format:
                    drive_format = "scsi"
                    disk_target = "sd%s" % letters[(idx % 51) + 1]
                else:
                    drive_format = "virtio"
                    disk_target = "vd%s" % letters[(idx % 51) + 1]
                disk_source = libvirt.create_local_disk(
                    "file", disk_path, '1', "qcow2")
                disk.device = "disk"
                disk.source = disk.new_disk_source(
                    **{"attrs": {
                        'file': disk_source
                    }})
                disk.target = {"dev": disk_target, "bus": drive_format}
                disk.driver = {"name": "qemu", 'type': disk_format}
                vmxml.add_device(disk)
        vmxml.sync()

        # Start VM
        logging.debug("VM XML: \n%s", vmxml)
        try:
            vm.start()
        except virt_vm.VMStartError, detail:
            for msg in failures.items():
                if msg[0] in detail:
                    test.cancel("%s", msg[1])
            test.fail("%s" % detail)

        # Check the memory and vcpu inside guest
        memtotal = vm.get_totalmem_sys()
        cpucount = vm.get_cpu_count()
        session = vm.wait_for_login()
        if network_str:
            guestnetworks = int(session.cmd_output(network_str))
            logging.debug("guestnet: %d", guestnetworks)
            if (guestnetworks - 2) != num_network:
                failed = True
                logging.error(
                    "mismatch in guest network devices: \n"
                    "Expected: %d\nActual: %d", num_network, guestnetworks)
        if disk_str:
            guestdisks = int(session.cmd_output(disk_str))
            logging.debug("guestdisk: %d", guestdisks)
            if guestdisks != num_disk:
                failed = True
                logging.error(
                    "mismatch in guest disk devices: \n"
                    "Expected: %d\nActual: %s", num_disk, guestdisks)
        session.close()
        guestmem = utils_misc.normalize_data_size("%s G" % guestmemory)
        # TODO:512 MB threshold deviation value, need to normalize
        if int(float(guestmem) - memtotal) > 512:
            failed = True
            logging.error(
                "mismatch in guest memory: \nExpected: "
                "%s\nActual: %s", float(guestmem), memtotal)
        if cpucount != current_vcpu:
            failed = True
            logging.error(
                "mismatch in guest vcpu:\nExpected: %d\nActual: "
                "%d", current_vcpu, cpucount)
        if failed:
            test.fail("Consult previous failures")
Пример #20
0
    def modify_iface_xml():
        """
        Modify interface xml options
        Two methods to modify domain interfae:
        1. modify guest xml, define it
        2. attach one interface for running guest

        :return: 0 for successful negative case
                 test.fail is fail for positive/negative case
                 None for successful positive case
        """
        if hotplug_iface:
            iface = Interface(iface_type)
        else:
            vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
            xml_devices = vmxml.devices
            iface_index = xml_devices.index(
                xml_devices.by_device_tag("interface")[0])
            iface = xml_devices[iface_index]

        if iface_type == 'network':
            iface.type_name = iface_type
            source = {iface_type: net_name}
        elif iface_type == 'bridge' and bridge_name:
            iface.type_name = iface_type
            source = {iface_type: bridge_name}
        elif iface_type == 'direct':
            iface.type_name = iface_type
            source = {'dev': interface, 'mode': 'bridge'}

        if source:
            del iface.source
            iface.source = source
        iface_model = params.get("iface_model", "virtio")
        iface.model = iface_model
        iface.coalesce = {'max': coalesce_value}
        if network_type == "ovsbridge" and iface_type == "bridge":
            iface.virtualport_type = "openvswitch"

        if not hotplug_iface:
            vmxml.devices = xml_devices
            vmxml.xmltreefile.write()
            try:
                vmxml.sync()
            except xcepts.LibvirtXMLError as details:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
        else:
            if not vm.is_alive():
                vm.start()
                # Wait guest boot completely
                time.sleep(2)
            try:
                ret = virsh.attach_device(vm_name, iface.xml,
                                          ignore_status=False,
                                          debug=True)
            except process.CmdError as error:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
Пример #21
0
    def modify_iface_xml():
        """
        Modify interface xml options
        Two methods to modify domain interfae:
        1. modify guest xml, define it
        2. attach one interface for running guest

        :return: 0 for successful negative case
                 test.fail is fail for positive/negative case
                 None for successful positive case
        """
        if hotplug_iface:
            iface = Interface(iface_type)
        else:
            vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
            xml_devices = vmxml.devices
            iface_index = xml_devices.index(
                xml_devices.by_device_tag("interface")[0])
            iface = xml_devices[iface_index]

        if iface_type == 'network':
            iface.type_name = iface_type
            source = {iface_type: net_name}
        elif iface_type == 'bridge' and bridge_name:
            iface.type_name = iface_type
            source = {iface_type: bridge_name}
        elif iface_type == 'direct':
            iface.type_name = iface_type
            source = {'dev': interface, 'mode': 'bridge'}

        if source:
            del iface.source
            iface.source = source
        iface_model = params.get("iface_model", "virtio")
        iface.model = iface_model
        iface.coalesce = {'max': coalesce_value}
        if network_type == "ovsbridge" and iface_type == "bridge":
            iface.virtualport_type = "openvswitch"

        if not hotplug_iface:
            vmxml.devices = xml_devices
            vmxml.xmltreefile.write()
            try:
                vmxml.sync()
            except xcepts.LibvirtXMLError as details:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
        else:
            if not vm.is_alive():
                vm.start()
                # Wait guest boot completely
                time.sleep(2)
            try:
                ret = virsh.attach_device(vm_name,
                                          iface.xml,
                                          ignore_status=False,
                                          debug=True)
            except process.CmdError as error:
                if status_error:
                    # Expect error for negetive test
                    return 0
                else:
                    test.fail("Define guest: FAIL")
Пример #22
0
def run(test, params, env):
    """
    Test pci/pcie-to-pci bridge

    Hotplug interface to pci/pcie-to-pci bridge, then check xml and
    inside vm.
    Hotunplug interface, then check xml and inside vm
    """
    vm_name = params.get('main_vm')
    pci_model = params.get('pci_model', 'pci')
    hotplug = 'yes' == params.get('hotplug', 'no')

    pci_model_name = params.get('pci_model_name')
    pci_br_has_device = 'yes' == params.get('pci_br_has_device', 'no')
    sound_dev_model_type = params.get('sound_dev_model_type', '')
    sound_dev_address = params.get('sound_dev_address', '')
    iface_model = params.get('iface_model', '')
    iface_source = params.get('iface_source', '')

    vmxml = VMXML.new_from_inactive_dumpxml(vm_name)
    bkxml = vmxml.copy()
    vm = env.get_vm(vm_name)

    try:

        # Check if there is a pci/pcie-to-pci bridge, if so,
        # just use the existing pci/pcie-to-pci-bridge to test
        ori_pci_br = [
            dev for dev in vmxml.get_devices('controller')
            if dev.type == 'pci' and dev.model == pci_model
        ]

        # If there is not a pci/pcie-to-pci bridge to test,
        # create one and add to vm
        if not ori_pci_br:
            logging.info('No %s on vm, create one', pci_model)
            pci_bridge = Controller('pci')
            pci_bridge.model = pci_model
            pci_bridge.model_name = {'name': pci_model_name}

            vmxml.add_device(pci_bridge)
            vmxml.sync()
            logging.debug(virsh.dumpxml(vm_name))

        # Check if pci/pcie-to-pci bridge is successfully added
        vmxml = VMXML.new_from_inactive_dumpxml(vm_name)
        cur_pci_br = [
            dev for dev in vmxml.get_devices('controller')
            if dev.type == 'pci' and dev.model == pci_model
        ]
        if not cur_pci_br:
            test.error('Failed to add %s controller to vm xml' % pci_model)

        pci_br = cur_pci_br[0]
        logging.debug(pci_br)
        pci_br_index = pci_br.index

        # If test scenario requires another pci device on pci/pcie-to-pci
        # bridge before hotplug, add a sound device and make sure
        # the 'bus' is same with pci bridge index
        if pci_br_has_device:
            sound_dev = Sound()
            sound_dev.model_type = sound_dev_model_type
            sound_dev.address = eval(sound_dev_address % pci_br_index)
            logging.debug(sound_dev.address)
            vmxml.add_device(sound_dev)
            vmxml.sync()

        # Test hotplug scenario
        if hotplug:
            vm.start()
            vm.wait_for_login().close()

            # Create interface to be hotplugged
            logging.info('Create interface to be hotplugged')
            iface = Interface('network')
            iface.model = iface_model
            iface.source = eval(iface_source)
            mac = utils_net.generate_mac_address_simple()
            iface.mac_address = mac
            logging.debug(iface)

            result = virsh.attach_device(vm_name, iface.xml, debug=True)
            libvirt.check_exit_status(result)

            xml_after_attach = VMXML.new_from_dumpxml(vm_name)
            logging.debug(virsh.dumpxml(vm_name))

            # Check if the iface with given mac address is successfully attached
            iface_list = [
                iface for iface in xml_after_attach.get_devices('interface')
                if iface.mac_address == mac
            ]

            logging.debug('iface list after attach: %s', iface_list)
            if not iface_list:
                test.error('Failed to attach interface %s' % iface)

            # Check inside vm
            def check_inside_vm(session, expect=True):
                ip_output = session.cmd('ip a')
                logging.debug(ip_output)

                return expect if mac in ip_output else not expect

            session = vm.wait_for_serial_login()
            if not utils_misc.wait_for(lambda: check_inside_vm(session, True),
                                       timeout=60,
                                       step=5):
                test.fail('Check interface inside vm failed,'
                          'interface not successfully attached:'
                          'not found mac address %s' % mac)
            session.close()

            # Test hotunplug
            result = virsh.detach_device(vm_name, iface.xml, debug=True)
            libvirt.check_exit_status(result)

            logging.debug(virsh.dumpxml(vm_name))

            # Check if the iface with given mac address has been
            # successfully detached
            xml_after_detach = VMXML.new_from_dumpxml(vm_name)
            iface_list_after_detach = [
                iface for iface in xml_after_detach.get_devices('interface')
                if iface.mac_address == mac
            ]

            logging.debug('iface list after detach: %s',
                          iface_list_after_detach)
            if iface_list_after_detach:
                test.fail('Failed to detach device: %s', iface)

            # Check again inside vm
            session = vm.wait_for_serial_login()
            if not utils_misc.wait_for(lambda: check_inside_vm(session, False),
                                       timeout=60,
                                       step=5):
                test.fail('Check interface inside vm failed,'
                          'interface not successfully detached:'
                          'found mac address %s' % mac)
            session.close()

    finally:
        bkxml.sync()
Пример #23
0
def run(test, params, env):
    """
    cpu, memory, network, disk limit test:
    1) prepare the guest with given topology, memory and if any devices
    2) Start and login to the guest
    3) Check if the guest functional
    4) if given run some stress test

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    TODO: 1. Add multiple pci-bridge and pci devices respectively.
          2. Get no. VM interfaces at test start and use for validation.
    """

    failures = {"Failed to allocate KVM HPT of order":
                "Host does no have enough cma to boot, try increasing \
                kvm_cma_resv_ratio in host boot",
                "unable to map backing store for guest RAM":
                "Not enough memory in the host to boot the guest"}
    vm_name = params.get("main_vm")
    max_vcpu = current_vcpu = int(params.get("max_vcpu", 240))
    vm_cores = int(params.get("limit_vcpu_cores", 240))
    vm_threads = int(params.get("limit_vcpu_threads", 1))
    vm_sockets = int(params.get("limit_vcpu_sockets", 1))
    usermaxmem = params.get("usermaxmem", '')
    default_mem = int(params.get("default_mem", 8))
    maxmem = params.get("maxmem", "no") == "yes"
    swap_setup = params.get("swap_setup", "yes") == "yes"
    blk_partition = params.get("blk_part", '')
    graphic = params.get("graphics", "no") == "yes"
    vm = env.get_vm(vm_name)
    guestmemory = None
    max_network = params.get("max_network", "no") == "yes"
    max_disk = params.get("max_disk", "no") == "yes"
    num_network = int(params.get("num_network", 16))
    num_disk = int(params.get("num_disk", 16))
    drive_format = params.get("drive_format", "scsi")
    disk_format = params.get("disk_format", "qcow2")
    netdst = params.get("netdst", "virbr0")
    memunit = params.get("memunit", 'G')
    failed = False
    vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)
    org_xml = vmxml.copy()
    # Destroy the vm
    vm.destroy()
    try:
        # Setup swap
        if swap_setup:
            if not blk_partition:
                test.cancel("block partition is not given")
            # Check if valid partition or raise
            cmd = "blkid /dev/%s|awk -F" " '{print $2}'" % blk_partition
            output = process.system_output(cmd, shell=True)
            if "UUID" not in output:
                test.cancel("Not a valid partition given for swap creation")
            # Create swap partition
            cmd = "mkswap /dev/%s" % blk_partition
            process.system(cmd, shell=True)
            cmd = "swapon /dev/%s" % blk_partition
            process.system(cmd, shell=True)

        # Check for host memory and cpu levels and validate against
        # requested limits, allow to max of 10X for CPU and 2.5X for memory
        host_memory = int(memory.rounded_memtotal())
        if maxmem:
            if usermaxmem:
                guestmemory = usermaxmem
            else:
                # Normalize to GB
                guestmemory = int(2.5 * host_memory/(1024 * 1024))
        else:
            pass
        if not guestmemory:
            # assign default memory
            guestmemory = default_mem

        # Set the current and max memory params
        vmxml.current_mem_unit = memunit
        vmxml.max_mem_unit = memunit
        vmxml.current_mem = int(guestmemory)
        vmxml.max_mem = int(guestmemory)
        vmxml.sync()

        # Set vcpu and topology
        libvirt_xml.VMXML.set_vm_vcpus(vm_name, max_vcpu, current_vcpu,
                                       vm_sockets, vm_cores, vm_threads)
        vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)

        # Set vnc display as needed
        graphics = vmxml.get_device_class('graphics')()
        if graphic:
            if not vmxml.get_graphics_devices("vnc"):
                graphics.add_graphic(vm_name, graphic="vnc")
        else:
            if vmxml.get_graphics_devices("vnc"):
                graphics.del_graphic(vm_name)
        vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name)

        network_str = None
        disk_str = None
        # Set network devices
        if max_network:
            network_str = "ip link|grep ^[1-9]|wc -l"
            for idx in range(num_network):
                network = Interface(type_name="bridge")
                network.mac_address = utils_net.generate_mac_address_simple()
                network.source = {"bridge": netdst}
                vmxml.add_device(network)

        # Set disk devices
        if max_disk:
            for idx in range(num_disk):
                disk_str = "lsblk|grep ^[s,v]|grep 1G|wc -l"
                disk = Disk()
                disk_path = os.path.join(data_dir.get_data_dir(), "images", "%s.qcow2" % idx)
                if "scsi" in drive_format:
                    drive_format = "scsi"
                    disk_target = "sd%s" % letters[(idx % 51)+1]
                else:
                    drive_format = "virtio"
                    disk_target = "vd%s" % letters[(idx % 51)+1]
                disk_source = libvirt.create_local_disk("file", disk_path, '1', "qcow2")
                disk.device = "disk"
                disk.source = disk.new_disk_source(**{"attrs": {'file': disk_source}})
                disk.target = {"dev": disk_target, "bus": drive_format}
                disk.driver = {"name": "qemu", 'type': disk_format}
                vmxml.add_device(disk)
        vmxml.sync()

        # Start VM
        logging.debug("VM XML: \n%s", vmxml)
        try:
            vm.start()
        except virt_vm.VMStartError, detail:
            for msg in failures.items():
                if msg[0] in detail:
                    test.cancel("%s", msg[1])
            test.fail("%s" % detail)

        # Check the memory and vcpu inside guest
        memtotal = vm.get_totalmem_sys()
        cpucount = vm.get_cpu_count()
        session = vm.wait_for_login()
        if network_str:
            guestnetworks = int(session.cmd_output(network_str))
            logging.debug("guestnet: %d", guestnetworks)
            if (guestnetworks - 2) != num_network:
                failed = True
                logging.error("mismatch in guest network devices: \n"
                              "Expected: %d\nActual: %d", num_network,
                              guestnetworks)
        if disk_str:
            guestdisks = int(session.cmd_output(disk_str))
            logging.debug("guestdisk: %d", guestdisks)
            if guestdisks != num_disk:
                failed = True
                logging.error("mismatch in guest disk devices: \n"
                              "Expected: %d\nActual: %s", num_disk, guestdisks)
        session.close()
        guestmem = utils_misc.normalize_data_size("%s G" % guestmemory)
        # TODO:512 MB threshold deviation value, need to normalize
        if int(float(guestmem) - memtotal) > 512:
            failed = True
            logging.error("mismatch in guest memory: \nExpected: "
                          "%s\nActual: %s", float(guestmem), memtotal)
        if cpucount != current_vcpu:
            failed = True
            logging.error("mismatch in guest vcpu:\nExpected: %d\nActual: "
                          "%d", current_vcpu, cpucount)
        if failed:
            test.fail("Consult previous failures")