Debian Codegraph @

octavia_tempest_plugin/tests/api/v2/test_flavor.py

raw symbols SCIP
1 # Copyright 2019 Rackspace US Inc. All rights reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import copy
16 from operator import itemgetter
17 from uuid import UUID
18
19 from oslo_serialization import jsonutils
20 from tempest import config
21 from tempest.lib.common.utils import data_utils
22 from tempest.lib import decorators
23 from tempest.lib import exceptions
24
25 from octavia_tempest_plugin.common import constants as const
26 from octavia_tempest_plugin.tests import test_base
27
28 CONF = config.CONF
29
30
31 class FlavorAPITest(test_base.LoadBalancerBaseTest):
32 """Test the flavor object API."""
33
34 @classmethod
35 def resource_setup(cls):
36 """Setup resources needed by the tests."""
37 super(FlavorAPITest, cls).resource_setup()
38
39 # We have to do this here as the api_version and clients are not
40 # setup in time to use a decorator or the skip_checks mixin
41 if not cls.lb_admin_flavor_profile_client.is_version_supported(
42 cls.api_version, '2.6'):
43 return
44
45 # Create a shared flavor profile
46 flavor_profile_name = data_utils.rand_name(
47 "lb_admin_flavorprofile-setup")
48 flavor_data = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
49 flavor_data_json = jsonutils.dumps(flavor_data)
50
51 flavor_profile_kwargs = {
52 const.NAME: flavor_profile_name,
53 const.PROVIDER_NAME: CONF.load_balancer.provider,
54 const.FLAVOR_DATA: flavor_data_json
55 }
56
57 cls.flavor_profile = (
58 cls.lb_admin_flavor_profile_client.create_flavor_profile(
59 **flavor_profile_kwargs))
60 cls.addClassResourceCleanup(
61 cls.lb_admin_flavor_profile_client.cleanup_flavor_profile,
62 cls.flavor_profile[const.ID])
63 cls.flavor_profile_id = cls.flavor_profile[const.ID]
64
65 @decorators.idempotent_id('7e8f39ce-53e0-4364-8778-6da9b9a59e5a')
66 def test_flavor_create(self):
67 """Tests flavor create and basic show APIs.
68
69 * Tests that users without the loadbalancer admin role cannot
70 create a flavor.
71 * Create a fully populated flavor.
72 * Validate the response reflects the requested values.
73 """
74 # We have to do this here as the api_version and clients are not
75 # setup in time to use a decorator or the skip_checks mixin
76 if not self.lb_admin_flavor_client.is_version_supported(
77 self.api_version, '2.6'):
78 raise self.skipException('Flavors are only available on '
79 'Octavia API version 2.6 or newer.')
80 flavor_name = data_utils.rand_name("lb_admin_flavor-create")
81 flavor_description = data_utils.arbitrary_string(size=255)
82
83 flavor_kwargs = {
84 const.NAME: flavor_name,
85 const.DESCRIPTION: flavor_description,
86 const.ENABLED: True,
87 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
88
89 # Test that a user without the load balancer admin role cannot
90 # create a flavor.
91 expected_allowed = ['os_admin', 'os_roles_lb_admin']
92 self.check_create_RBAC_enforcement(
93 'FlavorClient', 'create_flavor',
94 expected_allowed, None, None, **flavor_kwargs)
95
96 # Happy path
97 flavor = self.lb_admin_flavor_client.create_flavor(**flavor_kwargs)
98 self.addCleanup(self.lb_admin_flavor_client.cleanup_a_flavor,
99 flavor[const.ID])
100
101 UUID(flavor[const.ID])
102 self.assertEqual(flavor_name, flavor[const.NAME])
103 self.assertEqual(flavor_description, flavor[const.DESCRIPTION])
104 self.assertTrue(flavor[const.ENABLED])
105 self.assertEqual(self.flavor_profile_id,
106 flavor[const.FLAVOR_PROFILE_ID])
107
108 # Test that flavors do not support tags
109 flavor_tags = ["Hello", "World"]
110 tags_flavor_kwargs = flavor_kwargs.copy()
111 tags_flavor_kwargs[const.TAGS] = flavor_tags
112 self.assertRaises(TypeError,
113 self.lb_admin_flavor_client.create_flavor,
114 **tags_flavor_kwargs)
115
116 @decorators.idempotent_id('3ef040ee-fe7e-457b-a56f-8b152f7afa3b')
117 def test_flavor_list(self):
118 """Tests flavor list API and field filtering.
119
120 * Create three flavors.
121 * Validates that non-admin accounts cannot list the flavors.
122 * List the flavors using the default sort order.
123 * List the flavors using descending sort order.
124 * List the flavors using ascending sort order.
125 * List the flavors returning one field at a time.
126 * List the flavors returning two fields.
127 * List the flavors filtering to one of the three.
128 * List the flavors filtered, one field, and sorted.
129 """
130 # We have to do this here as the api_version and clients are not
131 # setup in time to use a decorator or the skip_checks mixin
132 if not self.lb_admin_flavor_client.is_version_supported(
133 self.api_version, '2.6'):
134 raise self.skipException('Flavors are only available on '
135 'Octavia API version 2.6 or newer.')
136
137 # Create flavor 1
138 flavor1_name = data_utils.rand_name("lb_admin_flavor-list-1")
139 flavor1_description = 'A'
140
141 flavor1_kwargs = {
142 const.NAME: flavor1_name,
143 const.DESCRIPTION: flavor1_description,
144 const.ENABLED: True,
145 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
146
147 flavor1 = (self.lb_admin_flavor_client.create_flavor(**flavor1_kwargs))
148 self.addCleanup(
149 self.lb_admin_flavor_client.cleanup_a_flavor, flavor1[const.ID])
150
151 # Create flavor 2
152 flavor2_name = data_utils.rand_name("lb_admin_flavor-list-2")
153 flavor2_description = 'B'
154
155 flavor2_kwargs = {
156 const.NAME: flavor2_name,
157 const.DESCRIPTION: flavor2_description,
158 const.ENABLED: False,
159 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
160
161 flavor2 = (self.lb_admin_flavor_client.create_flavor(**flavor2_kwargs))
162 self.addCleanup(
163 self.lb_admin_flavor_client.cleanup_a_flavor, flavor2[const.ID])
164
165 # Create flavor 3
166 flavor3_name = data_utils.rand_name("lb_admin_flavor-list-3")
167 flavor3_description = 'C'
168
169 flavor3_kwargs = {
170 const.NAME: flavor3_name,
171 const.DESCRIPTION: flavor3_description,
172 const.ENABLED: True,
173 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
174
175 flavor3 = (self.lb_admin_flavor_client.create_flavor(**flavor3_kwargs))
176 self.addCleanup(
177 self.lb_admin_flavor_client.cleanup_a_flavor, flavor3[const.ID])
178
179 # default sort order (by ID) reference list
180 ref_id_list_asc = [flavor1[const.ID], flavor2[const.ID],
181 flavor3[const.ID]]
182 ref_id_list_dsc = copy.deepcopy(ref_id_list_asc)
183 ref_id_list_asc.sort()
184 ref_id_list_dsc.sort(reverse=True)
185
186 # Test that a user without the load balancer role cannot
187 # list flavors.
188 expected_allowed = []
189 if CONF.load_balancer.RBAC_test_type == const.OWNERADMIN:
190 expected_allowed = [
191 'os_admin', 'os_primary', 'os_roles_lb_admin',
192 'os_roles_lb_member', 'os_roles_lb_member2']
193 if CONF.load_balancer.RBAC_test_type == const.KEYSTONE_DEFAULT_ROLES:
194 expected_allowed = ['os_admin', 'os_primary', 'os_roles_lb_admin',
195 'os_roles_lb_observer',
196 'os_roles_lb_global_observer',
197 'os_roles_lb_member', 'os_roles_lb_member2']
198 if CONF.load_balancer.RBAC_test_type == const.ADVANCED:
199 expected_allowed = [
200 'os_admin', 'os_roles_lb_admin',
201 'os_roles_lb_observer', 'os_roles_lb_global_observer',
202 'os_roles_lb_member', 'os_roles_lb_member2']
203 if expected_allowed:
204 self.check_list_RBAC_enforcement(
205 'FlavorClient', 'list_flavors', expected_allowed)
206
207 # Check the default sort order (by ID)
208 flavors = self.mem_flavor_client.list_flavors()
209 # Remove flavors not used in this test
210 flavors = [flav for flav in flavors
211 if 'lb_admin_flavor-list' in flav[const.NAME]]
212 self.assertEqual(3, len(flavors))
213 self.assertEqual(ref_id_list_asc[0], flavors[0][const.ID])
214 self.assertEqual(ref_id_list_asc[1], flavors[1][const.ID])
215 self.assertEqual(ref_id_list_asc[2], flavors[2][const.ID])
216
217 # Check the descending sort order by name
218 flavors = self.lb_admin_flavor_client.list_flavors(
219 query_params='{sort}={name}:{order}'.format(
220 sort=const.SORT, name=const.NAME, order=const.DESC))
221 # Remove flavors not used in this test
222 flavors = [flav for flav in flavors
223 if 'lb_admin_flavor-list' in flav[const.NAME]]
224 self.assertEqual(3, len(flavors))
225 self.assertEqual(flavor3_name, flavors[0][const.NAME])
226 self.assertEqual(flavor2_name, flavors[1][const.NAME])
227 self.assertEqual(flavor1_name, flavors[2][const.NAME])
228
229 # Check the ascending sort order by name
230 flavors = self.mem_flavor_client.list_flavors(
231 query_params='{sort}={name}:{order}'.format(
232 sort=const.SORT, name=const.NAME, order=const.ASC))
233 # Remove flavors not used in this test
234 flavors = [flav for flav in flavors
235 if 'lb_admin_flavor-list' in flav[const.NAME]]
236 self.assertEqual(3, len(flavors))
237 self.assertEqual(flavor1_name, flavors[0][const.NAME])
238 self.assertEqual(flavor2_name, flavors[1][const.NAME])
239 self.assertEqual(flavor3_name, flavors[2][const.NAME])
240
241 ref_flavors = [flavor1, flavor2, flavor3]
242 sorted_flavors = sorted(ref_flavors, key=itemgetter(const.ID))
243 sorted_enabled_flavors = [flav for flav in sorted_flavors
244 if flav[const.ENABLED]]
245
246 # Test fields
247 for field in const.SHOW_FLAVOR_FIELDS:
248 flavors = self.mem_flavor_client.list_flavors(
249 query_params='{fields}={field}&{fields}={name}'.format(
250 fields=const.FIELDS, field=field, name=const.NAME))
251 # Remove flavors not used in this test
252 flavors = [flav for flav in flavors
253 if 'lb_admin_flavor-list' in flav[const.NAME]]
254 self.assertEqual(3, len(flavors))
255 self.assertEqual(sorted_flavors[0][field], flavors[0][field])
256 self.assertEqual(sorted_flavors[1][field], flavors[1][field])
257 self.assertEqual(sorted_flavors[2][field], flavors[2][field])
258
259 # Test filtering
260 flavor = self.mem_flavor_client.list_flavors(
261 query_params='{name}={flav_name}'.format(
262 name=const.NAME, flav_name=flavor2[const.NAME]))
263 self.assertEqual(1, len(flavor))
264 self.assertEqual(flavor2[const.ID], flavor[0][const.ID])
265
266 # Test combined params
267 flavors = self.mem_flavor_client.list_flavors(
268 query_params='{enabled}={enable}&{fields}={name}&'
269 '{sort}={ID}:{desc}'.format(
270 enabled=const.ENABLED,
271 enable=True,
272 fields=const.FIELDS, name=const.NAME,
273 sort=const.SORT, ID=const.ID,
274 desc=const.DESC))
275 # Remove flavors not used in this test
276 flavors = [flav for flav in flavors
277 if 'lb_admin_flavor-list' in flav[const.NAME]]
278 self.assertEqual(2, len(flavors))
279 self.assertEqual(1, len(flavors[0]))
280 self.assertEqual(sorted_enabled_flavors[1][const.NAME],
281 flavors[0][const.NAME])
282 self.assertEqual(sorted_enabled_flavors[0][const.NAME],
283 flavors[1][const.NAME])
284
285 @decorators.idempotent_id('7492a862-4011-4924-8e81-70763f479cf8')
286 def test_flavor_show(self):
287 """Tests flavor show API.
288
289 * Create a fully populated flavor.
290 * Validates that non-lb-admin accounts cannot see the flavor.
291 * Show flavor details.
292 * Validate the show reflects the requested values.
293 """
294 # We have to do this here as the api_version and clients are not
295 # setup in time to use a decorator or the skip_checks mixin
296 if not self.lb_admin_flavor_client.is_version_supported(
297 self.api_version, '2.6'):
298 raise self.skipException('Flavors are only available on '
299 'Octavia API version 2.6 or newer.')
300 flavor_name = data_utils.rand_name("lb_admin_flavor-show")
301 flavor_description = data_utils.arbitrary_string(size=255)
302
303 flavor_kwargs = {
304 const.NAME: flavor_name,
305 const.DESCRIPTION: flavor_description,
306 const.ENABLED: True,
307 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
308
309 # Happy path
310 flavor = self.lb_admin_flavor_client.create_flavor(**flavor_kwargs)
311 self.addCleanup(self.lb_admin_flavor_client.cleanup_a_flavor,
312 flavor[const.ID])
313
314 # Test that a user without the load balancer role cannot
315 # show flavor details.
316 expected_allowed = []
317 if CONF.load_balancer.RBAC_test_type == const.OWNERADMIN:
318 expected_allowed = [
319 'os_admin', 'os_primary', 'os_roles_lb_admin',
320 'os_roles_lb_member', 'os_roles_lb_member2']
321 if CONF.load_balancer.RBAC_test_type == const.KEYSTONE_DEFAULT_ROLES:
322 expected_allowed = ['os_admin', 'os_primary', 'os_roles_lb_admin',
323 'os_roles_lb_observer',
324 'os_roles_lb_global_observer',
325 'os_roles_lb_member', 'os_roles_lb_member2']
326 if CONF.load_balancer.RBAC_test_type == const.ADVANCED:
327 expected_allowed = [
328 'os_admin', 'os_roles_lb_admin',
329 'os_roles_lb_observer', 'os_roles_lb_global_observer',
330 'os_roles_lb_member', 'os_roles_lb_member2']
331 if expected_allowed:
332 self.check_show_RBAC_enforcement(
333 'FlavorClient', 'show_flavor', expected_allowed,
334 flavor[const.ID])
335
336 result = self.mem_flavor_client.show_flavor(flavor[const.ID])
337
338 self.assertEqual(flavor[const.ID], result[const.ID])
339 self.assertEqual(flavor_name, result[const.NAME])
340 self.assertEqual(flavor_description, result[const.DESCRIPTION])
341 self.assertTrue(result[const.ENABLED])
342 self.assertEqual(self.flavor_profile_id,
343 result[const.FLAVOR_PROFILE_ID])
344
345 @decorators.idempotent_id('3d9e2820-a68e-4db9-bf94-53cbcff2dc15')
346 def test_flavor_update(self):
347 """Tests flavor update API.
348
349 * Create a fully populated flavor.
350 * Show flavor details.
351 * Validate the show reflects the initial values.
352 * Validates that non-admin accounts cannot update the flavor.
353 * Update the flavor details.
354 * Show flavor details.
355 * Validate the show reflects the updated values.
356 """
357 # We have to do this here as the api_version and clients are not
358 # setup in time to use a decorator or the skip_checks mixin
359 if not self.lb_admin_flavor_client.is_version_supported(
360 self.api_version, '2.6'):
361 raise self.skipException('Flavors are only available on '
362 'Octavia API version 2.6 or newer.')
363 flavor_name = data_utils.rand_name("lb_admin_flavor-update")
364 flavor_description = data_utils.arbitrary_string(size=255)
365
366 flavor_kwargs = {
367 const.NAME: flavor_name,
368 const.DESCRIPTION: flavor_description,
369 const.ENABLED: True,
370 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
371
372 # Happy path
373 flavor = self.lb_admin_flavor_client.create_flavor(**flavor_kwargs)
374 self.addCleanup(self.lb_admin_flavor_client.cleanup_a_flavor,
375 flavor[const.ID])
376
377 flavor_name2 = data_utils.rand_name("lb_admin_flavor-update-2")
378 flavor_description2 = data_utils.arbitrary_string(size=255)
379 flavor_updated_kwargs = {
380 const.NAME: flavor_name2,
381 const.DESCRIPTION: flavor_description2,
382 const.ENABLED: False}
383
384 # Test that a user without the load balancer role cannot
385 # update flavor details.
386 expected_allowed = ['os_admin', 'os_roles_lb_admin']
387 self.check_update_RBAC_enforcement(
388 'FlavorClient', 'update_flavor', expected_allowed, None, None,
389 flavor[const.ID], **flavor_updated_kwargs)
390
391 updated_flavor = self.lb_admin_flavor_client.update_flavor(
392 flavor[const.ID], **flavor_updated_kwargs)
393
394 self.assertEqual(flavor[const.ID], updated_flavor[const.ID])
395 self.assertEqual(flavor_name2, updated_flavor[const.NAME])
396 self.assertEqual(flavor_description2,
397 updated_flavor[const.DESCRIPTION])
398 self.assertEqual(flavor[const.FLAVOR_PROFILE_ID],
399 updated_flavor[const.FLAVOR_PROFILE_ID])
400 self.assertFalse(updated_flavor[const.ENABLED])
401
402 result = self.mem_flavor_client.show_flavor(flavor[const.ID])
403
404 self.assertEqual(flavor[const.ID], result[const.ID])
405 self.assertEqual(flavor_name2, result[const.NAME])
406 self.assertEqual(flavor_description2,
407 result[const.DESCRIPTION])
408 self.assertEqual(flavor[const.FLAVOR_PROFILE_ID],
409 result[const.FLAVOR_PROFILE_ID])
410 self.assertFalse(result[const.ENABLED])
411
412 @decorators.idempotent_id('dfe9173a-26f3-4bba-9e69-d2b817ff2b86')
413 def test_flavor_delete(self):
414 """Tests flavor create and delete APIs.
415
416 * Creates a flavor.
417 * Validates that other accounts cannot delete the flavor.
418 * Deletes the flavor.
419 * Validates the flavor no longer exists.
420 """
421 # We have to do this here as the api_version and clients are not
422 # setup in time to use a decorator or the skip_checks mixin
423 if not self.lb_admin_flavor_client.is_version_supported(
424 self.api_version, '2.6'):
425 raise self.skipException('Flavors are only available on '
426 'Octavia API version 2.6 or newer.')
427 flavor_name = data_utils.rand_name("lb_admin_flavor-delete")
428 flavor_description = data_utils.arbitrary_string(size=255)
429
430 flavor_kwargs = {
431 const.NAME: flavor_name,
432 const.DESCRIPTION: flavor_description,
433 const.ENABLED: True,
434 const.FLAVOR_PROFILE_ID: self.flavor_profile_id}
435
436 # Happy path
437 flavor = self.lb_admin_flavor_client.create_flavor(**flavor_kwargs)
438 self.addCleanup(self.lb_admin_flavor_client.cleanup_a_flavor,
439 flavor[const.ID])
440
441 # Test that a user without the load balancer admin role cannot
442 # delete a flavor.
443 expected_allowed = ['os_admin', 'os_roles_lb_admin']
444 self.check_delete_RBAC_enforcement(
445 'FlavorClient', 'delete_flavor', expected_allowed,
446 None, None, flavor[const.ID])
447
448 # Happy path
449 self.lb_admin_flavor_client.delete_flavor(flavor[const.ID])
450
451 self.assertRaises(exceptions.NotFound,
452 self.lb_admin_flavor_client.show_flavor,
453 flavor[const.ID])