blob: d70b785b2750d0cf2decbd1641c99fbe4157b6b5 [file] [log] [blame]
Daniel Mellado3c0aeab2016-01-29 11:30:25 +00001# Copyright 2015 Hewlett-Packard Development Company, L.P.
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
16from tempest.lib.common.utils import data_utils
17from tempest import test
18
19from neutron.tests.tempest.api import base
20
21SUBNETPOOL_NAME = 'smoke-subnetpool'
22SUBNET_NAME = 'smoke-subnet'
23
24
25class SubnetPoolsTestBase(base.BaseAdminNetworkTest):
26
27 @classmethod
28 def resource_setup(cls):
29 super(SubnetPoolsTestBase, cls).resource_setup()
30 min_prefixlen = '29'
31 prefixes = [u'10.11.12.0/24']
32 cls._subnetpool_data = {'prefixes': prefixes,
33 'min_prefixlen': min_prefixlen}
34
35 def _create_subnetpool(self, is_admin=False, **kwargs):
36 if 'name' not in kwargs:
37 name = data_utils.rand_name(SUBNETPOOL_NAME)
38 else:
39 name = kwargs.pop('name')
40
41 if 'prefixes' not in kwargs:
42 kwargs['prefixes'] = self._subnetpool_data['prefixes']
43
44 if 'min_prefixlen' not in kwargs:
45 kwargs['min_prefixlen'] = self._subnetpool_data['min_prefixlen']
46
47 return self.create_subnetpool(name=name, is_admin=is_admin, **kwargs)
48
49
50class SubnetPoolsTest(SubnetPoolsTestBase):
51
52 min_prefixlen = '28'
53 max_prefixlen = '31'
54 _ip_version = 4
55 subnet_cidr = u'10.11.12.0/31'
56 new_prefix = u'10.11.15.0/24'
57 larger_prefix = u'10.11.0.0/16'
58
59 """
60 Tests the following operations in the Neutron API using the REST client for
61 Neutron:
62
63 create a subnetpool for a tenant
64 list tenant's subnetpools
65 show a tenant subnetpool details
66 subnetpool update
67 delete a subnetpool
68
69 All subnetpool tests are run once with ipv4 and once with ipv6.
70
71 v2.0 of the Neutron API is assumed.
72
73 """
74
75 def _new_subnetpool_attributes(self):
76 new_name = data_utils.rand_name(SUBNETPOOL_NAME)
77 return {'name': new_name, 'min_prefixlen': self.min_prefixlen,
78 'max_prefixlen': self.max_prefixlen}
79
80 def _check_equality_updated_subnetpool(self, expected_values,
81 updated_pool):
82 self.assertEqual(expected_values['name'],
83 updated_pool['name'])
84 self.assertEqual(expected_values['min_prefixlen'],
85 updated_pool['min_prefixlen'])
86 self.assertEqual(expected_values['max_prefixlen'],
87 updated_pool['max_prefixlen'])
88 # expected_values may not contains all subnetpool values
89 if 'prefixes' in expected_values:
90 self.assertEqual(expected_values['prefixes'],
91 updated_pool['prefixes'])
92
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000093 @test.idempotent_id('6e1781ec-b45b-4042-aebe-f485c022996e')
94 def test_create_list_subnetpool(self):
95 created_subnetpool = self._create_subnetpool()
96 body = self.client.list_subnetpools()
97 subnetpools = body['subnetpools']
98 self.assertIn(created_subnetpool['id'],
99 [sp['id'] for sp in subnetpools],
100 "Created subnetpool id should be in the list")
101 self.assertIn(created_subnetpool['name'],
102 [sp['name'] for sp in subnetpools],
103 "Created subnetpool name should be in the list")
104
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000105 @test.idempotent_id('c72c1c0c-2193-4aca-ddd4-b1442640bbbb')
106 @test.requires_ext(extension="standard-attr-description",
107 service="network")
108 def test_create_update_subnetpool_description(self):
109 body = self._create_subnetpool(description='d1')
110 self.assertEqual('d1', body['description'])
111 sub_id = body['id']
112 body = filter(lambda x: x['id'] == sub_id,
113 self.client.list_subnetpools()['subnetpools'])[0]
114 self.assertEqual('d1', body['description'])
115 body = self.client.update_subnetpool(sub_id, description='d2')
116 self.assertEqual('d2', body['subnetpool']['description'])
117 body = filter(lambda x: x['id'] == sub_id,
118 self.client.list_subnetpools()['subnetpools'])[0]
119 self.assertEqual('d2', body['description'])
120
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000121 @test.idempotent_id('741d08c2-1e3f-42be-99c7-0ea93c5b728c')
122 def test_get_subnetpool(self):
123 created_subnetpool = self._create_subnetpool()
124 prefixlen = self._subnetpool_data['min_prefixlen']
125 body = self.client.show_subnetpool(created_subnetpool['id'])
126 subnetpool = body['subnetpool']
127 self.assertEqual(created_subnetpool['name'], subnetpool['name'])
128 self.assertEqual(created_subnetpool['id'], subnetpool['id'])
129 self.assertEqual(prefixlen, subnetpool['min_prefixlen'])
130 self.assertEqual(prefixlen, subnetpool['default_prefixlen'])
131 self.assertFalse(subnetpool['shared'])
132
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000133 @test.idempotent_id('764f1b93-1c4a-4513-9e7b-6c2fc5e9270c')
134 def test_tenant_update_subnetpool(self):
135 created_subnetpool = self._create_subnetpool()
136 pool_id = created_subnetpool['id']
137 subnetpool_data = self._new_subnetpool_attributes()
138 self.client.update_subnetpool(created_subnetpool['id'],
139 **subnetpool_data)
140
141 body = self.client.show_subnetpool(pool_id)
142 subnetpool = body['subnetpool']
143 self._check_equality_updated_subnetpool(subnetpool_data,
144 subnetpool)
145 self.assertFalse(subnetpool['shared'])
146
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000147 @test.idempotent_id('4b496082-c992-4319-90be-d4a7ce646290')
148 def test_update_subnetpool_prefixes_append(self):
149 # We can append new prefixes to subnetpool
150 create_subnetpool = self._create_subnetpool()
151 pool_id = create_subnetpool['id']
152 old_prefixes = self._subnetpool_data['prefixes']
153 new_prefixes = old_prefixes[:]
154 new_prefixes.append(self.new_prefix)
155 subnetpool_data = {'prefixes': new_prefixes}
156 self.client.update_subnetpool(pool_id, **subnetpool_data)
157 body = self.client.show_subnetpool(pool_id)
158 prefixes = body['subnetpool']['prefixes']
159 self.assertIn(self.new_prefix, prefixes)
160 self.assertIn(old_prefixes[0], prefixes)
161
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000162 @test.idempotent_id('2cae5d6a-9d32-42d8-8067-f13970ae13bb')
163 def test_update_subnetpool_prefixes_extend(self):
164 # We can extend current subnetpool prefixes
165 created_subnetpool = self._create_subnetpool()
166 pool_id = created_subnetpool['id']
167 old_prefixes = self._subnetpool_data['prefixes']
168 subnetpool_data = {'prefixes': [self.larger_prefix]}
169 self.client.update_subnetpool(pool_id, **subnetpool_data)
170 body = self.client.show_subnetpool(pool_id)
171 prefixes = body['subnetpool']['prefixes']
172 self.assertIn(self.larger_prefix, prefixes)
173 self.assertNotIn(old_prefixes[0], prefixes)
174
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000175 @test.idempotent_id('d70c6c35-913b-4f24-909f-14cd0d29b2d2')
176 def test_admin_create_shared_subnetpool(self):
177 created_subnetpool = self._create_subnetpool(is_admin=True,
178 shared=True)
179 pool_id = created_subnetpool['id']
180 # Shared subnetpool can be retrieved by tenant user.
181 body = self.client.show_subnetpool(pool_id)
182 subnetpool = body['subnetpool']
183 self.assertEqual(created_subnetpool['name'], subnetpool['name'])
184 self.assertTrue(subnetpool['shared'])
185
186 def _create_subnet_from_pool(self, subnet_values=None, pool_values=None):
187 if pool_values is None:
188 pool_values = {}
189
190 created_subnetpool = self._create_subnetpool(**pool_values)
191 pool_id = created_subnetpool['id']
192 subnet_name = data_utils.rand_name(SUBNETPOOL_NAME)
193 network = self.create_network()
194 subnet_kwargs = {'name': subnet_name,
195 'subnetpool_id': pool_id}
196 if subnet_values:
197 subnet_kwargs.update(subnet_values)
198 # not creating the subnet using the base.create_subnet because
199 # that function needs to be enhanced to support subnet_create when
200 # prefixlen and subnetpool_id is specified.
201 body = self.client.create_subnet(
202 network_id=network['id'],
203 ip_version=self._ip_version,
204 **subnet_kwargs)
205 subnet = body['subnet']
206 return pool_id, subnet
207
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000208 @test.idempotent_id('1362ed7d-3089-42eb-b3a5-d6cb8398ee77')
209 def test_create_subnet_from_pool_with_prefixlen(self):
210 subnet_values = {"prefixlen": self.max_prefixlen}
211 pool_id, subnet = self._create_subnet_from_pool(
212 subnet_values=subnet_values)
213 cidr = str(subnet['cidr'])
214 self.assertEqual(pool_id, subnet['subnetpool_id'])
215 self.assertTrue(cidr.endswith(str(self.max_prefixlen)))
216
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000217 @test.idempotent_id('86b86189-9789-4582-9c3b-7e2bfe5735ee')
218 def test_create_subnet_from_pool_with_subnet_cidr(self):
219 subnet_values = {"cidr": self.subnet_cidr}
220 pool_id, subnet = self._create_subnet_from_pool(
221 subnet_values=subnet_values)
222 cidr = str(subnet['cidr'])
223 self.assertEqual(pool_id, subnet['subnetpool_id'])
224 self.assertEqual(cidr, self.subnet_cidr)
225
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000226 @test.idempotent_id('83f76e3a-9c40-40c2-a015-b7c5242178d8')
227 def test_create_subnet_from_pool_with_default_prefixlen(self):
228 # If neither cidr nor prefixlen is specified,
229 # subnet will use subnetpool default_prefixlen for cidr.
230 pool_id, subnet = self._create_subnet_from_pool()
231 cidr = str(subnet['cidr'])
232 self.assertEqual(pool_id, subnet['subnetpool_id'])
233 prefixlen = self._subnetpool_data['min_prefixlen']
234 self.assertTrue(cidr.endswith(str(prefixlen)))
235
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000236 @test.idempotent_id('a64af292-ec52-4bde-b654-a6984acaf477')
237 def test_create_subnet_from_pool_with_quota(self):
238 pool_values = {'default_quota': 4}
239 subnet_values = {"prefixlen": self.max_prefixlen}
240 pool_id, subnet = self._create_subnet_from_pool(
241 subnet_values=subnet_values, pool_values=pool_values)
242 cidr = str(subnet['cidr'])
243 self.assertEqual(pool_id, subnet['subnetpool_id'])
244 self.assertTrue(cidr.endswith(str(self.max_prefixlen)))
245
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000246 @test.idempotent_id('49b44c64-1619-4b29-b527-ffc3c3115dc4')
247 @test.requires_ext(extension='address-scope', service='network')
248 def test_create_subnetpool_associate_address_scope(self):
249 address_scope = self.create_address_scope(
250 name=data_utils.rand_name('smoke-address-scope'),
251 ip_version=self._ip_version)
252 created_subnetpool = self._create_subnetpool(
253 address_scope_id=address_scope['id'])
254 body = self.client.show_subnetpool(created_subnetpool['id'])
255 self.assertEqual(address_scope['id'],
256 body['subnetpool']['address_scope_id'])
257
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000258 @test.idempotent_id('910b6393-db24-4f6f-87dc-b36892ad6c8c')
259 @test.requires_ext(extension='address-scope', service='network')
260 def test_update_subnetpool_associate_address_scope(self):
261 address_scope = self.create_address_scope(
262 name=data_utils.rand_name('smoke-address-scope'),
263 ip_version=self._ip_version)
264 created_subnetpool = self._create_subnetpool()
265 pool_id = created_subnetpool['id']
266 body = self.client.show_subnetpool(pool_id)
267 self.assertIsNone(body['subnetpool']['address_scope_id'])
268 self.client.update_subnetpool(pool_id,
269 address_scope_id=address_scope['id'])
270 body = self.client.show_subnetpool(pool_id)
271 self.assertEqual(address_scope['id'],
272 body['subnetpool']['address_scope_id'])
273
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000274 @test.idempotent_id('18302e80-46a3-4563-82ac-ccd1dd57f652')
275 @test.requires_ext(extension='address-scope', service='network')
276 def test_update_subnetpool_associate_another_address_scope(self):
277 address_scope = self.create_address_scope(
278 name=data_utils.rand_name('smoke-address-scope'),
279 ip_version=self._ip_version)
280 another_address_scope = self.create_address_scope(
281 name=data_utils.rand_name('smoke-address-scope'),
282 ip_version=self._ip_version)
283 created_subnetpool = self._create_subnetpool(
284 address_scope_id=address_scope['id'])
285 pool_id = created_subnetpool['id']
286 body = self.client.show_subnetpool(pool_id)
287 self.assertEqual(address_scope['id'],
288 body['subnetpool']['address_scope_id'])
289 self.client.update_subnetpool(
290 pool_id, address_scope_id=another_address_scope['id'])
291 body = self.client.show_subnetpool(pool_id)
292 self.assertEqual(another_address_scope['id'],
293 body['subnetpool']['address_scope_id'])
294
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000295 @test.idempotent_id('f8970048-e41b-42d6-934b-a1297b07706a')
296 @test.requires_ext(extension='address-scope', service='network')
297 def test_update_subnetpool_disassociate_address_scope(self):
298 address_scope = self.create_address_scope(
299 name=data_utils.rand_name('smoke-address-scope'),
300 ip_version=self._ip_version)
301 created_subnetpool = self._create_subnetpool(
302 address_scope_id=address_scope['id'])
303 pool_id = created_subnetpool['id']
304 body = self.client.show_subnetpool(pool_id)
305 self.assertEqual(address_scope['id'],
306 body['subnetpool']['address_scope_id'])
307 self.client.update_subnetpool(pool_id,
308 address_scope_id=None)
309 body = self.client.show_subnetpool(pool_id)
310 self.assertIsNone(body['subnetpool']['address_scope_id'])
311
312
313class SubnetPoolsTestV6(SubnetPoolsTest):
314
315 min_prefixlen = '48'
316 max_prefixlen = '64'
317 _ip_version = 6
318 subnet_cidr = '2001:db8:3::/64'
319 new_prefix = u'2001:db8:5::/64'
320 larger_prefix = u'2001:db8::/32'
321
322 @classmethod
323 def resource_setup(cls):
324 super(SubnetPoolsTestV6, cls).resource_setup()
325 min_prefixlen = '64'
326 prefixes = [u'2001:db8:3::/48']
327 cls._subnetpool_data = {'min_prefixlen': min_prefixlen,
328 'prefixes': prefixes}
329
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000330 @test.idempotent_id('f62d73dc-cf6f-4879-b94b-dab53982bf3b')
331 def test_create_dual_stack_subnets_from_subnetpools(self):
332 pool_id_v6, subnet_v6 = self._create_subnet_from_pool()
333 pool_values_v4 = {'prefixes': ['192.168.0.0/16'],
334 'min_prefixlen': 21,
335 'max_prefixlen': 32}
336 create_v4_subnetpool = self._create_subnetpool(**pool_values_v4)
337 pool_id_v4 = create_v4_subnetpool['id']
338 subnet_v4 = self.client.create_subnet(
339 network_id=subnet_v6['network_id'], ip_version=4,
340 subnetpool_id=pool_id_v4)['subnet']
341 self.assertEqual(subnet_v4['network_id'], subnet_v6['network_id'])