Fixing RO Security Vulnerabilities
[osm/RO.git] / NG-RO / osm_ng_ro / tests / test_ns.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17
18 import unittest
19 from unittest.mock import MagicMock, Mock, patch
20
21 from jinja2 import (
22 Environment,
23 select_autoescape,
24 StrictUndefined,
25 TemplateError,
26 TemplateNotFound,
27 UndefinedError,
28 )
29 from osm_ng_ro.ns import Ns, NsException
30
31
32 __author__ = "Eduardo Sousa"
33 __date__ = "$19-NOV-2021 00:00:00$"
34
35
36 class TestNs(unittest.TestCase):
37 def setUp(self):
38 pass
39
40 def test__create_task_without_extra_dict(self):
41 expected_result = {
42 "target_id": "vim_openstack_1",
43 "action_id": "123456",
44 "nsr_id": "654321",
45 "task_id": "123456:1",
46 "status": "SCHEDULED",
47 "action": "CREATE",
48 "item": "test_item",
49 "target_record": "test_target_record",
50 "target_record_id": "test_target_record_id",
51 }
52 deployment_info = {
53 "action_id": "123456",
54 "nsr_id": "654321",
55 "task_index": 1,
56 }
57
58 task = Ns._create_task(
59 deployment_info=deployment_info,
60 target_id="vim_openstack_1",
61 item="test_item",
62 action="CREATE",
63 target_record="test_target_record",
64 target_record_id="test_target_record_id",
65 )
66
67 self.assertEqual(deployment_info.get("task_index"), 2)
68 self.assertDictEqual(task, expected_result)
69
70 def test__create_task(self):
71 expected_result = {
72 "target_id": "vim_openstack_1",
73 "action_id": "123456",
74 "nsr_id": "654321",
75 "task_id": "123456:1",
76 "status": "SCHEDULED",
77 "action": "CREATE",
78 "item": "test_item",
79 "target_record": "test_target_record",
80 "target_record_id": "test_target_record_id",
81 # values coming from extra_dict
82 "params": "test_params",
83 "find_params": "test_find_params",
84 "depends_on": "test_depends_on",
85 }
86 deployment_info = {
87 "action_id": "123456",
88 "nsr_id": "654321",
89 "task_index": 1,
90 }
91
92 task = Ns._create_task(
93 deployment_info=deployment_info,
94 target_id="vim_openstack_1",
95 item="test_item",
96 action="CREATE",
97 target_record="test_target_record",
98 target_record_id="test_target_record_id",
99 extra_dict={
100 "params": "test_params",
101 "find_params": "test_find_params",
102 "depends_on": "test_depends_on",
103 },
104 )
105
106 self.assertEqual(deployment_info.get("task_index"), 2)
107 self.assertDictEqual(task, expected_result)
108
109 @patch("osm_ng_ro.ns.time")
110 def test__create_ro_task(self, mock_time: Mock):
111 now = 1637324838.994551
112 mock_time.return_value = now
113 task = {
114 "target_id": "vim_openstack_1",
115 "action_id": "123456",
116 "nsr_id": "654321",
117 "task_id": "123456:1",
118 "status": "SCHEDULED",
119 "action": "CREATE",
120 "item": "test_item",
121 "target_record": "test_target_record",
122 "target_record_id": "test_target_record_id",
123 # values coming from extra_dict
124 "params": "test_params",
125 "find_params": "test_find_params",
126 "depends_on": "test_depends_on",
127 }
128 expected_result = {
129 "_id": "123456:1",
130 "locked_by": None,
131 "locked_at": 0.0,
132 "target_id": "vim_openstack_1",
133 "vim_info": {
134 "created": False,
135 "created_items": None,
136 "vim_id": None,
137 "vim_name": None,
138 "vim_status": None,
139 "vim_details": None,
140 "vim_message": None,
141 "refresh_at": None,
142 },
143 "modified_at": now,
144 "created_at": now,
145 "to_check_at": now,
146 "tasks": [task],
147 }
148
149 ro_task = Ns._create_ro_task(
150 target_id="vim_openstack_1",
151 task=task,
152 )
153
154 self.assertDictEqual(ro_task, expected_result)
155
156 def test__process_image_params_with_empty_target_image(self):
157 expected_result = {
158 "find_params": {},
159 }
160 target_image = {}
161
162 result = Ns._process_image_params(
163 target_image=target_image,
164 indata=None,
165 vim_info=None,
166 target_record_id=None,
167 )
168
169 self.assertDictEqual(expected_result, result)
170
171 def test__process_image_params_with_wrong_target_image(self):
172 expected_result = {
173 "find_params": {},
174 }
175 target_image = {
176 "no_image": "to_see_here",
177 }
178
179 result = Ns._process_image_params(
180 target_image=target_image,
181 indata=None,
182 vim_info=None,
183 target_record_id=None,
184 )
185
186 self.assertDictEqual(expected_result, result)
187
188 def test__process_image_params_with_image(self):
189 expected_result = {
190 "find_params": {
191 "filter_dict": {
192 "name": "cirros",
193 },
194 },
195 }
196 target_image = {
197 "image": "cirros",
198 }
199
200 result = Ns._process_image_params(
201 target_image=target_image,
202 indata=None,
203 vim_info=None,
204 target_record_id=None,
205 )
206
207 self.assertDictEqual(expected_result, result)
208
209 def test__process_image_params_with_vim_image_id(self):
210 expected_result = {
211 "find_params": {
212 "filter_dict": {
213 "id": "123456",
214 },
215 },
216 }
217 target_image = {
218 "vim_image_id": "123456",
219 }
220
221 result = Ns._process_image_params(
222 target_image=target_image,
223 indata=None,
224 vim_info=None,
225 target_record_id=None,
226 )
227
228 self.assertDictEqual(expected_result, result)
229
230 def test__process_image_params_with_image_checksum(self):
231 expected_result = {
232 "find_params": {
233 "filter_dict": {
234 "checksum": "e3fc50a88d0a364313df4b21ef20c29e",
235 },
236 },
237 }
238 target_image = {
239 "image_checksum": "e3fc50a88d0a364313df4b21ef20c29e",
240 }
241
242 result = Ns._process_image_params(
243 target_image=target_image,
244 indata=None,
245 vim_info=None,
246 target_record_id=None,
247 )
248
249 self.assertDictEqual(expected_result, result)
250
251 def test__get_resource_allocation_params_with_empty_target_image(self):
252 expected_result = {}
253 quota_descriptor = {}
254
255 result = Ns._get_resource_allocation_params(
256 quota_descriptor=quota_descriptor,
257 )
258
259 self.assertDictEqual(expected_result, result)
260
261 def test__get_resource_allocation_params_with_wrong_target_image(self):
262 expected_result = {}
263 quota_descriptor = {
264 "no_quota": "present_here",
265 }
266
267 result = Ns._get_resource_allocation_params(
268 quota_descriptor=quota_descriptor,
269 )
270
271 self.assertDictEqual(expected_result, result)
272
273 def test__get_resource_allocation_params_with_limit(self):
274 expected_result = {
275 "limit": 10,
276 }
277 quota_descriptor = {
278 "limit": "10",
279 }
280
281 result = Ns._get_resource_allocation_params(
282 quota_descriptor=quota_descriptor,
283 )
284
285 self.assertDictEqual(expected_result, result)
286
287 def test__get_resource_allocation_params_with_reserve(self):
288 expected_result = {
289 "reserve": 20,
290 }
291 quota_descriptor = {
292 "reserve": "20",
293 }
294
295 result = Ns._get_resource_allocation_params(
296 quota_descriptor=quota_descriptor,
297 )
298
299 self.assertDictEqual(expected_result, result)
300
301 def test__get_resource_allocation_params_with_shares(self):
302 expected_result = {
303 "shares": 30,
304 }
305 quota_descriptor = {
306 "shares": "30",
307 }
308
309 result = Ns._get_resource_allocation_params(
310 quota_descriptor=quota_descriptor,
311 )
312
313 self.assertDictEqual(expected_result, result)
314
315 def test__get_resource_allocation_params(self):
316 expected_result = {
317 "limit": 10,
318 "reserve": 20,
319 "shares": 30,
320 }
321 quota_descriptor = {
322 "limit": "10",
323 "reserve": "20",
324 "shares": "30",
325 }
326
327 result = Ns._get_resource_allocation_params(
328 quota_descriptor=quota_descriptor,
329 )
330
331 self.assertDictEqual(expected_result, result)
332
333 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
334 def test__process_guest_epa_quota_params_with_empty_quota_epa_cpu(
335 self,
336 resource_allocation,
337 ):
338 expected_result = {}
339 guest_epa_quota = {}
340 epa_vcpu_set = True
341
342 result = Ns._process_guest_epa_quota_params(
343 guest_epa_quota=guest_epa_quota,
344 epa_vcpu_set=epa_vcpu_set,
345 )
346
347 self.assertDictEqual(expected_result, result)
348 self.assertFalse(resource_allocation.called)
349
350 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
351 def test__process_guest_epa_quota_params_with_empty_quota_false_epa_cpu(
352 self,
353 resource_allocation,
354 ):
355 expected_result = {}
356 guest_epa_quota = {}
357 epa_vcpu_set = False
358
359 result = Ns._process_guest_epa_quota_params(
360 guest_epa_quota=guest_epa_quota,
361 epa_vcpu_set=epa_vcpu_set,
362 )
363
364 self.assertDictEqual(expected_result, result)
365 self.assertFalse(resource_allocation.called)
366
367 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
368 def test__process_guest_epa_quota_params_with_wrong_quota_epa_cpu(
369 self,
370 resource_allocation,
371 ):
372 expected_result = {}
373 guest_epa_quota = {
374 "no-quota": "nothing",
375 }
376 epa_vcpu_set = True
377
378 result = Ns._process_guest_epa_quota_params(
379 guest_epa_quota=guest_epa_quota,
380 epa_vcpu_set=epa_vcpu_set,
381 )
382
383 self.assertDictEqual(expected_result, result)
384 self.assertFalse(resource_allocation.called)
385
386 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
387 def test__process_guest_epa_quota_params_with_wrong_quota_false_epa_cpu(
388 self,
389 resource_allocation,
390 ):
391 expected_result = {}
392 guest_epa_quota = {
393 "no-quota": "nothing",
394 }
395 epa_vcpu_set = False
396
397 result = Ns._process_guest_epa_quota_params(
398 guest_epa_quota=guest_epa_quota,
399 epa_vcpu_set=epa_vcpu_set,
400 )
401
402 self.assertDictEqual(expected_result, result)
403 self.assertFalse(resource_allocation.called)
404
405 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
406 def test__process_guest_epa_quota_params_with_cpu_quota_epa_cpu(
407 self,
408 resource_allocation,
409 ):
410 expected_result = {}
411 guest_epa_quota = {
412 "cpu-quota": {
413 "limit": "10",
414 "reserve": "20",
415 "shares": "30",
416 },
417 }
418 epa_vcpu_set = True
419
420 result = Ns._process_guest_epa_quota_params(
421 guest_epa_quota=guest_epa_quota,
422 epa_vcpu_set=epa_vcpu_set,
423 )
424
425 self.assertDictEqual(expected_result, result)
426 self.assertFalse(resource_allocation.called)
427
428 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
429 def test__process_guest_epa_quota_params_with_cpu_quota_false_epa_cpu(
430 self,
431 resource_allocation,
432 ):
433 expected_result = {
434 "cpu-quota": {
435 "limit": 10,
436 "reserve": 20,
437 "shares": 30,
438 },
439 }
440 guest_epa_quota = {
441 "cpu-quota": {
442 "limit": "10",
443 "reserve": "20",
444 "shares": "30",
445 },
446 }
447 epa_vcpu_set = False
448
449 resource_allocation_param = {
450 "limit": "10",
451 "reserve": "20",
452 "shares": "30",
453 }
454 resource_allocation.return_value = {
455 "limit": 10,
456 "reserve": 20,
457 "shares": 30,
458 }
459
460 result = Ns._process_guest_epa_quota_params(
461 guest_epa_quota=guest_epa_quota,
462 epa_vcpu_set=epa_vcpu_set,
463 )
464
465 resource_allocation.assert_called_once_with(resource_allocation_param)
466 self.assertDictEqual(expected_result, result)
467
468 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
469 def test__process_guest_epa_quota_params_with_mem_quota_epa_cpu(
470 self,
471 resource_allocation,
472 ):
473 expected_result = {
474 "mem-quota": {
475 "limit": 10,
476 "reserve": 20,
477 "shares": 30,
478 },
479 }
480 guest_epa_quota = {
481 "mem-quota": {
482 "limit": "10",
483 "reserve": "20",
484 "shares": "30",
485 },
486 }
487 epa_vcpu_set = True
488
489 resource_allocation_param = {
490 "limit": "10",
491 "reserve": "20",
492 "shares": "30",
493 }
494 resource_allocation.return_value = {
495 "limit": 10,
496 "reserve": 20,
497 "shares": 30,
498 }
499
500 result = Ns._process_guest_epa_quota_params(
501 guest_epa_quota=guest_epa_quota,
502 epa_vcpu_set=epa_vcpu_set,
503 )
504
505 resource_allocation.assert_called_once_with(resource_allocation_param)
506 self.assertDictEqual(expected_result, result)
507
508 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
509 def test__process_guest_epa_quota_params_with_mem_quota_false_epa_cpu(
510 self,
511 resource_allocation,
512 ):
513 expected_result = {
514 "mem-quota": {
515 "limit": 10,
516 "reserve": 20,
517 "shares": 30,
518 },
519 }
520 guest_epa_quota = {
521 "mem-quota": {
522 "limit": "10",
523 "reserve": "20",
524 "shares": "30",
525 },
526 }
527 epa_vcpu_set = False
528
529 resource_allocation_param = {
530 "limit": "10",
531 "reserve": "20",
532 "shares": "30",
533 }
534 resource_allocation.return_value = {
535 "limit": 10,
536 "reserve": 20,
537 "shares": 30,
538 }
539
540 result = Ns._process_guest_epa_quota_params(
541 guest_epa_quota=guest_epa_quota,
542 epa_vcpu_set=epa_vcpu_set,
543 )
544
545 resource_allocation.assert_called_once_with(resource_allocation_param)
546 self.assertDictEqual(expected_result, result)
547
548 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
549 def test__process_guest_epa_quota_params_with_disk_io_quota_epa_cpu(
550 self,
551 resource_allocation,
552 ):
553 expected_result = {
554 "disk-io-quota": {
555 "limit": 10,
556 "reserve": 20,
557 "shares": 30,
558 },
559 }
560 guest_epa_quota = {
561 "disk-io-quota": {
562 "limit": "10",
563 "reserve": "20",
564 "shares": "30",
565 },
566 }
567 epa_vcpu_set = True
568
569 resource_allocation_param = {
570 "limit": "10",
571 "reserve": "20",
572 "shares": "30",
573 }
574 resource_allocation.return_value = {
575 "limit": 10,
576 "reserve": 20,
577 "shares": 30,
578 }
579
580 result = Ns._process_guest_epa_quota_params(
581 guest_epa_quota=guest_epa_quota,
582 epa_vcpu_set=epa_vcpu_set,
583 )
584
585 resource_allocation.assert_called_once_with(resource_allocation_param)
586 self.assertDictEqual(expected_result, result)
587
588 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
589 def test__process_guest_epa_quota_params_with_disk_io_quota_false_epa_cpu(
590 self,
591 resource_allocation,
592 ):
593 expected_result = {
594 "disk-io-quota": {
595 "limit": 10,
596 "reserve": 20,
597 "shares": 30,
598 },
599 }
600 guest_epa_quota = {
601 "disk-io-quota": {
602 "limit": "10",
603 "reserve": "20",
604 "shares": "30",
605 },
606 }
607 epa_vcpu_set = False
608
609 resource_allocation_param = {
610 "limit": "10",
611 "reserve": "20",
612 "shares": "30",
613 }
614 resource_allocation.return_value = {
615 "limit": 10,
616 "reserve": 20,
617 "shares": 30,
618 }
619
620 result = Ns._process_guest_epa_quota_params(
621 guest_epa_quota=guest_epa_quota,
622 epa_vcpu_set=epa_vcpu_set,
623 )
624
625 resource_allocation.assert_called_once_with(resource_allocation_param)
626 self.assertDictEqual(expected_result, result)
627
628 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
629 def test__process_guest_epa_quota_params_with_vif_quota_epa_cpu(
630 self,
631 resource_allocation,
632 ):
633 expected_result = {
634 "vif-quota": {
635 "limit": 10,
636 "reserve": 20,
637 "shares": 30,
638 },
639 }
640 guest_epa_quota = {
641 "vif-quota": {
642 "limit": "10",
643 "reserve": "20",
644 "shares": "30",
645 },
646 }
647 epa_vcpu_set = True
648
649 resource_allocation_param = {
650 "limit": "10",
651 "reserve": "20",
652 "shares": "30",
653 }
654 resource_allocation.return_value = {
655 "limit": 10,
656 "reserve": 20,
657 "shares": 30,
658 }
659
660 result = Ns._process_guest_epa_quota_params(
661 guest_epa_quota=guest_epa_quota,
662 epa_vcpu_set=epa_vcpu_set,
663 )
664
665 resource_allocation.assert_called_once_with(resource_allocation_param)
666 self.assertDictEqual(expected_result, result)
667
668 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
669 def test__process_guest_epa_quota_params_with_vif_quota_false_epa_cpu(
670 self,
671 resource_allocation,
672 ):
673 expected_result = {
674 "vif-quota": {
675 "limit": 10,
676 "reserve": 20,
677 "shares": 30,
678 },
679 }
680 guest_epa_quota = {
681 "vif-quota": {
682 "limit": "10",
683 "reserve": "20",
684 "shares": "30",
685 },
686 }
687 epa_vcpu_set = False
688
689 resource_allocation_param = {
690 "limit": "10",
691 "reserve": "20",
692 "shares": "30",
693 }
694 resource_allocation.return_value = {
695 "limit": 10,
696 "reserve": 20,
697 "shares": 30,
698 }
699
700 result = Ns._process_guest_epa_quota_params(
701 guest_epa_quota=guest_epa_quota,
702 epa_vcpu_set=epa_vcpu_set,
703 )
704
705 resource_allocation.assert_called_once_with(resource_allocation_param)
706 self.assertDictEqual(expected_result, result)
707
708 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
709 def test__process_guest_epa_quota_params_with_quota_epa_cpu(
710 self,
711 resource_allocation,
712 ):
713 expected_result = {
714 "mem-quota": {
715 "limit": 10,
716 "reserve": 20,
717 "shares": 30,
718 },
719 "disk-io-quota": {
720 "limit": 10,
721 "reserve": 20,
722 "shares": 30,
723 },
724 "vif-quota": {
725 "limit": 10,
726 "reserve": 20,
727 "shares": 30,
728 },
729 }
730 guest_epa_quota = {
731 "cpu-quota": {
732 "limit": "10",
733 "reserve": "20",
734 "shares": "30",
735 },
736 "mem-quota": {
737 "limit": "10",
738 "reserve": "20",
739 "shares": "30",
740 },
741 "disk-io-quota": {
742 "limit": "10",
743 "reserve": "20",
744 "shares": "30",
745 },
746 "vif-quota": {
747 "limit": "10",
748 "reserve": "20",
749 "shares": "30",
750 },
751 }
752 epa_vcpu_set = True
753
754 resource_allocation.return_value = {
755 "limit": 10,
756 "reserve": 20,
757 "shares": 30,
758 }
759
760 result = Ns._process_guest_epa_quota_params(
761 guest_epa_quota=guest_epa_quota,
762 epa_vcpu_set=epa_vcpu_set,
763 )
764
765 self.assertTrue(resource_allocation.called)
766 self.assertDictEqual(expected_result, result)
767
768 @patch("osm_ng_ro.ns.Ns._get_resource_allocation_params")
769 def test__process_guest_epa_quota_params_with_quota_epa_cpu_no_set(
770 self,
771 resource_allocation,
772 ):
773 expected_result = {
774 "cpu-quota": {
775 "limit": 10,
776 "reserve": 20,
777 "shares": 30,
778 },
779 "mem-quota": {
780 "limit": 10,
781 "reserve": 20,
782 "shares": 30,
783 },
784 "disk-io-quota": {
785 "limit": 10,
786 "reserve": 20,
787 "shares": 30,
788 },
789 "vif-quota": {
790 "limit": 10,
791 "reserve": 20,
792 "shares": 30,
793 },
794 }
795 guest_epa_quota = {
796 "cpu-quota": {
797 "limit": "10",
798 "reserve": "20",
799 "shares": "30",
800 },
801 "mem-quota": {
802 "limit": "10",
803 "reserve": "20",
804 "shares": "30",
805 },
806 "disk-io-quota": {
807 "limit": "10",
808 "reserve": "20",
809 "shares": "30",
810 },
811 "vif-quota": {
812 "limit": "10",
813 "reserve": "20",
814 "shares": "30",
815 },
816 }
817 epa_vcpu_set = False
818
819 resource_allocation.return_value = {
820 "limit": 10,
821 "reserve": 20,
822 "shares": 30,
823 }
824
825 result = Ns._process_guest_epa_quota_params(
826 guest_epa_quota=guest_epa_quota,
827 epa_vcpu_set=epa_vcpu_set,
828 )
829
830 self.assertTrue(resource_allocation.called)
831 self.assertDictEqual(expected_result, result)
832
833 def test__process_guest_epa_numa_params_with_empty_numa_params(self):
834 expected_numa_result = {}
835 expected_epa_vcpu_set_result = False
836 guest_epa_quota = {}
837
838 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
839 guest_epa_quota=guest_epa_quota,
840 )
841
842 self.assertDictEqual(expected_numa_result, numa_result)
843 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
844
845 def test__process_guest_epa_numa_params_with_wrong_numa_params(self):
846 expected_numa_result = {}
847 expected_epa_vcpu_set_result = False
848 guest_epa_quota = {"no_nume": "here"}
849
850 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
851 guest_epa_quota=guest_epa_quota,
852 )
853
854 self.assertDictEqual(expected_numa_result, numa_result)
855 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
856
857 def test__process_guest_epa_numa_params_with_numa_node_policy(self):
858 expected_numa_result = {}
859 expected_epa_vcpu_set_result = False
860 guest_epa_quota = {"numa-node-policy": {}}
861
862 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
863 guest_epa_quota=guest_epa_quota,
864 )
865
866 self.assertDictEqual(expected_numa_result, numa_result)
867 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
868
869 def test__process_guest_epa_numa_params_with_no_node(self):
870 expected_numa_result = {}
871 expected_epa_vcpu_set_result = False
872 guest_epa_quota = {
873 "numa-node-policy": {
874 "node": [],
875 },
876 }
877
878 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
879 guest_epa_quota=guest_epa_quota,
880 )
881
882 self.assertDictEqual(expected_numa_result, numa_result)
883 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
884
885 def test__process_guest_epa_numa_params_with_1_node_num_cores(self):
886 expected_numa_result = {"cores": 3}
887 expected_epa_vcpu_set_result = True
888 guest_epa_quota = {
889 "numa-node-policy": {
890 "node": [
891 {
892 "num-cores": 3,
893 },
894 ],
895 },
896 }
897
898 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
899 guest_epa_quota=guest_epa_quota,
900 )
901
902 self.assertDictEqual(expected_numa_result, numa_result)
903 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
904
905 def test__process_guest_epa_numa_params_with_1_node_paired_threads(self):
906 expected_numa_result = {"paired-threads": 3}
907 expected_epa_vcpu_set_result = True
908 guest_epa_quota = {
909 "numa-node-policy": {
910 "node": [
911 {
912 "paired-threads": {"num-paired-threads": "3"},
913 },
914 ],
915 },
916 }
917
918 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
919 guest_epa_quota=guest_epa_quota,
920 )
921
922 self.assertDictEqual(expected_numa_result, numa_result)
923 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
924
925 def test__process_guest_epa_numa_params_with_1_node_paired_threads_ids(self):
926 expected_numa_result = {
927 "paired-threads-id": [("0", "1"), ("4", "5")],
928 }
929 expected_epa_vcpu_set_result = False
930 guest_epa_quota = {
931 "numa-node-policy": {
932 "node": [
933 {
934 "paired-threads": {
935 "paired-thread-ids": [
936 {
937 "thread-a": 0,
938 "thread-b": 1,
939 },
940 {
941 "thread-a": 4,
942 "thread-b": 5,
943 },
944 ],
945 },
946 },
947 ],
948 },
949 }
950
951 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
952 guest_epa_quota=guest_epa_quota,
953 )
954
955 self.assertDictEqual(expected_numa_result, numa_result)
956 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
957
958 def test__process_guest_epa_numa_params_with_1_node_num_threads(self):
959 expected_numa_result = {"threads": 3}
960 expected_epa_vcpu_set_result = True
961 guest_epa_quota = {
962 "numa-node-policy": {
963 "node": [
964 {
965 "num-threads": "3",
966 },
967 ],
968 },
969 }
970
971 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
972 guest_epa_quota=guest_epa_quota,
973 )
974
975 self.assertDictEqual(expected_numa_result, numa_result)
976 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
977
978 def test__process_guest_epa_numa_params_with_1_node_memory_mb(self):
979 expected_numa_result = {"memory": 2}
980 expected_epa_vcpu_set_result = False
981 guest_epa_quota = {
982 "numa-node-policy": {
983 "node": [
984 {
985 "memory-mb": 2048,
986 },
987 ],
988 },
989 }
990
991 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
992 guest_epa_quota=guest_epa_quota,
993 )
994
995 self.assertDictEqual(expected_numa_result, numa_result)
996 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
997
998 def test__process_guest_epa_numa_params_with_1_node(self):
999 expected_numa_result = {
1000 "cores": 3,
1001 "paired-threads": 3,
1002 "paired-threads-id": [("0", "1"), ("4", "5")],
1003 "threads": 3,
1004 "memory": 2,
1005 }
1006 expected_epa_vcpu_set_result = True
1007 guest_epa_quota = {
1008 "numa-node-policy": {
1009 "node": [
1010 {
1011 "num-cores": 3,
1012 "paired-threads": {
1013 "num-paired-threads": "3",
1014 "paired-thread-ids": [
1015 {
1016 "thread-a": 0,
1017 "thread-b": 1,
1018 },
1019 {
1020 "thread-a": 4,
1021 "thread-b": 5,
1022 },
1023 ],
1024 },
1025 "num-threads": "3",
1026 "memory-mb": 2048,
1027 },
1028 ],
1029 },
1030 }
1031
1032 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
1033 guest_epa_quota=guest_epa_quota,
1034 )
1035
1036 self.assertDictEqual(expected_numa_result, numa_result)
1037 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1038
1039 def test__process_guest_epa_numa_params_with_2_nodes(self):
1040 expected_numa_result = {
1041 "cores": 3,
1042 "paired-threads": 3,
1043 "paired-threads-id": [("0", "1"), ("4", "5")],
1044 "threads": 3,
1045 "memory": 2,
1046 }
1047 expected_epa_vcpu_set_result = True
1048 guest_epa_quota = {
1049 "numa-node-policy": {
1050 "node": [
1051 {
1052 "num-cores": 3,
1053 "paired-threads": {
1054 "num-paired-threads": "3",
1055 "paired-thread-ids": [
1056 {
1057 "thread-a": 0,
1058 "thread-b": 1,
1059 },
1060 {
1061 "thread-a": 4,
1062 "thread-b": 5,
1063 },
1064 ],
1065 },
1066 "num-threads": "3",
1067 "memory-mb": 2048,
1068 },
1069 {
1070 "num-cores": 7,
1071 "paired-threads": {
1072 "num-paired-threads": "7",
1073 "paired-thread-ids": [
1074 {
1075 "thread-a": 2,
1076 "thread-b": 3,
1077 },
1078 {
1079 "thread-a": 5,
1080 "thread-b": 6,
1081 },
1082 ],
1083 },
1084 "num-threads": "4",
1085 "memory-mb": 4096,
1086 },
1087 ],
1088 },
1089 }
1090
1091 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params(
1092 guest_epa_quota=guest_epa_quota,
1093 )
1094
1095 self.assertDictEqual(expected_numa_result, numa_result)
1096 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1097
1098 def test__process_guest_epa_cpu_pinning_params_with_empty_params(self):
1099 expected_numa_result = {}
1100 expected_epa_vcpu_set_result = False
1101 guest_epa_quota = {}
1102 vcpu_count = 0
1103 epa_vcpu_set = False
1104
1105 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_cpu_pinning_params(
1106 guest_epa_quota=guest_epa_quota,
1107 vcpu_count=vcpu_count,
1108 epa_vcpu_set=epa_vcpu_set,
1109 )
1110
1111 self.assertDictEqual(expected_numa_result, numa_result)
1112 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1113
1114 def test__process_guest_epa_cpu_pinning_params_with_wrong_params(self):
1115 expected_numa_result = {}
1116 expected_epa_vcpu_set_result = False
1117 guest_epa_quota = {
1118 "no-cpu-pinning-policy": "here",
1119 }
1120 vcpu_count = 0
1121 epa_vcpu_set = False
1122
1123 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_cpu_pinning_params(
1124 guest_epa_quota=guest_epa_quota,
1125 vcpu_count=vcpu_count,
1126 epa_vcpu_set=epa_vcpu_set,
1127 )
1128
1129 self.assertDictEqual(expected_numa_result, numa_result)
1130 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1131
1132 def test__process_guest_epa_cpu_pinning_params_with_epa_vcpu_set(self):
1133 expected_numa_result = {}
1134 expected_epa_vcpu_set_result = True
1135 guest_epa_quota = {
1136 "cpu-pinning-policy": "DEDICATED",
1137 }
1138 vcpu_count = 0
1139 epa_vcpu_set = True
1140
1141 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_cpu_pinning_params(
1142 guest_epa_quota=guest_epa_quota,
1143 vcpu_count=vcpu_count,
1144 epa_vcpu_set=epa_vcpu_set,
1145 )
1146
1147 self.assertDictEqual(expected_numa_result, numa_result)
1148 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1149
1150 def test__process_guest_epa_cpu_pinning_params_with_threads(self):
1151 expected_numa_result = {"threads": 3}
1152 expected_epa_vcpu_set_result = True
1153 guest_epa_quota = {
1154 "cpu-pinning-policy": "DEDICATED",
1155 "cpu-thread-pinning-policy": "PREFER",
1156 }
1157 vcpu_count = 3
1158 epa_vcpu_set = False
1159
1160 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_cpu_pinning_params(
1161 guest_epa_quota=guest_epa_quota,
1162 vcpu_count=vcpu_count,
1163 epa_vcpu_set=epa_vcpu_set,
1164 )
1165
1166 self.assertDictEqual(expected_numa_result, numa_result)
1167 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1168
1169 def test__process_guest_epa_cpu_pinning_params(self):
1170 expected_numa_result = {"cores": 3}
1171 expected_epa_vcpu_set_result = True
1172 guest_epa_quota = {
1173 "cpu-pinning-policy": "DEDICATED",
1174 }
1175 vcpu_count = 3
1176 epa_vcpu_set = False
1177
1178 numa_result, epa_vcpu_set_result = Ns._process_guest_epa_cpu_pinning_params(
1179 guest_epa_quota=guest_epa_quota,
1180 vcpu_count=vcpu_count,
1181 epa_vcpu_set=epa_vcpu_set,
1182 )
1183
1184 self.assertDictEqual(expected_numa_result, numa_result)
1185 self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result)
1186
1187 @patch("osm_ng_ro.ns.Ns._process_guest_epa_quota_params")
1188 @patch("osm_ng_ro.ns.Ns._process_guest_epa_cpu_pinning_params")
1189 @patch("osm_ng_ro.ns.Ns._process_guest_epa_numa_params")
1190 def test__process_guest_epa_params_with_empty_params(
1191 self,
1192 guest_epa_numa_params,
1193 guest_epa_cpu_pinning_params,
1194 guest_epa_quota_params,
1195 ):
1196 expected_result = {}
1197 target_flavor = {}
1198
1199 result = Ns._process_epa_params(
1200 target_flavor=target_flavor,
1201 )
1202
1203 self.assertDictEqual(expected_result, result)
1204 self.assertFalse(guest_epa_numa_params.called)
1205 self.assertFalse(guest_epa_cpu_pinning_params.called)
1206 self.assertFalse(guest_epa_quota_params.called)
1207
1208 @patch("osm_ng_ro.ns.Ns._process_guest_epa_quota_params")
1209 @patch("osm_ng_ro.ns.Ns._process_guest_epa_cpu_pinning_params")
1210 @patch("osm_ng_ro.ns.Ns._process_guest_epa_numa_params")
1211 def test__process_guest_epa_params_with_wrong_params(
1212 self,
1213 guest_epa_numa_params,
1214 guest_epa_cpu_pinning_params,
1215 guest_epa_quota_params,
1216 ):
1217 expected_result = {}
1218 target_flavor = {
1219 "no-guest-epa": "here",
1220 }
1221
1222 result = Ns._process_epa_params(
1223 target_flavor=target_flavor,
1224 )
1225
1226 self.assertDictEqual(expected_result, result)
1227 self.assertFalse(guest_epa_numa_params.called)
1228 self.assertFalse(guest_epa_cpu_pinning_params.called)
1229 self.assertFalse(guest_epa_quota_params.called)
1230
1231 @patch("osm_ng_ro.ns.Ns._process_guest_epa_quota_params")
1232 @patch("osm_ng_ro.ns.Ns._process_guest_epa_cpu_pinning_params")
1233 @patch("osm_ng_ro.ns.Ns._process_guest_epa_numa_params")
1234 def test__process_guest_epa_params(
1235 self,
1236 guest_epa_numa_params,
1237 guest_epa_cpu_pinning_params,
1238 guest_epa_quota_params,
1239 ):
1240 expected_result = {}
1241 target_flavor = {
1242 "guest-epa": {
1243 "vcpu-count": 1,
1244 },
1245 }
1246
1247 guest_epa_numa_params.return_value = ({}, False)
1248 guest_epa_cpu_pinning_params.return_value = ({}, False)
1249 guest_epa_quota_params.return_value = {}
1250
1251 result = Ns._process_epa_params(
1252 target_flavor=target_flavor,
1253 )
1254
1255 self.assertDictEqual(expected_result, result)
1256 self.assertTrue(guest_epa_numa_params.called)
1257 self.assertTrue(guest_epa_cpu_pinning_params.called)
1258 self.assertTrue(guest_epa_quota_params.called)
1259
1260 @patch("osm_ng_ro.ns.Ns._process_guest_epa_quota_params")
1261 @patch("osm_ng_ro.ns.Ns._process_guest_epa_cpu_pinning_params")
1262 @patch("osm_ng_ro.ns.Ns._process_guest_epa_numa_params")
1263 def test__process_guest_epa_params_with_mempage_size(
1264 self,
1265 guest_epa_numa_params,
1266 guest_epa_cpu_pinning_params,
1267 guest_epa_quota_params,
1268 ):
1269 expected_result = {
1270 "mempage-size": "1G",
1271 }
1272 target_flavor = {
1273 "guest-epa": {"vcpu-count": 1, "mempage-size": "1G"},
1274 }
1275
1276 guest_epa_numa_params.return_value = ({}, False)
1277 guest_epa_cpu_pinning_params.return_value = ({}, False)
1278 guest_epa_quota_params.return_value = {}
1279
1280 result = Ns._process_epa_params(
1281 target_flavor=target_flavor,
1282 )
1283
1284 self.assertDictEqual(expected_result, result)
1285 self.assertTrue(guest_epa_numa_params.called)
1286 self.assertTrue(guest_epa_cpu_pinning_params.called)
1287 self.assertTrue(guest_epa_quota_params.called)
1288
1289 @patch("osm_ng_ro.ns.Ns._process_guest_epa_quota_params")
1290 @patch("osm_ng_ro.ns.Ns._process_guest_epa_cpu_pinning_params")
1291 @patch("osm_ng_ro.ns.Ns._process_guest_epa_numa_params")
1292 def test__process_guest_epa_params_with_numa(
1293 self,
1294 guest_epa_numa_params,
1295 guest_epa_cpu_pinning_params,
1296 guest_epa_quota_params,
1297 ):
1298 expected_result = {
1299 "mempage-size": "1G",
1300 "numas": [
1301 {
1302 "cores": 3,
1303 "memory": 2,
1304 "paired-threads": 3,
1305 "paired-threads-id": [("0", "1"), ("4", "5")],
1306 "threads": 3,
1307 }
1308 ],
1309 "cpu-quota": {"limit": 10, "reserve": 20, "shares": 30},
1310 "disk-io-quota": {"limit": 10, "reserve": 20, "shares": 30},
1311 "mem-quota": {"limit": 10, "reserve": 20, "shares": 30},
1312 "vif-quota": {"limit": 10, "reserve": 20, "shares": 30},
1313 }
1314 target_flavor = {
1315 "guest-epa": {
1316 "vcpu-count": 1,
1317 "mempage-size": "1G",
1318 "cpu-pinning-policy": "DEDICATED",
1319 "cpu-thread-pinning-policy": "PREFER",
1320 "numa-node-policy": {
1321 "node": [
1322 {
1323 "num-cores": 3,
1324 "paired-threads": {
1325 "num-paired-threads": "3",
1326 "paired-thread-ids": [
1327 {
1328 "thread-a": 0,
1329 "thread-b": 1,
1330 },
1331 {
1332 "thread-a": 4,
1333 "thread-b": 5,
1334 },
1335 ],
1336 },
1337 "num-threads": "3",
1338 "memory-mb": 2048,
1339 },
1340 ],
1341 },
1342 "cpu-quota": {
1343 "limit": "10",
1344 "reserve": "20",
1345 "shares": "30",
1346 },
1347 "mem-quota": {
1348 "limit": "10",
1349 "reserve": "20",
1350 "shares": "30",
1351 },
1352 "disk-io-quota": {
1353 "limit": "10",
1354 "reserve": "20",
1355 "shares": "30",
1356 },
1357 "vif-quota": {
1358 "limit": "10",
1359 "reserve": "20",
1360 "shares": "30",
1361 },
1362 },
1363 }
1364
1365 guest_epa_numa_params.return_value = (
1366 {
1367 "cores": 3,
1368 "paired-threads": 3,
1369 "paired-threads-id": [("0", "1"), ("4", "5")],
1370 "threads": 3,
1371 "memory": 2,
1372 },
1373 True,
1374 )
1375 guest_epa_cpu_pinning_params.return_value = (
1376 {
1377 "threads": 3,
1378 },
1379 True,
1380 )
1381 guest_epa_quota_params.return_value = {
1382 "cpu-quota": {
1383 "limit": 10,
1384 "reserve": 20,
1385 "shares": 30,
1386 },
1387 "mem-quota": {
1388 "limit": 10,
1389 "reserve": 20,
1390 "shares": 30,
1391 },
1392 "disk-io-quota": {
1393 "limit": 10,
1394 "reserve": 20,
1395 "shares": 30,
1396 },
1397 "vif-quota": {
1398 "limit": 10,
1399 "reserve": 20,
1400 "shares": 30,
1401 },
1402 }
1403
1404 result = Ns._process_epa_params(
1405 target_flavor=target_flavor,
1406 )
1407
1408 self.assertDictEqual(expected_result, result)
1409 self.assertTrue(guest_epa_numa_params.called)
1410 self.assertTrue(guest_epa_cpu_pinning_params.called)
1411 self.assertTrue(guest_epa_quota_params.called)
1412
1413 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1414 def test__process_flavor_params_with_empty_target_flavor(
1415 self,
1416 epa_params,
1417 ):
1418
1419 target_flavor = {}
1420 indata = {
1421 "vnf": [
1422 {
1423 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1424 },
1425 ],
1426 }
1427 vim_info = {}
1428 target_record_id = ""
1429
1430 with self.assertRaises(KeyError):
1431 Ns._process_flavor_params(
1432 target_flavor=target_flavor,
1433 indata=indata,
1434 vim_info=vim_info,
1435 target_record_id=target_record_id,
1436 )
1437
1438 self.assertFalse(epa_params.called)
1439
1440 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1441 def test__process_flavor_params_with_wrong_target_flavor(
1442 self,
1443 epa_params,
1444 ):
1445
1446 target_flavor = {
1447 "no-target-flavor": "here",
1448 }
1449 indata = {}
1450 vim_info = {}
1451 target_record_id = ""
1452
1453 with self.assertRaises(KeyError):
1454 Ns._process_flavor_params(
1455 target_flavor=target_flavor,
1456 indata=indata,
1457 vim_info=vim_info,
1458 target_record_id=target_record_id,
1459 )
1460
1461 self.assertFalse(epa_params.called)
1462
1463 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1464 def test__process_flavor_params_with_empty_indata(
1465 self,
1466 epa_params,
1467 ):
1468
1469 expected_result = {
1470 "find_params": {
1471 "flavor_data": {
1472 "disk": 10,
1473 "ram": 1024,
1474 "vcpus": 2,
1475 },
1476 },
1477 "params": {
1478 "flavor_data": {
1479 "disk": 10,
1480 "name": "test",
1481 "ram": 1024,
1482 "vcpus": 2,
1483 },
1484 },
1485 }
1486 target_flavor = {
1487 "name": "test",
1488 "storage-gb": "10",
1489 "memory-mb": "1024",
1490 "vcpu-count": "2",
1491 }
1492 indata = {}
1493 vim_info = {}
1494 target_record_id = ""
1495
1496 epa_params.return_value = {}
1497
1498 result = Ns._process_flavor_params(
1499 target_flavor=target_flavor,
1500 indata=indata,
1501 vim_info=vim_info,
1502 target_record_id=target_record_id,
1503 )
1504
1505 self.assertTrue(epa_params.called)
1506 self.assertDictEqual(result, expected_result)
1507
1508 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1509 def test__process_flavor_params_with_wrong_indata(
1510 self,
1511 epa_params,
1512 ):
1513
1514 expected_result = {
1515 "find_params": {
1516 "flavor_data": {
1517 "disk": 10,
1518 "ram": 1024,
1519 "vcpus": 2,
1520 },
1521 },
1522 "params": {
1523 "flavor_data": {
1524 "disk": 10,
1525 "name": "test",
1526 "ram": 1024,
1527 "vcpus": 2,
1528 },
1529 },
1530 }
1531 target_flavor = {
1532 "name": "test",
1533 "storage-gb": "10",
1534 "memory-mb": "1024",
1535 "vcpu-count": "2",
1536 }
1537 indata = {
1538 "no-vnf": "here",
1539 }
1540 vim_info = {}
1541 target_record_id = ""
1542
1543 epa_params.return_value = {}
1544
1545 result = Ns._process_flavor_params(
1546 target_flavor=target_flavor,
1547 indata=indata,
1548 vim_info=vim_info,
1549 target_record_id=target_record_id,
1550 )
1551
1552 self.assertTrue(epa_params.called)
1553 self.assertDictEqual(result, expected_result)
1554
1555 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1556 def test__process_flavor_params_with_ephemeral_disk(
1557 self,
1558 epa_params,
1559 ):
1560 db = MagicMock(name="database mock")
1561 kwargs = {
1562 "db": db,
1563 }
1564
1565 db.get_one.return_value = {
1566 "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1567 "df": [
1568 {
1569 "id": "default-df",
1570 "vdu-profile": [
1571 {"id": "without_volumes-VM", "min-number-of-instances": 1}
1572 ],
1573 }
1574 ],
1575 "id": "without_volumes-vnf",
1576 "product-name": "without_volumes-vnf",
1577 "vdu": [
1578 {
1579 "id": "without_volumes-VM",
1580 "name": "without_volumes-VM",
1581 "sw-image-desc": "ubuntu20.04",
1582 "alternative-sw-image-desc": [
1583 "ubuntu20.04-aws",
1584 "ubuntu20.04-azure",
1585 ],
1586 "virtual-storage-desc": ["root-volume", "ephemeral-volume"],
1587 }
1588 ],
1589 "version": "1.0",
1590 "virtual-storage-desc": [
1591 {"id": "root-volume", "size-of-storage": "10"},
1592 {
1593 "id": "ephemeral-volume",
1594 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
1595 "size-of-storage": "1",
1596 },
1597 ],
1598 "_admin": {
1599 "storage": {
1600 "fs": "mongo",
1601 "path": "/app/storage/",
1602 },
1603 "type": "vnfd",
1604 },
1605 }
1606 expected_result = {
1607 "find_params": {
1608 "flavor_data": {
1609 "disk": 10,
1610 "ram": 1024,
1611 "vcpus": 2,
1612 "ephemeral": 10,
1613 },
1614 },
1615 "params": {
1616 "flavor_data": {
1617 "disk": 10,
1618 "name": "test",
1619 "ram": 1024,
1620 "vcpus": 2,
1621 "ephemeral": 10,
1622 },
1623 },
1624 }
1625 target_flavor = {
1626 "id": "test_id",
1627 "name": "test",
1628 "storage-gb": "10",
1629 "memory-mb": "1024",
1630 "vcpu-count": "2",
1631 }
1632 indata = {
1633 "vnf": [
1634 {
1635 "vdur": [
1636 {
1637 "ns-flavor-id": "test_id",
1638 "virtual-storages": [
1639 {
1640 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
1641 "size-of-storage": "10",
1642 },
1643 ],
1644 },
1645 ],
1646 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1647 },
1648 ],
1649 }
1650 vim_info = {}
1651 target_record_id = ""
1652
1653 epa_params.return_value = {}
1654
1655 result = Ns._process_flavor_params(
1656 target_flavor=target_flavor,
1657 indata=indata,
1658 vim_info=vim_info,
1659 target_record_id=target_record_id,
1660 **kwargs,
1661 )
1662
1663 self.assertTrue(epa_params.called)
1664 self.assertDictEqual(result, expected_result)
1665
1666 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1667 def test__process_flavor_params_with_swap_disk(
1668 self,
1669 epa_params,
1670 ):
1671
1672 expected_result = {
1673 "find_params": {
1674 "flavor_data": {
1675 "disk": 10,
1676 "ram": 1024,
1677 "vcpus": 2,
1678 "swap": 20,
1679 },
1680 },
1681 "params": {
1682 "flavor_data": {
1683 "disk": 10,
1684 "name": "test",
1685 "ram": 1024,
1686 "vcpus": 2,
1687 "swap": 20,
1688 },
1689 },
1690 }
1691 target_flavor = {
1692 "id": "test_id",
1693 "name": "test",
1694 "storage-gb": "10",
1695 "memory-mb": "1024",
1696 "vcpu-count": "2",
1697 }
1698 indata = {
1699 "vnf": [
1700 {
1701 "vdur": [
1702 {
1703 "ns-flavor-id": "test_id",
1704 "virtual-storages": [
1705 {
1706 "type-of-storage": "etsi-nfv-descriptors:swap-storage",
1707 "size-of-storage": "20",
1708 },
1709 ],
1710 },
1711 ],
1712 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1713 },
1714 ],
1715 }
1716 vim_info = {}
1717 target_record_id = ""
1718
1719 epa_params.return_value = {}
1720
1721 result = Ns._process_flavor_params(
1722 target_flavor=target_flavor,
1723 indata=indata,
1724 vim_info=vim_info,
1725 target_record_id=target_record_id,
1726 )
1727
1728 self.assertTrue(epa_params.called)
1729 self.assertDictEqual(result, expected_result)
1730
1731 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1732 def test__process_flavor_params_with_persistent_root_disk(
1733 self,
1734 epa_params,
1735 ):
1736 db = MagicMock(name="database mock")
1737
1738 kwargs = {
1739 "db": db,
1740 }
1741
1742 db.get_one.return_value = {
1743 "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1744 "df": [
1745 {
1746 "id": "default-df",
1747 "vdu-profile": [
1748 {"id": "several_volumes-VM", "min-number-of-instances": 1}
1749 ],
1750 }
1751 ],
1752 "id": "several_volumes-vnf",
1753 "product-name": "several_volumes-vnf",
1754 "vdu": [
1755 {
1756 "id": "several_volumes-VM",
1757 "name": "several_volumes-VM",
1758 "sw-image-desc": "ubuntu20.04",
1759 "alternative-sw-image-desc": [
1760 "ubuntu20.04-aws",
1761 "ubuntu20.04-azure",
1762 ],
1763 "virtual-storage-desc": [
1764 "persistent-root-volume",
1765 ],
1766 }
1767 ],
1768 "version": "1.0",
1769 "virtual-storage-desc": [
1770 {
1771 "id": "persistent-root-volume",
1772 "type-of-storage": "persistent-storage:persistent-storage",
1773 "size-of-storage": "10",
1774 },
1775 ],
1776 "_admin": {
1777 "storage": {
1778 "fs": "mongo",
1779 "path": "/app/storage/",
1780 },
1781 "type": "vnfd",
1782 },
1783 }
1784 expected_result = {
1785 "find_params": {
1786 "flavor_data": {
1787 "disk": 0,
1788 "ram": 1024,
1789 "vcpus": 2,
1790 },
1791 },
1792 "params": {
1793 "flavor_data": {
1794 "disk": 0,
1795 "name": "test",
1796 "ram": 1024,
1797 "vcpus": 2,
1798 },
1799 },
1800 }
1801 target_flavor = {
1802 "id": "test_id",
1803 "name": "test",
1804 "storage-gb": "10",
1805 "memory-mb": "1024",
1806 "vcpu-count": "2",
1807 }
1808 indata = {
1809 "vnf": [
1810 {
1811 "vdur": [
1812 {
1813 "vdu-name": "several_volumes-VM",
1814 "ns-flavor-id": "test_id",
1815 "virtual-storages": [
1816 {
1817 "type-of-storage": "persistent-storage:persistent-storage",
1818 "size-of-storage": "10",
1819 },
1820 ],
1821 },
1822 ],
1823 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1824 },
1825 ],
1826 }
1827 vim_info = {}
1828 target_record_id = ""
1829
1830 epa_params.return_value = {}
1831
1832 result = Ns._process_flavor_params(
1833 target_flavor=target_flavor,
1834 indata=indata,
1835 vim_info=vim_info,
1836 target_record_id=target_record_id,
1837 **kwargs,
1838 )
1839
1840 self.assertTrue(epa_params.called)
1841 self.assertDictEqual(result, expected_result)
1842
1843 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1844 def test__process_flavor_params_with_epa_params(
1845 self,
1846 epa_params,
1847 ):
1848
1849 expected_result = {
1850 "find_params": {
1851 "flavor_data": {
1852 "disk": 10,
1853 "ram": 1024,
1854 "vcpus": 2,
1855 "extended": {
1856 "numa": "there-is-numa-here",
1857 },
1858 },
1859 },
1860 "params": {
1861 "flavor_data": {
1862 "disk": 10,
1863 "name": "test",
1864 "ram": 1024,
1865 "vcpus": 2,
1866 "extended": {
1867 "numa": "there-is-numa-here",
1868 },
1869 },
1870 },
1871 }
1872 target_flavor = {
1873 "id": "test_id",
1874 "name": "test",
1875 "storage-gb": "10",
1876 "memory-mb": "1024",
1877 "vcpu-count": "2",
1878 }
1879 indata = {
1880 "vnf": [
1881 {
1882 "vdur": [
1883 {
1884 "ns-flavor-id": "test_id",
1885 },
1886 ],
1887 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1888 },
1889 ],
1890 }
1891 vim_info = {}
1892 target_record_id = ""
1893
1894 epa_params.return_value = {
1895 "numa": "there-is-numa-here",
1896 }
1897
1898 result = Ns._process_flavor_params(
1899 target_flavor=target_flavor,
1900 indata=indata,
1901 vim_info=vim_info,
1902 target_record_id=target_record_id,
1903 )
1904
1905 self.assertTrue(epa_params.called)
1906 self.assertDictEqual(result, expected_result)
1907
1908 @patch("osm_ng_ro.ns.Ns._process_epa_params")
1909 def test__process_flavor_params(
1910 self,
1911 epa_params,
1912 ):
1913 db = MagicMock(name="database mock")
1914
1915 kwargs = {
1916 "db": db,
1917 }
1918
1919 db.get_one.return_value = {
1920 "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
1921 "df": [
1922 {
1923 "id": "default-df",
1924 "vdu-profile": [
1925 {"id": "without_volumes-VM", "min-number-of-instances": 1}
1926 ],
1927 }
1928 ],
1929 "id": "without_volumes-vnf",
1930 "product-name": "without_volumes-vnf",
1931 "vdu": [
1932 {
1933 "id": "without_volumes-VM",
1934 "name": "without_volumes-VM",
1935 "sw-image-desc": "ubuntu20.04",
1936 "alternative-sw-image-desc": [
1937 "ubuntu20.04-aws",
1938 "ubuntu20.04-azure",
1939 ],
1940 "virtual-storage-desc": ["root-volume", "ephemeral-volume"],
1941 }
1942 ],
1943 "version": "1.0",
1944 "virtual-storage-desc": [
1945 {"id": "root-volume", "size-of-storage": "10"},
1946 {
1947 "id": "ephemeral-volume",
1948 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
1949 "size-of-storage": "1",
1950 },
1951 ],
1952 "_admin": {
1953 "storage": {
1954 "fs": "mongo",
1955 "path": "/app/storage/",
1956 },
1957 "type": "vnfd",
1958 },
1959 }
1960
1961 expected_result = {
1962 "find_params": {
1963 "flavor_data": {
1964 "disk": 10,
1965 "ram": 1024,
1966 "vcpus": 2,
1967 "ephemeral": 10,
1968 "swap": 20,
1969 "extended": {
1970 "numa": "there-is-numa-here",
1971 },
1972 },
1973 },
1974 "params": {
1975 "flavor_data": {
1976 "disk": 10,
1977 "name": "test",
1978 "ram": 1024,
1979 "vcpus": 2,
1980 "ephemeral": 10,
1981 "swap": 20,
1982 "extended": {
1983 "numa": "there-is-numa-here",
1984 },
1985 },
1986 },
1987 }
1988 target_flavor = {
1989 "id": "test_id",
1990 "name": "test",
1991 "storage-gb": "10",
1992 "memory-mb": "1024",
1993 "vcpu-count": "2",
1994 }
1995 indata = {
1996 "vnf": [
1997 {
1998 "vdur": [
1999 {
2000 "ns-flavor-id": "test_id",
2001 "virtual-storages": [
2002 {
2003 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
2004 "size-of-storage": "10",
2005 },
2006 {
2007 "type-of-storage": "etsi-nfv-descriptors:swap-storage",
2008 "size-of-storage": "20",
2009 },
2010 ],
2011 },
2012 ],
2013 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
2014 },
2015 ],
2016 }
2017 vim_info = {}
2018 target_record_id = ""
2019
2020 epa_params.return_value = {
2021 "numa": "there-is-numa-here",
2022 }
2023
2024 result = Ns._process_flavor_params(
2025 target_flavor=target_flavor,
2026 indata=indata,
2027 vim_info=vim_info,
2028 target_record_id=target_record_id,
2029 **kwargs,
2030 )
2031
2032 self.assertTrue(epa_params.called)
2033 self.assertDictEqual(result, expected_result)
2034
2035 def test__ip_profile_to_ro_with_none(self):
2036 ip_profile = None
2037
2038 result = Ns._ip_profile_to_ro(
2039 ip_profile=ip_profile,
2040 )
2041
2042 self.assertIsNone(result)
2043
2044 def test__ip_profile_to_ro_with_empty_profile(self):
2045 ip_profile = {}
2046
2047 result = Ns._ip_profile_to_ro(
2048 ip_profile=ip_profile,
2049 )
2050
2051 self.assertIsNone(result)
2052
2053 def test__ip_profile_to_ro_with_wrong_profile(self):
2054 ip_profile = {
2055 "no-profile": "here",
2056 }
2057 expected_result = {
2058 "ip_version": "IPv4",
2059 "subnet_address": None,
2060 "gateway_address": None,
2061 "dhcp_enabled": False,
2062 "dhcp_start_address": None,
2063 "dhcp_count": None,
2064 }
2065
2066 result = Ns._ip_profile_to_ro(
2067 ip_profile=ip_profile,
2068 )
2069
2070 self.assertDictEqual(expected_result, result)
2071
2072 def test__ip_profile_to_ro_with_ipv4_profile(self):
2073 ip_profile = {
2074 "ip-version": "ipv4",
2075 "subnet-address": "192.168.0.0/24",
2076 "gateway-address": "192.168.0.254",
2077 "dhcp-params": {
2078 "enabled": True,
2079 "start-address": "192.168.0.10",
2080 "count": 25,
2081 },
2082 }
2083 expected_result = {
2084 "ip_version": "IPv4",
2085 "subnet_address": "192.168.0.0/24",
2086 "gateway_address": "192.168.0.254",
2087 "dhcp_enabled": True,
2088 "dhcp_start_address": "192.168.0.10",
2089 "dhcp_count": 25,
2090 }
2091
2092 result = Ns._ip_profile_to_ro(
2093 ip_profile=ip_profile,
2094 )
2095
2096 self.assertDictEqual(expected_result, result)
2097
2098 def test__ip_profile_to_ro_with_ipv6_profile(self):
2099 ip_profile = {
2100 "ip-version": "ipv6",
2101 "subnet-address": "2001:0200:0001::/48",
2102 "gateway-address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe",
2103 "dhcp-params": {
2104 "enabled": True,
2105 "start-address": "2001:0200:0001::0010",
2106 "count": 25,
2107 },
2108 }
2109 expected_result = {
2110 "ip_version": "IPv6",
2111 "subnet_address": "2001:0200:0001::/48",
2112 "gateway_address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe",
2113 "dhcp_enabled": True,
2114 "dhcp_start_address": "2001:0200:0001::0010",
2115 "dhcp_count": 25,
2116 }
2117
2118 result = Ns._ip_profile_to_ro(
2119 ip_profile=ip_profile,
2120 )
2121
2122 self.assertDictEqual(expected_result, result)
2123
2124 def test__ip_profile_to_ro_with_dns_server(self):
2125 ip_profile = {
2126 "ip-version": "ipv4",
2127 "subnet-address": "192.168.0.0/24",
2128 "gateway-address": "192.168.0.254",
2129 "dhcp-params": {
2130 "enabled": True,
2131 "start-address": "192.168.0.10",
2132 "count": 25,
2133 },
2134 "dns-server": [
2135 {
2136 "address": "8.8.8.8",
2137 },
2138 {
2139 "address": "1.1.1.1",
2140 },
2141 {
2142 "address": "1.0.0.1",
2143 },
2144 ],
2145 }
2146 expected_result = {
2147 "ip_version": "IPv4",
2148 "subnet_address": "192.168.0.0/24",
2149 "gateway_address": "192.168.0.254",
2150 "dhcp_enabled": True,
2151 "dhcp_start_address": "192.168.0.10",
2152 "dhcp_count": 25,
2153 "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1",
2154 }
2155
2156 result = Ns._ip_profile_to_ro(
2157 ip_profile=ip_profile,
2158 )
2159
2160 self.assertDictEqual(expected_result, result)
2161
2162 def test__ip_profile_to_ro_with_security_group(self):
2163 ip_profile = {
2164 "ip-version": "ipv4",
2165 "subnet-address": "192.168.0.0/24",
2166 "gateway-address": "192.168.0.254",
2167 "dhcp-params": {
2168 "enabled": True,
2169 "start-address": "192.168.0.10",
2170 "count": 25,
2171 },
2172 "security-group": {
2173 "some-security-group": "here",
2174 },
2175 }
2176 expected_result = {
2177 "ip_version": "IPv4",
2178 "subnet_address": "192.168.0.0/24",
2179 "gateway_address": "192.168.0.254",
2180 "dhcp_enabled": True,
2181 "dhcp_start_address": "192.168.0.10",
2182 "dhcp_count": 25,
2183 "security_group": {
2184 "some-security-group": "here",
2185 },
2186 }
2187
2188 result = Ns._ip_profile_to_ro(
2189 ip_profile=ip_profile,
2190 )
2191
2192 self.assertDictEqual(expected_result, result)
2193
2194 def test__ip_profile_to_ro(self):
2195 ip_profile = {
2196 "ip-version": "ipv4",
2197 "subnet-address": "192.168.0.0/24",
2198 "gateway-address": "192.168.0.254",
2199 "dhcp-params": {
2200 "enabled": True,
2201 "start-address": "192.168.0.10",
2202 "count": 25,
2203 },
2204 "dns-server": [
2205 {
2206 "address": "8.8.8.8",
2207 },
2208 {
2209 "address": "1.1.1.1",
2210 },
2211 {
2212 "address": "1.0.0.1",
2213 },
2214 ],
2215 "security-group": {
2216 "some-security-group": "here",
2217 },
2218 }
2219 expected_result = {
2220 "ip_version": "IPv4",
2221 "subnet_address": "192.168.0.0/24",
2222 "gateway_address": "192.168.0.254",
2223 "dhcp_enabled": True,
2224 "dhcp_start_address": "192.168.0.10",
2225 "dhcp_count": 25,
2226 "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1",
2227 "security_group": {
2228 "some-security-group": "here",
2229 },
2230 }
2231
2232 result = Ns._ip_profile_to_ro(
2233 ip_profile=ip_profile,
2234 )
2235
2236 self.assertDictEqual(expected_result, result)
2237
2238 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2239 def test__process_net_params_with_empty_params(
2240 self,
2241 ip_profile_to_ro,
2242 ):
2243 target_vld = {
2244 "name": "vld-name",
2245 }
2246 indata = {
2247 "name": "ns-name",
2248 }
2249 vim_info = {
2250 "provider_network": "some-profile-here",
2251 }
2252 target_record_id = ""
2253 expected_result = {
2254 "params": {
2255 "net_name": "ns-name-vld-name",
2256 "net_type": "bridge",
2257 "ip_profile": {
2258 "some_ip_profile": "here",
2259 },
2260 "provider_network_profile": "some-profile-here",
2261 }
2262 }
2263
2264 ip_profile_to_ro.return_value = {
2265 "some_ip_profile": "here",
2266 }
2267
2268 result = Ns._process_net_params(
2269 target_vld=target_vld,
2270 indata=indata,
2271 vim_info=vim_info,
2272 target_record_id=target_record_id,
2273 )
2274
2275 self.assertDictEqual(expected_result, result)
2276 self.assertTrue(ip_profile_to_ro.called)
2277
2278 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2279 def test__process_net_params_with_vim_info_sdn(
2280 self,
2281 ip_profile_to_ro,
2282 ):
2283 target_vld = {
2284 "name": "vld-name",
2285 }
2286 indata = {
2287 "name": "ns-name",
2288 }
2289 vim_info = {
2290 "sdn": "some-sdn",
2291 "sdn-ports": ["some", "ports", "here"],
2292 "vlds": ["some", "vlds", "here"],
2293 "type": "sdn-type",
2294 }
2295 target_record_id = "vld.sdn.something"
2296 expected_result = {
2297 "params": {
2298 "sdn-ports": ["some", "ports", "here"],
2299 "vlds": ["some", "vlds", "here"],
2300 "type": "sdn-type",
2301 }
2302 }
2303
2304 result = Ns._process_net_params(
2305 target_vld=target_vld,
2306 indata=indata,
2307 vim_info=vim_info,
2308 target_record_id=target_record_id,
2309 )
2310
2311 self.assertDictEqual(expected_result, result)
2312 self.assertFalse(ip_profile_to_ro.called)
2313
2314 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2315 def test__process_net_params_with_vim_info_sdn_target_vim(
2316 self,
2317 ip_profile_to_ro,
2318 ):
2319 target_vld = {
2320 "name": "vld-name",
2321 }
2322 indata = {
2323 "name": "ns-name",
2324 }
2325 vim_info = {
2326 "sdn": "some-sdn",
2327 "sdn-ports": ["some", "ports", "here"],
2328 "vlds": ["some", "vlds", "here"],
2329 "target_vim": "some-vim",
2330 "type": "sdn-type",
2331 }
2332 target_record_id = "vld.sdn.something"
2333 expected_result = {
2334 "depends_on": ["some-vim vld.sdn"],
2335 "params": {
2336 "sdn-ports": ["some", "ports", "here"],
2337 "vlds": ["some", "vlds", "here"],
2338 "target_vim": "some-vim",
2339 "type": "sdn-type",
2340 },
2341 }
2342
2343 result = Ns._process_net_params(
2344 target_vld=target_vld,
2345 indata=indata,
2346 vim_info=vim_info,
2347 target_record_id=target_record_id,
2348 )
2349
2350 self.assertDictEqual(expected_result, result)
2351 self.assertFalse(ip_profile_to_ro.called)
2352
2353 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2354 def test__process_net_params_with_vim_network_name(
2355 self,
2356 ip_profile_to_ro,
2357 ):
2358 target_vld = {
2359 "name": "vld-name",
2360 }
2361 indata = {
2362 "name": "ns-name",
2363 }
2364 vim_info = {
2365 "vim_network_name": "some-network-name",
2366 }
2367 target_record_id = "vld.sdn.something"
2368 expected_result = {
2369 "find_params": {
2370 "filter_dict": {
2371 "name": "some-network-name",
2372 },
2373 },
2374 }
2375
2376 result = Ns._process_net_params(
2377 target_vld=target_vld,
2378 indata=indata,
2379 vim_info=vim_info,
2380 target_record_id=target_record_id,
2381 )
2382
2383 self.assertDictEqual(expected_result, result)
2384 self.assertFalse(ip_profile_to_ro.called)
2385
2386 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2387 def test__process_net_params_with_vim_network_id(
2388 self,
2389 ip_profile_to_ro,
2390 ):
2391 target_vld = {
2392 "name": "vld-name",
2393 }
2394 indata = {
2395 "name": "ns-name",
2396 }
2397 vim_info = {
2398 "vim_network_id": "some-network-id",
2399 }
2400 target_record_id = "vld.sdn.something"
2401 expected_result = {
2402 "find_params": {
2403 "filter_dict": {
2404 "id": "some-network-id",
2405 },
2406 },
2407 }
2408
2409 result = Ns._process_net_params(
2410 target_vld=target_vld,
2411 indata=indata,
2412 vim_info=vim_info,
2413 target_record_id=target_record_id,
2414 )
2415
2416 self.assertDictEqual(expected_result, result)
2417 self.assertFalse(ip_profile_to_ro.called)
2418
2419 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2420 def test__process_net_params_with_mgmt_network(
2421 self,
2422 ip_profile_to_ro,
2423 ):
2424 target_vld = {
2425 "id": "vld-id",
2426 "name": "vld-name",
2427 "mgmt-network": "some-mgmt-network",
2428 }
2429 indata = {
2430 "name": "ns-name",
2431 }
2432 vim_info = {}
2433 target_record_id = "vld.sdn.something"
2434 expected_result = {
2435 "find_params": {
2436 "mgmt": True,
2437 "name": "vld-id",
2438 },
2439 }
2440
2441 result = Ns._process_net_params(
2442 target_vld=target_vld,
2443 indata=indata,
2444 vim_info=vim_info,
2445 target_record_id=target_record_id,
2446 )
2447
2448 self.assertDictEqual(expected_result, result)
2449 self.assertFalse(ip_profile_to_ro.called)
2450
2451 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2452 def test__process_net_params_with_underlay_eline(
2453 self,
2454 ip_profile_to_ro,
2455 ):
2456 target_vld = {
2457 "name": "vld-name",
2458 "underlay": "some-underlay-here",
2459 "type": "ELINE",
2460 }
2461 indata = {
2462 "name": "ns-name",
2463 }
2464 vim_info = {
2465 "provider_network": "some-profile-here",
2466 }
2467 target_record_id = ""
2468 expected_result = {
2469 "params": {
2470 "ip_profile": {
2471 "some_ip_profile": "here",
2472 },
2473 "net_name": "ns-name-vld-name",
2474 "net_type": "ptp",
2475 "provider_network_profile": "some-profile-here",
2476 }
2477 }
2478
2479 ip_profile_to_ro.return_value = {
2480 "some_ip_profile": "here",
2481 }
2482
2483 result = Ns._process_net_params(
2484 target_vld=target_vld,
2485 indata=indata,
2486 vim_info=vim_info,
2487 target_record_id=target_record_id,
2488 )
2489
2490 self.assertDictEqual(expected_result, result)
2491 self.assertTrue(ip_profile_to_ro.called)
2492
2493 @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro")
2494 def test__process_net_params_with_underlay_elan(
2495 self,
2496 ip_profile_to_ro,
2497 ):
2498 target_vld = {
2499 "name": "vld-name",
2500 "underlay": "some-underlay-here",
2501 "type": "ELAN",
2502 }
2503 indata = {
2504 "name": "ns-name",
2505 }
2506 vim_info = {
2507 "provider_network": "some-profile-here",
2508 }
2509 target_record_id = ""
2510 expected_result = {
2511 "params": {
2512 "ip_profile": {
2513 "some_ip_profile": "here",
2514 },
2515 "net_name": "ns-name-vld-name",
2516 "net_type": "data",
2517 "provider_network_profile": "some-profile-here",
2518 }
2519 }
2520
2521 ip_profile_to_ro.return_value = {
2522 "some_ip_profile": "here",
2523 }
2524
2525 result = Ns._process_net_params(
2526 target_vld=target_vld,
2527 indata=indata,
2528 vim_info=vim_info,
2529 target_record_id=target_record_id,
2530 )
2531
2532 self.assertDictEqual(expected_result, result)
2533 self.assertTrue(ip_profile_to_ro.called)
2534
2535 def test__get_cloud_init_exception(self):
2536 db_mock = MagicMock(name="database mock")
2537 fs_mock = None
2538
2539 location = ""
2540
2541 with self.assertRaises(NsException):
2542 Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location)
2543
2544 def test__get_cloud_init_file_fs_exception(self):
2545 db_mock = MagicMock(name="database mock")
2546 fs_mock = None
2547
2548 location = "vnfr_id_123456:file:test_file"
2549 db_mock.get_one.return_value = {
2550 "_admin": {
2551 "storage": {
2552 "folder": "/home/osm",
2553 "pkg-dir": "vnfr_test_dir",
2554 },
2555 },
2556 }
2557
2558 with self.assertRaises(NsException):
2559 Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location)
2560
2561 def test__get_cloud_init_file(self):
2562 db_mock = MagicMock(name="database mock")
2563 fs_mock = MagicMock(name="filesystem mock")
2564 file_mock = MagicMock(name="file mock")
2565
2566 location = "vnfr_id_123456:file:test_file"
2567 cloud_init_content = "this is a cloud init file content"
2568
2569 db_mock.get_one.return_value = {
2570 "_admin": {
2571 "storage": {
2572 "folder": "/home/osm",
2573 "pkg-dir": "vnfr_test_dir",
2574 },
2575 },
2576 }
2577 fs_mock.file_open.return_value = file_mock
2578 file_mock.__enter__.return_value.read.return_value = cloud_init_content
2579
2580 result = Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location)
2581
2582 self.assertEqual(cloud_init_content, result)
2583
2584 def test__get_cloud_init_vdu(self):
2585 db_mock = MagicMock(name="database mock")
2586 fs_mock = None
2587
2588 location = "vnfr_id_123456:vdu:0"
2589 cloud_init_content = "this is a cloud init file content"
2590
2591 db_mock.get_one.return_value = {
2592 "vdu": {
2593 0: {
2594 "cloud-init": cloud_init_content,
2595 },
2596 },
2597 }
2598
2599 result = Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location)
2600
2601 self.assertEqual(cloud_init_content, result)
2602
2603 @patch("jinja2.Environment.__init__")
2604 def test__parse_jinja2_undefined_error(self, env_mock: Mock):
2605 cloud_init_content = None
2606 params = None
2607 context = None
2608
2609 env_mock.side_effect = UndefinedError("UndefinedError occurred.")
2610
2611 with self.assertRaises(NsException):
2612 Ns._parse_jinja2(
2613 cloud_init_content=cloud_init_content, params=params, context=context
2614 )
2615
2616 @patch("jinja2.Environment.__init__")
2617 def test__parse_jinja2_template_error(self, env_mock: Mock):
2618 cloud_init_content = None
2619 params = None
2620 context = None
2621
2622 env_mock.side_effect = TemplateError("TemplateError occurred.")
2623
2624 with self.assertRaises(NsException):
2625 Ns._parse_jinja2(
2626 cloud_init_content=cloud_init_content, params=params, context=context
2627 )
2628
2629 @patch("jinja2.Environment.__init__")
2630 def test__parse_jinja2_template_not_found(self, env_mock: Mock):
2631 cloud_init_content = None
2632 params = None
2633 context = None
2634
2635 env_mock.side_effect = TemplateNotFound("TemplateNotFound occurred.")
2636
2637 with self.assertRaises(NsException):
2638 Ns._parse_jinja2(
2639 cloud_init_content=cloud_init_content, params=params, context=context
2640 )
2641
2642 def test_rendering_jinja2_temp_without_special_characters(self):
2643 cloud_init_content = """
2644 disk_setup:
2645 ephemeral0:
2646 table_type: {{type}}
2647 layout: True
2648 overwrite: {{is_override}}
2649 runcmd:
2650 - [ ls, -l, / ]
2651 - [ sh, -xc, "echo $(date) '{{command}}'" ]
2652 """
2653 params = {
2654 "type": "mbr",
2655 "is_override": "False",
2656 "command": "; mkdir abc",
2657 }
2658 context = "cloud-init for VM"
2659 expected_result = """
2660 disk_setup:
2661 ephemeral0:
2662 table_type: mbr
2663 layout: True
2664 overwrite: False
2665 runcmd:
2666 - [ ls, -l, / ]
2667 - [ sh, -xc, "echo $(date) '; mkdir abc'" ]
2668 """
2669 result = Ns._parse_jinja2(
2670 cloud_init_content=cloud_init_content, params=params, context=context
2671 )
2672 self.assertEqual(result, expected_result)
2673
2674 def test_rendering_jinja2_temp_with_special_characters(self):
2675 cloud_init_content = """
2676 disk_setup:
2677 ephemeral0:
2678 table_type: {{type}}
2679 layout: True
2680 overwrite: {{is_override}}
2681 runcmd:
2682 - [ ls, -l, / ]
2683 - [ sh, -xc, "echo $(date) '{{command}}'" ]
2684 """
2685 params = {
2686 "type": "mbr",
2687 "is_override": "False",
2688 "command": "& rm -rf",
2689 }
2690 context = "cloud-init for VM"
2691 expected_result = """
2692 disk_setup:
2693 ephemeral0:
2694 table_type: mbr
2695 layout: True
2696 overwrite: False
2697 runcmd:
2698 - [ ls, -l, / ]
2699 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
2700 """
2701 result = Ns._parse_jinja2(
2702 cloud_init_content=cloud_init_content, params=params, context=context
2703 )
2704 self.assertNotEqual(result, expected_result)
2705
2706 def test_rendering_jinja2_temp_with_special_characters_autoescape_is_false(self):
2707 with patch("osm_ng_ro.ns.Environment") as mock_environment:
2708 mock_environment.return_value = Environment(
2709 undefined=StrictUndefined,
2710 autoescape=select_autoescape(default_for_string=False, default=False),
2711 )
2712 cloud_init_content = """
2713 disk_setup:
2714 ephemeral0:
2715 table_type: {{type}}
2716 layout: True
2717 overwrite: {{is_override}}
2718 runcmd:
2719 - [ ls, -l, / ]
2720 - [ sh, -xc, "echo $(date) '{{command}}'" ]
2721 """
2722 params = {
2723 "type": "mbr",
2724 "is_override": "False",
2725 "command": "& rm -rf /",
2726 }
2727 context = "cloud-init for VM"
2728 expected_result = """
2729 disk_setup:
2730 ephemeral0:
2731 table_type: mbr
2732 layout: True
2733 overwrite: False
2734 runcmd:
2735 - [ ls, -l, / ]
2736 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
2737 """
2738 result = Ns._parse_jinja2(
2739 cloud_init_content=cloud_init_content,
2740 params=params,
2741 context=context,
2742 )
2743 self.assertEqual(result, expected_result)
2744
2745 def test__process_vdu_params_empty_kargs(self):
2746 pass
2747
2748 def test__process_vdu_params_interface_ns_vld_id(self):
2749 pass
2750
2751 def test__process_vdu_params_interface_vnf_vld_id(self):
2752 pass
2753
2754 def test__process_vdu_params_interface_unknown(self):
2755 pass
2756
2757 def test__process_vdu_params_interface_port_security_enabled(self):
2758 pass
2759
2760 def test__process_vdu_params_interface_port_security_disable_strategy(self):
2761 pass
2762
2763 def test__process_vdu_params_interface_sriov(self):
2764 pass
2765
2766 def test__process_vdu_params_interface_pci_passthrough(self):
2767 pass
2768
2769 def test__process_vdu_params_interface_om_mgmt(self):
2770 pass
2771
2772 def test__process_vdu_params_interface_mgmt_interface(self):
2773 pass
2774
2775 def test__process_vdu_params_interface_mgmt_vnf(self):
2776 pass
2777
2778 def test__process_vdu_params_interface_bridge(self):
2779 pass
2780
2781 def test__process_vdu_params_interface_ip_address(self):
2782 pass
2783
2784 def test__process_vdu_params_interface_mac_address(self):
2785 pass
2786
2787 def test__process_vdu_params_vdu_cloud_init_missing(self):
2788 pass
2789
2790 def test__process_vdu_params_vdu_cloud_init_present(self):
2791 pass
2792
2793 def test__process_vdu_params_vdu_boot_data_drive(self):
2794 pass
2795
2796 def test__process_vdu_params_vdu_ssh_keys(self):
2797 pass
2798
2799 def test__process_vdu_params_vdu_ssh_access_required(self):
2800 pass
2801
2802 @patch("osm_ng_ro.ns.Ns._get_cloud_init")
2803 @patch("osm_ng_ro.ns.Ns._parse_jinja2")
2804 def test__process_vdu_params_vdu_persistent_root_volume(
2805 self, get_cloud_init, parse_jinja2
2806 ):
2807 db = MagicMock(name="database mock")
2808 kwargs = {
2809 "db": db,
2810 "vdu2cloud_init": {},
2811 "vnfr": {
2812 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
2813 "member-vnf-index-ref": "vnf-several-volumes",
2814 },
2815 }
2816 get_cloud_init.return_value = {}
2817 parse_jinja2.return_value = {}
2818 db.get_one.return_value = {
2819 "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
2820 "df": [
2821 {
2822 "id": "default-df",
2823 "vdu-profile": [
2824 {"id": "several_volumes-VM", "min-number-of-instances": 1}
2825 ],
2826 }
2827 ],
2828 "id": "several_volumes-vnf",
2829 "product-name": "several_volumes-vnf",
2830 "vdu": [
2831 {
2832 "id": "several_volumes-VM",
2833 "name": "several_volumes-VM",
2834 "sw-image-desc": "ubuntu20.04",
2835 "alternative-sw-image-desc": [
2836 "ubuntu20.04-aws",
2837 "ubuntu20.04-azure",
2838 ],
2839 "virtual-storage-desc": [
2840 "persistent-root-volume",
2841 "persistent-volume2",
2842 "ephemeral-volume",
2843 ],
2844 }
2845 ],
2846 "version": "1.0",
2847 "virtual-storage-desc": [
2848 {
2849 "id": "persistent-volume2",
2850 "type-of-storage": "persistent-storage:persistent-storage",
2851 "size-of-storage": "10",
2852 },
2853 {
2854 "id": "persistent-root-volume",
2855 "type-of-storage": "persistent-storage:persistent-storage",
2856 "size-of-storage": "10",
2857 },
2858 {
2859 "id": "ephemeral-volume",
2860 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
2861 "size-of-storage": "1",
2862 },
2863 ],
2864 "_admin": {
2865 "storage": {
2866 "fs": "mongo",
2867 "path": "/app/storage/",
2868 },
2869 "type": "vnfd",
2870 },
2871 }
2872
2873 target_vdu = {
2874 "_id": "09a0baa7-b7cb-4924-bd63-9f04a1c23960",
2875 "ns-flavor-id": "0",
2876 "ns-image-id": "0",
2877 "vdu-name": "several_volumes-VM",
2878 "interfaces": [
2879 {
2880 "name": "vdu-eth0",
2881 "ns-vld-id": "mgmtnet",
2882 }
2883 ],
2884 "virtual-storages": [
2885 {
2886 "id": "persistent-volume2",
2887 "size-of-storage": "10",
2888 "type-of-storage": "persistent-storage:persistent-storage",
2889 },
2890 {
2891 "id": "persistent-root-volume",
2892 "size-of-storage": "10",
2893 "type-of-storage": "persistent-storage:persistent-storage",
2894 },
2895 {
2896 "id": "ephemeral-volume",
2897 "size-of-storage": "1",
2898 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
2899 },
2900 ],
2901 }
2902 indata = {
2903 "name": "sample_name",
2904 }
2905 expected_result = [{"image_id": "ubuntu20.04", "size": "10"}, {"size": "10"}]
2906 result = Ns._process_vdu_params(
2907 target_vdu, indata, vim_info=None, target_record_id=None, **kwargs
2908 )
2909 self.assertEqual(
2910 expected_result, result["params"]["disk_list"], "Wrong Disk List"
2911 )
2912
2913 @patch("osm_ng_ro.ns.Ns._get_cloud_init")
2914 @patch("osm_ng_ro.ns.Ns._parse_jinja2")
2915 def test__process_vdu_params_vdu_without_persistent_storage(
2916 self, get_cloud_init, parse_jinja2
2917 ):
2918 db = MagicMock(name="database mock")
2919 kwargs = {
2920 "db": db,
2921 "vdu2cloud_init": {},
2922 "vnfr": {
2923 "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
2924 "member-vnf-index-ref": "vnf-several-volumes",
2925 },
2926 }
2927 get_cloud_init.return_value = {}
2928 parse_jinja2.return_value = {}
2929 db.get_one.return_value = {
2930 "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
2931 "df": [
2932 {
2933 "id": "default-df",
2934 "vdu-profile": [
2935 {"id": "without_volumes-VM", "min-number-of-instances": 1}
2936 ],
2937 }
2938 ],
2939 "id": "without_volumes-vnf",
2940 "product-name": "without_volumes-vnf",
2941 "vdu": [
2942 {
2943 "id": "without_volumes-VM",
2944 "name": "without_volumes-VM",
2945 "sw-image-desc": "ubuntu20.04",
2946 "alternative-sw-image-desc": [
2947 "ubuntu20.04-aws",
2948 "ubuntu20.04-azure",
2949 ],
2950 "virtual-storage-desc": ["root-volume", "ephemeral-volume"],
2951 }
2952 ],
2953 "version": "1.0",
2954 "virtual-storage-desc": [
2955 {"id": "root-volume", "size-of-storage": "10"},
2956 {
2957 "id": "ephemeral-volume",
2958 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
2959 "size-of-storage": "1",
2960 },
2961 ],
2962 "_admin": {
2963 "storage": {
2964 "fs": "mongo",
2965 "path": "/app/storage/",
2966 },
2967 "type": "vnfd",
2968 },
2969 }
2970
2971 target_vdu = {
2972 "_id": "09a0baa7-b7cb-4924-bd63-9f04a1c23960",
2973 "ns-flavor-id": "0",
2974 "ns-image-id": "0",
2975 "vdu-name": "without_volumes-VM",
2976 "interfaces": [
2977 {
2978 "name": "vdu-eth0",
2979 "ns-vld-id": "mgmtnet",
2980 }
2981 ],
2982 "virtual-storages": [
2983 {
2984 "id": "root-volume",
2985 "size-of-storage": "10",
2986 },
2987 {
2988 "id": "ephemeral-volume",
2989 "size-of-storage": "1",
2990 "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
2991 },
2992 ],
2993 }
2994 indata = {
2995 "name": "sample_name",
2996 }
2997 expected_result = []
2998 result = Ns._process_vdu_params(
2999 target_vdu, indata, vim_info=None, target_record_id=None, **kwargs
3000 )
3001 self.assertEqual(
3002 expected_result, result["params"]["disk_list"], "Wrong Disk List"
3003 )
3004
3005 def test__process_vdu_params(self):
3006 pass
3007
3008 @patch("osm_ng_ro.ns.Ns._assign_vim")
3009 def test__rebuild_start_stop_task(self, assign_vim):
3010 self.ns = Ns()
3011 extra_dict = {}
3012 actions = ["start", "stop", "rebuild"]
3013 vdu_id = "bb9c43f9-10a2-4569-a8a8-957c3528b6d1"
3014 vnf_id = "665b4165-ce24-4320-bf19-b9a45bade49f"
3015 vdu_index = "0"
3016 action_id = "bb937f49-3870-4169-b758-9732e1ff40f3"
3017 nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31"
3018 task_index = 0
3019 target_vim = "vim:f9f370ac-0d44-41a7-9000-457f2332bc35"
3020 t = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1"
3021 for action in actions:
3022 expected_result = {
3023 "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35",
3024 "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3",
3025 "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31",
3026 "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:0",
3027 "status": "SCHEDULED",
3028 "action": "EXEC",
3029 "item": "update",
3030 "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.0",
3031 "target_record_id": t,
3032 "params": {
3033 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3034 "action": action,
3035 },
3036 }
3037 extra_dict["params"] = {
3038 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3039 "action": action,
3040 }
3041 task = self.ns.rebuild_start_stop_task(
3042 vdu_id,
3043 vnf_id,
3044 vdu_index,
3045 action_id,
3046 nsr_id,
3047 task_index,
3048 target_vim,
3049 extra_dict,
3050 )
3051 self.assertEqual(task.get("action_id"), action_id)
3052 self.assertEqual(task.get("nsr_id"), nsr_id)
3053 self.assertEqual(task.get("target_id"), target_vim)
3054 self.assertDictEqual(task, expected_result)
3055
3056 @patch("osm_ng_ro.ns.Ns._assign_vim")
3057 def test_verticalscale_task(self, assign_vim):
3058 self.ns = Ns()
3059 extra_dict = {}
3060 vdu_index = "1"
3061 action_id = "bb937f49-3870-4169-b758-9732e1ff40f3"
3062 nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31"
3063 task_index = 1
3064 target_record_id = (
3065 "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:"
3066 "vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1"
3067 )
3068
3069 expected_result = {
3070 "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35",
3071 "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3",
3072 "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31",
3073 "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:1",
3074 "status": "SCHEDULED",
3075 "action": "EXEC",
3076 "item": "verticalscale",
3077 "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.1",
3078 "target_record_id": target_record_id,
3079 "params": {
3080 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3081 "flavor_dict": "flavor_dict",
3082 },
3083 }
3084 vdu = {
3085 "id": "bb9c43f9-10a2-4569-a8a8-957c3528b6d1",
3086 "vim_info": {
3087 "vim:f9f370ac-0d44-41a7-9000-457f2332bc35": {"interfaces": []}
3088 },
3089 }
3090 vnf = {"_id": "665b4165-ce24-4320-bf19-b9a45bade49f"}
3091 extra_dict["params"] = {
3092 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3093 "flavor_dict": "flavor_dict",
3094 }
3095 task = self.ns.verticalscale_task(
3096 vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict
3097 )
3098
3099 self.assertDictEqual(task, expected_result)
3100
3101 @patch("osm_ng_ro.ns.Ns._assign_vim")
3102 def test_migrate_task(self, assign_vim):
3103 self.ns = Ns()
3104 extra_dict = {}
3105 vdu_index = "1"
3106 action_id = "bb937f49-3870-4169-b758-9732e1ff40f3"
3107 nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31"
3108 task_index = 1
3109 target_record_id = (
3110 "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:"
3111 "vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1"
3112 )
3113
3114 expected_result = {
3115 "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35",
3116 "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3",
3117 "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31",
3118 "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:1",
3119 "status": "SCHEDULED",
3120 "action": "EXEC",
3121 "item": "migrate",
3122 "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.1",
3123 "target_record_id": target_record_id,
3124 "params": {
3125 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3126 "migrate_host": "migrateToHost",
3127 },
3128 }
3129 vdu = {
3130 "id": "bb9c43f9-10a2-4569-a8a8-957c3528b6d1",
3131 "vim_info": {
3132 "vim:f9f370ac-0d44-41a7-9000-457f2332bc35": {"interfaces": []}
3133 },
3134 }
3135 vnf = {"_id": "665b4165-ce24-4320-bf19-b9a45bade49f"}
3136 extra_dict["params"] = {
3137 "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396",
3138 "migrate_host": "migrateToHost",
3139 }
3140 task = self.ns.migrate_task(
3141 vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict
3142 )
3143
3144 self.assertDictEqual(task, expected_result)