X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=NG-RO%2Fosm_ng_ro%2Ftests%2Ftest_ns.py;h=c692c51b609e477d3b0a2fbf41e16624eb7f30d1;hb=cb7269c6ed3caa4ad7807e01bf0067f373423702;hp=d1ab0a46774ad6b8220a70d8613a16a8051a09b9;hpb=6a6e3344cc0d68064a592941e33cdc6629eb3405;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 d1ab0a46..c692c51b 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns.py +++ b/NG-RO/osm_ng_ro/tests/test_ns.py @@ -206,6 +206,7 @@ expected_extra_dict2 = { "start": True, }, } + tasks_by_target_record_id = { "nsrs:th47f48-9870-4169-b758-9732e1ff40f3": { "extra_dict": { @@ -307,6 +308,77 @@ runcmd: - [ ls, -l, / ] - [ sh, -xc, "echo $(date) '& rm -rf /'" ] """ +vdu_id = "bb9c43f9-10a2-4569-a8a8-957c3528b6d1" +vnf_id = "665b4165-ce24-4320-bf19-b9a45bade49f" +target_vim = "vim:f9f370ac-0d44-41a7-9000-457f2332bc35" +action_id = "bb937f49-3870-4169-b758-9732e1ff40f3" +nsr_id_2 = "993166fe-723e-4680-ac4b-b1af2541ae31" +target_record_1 = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.1.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" +target_record_id = ( + "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:" + "vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1" +) +expected_result_vertical_scale = { + "target_id": target_vim, + "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": target_record_1, + "target_record_id": target_record_id, + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "flavor_dict": "flavor_dict", + }, +} +vdu = { + "id": vdu_id, + "vim_info": {target_vim: {"interfaces": []}}, +} +vnf = {"_id": vnf_id} +extra_dict_vertical_scale = { + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "flavor_dict": "flavor_dict", + }, +} +extra_dict_migrate = { + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "migrate_host": "migrateToHost", + }, +} +expected_result_migrate = { + "target_id": target_vim, + "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.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35", + "target_record_id": target_record_id, + "params": { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "migrate_host": "migrateToHost", + }, +} +expected_result_rebuild_start_stop = { + "target_id": target_vim, + "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_id": "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.bb9c43f9-10a2-4569-a8a8-957c3528b6d1", +} + + +class TestException(Exception): + pass class CopyingMock(MagicMock): @@ -1815,7 +1887,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - target_flavor = {} indata = { "vnf": [ @@ -1842,7 +1913,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - target_flavor = { "no-target-flavor": "here", } @@ -1865,7 +1935,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - expected_result = { "find_params": { "flavor_data": { @@ -1910,7 +1979,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - expected_result = { "find_params": { "flavor_data": { @@ -2067,7 +2135,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - expected_result = { "find_params": { "flavor_data": { @@ -2132,7 +2199,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - kwargs = { "db": db, } @@ -2243,7 +2309,6 @@ class TestNs(unittest.TestCase): self, epa_params, ): - expected_result = { "find_params": { "flavor_data": { @@ -2304,11 +2369,57 @@ class TestNs(unittest.TestCase): self.assertDictEqual(result, expected_result) @patch("osm_ng_ro.ns.Ns._process_epa_params") - def test__process_flavor_params( + def test__process_flavor_params_with_vim_flavor_id( self, epa_params, ): + expected_result = { + "find_params": { + "vim_flavor_id": "test.flavor", + }, + } + target_flavor = { + "id": "test_id", + "name": "test", + "storage-gb": "10", + "memory-mb": "1024", + "vcpu-count": "2", + } + indata = { + "vnf": [ + { + "vdur": [ + { + "ns-flavor-id": "test_id", + "additionalParams": { + "OSM": {"vim_flavor_id": "test.flavor"} + }, + }, + ], + "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.assertFalse(epa_params.called) + self.assertDictEqual(result, expected_result) + @patch("osm_ng_ro.ns.Ns._process_epa_params") + def test__process_flavor_params( + self, + epa_params, + ): kwargs = { "db": db, } @@ -2429,213 +2540,8 @@ class TestNs(unittest.TestCase): self.assertTrue(epa_params.called) self.assertDictEqual(result, expected_result) - def test__ip_profile_to_ro_with_none(self): - ip_profile = None - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertIsNone(result) - - def test__ip_profile_to_ro_with_empty_profile(self): - ip_profile = {} - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertIsNone(result) - - def test__ip_profile_to_ro_with_wrong_profile(self): - ip_profile = { - "no-profile": "here", - } - expected_result = { - "ip_version": "IPv4", - "subnet_address": None, - "gateway_address": None, - "dhcp_enabled": False, - "dhcp_start_address": None, - "dhcp_count": None, - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - def test__ip_profile_to_ro_with_ipv4_profile(self): - ip_profile = { - "ip-version": "ipv4", - "subnet-address": "192.168.0.0/24", - "gateway-address": "192.168.0.254", - "dhcp-params": { - "enabled": True, - "start-address": "192.168.0.10", - "count": 25, - }, - } - expected_result = { - "ip_version": "IPv4", - "subnet_address": "192.168.0.0/24", - "gateway_address": "192.168.0.254", - "dhcp_enabled": True, - "dhcp_start_address": "192.168.0.10", - "dhcp_count": 25, - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - def test__ip_profile_to_ro_with_ipv6_profile(self): - ip_profile = { - "ip-version": "ipv6", - "subnet-address": "2001:0200:0001::/48", - "gateway-address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe", - "dhcp-params": { - "enabled": True, - "start-address": "2001:0200:0001::0010", - "count": 25, - }, - } - expected_result = { - "ip_version": "IPv6", - "subnet_address": "2001:0200:0001::/48", - "gateway_address": "2001:0200:0001:ffff:ffff:ffff:ffff:fffe", - "dhcp_enabled": True, - "dhcp_start_address": "2001:0200:0001::0010", - "dhcp_count": 25, - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - def test__ip_profile_to_ro_with_dns_server(self): - ip_profile = { - "ip-version": "ipv4", - "subnet-address": "192.168.0.0/24", - "gateway-address": "192.168.0.254", - "dhcp-params": { - "enabled": True, - "start-address": "192.168.0.10", - "count": 25, - }, - "dns-server": [ - { - "address": "8.8.8.8", - }, - { - "address": "1.1.1.1", - }, - { - "address": "1.0.0.1", - }, - ], - } - expected_result = { - "ip_version": "IPv4", - "subnet_address": "192.168.0.0/24", - "gateway_address": "192.168.0.254", - "dhcp_enabled": True, - "dhcp_start_address": "192.168.0.10", - "dhcp_count": 25, - "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1", - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - def test__ip_profile_to_ro_with_security_group(self): - ip_profile = { - "ip-version": "ipv4", - "subnet-address": "192.168.0.0/24", - "gateway-address": "192.168.0.254", - "dhcp-params": { - "enabled": True, - "start-address": "192.168.0.10", - "count": 25, - }, - "security-group": { - "some-security-group": "here", - }, - } - expected_result = { - "ip_version": "IPv4", - "subnet_address": "192.168.0.0/24", - "gateway_address": "192.168.0.254", - "dhcp_enabled": True, - "dhcp_start_address": "192.168.0.10", - "dhcp_count": 25, - "security_group": { - "some-security-group": "here", - }, - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - def test__ip_profile_to_ro(self): - ip_profile = { - "ip-version": "ipv4", - "subnet-address": "192.168.0.0/24", - "gateway-address": "192.168.0.254", - "dhcp-params": { - "enabled": True, - "start-address": "192.168.0.10", - "count": 25, - }, - "dns-server": [ - { - "address": "8.8.8.8", - }, - { - "address": "1.1.1.1", - }, - { - "address": "1.0.0.1", - }, - ], - "security-group": { - "some-security-group": "here", - }, - } - expected_result = { - "ip_version": "IPv4", - "subnet_address": "192.168.0.0/24", - "gateway_address": "192.168.0.254", - "dhcp_enabled": True, - "dhcp_start_address": "192.168.0.10", - "dhcp_count": 25, - "dns_address": "8.8.8.8;1.1.1.1;1.0.0.1", - "security_group": { - "some-security-group": "here", - }, - } - - result = Ns._ip_profile_to_ro( - ip_profile=ip_profile, - ) - - self.assertDictEqual(expected_result, result) - - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_empty_params( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2645,6 +2551,9 @@ class TestNs(unittest.TestCase): } vim_info = { "provider_network": "some-profile-here", + "ip_profile": { + "some_ip_profile": "here", + }, } target_record_id = "" expected_result = { @@ -2658,10 +2567,6 @@ class TestNs(unittest.TestCase): } } - ip_profile_to_ro.return_value = { - "some_ip_profile": "here", - } - result = Ns._process_net_params( target_vld=target_vld, indata=indata, @@ -2670,12 +2575,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertTrue(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_vim_info_sdn( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2706,12 +2608,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertFalse(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_vim_info_sdn_target_vim( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2745,12 +2644,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertFalse(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_vim_network_name( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2778,12 +2674,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertFalse(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_vim_network_id( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2811,12 +2704,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertFalse(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_mgmt_network( self, - ip_profile_to_ro, ): target_vld = { "id": "vld-id", @@ -2843,12 +2733,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertFalse(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_underlay_eline( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2860,6 +2747,9 @@ class TestNs(unittest.TestCase): } vim_info = { "provider_network": "some-profile-here", + "ip_profile": { + "some_ip_profile": "here", + }, } target_record_id = "" expected_result = { @@ -2873,10 +2763,6 @@ class TestNs(unittest.TestCase): } } - ip_profile_to_ro.return_value = { - "some_ip_profile": "here", - } - result = Ns._process_net_params( target_vld=target_vld, indata=indata, @@ -2885,12 +2771,9 @@ class TestNs(unittest.TestCase): ) self.assertDictEqual(expected_result, result) - self.assertTrue(ip_profile_to_ro.called) - @patch("osm_ng_ro.ns.Ns._ip_profile_to_ro") def test__process_net_params_with_underlay_elan( self, - ip_profile_to_ro, ): target_vld = { "name": "vld-name", @@ -2902,6 +2785,9 @@ class TestNs(unittest.TestCase): } vim_info = { "provider_network": "some-profile-here", + "ip_profile": { + "some_ip_profile": "here", + }, } target_record_id = "" expected_result = { @@ -2915,10 +2801,6 @@ class TestNs(unittest.TestCase): } } - ip_profile_to_ro.return_value = { - "some_ip_profile": "here", - } - result = Ns._process_net_params( target_vld=target_vld, indata=indata, @@ -2927,7 +2809,6 @@ 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") @@ -3140,143 +3021,278 @@ class TestNs(unittest.TestCase): self.assertEqual(result, expected_result) @patch("osm_ng_ro.ns.Ns._assign_vim") - def test__rebuild_start_stop_task(self, assign_vim): + def test__rebuild_start_stop_task__successful(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, - }, + params = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "action": action, + } + extra_dict["params"] = params + expected_result = deepcopy(expected_result_rebuild_start_stop) + expected_result[ + "target_record" + ] = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.0.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + expected_result["params"] = params + task = self.ns.rebuild_start_stop_task( + vdu_id, + vnf_id, + vdu_index, + action_id, + nsr_id_2, + task_index, + target_vim, + extra_dict, + ) + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test__rebuild_start_stop_task__empty_extra_dict__task_without_params( + self, assign_vim + ): + self.ns = Ns() + extra_dict = {} + actions = ["start", "stop", "rebuild"] + vdu_index = "0" + task_index = 0 + expected_result = deepcopy(expected_result_rebuild_start_stop) + expected_result[ + "target_record" + ] = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.0.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + for _ in actions: + task = self.ns.rebuild_start_stop_task( + vdu_id, + vnf_id, + vdu_index, + action_id, + nsr_id_2, + task_index, + target_vim, + extra_dict, + ) + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test__rebuild_start_stop_task__different_vdu_index__target_record_changes( + self, assign_vim + ): + self.ns = Ns() + extra_dict = {} + actions = ["start", "stop", "rebuild"] + vdu_index = "4" + task_index = 0 + for action in actions: + params = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "action": action, } - extra_dict["params"] = { + extra_dict["params"] = params + expected_result = deepcopy(expected_result_rebuild_start_stop) + expected_result[ + "target_record" + ] = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.4.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + expected_result["params"] = params + task = self.ns.rebuild_start_stop_task( + vdu_id, + vnf_id, + vdu_index, + action_id, + nsr_id_2, + task_index, + target_vim, + extra_dict, + ) + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test__rebuild_start_stop_task__different_task_index__task_id_changes( + self, assign_vim + ): + self.ns = Ns() + extra_dict = {} + actions = ["start", "stop", "rebuild"] + vdu_index = "0" + task_index = 3 + for action in actions: + params = { "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", "action": action, } + extra_dict["params"] = params + expected_result = deepcopy(expected_result_rebuild_start_stop) + expected_result[ + "target_record" + ] = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.0.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + expected_result["params"] = params + expected_result["task_id"] = "bb937f49-3870-4169-b758-9732e1ff40f3:3" task = self.ns.rebuild_start_stop_task( vdu_id, vnf_id, vdu_index, action_id, - nsr_id, + nsr_id_2, 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): + def test__rebuild_start_stop_task__assign_vim_raises__task_is_not_created( + self, assign_vim + ): self.ns = Ns() extra_dict = {} + actions = ["start", "stop", "rebuild"] + vdu_index = "0" + task_index = 0 + for action in actions: + params = { + "vim_vm_id": "f37b18ef-3caa-4dc9-ab91-15c669b16396", + "action": action, + } + extra_dict["params"] = params + assign_vim.side_effect = TestException("Can not connect to VIM.") + with self.assertRaises(TestException) as err: + task = self.ns.rebuild_start_stop_task( + vdu_id, + vnf_id, + vdu_index, + action_id, + nsr_id_2, + task_index, + target_vim, + extra_dict, + ) + self.assertEqual(task, None) + self.assertEqual(str(err.exception), "Can not connect to VIM.") + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_verticalscale_task__successful(self, assign_vim): + self.ns = Ns() 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" + task = self.ns.verticalscale_task( + vdu, + vnf, + vdu_index, + action_id, + nsr_id_2, + task_index, + extra_dict_vertical_scale, ) + self.assertDictEqual(task, expected_result_vertical_scale) - 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", - } + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_verticalscale_task__task_index_changes__task_id_changes(self, assign_vim): + self.ns = Ns() + vdu_index = "1" + task_index = 2 + expected_result = deepcopy(expected_result_vertical_scale) + expected_result["task_id"] = "bb937f49-3870-4169-b758-9732e1ff40f3:2" task = self.ns.verticalscale_task( - vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict + vdu, + vnf, + vdu_index, + action_id, + nsr_id_2, + task_index, + extra_dict_vertical_scale, ) - self.assertDictEqual(task, expected_result) @patch("osm_ng_ro.ns.Ns._assign_vim") - def test_migrate_task(self, assign_vim): + def test_verticalscale_task__empty_extra_dict__expected_result_without_params( + 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 = deepcopy(expected_result_vertical_scale) + expected_result.pop("params") + task = self.ns.verticalscale_task( + vdu, vnf, vdu_index, action_id, nsr_id_2, task_index, extra_dict ) + self.assertDictEqual(task, expected_result) - 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", - } + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_verticalscale_task__assign_vim_raises__task_is_not_created( + self, assign_vim + ): + self.ns = Ns() + vdu_index = "1" + task_index = 1 + assign_vim.side_effect = TestException("Can not connect to VIM.") + with self.assertRaises(TestException) as err: + task = self.ns.verticalscale_task( + vdu, + vnf, + vdu_index, + action_id, + nsr_id_2, + task_index, + extra_dict_vertical_scale, + ) + self.assertEqual(task, {}) + self.assertEqual(str(err.exception), "Can not connect to VIM.") + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_migrate_task__successful(self, assign_vim): + self.ns = Ns() + vdu_index = "1" + task_index = 1 task = self.ns.migrate_task( - vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict + vdu, vnf, vdu_index, action_id, nsr_id_2, task_index, extra_dict_migrate ) + self.assertDictEqual(task, expected_result_migrate) + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_migrate_task__empty_extra_dict__task_without_params(self, assign_vim): + self.ns = Ns() + extra_dict = {} + vdu_index = "1" + task_index = 1 + expected_result = deepcopy(expected_result_migrate) + expected_result.pop("params") + task = self.ns.migrate_task( + vdu, vnf, vdu_index, action_id, nsr_id_2, task_index, extra_dict + ) self.assertDictEqual(task, expected_result) + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_migrate_task__different_vdu_index__target_record_with_different_vdu_index( + self, assign_vim + ): + self.ns = Ns() + vdu_index = "4" + task_index = 1 + expected_result = deepcopy(expected_result_migrate) + expected_result[ + "target_record" + ] = "vnfrs:665b4165-ce24-4320-bf19-b9a45bade49f:vdur.4.vim_info.vim:f9f370ac-0d44-41a7-9000-457f2332bc35" + task = self.ns.migrate_task( + vdu, vnf, vdu_index, action_id, nsr_id_2, task_index, extra_dict_migrate + ) + self.assertDictEqual(task, expected_result) + + @patch("osm_ng_ro.ns.Ns._assign_vim") + def test_migrate_task__assign_vim_raises__task_is_not_created(self, assign_vim): + self.ns = Ns() + vdu_index = "1" + task_index = 1 + assign_vim.side_effect = TestException("Can not connect to VIM.") + with self.assertRaises(TestException) as err: + task = self.ns.migrate_task( + vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict_migrate + ) + self.assertDictEqual(task, {}) + self.assertEqual(str(err.exception), "Can not connect to VIM.") + class TestProcessVduParams(unittest.TestCase): def setUp(self): @@ -3381,7 +3397,7 @@ class TestProcessVduParams(unittest.TestCase): persist_root_disk = self.ns.find_persistent_root_volumes( vnfd, target_vdu, vdu_instantiation_volumes_list, disk_list ) - self.assertEqual(persist_root_disk, None) + self.assertEqual(persist_root_disk, {}) mock_volume_keeping_required.assert_not_called() self.assertEqual(disk_list, []) @@ -3650,7 +3666,8 @@ class TestProcessVduParams(unittest.TestCase): self, mock_volume_keeping_required ): """Find persistent ordinary volume, volume id is not persistent_root_disk dict, - vim-volume-id is given as instantiation parameter but disk id is not matching.""" + vim-volume-id is given as instantiation parameter but disk id is not matching. + """ mock_volume_keeping_required.return_value = True vdu_instantiation_volumes_list = [ { @@ -4761,14 +4778,21 @@ class TestProcessVduParams(unittest.TestCase): } persistent_ordinary_disk = {} disk_list = [] + extra_dict = {} expected_disk_list = [ { "size": "10", "keep": False, + "multiattach": False, + "name": "persistent-volume2", } ] self.ns._add_persistent_ordinary_disks_to_disk_list( - target_vdu, persistent_root_disk, persistent_ordinary_disk, disk_list + target_vdu, + persistent_root_disk, + persistent_ordinary_disk, + disk_list, + extra_dict, ) self.assertEqual(disk_list, expected_disk_list) mock_volume_keeping_required.assert_called_once_with(ordinary_disk) @@ -4792,9 +4816,14 @@ class TestProcessVduParams(unittest.TestCase): } persistent_ordinary_disk = {} disk_list = [] + extra_dict = {} self.ns._add_persistent_ordinary_disks_to_disk_list( - target_vdu, persistent_root_disk, persistent_ordinary_disk, disk_list + target_vdu, + persistent_root_disk, + persistent_ordinary_disk, + disk_list, + extra_dict, ) self.assertEqual(disk_list, []) mock_volume_keeping_required.assert_not_called()