[Kimchi-devel] [PATCH] [Kimchi 2/2] Create test cases for GPU attachment

Jose Ricardo Ziviani joserz at linux.vnet.ibm.com
Fri Feb 26 18:28:45 UTC 2016


Signed-off-by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
---
 mockmodel.py            | 160 +++++++++++++++++++++++++++++++++++++++++++++++-
 tests/test_mockmodel.py |  33 ++++++++++
 2 files changed, 192 insertions(+), 1 deletion(-)

diff --git a/mockmodel.py b/mockmodel.py
index d010df7..034e6f6 100644
--- a/mockmodel.py
+++ b/mockmodel.py
@@ -35,7 +35,9 @@ from wok.plugins.kimchi import imageinfo
 from wok.plugins.kimchi import osinfo
 from wok.plugins.kimchi.model import cpuinfo
 from wok.plugins.kimchi.model import vmifaces
+from wok.plugins.kimchi.model.groups import PAMGroupsModel
 from wok.plugins.kimchi.model.host import DeviceModel
+from wok.plugins.kimchi.model.host import DevicesModel
 from wok.plugins.kimchi.model.libvirtstoragepool import IscsiPoolDef
 from wok.plugins.kimchi.model.libvirtstoragepool import NetfsPoolDef
 from wok.plugins.kimchi.model.libvirtstoragepool import StoragePoolDef
@@ -47,7 +49,7 @@ from wok.plugins.kimchi.model.storagevolumes import StorageVolumesModel
 from wok.plugins.kimchi.model import storagevolumes
 from wok.plugins.kimchi.model.templates import LibvirtVMTemplate
 from wok.plugins.kimchi.model.users import PAMUsersModel
-from wok.plugins.kimchi.model.groups import PAMGroupsModel
+from wok.plugins.kimchi.model.vmhostdevs import VMHostDevsModel
 from wok.plugins.kimchi.utils import pool_name_from_uri
 from wok.plugins.kimchi.vmtemplate import VMTemplate
 
@@ -118,6 +120,10 @@ class MockModel(Model):
                 setattr(self, m, mock_method)
 
         DeviceModel.lookup = self._mock_device_lookup
+        DeviceModel.get_iommu_groups = self._mock_device_get_iommu_groups
+        DeviceModel.is_device_3D_controller = \
+            self._mock_device_is_device_3D_controller
+        DevicesModel.get_list = self._mock_devices_get_list
         StoragePoolsModel._check_lvm = self._check_lvm
         StoragePoolModel._update_lvm_disks = self._update_lvm_disks
         StorageVolumesModel.get_list = self._mock_storagevolumes_get_list
@@ -125,6 +131,7 @@ class MockModel(Model):
         LibvirtVMTemplate._get_volume_path = self._get_volume_path
         VMTemplate.get_iso_info = self._probe_image
         imageinfo.probe_image = self._probe_image
+        VMHostDevsModel._get_pci_device_xml = self._get_pci_device_xml
 
     def reset(self):
         MockModel._mock_vms = defaultdict(list)
@@ -326,6 +333,16 @@ class MockModel(Model):
         return [dev['name'] for dev in self._mock_devices.devices.values()
                 if dev['device_type'] == _cap]
 
+    def _mock_device_get_iommu_groups(self):
+        return [dev['iommuGroup'] for dev in
+                self._mock_devices.devices.values()
+                if 'iommuGroup' in dev]
+
+    def _mock_device_is_device_3D_controller(self, info):
+        if 'driver' in info and 'name' in info['driver']:
+            return info['driver']['name'] == 'nvidia'
+        return False
+
     def _mock_device_lookup(self, dev_name):
         return self._mock_devices.devices[dev_name]
 
@@ -405,6 +422,43 @@ class MockModel(Model):
             if sn.name == name:
                 sn.current = True
 
