From edf98746a52408386efab26143778198b0efd3c5 Mon Sep 17 00:00:00 2001 From: dhebeha Date: Sat, 5 Sep 2020 20:16:48 +0530 Subject: Add support to process NSI selection request Issue-ID: OPTFRA-802 Signed-off-by: dhebeha Signed-off-by: krishnaa96 Change-Id: I85d951061abc697714425bd223b89102d4f2ede9 --- .../slice_selection/test_remote_opt_processor.py | 80 +++++++++++++++++----- 1 file changed, 61 insertions(+), 19 deletions(-) (limited to 'test/apps/slice_selection/test_remote_opt_processor.py') diff --git a/test/apps/slice_selection/test_remote_opt_processor.py b/test/apps/slice_selection/test_remote_opt_processor.py index 5321880..7c2d191 100644 --- a/test/apps/slice_selection/test_remote_opt_processor.py +++ b/test/apps/slice_selection/test_remote_opt_processor.py @@ -20,7 +20,7 @@ import json import unittest from requests import RequestException, Response -from apps.slice_selection.optimizers.conductor.remote_opt_processor import process_nsi_selection_opt +from apps.slice_selection.optimizers.conductor.remote_opt_processor import SliceSelectionOptimizer from osdf.adapters.local_data import local_policies from osdf.utils.interfaces import json_from_file, yaml_from_file from osdf.utils.programming_utils import DotDict @@ -37,31 +37,33 @@ class TestRemoteOptProcessor(unittest.TestCase): "deployment": "config/osdf_config.yaml", "core": "config/common_config.yaml" } + slice_spec = "config/slicing_config.yaml" + self.slice_config = config_loader.load_config_file(slice_spec) self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec)) + self.patcher_RestClient = patch( + 'osdf.utils.interfaces.RestClient.request', return_value=MagicMock()) + self.mock_rc = self.patcher_RestClient.start() def tearDown(self): patch.stopall() def test_process_nsi_selection_opt(self): main_dir = "" - request_file = main_dir + 'test/apps/slice_selection/nsi_request.json' + request_file = main_dir + 'test/apps/slice_selection/nsi_selection_request.json' not_shared_request_file = main_dir + 'test/apps/slice_selection/not_shared_nsi_request.json' #response files new_solution_response_file = main_dir + 'test/apps/slice_selection/new_solution_nsi_response.json' shared_solution_response_file = main_dir + 'test/apps/slice_selection/shared_solution_nsi_response.json' no_solution_response_file = main_dir + 'test/apps/slice_selection/no_recomm_nsi_response.json' - not_shared_response_file = main_dir + 'test/apps/slice_selection/not_shared_nsi_response.json' error_response_file = main_dir + 'test/apps/slice_selection/nsi_error_response.json' - not_shared_request_json = json_from_file(not_shared_request_file) - not_shared_response_json = json_from_file(not_shared_response_file) request_json = json_from_file(request_file) new_solution_response_json = json_from_file(new_solution_response_file) shared_solution_response_json = json_from_file(shared_solution_response_file) no_solution_response_json = json_from_file(no_solution_response_file) error_response_json = json_from_file(error_response_file) - policies_path = main_dir + 'test/policy-local-files' + policies_path = main_dir + 'test/policy-local-files/slice-selection-files' slice_policies_file = main_dir + 'test/apps/slice_selection/slice_policies.txt' valid_policies_files = local_policies.get_policy_names_from_file(slice_policies_file) @@ -69,36 +71,41 @@ class TestRemoteOptProcessor(unittest.TestCase): self.patcher_get_policies = patch('osdf.adapters.policy.interface.remote_api', return_value=policies) self.Mock_get_policies = self.patcher_get_policies.start() + # new solution new_solution_conductor_response_file = 'test/apps/slice_selection/new_solution_conductor_response.json' new_solution_conductor_response = json_from_file(new_solution_conductor_response_file) self.patcher_req = patch('osdf.adapters.conductor.conductor.request', return_value=new_solution_conductor_response) self.Mock_req = self.patcher_req.start() - self.assertEquals(new_solution_response_json, process_nsi_selection_opt(request_json, self.osdf_config)) + slice_select_opt = SliceSelectionOptimizer(self.osdf_config, self.slice_config, request_json, 'NSI') + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=new_solution_response_json, noresponse=True) self.patcher_req.stop() + # shared solution + request_json['preferReuse'] = True shared_solution_conductor_response_file = 'test/apps/slice_selection/shared_solution_conductor_response.json' shared_solution_conductor_response = json_from_file(shared_solution_conductor_response_file) self.patcher_req = patch('osdf.adapters.conductor.conductor.request', return_value=shared_solution_conductor_response) self.Mock_req = self.patcher_req.start() - self.assertEquals(shared_solution_response_json, - process_nsi_selection_opt(request_json, self.osdf_config)) + slice_select_opt = SliceSelectionOptimizer(self.osdf_config, self.slice_config, request_json, 'NSI') + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=shared_solution_response_json, noresponse=True) self.patcher_req.stop() - # not-shared solution - self.assertEquals(not_shared_response_json, - process_nsi_selection_opt(not_shared_request_json, self.osdf_config)) + # no recommendation no_solution_conductor_response_file = 'test/apps/slice_selection/no_rec.json' no_solution_conductor_response = json_from_file(no_solution_conductor_response_file) self.patcher_req = patch('osdf.adapters.conductor.conductor.request', return_value=no_solution_conductor_response) self.Mock_req = self.patcher_req.start() - self.assertEquals(no_solution_response_json, - process_nsi_selection_opt(request_json, self.osdf_config)) + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=no_solution_response_json, noresponse=True) self.patcher_req.stop() + # Exception conductor_error_response_file = 'test/apps/slice_selection/conductor_error_response.json' conductor_error_response = json_from_file(conductor_error_response_file) @@ -107,17 +114,52 @@ class TestRemoteOptProcessor(unittest.TestCase): self.patcher_req = patch('osdf.adapters.conductor.conductor.request', side_effect=RequestException(response=response)) self.Mock_req = self.patcher_req.start() - self.assertEquals(error_response_json, process_nsi_selection_opt(request_json, self.osdf_config)) + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=error_response_json, noresponse=True) + self.patcher_req.stop() + + self.patcher_req = patch('osdf.adapters.conductor.conductor.request', + side_effect=Exception("Some error message")) + self.Mock_req = self.patcher_req.start() + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=error_response_json, noresponse=True) self.patcher_req.stop() + def test_process_nssi_selection_opt(self): + main_dir = "" + request_file = main_dir + 'test/apps/slice_selection/nssi_selection_request.json' + # response files + shared_solution_response_file = main_dir + 'test/apps/slice_selection/shared_solution_nssi_response.json' + error_response_file = main_dir + 'test/apps/slice_selection/nssi_error_response.json' + + request_json = json_from_file(request_file) + shared_solution_response_json = json_from_file(shared_solution_response_file) + error_response_json = json_from_file(error_response_file) + + policies_path = main_dir + 'test/policy-local-files/slice-selection-files' + slice_policies_file = main_dir + 'test/apps/slice_selection/subnet_policies.txt' + + valid_policies_files = local_policies.get_policy_names_from_file(slice_policies_file) + policies = [json_from_file(policies_path + '/' + name) for name in valid_policies_files] + self.patcher_get_policies = patch('osdf.adapters.policy.interface.remote_api', + return_value=policies) + self.Mock_get_policies = self.patcher_get_policies.start() + + shared_solution_conductor_response_file = 'test/apps/slice_selection/nssi_conductor_response.json' + shared_solution_conductor_response = json_from_file(shared_solution_conductor_response_file) self.patcher_req = patch('osdf.adapters.conductor.conductor.request', - side_effect=Exception("test_exception")) + return_value=shared_solution_conductor_response) self.Mock_req = self.patcher_req.start() - self.assertEquals('test_exception', - process_nsi_selection_opt(request_json, self.osdf_config).get('statusMessage')) + slice_select_opt = SliceSelectionOptimizer(self.osdf_config, self.slice_config, request_json, 'NSSI') + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=shared_solution_response_json, noresponse=True) self.patcher_req.stop() + request_json['sliceProfile']['resourceSharingLevel'] = "not-shared" + slice_select_opt = SliceSelectionOptimizer(self.osdf_config, self.slice_config, request_json, 'NSSI') + slice_select_opt.process_slice_selection_opt() + self.mock_rc.assert_called_with(json=error_response_json, noresponse=True) + if __name__ == "__main__": unittest.main() - -- cgit 1.2.3-korg