def test_produce_pci_devices(current_actor_libraries):
    output = []

    def fake_producer(*args):
        output.extend(args)

    devices = [
        PCIDevice(slot='00:00.0',
                  dev_cls='Host bridge',
                  vendor='Intel Corporation',
                  name='440FX - 82441FX PMC [Natoma]',
                  subsystem_vendor='Red Hat, Inc.',
                  subsystem_name='Qemu virtual machine',
                  rev='02'),
        PCIDevice(slot='00:01.0',
                  dev_cls='ISA bridge',
                  vendor='Intel Corporation',
                  name='82371SB PIIX3 ISA [Natoma/Triton II]',
                  subsystem_vendor='Red Hat, Inc.',
                  subsystem_name='Qemu virtual machine'),
        PCIDevice(slot='00:01.1',
                  dev_cls='IDE interface',
                  vendor='Intel Corporation',
                  name='82371SB PIIX3 IDE [Natoma/Triton II]',
                  subsystem_vendor='Red Hat, Inc.',
                  subsystem_name='Qemu virtual machine',
                  progif='80'),
    ]

    produce_pci_devices(fake_producer, devices)
    assert len(output) == 1
    assert len(output[0].devices) == 3
def parse_pci_devices(devices):
    ''' Parse lspci output and return a list of PCI devices '''
    for d in devices:
        raw = shlex.split(d)

        params = [r for r in raw if not r.startswith('-')]
        optionals = [r for r in raw if r.startswith('-')]

        rev = ''
        progif = ''
        for o in optionals:
            if o.startswith('-r'):
                rev = o.lstrip('-r')

            if o.startswith('-p'):
                progif = o.lstrip('-p')

        yield PCIDevice(
            slot=get_from_list(params, 0),
            dev_cls=get_from_list(params, 1),
            vendor=get_from_list(params, 2),
            name=get_from_list(params, 3),
            subsystem_vendor=get_from_list(params, 4),
            subsystem_name=get_from_list(params, 5),
            rev=rev,
            progif=progif
        )
def test_unsupported_lsi_scsi(current_actor_context):
    devices = [{
        'slot': '02:01.0',
        'dev_cls': 'SCSI storage controller',
        'vendor': 'LSI Logic / Symbios Logic',
        'name': '53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI',
        'subsystem_name': 'SCSI Controller',
        'subsystem_vendor': 'LSI Logix / Symbios Logic'
    }, {
        'slot': '03:00.0',
        'dev_cls': 'Serial Attached SCSI Controller',
        'vendor': 'VMWare',
        'name': 'PVSCSI SCSI Controller',
        'subsystem_name': 'SCSI Controller',
        'subsystem_vendor': 'VMWare'
    }, {
        'slot': '13:00.0',
        'dev_cls': 'Serial Attached SCSI Controller',
        'vendor': 'LSI Logic / Symbios Logic',
        'name': 'SAS1068 PCI-X Fusion-MPT SAS',
        'subsystem_name': 'SCSI Controller',
        'subsystem_vendor': 'VMWare'
    }]

    error_summary = 'LSI Logic SCSI Controller is not supported'

    current_actor_context.feed(
        PCIDevices(devices=map(lambda x: PCIDevice(**x), devices)))
    current_actor_context.run()
    assert current_actor_context.consume(Inhibitor)
def test_no_unsupported_devices(current_actor_context):
    devices = [{
        'slot': '03:00.0',
        'dev_cls': 'Serial Attached SCSI Controller',
        'vendor': 'VMWare',
        'name': 'PVSCSI SCSI Controller',
        'subsystem_name': 'SCSI Controller',
        'subsystem_vendor': 'VMWare'
    }]

    current_actor_context.feed(
        PCIDevices(devices=map(lambda x: PCIDevice(**x), devices)))
    current_actor_context.run()
    assert not current_actor_context.consume(Inhibitor)
