blob: abbb779bdbe53f0a12787f5f2117a7a4d32b3d72 [file] [log] [blame]
nayna-patel197c0122013-07-11 10:18:00 +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
ChenZheng04ac6942013-12-09 16:42:52 +080016import netaddr
zhufl6b7040a2017-01-18 16:38:34 +080017import testtools
ChenZheng04ac6942013-12-09 16:42:52 +080018
Ken'ichi Ohmichi3fd5f0e2017-05-04 18:40:36 -070019from tempest.api.network import base
Andrea Frittolicd368412017-08-14 21:37:56 +010020from tempest.common import utils
Matthew Treinish03b48df2014-01-29 16:59:49 +000021from tempest import config
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080022from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080023from tempest.lib import decorators
nayna-patel197c0122013-07-11 10:18:00 +000024
Matthew Treinish03b48df2014-01-29 16:59:49 +000025CONF = config.CONF
26
nayna-patel197c0122013-07-11 10:18:00 +000027
Ken'ichi Ohmichi3204a0e2017-05-04 18:44:28 -070028class RoutersTest(base.BaseNetworkTest):
Ken'ichi Ohmichi3fd5f0e2017-05-04 18:40:36 -070029
30 def _cleanup_router(self, router):
31 self.delete_router(router)
32 self.routers.remove(router)
33
34 def _create_router(self, name=None, admin_state_up=False,
35 external_network_id=None, enable_snat=None):
36 # associate a cleanup with created routers to avoid quota limits
37 router = self.create_router(name, admin_state_up,
38 external_network_id, enable_snat)
39 self.addCleanup(self._cleanup_router, router)
40 return router
41
42 def _add_router_interface_with_subnet_id(self, router_id, subnet_id):
43 interface = self.routers_client.add_router_interface(
44 router_id, subnet_id=subnet_id)
45 self.addCleanup(self._remove_router_interface_with_subnet_id,
46 router_id, subnet_id)
47 self.assertEqual(subnet_id, interface['subnet_id'])
48 return interface
49
50 def _remove_router_interface_with_subnet_id(self, router_id, subnet_id):
51 body = self.routers_client.remove_router_interface(router_id,
52 subnet_id=subnet_id)
53 self.assertEqual(subnet_id, body['subnet_id'])
nayna-patel197c0122013-07-11 10:18:00 +000054
55 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053056 def skip_checks(cls):
57 super(RoutersTest, cls).skip_checks()
Andrea Frittolicd368412017-08-14 21:37:56 +010058 if not utils.is_extension_enabled('router', 'network'):
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090059 msg = "router extension not enabled."
60 raise cls.skipException(msg)
Rohan Kanadea565e452015-01-27 14:00:13 +053061
Jordan Pittier3b46d272017-04-12 16:17:28 +020062 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080063 @decorators.idempotent_id('f64403e2-8483-4b34-8ccd-b09a87bcc68c')
zhufl6b7040a2017-01-18 16:38:34 +080064 @testtools.skipUnless(CONF.network.public_network_id,
65 'The public_network_id option must be specified.')
nayna-patel197c0122013-07-11 10:18:00 +000066 def test_create_show_list_update_delete_router(self):
67 # Create a router
zhufl3484f992017-10-10 16:18:29 +080068 name = data_utils.rand_name(self.__class__.__name__ + '-router')
Tianbiao Qi83158912016-11-03 10:37:30 +080069 router = self._create_router(
zhufl3484f992017-10-10 16:18:29 +080070 name=name,
Tianbiao Qi83158912016-11-03 10:37:30 +080071 admin_state_up=False,
72 external_network_id=CONF.network.public_network_id)
zhufl3484f992017-10-10 16:18:29 +080073 self.assertEqual(router['name'], name)
Tianbiao Qi83158912016-11-03 10:37:30 +080074 self.assertEqual(router['admin_state_up'], False)
nayna-patel197c0122013-07-11 10:18:00 +000075 self.assertEqual(
Tianbiao Qi83158912016-11-03 10:37:30 +080076 router['external_gateway_info']['network_id'],
Matthew Treinish03b48df2014-01-29 16:59:49 +000077 CONF.network.public_network_id)
nayna-patel197c0122013-07-11 10:18:00 +000078 # Show details of the created router
Tianbiao Qi83158912016-11-03 10:37:30 +080079 router_show = self.routers_client.show_router(
80 router['id'])['router']
81 self.assertEqual(router_show['name'], router['name'])
nayna-patel197c0122013-07-11 10:18:00 +000082 self.assertEqual(
Tianbiao Qi83158912016-11-03 10:37:30 +080083 router_show['external_gateway_info']['network_id'],
Matthew Treinish03b48df2014-01-29 16:59:49 +000084 CONF.network.public_network_id)
nayna-patel197c0122013-07-11 10:18:00 +000085 # List routers and verify if created router is there in response
Tianbiao Qi83158912016-11-03 10:37:30 +080086 routers = self.routers_client.list_routers()['routers']
87 self.assertIn(router['id'], map(lambda x: x['id'], routers))
nayna-patel197c0122013-07-11 10:18:00 +000088 # Update the name of router and verify if it is updated
Tianbiao Qi83158912016-11-03 10:37:30 +080089 updated_name = 'updated' + router['name']
90 router_update = self.routers_client.update_router(
91 router['id'], name=updated_name)['router']
92 self.assertEqual(router_update['name'], updated_name)
93 router_show = self.routers_client.show_router(
94 router['id'])['router']
95 self.assertEqual(router_show['name'], updated_name)
nayna-patel197c0122013-07-11 10:18:00 +000096
Jordan Pittier3b46d272017-04-12 16:17:28 +020097 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080098 @decorators.idempotent_id('b42e6e39-2e37-49cc-a6f4-8467e940900a')
nayna-patel197c0122013-07-11 10:18:00 +000099 def test_add_remove_router_interface_with_subnet_id(self):
100 network = self.create_network()
101 subnet = self.create_subnet(network)
zhufld2c40ca2016-10-18 17:03:07 +0800102 router = self._create_router()
Chang Bo Guof099f802013-09-13 19:01:46 -0700103 # Add router interface with subnet id
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000104 interface = self.routers_client.add_router_interface(
105 router['id'], subnet_id=subnet['id'])
nayna-patel197c0122013-07-11 10:18:00 +0000106 self.addCleanup(self._remove_router_interface_with_subnet_id,
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700107 router['id'], subnet['id'])
Attila Fazekas4b8b0082013-10-25 14:24:32 +0200108 self.assertIn('subnet_id', interface.keys())
109 self.assertIn('port_id', interface.keys())
nayna-patel197c0122013-07-11 10:18:00 +0000110 # Verify router id is equal to device id in port details
John Warren49c0fe52015-10-22 12:35:54 -0400111 show_port_body = self.ports_client.show_port(
nayna-patel197c0122013-07-11 10:18:00 +0000112 interface['port_id'])
113 self.assertEqual(show_port_body['port']['device_id'],
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700114 router['id'])
nayna-patel197c0122013-07-11 10:18:00 +0000115
Jordan Pittier3b46d272017-04-12 16:17:28 +0200116 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800117 @decorators.idempotent_id('2b7d2f37-6748-4d78-92e5-1d590234f0d5')
nayna-patel197c0122013-07-11 10:18:00 +0000118 def test_add_remove_router_interface_with_port_id(self):
119 network = self.create_network()
120 self.create_subnet(network)
zhufld2c40ca2016-10-18 17:03:07 +0800121 router = self._create_router()
John Warren49c0fe52015-10-22 12:35:54 -0400122 port_body = self.ports_client.create_port(
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400123 network_id=network['id'])
nayna-patel197c0122013-07-11 10:18:00 +0000124 # add router interface to port created above
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000125 interface = self.routers_client.add_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530126 router['id'],
127 port_id=port_body['port']['id'])
Ken'ichi Ohmichiea2a31a2017-05-04 14:08:07 -0700128 self.addCleanup(self.routers_client.remove_router_interface,
129 router['id'], port_id=port_body['port']['id'])
Attila Fazekas4b8b0082013-10-25 14:24:32 +0200130 self.assertIn('subnet_id', interface.keys())
131 self.assertIn('port_id', interface.keys())
nayna-patel197c0122013-07-11 10:18:00 +0000132 # Verify router id is equal to device id in port details
John Warren49c0fe52015-10-22 12:35:54 -0400133 show_port_body = self.ports_client.show_port(
nayna-patel197c0122013-07-11 10:18:00 +0000134 interface['port_id'])
135 self.assertEqual(show_port_body['port']['device_id'],
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700136 router['id'])
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700137
lianghao8c8074c2017-03-09 22:16:52 +0800138 @decorators.idempotent_id('cbe42f84-04c2-11e7-8adb-fa163e4fa634')
Andrea Frittolicd368412017-08-14 21:37:56 +0100139 @utils.requires_ext(extension='ext-gw-mode', service='network')
lianghao8c8074c2017-03-09 22:16:52 +0800140 @testtools.skipUnless(CONF.network.public_network_id,
141 'The public_network_id option must be specified.')
Jordan Pittier7252ce02017-03-26 20:48:14 +0200142 @decorators.skip_because(bug='1676207')
lianghao8c8074c2017-03-09 22:16:52 +0800143 def test_create_router_set_gateway_with_fixed_ip(self):
144 # Don't know public_network_address, so at first create address
145 # from public_network and delete
146 port = self.admin_ports_client.create_port(
147 network_id=CONF.network.public_network_id)['port']
148 self.admin_ports_client.delete_port(port_id=port['id'])
149
150 fixed_ip = {
151 'subnet_id': port['fixed_ips'][0]['subnet_id'],
152 'ip_address': port['fixed_ips'][0]['ip_address']
153 }
154 external_gateway_info = {
155 'network_id': CONF.network.public_network_id,
156 'external_fixed_ips': [fixed_ip]
157 }
158
159 # Create a router and set gateway to fixed_ip
160 router = self.admin_routers_client.create_router(
161 external_gateway_info=external_gateway_info)['router']
162 self.addCleanup(self.admin_routers_client.delete_router,
163 router_id=router['id'])
164 # Examine router's gateway is equal to fixed_ip
165 self.assertEqual(router['external_gateway_info'][
166 'external_fixed_ips'][0]['ip_address'],
167 fixed_ip['ip_address'])
168
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800169 @decorators.idempotent_id('c86ac3a8-50bd-4b00-a6b8-62af84a0765c')
Andrea Frittolicd368412017-08-14 21:37:56 +0100170 @utils.requires_ext(extension='extraroute', service='network')
Abhishek6707b5e2016-01-14 15:49:11 +0530171 def test_update_delete_extra_route(self):
Shuquan Huangde79d262015-06-26 02:48:22 +0000172 # Create different cidr for each subnet to avoid cidr duplicate
Sean Dagueed6e5862016-04-04 10:49:13 -0400173 # The cidr starts from project_cidr
zhuflb0dbe4b2017-09-11 16:45:17 +0800174 next_cidr = self.cidr
Shuquan Huangde79d262015-06-26 02:48:22 +0000175 # Prepare to build several routes
176 test_routes = []
David Kranz4d06ebb2015-07-31 15:18:08 -0400177 routes_num = 4
Shuquan Huangde79d262015-06-26 02:48:22 +0000178 # Create a router
zhufld2c40ca2016-10-18 17:03:07 +0800179 router = self._create_router(admin_state_up=True)
ChenZheng04ac6942013-12-09 16:42:52 +0800180 self.addCleanup(
181 self._delete_extra_routes,
Brandon Palmf35aa8b2015-08-07 14:56:59 -0500182 router['id'])
Sylvain Afchain85c1e542014-01-11 08:23:16 +0100183 # Update router extra route, second ip of the range is
184 # used as next hop
Shuquan Huangde79d262015-06-26 02:48:22 +0000185 for i in range(routes_num):
186 network = self.create_network()
187 subnet = self.create_subnet(network, cidr=next_cidr)
188 next_cidr = next_cidr.next()
189
190 # Add router interface with subnet id
Brandon Palmf35aa8b2015-08-07 14:56:59 -0500191 self.create_router_interface(router['id'], subnet['id'])
Shuquan Huangde79d262015-06-26 02:48:22 +0000192
193 cidr = netaddr.IPNetwork(subnet['cidr'])
194 next_hop = str(cidr[2])
195 destination = str(subnet['cidr'])
196 test_routes.append(
197 {'nexthop': next_hop, 'destination': destination}
198 )
199
200 test_routes.sort(key=lambda x: x['destination'])
Ken'ichi Ohmichi50fc7842016-05-30 11:21:08 -0700201 extra_route = self.routers_client.update_router(
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000202 router['id'], routes=test_routes)
203 show_body = self.routers_client.show_router(router['id'])
Shuquan Huangde79d262015-06-26 02:48:22 +0000204 # Assert the number of routes
205 self.assertEqual(routes_num, len(extra_route['router']['routes']))
206 self.assertEqual(routes_num, len(show_body['router']['routes']))
207
208 routes = extra_route['router']['routes']
209 routes.sort(key=lambda x: x['destination'])
210 # Assert the nexthops & destination
211 for i in range(routes_num):
212 self.assertEqual(test_routes[i]['destination'],
213 routes[i]['destination'])
214 self.assertEqual(test_routes[i]['nexthop'], routes[i]['nexthop'])
215
216 routes = show_body['router']['routes']
217 routes.sort(key=lambda x: x['destination'])
218 for i in range(routes_num):
219 self.assertEqual(test_routes[i]['destination'],
220 routes[i]['destination'])
221 self.assertEqual(test_routes[i]['nexthop'], routes[i]['nexthop'])
ChenZheng04ac6942013-12-09 16:42:52 +0800222
Ken'ichi Ohmichi1f387612016-05-30 11:28:06 -0700223 self._delete_extra_routes(router['id'])
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000224 show_body_after_deletion = self.routers_client.show_router(
225 router['id'])
Abhishek6707b5e2016-01-14 15:49:11 +0530226 self.assertEmpty(show_body_after_deletion['router']['routes'])
227
ChenZheng04ac6942013-12-09 16:42:52 +0800228 def _delete_extra_routes(self, router_id):
Ken'ichi Ohmichi1f387612016-05-30 11:28:06 -0700229 self.routers_client.update_router(router_id, routes=None)
liudongfbdfc7e2014-02-15 10:52:17 +0800230
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800231 @decorators.idempotent_id('a8902683-c788-4246-95c7-ad9c6d63a4d9')
liudongfbdfc7e2014-02-15 10:52:17 +0800232 def test_update_router_admin_state(self):
zhufld2c40ca2016-10-18 17:03:07 +0800233 router = self._create_router()
Brandon Palmf35aa8b2015-08-07 14:56:59 -0500234 self.assertFalse(router['admin_state_up'])
liudongfbdfc7e2014-02-15 10:52:17 +0800235 # Update router admin state
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000236 update_body = self.routers_client.update_router(router['id'],
237 admin_state_up=True)
liudongfbdfc7e2014-02-15 10:52:17 +0800238 self.assertTrue(update_body['router']['admin_state_up'])
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000239 show_body = self.routers_client.show_router(router['id'])
liudongfbdfc7e2014-02-15 10:52:17 +0800240 self.assertTrue(show_body['router']['admin_state_up'])
241
Jordan Pittier3b46d272017-04-12 16:17:28 +0200242 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800243 @decorators.idempotent_id('802c73c9-c937-4cef-824b-2191e24a6aab')
liudongfbdfc7e2014-02-15 10:52:17 +0800244 def test_add_multiple_router_interfaces(self):
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700245 network01 = self.create_network(
246 network_name=data_utils.rand_name('router-network01-'))
247 network02 = self.create_network(
248 network_name=data_utils.rand_name('router-network02-'))
249 subnet01 = self.create_subnet(network01)
zhuflb0dbe4b2017-09-11 16:45:17 +0800250 sub02_cidr = self.cidr.next()
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700251 subnet02 = self.create_subnet(network02, cidr=sub02_cidr)
zhufld2c40ca2016-10-18 17:03:07 +0800252 router = self._create_router()
liudongfbdfc7e2014-02-15 10:52:17 +0800253 interface01 = self._add_router_interface_with_subnet_id(router['id'],
254 subnet01['id'])
255 self._verify_router_interface(router['id'], subnet01['id'],
256 interface01['port_id'])
257 interface02 = self._add_router_interface_with_subnet_id(router['id'],
258 subnet02['id'])
259 self._verify_router_interface(router['id'], subnet02['id'],
260 interface02['port_id'])
261
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800262 @decorators.idempotent_id('96522edf-b4b5-45d9-8443-fa11c26e6eff')
Hardik Italiae1a17072015-07-15 17:58:24 -0700263 def test_router_interface_port_update_with_fixed_ip(self):
264 network = self.create_network()
265 subnet = self.create_subnet(network)
zhufld2c40ca2016-10-18 17:03:07 +0800266 router = self._create_router()
Hardik Italiae1a17072015-07-15 17:58:24 -0700267 fixed_ip = [{'subnet_id': subnet['id']}]
268 interface = self._add_router_interface_with_subnet_id(router['id'],
269 subnet['id'])
270 self.assertIn('port_id', interface)
271 self.assertIn('subnet_id', interface)
272 port = self.ports_client.show_port(interface['port_id'])
273 self.assertEqual(port['port']['id'], interface['port_id'])
274 router_port = self.ports_client.update_port(port['port']['id'],
275 fixed_ips=fixed_ip)
276 self.assertEqual(subnet['id'],
277 router_port['port']['fixed_ips'][0]['subnet_id'])
278
liudongfbdfc7e2014-02-15 10:52:17 +0800279 def _verify_router_interface(self, router_id, subnet_id, port_id):
John Warren49c0fe52015-10-22 12:35:54 -0400280 show_port_body = self.ports_client.show_port(port_id)
liudongfbdfc7e2014-02-15 10:52:17 +0800281 interface_port = show_port_body['port']
282 self.assertEqual(router_id, interface_port['device_id'])
283 self.assertEqual(subnet_id,
284 interface_port['fixed_ips'][0]['subnet_id'])
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +0300285
286
287class RoutersIpV6Test(RoutersTest):
288 _ip_version = 6