|
13 | 13 | # under the License.
|
14 | 14 |
|
15 | 15 | import ddt
|
| 16 | +from neutron_lib.api.definitions import external_net |
16 | 17 | from neutron_lib.api.definitions import portbindings
|
17 | 18 | from oslo_utils import uuidutils
|
18 | 19 | from ovsdbapp.backend.ovs_idl import event
|
@@ -321,3 +322,76 @@ def test_without_transaction(self, method, _args, _kwargs):
|
321 | 322 | def test_needed_parameters(self, method):
|
322 | 323 | self.assertRaises(RuntimeError, method, uuidutils.generate_uuid(),
|
323 | 324 | None, None)
|
| 325 | + |
| 326 | + |
| 327 | +class TestGetLogicalRouterPortHAChassis(base.TestOVNFunctionalBase): |
| 328 | + def _create_network_and_port(self): |
| 329 | + kwargs = {external_net.EXTERNAL: True, 'as_admin': True} |
| 330 | + net = self._make_network(self.fmt, 'n1', True, **kwargs)['network'] |
| 331 | + port_data = {'port': {'network_id': net['id'], |
| 332 | + 'tenant_id': self._tenant_id,}} |
| 333 | + port_req = self.new_create_request('ports', port_data, self.fmt) |
| 334 | + port_res = port_req.get_response(self.api) |
| 335 | + return self.deserialize(self.fmt, port_res)['port'] |
| 336 | + |
| 337 | + def _create_gw_chassis(self, num_chassis): |
| 338 | + chassis = [] |
| 339 | + for _ in range(num_chassis): |
| 340 | + chassis.append(self.add_fake_chassis( |
| 341 | + uuidutils.generate_uuid(), azs=[], |
| 342 | + enable_chassis_as_gw=True)) |
| 343 | + return chassis |
| 344 | + |
| 345 | + def _create_router(self, network_id): |
| 346 | + gw_info = {'network_id': network_id} |
| 347 | + router = {'router': {'name': uuidutils.generate_uuid(), |
| 348 | + 'admin_state_up': True, |
| 349 | + 'tenant_id': self._tenant_id, |
| 350 | + 'external_gateway_info': gw_info}} |
| 351 | + return self.l3_plugin.create_router(self.context, router) |
| 352 | + |
| 353 | + def _set_lrp_hcg(self, gw_port_id, hcg): |
| 354 | + lrp_name = utils.ovn_lrouter_port_name(gw_port_id) |
| 355 | + self.nb_api.db_set( |
| 356 | + 'Logical_Router_Port', lrp_name, |
| 357 | + ('ha_chassis_group', hcg.uuid)).execute() |
| 358 | + return self.nb_api.lookup('Logical_Router_Port', lrp_name) |
| 359 | + |
| 360 | + def _get_router_hcg(self, router_id): |
| 361 | + hcg_name = utils.ovn_name(router_id) |
| 362 | + return self.nb_api.lookup('HA_Chassis_Group', hcg_name) |
| 363 | + |
| 364 | + def _check_chassis(self, ha_chassis, expected_chassis, priorities=None): |
| 365 | + length = len(priorities) if priorities else len(expected_chassis) |
| 366 | + self.assertEqual(length, len(ha_chassis)) |
| 367 | + ch_priorities = set([]) |
| 368 | + for hc in ha_chassis: |
| 369 | + self.assertIn(hc[0], expected_chassis) |
| 370 | + ch_priorities.add(hc[1]) |
| 371 | + self.assertEqual(length, len(ch_priorities)) |
| 372 | + if priorities: |
| 373 | + for ch_priority in ch_priorities: |
| 374 | + self.assertIn(ch_priority, priorities) |
| 375 | + |
| 376 | + def test_get_ha_chassis(self): |
| 377 | + port = self._create_network_and_port() |
| 378 | + ch_list = self._create_gw_chassis(5) |
| 379 | + router = self._create_router(port['network_id']) |
| 380 | + hcg = self._get_router_hcg(router['id']) |
| 381 | + lrp = self._set_lrp_hcg(router['gw_port_id'], hcg) |
| 382 | + |
| 383 | + ha_chassis = utils.get_logical_router_port_ha_chassis(self.nb_api, lrp) |
| 384 | + self._check_chassis(ha_chassis, ch_list) |
| 385 | + |
| 386 | + def test_get_ha_chassis_priorities(self): |
| 387 | + port = self._create_network_and_port() |
| 388 | + ch_list = self._create_gw_chassis(5) |
| 389 | + router = self._create_router(port['network_id']) |
| 390 | + hcg = self._get_router_hcg(router['id']) |
| 391 | + lrp = self._set_lrp_hcg(router['gw_port_id'], hcg) |
| 392 | + |
| 393 | + prio = [ovn_const.HA_CHASSIS_GROUP_HIGHEST_PRIORITY, |
| 394 | + ovn_const.HA_CHASSIS_GROUP_HIGHEST_PRIORITY - 1] |
| 395 | + ha_chassis = utils.get_logical_router_port_ha_chassis( |
| 396 | + self.nb_api, lrp, priorities=prio) |
| 397 | + self._check_chassis(ha_chassis, ch_list, priorities=prio) |
0 commit comments