Debian Codegraph @

octavia_tempest_plugin/tests/api/v2/test_flavor_profile.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 FlavorProfileAPITest(test_base.LoadBalancerBaseTest):
32 """Test the flavor profile object API."""
33
34 @decorators.idempotent_id('d0e3a08e-d58a-4460-83ed-34307ca04cde')
35 def test_flavor_profile_create(self):
36 """Tests flavor profile create and basic show APIs.
37
38 * Tests that users without the loadbalancer admin role cannot
39 create flavor profiles.
40 * Create a fully populated flavor profile.
41 * Validate the response reflects the requested values.
42 """
43 # We have to do this here as the api_version and clients are not
44 # setup in time to use a decorator or the skip_checks mixin
45 if not self.lb_admin_flavor_profile_client.is_version_supported(
46 self.api_version, '2.6'):
47 raise self.skipException('Flavor profiles are only available on '
48 'Octavia API version 2.6 or newer.')
49
50 flavor_profile_name = data_utils.rand_name(
51 "lb_admin_flavorprofile1-create")
52 flavor_data = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
53 flavor_data_json = jsonutils.dumps(flavor_data)
54
55 flavor_profile_kwargs = {
56 const.NAME: flavor_profile_name,
57 const.PROVIDER_NAME: CONF.load_balancer.provider,
58 const.FLAVOR_DATA: flavor_data_json
59 }
60
61 # Test that a user without the load balancer admin role cannot
62 # create a flavor profile
63 expected_allowed = ['os_admin', 'os_roles_lb_admin']
64 self.check_create_RBAC_enforcement(
65 'FlavorProfileClient', 'create_flavor_profile',
66 expected_allowed, None, None, **flavor_profile_kwargs)
67
68 # Happy path
69 flavor_profile = (
70 self.lb_admin_flavor_profile_client.create_flavor_profile(
71 **flavor_profile_kwargs))
72 self.addCleanup(
73 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
74 flavor_profile[const.ID])
75
76 UUID(flavor_profile[const.ID])
77 self.assertEqual(flavor_profile_name, flavor_profile[const.NAME])
78 self.assertEqual(CONF.load_balancer.provider,
79 flavor_profile[const.PROVIDER_NAME])
80 self.assertEqual(flavor_data_json, flavor_profile[const.FLAVOR_DATA])
81
82 # Testing that flavor_profiles do not support tags
83 flavor_profile_tags = ["Hello", "World"]
84 tags_flavor_profile_kwargs = flavor_profile_kwargs.copy()
85 tags_flavor_profile_kwargs[const.TAGS] = flavor_profile_tags
86 self.assertRaises(
87 TypeError,
88 self.lb_admin_flavor_profile_client.create_flavor_profile,
89 **tags_flavor_profile_kwargs)
90
91 @decorators.idempotent_id('c4e17fdf-849a-4132-93ae-dfca21ce4444')
92 def test_flavor_profile_list(self):
93 """Tests flavor profile list API and field filtering.
94
95 * Create three flavor profiles.
96 * Validates that non-admin accounts cannot list the flavor profiles.
97 * List the flavor profiles using the default sort order.
98 * List the flavor profiles using descending sort order.
99 * List the flavor profiles using ascending sort order.
100 * List the flavor profiles returning one field at a time.
101 * List the flavor profiles returning two fields.
102 * List the flavor profiles filtering to one of the three.
103 * List the flavor profiles filtered, one field, and sorted.
104 """
105 # We have to do this here as the api_version and clients are not
106 # setup in time to use a decorator or the skip_checks mixin
107 if not self.lb_admin_flavor_profile_client.is_version_supported(
108 self.api_version, '2.6'):
109 raise self.skipException('Flavor profiles are only available on '
110 'Octavia API version 2.6 or newer.')
111
112 # Create flavor profile 1
113 flavor_profile1_name = data_utils.rand_name(
114 "lb_admin_flavorprofile-list-1")
115 flavor_data1 = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
116 flavor_data1_json = jsonutils.dumps(flavor_data1)
117
118 flavor_profile1_kwargs = {
119 const.NAME: flavor_profile1_name,
120 const.PROVIDER_NAME: CONF.load_balancer.provider,
121 const.FLAVOR_DATA: flavor_data1_json
122 }
123 flavor_profile1 = (
124 self.lb_admin_flavor_profile_client.create_flavor_profile(
125 **flavor_profile1_kwargs))
126 self.addCleanup(
127 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
128 flavor_profile1[const.ID])
129
130 # Create flavor profile 2
131 flavor_profile2_name = data_utils.rand_name(
132 "lb_admin_flavorprofile-list-2")
133 flavor_data2 = {const.LOADBALANCER_TOPOLOGY: const.ACTIVE_STANDBY}
134 flavor_data2_json = jsonutils.dumps(flavor_data2)
135
136 flavor_profile2_kwargs = {
137 const.NAME: flavor_profile2_name,
138 const.PROVIDER_NAME: CONF.load_balancer.provider,
139 const.FLAVOR_DATA: flavor_data2_json
140 }
141 flavor_profile2 = (
142 self.lb_admin_flavor_profile_client.create_flavor_profile(
143 **flavor_profile2_kwargs))
144 self.addCleanup(
145 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
146 flavor_profile2[const.ID])
147
148 # Create flavor profile 3
149 flavor_profile3_name = data_utils.rand_name(
150 "lb_admin_flavorprofile-list-3")
151 flavor_data3 = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
152 flavor_data3_json = jsonutils.dumps(flavor_data3)
153
154 flavor_profile3_kwargs = {
155 const.NAME: flavor_profile3_name,
156 const.PROVIDER_NAME: CONF.load_balancer.provider,
157 const.FLAVOR_DATA: flavor_data3_json
158 }
159 flavor_profile3 = (
160 self.lb_admin_flavor_profile_client.create_flavor_profile(
161 **flavor_profile3_kwargs))
162 self.addCleanup(
163 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
164 flavor_profile3[const.ID])
165
166 # default sort order (by ID) reference list
167 ref_id_list_asc = [flavor_profile1[const.ID],
168 flavor_profile2[const.ID],
169 flavor_profile3[const.ID]]
170 ref_id_list_dsc = copy.deepcopy(ref_id_list_asc)
171 ref_id_list_asc.sort()
172 ref_id_list_dsc.sort(reverse=True)
173
174 # Test that a user without the load balancer admin role cannot
175 # list flavor profiles.
176 expected_allowed = ['os_admin', 'os_roles_lb_admin']
177 self.check_list_RBAC_enforcement(
178 'FlavorProfileClient', 'list_flavor_profiles',
179 expected_allowed)
180
181 # Check the default sort order (by ID)
182 profiles = self.lb_admin_flavor_profile_client.list_flavor_profiles()
183 # Remove flavor profiles not used in this test
184 profiles = [prof for prof in profiles
185 if 'lb_admin_flavorprofile-list' in prof[const.NAME]]
186 self.assertEqual(3, len(profiles))
187 self.assertEqual(ref_id_list_asc[0], profiles[0][const.ID])
188 self.assertEqual(ref_id_list_asc[1], profiles[1][const.ID])
189 self.assertEqual(ref_id_list_asc[2], profiles[2][const.ID])
190
191 # Check the descending sort order by name
192 profiles = self.lb_admin_flavor_profile_client.list_flavor_profiles(
193 query_params='{sort}={name}:{order}'.format(
194 sort=const.SORT, name=const.NAME, order=const.DESC))
195 # Remove flavor profiles not used in this test
196 profiles = [prof for prof in profiles
197 if 'lb_admin_flavorprofile-list' in prof[const.NAME]]
198 self.assertEqual(3, len(profiles))
199 self.assertEqual(flavor_profile3_name, profiles[0][const.NAME])
200 self.assertEqual(flavor_profile2_name, profiles[1][const.NAME])
201 self.assertEqual(flavor_profile1_name, profiles[2][const.NAME])
202
203 # Check the ascending sort order by name
204 profiles = self.lb_admin_flavor_profile_client.list_flavor_profiles(
205 query_params='{sort}={name}:{order}'.format(
206 sort=const.SORT, name=const.NAME, order=const.ASC))
207 # Remove flavor profiles not used in this test
208 profiles = [prof for prof in profiles
209 if 'lb_admin_flavorprofile-list' in prof[const.NAME]]
210 self.assertEqual(3, len(profiles))
211 self.assertEqual(flavor_profile1_name, profiles[0][const.NAME])
212 self.assertEqual(flavor_profile2_name, profiles[1][const.NAME])
213 self.assertEqual(flavor_profile3_name, profiles[2][const.NAME])
214
215 ref_profiles = [flavor_profile1, flavor_profile2, flavor_profile3]
216 sorted_profiles = sorted(ref_profiles, key=itemgetter(const.ID))
217
218 # Test fields
219 flavor_profile_client = self.lb_admin_flavor_profile_client
220 for field in const.SHOW_FLAVOR_PROFILE_FIELDS:
221 profiles = flavor_profile_client.list_flavor_profiles(
222 query_params='{fields}={field}&{fields}={name}'.format(
223 fields=const.FIELDS, field=field, name=const.NAME))
224 # Remove flavor profiles not used in this test
225 profiles = [prof for prof in profiles
226 if 'lb_admin_flavorprofile-list' in prof[const.NAME]]
227
228 self.assertEqual(3, len(profiles))
229 self.assertEqual(sorted_profiles[0][field], profiles[0][field])
230 self.assertEqual(sorted_profiles[1][field], profiles[1][field])
231 self.assertEqual(sorted_profiles[2][field], profiles[2][field])
232
233 # Test filtering
234 profile = self.lb_admin_flavor_profile_client.list_flavor_profiles(
235 query_params='{name}={prof_name}'.format(
236 name=const.NAME,
237 prof_name=flavor_profile2[const.NAME]))
238 self.assertEqual(1, len(profile))
239 self.assertEqual(flavor_profile2[const.ID], profile[0][const.ID])
240
241 # Test combined params
242 profiles = self.lb_admin_flavor_profile_client.list_flavor_profiles(
243 query_params='{provider_name}={provider}&{fields}={name}&'
244 '{sort}={ID}:{desc}'.format(
245 provider_name=const.PROVIDER_NAME,
246 provider=CONF.load_balancer.provider,
247 fields=const.FIELDS, name=const.NAME,
248 sort=const.SORT, ID=const.ID,
249 desc=const.DESC))
250 # Remove flavor profiles not used in this test
251 profiles = [prof for prof in profiles
252 if 'lb_admin_flavorprofile-list' in prof[const.NAME]]
253 self.assertEqual(3, len(profiles))
254 self.assertEqual(1, len(profiles[0]))
255 self.assertEqual(sorted_profiles[2][const.NAME],
256 profiles[0][const.NAME])
257 self.assertEqual(sorted_profiles[1][const.NAME],
258 profiles[1][const.NAME])
259 self.assertEqual(sorted_profiles[0][const.NAME],
260 profiles[2][const.NAME])
261
262 @decorators.idempotent_id('a2c2ff9a-fce1-42fd-8cfd-56dea31610f6')
263 def test_flavor_profile_show(self):
264 """Tests flavor profile show API.
265
266 * Create a fully populated flavor profile.
267 * Show flavor profile details.
268 * Validate the show reflects the requested values.
269 * Validates that non-lb-admin accounts cannot see the flavor profile.
270 """
271 # We have to do this here as the api_version and clients are not
272 # setup in time to use a decorator or the skip_checks mixin
273 if not self.lb_admin_flavor_profile_client.is_version_supported(
274 self.api_version, '2.6'):
275 raise self.skipException('Flavor profiles are only available on '
276 'Octavia API version 2.6 or newer.')
277
278 flavor_profile_name = data_utils.rand_name(
279 "lb_admin_flavorprofile1-show")
280 flavor_data = {const.LOADBALANCER_TOPOLOGY: const.ACTIVE_STANDBY}
281 flavor_data_json = jsonutils.dumps(flavor_data)
282
283 flavor_profile_kwargs = {
284 const.NAME: flavor_profile_name,
285 const.PROVIDER_NAME: CONF.load_balancer.provider,
286 const.FLAVOR_DATA: flavor_data_json
287 }
288
289 flavor_profile = (
290 self.lb_admin_flavor_profile_client.create_flavor_profile(
291 **flavor_profile_kwargs))
292 self.addCleanup(
293 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
294 flavor_profile[const.ID])
295
296 # Test that a user without the load balancer admin role cannot
297 # show a flavor profile.
298 expected_allowed = ['os_admin', 'os_roles_lb_admin']
299 self.check_show_RBAC_enforcement(
300 'FlavorProfileClient', 'show_flavor_profile',
301 expected_allowed, flavor_profile[const.ID])
302
303 result = (
304 self.lb_admin_flavor_profile_client.show_flavor_profile(
305 flavor_profile[const.ID]))
306
307 self.assertEqual(flavor_profile_name, result[const.NAME])
308 self.assertEqual(CONF.load_balancer.provider,
309 result[const.PROVIDER_NAME])
310 self.assertEqual(flavor_data_json, result[const.FLAVOR_DATA])
311
312 @decorators.idempotent_id('32a2e285-8dfc-485f-a450-a4d450d3c3ec')
313 def test_flavor_profile_update(self):
314 """Tests flavor profile update API.
315
316 * Create a fully populated flavor profile.
317 * Show flavor profile details.
318 * Validate the show reflects the initial values.
319 * Validates that non-admin accounts cannot update the flavor profile.
320 * Update the flavor profile details.
321 * Show flavor profile details.
322 * Validate the show reflects the updated values.
323 """
324
325 # We have to do this here as the api_version and clients are not
326 # setup in time to use a decorator or the skip_checks mixin
327 if not self.lb_admin_flavor_profile_client.is_version_supported(
328 self.api_version, '2.6'):
329 raise self.skipException('Flavor profiles are only available on '
330 'Octavia API version 2.6 or newer.')
331
332 flavor_profile_name = data_utils.rand_name(
333 "lb_admin_flavorprofile1-update")
334 flavor_data = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
335 flavor_data_json = jsonutils.dumps(flavor_data)
336
337 flavor_profile_kwargs = {
338 const.NAME: flavor_profile_name,
339 const.PROVIDER_NAME: CONF.load_balancer.provider,
340 const.FLAVOR_DATA: flavor_data_json
341 }
342
343 flavor_profile = (
344 self.lb_admin_flavor_profile_client.create_flavor_profile(
345 **flavor_profile_kwargs))
346 self.addCleanup(
347 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
348 flavor_profile[const.ID])
349
350 self.assertEqual(flavor_profile_name, flavor_profile[const.NAME])
351 self.assertEqual(CONF.load_balancer.provider,
352 flavor_profile[const.PROVIDER_NAME])
353 self.assertEqual(flavor_data_json, flavor_profile[const.FLAVOR_DATA])
354
355 flavor_profile_name2 = data_utils.rand_name(
356 "lb_admin_flavorprofile1-update2")
357 flavor_data2 = {const.LOADBALANCER_TOPOLOGY: const.ACTIVE_STANDBY}
358 flavor_data2_json = jsonutils.dumps(flavor_data2)
359
360 # TODO(johnsom) Figure out a reliable second provider
361 flavor_profile_updated_kwargs = {
362 const.NAME: flavor_profile_name2,
363 const.PROVIDER_NAME: CONF.load_balancer.provider,
364 const.FLAVOR_DATA: flavor_data2_json
365 }
366
367 # Test that a user without the load balancer admin role cannot
368 # update a flavor profile.
369 expected_allowed = ['os_admin', 'os_roles_lb_admin']
370 self.check_update_RBAC_enforcement(
371 'FlavorProfileClient', 'update_flavor_profile',
372 expected_allowed, None, None, flavor_profile[const.ID],
373 **flavor_profile_updated_kwargs)
374
375 result = self.lb_admin_flavor_profile_client.update_flavor_profile(
376 flavor_profile[const.ID], **flavor_profile_updated_kwargs)
377
378 self.assertEqual(flavor_profile_name2, result[const.NAME])
379 self.assertEqual(CONF.load_balancer.provider,
380 result[const.PROVIDER_NAME])
381 self.assertEqual(flavor_data2_json, result[const.FLAVOR_DATA])
382
383 # Check that a show reflects the new values
384 get_result = (
385 self.lb_admin_flavor_profile_client.show_flavor_profile(
386 flavor_profile[const.ID]))
387
388 self.assertEqual(flavor_profile_name2, get_result[const.NAME])
389 self.assertEqual(CONF.load_balancer.provider,
390 get_result[const.PROVIDER_NAME])
391 self.assertEqual(flavor_data2_json, get_result[const.FLAVOR_DATA])
392
393 @decorators.idempotent_id('4c2eaacf-c2c8-422a-b7dc-a30ceba6bcd4')
394 def test_flavor_profile_delete(self):
395 """Tests flavor profile create and delete APIs.
396
397 * Creates a flavor profile.
398 * Validates that other accounts cannot delete the flavor profile.
399 * Deletes the flavor profile.
400 * Validates the flavor profile is in the DELETED state.
401 """
402 # We have to do this here as the api_version and clients are not
403 # setup in time to use a decorator or the skip_checks mixin
404 if not self.lb_admin_flavor_profile_client.is_version_supported(
405 self.api_version, '2.6'):
406 raise self.skipException('Flavor profiles are only available on '
407 'Octavia API version 2.6 or newer.')
408
409 flavor_profile_name = data_utils.rand_name(
410 "lb_admin_flavorprofile1-delete")
411 flavor_data = {const.LOADBALANCER_TOPOLOGY: const.SINGLE}
412 flavor_data_json = jsonutils.dumps(flavor_data)
413
414 flavor_profile_kwargs = {
415 const.NAME: flavor_profile_name,
416 const.PROVIDER_NAME: CONF.load_balancer.provider,
417 const.FLAVOR_DATA: flavor_data_json
418 }
419
420 flavor_profile = (
421 self.lb_admin_flavor_profile_client.create_flavor_profile(
422 **flavor_profile_kwargs))
423 self.addCleanup(
424 self.lb_admin_flavor_profile_client.cleanup_flavor_profile,
425 flavor_profile[const.ID])
426
427 # Test that a user without the load balancer admin role cannot
428 # delete a flavor profile
429 expected_allowed = ['os_admin', 'os_roles_lb_admin']
430 self.check_delete_RBAC_enforcement(
431 'FlavorProfileClient', 'delete_flavor_profile',
432 expected_allowed, None, None, flavor_profile[const.ID])
433
434 # Happy path
435 self.lb_admin_flavor_profile_client.delete_flavor_profile(
436 flavor_profile[const.ID])
437
438 self.assertRaises(
439 exceptions.NotFound,
440 self.lb_admin_flavor_profile_client.show_flavor_profile,
441 flavor_profile[const.ID])