Debian Codegraph @

neutron_lib/tests/unit/placement/test_utils.py

raw
1 # Copyright 2018 Ericsson
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from unittest import mock
16 import uuid
17
18 from neutron_lib.placement import utils as place_utils
19 from neutron_lib.tests import _base as base
20
21
22 class TestPlacementUtils(base.BaseTestCase):
23
24 def setUp(self):
25 super().setUp()
26
27 self._uuid_ns = uuid.UUID('94fedd4d-1ce0-4bb3-9c9a-c9c0f56de154')
28
29 def test_physnet_trait(self):
30 self.assertEqual(
31 'CUSTOM_PHYSNET_SOME_PHYSNET',
32 place_utils.physnet_trait('some-physnet'))
33
34 def test_vnic_type_trait(self):
35 self.assertEqual(
36 'CUSTOM_VNIC_TYPE_SOMEVNICTYPE',
37 place_utils.vnic_type_trait('somevnictype'))
38
39 def test_six_uuid5_literal(self):
40 try:
41 # assertNotRaises
42 place_utils.six_uuid5(
43 namespace=self._uuid_ns,
44 name='may or may not be a unicode string' +
45 ' depending on Python version')
46 except Exception:
47 self.fail('could not generate uuid')
48
49 def test_six_uuid5_unicode(self):
50 try:
51 # assertNotRaises
52 place_utils.six_uuid5(
53 namespace=self._uuid_ns,
54 name='unicode string')
55 except Exception:
56 self.fail('could not generate uuid')
57
58 def test_agent_resource_provider_uuid(self):
59 try:
60 # assertNotRaises
61 place_utils.agent_resource_provider_uuid(
62 namespace=self._uuid_ns,
63 host='some host')
64 except Exception:
65 self.fail('could not generate agent resource provider uuid')
66
67 def test_device_resource_provider_uuid(self):
68 try:
69 # assertNotRaises
70 place_utils.device_resource_provider_uuid(
71 namespace=self._uuid_ns,
72 host='some host',
73 device='some device')
74 except Exception:
75 self.fail('could not generate device resource provider uuid')
76
77 def test_resource_request_group_uuid(self):
78 try:
79 # assertNotRaises
80 place_utils.resource_request_group_uuid(
81 namespace=self._uuid_ns,
82 qos_rules=[
83 mock.MagicMock(id='fake_id_0'),
84 mock.MagicMock(id='fake_id_1')
85 ])
86 except Exception:
87 self.fail('could not generate resource request group uuid')
88
89 def test_agent_resource_provider_uuid_stable(self):
90 uuid_a = place_utils.agent_resource_provider_uuid(
91 namespace=self._uuid_ns,
92 host='somehost')
93 uuid_b = place_utils.agent_resource_provider_uuid(
94 namespace=self._uuid_ns,
95 host='somehost')
96 self.assertEqual(uuid_a, uuid_b)
97
98 def test_device_resource_provider_uuid_stable(self):
99 uuid_a = place_utils.device_resource_provider_uuid(
100 namespace=self._uuid_ns,
101 host='somehost',
102 device='some-device')
103 uuid_b = place_utils.device_resource_provider_uuid(
104 namespace=self._uuid_ns,
105 host='somehost',
106 device='some-device')
107 self.assertEqual(uuid_a, uuid_b)
108
109 def test_resource_request_group_uuid_stable(self):
110 uuid_a = place_utils.resource_request_group_uuid(
111 namespace=self._uuid_ns,
112 qos_rules=[
113 mock.MagicMock(id='fake_id_0'),
114 mock.MagicMock(id='fake_id_1')
115 ]
116 )
117 uuid_b = place_utils.resource_request_group_uuid(
118 namespace=self._uuid_ns,
119 qos_rules=[
120 mock.MagicMock(id='fake_id_0'),
121 mock.MagicMock(id='fake_id_1')
122 ]
123 )
124 self.assertEqual(uuid_a, uuid_b)
125
126 def test_parse_rp_bandwidths(self):
127 self.assertEqual(
128 {},
129 place_utils.parse_rp_bandwidths([]),
130 )
131
132 self.assertEqual(
133 {'eth0': {'egress': None, 'ingress': None}},
134 place_utils.parse_rp_bandwidths(['eth0']),
135 )
136
137 self.assertEqual(
138 {'eth0': {'egress': None, 'ingress': None}},
139 place_utils.parse_rp_bandwidths(['eth0::']),
140 )
141
142 self.assertRaises(
143 ValueError,
144 place_utils.parse_rp_bandwidths,
145 ['eth0::', 'eth0::'],
146 )
147
148 self.assertRaises(
149 ValueError,
150 place_utils.parse_rp_bandwidths,
151 ['eth0:not a number:not a number'],
152 )
153
154 self.assertEqual(
155 {'eth0': {'egress': 1, 'ingress': None}},
156 place_utils.parse_rp_bandwidths(['eth0:1:']),
157 )
158
159 self.assertEqual(
160 {'eth0': {'egress': None, 'ingress': 1}},
161 place_utils.parse_rp_bandwidths(['eth0::1']),
162 )
163
164 self.assertEqual(
165 {'eth0': {'egress': 1, 'ingress': 1}},
166 place_utils.parse_rp_bandwidths(['eth0:1:1']),
167 )
168
169 self.assertEqual(
170 {'eth0': {'egress': 1, 'ingress': 1},
171 'eth1': {'egress': 10, 'ingress': 10}},
172 place_utils.parse_rp_bandwidths(['eth0:1:1', 'eth1:10:10']),
173 )
174
175 def test_parse_rp_pp_with_direction(self):
176 self.assertEqual(
177 {},
178 place_utils.parse_rp_pp_with_direction([], 'default_host'),
179 )
180
181 self.assertEqual(
182 {'host0': {'egress': None, 'ingress': None}},
183 place_utils.parse_rp_pp_with_direction(['host0'],
184 'default_host'),
185 )
186
187 self.assertEqual(
188 {'host0': {'egress': None, 'ingress': None}},
189 place_utils.parse_rp_pp_with_direction(['host0::'],
190 'default_host'),
191 )
192
193 self.assertEqual(
194 {'default_host': {'egress': None, 'ingress': None}},
195 place_utils.parse_rp_pp_with_direction(['::'],
196 'default_host'),
197 )
198
199 self.assertEqual(
200 {'host0': {'egress': 1, 'ingress': None}},
201 place_utils.parse_rp_pp_with_direction(['host0:1:'],
202 'default_host'),
203 )
204
205 self.assertEqual(
206 {'host0': {'egress': None, 'ingress': 1}},
207 place_utils.parse_rp_pp_with_direction(['host0::1'],
208 'default_host'),
209 )
210
211 self.assertEqual(
212 {'host0': {'egress': 1, 'ingress': 1}},
213 place_utils.parse_rp_pp_with_direction(['host0:1:1'],
214 'default_host'),
215 )
216
217 self.assertEqual(
218 {'default_host': {'egress': 0, 'ingress': 0}},
219 place_utils.parse_rp_pp_with_direction([':0:0'],
220 'default_host'),
221 )
222
223 self.assertEqual(
224 {'host0': {'egress': 1, 'ingress': 1},
225 'host1': {'egress': 10, 'ingress': 10}},
226 place_utils.parse_rp_pp_with_direction(
227 ['host0:1:1', 'host1:10:10'],
228 'default_host'),
229 )
230
231 self.assertRaises(
232 ValueError,
233 place_utils.parse_rp_pp_with_direction,
234 ['default_host::', '::'], 'default_host',
235 )
236
237 self.assertRaises(
238 ValueError,
239 place_utils.parse_rp_pp_with_direction,
240 ['host0::', 'host0::'], 'default_host',
241 )
242
243 self.assertRaises(
244 ValueError,
245 place_utils.parse_rp_pp_with_direction,
246 ['host0:not a number:not a number'], 'default_host',
247 )
248
249 def test_parse_rp_pp_without_direction(self):
250 self.assertEqual(
251 {},
252 place_utils.parse_rp_pp_without_direction([], 'default_host'),
253 )
254
255 self.assertEqual(
256 {'host0': {'any': None}},
257 place_utils.parse_rp_pp_without_direction(['host0'],
258 'default_host'),
259 )
260
261 self.assertEqual(
262 {'host0': {'any': None}},
263 place_utils.parse_rp_pp_without_direction(['host0:'],
264 'default_host'),
265 )
266
267 self.assertEqual(
268 {'host0': {'any': 1}},
269 place_utils.parse_rp_pp_without_direction(['host0:1'],
270 'default_host'),
271 )
272
273 self.assertEqual(
274 {'default_host': {'any': None}},
275 place_utils.parse_rp_pp_without_direction([':'],
276 'default_host'),
277 )
278
279 self.assertEqual(
280 {'default_host': {'any': 0}},
281 place_utils.parse_rp_pp_without_direction([':0'],
282 'default_host'),
283 )
284
285 self.assertEqual(
286 {'host0': {'any': 1},
287 'host1': {'any': 10}},
288 place_utils.parse_rp_pp_without_direction(
289 ['host0:1', 'host1:10'],
290 'default_host'),
291 )
292
293 self.assertRaises(
294 ValueError,
295 place_utils.parse_rp_pp_without_direction,
296 ['default_host:', ':'], 'default_host',
297 )
298
299 self.assertRaises(
300 ValueError,
301 place_utils.parse_rp_pp_without_direction,
302 ['host0:', 'host0:'], 'default_host',
303 )
304
305 self.assertRaises(
306 ValueError,
307 place_utils.parse_rp_pp_without_direction,
308 ['host0:not a number'], 'default_host',
309 )
310
311 def test_parse_rp_options(self):
312 self.assertEqual(
313 {},
314 place_utils._parse_rp_options([], tuple()),
315 )
316
317 self.assertEqual(
318 {'outer_key0': {'inner_key0': None}},
319 place_utils._parse_rp_options(['outer_key0'], ('inner_key0',)),
320 )
321 self.assertEqual(
322 {'outer_key0': {'inner_key0': None},
323 'outer_key1': {'inner_key0': None},
324 'outer_key2': {'inner_key0': None},
325 'outer_key3': {'inner_key0': None}},
326 place_utils._parse_rp_options(
327 ['outer_key0', 'outer_key1', 'outer_key2', 'outer_key3'],
328 ('inner_key0',)),
329 )
330
331 self.assertEqual(
332 {'outer_key0': {
333 'inner_key0': None,
334 'inner_key1': None,
335 'inner_key2': None,
336 'inner_key3': None}},
337 place_utils._parse_rp_options(
338 ['outer_key0'],
339 ('inner_key0', 'inner_key1', 'inner_key2', 'inner_key3')),
340 )
341
342 self.assertEqual(
343 {'outer_key0': {
344 'inner_key0': None,
345 'inner_key1': None,
346 'inner_key2': None,
347 'inner_key3': None},
348 'outer_key1': {
349 'inner_key0': None,
350 'inner_key1': None,
351 'inner_key2': None,
352 'inner_key3': None},
353 '': {
354 'inner_key0': None,
355 'inner_key1': None,
356 'inner_key2': None,
357 'inner_key3': None},
358 'outer_key3': {
359 'inner_key0': None,
360 'inner_key1': 1,
361 'inner_key2': 2,
362 'inner_key3': 3}},
363 place_utils._parse_rp_options(
364 ['outer_key0', 'outer_key1::::', '::::', 'outer_key3::1:2:3'],
365 ('inner_key0', 'inner_key1', 'inner_key2', 'inner_key3')),
366 )
367
368 self.assertRaises(
369 ValueError,
370 place_utils._parse_rp_options,
371 ['outer_key0:'], ('inner_key1', 'inner_key2'),
372 )
373
374 self.assertRaises(
375 ValueError,
376 place_utils._parse_rp_options,
377 ['outer_key0::'], ('inner_key1',),
378 )
379
380 def test_parse_rp_inventory_defaults(self):
381 self.assertEqual(
382 {},
383 place_utils.parse_rp_inventory_defaults({}),
384 )
385
386 self.assertRaises(
387 ValueError,
388 place_utils.parse_rp_inventory_defaults,
389 {'allocation_ratio': '-1.0'}
390 )
391
392 self.assertEqual(
393 {'allocation_ratio': 1.0},
394 place_utils.parse_rp_inventory_defaults(
395 {'allocation_ratio': '1.0'}),
396 )
397
398 self.assertRaises(
399 ValueError,
400 place_utils.parse_rp_inventory_defaults,
401 {'min_unit': '-1'}
402 )
403
404 self.assertEqual(
405 {'min_unit': 1},
406 place_utils.parse_rp_inventory_defaults(
407 {'min_unit': '1'}),
408 )
409
410 self.assertRaises(
411 ValueError,
412 place_utils.parse_rp_inventory_defaults,
413 {'no such inventory parameter': 1}
414 )