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