def parse_pci_device(textual_block, numeric_block):
    """ Parse one block from lspci output describing one PCI device """
    device = {
        'Slot': '',
        'Class': '',
        'Vendor': '',
        'Device': '',
        'SVendor': '',
        'SDevice': '',
        'PhySlot': '',
        'Rev': '',
        'ProgIf': '',
        'Driver': '',
        'Module': [],
        'NUMANode': ''
    }
    for line in textual_block.splitlines():
        key, value = line.split(':\t')

        if key in device:
            if isinstance(device[key], list):
                device[key].append(value)
            else:
                if device[key]:
                    api.current_logger().debug(
                        'Unexpected duplicate key - {k}: {v} (current value: {vcurr}), ignoring'
                        .format(k=key, v=value, vcurr=device[key]))
                else:
                    device[key] = value
        else:
            api.current_logger().debug(
                'Unrecognized key - {k}: {v}, ignoring'.format(k=key, v=value))

    return PCIDevice(slot=device['Slot'],
                     dev_cls=device['Class'],
                     vendor=device['Vendor'],
                     name=device['Device'],
                     subsystem_vendor=device['SVendor'],
                     subsystem_name=device['SDevice'],
                     physical_slot=device['PhySlot'],
                     rev=device['Rev'],
                     progif=device['ProgIf'],
                     driver=device['Driver'],
                     modules=device['Module'],
                     numa_node=device['NUMANode'],
                     pci_id=":".join(PCI_ID_REG.findall(numeric_block)))
import pytest

from leapp.models import PCIDevice, PCIDevices, KernelModuleParameter
from leapp.reporting import Report
from leapp.snactor.fixture import current_actor_context


devices_ok = [
    PCIDevice(slot="00:00.0", dev_cls="foo", vendor="bar", name="foobar", driver="i915"),
    PCIDevice(slot="00:01.0", dev_cls="foo", vendor="bar", name="foobar", driver="serial"),
    PCIDevice(slot="00:02.0", dev_cls="foo", vendor="bar", name="foobar", driver="pcieport"),
    PCIDevice(slot="00:03.0", dev_cls="foo", vendor="bar", name="foobar", driver="nvme")
]
devices_driverless = [
    PCIDevice(slot="00:04.0", dev_cls="foo", vendor="bar", name="foobar", driver=""),
    PCIDevice(slot="00:05.0", dev_cls="foo", vendor="bar", name="foobar", driver="")
]
devices_removed = [
    PCIDevice(slot="00:06.0", dev_cls="foo", vendor="bar", name="foobar", driver="floppy"),
    PCIDevice(slot="00:07.0", dev_cls="foo", vendor="bar", name="foobar", driver="initio"),
    PCIDevice(slot="00:08.0", dev_cls="foo", vendor="bar", name="foobar", driver="pata_acpi"),
    PCIDevice(slot="00:09.0", dev_cls="foo", vendor="bar", name="foobar", driver="iwl4965")
]


@pytest.mark.parametrize("devices,expected", [
    ([], True),
    (devices_ok, True),
    (devices_driverless, True),
    (devices_ok + devices_driverless, True),
    (devices_removed, False),
    PCIDevices,
    RestrictedPCIDevice,
    RestrictedPCIDevices,
    fields,
)
from leapp.topics.systeminfo import SystemInfoTopic

logger = logging.getLogger(__name__)

pci_devices = PCIDevices(devices=[
    PCIDevice(
        slot="00:00.0",
        driver="",
        dev_cls="Host bridge",
        vendor="Intel Corporation",
        name="440FX - 82441FX PMC [Natoma]",
        subsystem_vendor="Red Hat, Inc.",
        subsystem_name="Qemu virtual machine",
        pci_id="",
        rev="02",
    ),
    PCIDevice(
        slot="00:01.0",
        dev_cls="ISA bridge",
        vendor="Intel Corporation",
        name="82371SB PIIX3 ISA [Natoma/Triton II]",
        subsystem_vendor="Red Hat, Inc.",
        pci_id="15b560:0739",
        subsystem_name="Qemu virtual machine",
    ),
    PCIDevice(