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