+    def _attach_device(self, vm_name, xmlstr):
+        MockModel._mock_vms[vm_name].append(xmlstr)
+
+    def _get_pci_device_xml(self, dev_info, slot, is_multifunction):
+        if 'detach_driver' not in dev_info:
+            dev_info['detach_driver'] = 'kvm'
+
+        source = E.source(E.address(domain=str(dev_info['domain']),
+                                    bus=str(dev_info['bus']),
+                                    slot=str(dev_info['slot']),
+                                    function=str(dev_info['function'])))
+        driver = E.driver(name=dev_info['detach_driver'])
+
+        if is_multifunction:
+            multi = E.address(type='pci',
+                              domain='0',
+                              bus='0',
+                              slot=str(slot),
+                              function=str(dev_info['function']))
+
+            if dev_info['function'] == 0:
+                multi = E.address(type='pci',
+                                  domain='0',
+                                  bus='0',
+                                  slot=str(slot),
+                                  function=str(dev_info['function']),
+                                  multifunction='on')
+
+            host_dev = E.hostdev(source, driver, multi,
+                                 mode='subsystem', type='pci', managed='yes')
+
+        else:
+            host_dev = E.hostdev(source, driver,
+                                 mode='subsystem', type='pci', managed='yes')
+
+        return ET.tostring(host_dev)
+
 
 class MockStorageVolumes(object):
     def __init__(self):
@@ -481,6 +535,7 @@ class MockDevices(object):
                                      'Centrino Advanced-N 6205 [Taylor Peak]',
                                      'id': '0x0085'},
                                  'slot': 0,
+                                 'vga3d': False,
                                  'vendor': {'description': 'Intel Corporation',
                                             'id': '0x8086'}},
             'pci_0000_0d_00_0': {'bus': 13,
@@ -497,8 +552,111 @@ class MockDevices(object):
                                              'PCIe SDXC/MMC Host Controller',
                                              'id': '0xe823'},
                                  'slot': 0,
+                                 'vga3d': False,
                                  'vendor': {'description': 'Ricoh Co Ltd',
                                             'id': '0x1180'}},
