blob: 7758b1a402d30caa18cadac325c4047cf59f43b5 [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
Frode Nordahl1bb8e622023-10-16 15:16:34 +0200332 @tutils.requires_ext(extension="external-gateway-multihoming",
333 service="network")
Frode Nordahl395e6fa2023-10-09 17:54:13 +0200334 def setUpClass(cls):
335 super().setUpClass()
Frode Nordahl395e6fa2023-10-09 17:54:13 +0200336
337 @decorators.idempotent_id('33e9a156-a83f-435f-90ee-1a49dc9c350d')
338 def test_create_router_enable_default_route_ecmp(self):
339 router1 = self._create_admin_router(data_utils.rand_name('router1'),
340 enable_default_route_ecmp=True)
341 router2 = self._create_admin_router(data_utils.rand_name('router2'),
342 enable_default_route_ecmp=False)
343 self.assertEqual(router1['enable_default_route_ecmp'], True)
344 self.assertEqual(router2['enable_default_route_ecmp'], False)
345
346 @decorators.idempotent_id('bfbad985-2df2-4cd9-9c32-819b5508c40e')
347 def test_update_router_enable_default_route_ecmp(self):
348 router = self._create_router(data_utils.rand_name('router'))
349 updated_router = self.admin_client.update_router(
350 router['id'],
351 enable_default_route_ecmp=not router['enable_default_route_ecmp'])
352 self.assertNotEqual(
353 router['enable_default_route_ecmp'],
354 updated_router['router']['enable_default_route_ecmp'])
355
356 @decorators.idempotent_id('a22016a6-f118-4eb5-abab-7e241ae01848')
357 def test_update_router_enable_default_route_bfd(self):
358 router = self._create_router(data_utils.rand_name('router'))
359 updated_router = self.admin_client.update_router(
360 router['id'],
361 enable_default_route_bfd=not router['enable_default_route_bfd'])
362 self.assertNotEqual(
363 router['enable_default_route_bfd'],
364 updated_router['router']['enable_default_route_bfd'])
365
366 @decorators.idempotent_id('842f6edb-e072-4805-bf11-04c25420776d')
367 def test_create_router_enable_default_route_bfd(self):
368 router1 = self._create_admin_router(data_utils.rand_name('router1'),
369 enable_default_route_bfd=True)
370 router2 = self._create_admin_router(data_utils.rand_name('router2'),
371 enable_default_route_bfd=False)
372 self.assertEqual(router1['enable_default_route_bfd'], True)
373 self.assertEqual(router2['enable_default_route_bfd'], False)
374
375 @decorators.idempotent_id('089fa304-3726-4120-9759-668e8ff1114c')
376 def test_create_router_add_external_gateways_one(self):
377 router = self._create_router(data_utils.rand_name('router'))
378 self.assertEqual(len(router['external_gateways']), 0)
379
380 res = self.client.router_add_external_gateways(
381 router['id'],
382 [{'network_id': CONF.network.public_network_id,
383 'enable_snat': False}])
384 self.assertEqual(len(res['router']['external_gateways']), 1)
385 self.assertEqual(
386 res['router']['external_gateways'][0]['network_id'],
387 CONF.network.public_network_id)
388
389 @decorators.idempotent_id('60a1e7db-04ef-4a3a-9ff1-01a990d365fd')
390 def test_create_router_add_external_gateways(self):
391 router = self._create_router(data_utils.rand_name('router'))
392 self.assertEqual(len(router['external_gateways']), 0)
393
394 res = self.client.router_add_external_gateways(
395 router['id'],
396 [
397 {'network_id': CONF.network.public_network_id,
398 'enable_snat': False},
399 {'network_id': CONF.network.public_network_id,
400 'enable_snat': False},
401 {'network_id': CONF.network.public_network_id,
402 'enable_snat': False},
403 ])
404 self.assertEqual(len(res['router']['external_gateways']), 3)
405 self.assertEqual(
406 res['router']['external_gateway_info']['network_id'],
407 res['router']['external_gateways'][0]['network_id'])
408 self.assertEqual(
409 res['router']['external_gateway_info']['external_fixed_ips'],
410 res['router']['external_gateways'][0]['external_fixed_ips'])
411 for n in range(0, 3):
412 self.assertEqual(
413 res['router']['external_gateways'][n]['network_id'],
414 CONF.network.public_network_id)
415 if n:
416 self.assertNotEqual(
417 res['router']['external_gateways'][
418 n]['external_fixed_ips'],
419 res['router']['external_gateways'][
420 n - 1]['external_fixed_ips'])
421
422 @decorators.idempotent_id('e49efc57-7b25-43a3-8e55-2d87a3759c57')
423 def test_create_router_add_external_gateways_compat(self):
424 router = self._create_router(
425 data_utils.rand_name('router'),
426 external_network_id=CONF.network.public_network_id,
427 enable_snat=False)
428 self.assertEqual(len(router['external_gateways']), 1)
429 res = self.client.router_add_external_gateways(
430 router['id'],
431 [{'network_id': CONF.network.public_network_id,
432 'enable_snat': False}])
433 self.assertEqual(len(res['router']['external_gateways']), 2)
434
435 @decorators.idempotent_id('2a238eec-d9d5-435a-9013-d6e195ecd5d1')
436 def test_create_router_remove_external_gateways_compat(self):
437 router = self._create_router(
438 data_utils.rand_name('router'),
439 external_network_id=CONF.network.public_network_id,
440 enable_snat=False)
441 self.assertEqual(len(router['external_gateways']), 1)
442 res = self.client.router_remove_external_gateways(
443 router['id'],
444 [{'network_id': CONF.network.public_network_id}])
445 self.assertEqual(len(res['router']['external_gateways']), 0)
446
447 @decorators.idempotent_id('03ab196a-dac0-4363-93e4-ea799246870b')
448 def test_create_router_add_remove_external_gateways(self):
449 router = self._create_router(data_utils.rand_name('router'))
450 self.assertEqual(len(router['external_gateways']), 0)
451
452 res = self.client.router_add_external_gateways(
453 router['id'],
454 [
455 {'network_id': CONF.network.public_network_id,
456 'enable_snat': False},
457 {'network_id': CONF.network.public_network_id,
458 'enable_snat': False},
459 {'network_id': CONF.network.public_network_id,
460 'enable_snat': False},
461 ])
462 self.assertEqual(len(res['router']['external_gateways']), 3)
463 remove_gateways = [res['router']['external_gateways'][2]]
464 res = self.client.router_remove_external_gateways(router['id'],
465 remove_gateways)
466 self.assertEqual(len(res['router']['external_gateways']), 2)
467 for n in range(0, 2):
468 self.assertNotEqual(
469 res['router']['external_gateways'][
470 n]['external_fixed_ips'],
471 remove_gateways[0])
472
473 @decorators.idempotent_id('17e94c9f-c59f-4e50-abd5-d1256460e311')
474 def test_create_router_update_external_gateways(self):
475 """Add three GW ports, delete last one, re-use IPs in update on second.
476
477 NOTE(fnordahl): Main reason for IP re-use is to ensure we don't tread
478 on allocations done by other tests.
479 """
480 router = self._create_router(data_utils.rand_name('router'))
481 self.assertEqual(len(router['external_gateways']), 0)
482
483 res = self.client.router_add_external_gateways(
484 router['id'],
485 [
486 {'network_id': CONF.network.public_network_id,
487 'enable_snat': False},
488 {'network_id': CONF.network.public_network_id,
489 'enable_snat': False},
490 {'network_id': CONF.network.public_network_id,
491 'enable_snat': False},
492 ])
493 self.assertEqual(len(res['router']['external_gateways']), 3)
494 external_gateways = res['router']['external_gateways']
495 remove_gateways = [external_gateways.pop(2)]
496 res_remove_gws = self.client.router_remove_external_gateways(
497 router['id'],
498 remove_gateways)
499 for n in range(0, 2):
500 self.assertNotEqual(
501 res_remove_gws['router']['external_gateways'][
502 n]['external_fixed_ips'],
503 remove_gateways[0])
504
505 external_gateways[1] = remove_gateways[0]
506 res_update_gws = self.client.router_update_external_gateways(
507 router['id'],
508 external_gateways)
509
510 self.assertEqual(len(res_update_gws['router']['external_gateways']), 2)
511 for n in range(0, 2):
512 if res_update_gws['router']['external_gateways'][
513 n] == remove_gateways[0]:
514 break
515 else:
516 self.fail('%s not in %s' % (
517 remove_gateways[0],
518 res_update_gws['router']['external_gateways']))
519
520
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000521class RoutersIpV6Test(RoutersTest):
522 _ip_version = 6
523
524
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200525class DvrRoutersTest(base_routers.BaseRouterTest):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000526
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000527 required_extensions = ['dvr']
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000528
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000529 @decorators.idempotent_id('141297aa-3424-455d-aa8d-f2d95731e00a')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000530 def test_create_distributed_router(self):
531 name = data_utils.rand_name('router')
Brian Haleyee000852019-08-29 17:27:38 -0400532 router = self._create_admin_router(name, distributed=True)
533 self.assertTrue(router['distributed'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000534
Dongcan Yea71b8342018-05-02 06:56:26 +0000535
536class DvrRoutersTestToCentralized(base_routers.BaseRouterTest):
537
538 required_extensions = ['dvr', 'l3-ha']
539
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000540 @decorators.idempotent_id('644d7a4a-01a1-4b68-bb8d-0c0042cb1729')
Kailun Qince246d02018-07-20 21:38:07 +0800541 def test_convert_distributed_router_back_to_centralized(self):
542 # Convert a centralized router to distributed firstly
Takashi Kajinamida451772023-03-22 00:19:39 +0900543 router_args = {'tenant_id': self.client.project_id,
Brian Haley198a2d92017-06-16 16:23:44 -0400544 'distributed': False, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400545 router = self._create_admin_router(
Brian Haley198a2d92017-06-16 16:23:44 -0400546 data_utils.rand_name('router'), admin_state_up=False,
Brian Haleyee000852019-08-29 17:27:38 -0400547 **router_args)
Brian Haley198a2d92017-06-16 16:23:44 -0400548 self.assertFalse(router['distributed'])
549 self.assertFalse(router['ha'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000550 update_body = self.admin_client.update_router(router['id'],
551 distributed=True)
552 self.assertTrue(update_body['router']['distributed'])
553 show_body = self.admin_client.show_router(router['id'])
554 self.assertTrue(show_body['router']['distributed'])
Kailun Qince246d02018-07-20 21:38:07 +0800555 self.assertFalse(show_body['router']['ha'])
556 # Then convert the distributed router back to centralized
557 update_body = self.admin_client.update_router(router['id'],
558 distributed=False)
559 self.assertFalse(update_body['router']['distributed'])
560 show_body = self.admin_client.show_router(router['id'])
561 self.assertFalse(show_body['router']['distributed'])
562 self.assertFalse(show_body['router']['ha'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000563 show_body = self.client.show_router(router['id'])
564 self.assertNotIn('distributed', show_body['router'])
Brian Haley198a2d92017-06-16 16:23:44 -0400565 self.assertNotIn('ha', show_body['router'])
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200566
567
Matt Welchffe9be82019-07-02 13:24:29 +0000568class DvrRoutersTestUpdateDistributedExtended(base_routers.BaseRouterTest):
569
570 required_extensions = ['dvr', 'l3-ha',
571 'router-admin-state-down-before-update']
572
573 @decorators.idempotent_id('0ffb9973-0c1a-4b76-a1f2-060178057661')
574 def test_convert_centralized_router_to_distributed_extended(self):
Takashi Kajinamida451772023-03-22 00:19:39 +0900575 router_args = {'tenant_id': self.client.project_id,
Matt Welchffe9be82019-07-02 13:24:29 +0000576 'distributed': False, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400577 router = self._create_admin_router(
Matt Welchffe9be82019-07-02 13:24:29 +0000578 data_utils.rand_name('router'), admin_state_up=True,
Brian Haleyee000852019-08-29 17:27:38 -0400579 **router_args)
Matt Welchffe9be82019-07-02 13:24:29 +0000580 self.assertTrue(router['admin_state_up'])
581 self.assertFalse(router['distributed'])
582 # take router down to allow setting the router to distributed
583 update_body = self.admin_client.update_router(router['id'],
584 admin_state_up=False)
585 self.assertFalse(update_body['router']['admin_state_up'])
586 # set the router to distributed
587 update_body = self.admin_client.update_router(router['id'],
588 distributed=True)
589 self.assertTrue(update_body['router']['distributed'])
590 # bring the router back up
591 update_body = self.admin_client.update_router(router['id'],
592 admin_state_up=True)
593 self.assertTrue(update_body['router']['admin_state_up'])
594 self.assertTrue(update_body['router']['distributed'])
595
596 @decorators.idempotent_id('e9a8f55b-c535-44b7-8b0a-20af6a7c2921')
597 def test_convert_distributed_router_to_centralized_extended(self):
Takashi Kajinamida451772023-03-22 00:19:39 +0900598 router_args = {'tenant_id': self.client.project_id,
Matt Welchffe9be82019-07-02 13:24:29 +0000599 'distributed': True, 'ha': False}
Brian Haleyee000852019-08-29 17:27:38 -0400600 router = self._create_admin_router(
Matt Welchffe9be82019-07-02 13:24:29 +0000601 data_utils.rand_name('router'), admin_state_up=True,
Brian Haleyee000852019-08-29 17:27:38 -0400602 **router_args)
Matt Welchffe9be82019-07-02 13:24:29 +0000603 self.assertTrue(router['admin_state_up'])
604 self.assertTrue(router['distributed'])
605 # take router down to allow setting the router to centralized
606 update_body = self.admin_client.update_router(router['id'],
607 admin_state_up=False)
608 self.assertFalse(update_body['router']['admin_state_up'])
609 # set router to centralized
610 update_body = self.admin_client.update_router(router['id'],
611 distributed=False)
612 self.assertFalse(update_body['router']['distributed'])
613 # bring router back up
614 update_body = self.admin_client.update_router(router['id'],
615 admin_state_up=True)
616 self.assertTrue(update_body['router']['admin_state_up'])
617 self.assertFalse(update_body['router']['distributed'])
618
619
Dongcan Ye6d8ec4a2017-05-03 15:07:36 +0800620class HaRoutersTest(base_routers.BaseRouterTest):
621
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000622 required_extensions = ['l3-ha']
Dongcan Ye6d8ec4a2017-05-03 15:07:36 +0800623
624 @decorators.idempotent_id('77db8eae-3aa3-4e61-bf2a-e739ce042e53')
625 def test_convert_legacy_router(self):
626 router = self._create_router(data_utils.rand_name('router'))
627 self.assertNotIn('ha', router)
628 update_body = self.admin_client.update_router(router['id'],
629 ha=True)
630 self.assertTrue(update_body['router']['ha'])
631 show_body = self.admin_client.show_router(router['id'])
632 self.assertTrue(show_body['router']['ha'])
633 show_body = self.client.show_router(router['id'])
634 self.assertNotIn('ha', show_body['router'])
635
636
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200637class RoutersSearchCriteriaTest(base.BaseSearchCriteriaTest):
638
Jakub Libosvar1982aa12017-05-30 11:15:33 +0000639 required_extensions = ['router']
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200640 resource = 'router'
641
642 @classmethod
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200643 def resource_setup(cls):
644 super(RoutersSearchCriteriaTest, cls).resource_setup()
645 for name in cls.resource_names:
646 cls.create_router(router_name=name)
647
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000648 @decorators.idempotent_id('03a69efb-90a7-435b-bb5c-3add3612085a')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200649 def test_list_sorts_asc(self):
650 self._test_list_sorts_asc()
651
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000652 @decorators.idempotent_id('95913d30-ff41-4b17-9f44-5258c651e78c')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200653 def test_list_sorts_desc(self):
654 self._test_list_sorts_desc()
655
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000656 @decorators.idempotent_id('7f7d40b1-e165-4817-8dc5-02f8e2f0dff3')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200657 def test_list_pagination(self):
658 self._test_list_pagination()
659
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000660 @decorators.idempotent_id('a5b83e83-3d98-45bb-a2c7-0ee179ffd42c')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200661 def test_list_pagination_with_marker(self):
662 self._test_list_pagination_with_marker()
663
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000664 @decorators.idempotent_id('40804af8-c25d-45f8-b8a8-b4c70345215d')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200665 def test_list_pagination_with_href_links(self):
666 self._test_list_pagination_with_href_links()
667
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000668 @decorators.idempotent_id('77b9676c-d3cb-43af-a0e8-a5b8c6099e70')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200669 def test_list_pagination_page_reverse_asc(self):
670 self._test_list_pagination_page_reverse_asc()
671
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000672 @decorators.idempotent_id('3133a2c5-1bb9-4fc7-833e-cf9a1d160255')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200673 def test_list_pagination_page_reverse_desc(self):
674 self._test_list_pagination_page_reverse_desc()
675
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000676 @decorators.idempotent_id('8252e2f0-b3da-4738-8e25-f6f8d878a2da')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200677 def test_list_pagination_page_reverse_with_href_links(self):
678 self._test_list_pagination_page_reverse_with_href_links()
679
Sławek Kapłońskic0caa2e2017-02-25 10:11:32 +0000680 @decorators.idempotent_id('fb102124-20f8-4cb3-8c81-f16f5e41d192')
Ihar Hrachyshka44d1d3f2016-06-14 11:51:37 +0200681 def test_list_no_pagination_limit_0(self):
682 self._test_list_no_pagination_limit_0()
Arkady Shtemplerb667ac32020-11-17 20:23:41 +0200683
684
685class RoutersDeleteTest(base_routers.BaseRouterTest):
686 """The only test in this class is a test that removes router!
687
688 * We cannot delete common and mandatory resources (router in this case)
689 * using the existing classes, as it will cause failure in other tests
690 * running in parallel.
691 """
692 @classmethod
693 def resource_setup(cls):
694 super(RoutersDeleteTest, cls).resource_setup()
695 cls.secgroup = cls.create_security_group(
696 name=data_utils.rand_name("test_port_secgroup"))
697 router_kwargs = {
698 'router_name': data_utils.rand_name('router_to_delete'),
699 'external_network_id': CONF.network.public_network_id}
700 cls.router = cls.create_router(**router_kwargs)
701
702 @decorators.idempotent_id('dbbc5c74-63c8-11eb-8881-74e5f9e2a801')
703 def test_delete_router(self):
704 # Create a port on tenant network and associate to the router.
705 # Try to delete router. Expected result: "Conflict Error" is raised.
706 network = self.create_network()
707 subnet = self.create_subnet(network)
708 self.create_router_interface(self.router['id'], subnet['id'])
709 port = self.create_port(
710 network, name=data_utils.rand_name("port"),
711 security_groups=[self.secgroup['id']])
712 self.create_floatingip(port=port)
713 self.assertRaises(
714 lib_exc.Conflict, self.client.delete_router, self.router['id'])
715 # Delete the associated port
716 # Try to delete router. Expected result: "Conflict Error" is raised.
717 # Note: there are still interfaces in use.
718 self.client.delete_port(port['id'])
719 self.assertRaises(
720 lib_exc.Conflict, self.client.delete_router, self.router['id'])
721 # Delete the rest of the router's ports
722 # Try to delete router. Expected result: "PASS"
723 interfaces = [
724 port for port in self.client.list_router_interfaces(
725 self.router['id'])['ports']
726 if port['device_owner'] in const.ROUTER_INTERFACE_OWNERS]
727 for i in interfaces:
728 try:
729 self.assertRaises(
730 lib_exc.Conflict, self.client.delete_router,
731 self.router['id'])
732 self.client.remove_router_interface_with_subnet_id(
733 self.router['id'], i['fixed_ips'][0]['subnet_id'])
734 except lib_exc.NotFound:
735 pass
736 self.client.delete_router(self.router['id'])