blob: 66cea7845967677cc4c234f182b907f5a2c25c6a [file] [log] [blame]
Daniel Mellado3c0aeab2016-01-29 11:30:25 +00001# Copyright 2013 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import netaddr
Dongcan Yea71b8342018-05-02 06:56:26 +000017
Arkady Shtemplerb667ac32020-11-17 20:23:41 +020018from neutron_lib import constants as const
19
Chandan Kumarc125fd12017-11-15 19:41:01 +053020from tempest.common import utils as tutils
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000021from tempest.lib.common.utils import data_utils
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000022from tempest.lib import decorators
Arkady Shtemplerb667ac32020-11-17 20:23:41 +020023from tempest.lib import exceptions as lib_exc
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000024
Chandan Kumar667d3d32017-09-22 12:24:06 +053025from neutron_tempest_plugin.api import base
26from neutron_tempest_plugin.api import base_routers
27from neutron_tempest_plugin.common import utils
28from neutron_tempest_plugin import config
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000029
30CONF = config.CONF
31
32
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +020033class RoutersTest(base_routers.BaseRouterTest):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000034
Jakub Libosvar1982aa12017-05-30 11:15:33 +000035 required_extensions = ['router']
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000036
37 @classmethod
38 def resource_setup(cls):
39 super(RoutersTest, cls).resource_setup()
40 cls.tenant_cidr = (
41 config.safe_get_config_value('network', 'project_network_cidr')
42 if cls._ip_version == 4 else
43 config.safe_get_config_value('network', 'project_network_v6_cidr'))
44
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000045 @decorators.idempotent_id('c72c1c0c-2193-4aca-eeee-b1442640eeee')
Chandan Kumarc125fd12017-11-15 19:41:01 +053046 @tutils.requires_ext(extension="standard-attr-description",
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000047 service="network")
48 def test_create_update_router_description(self):
49 body = self.create_router(description='d1', router_name='test')
50 self.assertEqual('d1', body['description'])
51 body = self.client.show_router(body['id'])['router']
52 self.assertEqual('d1', body['description'])
53 body = self.client.update_router(body['id'], description='d2')
54 self.assertEqual('d2', body['router']['description'])
55 body = self.client.show_router(body['router']['id'])['router']
56 self.assertEqual('d2', body['description'])
57
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000058 @decorators.idempotent_id('847257cc-6afd-4154-b8fb-af49f5670ce8')
Chandan Kumarc125fd12017-11-15 19:41:01 +053059 @tutils.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000060 def test_create_router_with_default_snat_value(self):
61 # Create a router with default snat rule
62 name = data_utils.rand_name('router')
63 router = self._create_router(
64 name, external_network_id=CONF.network.public_network_id)
65 self._verify_router_gateway(
66 router['id'], {'network_id': CONF.network.public_network_id,
67 'enable_snat': True})
68
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +000069 @decorators.idempotent_id('ea74068d-09e9-4fd7-8995-9b6a1ace920f')
Chandan Kumarc125fd12017-11-15 19:41:01 +053070 @tutils.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000071 def test_create_router_with_snat_explicit(self):
72 name = data_utils.rand_name('snat-router')
73 # Create a router enabling snat attributes
74 enable_snat_states = [False, True]
75 for enable_snat in enable_snat_states:
76 external_gateway_info = {
77 'network_id': CONF.network.public_network_id,
78 'enable_snat': enable_snat}
Brian Haleyee000852019-08-29 17:27:38 -040079 router = self._create_admin_router(
80 name, external_network_id=CONF.network.public_network_id,
81 enable_snat=enable_snat)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000082 # Verify snat attributes after router creation
Brian Haleyee000852019-08-29 17:27:38 -040083 self._verify_router_gateway(router['id'],
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000084 exp_ext_gw_info=external_gateway_info)
85
86 def _verify_router_gateway(self, router_id, exp_ext_gw_info=None):
87 show_body = self.admin_client.show_router(router_id)
88 actual_ext_gw_info = show_body['router']['external_gateway_info']
89 if exp_ext_gw_info is None:
90 self.assertIsNone(actual_ext_gw_info)
91 return
92 # Verify only keys passed in exp_ext_gw_info
fpxieaa3bace2017-04-07 17:12:15 +080093 for k, v in exp_ext_gw_info.items():
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000094 self.assertEqual(v, actual_ext_gw_info[k])
95
96 def _verify_gateway_port(self, router_id):
97 list_body = self.admin_client.list_ports(
98 network_id=CONF.network.public_network_id,
99 device_id=router_id)
100 self.assertEqual(len(list_body['ports']), 1)
101 gw_port = list_body['ports'][0]
102 fixed_ips = gw_port['fixed_ips']
103 self.assertGreaterEqual(len(fixed_ips), 1)
104 public_net_body = self.admin_client.show_network(
105 CONF.network.public_network_id)
Chandan Kumarc125fd12017-11-15 19:41:01 +0530106 public_subnet_ids = public_net_body['network']['subnets']
107 for fixed_ip in fixed_ips:
108 self.assertIn(fixed_ip['subnet_id'],
109 public_subnet_ids)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000110
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000111 @decorators.idempotent_id('b386c111-3b21-466d-880c-5e72b01e1a33')
Chandan Kumarc125fd12017-11-15 19:41:01 +0530112 @tutils.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000113 def test_update_router_set_gateway_with_snat_explicit(self):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530114 router = self._create_router(data_utils.rand_name('router'))
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000115 self.admin_client.update_router_with_snat_gw_info(
116 router['id'],
117 external_gateway_info={
118 'network_id': CONF.network.public_network_id,
119 'enable_snat': True})
120 self._verify_router_gateway(
121 router['id'],
122 {'network_id': CONF.network.public_network_id,
123 'enable_snat': True})
124 self._verify_gateway_port(router['id'])
125
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000126 @decorators.idempotent_id('96536bc7-8262-4fb2-9967-5c46940fa279')
Chandan Kumarc125fd12017-11-15 19:41:01 +0530127 @tutils.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000128 def test_update_router_set_gateway_without_snat(self):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530129 router = self._create_router(data_utils.rand_name('router'))
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000130 self.admin_client.update_router_with_snat_gw_info(
131 router['id'],
132 external_gateway_info={
133 'network_id': CONF.network.public_network_id,
134 'enable_snat': False})
135 self._verify_router_gateway(
136 router['id'],
137 {'network_id': CONF.network.public_network_id,
138 'enable_snat': False})
139 self._verify_gateway_port(router['id'])
140
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000141 @decorators.idempotent_id('f2faf994-97f4-410b-a831-9bc977b64374')
Chandan Kumarc125fd12017-11-15 19:41:01 +0530142 @tutils.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000143 def test_update_router_reset_gateway_without_snat(self):
144 router = self._create_router(
Chandan Kumarc125fd12017-11-15 19:41:01 +0530145 data_utils.rand_name('router'),
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000146 external_network_id=CONF.network.public_network_id)
147 self.admin_client.update_router_with_snat_gw_info(
148 router['id'],
149 external_gateway_info={
150 'network_id': CONF.network.public_network_id,
151 'enable_snat': False})
152 self._verify_router_gateway(
153 router['id'],
154 {'network_id': CONF.network.public_network_id,
155 'enable_snat': False})
156 self._verify_gateway_port(router['id'])
157
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000158 @decorators.idempotent_id('db3093b1-93b6-4893-be83-c4716c251b3e')
Kevin Bentonc66aa802016-07-23 22:36:37 -0700159 def test_router_interface_status(self):
160 network = self.create_network()
161 subnet = self.create_subnet(network)
162 # Add router interface with subnet id
Chandan Kumarc125fd12017-11-15 19:41:01 +0530163 router = self._create_router(data_utils.rand_name('router'), True)
Kevin Bentonc66aa802016-07-23 22:36:37 -0700164 intf = self.create_router_interface(router['id'], subnet['id'])
Brian Haley33ef4602018-04-26 14:37:49 -0400165
166 def _status_active():
167 return self.client.show_port(
168 intf['port_id'])['port']['status'] == 'ACTIVE'
169
170 utils.wait_until_true(_status_active, exception=AssertionError)
Kevin Bentonc66aa802016-07-23 22:36:37 -0700171
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000172 @decorators.idempotent_id('c86ac3a8-50bd-4b00-a6b8-62af84a0765c')
Chandan Kumarc125fd12017-11-15 19:41:01 +0530173 @tutils.requires_ext(extension='extraroute', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000174 def test_update_extra_route(self):
175 self.network = self.create_network()
176 self.name = self.network['name']
177 self.subnet = self.create_subnet(self.network)
178 # Add router interface with subnet id
179 self.router = self._create_router(
180 data_utils.rand_name('router-'), True)
181 self.create_router_interface(self.router['id'], self.subnet['id'])
182 self.addCleanup(
183 self._delete_extra_routes,
184 self.router['id'])
185 # Update router extra route, second ip of the range is
186 # used as next hop
187 cidr = netaddr.IPNetwork(self.subnet['cidr'])
188 next_hop = str(cidr[2])
189 destination = str(self.subnet['cidr'])
190 extra_route = self.client.update_extra_routes(self.router['id'],
191 next_hop, destination)
192 self.assertEqual(1, len(extra_route['router']['routes']))
193 self.assertEqual(destination,
194 extra_route['router']['routes'][0]['destination'])
195 self.assertEqual(next_hop,
196 extra_route['router']['routes'][0]['nexthop'])
197 show_body = self.client.show_router(self.router['id'])
198 self.assertEqual(destination,
199 show_body['router']['routes'][0]['destination'])
200 self.assertEqual(next_hop,
201 show_body['router']['routes'][0]['nexthop'])
202
203 def _delete_extra_routes(self, router_id):
204 self.client.delete_extra_routes(router_id)
205
Bence Romsics46bd3af2019-09-13 10:52:41 +0200206 @decorators.idempotent_id('b29d1698-d603-11e9-9c66-079cc4aec539')
207 @tutils.requires_ext(extension='extraroute-atomic', service='network')
208 def test_extra_routes_atomic(self):
209 self.network = self.create_network()
210 self.subnet = self.create_subnet(self.network)
211 self.router = self._create_router(
212 data_utils.rand_name('router-'), True)
213 self.create_router_interface(self.router['id'], self.subnet['id'])
214 self.addCleanup(
215 self._delete_extra_routes,
216 self.router['id'])
217
218 if self._ip_version == 6:
219 dst = '2001:db8:%s::/64'
220 else:
221 dst = '10.0.%s.0/24'
222
223 cidr = netaddr.IPNetwork(self.subnet['cidr'])
224
225 routes = [
226 {'destination': dst % 2, 'nexthop': cidr[2]},
227 ]
228 resp = self.client.add_extra_routes_atomic(
229 self.router['id'], routes)
230 self.assertEqual(1, len(resp['router']['routes']))
231
232 routes = [
233 {'destination': dst % 2, 'nexthop': cidr[2]},
234 {'destination': dst % 3, 'nexthop': cidr[3]},
235 ]
236 resp = self.client.add_extra_routes_atomic(
237 self.router['id'], routes)
238 self.assertEqual(2, len(resp['router']['routes']))
239
240 routes = [
241 {'destination': dst % 3, 'nexthop': cidr[3]},
242 {'destination': dst % 4, 'nexthop': cidr[4]},
243 ]
244 resp = self.client.remove_extra_routes_atomic(
245 self.router['id'], routes)
246 self.assertEqual(1, len(resp['router']['routes']))
247
248 routes = [
249 {'destination': dst % 2, 'nexthop': cidr[5]},
250 ]
251 resp = self.client.add_extra_routes_atomic(
252 self.router['id'], routes)
253 self.assertEqual(2, len(resp['router']['routes']))
254
255 routes = [
256 {'destination': dst % 2, 'nexthop': cidr[5]},
257 ]
258 resp = self.client.remove_extra_routes_atomic(
259 self.router['id'], routes)
260 self.assertEqual(1, len(resp['router']['routes']))
261
262 routes = [
263 {'destination': dst % 2, 'nexthop': cidr[2]},
264 {'destination': dst % 3, 'nexthop': cidr[3]},
265 {'destination': dst % 2, 'nexthop': cidr[5]},
266 ]
267 resp = self.client.remove_extra_routes_atomic(
268 self.router['id'], routes)
269 self.assertEqual(0, len(resp['router']['routes']))
270
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000271 @decorators.idempotent_id('01f185d1-d1a6-4cf9-abf7-e0e1384c169c')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000272 def test_network_attached_with_two_routers(self):
273 network = self.create_network(data_utils.rand_name('network1'))
274 self.create_subnet(network)
275 port1 = self.create_port(network)
276 port2 = self.create_port(network)
277 router1 = self._create_router(data_utils.rand_name('router1'))
278 router2 = self._create_router(data_utils.rand_name('router2'))
279 self.client.add_router_interface_with_port_id(
280 router1['id'], port1['id'])
281 self.client.add_router_interface_with_port_id(
282 router2['id'], port2['id'])
283 self.addCleanup(self.client.remove_router_interface_with_port_id,
284 router1['id'], port1['id'])
285 self.addCleanup(self.client.remove_router_interface_with_port_id,
286 router2['id'], port2['id'])
287 body = self.client.show_port(port1['id'])
288 port_show1 = body['port']
289 body = self.client.show_port(port2['id'])
290 port_show2 = body['port']
291 self.assertEqual(port_show1['network_id'], network['id'])
292 self.assertEqual(port_show2['network_id'], network['id'])
293 self.assertEqual(port_show1['device_id'], router1['id'])
294 self.assertEqual(port_show2['device_id'], router2['id'])
295
Rodolfo Alonso Hernandez780d81e2024-01-14 10:02:13 +0000296 @decorators.idempotent_id('4f8a2a1e-7fe9-4d99-9bff-5dc0e78b7e06')
297 def test_router_interface_update_and_remove_gateway_ip(self):
298 network = self.create_network()
299 subnet = self.create_subnet(network, allocation_pool_size=5)
300
301 # Update the subnet gateway IP, using the next one. Because the
302 # allocation pool is on the upper part of the CIDR, the lower IP
303 # addresses are free. This operation must be allowed because the subnet
304 # does not have yet a router port.
305 gateway_ip = netaddr.IPAddress(subnet['gateway_ip'])
306 self.client.update_subnet(subnet['id'], gateway_ip=str(gateway_ip + 1))
307
308 router = self._create_router(data_utils.rand_name('router'), True)
309 intf = self.create_router_interface(router['id'], subnet['id'])
310
311 def _status_active():
312 return self.client.show_port(
313 intf['port_id'])['port']['status'] == 'ACTIVE'
314
315 utils.wait_until_true(_status_active, exception=AssertionError)
316
317 # The gateway update must raise a ``GatewayIpInUse`` exception because
318 # there is an allocated router port.
319 gateway_ip = netaddr.IPAddress(subnet['gateway_ip'])
320 self.assertRaises(lib_exc.Conflict, self.client.update_subnet,
321 subnet['id'], gateway_ip=str(gateway_ip + 2))
322
323 # The gateway deletion returns the same exception.
324 gateway_ip = netaddr.IPAddress(subnet['gateway_ip'])
325 self.assertRaises(lib_exc.Conflict, self.client.update_subnet,
326 subnet['id'], gateway_ip=None)
327
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000328
Frode Nordahl395e6fa2023-10-09 17:54:13 +0200329class ExternalGWMultihomingRoutersTest(base_routers.BaseRouterTest):
330
331 @classmethod
332 def setUpClass(cls):
333 super().setUpClass()
334 ext_alias = 'external-gateway-multihoming'
335 try:
336 cls.client.get_extension(ext_alias)
337 except lib_exc.NotFound:
338 raise cls.skipException(f'{ext_alias} extension not available.')
339
340 @decorators.idempotent_id('33e9a156-a83f-435f-90ee-1a49dc9c350d')
341 def test_create_router_enable_default_route_ecmp(self):
342 router1 = self._create_admin_router(data_utils.rand_name('router1'),
343 enable_default_route_ecmp=True)
344 router2 = self._create_admin_router(data_utils.rand_name('router2'),
345 enable_default_route_ecmp=False)
346 self.assertEqual(router1['enable_default_route_ecmp'], True)
347 self.assertEqual(router2['enable_default_route_ecmp'], False)
348
349 @decorators.idempotent_id('bfbad985-2df2-4cd9-9c32-819b5508c40e')
350 def test_update_router_enable_default_route_ecmp(self):
351 router = self._create_router(data_utils.rand_name('router'))
352 updated_router = self.admin_client.update_router(
353 router['id'],
354 enable_default_route_ecmp=not router['enable_default_route_ecmp'])
355 self.assertNotEqual(
356 router['enable_default_route_ecmp'],
357 updated_router['router']['enable_default_route_ecmp'])
358
359 @decorators.idempotent_id('a22016a6-f118-4eb5-abab-7e241ae01848')
360 def test_update_router_enable_default_route_bfd(self):
361 router = self._create_router(data_utils.rand_name('router'))
362 updated_router = self.admin_client.update_router(
363 router['id'],
364 enable_default_route_bfd=not router['enable_default_route_bfd'])
365 self.assertNotEqual(
366 router['enable_default_route_bfd'],
367 updated_router['router']['enable_default_route_bfd'])
368
369 @decorators.idempotent_id('842f6edb-e072-4805-bf11-04c25420776d')
370 def test_create_router_enable_default_route_bfd(self):
371 router1 = self._create_admin_router(data_utils.rand_name('router1'),
372 enable_default_route_bfd=True)
373 router2 = self._create_admin_router(data_utils.rand_name('router2'),
374 enable_default_route_bfd=False)
375 self.assertEqual(router1['enable_default_route_bfd'], True)
376 self.assertEqual(router2['enable_default_route_bfd'], False)
377
378 @decorators.idempotent_id('089fa304-3726-4120-9759-668e8ff1114c')
379 def test_create_router_add_external_gateways_one(self):
380 router = self._create_router(data_utils.rand_name('router'))
381 self.assertEqual(len(router['external_gateways']), 0)
382
383 res = self.client.router_add_external_gateways(
384 router['id'],
385 [{'network_id': CONF.network.public_network_id,
386 'enable_snat': False}])
387 self.assertEqual(len(res['router']['external_gateways']), 1)
388 self.assertEqual(
389 res['router']['external_gateways'][0]['network_id'],
390 CONF.network.public_network_id)
391
392 @decorators.idempotent_id('60a1e7db-04ef-4a3a-9ff1-01a990d365fd')
393 def test_create_router_add_external_gateways(self):
394 router = self._create_router(data_utils.rand_name('router'))
395 self.assertEqual(len(router['external_gateways']), 0)
396
397 res = self.client.router_add_external_gateways(
398 router['id'],
399 [
400 {'network_id': CONF.network.public_network_id,
401 'enable_snat': False},
402 {'network_id': CONF.network.public_network_id,
403 'enable_snat': False},
404 {'network_id': CONF.network.public_network_id,
405 'enable_snat': False},
406 ])
407 self.assertEqual(len(res['router']['external_gateways']), 3)
408 self.assertEqual(
409 res['router']['external_gateway_info']['network_id'],
410 res['router']['external_gateways'][0]['network_id'])
411 self.assertEqual(
412 res['router']['external_gateway_info']['external_fixed_ips'],
413 res['router']['external_gateways'][0]['external_fixed_ips'])
414 for n in range(0, 3):
415 self.assertEqual(
416 res['router']['external_gateways'][n]['network_id'],
417 CONF.network.public_network_id)
418 if n:
419 self.assertNotEqual(
420 res['router']['external_gateways'][
421 n]['external_fixed_ips'],
422 res['router']['external_gateways'][
423 n - 1]['external_fixed_ips'])
424
425 @decorators.idempotent_id('e49efc57-7b25-43a3-8e55-2d87a3759c57')
426 def test_create_router_add_external_gateways_compat(self):
427 router = self._create_router(
428 data_utils.rand_name('router'),
429 external_network_id=CONF.network.public_network_id,
430 enable_snat=False)
431 self.assertEqual(len(router['external_gateways']), 1)
432 res = self.client.router_add_external_gateways(
433 router['id'],
434 [{'network_id': CONF.network.public_network_id,
435 'enable_snat': False}])
436 self.assertEqual(len(res['router']['external_gateways']), 2)
437
438 @decorators.idempotent_id('2a238eec-d9d5-435a-9013-d6e195ecd5d1')
439 def test_create_router_remove_external_gateways_compat(self):
440 router = self._create_router(
441 data_utils.rand_name('router'),
442 external_network_id=CONF.network.public_network_id,
443 enable_snat=False)
444 self.assertEqual(len(router['external_gateways']), 1)
445 res = self.client.router_remove_external_gateways(
446 router['id'],
447 [{'network_id': CONF.network.public_network_id}])
448 self.assertEqual(len(res['router']['external_gateways']), 0)
449
450 @decorators.idempotent_id('03ab196a-dac0-4363-93e4-ea799246870b')
451 def test_create_router_add_remove_external_gateways(self):
452 router = self._create_router(data_utils.rand_name('router'))
453 self.assertEqual(len(router['external_gateways']), 0)
454
455 res = self.client.router_add_external_gateways(
456 router['id'],
457 [
458 {'network_id': CONF.network.public_network_id,
459 'enable_snat': False},
460 {'network_id': CONF.network.public_network_id,
461 'enable_snat': False},
462 {'network_id': CONF.network.public_network_id,
463 'enable_snat': False},
464 ])
465 self.assertEqual(len(res['router']['external_gateways']), 3)
466 remove_gateways = [res['router']['external_gateways'][2]]
467 res = self.client.router_remove_external_gateways(router['id'],
468 remove_gateways)
469 self.assertEqual(len(res['router']['external_gateways']), 2)
470 for n in range(0, 2):
471 self.assertNotEqual(
472 res['router']['external_gateways'][
473 n]['external_fixed_ips'],
474 remove_gateways[0])
475
476 @decorators.idempotent_id('17e94c9f-c59f-4e50-abd5-d1256460e311')
477 def test_create_router_update_external_gateways(self):
478 """Add three GW ports, delete last one, re-use IPs in update on second.
479
480 NOTE(fnordahl): Main reason for IP re-use is to ensure we don't tread
481 on allocations done by other tests.
482 """
483 router = self._create_router(data_utils.rand_name('router'))
484 self.assertEqual(len(router['external_gateways']), 0)
485
486 res = self.client.router_add_external_gateways(
487 router['id'],
488 [
489 {'network_id': CONF.network.public_network_id,
490 'enable_snat': False},
491 {'network_id': CONF.network.public_network_id,
492 'enable_snat': False},
493 {'network_id': CONF.network.public_network_id,
494 'enable_snat': False},
495 ])
496 self.assertEqual(len(res['router']['external_gateways']), 3)
497 external_gateways = res['router']['external_gateways']
498 remove_gateways = [external_gateways.pop(2)]
499 res_remove_gws = self.client.router_remove_external_gateways(
500 router['id'],
501 remove_gateways)
502 for n in range(0, 2):
503 self.assertNotEqual(
504 res_remove_gws['router']['external_gateways'][
505 n]['external_fixed_ips'],
506 remove_gateways[0])
507
508 external_gateways[1] = remove_gateways[0]
509 res_update_gws = self.client.router_update_external_gateways(
510 router['id'],
511 external_gateways)
512
513 self.assertEqual(len(res_update_gws['router']['external_gateways']), 2)
514 for n in range(0, 2):
515 if res_update_gws['router']['external_gateways'][
516 n] == remove_gateways[0]:
517 break
518 else:
519 self.fail('%s not in %s' % (
520 remove_gateways[0],
521 res_update_gws['router']['external_gateways']))
522
523
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000524class RoutersIpV6Test(RoutersTest):
525 _ip_version = 6
526
527
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200528class DvrRoutersTest(base_routers.BaseRouterTest):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000529
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000530 required_extensions = ['dvr']
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000531
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000532 @decorators.idempotent_id('141297aa-3424-455d-aa8d-f2d95731e00a')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000533 def test_create_distributed_router(self):
534 name = data_utils.rand_name('router')
Brian Haleyee000852019-08-29 17:27:38 -0400535 router = self._create_admin_router(name, distributed=True)
536 self.assertTrue(router['distributed'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000537
Dongcan Yea71b8342018-05-02 06:56:26 +0000538
539class DvrRoutersTestToCentralized(base_routers.BaseRouterTest):
540
541 required_extensions = ['dvr', 'l3-ha']
542
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000543 @decorators.idempotent_id('644d7a4a-01a1-4b68-bb8d-0c0042cb1729')
Kailun Qince246d02018-07-20 21:38:07 +0800544 def test_convert_distributed_router_back_to_centralized(self):
545 # Convert a centralized router to distributed firstly
Takashi Kajinamida451772023-03-22 00:19:39 +0900546 router_args = {'tenant_id': self.client.project_id,
Brian Haley198a2d92017-06-16 16:23:44 -0400547 'distributed': False, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400548 router = self._create_admin_router(
Brian Haley198a2d92017-06-16 16:23:44 -0400549 data_utils.rand_name('router'), admin_state_up=False,
Brian Haleyee000852019-08-29 17:27:38 -0400550 **router_args)
Brian Haley198a2d92017-06-16 16:23:44 -0400551 self.assertFalse(router['distributed'])
552 self.assertFalse(router['ha'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000553 update_body = self.admin_client.update_router(router['id'],
554 distributed=True)
555 self.assertTrue(update_body['router']['distributed'])
556 show_body = self.admin_client.show_router(router['id'])
557 self.assertTrue(show_body['router']['distributed'])
Kailun Qince246d02018-07-20 21:38:07 +0800558 self.assertFalse(show_body['router']['ha'])
559 # Then convert the distributed router back to centralized
560 update_body = self.admin_client.update_router(router['id'],
561 distributed=False)
562 self.assertFalse(update_body['router']['distributed'])
563 show_body = self.admin_client.show_router(router['id'])
564 self.assertFalse(show_body['router']['distributed'])
565 self.assertFalse(show_body['router']['ha'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000566 show_body = self.client.show_router(router['id'])
567 self.assertNotIn('distributed', show_body['router'])
Brian Haley198a2d92017-06-16 16:23:44 -0400568 self.assertNotIn('ha', show_body['router'])
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200569
570
Matt Welchffe9be82019-07-02 13:24:29 +0000571class DvrRoutersTestUpdateDistributedExtended(base_routers.BaseRouterTest):
572
573 required_extensions = ['dvr', 'l3-ha',
574 'router-admin-state-down-before-update']
575
576 @decorators.idempotent_id('0ffb9973-0c1a-4b76-a1f2-060178057661')
577 def test_convert_centralized_router_to_distributed_extended(self):
Takashi Kajinamida451772023-03-22 00:19:39 +0900578 router_args = {'tenant_id': self.client.project_id,
Matt Welchffe9be82019-07-02 13:24:29 +0000579 'distributed': False, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400580 router = self._create_admin_router(
Matt Welchffe9be82019-07-02 13:24:29 +0000581 data_utils.rand_name('router'), admin_state_up=True,
Brian Haleyee000852019-08-29 17:27:38 -0400582 **router_args)
Matt Welchffe9be82019-07-02 13:24:29 +0000583 self.assertTrue(router['admin_state_up'])
584 self.assertFalse(router['distributed'])
585 # take router down to allow setting the router to distributed
586 update_body = self.admin_client.update_router(router['id'],
587 admin_state_up=False)
588 self.assertFalse(update_body['router']['admin_state_up'])
589 # set the router to distributed
590 update_body = self.admin_client.update_router(router['id'],
591 distributed=True)
592 self.assertTrue(update_body['router']['distributed'])
593 # bring the router back up
594 update_body = self.admin_client.update_router(router['id'],
595 admin_state_up=True)
596 self.assertTrue(update_body['router']['admin_state_up'])
597 self.assertTrue(update_body['router']['distributed'])
598
599 @decorators.idempotent_id('e9a8f55b-c535-44b7-8b0a-20af6a7c2921')
600 def test_convert_distributed_router_to_centralized_extended(self):
Takashi Kajinamida451772023-03-22 00:19:39 +0900601 router_args = {'tenant_id': self.client.project_id,
Matt Welchffe9be82019-07-02 13:24:29 +0000602 'distributed': True, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400603 router = self._create_admin_router(
Matt Welchffe9be82019-07-02 13:24:29 +0000604 data_utils.rand_name('router'), admin_state_up=True,
Brian Haleyee000852019-08-29 17:27:38 -0400605 **router_args)
Matt Welchffe9be82019-07-02 13:24:29 +0000606 self.assertTrue(router['admin_state_up'])
607 self.assertTrue(router['distributed'])
608 # take router down to allow setting the router to centralized
609 update_body = self.admin_client.update_router(router['id'],
610 admin_state_up=False)
611 self.assertFalse(update_body['router']['admin_state_up'])
612 # set router to centralized
613 update_body = self.admin_client.update_router(router['id'],
614 distributed=False)
615 self.assertFalse(update_body['router']['distributed'])
616 # bring router back up
617 update_body = self.admin_client.update_router(router['id'],
618 admin_state_up=True)
619 self.assertTrue(update_body['router']['admin_state_up'])
620 self.assertFalse(update_body['router']['distributed'])
621
622
Dongcan Ye6d8ec4a2017-05-03 15:07:36 +0800623class HaRoutersTest(base_routers.BaseRouterTest):
624
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000625 required_extensions = ['l3-ha']
Dongcan Ye6d8ec4a2017-05-03 15:07:36 +0800626
627 @decorators.idempotent_id('77db8eae-3aa3-4e61-bf2a-e739ce042e53')
628 def test_convert_legacy_router(self):
629 router = self._create_router(data_utils.rand_name('router'))
630 self.assertNotIn('ha', router)
631 update_body = self.admin_client.update_router(router['id'],
632 ha=True)
633 self.assertTrue(update_body['router']['ha'])
634 show_body = self.admin_client.show_router(router['id'])
635 self.assertTrue(show_body['router']['ha'])
636 show_body = self.client.show_router(router['id'])
637 self.assertNotIn('ha', show_body['router'])
638
639
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200640class RoutersSearchCriteriaTest(base.BaseSearchCriteriaTest):
641
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000642 required_extensions = ['router']
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200643 resource = 'router'
644
645 @classmethod
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200646 def resource_setup(cls):
647 super(RoutersSearchCriteriaTest, cls).resource_setup()
648 for name in cls.resource_names:
649 cls.create_router(router_name=name)
650
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000651 @decorators.idempotent_id('03a69efb-90a7-435b-bb5c-3add3612085a')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200652 def test_list_sorts_asc(self):
653 self._test_list_sorts_asc()
654
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000655 @decorators.idempotent_id('95913d30-ff41-4b17-9f44-5258c651e78c')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200656 def test_list_sorts_desc(self):
657 self._test_list_sorts_desc()
658
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000659 @decorators.idempotent_id('7f7d40b1-e165-4817-8dc5-02f8e2f0dff3')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200660 def test_list_pagination(self):
661 self._test_list_pagination()
662
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000663 @decorators.idempotent_id('a5b83e83-3d98-45bb-a2c7-0ee179ffd42c')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200664 def test_list_pagination_with_marker(self):
665 self._test_list_pagination_with_marker()
666
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000667 @decorators.idempotent_id('40804af8-c25d-45f8-b8a8-b4c70345215d')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200668 def test_list_pagination_with_href_links(self):
669 self._test_list_pagination_with_href_links()
670
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000671 @decorators.idempotent_id('77b9676c-d3cb-43af-a0e8-a5b8c6099e70')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200672 def test_list_pagination_page_reverse_asc(self):
673 self._test_list_pagination_page_reverse_asc()
674
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000675 @decorators.idempotent_id('3133a2c5-1bb9-4fc7-833e-cf9a1d160255')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200676 def test_list_pagination_page_reverse_desc(self):
677 self._test_list_pagination_page_reverse_desc()
678
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000679 @decorators.idempotent_id('8252e2f0-b3da-4738-8e25-f6f8d878a2da')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200680 def test_list_pagination_page_reverse_with_href_links(self):
681 self._test_list_pagination_page_reverse_with_href_links()
682
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000683 @decorators.idempotent_id('fb102124-20f8-4cb3-8c81-f16f5e41d192')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200684 def test_list_no_pagination_limit_0(self):
685 self._test_list_no_pagination_limit_0()
Arkady Shtemplerb667ac32020-11-17 20:23:41 +0200686
687
688class RoutersDeleteTest(base_routers.BaseRouterTest):
689 """The only test in this class is a test that removes router!
690
691 * We cannot delete common and mandatory resources (router in this case)
692 * using the existing classes, as it will cause failure in other tests
693 * running in parallel.
694 """
695 @classmethod
696 def resource_setup(cls):
697 super(RoutersDeleteTest, cls).resource_setup()
698 cls.secgroup = cls.create_security_group(
699 name=data_utils.rand_name("test_port_secgroup"))
700 router_kwargs = {
701 'router_name': data_utils.rand_name('router_to_delete'),
702 'external_network_id': CONF.network.public_network_id}
703 cls.router = cls.create_router(**router_kwargs)
704
705 @decorators.idempotent_id('dbbc5c74-63c8-11eb-8881-74e5f9e2a801')
706 def test_delete_router(self):
707 # Create a port on tenant network and associate to the router.
708 # Try to delete router. Expected result: "Conflict Error" is raised.
709 network = self.create_network()
710 subnet = self.create_subnet(network)
711 self.create_router_interface(self.router['id'], subnet['id'])
712 port = self.create_port(
713 network, name=data_utils.rand_name("port"),
714 security_groups=[self.secgroup['id']])
715 self.create_floatingip(port=port)
716 self.assertRaises(
717 lib_exc.Conflict, self.client.delete_router, self.router['id'])
718 # Delete the associated port
719 # Try to delete router. Expected result: "Conflict Error" is raised.
720 # Note: there are still interfaces in use.
721 self.client.delete_port(port['id'])
722 self.assertRaises(
723 lib_exc.Conflict, self.client.delete_router, self.router['id'])
724 # Delete the rest of the router's ports
725 # Try to delete router. Expected result: "PASS"
726 interfaces = [
727 port for port in self.client.list_router_interfaces(
728 self.router['id'])['ports']
729 if port['device_owner'] in const.ROUTER_INTERFACE_OWNERS]
730 for i in interfaces:
731 try:
732 self.assertRaises(
733 lib_exc.Conflict, self.client.delete_router,
734 self.router['id'])
735 self.client.remove_router_interface_with_subnet_id(
736 self.router['id'], i['fixed_ips'][0]['subnet_id'])
737 except lib_exc.NotFound:
738 pass
739 self.client.delete_router(self.router['id'])