blob: 65fda89b2beed8777ae16682846785961ee6d123 [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
17import six
18from tempest.lib.common.utils import data_utils
19from tempest import test
20
21from neutron.tests.tempest.api import base_routers as base
22from neutron.tests.tempest import config
23
24CONF = config.CONF
25
26
27class RoutersTest(base.BaseRouterTest):
28
29 @classmethod
30 @test.requires_ext(extension="router", service="network")
31 def skip_checks(cls):
32 super(RoutersTest, cls).skip_checks()
33
34 @classmethod
35 def resource_setup(cls):
36 super(RoutersTest, cls).resource_setup()
37 cls.tenant_cidr = (
38 config.safe_get_config_value('network', 'project_network_cidr')
39 if cls._ip_version == 4 else
40 config.safe_get_config_value('network', 'project_network_v6_cidr'))
41
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000042 @test.idempotent_id('c72c1c0c-2193-4aca-eeee-b1442640eeee')
43 @test.requires_ext(extension="standard-attr-description",
44 service="network")
45 def test_create_update_router_description(self):
46 body = self.create_router(description='d1', router_name='test')
47 self.assertEqual('d1', body['description'])
48 body = self.client.show_router(body['id'])['router']
49 self.assertEqual('d1', body['description'])
50 body = self.client.update_router(body['id'], description='d2')
51 self.assertEqual('d2', body['router']['description'])
52 body = self.client.show_router(body['router']['id'])['router']
53 self.assertEqual('d2', body['description'])
54
55 @test.idempotent_id('847257cc-6afd-4154-b8fb-af49f5670ce8')
56 @test.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000057 def test_create_router_with_default_snat_value(self):
58 # Create a router with default snat rule
59 name = data_utils.rand_name('router')
60 router = self._create_router(
61 name, external_network_id=CONF.network.public_network_id)
62 self._verify_router_gateway(
63 router['id'], {'network_id': CONF.network.public_network_id,
64 'enable_snat': True})
65
66 @test.idempotent_id('ea74068d-09e9-4fd7-8995-9b6a1ace920f')
67 @test.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000068 def test_create_router_with_snat_explicit(self):
69 name = data_utils.rand_name('snat-router')
70 # Create a router enabling snat attributes
71 enable_snat_states = [False, True]
72 for enable_snat in enable_snat_states:
73 external_gateway_info = {
74 'network_id': CONF.network.public_network_id,
75 'enable_snat': enable_snat}
76 create_body = self.admin_client.create_router(
77 name, external_gateway_info=external_gateway_info)
78 self.addCleanup(self.admin_client.delete_router,
79 create_body['router']['id'])
80 # Verify snat attributes after router creation
81 self._verify_router_gateway(create_body['router']['id'],
82 exp_ext_gw_info=external_gateway_info)
83
84 def _verify_router_gateway(self, router_id, exp_ext_gw_info=None):
85 show_body = self.admin_client.show_router(router_id)
86 actual_ext_gw_info = show_body['router']['external_gateway_info']
87 if exp_ext_gw_info is None:
88 self.assertIsNone(actual_ext_gw_info)
89 return
90 # Verify only keys passed in exp_ext_gw_info
91 for k, v in six.iteritems(exp_ext_gw_info):
92 self.assertEqual(v, actual_ext_gw_info[k])
93
94 def _verify_gateway_port(self, router_id):
95 list_body = self.admin_client.list_ports(
96 network_id=CONF.network.public_network_id,
97 device_id=router_id)
98 self.assertEqual(len(list_body['ports']), 1)
99 gw_port = list_body['ports'][0]
100 fixed_ips = gw_port['fixed_ips']
101 self.assertGreaterEqual(len(fixed_ips), 1)
102 public_net_body = self.admin_client.show_network(
103 CONF.network.public_network_id)
104 public_subnet_id = public_net_body['network']['subnets'][0]
105 self.assertIn(public_subnet_id,
106 [x['subnet_id'] for x in fixed_ips])
107
108 @test.idempotent_id('b386c111-3b21-466d-880c-5e72b01e1a33')
109 @test.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000110 def test_update_router_set_gateway_with_snat_explicit(self):
111 router = self._create_router(data_utils.rand_name('router-'))
112 self.admin_client.update_router_with_snat_gw_info(
113 router['id'],
114 external_gateway_info={
115 'network_id': CONF.network.public_network_id,
116 'enable_snat': True})
117 self._verify_router_gateway(
118 router['id'],
119 {'network_id': CONF.network.public_network_id,
120 'enable_snat': True})
121 self._verify_gateway_port(router['id'])
122
123 @test.idempotent_id('96536bc7-8262-4fb2-9967-5c46940fa279')
124 @test.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000125 def test_update_router_set_gateway_without_snat(self):
126 router = self._create_router(data_utils.rand_name('router-'))
127 self.admin_client.update_router_with_snat_gw_info(
128 router['id'],
129 external_gateway_info={
130 'network_id': CONF.network.public_network_id,
131 'enable_snat': False})
132 self._verify_router_gateway(
133 router['id'],
134 {'network_id': CONF.network.public_network_id,
135 'enable_snat': False})
136 self._verify_gateway_port(router['id'])
137
138 @test.idempotent_id('f2faf994-97f4-410b-a831-9bc977b64374')
139 @test.requires_ext(extension='ext-gw-mode', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000140 def test_update_router_reset_gateway_without_snat(self):
141 router = self._create_router(
142 data_utils.rand_name('router-'),
143 external_network_id=CONF.network.public_network_id)
144 self.admin_client.update_router_with_snat_gw_info(
145 router['id'],
146 external_gateway_info={
147 'network_id': CONF.network.public_network_id,
148 'enable_snat': False})
149 self._verify_router_gateway(
150 router['id'],
151 {'network_id': CONF.network.public_network_id,
152 'enable_snat': False})
153 self._verify_gateway_port(router['id'])
154
155 @test.idempotent_id('c86ac3a8-50bd-4b00-a6b8-62af84a0765c')
156 @test.requires_ext(extension='extraroute', service='network')
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000157 def test_update_extra_route(self):
158 self.network = self.create_network()
159 self.name = self.network['name']
160 self.subnet = self.create_subnet(self.network)
161 # Add router interface with subnet id
162 self.router = self._create_router(
163 data_utils.rand_name('router-'), True)
164 self.create_router_interface(self.router['id'], self.subnet['id'])
165 self.addCleanup(
166 self._delete_extra_routes,
167 self.router['id'])
168 # Update router extra route, second ip of the range is
169 # used as next hop
170 cidr = netaddr.IPNetwork(self.subnet['cidr'])
171 next_hop = str(cidr[2])
172 destination = str(self.subnet['cidr'])
173 extra_route = self.client.update_extra_routes(self.router['id'],
174 next_hop, destination)
175 self.assertEqual(1, len(extra_route['router']['routes']))
176 self.assertEqual(destination,
177 extra_route['router']['routes'][0]['destination'])
178 self.assertEqual(next_hop,
179 extra_route['router']['routes'][0]['nexthop'])
180 show_body = self.client.show_router(self.router['id'])
181 self.assertEqual(destination,
182 show_body['router']['routes'][0]['destination'])
183 self.assertEqual(next_hop,
184 show_body['router']['routes'][0]['nexthop'])
185
186 def _delete_extra_routes(self, router_id):
187 self.client.delete_extra_routes(router_id)
188
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000189 @test.idempotent_id('01f185d1-d1a6-4cf9-abf7-e0e1384c169c')
190 def test_network_attached_with_two_routers(self):
191 network = self.create_network(data_utils.rand_name('network1'))
192 self.create_subnet(network)
193 port1 = self.create_port(network)
194 port2 = self.create_port(network)
195 router1 = self._create_router(data_utils.rand_name('router1'))
196 router2 = self._create_router(data_utils.rand_name('router2'))
197 self.client.add_router_interface_with_port_id(
198 router1['id'], port1['id'])
199 self.client.add_router_interface_with_port_id(
200 router2['id'], port2['id'])
201 self.addCleanup(self.client.remove_router_interface_with_port_id,
202 router1['id'], port1['id'])
203 self.addCleanup(self.client.remove_router_interface_with_port_id,
204 router2['id'], port2['id'])
205 body = self.client.show_port(port1['id'])
206 port_show1 = body['port']
207 body = self.client.show_port(port2['id'])
208 port_show2 = body['port']
209 self.assertEqual(port_show1['network_id'], network['id'])
210 self.assertEqual(port_show2['network_id'], network['id'])
211 self.assertEqual(port_show1['device_id'], router1['id'])
212 self.assertEqual(port_show2['device_id'], router2['id'])
213
214
215class RoutersIpV6Test(RoutersTest):
216 _ip_version = 6
217
218
219class DvrRoutersTest(base.BaseRouterTest):
220
221 @classmethod
222 @test.requires_ext(extension="dvr", service="network")
223 def skip_checks(cls):
224 super(DvrRoutersTest, cls).skip_checks()
225
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000226 @test.idempotent_id('141297aa-3424-455d-aa8d-f2d95731e00a')
227 def test_create_distributed_router(self):
228 name = data_utils.rand_name('router')
229 create_body = self.admin_client.create_router(
230 name, distributed=True)
231 self.addCleanup(self._delete_router,
232 create_body['router']['id'],
233 self.admin_client)
234 self.assertTrue(create_body['router']['distributed'])
235
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000236 @test.idempotent_id('644d7a4a-01a1-4b68-bb8d-0c0042cb1729')
237 def test_convert_centralized_router(self):
238 router = self._create_router(data_utils.rand_name('router'))
239 self.assertNotIn('distributed', router)
240 update_body = self.admin_client.update_router(router['id'],
241 distributed=True)
242 self.assertTrue(update_body['router']['distributed'])
243 show_body = self.admin_client.show_router(router['id'])
244 self.assertTrue(show_body['router']['distributed'])
245 show_body = self.client.show_router(router['id'])
246 self.assertNotIn('distributed', show_body['router'])