"Fossies" - the Fresh Open Source Software Archive

Member "nova-22.0.1/nova/tests/unit/virt/libvirt/test_vif.py" (19 Nov 2020, 68524 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_vif.py": 22.0.0_vs_22.0.1.

    1 #    Copyright 2012 Nicira, Inc
    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 fixtures
   16 from lxml import etree
   17 import mock
   18 import os_vif
   19 from os_vif import exception as osv_exception
   20 from os_vif import objects as osv_objects
   21 from os_vif.objects import fields as osv_fields
   22 from oslo_concurrency import processutils
   23 from oslo_config import cfg
   24 from oslo_utils.fixture import uuidsentinel as uuids
   25 import six
   26 
   27 from nova import exception
   28 from nova.network import model as network_model
   29 from nova import objects
   30 from nova.pci import utils as pci_utils
   31 from nova import test
   32 from nova.tests.unit.virt import fakelibosinfo
   33 from nova.tests.unit.virt.libvirt import fakelibvirt
   34 from nova.virt.libvirt import config as vconfig
   35 from nova.virt.libvirt import vif
   36 
   37 CONF = cfg.CONF
   38 
   39 
   40 class LibvirtVifTestCase(test.NoDBTestCase):
   41 
   42     gateway_bridge_4 = network_model.IP(address='101.168.1.1', type='gateway')
   43     dns_bridge_4 = network_model.IP(address='8.8.8.8', type=None)
   44     ips_bridge_4 = [network_model.IP(address='101.168.1.9', type=None)]
   45     subnet_bridge_4 = network_model.Subnet(
   46         cidr='101.168.1.0/24',
   47         dns=[dns_bridge_4],
   48         gateway=gateway_bridge_4,
   49         routes=None,
   50         dhcp_server='191.168.1.1')
   51 
   52     gateway_bridge_6 = network_model.IP(address='101:1db9::1', type='gateway')
   53     subnet_bridge_6 = network_model.Subnet(cidr='101:1db9::/64',
   54         dns=None,
   55         gateway=gateway_bridge_6,
   56         ips=None,
   57         routes=None)
   58 
   59     network_bridge = network_model.Network(id=uuids.network,
   60         bridge='br0',
   61         label=None,
   62         subnets=[subnet_bridge_4, subnet_bridge_6],
   63         bridge_interface='eth0',
   64         vlan=99, mtu=9000)
   65 
   66     vif_bridge = network_model.VIF(id=uuids.vif,
   67         address='ca:fe:de:ad:be:ef',
   68         network=network_bridge,
   69         type=network_model.VIF_TYPE_BRIDGE,
   70         devname='tap-xxx-yyy-zzz',
   71         ovs_interfaceid=None)
   72 
   73     network_bridge_neutron = network_model.Network(id=uuids.network,
   74         bridge=None,
   75         label=None,
   76         subnets=[subnet_bridge_4, subnet_bridge_6],
   77         bridge_interface='eth0',
   78         vlan=99)
   79 
   80     vif_bridge_neutron = network_model.VIF(id=uuids.vif,
   81         address='ca:fe:de:ad:be:ef',
   82         network=network_bridge_neutron,
   83         type=None,
   84         devname='tap-xxx-yyy-zzz',
   85         ovs_interfaceid=uuids.ovs)
   86 
   87     network_ovs = network_model.Network(id=uuids.network,
   88         bridge='br0',
   89         label=None,
   90         subnets=[subnet_bridge_4, subnet_bridge_6],
   91         bridge_interface=None,
   92         vlan=99, mtu=1000)
   93 
   94     network_ivs = network_model.Network(id=uuids.network,
   95         bridge='br0',
   96         label=None,
   97         subnets=[subnet_bridge_4, subnet_bridge_6],
   98         bridge_interface=None,
   99         vlan=99)
  100 
  101     vif_agilio_ovs = network_model.VIF(id=uuids.vif,
  102         address='ca:fe:de:ad:be:ef',
  103         network=network_ovs,
  104         type=network_model.VIF_TYPE_AGILIO_OVS,
  105         details={'port_filter': False},
  106         devname='tap-xxx-yyy-zzz',
  107         ovs_interfaceid=uuids.ovs)
  108 
  109     vif_agilio_ovs_direct = network_model.VIF(id=uuids.vif,
  110         address='ca:fe:de:ad:be:ef',
  111         network=network_ovs,
  112         type=network_model.VIF_TYPE_AGILIO_OVS,
  113         vnic_type=network_model.VNIC_TYPE_DIRECT,
  114         ovs_interfaceid=uuids.ovs,
  115         devname='tap-xxx-yyy-zzz',
  116         profile={'pci_slot': '0000:0a:00.1'})
  117 
  118     vif_agilio_ovs_forwarder = network_model.VIF(id=uuids.vif,
  119         address='ca:fe:de:ad:be:ef',
  120         network=network_ovs,
  121         type=network_model.VIF_TYPE_AGILIO_OVS,
  122         vnic_type=network_model.VNIC_TYPE_VIRTIO_FORWARDER,
  123         profile={'pci_slot': '0000:0a:00.1'},
  124         details={
  125             network_model.VIF_DETAILS_VHOSTUSER_MODE: 'client',
  126             network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/usv-xxx-yyy-zzz',
  127             network_model.VIF_DETAILS_VHOSTUSER_OVS_PLUG: True},
  128         ovs_interfaceid=uuids.ovs, mtu=1500)
  129 
  130     vif_ovs = network_model.VIF(id=uuids.vif,
  131         address='ca:fe:de:ad:be:ef',
  132         network=network_ovs,
  133         type=network_model.VIF_TYPE_OVS,
  134         details={'port_filter': False},
  135         devname='tap-xxx-yyy-zzz',
  136         ovs_interfaceid=uuids.ovs)
  137 
  138     vif_ovs_direct = network_model.VIF(id=uuids.vif,
  139         address='ca:fe:de:ad:be:ef',
  140         network=network_ovs,
  141         vnic_type=network_model.VNIC_TYPE_DIRECT,
  142         profile={'pci_slot': '0000:0a:00.1'},
  143         type=network_model.VIF_TYPE_OVS,
  144         details={'port_filter': False},
  145         ovs_interfaceid=uuids.ovs)
  146 
  147     vif_ovs_filter_cap = network_model.VIF(id=uuids.vif,
  148         address='ca:fe:de:ad:be:ef',
  149         network=network_ovs,
  150         type=network_model.VIF_TYPE_OVS,
  151         details={'port_filter': True},
  152         devname='tap-xxx-yyy-zzz',
  153         ovs_interfaceid=uuids.ovs)
  154 
  155     vif_ovs_legacy = network_model.VIF(id=uuids.vif,
  156         address='ca:fe:de:ad:be:ef',
  157         network=network_ovs,
  158         type=None,
  159         devname=None,
  160         ovs_interfaceid=None)
  161 
  162     vif_ivs = network_model.VIF(id=uuids.vif,
  163         address='ca:fe:de:ad:be:ef',
  164         network=network_ivs,
  165         type=network_model.VIF_TYPE_IVS,
  166         devname='tap-xxx-yyy-zzz',
  167         ovs_interfaceid=uuids.ovs)
  168 
  169     vif_none = network_model.VIF(id=uuids.vif,
  170         address='ca:fe:de:ad:be:ef',
  171         network=network_bridge,
  172         type=None,
  173         devname='tap-xxx-yyy-zzz',
  174         ovs_interfaceid=None)
  175 
  176     network_8021 = network_model.Network(id=uuids.network,
  177         bridge=None,
  178         label=None,
  179         subnets=[subnet_bridge_4,
  180                  subnet_bridge_6],
  181         interface='eth0',
  182         vlan=99)
  183 
  184     vif_8021qbh = network_model.VIF(id=uuids.vif,
  185         address='ca:fe:de:ad:be:ef',
  186         network=network_8021,
  187         type=network_model.VIF_TYPE_802_QBH,
  188         vnic_type=network_model.VNIC_TYPE_DIRECT,
  189         ovs_interfaceid=None,
  190         details={
  191             network_model.VIF_DETAILS_PROFILEID:
  192             'MyPortProfile'},
  193         profile={'pci_vendor_info': '1137:0043',
  194                  'pci_slot': '0000:0a:00.1',
  195                  'physical_network': 'phynet1'})
  196 
  197     vif_hw_veb = network_model.VIF(id=uuids.vif,
  198         address='ca:fe:de:ad:be:ef',
  199         network=network_8021,
  200         type=network_model.VIF_TYPE_HW_VEB,
  201         vnic_type=network_model.VNIC_TYPE_DIRECT,
  202         ovs_interfaceid=None,
  203         details={
  204             network_model.VIF_DETAILS_VLAN: 100},
  205         profile={'pci_vendor_info': '1137:0043',
  206                  'pci_slot': '0000:0a:00.1',
  207                  'physical_network': 'phynet1'})
  208 
  209     vif_hw_veb_trusted = network_model.VIF(id=uuids.vif,
  210         address='ca:fe:de:ad:be:ef',
  211         network=network_8021,
  212         type=network_model.VIF_TYPE_HW_VEB,
  213         vnic_type=network_model.VNIC_TYPE_DIRECT,
  214         ovs_interfaceid=None,
  215         details={
  216             network_model.VIF_DETAILS_VLAN: 100},
  217         profile={'pci_vendor_info': '1137:0043',
  218                  'pci_slot': '0000:0a:00.1',
  219                  'physical_network': 'phynet1',
  220                  'trusted': 'True'})
  221 
  222     vif_hostdev_physical = network_model.VIF(id=uuids.vif,
  223         address='ca:fe:de:ad:be:ef',
  224         network=network_8021,
  225         type=network_model.VIF_TYPE_HOSTDEV,
  226         vnic_type=network_model.VNIC_TYPE_DIRECT_PHYSICAL,
  227         ovs_interfaceid=None,
  228         profile={'pci_vendor_info': '1137:0043',
  229                  'pci_slot': '0000:0a:00.1',
  230                  'physical_network': 'phynet1'})
  231 
  232     vif_hw_veb_macvtap = network_model.VIF(id=uuids.vif,
  233         address='ca:fe:de:ad:be:ef',
  234         network=network_8021,
  235         type=network_model.VIF_TYPE_HW_VEB,
  236         vnic_type=network_model.VNIC_TYPE_MACVTAP,
  237         ovs_interfaceid=None,
  238         details={network_model.VIF_DETAILS_VLAN: 100},
  239         profile={'pci_vendor_info': '1137:0043',
  240                  'pci_slot': '0000:0a:00.1',
  241                  'physical_network': 'phynet1'})
  242 
  243     vif_8021qbg = network_model.VIF(id=uuids.vif,
  244         address='ca:fe:de:ad:be:ef',
  245         network=network_8021,
  246         type=network_model.VIF_TYPE_802_QBG,
  247         ovs_interfaceid=None,
  248         qbg_params=network_model.VIF8021QbgParams(
  249         managerid="xxx-yyy-zzz",
  250         typeid="aaa-bbb-ccc",
  251         typeidversion="1",
  252         instanceid="ddd-eee-fff"))
  253 
  254     network_midonet = network_model.Network(id=uuids.network,
  255         label=None,
  256         bridge=None,
  257         subnets=[subnet_bridge_4],
  258         interface='eth0')
  259 
  260     network_vrouter = network_model.Network(id=uuids.network,
  261         label=None,
  262         bridge=None,
  263         subnets=[subnet_bridge_4, subnet_bridge_6],
  264         interface='eth0')
  265 
  266     vif_vrouter = network_model.VIF(id=uuids.vif,
  267         address='ca:fe:de:ad:be:ef',
  268         network=network_vrouter,
  269         type=network_model.VIF_TYPE_VROUTER,
  270         devname='tap-xxx-yyy-zzz')
  271 
  272     vif_vrouter_direct = network_model.VIF(id=uuids.vif,
  273         address='ca:fe:de:ad:be:ef',
  274         network=network_vrouter,
  275         type=network_model.VIF_TYPE_VROUTER,
  276         vnic_type=network_model.VNIC_TYPE_DIRECT,
  277         profile={'pci_slot': '0000:0a:00.1'},
  278         devname='tap-xxx-yyy-zzz')
  279 
  280     vif_vrouter_forwarder = network_model.VIF(id=uuids.vif,
  281         address='ca:fe:de:ad:be:ef',
  282         network=network_vrouter,
  283         type=network_model.VIF_TYPE_VROUTER,
  284         vnic_type=network_model.VNIC_TYPE_VIRTIO_FORWARDER,
  285         profile={'pci_slot': '0000:0a:00.1'},
  286         details={
  287             network_model.VIF_DETAILS_VHOSTUSER_MODE: 'server',
  288             network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/usv-xxx-yyy-zzz',
  289             network_model.VIF_DETAILS_VHOSTUSER_VROUTER_PLUG: True})
  290 
  291     vif_contrail_vrouter = network_model.VIF(id=uuids.vif,
  292         address='ca:fe:de:ad:be:ef',
  293         network=network_vrouter,
  294         type=network_model.VIF_TYPE_VHOSTUSER,
  295         details={
  296             network_model.VIF_DETAILS_VHOSTUSER_MODE: 'server',
  297             network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/usv-xxx-yyy-zzz',
  298             network_model.VIF_DETAILS_VHOSTUSER_VROUTER_PLUG: True})
  299 
  300     vif_ib_hostdev = network_model.VIF(id=uuids.vif,
  301         address='ca:fe:de:ad:be:ef',
  302         network=network_8021,
  303         type=network_model.VIF_TYPE_IB_HOSTDEV,
  304         vnic_type=network_model.VNIC_TYPE_DIRECT,
  305         ovs_interfaceid=None,
  306         details={network_model.VIF_DETAILS_VLAN: 100},
  307         profile={'pci_vendor_info': '1137:0043',
  308                  'pci_slot': '0000:0a:00.1',
  309                  'physical_network': 'phynet1'})
  310 
  311     vif_midonet = network_model.VIF(id=uuids.vif,
  312         address='ca:fe:de:ad:be:ef',
  313         network=network_midonet,
  314         type=network_model.VIF_TYPE_MIDONET,
  315         devname='tap-xxx-yyy-zzz')
  316 
  317     vif_tap = network_model.VIF(id=uuids.vif,
  318         address='ca:fe:de:ad:be:ef',
  319         type=network_model.VIF_TYPE_TAP,
  320         devname='tap-xxx-yyy-zzz')
  321 
  322     vif_iovisor = network_model.VIF(id=uuids.vif,
  323         address='ca:fe:de:ad:be:ef',
  324         network=network_bridge,
  325         type=network_model.VIF_TYPE_IOVISOR,
  326         devname='tap-xxx-yyy-zzz',
  327         ovs_interfaceid=None)
  328 
  329     vif_vhostuser = network_model.VIF(id=uuids.vif,
  330         address='ca:fe:de:ad:be:ef',
  331         network=network_bridge,
  332         type=network_model.VIF_TYPE_VHOSTUSER,
  333         details={
  334             network_model.VIF_DETAILS_VHOSTUSER_MODE: 'client',
  335             network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/vif-xxx-yyy-zzz'
  336         })
  337 
  338     vif_vhostuser_ovs = network_model.VIF(id=uuids.vif,
  339         address='ca:fe:de:ad:be:ef',
  340         network=network_bridge,
  341         type=network_model.VIF_TYPE_VHOSTUSER,
  342         details={
  343             network_model.VIF_DETAILS_VHOSTUSER_MODE: 'client',
  344             network_model.VIF_DETAILS_VHOSTUSER_SOCKET: '/tmp/usv-xxx-yyy-zzz',
  345             network_model.VIF_DETAILS_VHOSTUSER_OVS_PLUG: True},
  346         ovs_interfaceid=uuids.ovs, mtu=1500)
  347 
  348     vif_vhostuser_no_path = network_model.VIF(id=uuids.vif,
  349         address='ca:fe:de:ad:be:ef',
  350         network=network_bridge,
  351         type=network_model.VIF_TYPE_VHOSTUSER,
  352         details={network_model.VIF_DETAILS_VHOSTUSER_MODE: 'client'})
  353 
  354     vif_macvtap_vlan = network_model.VIF(id=uuids.vif,
  355         address='ca:fe:de:ad:be:ef',
  356         network=network_8021,
  357         type=network_model.VIF_TYPE_MACVTAP,
  358         details={
  359             network_model.VIF_DETAILS_VLAN: 1,
  360             network_model.VIF_DETAILS_PHYS_INTERFACE: 'eth0',
  361             network_model.VIF_DETAILS_MACVTAP_SOURCE: 'eth0.1',
  362             network_model.VIF_DETAILS_MACVTAP_MODE: 'vepa'})
  363 
  364     vif_macvtap_flat = network_model.VIF(id=uuids.vif,
  365         address='ca:fe:de:ad:be:ef',
  366         network=network_8021,
  367         type=network_model.VIF_TYPE_MACVTAP,
  368         details={
  369             network_model.VIF_DETAILS_PHYS_INTERFACE: 'eth0',
  370             network_model.VIF_DETAILS_MACVTAP_SOURCE: 'eth0',
  371             network_model.VIF_DETAILS_MACVTAP_MODE: 'bridge'})
  372 
  373     vif_macvtap_exception = network_model.VIF(id=uuids.vif,
  374         address='ca:fe:de:ad:be:ef',
  375         network=network_8021,
  376         type=network_model.VIF_TYPE_MACVTAP)
  377 
  378     instance = objects.Instance(id=1,
  379         uuid='f0000000-0000-0000-0000-000000000001',
  380         project_id=723)
  381 
  382     bandwidth = {
  383         'quota:vif_inbound_peak': '200',
  384         'quota:vif_outbound_peak': '20',
  385         'quota:vif_inbound_average': '100',
  386         'quota:vif_outbound_average': '10',
  387         'quota:vif_inbound_burst': '300',
  388         'quota:vif_outbound_burst': '30'
  389     }
  390 
  391     def setup_os_vif_objects(self):
  392         self.os_vif_network = osv_objects.network.Network(
  393             id="b82c1929-051e-481d-8110-4669916c7915",
  394             label="Demo Net",
  395             subnets=osv_objects.subnet.SubnetList(
  396                 objects=[]),
  397             mtu=9000)
  398 
  399         self.os_vif_bridge = osv_objects.vif.VIFBridge(
  400             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  401             address="22:52:25:62:e2:aa",
  402             plugin="linux_bridge",
  403             vif_name="nicdc065497-3c",
  404             bridge_name="br100",
  405             has_traffic_filtering=False,
  406             network=self.os_vif_network)
  407 
  408         self.os_vif_ovs_prof = osv_objects.vif.VIFPortProfileOpenVSwitch(
  409             interface_id="07bd6cea-fb37-4594-b769-90fc51854ee9",
  410             profile_id="fishfood")
  411 
  412         self.os_vif_repr_prof = osv_objects.vif.VIFPortProfileOVSRepresentor(
  413             interface_id="07bd6cea-fb37-4594-b769-90fc51854ee9",
  414             profile_id="fishfood",
  415             representor_name='nicdc065497-3c',
  416             representor_address='0000:0a:00.1')
  417 
  418         self.os_vif_agilio_ovs = osv_objects.vif.VIFOpenVSwitch(
  419             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  420             address="22:52:25:62:e2:aa",
  421             plugin="agilio_ovs",
  422             vif_name="nicdc065497-3c",
  423             bridge_name="br0",
  424             port_profile=self.os_vif_ovs_prof,
  425             network=self.os_vif_network)
  426 
  427         self.os_vif_agilio_forwarder = osv_objects.vif.VIFVHostUser(
  428             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  429             address="22:52:25:62:e2:aa",
  430             plugin="agilio_ovs",
  431             vif_name="nicdc065497-3c",
  432             path='/var/run/openvswitch/vhudc065497-3c',
  433             mode='client',
  434             port_profile=self.os_vif_repr_prof,
  435             network=self.os_vif_network)
  436 
  437         self.os_vif_agilio_direct = osv_objects.vif.VIFHostDevice(
  438             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  439             address="22:52:25:62:e2:aa",
  440             plugin="agilio_ovs",
  441             vif_name="nicdc065497-3c",
  442             dev_type=osv_fields.VIFHostDeviceDevType.ETHERNET,
  443             dev_address='0000:0a:00.1',
  444             port_profile=self.os_vif_repr_prof,
  445             network=self.os_vif_network)
  446 
  447         self.os_vif_ovs = osv_objects.vif.VIFOpenVSwitch(
  448             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  449             address="22:52:25:62:e2:aa",
  450             unplugin="linux_bridge",
  451             vif_name="nicdc065497-3c",
  452             bridge_name="br0",
  453             port_profile=self.os_vif_ovs_prof,
  454             network=self.os_vif_network)
  455 
  456         self.os_vif_ovs_hybrid = osv_objects.vif.VIFBridge(
  457             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  458             address="22:52:25:62:e2:aa",
  459             unplugin="linux_bridge",
  460             vif_name="nicdc065497-3c",
  461             bridge_name="br0",
  462             port_profile=self.os_vif_ovs_prof,
  463             has_traffic_filtering=False,
  464             network=self.os_vif_network)
  465 
  466         self.os_vif_vhostuser = osv_objects.vif.VIFVHostUser(
  467             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  468             address="22:52:25:62:e2:aa",
  469             plugin="openvswitch",
  470             vif_name="vhudc065497-3c",
  471             path='/var/run/openvswitch/vhudc065497-3c',
  472             mode='client',
  473             port_profile=self.os_vif_ovs_prof,
  474             network=self.os_vif_network)
  475 
  476         self.os_vif_hostdevice_ethernet = osv_objects.vif.VIFHostDevice(
  477             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  478             address="22:52:25:62:e2:aa",
  479             plugin="linux_bridge",
  480             vif_name="nicdc065497-3c",
  481             dev_type=osv_fields.VIFHostDeviceDevType.ETHERNET,
  482             dev_address='0000:0a:00.1',
  483             network=self.os_vif_network)
  484 
  485         self.os_vif_hostdevice_generic = osv_objects.vif.VIFHostDevice(
  486             id="dc065497-3c8d-4f44-8fb4-e1d33c16a536",
  487             address="22:52:25:62:e2:aa",
  488             plugin="linux_bridge",
  489             vif_name="nicdc065497-3c",
  490             dev_type=osv_fields.VIFHostDeviceDevType.GENERIC,
  491             dev_address='0000:0a:00.1',
  492             network=self.os_vif_network)
  493 
  494         self.os_vif_inst_info = osv_objects.instance_info.InstanceInfo(
  495             uuid="d5b1090c-9e00-4fa4-9504-4b1494857970",
  496             name="instance-000004da",
  497             project_id="2f37d7f6-e51a-4a1f-8b6e-b0917ffc8390")
  498 
  499     def setUp(self):
  500         super(LibvirtVifTestCase, self).setUp()
  501         self.useFixture(fakelibvirt.FakeLibvirtFixture(stub_os_vif=False))
  502         # os_vif.initialize is typically done in nova-compute startup
  503         os_vif.initialize()
  504         self.setup_os_vif_objects()
  505 
  506     def _get_node(self, xml):
  507         doc = etree.fromstring(xml)
  508         ret = doc.findall('./devices/interface')
  509         self.assertEqual(len(ret), 1)
  510         return ret[0]
  511 
  512     def _assertMacEquals(self, node, vif):
  513         mac = node.find("mac").get("address")
  514         self.assertEqual(mac, vif['address'])
  515 
  516     def _assertTypeEquals(self, node, type, attr, source, br_want):
  517         self.assertEqual(node.get("type"), type)
  518         br_name = node.find(attr).get(source)
  519         self.assertEqual(br_name, br_want)
  520 
  521     def _assertTypeAndMacEquals(self, node, type, attr, source, vif,
  522                                 br_want=None):
  523         self._assertTypeEquals(node, type, attr, source, br_want)
  524         self._assertMacEquals(node, vif)
  525 
  526     def _assertModel(self, xml, model_want=None, driver_want=None):
  527         node = self._get_node(xml)
  528         if model_want is None:
  529             ret = node.findall("model")
  530             self.assertEqual(len(ret), 0)
  531         else:
  532             model = node.find("model").get("type")
  533             self.assertEqual(model, model_want)
  534         if driver_want is None:
  535             ret = node.findall("driver")
  536             self.assertEqual(len(ret), 0)
  537         else:
  538             driver = node.find("driver").get("name")
  539             self.assertEqual(driver, driver_want)
  540 
  541     def _assertTypeAndPciEquals(self, node, type, vif):
  542         self.assertEqual(node.get("type"), type)
  543         self._assertPciEqual(node, vif, type="pci")
  544 
  545     def _assertPciEqual(self, node, vif, type=None):
  546         address = node.find("source").find("address")
  547         if type:
  548             addr_type = address.get("type")
  549             self.assertEqual(type, addr_type)
  550         pci_slot = "%(domain)s:%(bus)s:%(slot)s.%(func)s" % {
  551                      'domain': address.get("domain")[2:],
  552                      'bus': address.get("bus")[2:],
  553                      'slot': address.get("slot")[2:],
  554                      'func': address.get("function")[2:]}
  555 
  556         pci_slot_want = vif['profile']['pci_slot']
  557         self.assertEqual(pci_slot, pci_slot_want)
  558 
  559     def _assertQueueSizeEquals(self, node, rx_want, tx_want):
  560         rx_queue_size = node.find("driver").get("rx_queue_size")
  561         tx_queue_size = node.find("driver").get("tx_queue_size")
  562         self.assertEqual(rx_queue_size, rx_want)
  563         self.assertEqual(tx_queue_size, tx_want)
  564 
  565     def _assertXmlEqual(self, expectedXmlstr, actualXmlstr):
  566         if not isinstance(actualXmlstr, six.string_types):
  567             actualXmlstr = etree.tostring(actualXmlstr, encoding='unicode',
  568                                           pretty_print=True)
  569         self.assertXmlEqual(expectedXmlstr, actualXmlstr)
  570 
  571     def _get_conf(self):
  572         conf = vconfig.LibvirtConfigGuest()
  573         conf.virt_type = "qemu"
  574         conf.name = "fake-name"
  575         conf.uuid = "fake-uuid"
  576         conf.memory = 100 * 1024
  577         conf.vcpus = 4
  578         return conf
  579 
  580     def _get_instance_xml(self, driver, vif, image_meta=None, flavor=None):
  581         if flavor is None:
  582             flavor = objects.Flavor(name='m1.small',
  583                                 memory_mb=128,
  584                                 vcpus=1,
  585                                 root_gb=0,
  586                                 ephemeral_gb=0,
  587                                 swap=0,
  588                                 extra_specs=dict(self.bandwidth),
  589                                 deleted_at=None,
  590                                 deleted=0,
  591                                 created_at=None, flavorid=1,
  592                                 is_public=True, vcpu_weight=None,
  593                                 id=2, disabled=False, rxtx_factor=1.0)
  594 
  595         if image_meta is None:
  596             image_meta = objects.ImageMeta.from_dict({})
  597 
  598         conf = self._get_conf()
  599         nic = driver.get_config(self.instance, vif, image_meta,
  600                                 flavor, CONF.libvirt.virt_type)
  601         # TODO(stephenfin): There doesn't appear to be any reason we should do
  602         # this: just return 'nic.to_xml()' and remove '_get_node'
  603         conf.add_device(nic)
  604         return conf.to_xml()
  605 
  606     def _test_virtio_multiqueue(self, vcpus, want_queues):
  607         self.flags(use_virtio_for_bridges=True,
  608                    virt_type='kvm',
  609                    group='libvirt')
  610 
  611         flavor = objects.Flavor(name='m1.small',
  612                     memory_mb=128,
  613                     vcpus=vcpus,
  614                     root_gb=0,
  615                     ephemeral_gb=0,
  616                     swap=0,
  617                     deleted_at=None,
  618                     deleted=0,
  619                     created_at=None, flavorid=1,
  620                     is_public=True, vcpu_weight=None,
  621                     id=2, disabled=False, rxtx_factor=1.0,
  622                     extra_specs={})
  623 
  624         d = vif.LibvirtGenericVIFDriver()
  625         image_meta = objects.ImageMeta.from_dict(
  626             {'properties': {'hw_vif_model': 'virtio',
  627                             'hw_vif_multiqueue_enabled': 'true'}})
  628         xml = self._get_instance_xml(d, self.vif_bridge,
  629                                      image_meta, flavor)
  630 
  631         node = self._get_node(xml)
  632         driver = node.find("driver").get("name")
  633         self.assertEqual(driver, 'vhost')
  634         queues = node.find("driver").get("queues")
  635         self.assertEqual(queues, want_queues)
  636 
  637     def test_virtio_multiqueue(self):
  638         self._test_virtio_multiqueue(4, '4')
  639 
  640     @mock.patch('os.uname', return_value=('Linux', '', '2.6.32-21-generic'))
  641     def test_virtio_multiqueue_in_kernel_2(self, mock_uname):
  642         self._test_virtio_multiqueue(10, '1')
  643 
  644     @mock.patch('os.uname', return_value=('Linux', '', '3.19.0-47-generic'))
  645     def test_virtio_multiqueue_in_kernel_3(self, mock_uname):
  646         self._test_virtio_multiqueue(10, '8')
  647 
  648     @mock.patch('os.uname', return_value=('Linux', '', '4.2.0-35-generic'))
  649     def test_virtio_multiqueue_in_kernel_4(self, mock_uname):
  650         self._test_virtio_multiqueue(10, '10')
  651 
  652     @mock.patch('os.uname', return_value=('Linux', '', '2.6.32-21-generic'))
  653     def test_virtio_multiqueue_in_kernel_2_max_queues(self, mock_uname):
  654         self.flags(max_queues=2, group='libvirt')
  655         self._test_virtio_multiqueue(10, '2')
  656 
  657     @mock.patch('os.uname', return_value=('Linux', '', '3.19.0-47-generic'))
  658     def test_virtio_multiqueue_in_kernel_3_max_queues(self, mock_uname):
  659         self.flags(max_queues=2, group='libvirt')
  660         self._test_virtio_multiqueue(10, '2')
  661 
  662     @mock.patch('os.uname', return_value=('Linux', '', '4.2.0-35-generic'))
  663     def test_virtio_multiqueue_in_kernel_4_max_queues(self, mock_uname):
  664         self.flags(max_queues=2, group='libvirt')
  665         self._test_virtio_multiqueue(10, '2')
  666 
  667     def test_vhostuser_os_vif_multiqueue(self):
  668         d = vif.LibvirtGenericVIFDriver()
  669         image_meta = objects.ImageMeta.from_dict(
  670             {'properties': {'hw_vif_model': 'virtio',
  671                             'hw_vif_multiqueue_enabled': 'true'}})
  672         flavor = objects.Flavor(name='m1.small',
  673                     memory_mb=128,
  674                     vcpus=4,
  675                     root_gb=0,
  676                     ephemeral_gb=0,
  677                     swap=0,
  678                     deleted_at=None,
  679                     deleted=0,
  680                     created_at=None, flavorid=1,
  681                     is_public=True, vcpu_weight=None,
  682                     id=2, disabled=False, rxtx_factor=1.0)
  683         conf = d.get_base_config(None, 'ca:fe:de:ad:be:ef', image_meta,
  684                                  flavor, 'kvm', 'normal')
  685         self.assertEqual(4, conf.vhost_queues)
  686         self.assertEqual('vhost', conf.driver_name)
  687 
  688         d._set_config_VIFVHostUser(self.instance, self.os_vif_vhostuser, conf)
  689         self.assertEqual(4, conf.vhost_queues)
  690         self.assertIsNone(conf.driver_name)
  691 
  692     def _test_virtio_config_queue_sizes(
  693             self, vnic_type=network_model.VNIC_TYPE_NORMAL):
  694         self.flags(rx_queue_size=512, group='libvirt')
  695         self.flags(tx_queue_size=1024, group='libvirt')
  696         v = vif.LibvirtGenericVIFDriver()
  697         conf = v.get_base_config(
  698             None, 'ca:fe:de:ad:be:ef', {}, objects.Flavor(), 'kvm', vnic_type)
  699         return v, conf
  700 
  701     def test_virtio_vhost_queue_sizes(self):
  702         _, conf = self._test_virtio_config_queue_sizes()
  703         self.assertEqual(512, conf.vhost_rx_queue_size)
  704         self.assertIsNone(conf.vhost_tx_queue_size)
  705 
  706     def test_virtio_vhost_queue_sizes_vnic_type_direct(self):
  707         _, conf = self._test_virtio_config_queue_sizes(
  708             vnic_type=network_model.VNIC_TYPE_DIRECT)
  709         self.assertIsNone(conf.vhost_rx_queue_size)
  710         self.assertIsNone(conf.vhost_tx_queue_size)
  711 
  712     def test_virtio_vhost_queue_sizes_vnic_type_direct_physical(self):
  713         _, conf = self._test_virtio_config_queue_sizes(
  714             vnic_type=network_model.VNIC_TYPE_DIRECT_PHYSICAL)
  715         self.assertIsNone(conf.vhost_rx_queue_size)
  716         self.assertIsNone(conf.vhost_tx_queue_size)
  717 
  718     def test_virtio_vhost_queue_sizes_vnic_type_macvtap(self):
  719         _, conf = self._test_virtio_config_queue_sizes(
  720             vnic_type=network_model.VNIC_TYPE_MACVTAP)
  721         self.assertEqual(512, conf.vhost_rx_queue_size)
  722         self.assertIsNone(conf.vhost_tx_queue_size)
  723 
  724     def test_virtio_vhost_queue_sizes_vnic_type_virtio_forwarder(self):
  725         _, conf = self._test_virtio_config_queue_sizes(
  726             vnic_type=network_model.VNIC_TYPE_VIRTIO_FORWARDER)
  727         self.assertEqual(512, conf.vhost_rx_queue_size)
  728         self.assertIsNone(conf.vhost_tx_queue_size)
  729 
  730     def test_virtio_vhostuser_osvif_queue_sizes(self):
  731         v, conf = self._test_virtio_config_queue_sizes()
  732         v._set_config_VIFVHostUser(self.instance, self.os_vif_vhostuser, conf)
  733         self.assertEqual(512, conf.vhost_rx_queue_size)
  734         self.assertEqual(1024, conf.vhost_tx_queue_size)
  735 
  736     def test_multiple_nics(self):
  737         conf = self._get_conf()
  738         # Tests multiple nic configuration and that target_dev is
  739         # set for each
  740         nics = [{'net_type': 'bridge',
  741                  'mac_addr': '00:00:00:00:00:0b',
  742                  'source_dev': 'b_source_dev',
  743                  'target_dev': 'b_target_dev'},
  744                 {'net_type': 'ethernet',
  745                  'mac_addr': '00:00:00:00:00:0e',
  746                  'source_dev': 'e_source_dev',
  747                  'target_dev': 'e_target_dev'},
  748                 {'net_type': 'direct',
  749                  'mac_addr': '00:00:00:00:00:0d',
  750                  'source_dev': 'd_source_dev',
  751                  'target_dev': 'd_target_dev'}]
  752 
  753         for nic in nics:
  754             nic_conf = vconfig.LibvirtConfigGuestInterface()
  755             nic_conf.net_type = nic['net_type']
  756             nic_conf.target_dev = nic['target_dev']
  757             nic_conf.mac_addr = nic['mac_addr']
  758             nic_conf.source_dev = nic['source_dev']
  759             conf.add_device(nic_conf)
  760 
  761         xml = conf.to_xml()
  762         doc = etree.fromstring(xml)
  763         for nic in nics:
  764             path = "./devices/interface/[@type='%s']" % nic['net_type']
  765             node = doc.find(path)
  766             self.assertEqual(nic['net_type'], node.get("type"))
  767             self.assertEqual(nic['mac_addr'],
  768                              node.find("mac").get("address"))
  769             self.assertEqual(nic['target_dev'],
  770                              node.find("target").get("dev"))
  771 
  772     def test_model_novirtio(self):
  773         self.flags(use_virtio_for_bridges=False,
  774                    virt_type='kvm',
  775                    group='libvirt')
  776 
  777         d = vif.LibvirtGenericVIFDriver()
  778         xml = self._get_instance_xml(d, self.vif_bridge)
  779         self._assertModel(xml)
  780 
  781     def test_model_kvm(self):
  782         self.flags(use_virtio_for_bridges=True,
  783                    virt_type='kvm',
  784                    group='libvirt')
  785 
  786         d = vif.LibvirtGenericVIFDriver()
  787         xml = self._get_instance_xml(d, self.vif_bridge)
  788         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
  789 
  790     def test_model_parallels(self):
  791         self.flags(use_virtio_for_bridges=True,
  792                    virt_type='parallels',
  793                    group='libvirt')
  794 
  795         d = vif.LibvirtGenericVIFDriver()
  796         xml = self._get_instance_xml(d, self.vif_bridge)
  797         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
  798 
  799     def test_model_kvm_qemu_parallels_custom(self):
  800         for virt in ('kvm', 'qemu', 'parallels'):
  801             self.flags(use_virtio_for_bridges=True,
  802                        virt_type=virt,
  803                        group='libvirt')
  804 
  805             d = vif.LibvirtGenericVIFDriver()
  806             if virt == 'parallels':
  807                 supported = (network_model.VIF_MODEL_RTL8139,
  808                              network_model.VIF_MODEL_E1000)
  809             elif virt == 'qemu':
  810                 supported = (network_model.VIF_MODEL_LAN9118,
  811                              network_model.VIF_MODEL_NE2K_PCI,
  812                              network_model.VIF_MODEL_PCNET,
  813                              network_model.VIF_MODEL_RTL8139,
  814                              network_model.VIF_MODEL_E1000,
  815                              network_model.VIF_MODEL_E1000E,
  816                              network_model.VIF_MODEL_SPAPR_VLAN,
  817                              network_model.VIF_MODEL_VMXNET3)
  818             else:
  819                 supported = (network_model.VIF_MODEL_NE2K_PCI,
  820                              network_model.VIF_MODEL_PCNET,
  821                              network_model.VIF_MODEL_RTL8139,
  822                              network_model.VIF_MODEL_E1000,
  823                              network_model.VIF_MODEL_E1000E,
  824                              network_model.VIF_MODEL_SPAPR_VLAN,
  825                              network_model.VIF_MODEL_VMXNET3)
  826             for model in supported:
  827                 image_meta = objects.ImageMeta.from_dict(
  828                     {'properties': {'hw_vif_model': model}})
  829                 xml = self._get_instance_xml(d, self.vif_bridge,
  830                                              image_meta)
  831                 self._assertModel(xml, model)
  832 
  833     @mock.patch.object(vif.designer, 'set_vif_guest_frontend_config')
  834     def test_model_with_osinfo(self, mock_set):
  835         self.flags(use_virtio_for_bridges=True,
  836                    virt_type='kvm',
  837                    group='libvirt')
  838         self.useFixture(fixtures.MonkeyPatch(
  839             'nova.virt.osinfo.libosinfo',
  840             fakelibosinfo))
  841         d = vif.LibvirtGenericVIFDriver()
  842         image_meta = {'properties': {'os_name': 'fedora22'}}
  843         image_meta = objects.ImageMeta.from_dict(image_meta)
  844         d.get_base_config(None, 'ca:fe:de:ad:be:ef', image_meta,
  845                           None, 'kvm', 'normal')
  846         mock_set.assert_called_once_with(mock.ANY, 'ca:fe:de:ad:be:ef',
  847                                          'virtio', None, None, None)
  848 
  849     @mock.patch.object(vif.designer, 'set_vif_guest_frontend_config',
  850                        wraps=vif.designer.set_vif_guest_frontend_config)
  851     def test_model_sriov_direct(self, mock_set):
  852         """Direct attach vNICs shouldn't retrieve info from image_meta."""
  853         self.flags(use_virtio_for_bridges=True,
  854                    virt_type='kvm',
  855                    group='libvirt')
  856         self.useFixture(fixtures.MonkeyPatch(
  857             'nova.virt.osinfo.libosinfo',
  858             fakelibosinfo))
  859         d = vif.LibvirtGenericVIFDriver()
  860         image_meta = objects.ImageMeta.from_dict(
  861             {'properties': {'hw_vif_model': 'virtio'}})
  862         conf = d.get_base_config(None, 'ca:fe:de:ad:be:ef', image_meta,
  863                                  None, 'kvm', network_model.VNIC_TYPE_DIRECT)
  864         mock_set.assert_called_once_with(mock.ANY, 'ca:fe:de:ad:be:ef',
  865                                          None, None, None, None)
  866         self.assertIsNone(conf.vhost_queues)
  867         self.assertIsNone(conf.driver_name)
  868         self.assertIsNone(conf.model)
  869 
  870     def test_model_qemu(self):
  871         vif_objs = [
  872             self.vif_bridge,
  873             self.vif_8021qbg,
  874             self.vif_iovisor,
  875             self.vif_ovs,
  876         ]
  877 
  878         self.flags(use_virtio_for_bridges=True,
  879                    virt_type='qemu',
  880                    group='libvirt')
  881 
  882         for vif_obj in vif_objs:
  883             d = vif.LibvirtGenericVIFDriver()
  884 
  885             xml = self._get_instance_xml(d, vif_obj)
  886 
  887             doc = etree.fromstring(xml)
  888 
  889             bandwidth = doc.find('./devices/interface/bandwidth')
  890             self.assertIsNotNone(bandwidth)
  891 
  892             inbound = bandwidth.find('inbound')
  893             self.assertEqual(inbound.get("average"),
  894                              self.bandwidth['quota:vif_inbound_average'])
  895             self.assertEqual(inbound.get("peak"),
  896                              self.bandwidth['quota:vif_inbound_peak'])
  897             self.assertEqual(inbound.get("burst"),
  898                              self.bandwidth['quota:vif_inbound_burst'])
  899 
  900             outbound = bandwidth.find('outbound')
  901             self.assertEqual(outbound.get("average"),
  902                              self.bandwidth['quota:vif_outbound_average'])
  903             self.assertEqual(outbound.get("peak"),
  904                              self.bandwidth['quota:vif_outbound_peak'])
  905             self.assertEqual(outbound.get("burst"),
  906                              self.bandwidth['quota:vif_outbound_burst'])
  907 
  908             self._assertModel(xml, network_model.VIF_MODEL_VIRTIO, "qemu")
  909 
  910     def test_model_xen(self):
  911         self.flags(use_virtio_for_bridges=True,
  912                    virt_type='xen',
  913                    group='libvirt')
  914 
  915         d = vif.LibvirtGenericVIFDriver()
  916         xml = self._get_instance_xml(d, self.vif_bridge)
  917         self._assertModel(xml)
  918 
  919     def test_generic_driver_none(self):
  920         d = vif.LibvirtGenericVIFDriver()
  921         self.assertRaises(exception.NovaException,
  922                           self._get_instance_xml,
  923                           d,
  924                           self.vif_none)
  925 
  926     def _check_bridge_driver(self, d, vif, br_want):
  927         xml = self._get_instance_xml(d, vif)
  928         node = self._get_node(xml)
  929         self._assertTypeAndMacEquals(node, "bridge", "source", "bridge",
  930                                      self.vif_bridge, br_want)
  931 
  932     def test_generic_driver_bridge(self):
  933         d = vif.LibvirtGenericVIFDriver()
  934         self._check_bridge_driver(d,
  935                                   self.vif_bridge,
  936                                   self.vif_bridge['network']['bridge'])
  937 
  938     @mock.patch.object(pci_utils, 'get_ifname_by_pci_address')
  939     @mock.patch.object(pci_utils, 'get_vf_num_by_pci_address', return_value=1)
  940     @mock.patch('nova.privsep.linux_net.set_device_macaddr')
  941     @mock.patch('nova.privsep.linux_net.set_device_macaddr_and_vlan')
  942     def _test_hw_veb_op(self, op, vlan, mock_set_macaddr_and_vlan,
  943                         mock_set_macaddr, mock_get_vf_num,
  944                         mock_get_ifname):
  945         mock_get_ifname.side_effect = ['eth1', 'eth13']
  946         vlan_id = int(vlan)
  947         port_state = 'up' if vlan_id > 0 else 'down'
  948         mac = ('00:00:00:00:00:00' if op.__name__ == 'unplug'
  949                else self.vif_hw_veb_macvtap['address'])
  950         calls = {
  951             'get_ifname':
  952                 [mock.call(self.vif_hw_veb_macvtap['profile']['pci_slot'],
  953                            pf_interface=True),
  954                  mock.call(self.vif_hw_veb_macvtap['profile']['pci_slot'])],
  955             'get_vf_num':
  956                 [mock.call(self.vif_hw_veb_macvtap['profile']['pci_slot'])],
  957             'set_macaddr': [mock.call('eth13', mac, port_state=port_state)]
  958         }
  959         op(self.instance, self.vif_hw_veb_macvtap)
  960         mock_get_ifname.assert_has_calls(calls['get_ifname'])
  961         mock_get_vf_num.assert_has_calls(calls['get_vf_num'])
  962         mock_set_macaddr.assert_has_calls(calls['set_macaddr'])
  963         mock_set_macaddr_and_vlan.assert_called_once_with(
  964             'eth1', 1, mock.ANY, vlan_id)
  965 
  966     def test_plug_hw_veb(self):
  967         d = vif.LibvirtGenericVIFDriver()
  968         details = self.vif_hw_veb_macvtap['details']
  969         vlan = details[network_model.VIF_DETAILS_VLAN]
  970         self._test_hw_veb_op(d.plug, vlan)
  971 
  972     def test_unplug_hw_veb(self):
  973         d = vif.LibvirtGenericVIFDriver()
  974         self._test_hw_veb_op(d.unplug, 0)
  975 
  976     @mock.patch('nova.virt.libvirt.vif.set_vf_trusted')
  977     def test_plug_hw_veb_trusted(self, mset_vf_trusted):
  978         d = vif.LibvirtGenericVIFDriver()
  979         d.plug(self.instance, self.vif_hw_veb_trusted)
  980         mset_vf_trusted.assert_called_once_with('0000:0a:00.1', True)
  981 
  982     @mock.patch('nova.virt.libvirt.vif.set_vf_trusted')
  983     def test_unplug_hw_veb_trusted(self, mset_vf_trusted):
  984         d = vif.LibvirtGenericVIFDriver()
  985         d.unplug(self.instance, self.vif_hw_veb_trusted)
  986         mset_vf_trusted.assert_called_once_with('0000:0a:00.1', False)
  987 
  988     @mock.patch('nova.privsep.libvirt.unplug_plumgrid_vif',
  989                 side_effect=processutils.ProcessExecutionError)
  990     def test_unplug_iovisor(self, mock_unplug):
  991         d = vif.LibvirtGenericVIFDriver()
  992         d.unplug(self.instance, self.vif_iovisor)
  993 
  994     @mock.patch('nova.privsep.linux_net.device_exists')
  995     @mock.patch('nova.privsep.libvirt.plug_plumgrid_vif')
  996     @mock.patch('nova.privsep.linux_net.create_tap_dev')
  997     def test_plug_iovisor(self, mock_create_tap_dev, mock_plug, device_exists):
  998         device_exists.return_value = True
  999         d = vif.LibvirtGenericVIFDriver()
 1000         d.plug(self.instance, self.vif_iovisor)
 1001         mock_plug.assert_has_calls(
 1002             [mock.call('tap-xxx-yyy-zzz',
 1003                        self.vif_iovisor['id'],
 1004                        self.vif_iovisor['address'],
 1005                        self.vif_iovisor['network']['id'],
 1006                        self.instance.project_id)])
 1007 
 1008     def _check_ovs_virtualport_driver(self, d, vif, want_iface_id):
 1009         xml = self._get_instance_xml(d, vif)
 1010         node = self._get_node(xml)
 1011         self._assertTypeAndMacEquals(node, "bridge", "source", "bridge",
 1012                                      vif, "br0")
 1013         vp = node.find("virtualport")
 1014         self.assertEqual(vp.get("type"), "openvswitch")
 1015         iface_id_found = False
 1016         for p_elem in vp.findall("parameters"):
 1017             iface_id = p_elem.get("interfaceid", None)
 1018             if iface_id:
 1019                 self.assertEqual(iface_id, want_iface_id)
 1020                 iface_id_found = True
 1021 
 1022         self.assertTrue(iface_id_found)
 1023 
 1024     def test_generic_ovs_virtualport_driver(self):
 1025         d = vif.LibvirtGenericVIFDriver()
 1026         want_iface_id = self.vif_ovs['ovs_interfaceid']
 1027         self._check_ovs_virtualport_driver(d,
 1028                                            self.vif_ovs,
 1029                                            want_iface_id)
 1030 
 1031     def test_direct_plug_with_port_filter_cap_no_nova_firewall(self):
 1032         d = vif.LibvirtGenericVIFDriver()
 1033         br_want = self.vif_midonet['devname']
 1034         xml = self._get_instance_xml(d, self.vif_ovs_filter_cap)
 1035         node = self._get_node(xml)
 1036         self._assertTypeAndMacEquals(node, "bridge", "target", "dev",
 1037                                      self.vif_ovs_filter_cap, br_want)
 1038 
 1039     def test_ib_hostdev_driver(self):
 1040         d = vif.LibvirtGenericVIFDriver()
 1041         xml = self._get_instance_xml(d, self.vif_ib_hostdev)
 1042         doc = etree.fromstring(xml)
 1043         node = doc.findall('./devices/hostdev')[0]
 1044         self.assertEqual(1, len(node))
 1045         self._assertPciEqual(node, self.vif_ib_hostdev)
 1046 
 1047     def test_midonet_ethernet_vif_driver(self):
 1048         d = vif.LibvirtGenericVIFDriver()
 1049         br_want = self.vif_midonet['devname']
 1050         xml = self._get_instance_xml(d, self.vif_midonet)
 1051         node = self._get_node(xml)
 1052         self._assertTypeAndMacEquals(node, "ethernet", "target", "dev",
 1053                                      self.vif_midonet, br_want)
 1054 
 1055     def test_tap_ethernet_vif_driver(self):
 1056         d = vif.LibvirtGenericVIFDriver()
 1057         br_want = self.vif_tap['devname']
 1058         xml = self._get_instance_xml(d, self.vif_tap)
 1059         node = self._get_node(xml)
 1060         self._assertTypeAndMacEquals(node, "ethernet", "target", "dev",
 1061                                      self.vif_tap, br_want)
 1062 
 1063     @mock.patch('nova.privsep.linux_net.device_exists', return_value=True)
 1064     @mock.patch('nova.privsep.linux_net.set_device_mtu')
 1065     @mock.patch('nova.privsep.linux_net.create_tap_dev')
 1066     def test_plug_tap_kvm_virtio(self, mock_create_tap_dev, mock_set_mtu,
 1067                       mock_device_exists):
 1068 
 1069         d1 = vif.LibvirtGenericVIFDriver()
 1070         ins = objects.Instance(
 1071             id=1, uuid='f0000000-0000-0000-0000-000000000001',
 1072             project_id=723, system_metadata={}
 1073         )
 1074         d1.plug(ins, self.vif_tap)
 1075         mock_create_tap_dev.assert_called_once_with('tap-xxx-yyy-zzz', None,
 1076                                                     multiqueue=False)
 1077 
 1078         mock_create_tap_dev.reset_mock()
 1079 
 1080         d2 = vif.LibvirtGenericVIFDriver()
 1081         mq_ins = objects.Instance(
 1082             id=1, uuid='f0000000-0000-0000-0000-000000000001',
 1083             project_id=723, system_metadata={
 1084                 'image_hw_vif_multiqueue_enabled': 'True'
 1085             }
 1086         )
 1087         d2.plug(mq_ins, self.vif_tap)
 1088         mock_create_tap_dev.assert_called_once_with('tap-xxx-yyy-zzz', None,
 1089                                                     multiqueue=True)
 1090 
 1091     @mock.patch('nova.privsep.linux_net.device_exists', return_value=True)
 1092     @mock.patch('nova.privsep.linux_net.set_device_mtu')
 1093     @mock.patch('nova.privsep.linux_net.create_tap_dev')
 1094     def test_plug_tap_mq_ignored_virt_type(
 1095             self, mock_create_tap_dev, mock_set_mtu, mock_device_exists):
 1096 
 1097         self.flags(use_virtio_for_bridges=True,
 1098                    virt_type='xen',
 1099                    group='libvirt')
 1100 
 1101         d1 = vif.LibvirtGenericVIFDriver()
 1102         ins = objects.Instance(
 1103             id=1, uuid='f0000000-0000-0000-0000-000000000001',
 1104             project_id=723, system_metadata={
 1105                 'image_hw_vif_multiqueue_enabled': 'True'
 1106             }
 1107         )
 1108         d1.plug(ins, self.vif_tap)
 1109         mock_create_tap_dev.assert_called_once_with('tap-xxx-yyy-zzz',
 1110                                                     None,
 1111                                                     multiqueue=False)
 1112 
 1113     @mock.patch('nova.privsep.linux_net.device_exists', return_value=True)
 1114     @mock.patch('nova.privsep.linux_net.set_device_mtu')
 1115     @mock.patch('nova.privsep.linux_net.create_tap_dev')
 1116     def test_plug_tap_mq_ignored_vif_model(
 1117             self, mock_create_tap_dev, mock_set_mtu, mock_device_exists):
 1118 
 1119         d1 = vif.LibvirtGenericVIFDriver()
 1120         ins = objects.Instance(
 1121             id=1, uuid='f0000000-0000-0000-0000-000000000001',
 1122             project_id=723, system_metadata={
 1123                 'image_hw_vif_multiqueue_enabled': 'True',
 1124                 'image_hw_vif_model': 'e1000',
 1125             }
 1126         )
 1127         d1.plug(ins, self.vif_tap)
 1128         mock_create_tap_dev.assert_called_once_with('tap-xxx-yyy-zzz',
 1129                                                     None,
 1130                                                     multiqueue=False)
 1131 
 1132     def test_unplug_tap(self):
 1133         d = vif.LibvirtGenericVIFDriver()
 1134         d.unplug(self.instance, self.vif_tap)
 1135 
 1136     def test_generic_8021qbh_driver(self):
 1137         d = vif.LibvirtGenericVIFDriver()
 1138         xml = self._get_instance_xml(d, self.vif_8021qbh)
 1139         node = self._get_node(xml)
 1140         self._assertTypeAndPciEquals(node, "hostdev", self.vif_8021qbh)
 1141         self._assertMacEquals(node, self.vif_8021qbh)
 1142         vp = node.find("virtualport")
 1143         self.assertEqual(vp.get("type"), "802.1Qbh")
 1144         profile_id_found = False
 1145         for p_elem in vp.findall("parameters"):
 1146             details = self.vif_8021qbh["details"]
 1147             profile_id = p_elem.get("profileid", None)
 1148             if profile_id:
 1149                 self.assertEqual(profile_id,
 1150                                  details[network_model.VIF_DETAILS_PROFILEID])
 1151                 profile_id_found = True
 1152 
 1153         self.assertTrue(profile_id_found)
 1154 
 1155     def test_hw_veb_driver(self):
 1156         d = vif.LibvirtGenericVIFDriver()
 1157         xml = self._get_instance_xml(d, self.vif_hw_veb)
 1158         node = self._get_node(xml)
 1159         self._assertTypeAndPciEquals(node, "hostdev", self.vif_hw_veb)
 1160         self._assertMacEquals(node, self.vif_hw_veb)
 1161 
 1162         conf = vconfig.LibvirtConfigGuestInterface()
 1163         conf.parse_dom(node)
 1164         self.assertEqual(conf.vlan, self.vif_hw_veb["details"]["vlan"])
 1165 
 1166     def test_hostdev_physical_driver(self):
 1167         d = vif.LibvirtGenericVIFDriver()
 1168         xml = self._get_instance_xml(d, self.vif_hostdev_physical)
 1169         doc = etree.fromstring(xml)
 1170         node = doc.findall('./devices/hostdev')[0]
 1171         self.assertEqual(1, len(node))
 1172         self._assertPciEqual(node, self.vif_hostdev_physical)
 1173 
 1174     @mock.patch.object(pci_utils, 'get_ifname_by_pci_address',
 1175                        return_value='eth1')
 1176     def test_hw_veb_driver_macvtap(self, mock_get_ifname):
 1177         d = vif.LibvirtGenericVIFDriver()
 1178         xml = self._get_instance_xml(d, self.vif_hw_veb_macvtap)
 1179         node = self._get_node(xml)
 1180         self.assertEqual(node.get("type"), "direct")
 1181         self._assertTypeEquals(node, "direct", "source",
 1182                                "dev", "eth1")
 1183         self._assertTypeEquals(node, "direct", "source",
 1184                                "mode", "passthrough")
 1185         self._assertMacEquals(node, self.vif_hw_veb_macvtap)
 1186         vlan = node.find("vlan").find("tag").get("id")
 1187         vlan_want = self.vif_hw_veb["details"]["vlan"]
 1188         self.assertEqual(int(vlan), vlan_want)
 1189 
 1190     def test_driver_macvtap_vlan(self):
 1191         d = vif.LibvirtGenericVIFDriver()
 1192         xml = self._get_instance_xml(d, self.vif_macvtap_vlan)
 1193         node = self._get_node(xml)
 1194         self.assertEqual(node.get("type"), "direct")
 1195         self._assertTypeEquals(node, "direct", "source",
 1196                                "dev", "eth0.1")
 1197         self._assertTypeEquals(node, "direct", "source",
 1198                                "mode", "vepa")
 1199         self._assertMacEquals(node, self.vif_macvtap_vlan)
 1200 
 1201     def test_driver_macvtap_flat(self):
 1202         d = vif.LibvirtGenericVIFDriver()
 1203         xml = self._get_instance_xml(d, self.vif_macvtap_flat)
 1204         node = self._get_node(xml)
 1205         self.assertEqual(node.get("type"), "direct")
 1206         self._assertTypeEquals(node, "direct", "source",
 1207                                "dev", "eth0")
 1208         self._assertTypeEquals(node, "direct", "source",
 1209                                "mode", "bridge")
 1210         self._assertMacEquals(node, self.vif_macvtap_flat)
 1211 
 1212     def test_driver_macvtap_exception(self):
 1213         d = vif.LibvirtGenericVIFDriver()
 1214         e = self.assertRaises(exception.VifDetailsMissingMacvtapParameters,
 1215                           self._get_instance_xml,
 1216                           d,
 1217                           self.vif_macvtap_exception)
 1218         self.assertIn('macvtap_source', six.text_type(e))
 1219         self.assertIn('macvtap_mode', six.text_type(e))
 1220         self.assertIn('physical_interface', six.text_type(e))
 1221 
 1222     @mock.patch('nova.virt.libvirt.vif.ensure_vlan')
 1223     def test_macvtap_plug_vlan(self, ensure_vlan_mock):
 1224         d = vif.LibvirtGenericVIFDriver()
 1225         d.plug(self.instance, self.vif_macvtap_vlan)
 1226         ensure_vlan_mock.assert_called_once_with(1, 'eth0', interface='eth0.1')
 1227 
 1228     @mock.patch('nova.virt.libvirt.vif.ensure_vlan')
 1229     def test_macvtap_plug_flat(self, ensure_vlan_mock):
 1230         d = vif.LibvirtGenericVIFDriver()
 1231         d.plug(self.instance, self.vif_macvtap_flat)
 1232         self.assertFalse(ensure_vlan_mock.called)
 1233 
 1234     def test_generic_iovisor_driver(self):
 1235         d = vif.LibvirtGenericVIFDriver()
 1236         br_want = self.vif_iovisor['devname']
 1237         xml = self._get_instance_xml(d, self.vif_iovisor)
 1238         node = self._get_node(xml)
 1239         self._assertTypeAndMacEquals(node, "ethernet", "target", "dev",
 1240                                      self.vif_iovisor, br_want)
 1241 
 1242     def test_generic_8021qbg_driver(self):
 1243         d = vif.LibvirtGenericVIFDriver()
 1244         xml = self._get_instance_xml(d, self.vif_8021qbg)
 1245 
 1246         node = self._get_node(xml)
 1247         self._assertTypeEquals(node, "direct", "source", "dev", "eth0")
 1248         self._assertMacEquals(node, self.vif_8021qbg)
 1249 
 1250         vp = node.find("virtualport")
 1251         self.assertEqual(vp.get("type"), "802.1Qbg")
 1252         manager_id_found = False
 1253         type_id_found = False
 1254         typeversion_id_found = False
 1255         instance_id_found = False
 1256         for p_elem in vp.findall("parameters"):
 1257             wantparams = self.vif_8021qbg['qbg_params']
 1258             manager_id = p_elem.get("managerid", None)
 1259             type_id = p_elem.get("typeid", None)
 1260             typeversion_id = p_elem.get("typeidversion", None)
 1261             instance_id = p_elem.get("instanceid", None)
 1262             if manager_id:
 1263                 self.assertEqual(manager_id,
 1264                                  wantparams['managerid'])
 1265                 manager_id_found = True
 1266             if type_id:
 1267                 self.assertEqual(type_id,
 1268                                  wantparams['typeid'])
 1269                 type_id_found = True
 1270             if typeversion_id:
 1271                 self.assertEqual(typeversion_id,
 1272                                  wantparams['typeidversion'])
 1273                 typeversion_id_found = True
 1274             if instance_id:
 1275                 self.assertEqual(instance_id,
 1276                                  wantparams['instanceid'])
 1277                 instance_id_found = True
 1278 
 1279         self.assertTrue(manager_id_found)
 1280         self.assertTrue(type_id_found)
 1281         self.assertTrue(typeversion_id_found)
 1282         self.assertTrue(instance_id_found)
 1283 
 1284     def test_vhostuser_driver(self):
 1285         d = vif.LibvirtGenericVIFDriver()
 1286         xml = self._get_instance_xml(d, self.vif_vhostuser)
 1287         node = self._get_node(xml)
 1288         self.assertEqual(node.get("type"),
 1289                          network_model.VIF_TYPE_VHOSTUSER)
 1290 
 1291         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1292                                "source", "mode", "client")
 1293         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1294                                "source", "path", "/tmp/vif-xxx-yyy-zzz")
 1295         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1296                                "source", "type", "unix")
 1297         self._assertMacEquals(node, self.vif_vhostuser)
 1298         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
 1299 
 1300     def test_vhostuser_driver_queue_sizes(self):
 1301         self.flags(rx_queue_size=512, group='libvirt')
 1302         self.flags(tx_queue_size=1024, group='libvirt')
 1303         d = vif.LibvirtGenericVIFDriver()
 1304         xml = self._get_instance_xml(d, self.vif_vhostuser)
 1305         node = self._get_node(xml)
 1306         self._assertQueueSizeEquals(node, "512", "1024")
 1307 
 1308     def test_vhostuser_driver_no_path(self):
 1309         d = vif.LibvirtGenericVIFDriver()
 1310 
 1311         self.assertRaises(exception.VifDetailsMissingVhostuserSockPath,
 1312                           self._get_instance_xml,
 1313                           d,
 1314                           self.vif_vhostuser_no_path)
 1315 
 1316     def test_vhostuser_driver_ovs(self):
 1317         d = vif.LibvirtGenericVIFDriver()
 1318         xml = self._get_instance_xml(d,
 1319                                      self.vif_vhostuser_ovs)
 1320         node = self._get_node(xml)
 1321         self.assertEqual(node.get("type"),
 1322                          network_model.VIF_TYPE_VHOSTUSER)
 1323 
 1324         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1325                                "source", "mode", "client")
 1326         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1327                                "source", "path", "/tmp/usv-xxx-yyy-zzz")
 1328         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1329                                "source", "type", "unix")
 1330         self._assertMacEquals(node, self.vif_vhostuser_ovs)
 1331         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
 1332 
 1333     def test_ovs_direct(self):
 1334         d = vif.LibvirtGenericVIFDriver()
 1335         xml = self._get_instance_xml(d, self.vif_ovs_direct)
 1336         node = self._get_node(xml)
 1337         self._assertTypeAndPciEquals(node,
 1338                                      "hostdev",
 1339                                      self.vif_ovs_direct)
 1340         self._assertMacEquals(node, self.vif_ovs_direct)
 1341 
 1342     def test_agilio_ovs_direct(self):
 1343         d = vif.LibvirtGenericVIFDriver()
 1344         xml = self._get_instance_xml(d, self.vif_agilio_ovs_direct)
 1345         node = self._get_node(xml)
 1346         self._assertTypeAndPciEquals(node,
 1347                                      "hostdev",
 1348                                      self.vif_agilio_ovs_direct)
 1349         self._assertMacEquals(node, self.vif_agilio_ovs_direct)
 1350 
 1351     def test_agilio_ovs_forwarder(self):
 1352         d = vif.LibvirtGenericVIFDriver()
 1353         xml = self._get_instance_xml(d,
 1354                                      self.vif_agilio_ovs_forwarder)
 1355         node = self._get_node(xml)
 1356         self.assertEqual(node.get("type"),
 1357                          network_model.VIF_TYPE_VHOSTUSER)
 1358 
 1359         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1360                                "source", "mode", "client")
 1361         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1362                                "source", "path", "/tmp/usv-xxx-yyy-zzz")
 1363         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1364                                "source", "type", "unix")
 1365         self._assertMacEquals(node, self.vif_agilio_ovs_forwarder)
 1366         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
 1367 
 1368     def test_ivs_ethernet_driver(self):
 1369         d = vif.LibvirtGenericVIFDriver()
 1370         xml = self._get_instance_xml(d, self.vif_ivs)
 1371         node = self._get_node(xml)
 1372         dev_want = self.vif_ivs['devname']
 1373         self._assertTypeAndMacEquals(node, "ethernet", "target", "dev",
 1374                                      self.vif_ivs, dev_want)
 1375         script = node.find("script")
 1376         self.assertIsNone(script)
 1377 
 1378     def test_vrouter(self):
 1379         """Test for the Contrail / Tungsten Fabric kernel datapath."""
 1380         d = vif.LibvirtGenericVIFDriver()
 1381         dev_want = self.vif_vrouter['devname']
 1382         xml = self._get_instance_xml(d, self.vif_vrouter)
 1383         node = self._get_node(xml)
 1384         self._assertTypeAndMacEquals(node, "ethernet", "target", "dev",
 1385                                      self.vif_vrouter, dev_want)
 1386 
 1387     def test_vrouter_direct(self):
 1388         """Test for Contrail / Tungsten Fabric direct offloaded datapath."""
 1389         d = vif.LibvirtGenericVIFDriver()
 1390         xml = self._get_instance_xml(d, self.vif_vrouter_direct)
 1391         node = self._get_node(xml)
 1392         self._assertTypeAndPciEquals(node,
 1393                                      "hostdev",
 1394                                      self.vif_vrouter_direct)
 1395         self._assertMacEquals(node, self.vif_vrouter_direct)
 1396 
 1397     def test_vrouter_forwarder(self):
 1398         """Test for Contrail / Tungsten Fabric indirect offloaded datapath."""
 1399         d = vif.LibvirtGenericVIFDriver()
 1400         xml = self._get_instance_xml(d,
 1401                                      self.vif_vrouter_forwarder)
 1402         node = self._get_node(xml)
 1403         self.assertEqual(node.get("type"),
 1404                          network_model.VIF_TYPE_VHOSTUSER)
 1405 
 1406         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1407                                "source", "mode", "server")
 1408         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1409                                "source", "path", "/tmp/usv-xxx-yyy-zzz")
 1410         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1411                                "source", "type", "unix")
 1412         self._assertMacEquals(node, self.vif_vrouter_forwarder)
 1413         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
 1414 
 1415     def test_contrail_vrouter(self):
 1416         """Test for the Contrail / Tungsten Fabric DPDK datapath."""
 1417         d = vif.LibvirtGenericVIFDriver()
 1418         xml = self._get_instance_xml(d,
 1419                                      self.vif_contrail_vrouter)
 1420         node = self._get_node(xml)
 1421         self.assertEqual(node.get("type"),
 1422                          network_model.VIF_TYPE_VHOSTUSER)
 1423 
 1424         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1425                                "source", "mode", "server")
 1426         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1427                                "source", "path", "/tmp/usv-xxx-yyy-zzz")
 1428         self._assertTypeEquals(node, network_model.VIF_TYPE_VHOSTUSER,
 1429                                "source", "type", "unix")
 1430         self._assertMacEquals(node, self.vif_contrail_vrouter)
 1431         self._assertModel(xml, network_model.VIF_MODEL_VIRTIO)
 1432 
 1433     @mock.patch("nova.network.os_vif_util.nova_to_osvif_instance")
 1434     @mock.patch("nova.network.os_vif_util.nova_to_osvif_vif")
 1435     @mock.patch.object(os_vif, "plug")
 1436     def _test_osvif_plug(self, fail, mock_plug,
 1437                          mock_convert_vif, mock_convert_inst):
 1438         mock_convert_vif.return_value = self.os_vif_bridge
 1439         mock_convert_inst.return_value = self.os_vif_inst_info
 1440 
 1441         d = vif.LibvirtGenericVIFDriver()
 1442         if fail:
 1443             mock_plug.side_effect = osv_exception.ExceptionBase("Wibble")
 1444             self.assertRaises(exception.NovaException,
 1445                               d.plug,
 1446                               self.instance, self.vif_bridge)
 1447         else:
 1448             d.plug(self.instance, self.vif_bridge)
 1449 
 1450         mock_plug.assert_called_once_with(self.os_vif_bridge,
 1451                                           self.os_vif_inst_info)
 1452 
 1453     def test_osvif_plug_normal(self):
 1454         self._test_osvif_plug(False)
 1455 
 1456     def test_osvif_plug_fail(self):
 1457         self._test_osvif_plug(True)
 1458 
 1459     @mock.patch("nova.network.os_vif_util.nova_to_osvif_instance")
 1460     @mock.patch("nova.network.os_vif_util.nova_to_osvif_vif")
 1461     @mock.patch.object(os_vif, "unplug")
 1462     def _test_osvif_unplug(self, fail, mock_unplug,
 1463                          mock_convert_vif, mock_convert_inst):
 1464         mock_convert_vif.return_value = self.os_vif_bridge
 1465         mock_convert_inst.return_value = self.os_vif_inst_info
 1466 
 1467         d = vif.LibvirtGenericVIFDriver()
 1468         if fail:
 1469             mock_unplug.side_effect = osv_exception.ExceptionBase("Wibble")
 1470             self.assertRaises(exception.NovaException,
 1471                               d.unplug,
 1472                               self.instance, self.vif_bridge)
 1473         else:
 1474             d.unplug(self.instance, self.vif_bridge)
 1475 
 1476         mock_unplug.assert_called_once_with(self.os_vif_bridge,
 1477                                             self.os_vif_inst_info)
 1478 
 1479     def test_osvif_unplug_normal(self):
 1480         self._test_osvif_unplug(False)
 1481 
 1482     def test_osvif_unplug_fail(self):
 1483         self._test_osvif_unplug(True)
 1484 
 1485     @mock.patch("nova.network.os_vif_util.nova_to_osvif_instance")
 1486     @mock.patch("nova.network.os_vif_util.nova_to_osvif_vif")
 1487     def _test_config_os_vif(self, os_vif_model, vif_model,
 1488             expected_xml, mock_convert_vif,
 1489             mock_convert_inst):
 1490         mock_convert_vif.return_value = os_vif_model
 1491         mock_convert_inst.return_value = self.os_vif_inst_info
 1492 
 1493         d = vif.LibvirtGenericVIFDriver()
 1494         xml = self._get_instance_xml(d, vif_model)
 1495         node = self._get_node(xml)
 1496 
 1497         self._assertXmlEqual(expected_xml, node)
 1498 
 1499     def test_config_os_vif_bridge(self):
 1500         os_vif_type = self.os_vif_bridge
 1501         vif_type = self.vif_bridge
 1502 
 1503         expected_xml = """
 1504             <interface type="bridge">
 1505              <mac address="22:52:25:62:e2:aa"/>
 1506              <model type="virtio"/>
 1507              <source bridge="br100"/>
 1508              <mtu size="9000"/>
 1509              <target dev="nicdc065497-3c"/>
 1510              <bandwidth>
 1511               <inbound average="100" peak="200" burst="300"/>
 1512               <outbound average="10" peak="20" burst="30"/>
 1513              </bandwidth>
 1514             </interface>"""
 1515 
 1516         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1517 
 1518     def test_config_os_vif_bridge_nofw(self):
 1519         os_vif_type = self.os_vif_bridge
 1520         vif_type = self.vif_bridge
 1521 
 1522         expected_xml = """
 1523             <interface type="bridge">
 1524              <mac address="22:52:25:62:e2:aa"/>
 1525              <model type="virtio"/>
 1526              <source bridge="br100"/>
 1527              <mtu size="9000"/>
 1528              <target dev="nicdc065497-3c"/>
 1529              <bandwidth>
 1530               <inbound average="100" peak="200" burst="300"/>
 1531               <outbound average="10" peak="20" burst="30"/>
 1532              </bandwidth>
 1533             </interface>"""
 1534 
 1535         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1536 
 1537     def test_config_os_vif_vhostuser(self):
 1538         os_vif_type = self.os_vif_vhostuser
 1539         vif_type = self.vif_vhostuser
 1540 
 1541         expected_xml = """
 1542             <interface type="vhostuser">
 1543              <mac address="22:52:25:62:e2:aa"/>
 1544              <model type="virtio"/>
 1545              <source mode="client"
 1546               path="/var/run/openvswitch/vhudc065497-3c" type="unix"/>
 1547              <target dev="vhudc065497-3c"/>
 1548             </interface>"""
 1549 
 1550         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1551 
 1552     def test_config_os_vif_agilio_ovs_fallthrough(self):
 1553         os_vif_type = self.os_vif_agilio_ovs
 1554         vif_type = self.vif_agilio_ovs
 1555 
 1556         expected_xml = """
 1557             <interface type="bridge">
 1558              <mac address="22:52:25:62:e2:aa"/>
 1559              <model type="virtio"/>
 1560              <source bridge="br0"/>
 1561              <mtu size="9000"/>
 1562              <target dev="nicdc065497-3c"/>
 1563              <virtualport type="openvswitch">
 1564               <parameters
 1565               interfaceid="07bd6cea-fb37-4594-b769-90fc51854ee9"/>
 1566              </virtualport>
 1567               <bandwidth>
 1568                <inbound average="100" peak="200" burst="300"/>
 1569                <outbound average="10" peak="20" burst="30"/>
 1570               </bandwidth>
 1571             </interface>"""
 1572 
 1573         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1574 
 1575     def test_config_os_vif_agilio_ovs_forwarder(self):
 1576         os_vif_type = self.os_vif_agilio_forwarder
 1577         vif_type = self.vif_agilio_ovs_forwarder
 1578 
 1579         expected_xml = """
 1580             <interface type="vhostuser">
 1581              <mac address="22:52:25:62:e2:aa"/>
 1582              <model type="virtio"/>
 1583              <source mode="client"
 1584               path="/var/run/openvswitch/vhudc065497-3c" type="unix"/>
 1585              <target dev="nicdc065497-3c"/>
 1586             </interface>"""
 1587 
 1588         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1589 
 1590     def test_config_os_vif_agilio_ovs_direct(self):
 1591         os_vif_type = self.os_vif_agilio_direct
 1592         vif_type = self.vif_agilio_ovs_direct
 1593 
 1594         expected_xml = """
 1595             <interface type="hostdev" managed="yes">
 1596              <mac address="22:52:25:62:e2:aa"/>
 1597              <source>
 1598               <address type="pci" domain="0x0000"
 1599                bus="0x0a" slot="0x00" function="0x1"/>
 1600              </source>
 1601             </interface>"""
 1602 
 1603         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1604 
 1605     def test_config_os_vif_ovs(self):
 1606         os_vif_type = self.os_vif_ovs
 1607         vif_type = self.vif_ovs
 1608 
 1609         expected_xml = """
 1610             <interface type="bridge">
 1611              <mac address="22:52:25:62:e2:aa"/>
 1612              <model type="virtio"/>
 1613              <source bridge="br0"/>
 1614              <mtu size="9000"/>
 1615              <target dev="nicdc065497-3c"/>
 1616              <virtualport type="openvswitch">
 1617               <parameters interfaceid="07bd6cea-fb37-4594-b769-90fc51854ee9"/>
 1618              </virtualport>
 1619              <bandwidth>
 1620               <inbound average="100" peak="200" burst="300"/>
 1621               <outbound average="10" peak="20" burst="30"/>
 1622              </bandwidth>
 1623             </interface>"""
 1624 
 1625         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1626 
 1627     def test_config_os_vif_ovs_hybrid(self):
 1628         os_vif_type = self.os_vif_ovs_hybrid
 1629         vif_type = self.vif_ovs
 1630 
 1631         expected_xml = """
 1632             <interface type="bridge">
 1633              <mac address="22:52:25:62:e2:aa"/>
 1634              <model type="virtio"/>
 1635              <source bridge="br0"/>
 1636              <mtu size="9000"/>
 1637              <target dev="nicdc065497-3c"/>
 1638              <bandwidth>
 1639               <inbound average="100" peak="200" burst="300"/>
 1640               <outbound average="10" peak="20" burst="30"/>
 1641              </bandwidth>
 1642             </interface>"""
 1643 
 1644         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1645 
 1646     def test_config_os_vif_hostdevice_ethernet(self):
 1647         os_vif_type = self.os_vif_hostdevice_ethernet
 1648         vif_type = self.vif_bridge
 1649 
 1650         expected_xml = """
 1651             <interface type="hostdev" managed="yes">
 1652              <mac address="22:52:25:62:e2:aa"/>
 1653              <source>
 1654               <address type="pci" domain="0x0000"
 1655                bus="0x0a" slot="0x00" function="0x1"/>
 1656              </source>
 1657             </interface>"""
 1658 
 1659         self._test_config_os_vif(os_vif_type, vif_type, expected_xml)
 1660 
 1661     @mock.patch("nova.network.os_vif_util.nova_to_osvif_instance")
 1662     @mock.patch("nova.network.os_vif_util.nova_to_osvif_vif")
 1663     def test_config_os_vif_hostdevice_generic(self, mock_convert_vif,
 1664                                               mock_convert_inst):
 1665         mock_convert_vif.return_value = self.os_vif_hostdevice_generic
 1666         mock_convert_inst.return_value = self.os_vif_inst_info
 1667 
 1668         d = vif.LibvirtGenericVIFDriver()
 1669 
 1670         self.assertRaises(exception.InternalError,
 1671                           self._get_instance_xml, d, self.vif_bridge)