1414
1515import contextlib
1616
17- from neutron .services .trunk import plugin as trunk_plugin
18- from neutron .tests .functional import base
17+ from neutron_lib .api .definitions import portbindings
1918from neutron_lib import constants as n_consts
20- from neutron_lib .objects import registry as obj_reg
19+ from neutron_lib .db import api as db_api
2120from neutron_lib .plugins import utils
2221from neutron_lib .services .trunk import constants as trunk_consts
2322from oslo_utils import uuidutils
2423
2524from neutron .common .ovn import constants as ovn_const
25+ from neutron .objects import ports as port_obj
26+ from neutron .services .trunk import plugin as trunk_plugin
27+ from neutron .tests .functional import base
2628
2729
2830class TestOVNTrunkDriver (base .TestOVNFunctionalBase ):
2931
30- def setUp (self ):
31- super (TestOVNTrunkDriver , self ).setUp ()
32+ def setUp (self , ** kwargs ):
33+ super ().setUp (** kwargs )
3234 self .trunk_plugin = trunk_plugin .TrunkPlugin ()
3335 self .trunk_plugin .add_segmentation_type (
3436 trunk_consts .SEGMENTATION_TYPE_VLAN ,
@@ -39,7 +41,8 @@ def trunk(self, sub_ports=None):
3941 sub_ports = sub_ports or []
4042 with self .network () as network :
4143 with self .subnet (network = network ) as subnet :
42- with self .port (subnet = subnet ) as parent_port :
44+ with self .port (subnet = subnet ,
45+ device_owner = 'compute:nova' ) as parent_port :
4346 tenant_id = uuidutils .generate_uuid ()
4447 trunk = {'trunk' : {
4548 'port_id' : parent_port ['port' ]['id' ],
@@ -64,17 +67,14 @@ def _get_ovn_trunk_info(self):
6467 if row .parent_name and row .tag :
6568 device_owner = row .external_ids [
6669 ovn_const .OVN_DEVICE_OWNER_EXT_ID_KEY ]
67- revision_number = row .external_ids [
68- ovn_const .OVN_REV_NUM_EXT_ID_KEY ]
6970 ovn_trunk_info .append ({'port_id' : row .name ,
7071 'parent_port_id' : row .parent_name ,
7172 'tag' : row .tag ,
7273 'device_owner' : device_owner ,
73- 'revision_number' : revision_number ,
7474 })
7575 return ovn_trunk_info
7676
77- def _verify_trunk_info (self , trunk , has_items ):
77+ def _verify_trunk_info (self , trunk , has_items , host = '' ):
7878 ovn_subports_info = self ._get_ovn_trunk_info ()
7979 neutron_subports_info = []
8080 for subport in trunk .get ('sub_ports' , []):
@@ -83,19 +83,27 @@ def _verify_trunk_info(self, trunk, has_items):
8383 'parent_port_id' : [trunk ['port_id' ]],
8484 'tag' : [subport ['segmentation_id' ]],
8585 'device_owner' : trunk_consts .TRUNK_SUBPORT_OWNER ,
86- 'revision_number' : '2' ,
8786 })
88- # Check that the subport has the binding is active.
89- binding = obj_reg .load_class ('PortBinding' ).get_object (
90- self .context , port_id = subport ['port_id' ], host = '' )
91- self .assertEqual (n_consts .PORT_STATUS_ACTIVE , binding ['status' ])
87+ # Check the subport binding.
88+ pb = port_obj .PortBinding .get_object (
89+ self .context , port_id = subport ['port_id' ], host = host )
90+ self .assertEqual (n_consts .PORT_STATUS_ACTIVE , pb .status )
91+ self .assertEqual (host , pb .host )
9292
9393 self .assertCountEqual (ovn_subports_info , neutron_subports_info )
9494 self .assertEqual (has_items , len (neutron_subports_info ) != 0 )
9595
9696 if trunk .get ('status' ):
9797 self .assertEqual (trunk_consts .TRUNK_ACTIVE_STATUS , trunk ['status' ])
9898
99+ def _bind_port (self , port_id , host ):
100+ with db_api .CONTEXT_WRITER .using (self .context ):
101+ pb = port_obj .PortBinding .get_object (self .context ,
102+ port_id = port_id , host = '' )
103+ pb .delete ()
104+ port_obj .PortBinding (self .context , port_id = port_id , host = host ,
105+ vif_type = portbindings .VIF_TYPE_OVS ).create ()
106+
99107 def test_trunk_create (self ):
100108 with self .trunk () as trunk :
101109 self ._verify_trunk_info (trunk , has_items = False )
@@ -113,10 +121,22 @@ def test_subport_add(self):
113121 new_trunk = self .trunk_plugin .get_trunk (self .context ,
114122 trunk ['id' ])
115123 self ._verify_trunk_info (new_trunk , has_items = True )
124+ # Bind parent port. That will trigger the binding of the
125+ # trunk subports too, using the same host ID.
126+ self ._bind_port (trunk ['port_id' ], 'host1' )
127+ self .mech_driver .set_port_status_up (trunk ['port_id' ])
128+ self ._verify_trunk_info (new_trunk , has_items = True ,
129+ host = 'host1' )
116130
117131 def test_subport_delete (self ):
118132 with self .subport () as subport :
119133 with self .trunk ([subport ]) as trunk :
134+ # Bind parent port.
135+ self ._bind_port (trunk ['port_id' ], 'host1' )
136+ self .mech_driver .set_port_status_up (trunk ['port_id' ])
137+ self ._verify_trunk_info (trunk , has_items = True ,
138+ host = 'host1' )
139+
120140 self .trunk_plugin .remove_subports (self .context , trunk ['id' ],
121141 {'sub_ports' : [subport ]})
122142 new_trunk = self .trunk_plugin .get_trunk (self .context ,
0 commit comments