"Fossies" - the Fresh Open Source Software Archive

Member "nova-22.0.1/nova/tests/unit/virt/libvirt/test_designer.py" (19 Nov 2020, 12634 Bytes) of package /linux/misc/openstack/nova-22.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_designer.py": 22.0.0_vs_22.0.1.

    1 # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 import mock
   16 
   17 from nova.pci import utils as pci_utils
   18 from nova import test
   19 from nova.tests.unit import matchers
   20 from nova.tests.unit.virt.libvirt import fake_libvirt_data
   21 from nova.virt.libvirt import config
   22 from nova.virt.libvirt import designer
   23 
   24 
   25 class DesignerTestCase(test.NoDBTestCase):
   26 
   27     def test_set_vif_bandwidth_config_no_extra_specs(self):
   28         # Test whether test_set_vif_bandwidth_config_no_extra_specs fails when
   29         # its second parameter has no 'extra_specs' field.
   30 
   31         try:
   32             # The conf will never be user be used, so we can use 'None'.
   33             # An empty dictionary is fine: all that matters it that there is no
   34             # 'extra_specs' field.
   35             designer.set_vif_bandwidth_config(None, {})
   36         except KeyError as e:
   37             self.fail('KeyError: %s' % e)
   38 
   39     def test_set_vif_guest_frontend_config(self):
   40         conf = config.LibvirtConfigGuestInterface()
   41         designer.set_vif_guest_frontend_config(conf, 'fake-mac',
   42                                                'fake-model', 'fake-driver',
   43                                                'fake-queues', None)
   44         self.assertEqual('fake-mac', conf.mac_addr)
   45         self.assertEqual('fake-model', conf.model)
   46         self.assertEqual('fake-driver', conf.driver_name)
   47         self.assertEqual('fake-queues', conf.vhost_queues)
   48         self.assertIsNone(conf.vhost_rx_queue_size)
   49 
   50     def test_set_vif_guest_frontend_config_rx_queue_size(self):
   51         conf = config.LibvirtConfigGuestInterface()
   52         designer.set_vif_guest_frontend_config(conf, 'fake-mac',
   53                                                'fake-model', 'fake-driver',
   54                                                'fake-queues', 1024)
   55         self.assertEqual('fake-mac', conf.mac_addr)
   56         self.assertEqual('fake-model', conf.model)
   57         self.assertEqual('fake-driver', conf.driver_name)
   58         self.assertEqual('fake-queues', conf.vhost_queues)
   59         self.assertEqual(1024, conf.vhost_rx_queue_size)
   60 
   61     def test_set_vif_host_backend_ethernet_config_libvirt_1_3_3(self):
   62         conf = config.LibvirtConfigGuestInterface()
   63         designer.set_vif_host_backend_ethernet_config(conf, 'fake-tap')
   64         self.assertEqual('ethernet', conf.net_type)
   65         self.assertEqual('fake-tap', conf.target_dev)
   66         self.assertIsNone(conf.script)
   67 
   68     def test_set_vif_host_backend_802qbg_config(self):
   69         conf = config.LibvirtConfigGuestInterface()
   70         designer.set_vif_host_backend_802qbg_config(conf, 'fake-devname',
   71                                                     'fake-managerid',
   72                                                     'fake-typeid',
   73                                                     'fake-typeidversion',
   74                                                     'fake-instanceid',
   75                                                     'fake-tap')
   76         self.assertEqual('direct', conf.net_type)
   77         self.assertEqual('fake-devname', conf.source_dev)
   78         self.assertEqual('vepa', conf.source_mode)
   79         self.assertEqual('802.1Qbg', conf.vporttype)
   80         expected = [{'key': 'managerid', 'value': 'fake-managerid'},
   81                     {'key': 'typeid', 'value': 'fake-typeid'},
   82                     {'key': 'typeidversion', 'value': 'fake-typeidversion'},
   83                     {'key': 'instanceid', 'value': 'fake-instanceid'}]
   84         self.assertThat(expected, matchers.DictListMatches(conf.vportparams))
   85         self.assertEqual('fake-tap', conf.target_dev)
   86 
   87     @mock.patch.object(pci_utils, 'get_ifname_by_pci_address',
   88                        return_value='fake-devname')
   89     def test_set_vif_host_backend_802qbh_config_direct(self,
   90                                                        mock_pci):
   91         conf = config.LibvirtConfigGuestInterface()
   92         designer.set_vif_host_backend_802qbh_config(conf, 'direct',
   93                                                     'fake-pci-dev',
   94                                                     'fake-profileid',
   95                                                     'fake-tap')
   96         self.assertEqual('direct', conf.net_type)
   97         self.assertEqual('fake-devname', conf.source_dev)
   98         self.assertEqual('passthrough', conf.source_mode)
   99         self.assertEqual('vhost', conf.driver_name)
  100         mock_pci.assert_called_with('fake-pci-dev')
  101         self.assertEqual('802.1Qbh', conf.vporttype)
  102         self.assertEqual('fake-tap', conf.target_dev)
  103 
  104     def test_set_vif_host_backend_802qbh_config_hostdev(self):
  105         conf = config.LibvirtConfigGuestInterface()
  106         designer.set_vif_host_backend_802qbh_config(conf, 'hostdev',
  107                                                     'fake-devname',
  108                                                     'fake-profileid',
  109                                                     'fake-tap')
  110         self.assertEqual('hostdev', conf.net_type)
  111         self.assertEqual('fake-devname', conf.source_dev)
  112         self.assertIsNone(conf.model)
  113         self.assertEqual('802.1Qbh', conf.vporttype)
  114         self.assertEqual('fake-tap', conf.target_dev)
  115 
  116     @mock.patch.object(pci_utils, 'get_ifname_by_pci_address',
  117                        return_value='fake-devname')
  118     def test_set_vif_host_backend_hw_veb_direct(self,
  119                                                 mock_pci):
  120         conf = config.LibvirtConfigGuestInterface()
  121         designer.set_vif_host_backend_hw_veb(conf, 'direct',
  122                                              'fake-pci-dev',
  123                                              'fake-vlan',
  124                                              'fake-tap')
  125         self.assertEqual('direct', conf.net_type)
  126         self.assertEqual('fake-devname', conf.source_dev)
  127         self.assertEqual('passthrough', conf.source_mode)
  128         self.assertEqual('vhost', conf.driver_name)
  129         self.assertEqual('fake-tap', conf.target_dev)
  130         mock_pci.assert_called_with('fake-pci-dev')
  131 
  132     def test_set_vif_host_backend_hw_veb_hostdev(self):
  133         conf = config.LibvirtConfigGuestInterface()
  134         designer.set_vif_host_backend_hw_veb(conf, 'hostdev',
  135                                              'fake-devname',
  136                                              'fake-vlan',
  137                                              'fake-tap')
  138         self.assertEqual('hostdev', conf.net_type)
  139         self.assertEqual('fake-devname', conf.source_dev)
  140         self.assertIsNone(conf.model)
  141         self.assertEqual('fake-vlan', conf.vlan)
  142         self.assertEqual('fake-tap', conf.target_dev)
  143 
  144     @mock.patch.object(pci_utils, 'get_pci_address_fields',
  145                        return_value=('fake-domain', 'fake-bus',
  146                                      'fake-slot', 'fake-function'))
  147     def test_set_vif_host_backend_hostdev_pci_config(self, mock_pci_fields):
  148         conf = config.LibvirtConfigGuestInterface()
  149         designer.set_vif_host_backend_hostdev_pci_config(conf,
  150                                                         'fake-pci-slot')
  151         self.assertEqual('fake-domain', conf.domain)
  152         self.assertEqual('fake-bus', conf.bus)
  153         self.assertEqual('fake-slot', conf.slot)
  154         self.assertEqual('fake-function', conf.function)
  155         mock_pci_fields.assert_called_with('fake-pci-slot')
  156 
  157     def test_set_vif_host_backend_direct_config(self):
  158         conf = config.LibvirtConfigGuestInterface()
  159         designer.set_vif_host_backend_direct_config(conf, 'fake-devname',
  160                                                     mode="passthrough")
  161         self.assertEqual('direct', conf.net_type)
  162         self.assertEqual('fake-devname', conf.source_dev)
  163         self.assertEqual('passthrough', conf.source_mode)
  164         self.assertEqual('virtio', conf.model)
  165 
  166     def test_set_vif_host_backend_vhostuser_config(self):
  167         conf = config.LibvirtConfigGuestInterface()
  168         designer.set_vif_host_backend_vhostuser_config(conf, 'fake-mode',
  169                                                        'fake-path', None, None)
  170         self.assertEqual('vhostuser', conf.net_type)
  171         self.assertEqual('unix', conf.vhostuser_type)
  172         self.assertEqual('fake-mode', conf.vhostuser_mode)
  173         self.assertEqual('fake-path', conf.vhostuser_path)
  174         self.assertIsNone(conf.vhost_rx_queue_size)
  175         self.assertIsNone(conf.vhost_tx_queue_size)
  176 
  177     def test_set_vif_host_backend_vhostuser_config_queue_size(self):
  178         conf = config.LibvirtConfigGuestInterface()
  179         designer.set_vif_host_backend_vhostuser_config(conf, 'fake-mode',
  180                                                        'fake-path', 512, 1024)
  181         self.assertEqual('vhostuser', conf.net_type)
  182         self.assertEqual('unix', conf.vhostuser_type)
  183         self.assertEqual('fake-mode', conf.vhostuser_mode)
  184         self.assertEqual('fake-path', conf.vhostuser_path)
  185         self.assertEqual(512, conf.vhost_rx_queue_size)
  186         self.assertEqual(1024, conf.vhost_tx_queue_size)
  187 
  188     def test_set_vif_host_backend_vhostuser_config_tx_queue_size(self):
  189         conf = config.LibvirtConfigGuestInterface()
  190         designer.set_vif_host_backend_vhostuser_config(conf, 'fake-mode',
  191                                                        'fake-path', None, 1024)
  192         self.assertEqual('vhostuser', conf.net_type)
  193         self.assertEqual('unix', conf.vhostuser_type)
  194         self.assertEqual('fake-mode', conf.vhostuser_mode)
  195         self.assertEqual('fake-path', conf.vhostuser_path)
  196         self.assertIsNone(conf.vhost_rx_queue_size)
  197         self.assertEqual(1024, conf.vhost_tx_queue_size)
  198 
  199     def test_set_vif_host_backend_vhostuser_config_rx_queue_size(self):
  200         conf = config.LibvirtConfigGuestInterface()
  201         designer.set_vif_host_backend_vhostuser_config(conf, 'fake-mode',
  202                                                        'fake-path', 512, None)
  203         self.assertEqual('vhostuser', conf.net_type)
  204         self.assertEqual('unix', conf.vhostuser_type)
  205         self.assertEqual('fake-mode', conf.vhostuser_mode)
  206         self.assertEqual('fake-path', conf.vhostuser_path)
  207         self.assertEqual(512, conf.vhost_rx_queue_size)
  208         self.assertIsNone(conf.vhost_tx_queue_size)
  209 
  210     def test_set_vif_host_backend_vhostuser_config_tapname(self):
  211         conf = config.LibvirtConfigGuestInterface()
  212         designer.set_vif_host_backend_vhostuser_config(conf, 'fake-mode',
  213                                                        'fake-path', None, None,
  214                                                        'fake-tap')
  215         self.assertEqual('vhostuser', conf.net_type)
  216         self.assertEqual('unix', conf.vhostuser_type)
  217         self.assertEqual('fake-mode', conf.vhostuser_mode)
  218         self.assertEqual('fake-path', conf.vhostuser_path)
  219         self.assertIsNone(conf.vhost_rx_queue_size)
  220         self.assertIsNone(conf.vhost_tx_queue_size)
  221         self.assertEqual('fake-tap', conf.target_dev)
  222 
  223     def test_set_vif_mtu_config(self):
  224         conf = config.LibvirtConfigGuestInterface()
  225         designer.set_vif_mtu_config(conf, 9000)
  226         self.assertEqual(9000, conf.mtu)
  227 
  228     def test_set_driver_iommu_for_sev(self):
  229         conf = fake_libvirt_data.fake_kvm_guest()
  230 
  231         # obj.devices[11]
  232         controller = config.LibvirtConfigGuestController()
  233         controller.type = 'virtio-serial'
  234         controller.index = 0
  235         conf.add_device(controller)
  236 
  237         designer.set_driver_iommu_for_sev(conf)
  238 
  239         # All disks/interfaces/memballoon are expected to be virtio,
  240         # thus driver_iommu should be on
  241         self.assertEqual(11, len(conf.devices))
  242         for i in (0, 2, 3, 6, 8, 9, 10):
  243             dev = conf.devices[i]
  244             self.assertTrue(
  245                 dev.driver_iommu,
  246                 "expected device %d to have driver_iommu enabled\n%s" %
  247                 (i, dev.to_xml()))
  248 
  249         for i in (1, 4):
  250             dev = conf.devices[i]
  251             self.assertFalse(
  252                 dev.driver_iommu,
  253                 "didn't expect device %i to have driver_iommu enabled\n%s" %
  254                 (i, dev.to_xml()))