1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# -------------------------------------------------------------------------
# Copyright (C) 2020 Wipro Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -------------------------------------------------------------------------
#
import json
import unittest
from requests import RequestException
from apps.slice_selection.optimizers.conductor.remote_opt_processor import process_nsi_selection_opt
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
import osdf.config.loader as config_loader
from mock import patch, MagicMock
import json
from osdf.logging.osdf_logging import error_log, debug_log
from osdf.adapters.policy.interface import get_policies
class TestRemoteOptProcessor(unittest.TestCase):
def setUp(self):
self.config_spec = {
"deployment": "config/osdf_config.yaml",
"core": "config/common_config.yaml"
}
self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec))
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'
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'
error_response_file = main_dir + 'test/apps/slice_selection/nsi_error_response.json'
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)
error_response_json = json_from_file(error_response_file)
policies_path = main_dir + 'test/policy-local-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)
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()
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))
self.patcher_req.stop()
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))
self.patcher_req.stop()
conductor_error_response_file = 'test/apps/slice_selection/conductor_error_response.json'
conductor_error_response = json_from_file(conductor_error_response_file)
self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
side_effect=RequestException(response=json.dumps(conductor_error_response)))
self.Mock_req = self.patcher_req.start()
self.assertEquals(error_response_json, process_nsi_selection_opt(request_json, self.osdf_config))
self.patcher_req.stop()
self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
side_effect=Exception("test_exception"))
self.Mock_req = self.patcher_req.start()
self.assertEquals('test_exception',
process_nsi_selection_opt(request_json, self.osdf_config).get('statusMessage'))
self.patcher_req.stop()
if __name__ == "__main__":
unittest.main()
|