+            'pci_0000_09_01_0': {'bus': 9,
+                                 'device_type': 'pci',
+                                 'domain': 0,
+                                 'driver': {'name': 'tg3'},
+                                 'function': 0,
+                                 'iommuGroup': 8,
+                                 'name': 'pci_0000_09_01_0',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0000:09:01.0',
+                                 'product': {'description':
+                                             'NetXtreme BCM5719',
+                                             'id': '0x1657'},
+                                 'slot': 1,
+                                 'vga3d': False,
+                                 'vendor': {'description': 'Broadcom Corp',
+                                            'id': '0x14e4'}},
+            'pci_0000_09_01_1': {'bus': 9,
+                                 'device_type': 'pci',
+                                 'domain': 0,
+                                 'driver': {'name': 'tg3'},
+                                 'function': 1,
+                                 'iommuGroup': 8,
+                                 'name': 'pci_0000_09_01_1',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0000:09:01.1',
+                                 'product': {'description':
+                                             'NetXtreme BCM5719',
+                                             'id': '0x1657'},
+                                 'slot': 1,
+                                 'vga3d': False,
+                                 'vendor': {'description': 'Broadcom Corp',
+                                            'id': '0x14e4'}},
+            'pci_0000_09_01_2': {'bus': 9,
+                                 'device_type': 'pci',
+                                 'domain': 0,
+                                 'driver': {'name': 'tg3'},
+                                 'function': 2,
+                                 'iommuGroup': 8,
+                                 'name': 'pci_0000_09_01_2',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0000:09:01.2',
+                                 'product': {'description':
+                                             'NetXtreme BCM5719',
+                                             'id': '0x1657'},
+                                 'slot': 1,
+                                 'vga3d': False,
+                                 'vendor': {'description': 'Broadcom Corp',
+                                            'id': '0x14e4'}},
+            'pci_0000_1a_00_0': {'bus': 26,
+                                 'device_type': 'pci',
+                                 'domain': 0,
+                                 'driver': {'name': 'nvidia'},
+                                 'function': 0,
+                                 'iommuGroup': 9,
+                                 'name': 'pci_0000_1a_00_0',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0000:1a:00.0',
+                                 'product': {'description':
+                                             'GK210GL [Tesla K80]',
+                                             'id': '0x0302'},
+                                 'slot': 0,
+                                 'vga3d': True,
+                                 'vendor': {'description': 'NVIDIA Corp',
+                                            'id': '0x10de'}},
+            'pci_0001_1b_00_0': {'bus': 27,
+                                 'device_type': 'pci',
+                                 'domain': 1,
+                                 'driver': {'name': 'nvidia'},
+                                 'function': 0,
+                                 'iommuGroup': 7,
+                                 'name': 'pci_0001_1b_00_0',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0001:1b:00.0',
+                                 'product': {'description':
+                                             'GK210GL [Tesla K80]',
+                                             'id': '0x0302'},
+                                 'slot': 0,
+                                 'vga3d': True,
+                                 'vendor': {'description': 'NVIDIA Corp',
+                                            'id': '0x10de'}},
+            'pci_0000_1c_00_0': {'bus': 28,
+                                 'device_type': 'pci',
+                                 'domain': 0,
+                                 'driver': {'name': 'nvidia'},
+                                 'function': 0,
+                                 'iommuGroup': 7,
+                                 'name': 'pci_0000_1c_00_0',
+                                 'parent': 'computer',
+                                 'path':
+                                 '/sys/devices/pci0000:00/0000:0d:00.0',
+                                 'product': {'description':
+                                             'GK210GL [Tesla K80]',
+                                             'id': '0x0302'},
+                                 'slot': 0,
+                                 'vga3d': True,
+                                 'vendor': {'description': 'NVIDIA Corp',
+                                            'id': '0x10de'}},
             'scsi_host0': {'adapter': {'fabric_wwn': '37df6c1efa1b4388',
                                        'type': 'fc_host',
                                        'wwnn': 'efb6563f06434a98',
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index 5c9fad4..0668191 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -26,6 +26,7 @@ import unittest
 from tests.utils import get_free_port, patch_auth, request, run_server
 from tests.utils import wait_task
 
+from wok.exception import InvalidOperation
 from wok.plugins.kimchi import mockmodel
 from wok.plugins.kimchi.osinfo import get_template_default
 
@@ -112,6 +113,38 @@ class MockModelTests(unittest.TestCase):
         vms.append(u'test')
         self.assertEqual(model.vms_get_list(), sorted(vms))
 
+    def test_memory_window_changes(self):
+        model.templates_create({'name': u'test',
+                                'cdrom': fake_iso})
+        task = model.vms_create({'name': u'test-vm',
+                                 'template': '/plugins/kimchi/templates/test'})
+        wait_task(model.task_lookup, task['id'])
+
+        info = model.device_lookup('pci_0000_1a_00_0')
+        model.vmhostdevs_update_mmio_guest(u'test-vm', True)
+        model._attach_device(u'test-vm',
+                             model._get_pci_device_xml(info, 0, False))
+
+    def test_hotplug_3D_card(self):
+        model.templates_create({'name': u'test',
+                                'cdrom': fake_iso})
+        task = model.vms_create({'name': u'test-vm',
+                                 'template': '/plugins/kimchi/templates/test'})
+        wait_task(model.task_lookup, task['id'])
+        model.vm_start(u'test-vm')
+
+        # attach the 3D cards found to a running guest
+        all_devices = model.devices_get_list()
+        for device in all_devices:
+            device_info = model.device_lookup(device)
+            if model.device_is_device_3D_controller(device_info):
+                try:
+                    model.vmhostdevs_create(u'test-vm', {'name': device})
+
+                # expect the error: KCHVMHDEV0006E
+                except InvalidOperation as e:
+                    self.assertEqual(e.message[:14], u'KCHVMHDEV0006E')
+
     def test_vm_info(self):
         model.templates_create({'name': u'test',
                                 'cdrom': fake_iso})
-- 
1.9.1




More information about the Kimchi-devel mailing list