X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=NG-RO%2Fosm_ng_ro%2Ftests%2Ftest_ns.py;h=af448508a301d6096994c9b96f3eda3e8410844a;hb=09dcc583ec5ab04e80e7a4349dcc4061ed794b47;hp=34590c1224233e119b3989aa2668761420dfde3d;hpb=049cbb1b256805f589c24776dcf092c77fefec6a;p=osm%2FRO.git diff --git a/NG-RO/osm_ng_ro/tests/test_ns.py b/NG-RO/osm_ng_ro/tests/test_ns.py index 34590c12..af448508 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns.py +++ b/NG-RO/osm_ng_ro/tests/test_ns.py @@ -16,9 +16,17 @@ ####################################################################################### import unittest -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch -from osm_ng_ro.ns import Ns +from jinja2 import ( + Environment, + select_autoescape, + StrictUndefined, + TemplateError, + TemplateNotFound, + UndefinedError, +) +from osm_ng_ro.ns import Ns, NsException __author__ = "Eduardo Sousa" @@ -129,6 +137,7 @@ class TestNs(unittest.TestCase): "vim_name": None, "vim_status": None, "vim_details": None, + "vim_message": None, "refresh_at": None, }, "modified_at": now, @@ -822,19 +831,18 @@ class TestNs(unittest.TestCase): self.assertDictEqual(expected_result, result) def test__process_guest_epa_numa_params_with_empty_numa_params(self): - expected_numa_result = {} + expected_numa_result = [] expected_epa_vcpu_set_result = False guest_epa_quota = {} numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params( guest_epa_quota=guest_epa_quota, ) - - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_wrong_numa_params(self): - expected_numa_result = {} + expected_numa_result = [] expected_epa_vcpu_set_result = False guest_epa_quota = {"no_nume": "here"} @@ -842,11 +850,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_numa_node_policy(self): - expected_numa_result = {} + expected_numa_result = [] expected_epa_vcpu_set_result = False guest_epa_quota = {"numa-node-policy": {}} @@ -854,11 +862,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_no_node(self): - expected_numa_result = {} + expected_numa_result = [] expected_epa_vcpu_set_result = False guest_epa_quota = { "numa-node-policy": { @@ -870,11 +878,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_1_node_num_cores(self): - expected_numa_result = {"cores": 3} + expected_numa_result = [{"cores": 3}] expected_epa_vcpu_set_result = True guest_epa_quota = { "numa-node-policy": { @@ -890,11 +898,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_1_node_paired_threads(self): - expected_numa_result = {"paired-threads": 3} + expected_numa_result = [{"paired_threads": 3}] expected_epa_vcpu_set_result = True guest_epa_quota = { "numa-node-policy": { @@ -910,13 +918,15 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_1_node_paired_threads_ids(self): - expected_numa_result = { - "paired-threads-id": [("0", "1"), ("4", "5")], - } + expected_numa_result = [ + { + "paired-threads-id": [("0", "1"), ("4", "5")], + } + ] expected_epa_vcpu_set_result = False guest_epa_quota = { "numa-node-policy": { @@ -943,11 +953,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_1_node_num_threads(self): - expected_numa_result = {"threads": 3} + expected_numa_result = [{"threads": 3}] expected_epa_vcpu_set_result = True guest_epa_quota = { "numa-node-policy": { @@ -963,11 +973,11 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_1_node_memory_mb(self): - expected_numa_result = {"memory": 2} + expected_numa_result = [{"memory": 2}] expected_epa_vcpu_set_result = False guest_epa_quota = { "numa-node-policy": { @@ -983,17 +993,71 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) - def test__process_guest_epa_numa_params_with_1_node(self): - expected_numa_result = { - "cores": 3, - "paired-threads": 3, - "paired-threads-id": [("0", "1"), ("4", "5")], - "threads": 3, - "memory": 2, + def test__process_guest_epa_numa_params_with_1_node_vcpu(self): + expected_numa_result = [ + { + "id": 0, + "vcpu": [0, 1], + } + ] + expected_epa_vcpu_set_result = False + guest_epa_quota = { + "numa-node-policy": { + "node": [{"id": "0", "vcpu": [{"id": "0"}, {"id": "1"}]}], + }, + } + + numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params( + guest_epa_quota=guest_epa_quota, + ) + + self.assertEqual(expected_numa_result, numa_result) + self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) + + def test__process_guest_epa_numa_params_with_2_node_vcpu(self): + expected_numa_result = [ + { + "id": 0, + "vcpu": [0, 1], + }, + { + "id": 1, + "vcpu": [2, 3], + }, + ] + + expected_epa_vcpu_set_result = False + guest_epa_quota = { + "numa-node-policy": { + "node": [ + {"id": "0", "vcpu": [{"id": "0"}, {"id": "1"}]}, + {"id": "1", "vcpu": [{"id": "2"}, {"id": "3"}]}, + ], + }, } + + numa_result, epa_vcpu_set_result = Ns._process_guest_epa_numa_params( + guest_epa_quota=guest_epa_quota, + ) + + self.assertEqual(expected_numa_result, numa_result) + self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) + + def test__process_guest_epa_numa_params_with_1_node(self): + expected_numa_result = [ + { + # "id": 0, + # "vcpu": [0, 1], + "cores": 3, + "paired_threads": 3, + "paired-threads-id": [("0", "1"), ("4", "5")], + "threads": 3, + "memory": 2, + } + ] expected_epa_vcpu_set_result = True guest_epa_quota = { "numa-node-policy": { @@ -1024,17 +1088,26 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_numa_params_with_2_nodes(self): - expected_numa_result = { - "cores": 3, - "paired-threads": 3, - "paired-threads-id": [("0", "1"), ("4", "5")], - "threads": 3, - "memory": 2, - } + expected_numa_result = [ + { + "cores": 3, + "paired_threads": 3, + "paired-threads-id": [("0", "1"), ("4", "5")], + "threads": 3, + "memory": 2, + }, + { + "cores": 7, + "paired_threads": 7, + "paired-threads-id": [("2", "3"), ("5", "6")], + "threads": 4, + "memory": 4, + }, + ] expected_epa_vcpu_set_result = True guest_epa_quota = { "numa-node-policy": { @@ -1083,7 +1156,7 @@ class TestNs(unittest.TestCase): guest_epa_quota=guest_epa_quota, ) - self.assertDictEqual(expected_numa_result, numa_result) + self.assertEqual(expected_numa_result, numa_result) self.assertEqual(expected_epa_vcpu_set_result, epa_vcpu_set_result) def test__process_guest_epa_cpu_pinning_params_with_empty_params(self): @@ -1228,10 +1301,15 @@ class TestNs(unittest.TestCase): guest_epa_cpu_pinning_params, guest_epa_quota_params, ): - expected_result = {} + expected_result = { + "mem-policy": "STRICT", + } target_flavor = { "guest-epa": { "vcpu-count": 1, + "numa-node-policy": { + "mem-policy": "STRICT", + }, }, } @@ -1259,9 +1337,16 @@ class TestNs(unittest.TestCase): ): expected_result = { "mempage-size": "1G", + "mem-policy": "STRICT", } target_flavor = { - "guest-epa": {"vcpu-count": 1, "mempage-size": "1G"}, + "guest-epa": { + "vcpu-count": 1, + "mempage-size": "1G", + "numa-node-policy": { + "mem-policy": "STRICT", + }, + }, } guest_epa_numa_params.return_value = ({}, False) @@ -1288,6 +1373,8 @@ class TestNs(unittest.TestCase): ): expected_result = { "mempage-size": "1G", + "cpu-pinning-policy": "DEDICATED", + "cpu-thread-pinning-policy": "PREFER", "numas": [ { "cores": 3, @@ -1354,13 +1441,15 @@ class TestNs(unittest.TestCase): } guest_epa_numa_params.return_value = ( - { - "cores": 3, - "paired-threads": 3, - "paired-threads-id": [("0", "1"), ("4", "5")], - "threads": 3, - "memory": 2, - }, + [ + { + "cores": 3, + "paired-threads": 3, + "paired-threads-id": [("0", "1"), ("4", "5")], + "threads": 3, + "memory": 2, + }, + ], True, ) guest_epa_cpu_pinning_params.return_value = ( @@ -1395,8 +1484,7 @@ class TestNs(unittest.TestCase): result = Ns._process_epa_params( target_flavor=target_flavor, ) - - self.assertDictEqual(expected_result, result) + self.assertEqual(expected_result, result) self.assertTrue(guest_epa_numa_params.called) self.assertTrue(guest_epa_cpu_pinning_params.called) self.assertTrue(guest_epa_quota_params.called) @@ -1406,8 +1494,15 @@ class TestNs(unittest.TestCase): self, epa_params, ): + target_flavor = {} - indata = {} + indata = { + "vnf": [ + { + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + }, + ], + } vim_info = {} target_record_id = "" @@ -1426,6 +1521,7 @@ class TestNs(unittest.TestCase): self, epa_params, ): + target_flavor = { "no-target-flavor": "here", } @@ -1448,6 +1544,7 @@ class TestNs(unittest.TestCase): self, epa_params, ): + expected_result = { "find_params": { "flavor_data": { @@ -1492,6 +1589,7 @@ class TestNs(unittest.TestCase): self, epa_params, ): + expected_result = { "find_params": { "flavor_data": { @@ -1538,6 +1636,52 @@ class TestNs(unittest.TestCase): self, epa_params, ): + db = MagicMock(name="database mock") + kwargs = { + "db": db, + } + + db.get_one.return_value = { + "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "df": [ + { + "id": "default-df", + "vdu-profile": [ + {"id": "without_volumes-VM", "min-number-of-instances": 1} + ], + } + ], + "id": "without_volumes-vnf", + "product-name": "without_volumes-vnf", + "vdu": [ + { + "id": "without_volumes-VM", + "name": "without_volumes-VM", + "sw-image-desc": "ubuntu20.04", + "alternative-sw-image-desc": [ + "ubuntu20.04-aws", + "ubuntu20.04-azure", + ], + "virtual-storage-desc": ["root-volume", "ephemeral-volume"], + } + ], + "version": "1.0", + "virtual-storage-desc": [ + {"id": "root-volume", "size-of-storage": "10"}, + { + "id": "ephemeral-volume", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + "size-of-storage": "1", + }, + ], + "_admin": { + "storage": { + "fs": "mongo", + "path": "/app/storage/", + }, + "type": "vnfd", + }, + } expected_result = { "find_params": { "flavor_data": { @@ -1578,6 +1722,7 @@ class TestNs(unittest.TestCase): ], }, ], + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", }, ], } @@ -1591,6 +1736,7 @@ class TestNs(unittest.TestCase): indata=indata, vim_info=vim_info, target_record_id=target_record_id, + **kwargs, ) self.assertTrue(epa_params.called) @@ -1601,6 +1747,7 @@ class TestNs(unittest.TestCase): self, epa_params, ): + expected_result = { "find_params": { "flavor_data": { @@ -1641,6 +1788,118 @@ class TestNs(unittest.TestCase): ], }, ], + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + }, + ], + } + vim_info = {} + target_record_id = "" + + epa_params.return_value = {} + + result = Ns._process_flavor_params( + target_flavor=target_flavor, + indata=indata, + vim_info=vim_info, + target_record_id=target_record_id, + ) + + self.assertTrue(epa_params.called) + self.assertDictEqual(result, expected_result) + + @patch("osm_ng_ro.ns.Ns._process_epa_params") + def test__process_flavor_params_with_persistent_root_disk( + self, + epa_params, + ): + db = MagicMock(name="database mock") + + kwargs = { + "db": db, + } + + db.get_one.return_value = { + "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "df": [ + { + "id": "default-df", + "vdu-profile": [ + {"id": "several_volumes-VM", "min-number-of-instances": 1} + ], + } + ], + "id": "several_volumes-vnf", + "product-name": "several_volumes-vnf", + "vdu": [ + { + "id": "several_volumes-VM", + "name": "several_volumes-VM", + "sw-image-desc": "ubuntu20.04", + "alternative-sw-image-desc": [ + "ubuntu20.04-aws", + "ubuntu20.04-azure", + ], + "virtual-storage-desc": [ + "persistent-root-volume", + ], + } + ], + "version": "1.0", + "virtual-storage-desc": [ + { + "id": "persistent-root-volume", + "type-of-storage": "persistent-storage:persistent-storage", + "size-of-storage": "10", + }, + ], + "_admin": { + "storage": { + "fs": "mongo", + "path": "/app/storage/", + }, + "type": "vnfd", + }, + } + expected_result = { + "find_params": { + "flavor_data": { + "disk": 0, + "ram": 1024, + "vcpus": 2, + }, + }, + "params": { + "flavor_data": { + "disk": 0, + "name": "test", + "ram": 1024, + "vcpus": 2, + }, + }, + } + target_flavor = { + "id": "test_id", + "name": "test", + "storage-gb": "10", + "memory-mb": "1024", + "vcpu-count": "2", + } + indata = { + "vnf": [ + { + "vdur": [ + { + "vdu-name": "several_volumes-VM", + "ns-flavor-id": "test_id", + "virtual-storages": [ + { + "type-of-storage": "persistent-storage:persistent-storage", + "size-of-storage": "10", + }, + ], + }, + ], + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", }, ], } @@ -1654,6 +1913,7 @@ class TestNs(unittest.TestCase): indata=indata, vim_info=vim_info, target_record_id=target_record_id, + **kwargs, ) self.assertTrue(epa_params.called) @@ -1664,6 +1924,7 @@ class TestNs(unittest.TestCase): self, epa_params, ): + expected_result = { "find_params": { "flavor_data": { @@ -1694,7 +1955,18 @@ class TestNs(unittest.TestCase): "memory-mb": "1024", "vcpu-count": "2", } - indata = {} + indata = { + "vnf": [ + { + "vdur": [ + { + "ns-flavor-id": "test_id", + }, + ], + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + }, + ], + } vim_info = {} target_record_id = "" @@ -1717,6 +1989,54 @@ class TestNs(unittest.TestCase): self, epa_params, ): + db = MagicMock(name="database mock") + + kwargs = { + "db": db, + } + + db.get_one.return_value = { + "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "df": [ + { + "id": "default-df", + "vdu-profile": [ + {"id": "without_volumes-VM", "min-number-of-instances": 1} + ], + } + ], + "id": "without_volumes-vnf", + "product-name": "without_volumes-vnf", + "vdu": [ + { + "id": "without_volumes-VM", + "name": "without_volumes-VM", + "sw-image-desc": "ubuntu20.04", + "alternative-sw-image-desc": [ + "ubuntu20.04-aws", + "ubuntu20.04-azure", + ], + "virtual-storage-desc": ["root-volume", "ephemeral-volume"], + } + ], + "version": "1.0", + "virtual-storage-desc": [ + {"id": "root-volume", "size-of-storage": "10"}, + { + "id": "ephemeral-volume", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + "size-of-storage": "1", + }, + ], + "_admin": { + "storage": { + "fs": "mongo", + "path": "/app/storage/", + }, + "type": "vnfd", + }, + } + expected_result = { "find_params": { "flavor_data": { @@ -1769,6 +2089,7 @@ class TestNs(unittest.TestCase): ], }, ], + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", }, ], } @@ -1784,6 +2105,7 @@ class TestNs(unittest.TestCase): indata=indata, vim_info=vim_info, target_record_id=target_record_id, + **kwargs, ) self.assertTrue(epa_params.called) @@ -2288,3 +2610,614 @@ class TestNs(unittest.TestCase): self.assertDictEqual(expected_result, result) self.assertTrue(ip_profile_to_ro.called) + + def test__get_cloud_init_exception(self): + db_mock = MagicMock(name="database mock") + fs_mock = None + + location = "" + + with self.assertRaises(NsException): + Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location) + + def test__get_cloud_init_file_fs_exception(self): + db_mock = MagicMock(name="database mock") + fs_mock = None + + location = "vnfr_id_123456:file:test_file" + db_mock.get_one.return_value = { + "_admin": { + "storage": { + "folder": "/home/osm", + "pkg-dir": "vnfr_test_dir", + }, + }, + } + + with self.assertRaises(NsException): + Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location) + + def test__get_cloud_init_file(self): + db_mock = MagicMock(name="database mock") + fs_mock = MagicMock(name="filesystem mock") + file_mock = MagicMock(name="file mock") + + location = "vnfr_id_123456:file:test_file" + cloud_init_content = "this is a cloud init file content" + + db_mock.get_one.return_value = { + "_admin": { + "storage": { + "folder": "/home/osm", + "pkg-dir": "vnfr_test_dir", + }, + }, + } + fs_mock.file_open.return_value = file_mock + file_mock.__enter__.return_value.read.return_value = cloud_init_content + + result = Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location) + + self.assertEqual(cloud_init_content, result) + + def test__get_cloud_init_vdu(self): + db_mock = MagicMock(name="database mock") + fs_mock = None + + location = "vnfr_id_123456:vdu:0" + cloud_init_content = "this is a cloud init file content" + + db_mock.get_one.return_value = { + "vdu": { + 0: { + "cloud-init": cloud_init_content, + }, + }, + } + + result = Ns._get_cloud_init(db=db_mock, fs=fs_mock, location=location) + + self.assertEqual(cloud_init_content, result) + + @patch("jinja2.Environment.__init__") + def test__parse_jinja2_undefined_error(self, env_mock: Mock): + cloud_init_content = None + params = None + context = None + + env_mock.side_effect = UndefinedError("UndefinedError occurred.") + + with self.assertRaises(NsException): + Ns._parse_jinja2( + cloud_init_content=cloud_init_content, params=params, context=context + ) + + @patch("jinja2.Environment.__init__") + def test__parse_jinja2_template_error(self, env_mock: Mock): + cloud_init_content = None + params = None + context = None + + env_mock.side_effect = TemplateError("TemplateError occurred.") + + with self.assertRaises(NsException): + Ns._parse_jinja2( + cloud_init_content=cloud_init_content, params=params, context=context + ) + + @patch("jinja2.Environment.__init__") + def test__parse_jinja2_template_not_found(self, env_mock: Mock): + cloud_init_content = None + params = None + context = None + + env_mock.side_effect = TemplateNotFound("TemplateNotFound occurred.") + + with self.assertRaises(NsException): + Ns._parse_jinja2( + cloud_init_content=cloud_init_content, params=params, context=context + ) + + def test_rendering_jinja2_temp_without_special_characters(self): + cloud_init_content = """ + disk_setup: + ephemeral0: + table_type: {{type}} + layout: True + overwrite: {{is_override}} + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '{{command}}'" ] + """ + params = { + "type": "mbr", + "is_override": "False", + "command": "; mkdir abc", + } + context = "cloud-init for VM" + expected_result = """ + disk_setup: + ephemeral0: + table_type: mbr + layout: True + overwrite: False + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '; mkdir abc'" ] + """ + result = Ns._parse_jinja2( + cloud_init_content=cloud_init_content, params=params, context=context + ) + self.assertEqual(result, expected_result) + + def test_rendering_jinja2_temp_with_special_characters(self): + cloud_init_content = """ + disk_setup: + ephemeral0: + table_type: {{type}} + layout: True + overwrite: {{is_override}} + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '{{command}}'" ] + """ + params = { + "type": "mbr", + "is_override": "False", + "command": "& rm -rf", + } + context = "cloud-init for VM" + expected_result = """ + disk_setup: + ephemeral0: + table_type: mbr + layout: True + overwrite: False + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '& rm -rf /'" ] + """ + result = Ns._parse_jinja2( + cloud_init_content=cloud_init_content, params=params, context=context + ) + self.assertNotEqual(result, expected_result) + + def test_rendering_jinja2_temp_with_special_characters_autoescape_is_false(self): + with patch("osm_ng_ro.ns.Environment") as mock_environment: + mock_environment.return_value = Environment( + undefined=StrictUndefined, + autoescape=select_autoescape(default_for_string=False, default=False), + ) + cloud_init_content = """ + disk_setup: + ephemeral0: + table_type: {{type}} + layout: True + overwrite: {{is_override}} + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '{{command}}'" ] + """ + params = { + "type": "mbr", + "is_override": "False", + "command": "& rm -rf /", + } + context = "cloud-init for VM" + expected_result = """ + disk_setup: + ephemeral0: + table_type: mbr + layout: True + overwrite: False + runcmd: + - [ ls, -l, / ] + - [ sh, -xc, "echo $(date) '& rm -rf /'" ] + """ + result = Ns._parse_jinja2( + cloud_init_content=cloud_init_content, + params=params, + context=context, + ) + self.assertEqual(result, expected_result) + + def test__process_vdu_params_empty_kargs(self): + pass + + def test__process_vdu_params_interface_ns_vld_id(self): + pass + + def test__process_vdu_params_interface_vnf_vld_id(self): + pass + + def test__process_vdu_params_interface_unknown(self): + pass + + def test__process_vdu_params_interface_port_security_enabled(self): + pass + + def test__process_vdu_params_interface_port_security_disable_strategy(self): + pass + + def test__process_vdu_params_interface_sriov(self): + pass + + def test__process_vdu_params_interface_pci_passthrough(self): + pass + + def test__process_vdu_params_interface_om_mgmt(self): + pass + + def test__process_vdu_params_interface_mgmt_interface(self): + pass + + def test__process_vdu_params_interface_mgmt_vnf(self): + pass + + def test__process_vdu_params_interface_bridge(self): + pass + + def test__process_vdu_params_interface_ip_address(self): + pass + + def test__process_vdu_params_interface_mac_address(self): + pass + + def test__process_vdu_params_vdu_cloud_init_missing(self): + pass + + def test__process_vdu_params_vdu_cloud_init_present(self): + pass + + def test__process_vdu_params_vdu_boot_data_drive(self): + pass + + def test__process_vdu_params_vdu_ssh_keys(self): + pass + + def test__process_vdu_params_vdu_ssh_access_required(self): + pass + + @patch("osm_ng_ro.ns.Ns._get_cloud_init") + @patch("osm_ng_ro.ns.Ns._parse_jinja2") + def test__process_vdu_params_vdu_persistent_root_volume( + self, get_cloud_init, parse_jinja2 + ): + db = MagicMock(name="database mock") + kwargs = { + "db": db, + "vdu2cloud_init": {}, + "vnfr": { + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "member-vnf-index-ref": "vnf-several-volumes", + }, + } + get_cloud_init.return_value = {} + parse_jinja2.return_value = {} + db.get_one.return_value = { + "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "df": [ + { + "id": "default-df", + "vdu-profile": [ + {"id": "several_volumes-VM", "min-number-of-instances": 1} + ], + } + ], + "id": "several_volumes-vnf", + "product-name": "several_volumes-vnf", + "vdu": [ + { + "id": "several_volumes-VM", + "name": "several_volumes-VM", + "sw-image-desc": "ubuntu20.04", + "alternative-sw-image-desc": [ + "ubuntu20.04-aws", + "ubuntu20.04-azure", + ], + "virtual-storage-desc": [ + "persistent-root-volume", + "persistent-volume2", + "ephemeral-volume", + ], + } + ], + "version": "1.0", + "virtual-storage-desc": [ + { + "id": "persistent-volume2", + "type-of-storage": "persistent-storage:persistent-storage", + "size-of-storage": "10", + }, + { + "id": "persistent-root-volume", + "type-of-storage": "persistent-storage:persistent-storage", + "size-of-storage": "10", + }, + { + "id": "ephemeral-volume", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + "size-of-storage": "1", + }, + ], + "_admin": { + "storage": { + "fs": "mongo", + "path": "/app/storage/", + }, + "type": "vnfd", + }, + } + + target_vdu = { + "_id": "09a0baa7-b7cb-4924-bd63-9f04a1c23960", + "ns-flavor-id": "0", + "ns-image-id": "0", + "vdu-name": "several_volumes-VM", + "interfaces": [ + { + "name": "vdu-eth0", + "ns-vld-id": "mgmtnet", + } + ], + "virtual-storages": [ + { + "id": "persistent-volume2", + "size-of-storage": "10", + "type-of-storage": "persistent-storage:persistent-storage", + }, + { + "id": "persistent-root-volume", + "size-of-storage": "10", + "type-of-storage": "persistent-storage:persistent-storage", + }, + { + "id": "ephemeral-volume", + "size-of-storage": "1", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + }, + ], + } + indata = { + "name": "sample_name", + } + expected_result = [{"image_id": "ubuntu20.04", "size": "10"}, {"size": "10"}] + result = Ns._process_vdu_params( + target_vdu, indata, vim_info=None, target_record_id=None, **kwargs + ) + self.assertEqual( + expected_result, result["params"]["disk_list"], "Wrong Disk List" + ) + + @patch("osm_ng_ro.ns.Ns._get_cloud_init") + @patch("osm_ng_ro.ns.Ns._parse_jinja2") + def test__process_vdu_params_vdu_without_persistent_storage( + self, get_cloud_init, parse_jinja2 + ): + db = MagicMock(name="database mock") + kwargs = { + "db": db, + "vdu2cloud_init": {}, + "vnfr": { + "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "member-vnf-index-ref": "vnf-several-volumes", + }, + } + get_cloud_init.return_value = {} + parse_jinja2.return_value = {} + db.get_one.return_value = { + "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18", + "df": [ + { + "id": "default-df", + "vdu-profile": [ + {"id": "without_volumes-VM", "min-number-of-instances": 1} + ], + } + ], + "id": "without_volumes-vnf", + "product-name": "without_volumes-vnf", + "vdu": [ + { + "id": "without_volumes-VM", + "name": "without_volumes-VM", + "sw-image-desc": "ubuntu20.04", + "alternative-sw-image-desc": [ + "ubuntu20.04-aws", + "ubuntu20.04-azure", + ], + "virtual-storage-desc": ["root-volume", "ephemeral-volume"], + } + ], + "version": "1.0", + "virtual-storage-desc": [ + {"id": "root-volume", "size-of-storage": "10"}, + { + "id": "ephemeral-volume", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + "size-of-storage": "1", + }, + ], + "_admin": { + "storage": { + "fs": "mongo", + "path": "/app/storage/", + }, + "type": "vnfd", + }, + } + + target_vdu = { + "_id": "09a0baa7-b7cb-4924-bd63-9f04a1c23960", + "ns-flavor-id": "0", + "ns-image-id": "0", + "vdu-name": "without_volumes-VM", + "interfaces": [ + { + "name": "vdu-eth0", + "ns-vld-id": "mgmtnet", + } + ], + "virtual-storages": [ + { + "id": "root-volume", + "size-of-storage": "10", + }, + { + "id": "ephemeral-volume", + "size-of-storage": "1", + "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage", + }, + ], + } + indata = { + "name": "sample_name", + } + expected_result = [] + result = Ns._process_vdu_params( + target_vdu, indata, vim_info=None, target_record_id=None, **kwargs + ) + self.assertEqual( + expected_result, result["params"]["disk_list"], "Wrong Disk List" + ) + + def test__process_vdu_params(self): + pass + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test__rebuild_start_stop_task(self, assign_vim): + self.ns = Ns() + extra_dict = {} + actions = ["start", "stop", "rebuild"] + vdu_id = "bb9c43f9-10a2-4569-a8a8-957c3528b6d1" + vnf_id = "665b4165-ce24-4320-bf19-b9a45bade49f" + vdu_index = "0" + action_id = "bb937f49-3870-4169-b758-9732e1ff40f3" + nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31" + task_index = 0 + target_vim = "vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + t = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1" + for action in actions: + expected_result = { + "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35", + "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3", + "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31", + "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:0", + "status": "SCHEDULED", + "action": "EXEC", + "item": "update", + "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.0", + "target_record_id": t, + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "action": action, + }, + } + extra_dict["params"] = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "action": action, + } + task = self.ns.rebuild_start_stop_task( + vdu_id, + vnf_id, + vdu_index, + action_id, + nsr_id, + task_index, + target_vim, + extra_dict, + ) + self.assertEqual(task.get("action_id"), action_id) + self.assertEqual(task.get("nsr_id"), nsr_id) + self.assertEqual(task.get("target_id"), target_vim) + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_verticalscale_task(self, assign_vim): + self.ns = Ns() + extra_dict = {} + vdu_index = "1" + action_id = "bb937f49-3870-4169-b758-9732e1ff40f3" + nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31" + task_index = 1 + target_record_id = ( + "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:" + "vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1" + ) + + expected_result = { + "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35", + "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3", + "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31", + "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:1", + "status": "SCHEDULED", + "action": "EXEC", + "item": "verticalscale", + "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.1", + "target_record_id": target_record_id, + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "flavor_dict": "flavor_dict", + }, + } + vdu = { + "id": "bb9c43f9-10a2-4569-a8a8-957c3528b6d1", + "vim_info": { + "vim:f9f370ac-0d44-41a7-9000-457f2332bc35": {"interfaces": []} + }, + } + vnf = {"_id": "665b4165-ce24-4320-bf19-b9a45bade49f"} + extra_dict["params"] = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "flavor_dict": "flavor_dict", + } + task = self.ns.verticalscale_task( + vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict + ) + + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_migrate_task(self, assign_vim): + self.ns = Ns() + extra_dict = {} + vdu_index = "1" + action_id = "bb937f49-3870-4169-b758-9732e1ff40f3" + nsr_id = "993166fe-723e-4680-ac4b-b1af2541ae31" + task_index = 1 + target_record_id = ( + "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:" + "vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1" + ) + + expected_result = { + "target_id": "vim:f9f370ac-0d44-41a7-9000-457f2332bc35", + "action_id": "bb937f49-3870-4169-b758-9732e1ff40f3", + "nsr_id": "993166fe-723e-4680-ac4b-b1af2541ae31", + "task_id": "bb937f49-3870-4169-b758-9732e1ff40f3:1", + "status": "SCHEDULED", + "action": "EXEC", + "item": "migrate", + "target_record": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.1", + "target_record_id": target_record_id, + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "migrate_host": "migrateToHost", + }, + } + vdu = { + "id": "bb9c43f9-10a2-4569-a8a8-957c3528b6d1", + "vim_info": { + "vim:f9f370ac-0d44-41a7-9000-457f2332bc35": {"interfaces": []} + }, + } + vnf = {"_id": "665b4165-ce24-4320-bf19-b9a45bade49f"} + extra_dict["params"] = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "migrate_host": "migrateToHost", + } + task = self.ns.migrate_task( + vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict + ) + + self.assertDictEqual(task, expected_result)