1 # -------------------------------------------------------------------------
2 # Copyright (c) 2017-2018 AT&T Intellectual Property
3 # Copyright (C) 2020 Wipro Limited.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
22 from osdf.adapters.local_data import local_policies
23 from osdf.config.base import osdf_config
24 from osdf.adapters.policy import interface
25 from osdf.utils.interfaces import RestClient, json_from_file
27 from mock import patch
28 from osdf.adapters.conductor import translation
29 from osdf.operation.exceptions import BusinessException
32 class TestPolicyCalls(unittest.TestCase):
36 parameter_data_file = main_dir + "test/placement-tests/request.json"
37 policy_data_path = main_dir + "test/policy-local-files/"
38 local_config_file = main_dir + "config/common_config.yaml"
40 valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
41 valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
43 self.request_json = json_from_file(parameter_data_file)
44 self.policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
49 def get_req_resp(self, req_file, resp_file):
50 """Get request/response from files"""
51 req_json = json_from_file(req_file)
52 resp_json = json_from_file(resp_file)
53 return req_json, resp_json
55 def test_policy_api_call(self):
56 req_json, policy_response = self.get_req_resp("test/placement-tests/request.json",
57 "test/placement-tests/policy_response.json")
58 with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
59 policy_list = interface.remote_api(req_json, osdf_config, service_type="placement")
60 self.assertIsNotNone(policy_list)
62 def test_policy_api_call2(self):
63 req_json, policy_response = self.get_req_resp("test/placement-tests/request.json",
64 "test/placement-tests/policy_response2.json")
65 with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
66 policy_list = interface.remote_api(req_json, osdf_config, service_type="placement")
67 policy_type = [policy[list(policy.keys())[0]]['type'] for policy in policy_list]
68 #self.assertEqual(set(policy_type), {'hpaPolicy', 'SubscriberPolicy'})
70 def failure_policy_call(self, req_json_file, resp_json_file):
71 req_json, policy_response = self.get_req_resp(req_json_file, resp_json_file)
72 with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
73 self.assertRaises(BusinessException,
74 lambda: interface.remote_api(req_json, osdf_config, service_type="placement"))
76 # def test_policy_api_call_failed_multi(self):
77 # prefix = "./test/placement-tests"
78 # fail_cases = [("request_error1.json", "policy_response.json"),
79 # ("request.json", "policy_response_error1.json"),
80 # ("request.json", "policy_response_error2.json")]
81 # for req, resp in fail_cases:
82 # self.failure_policy_call(prefix + "/" + req, prefix + "/" + resp)
84 def test_get_by_scope(self):
85 req_json_file = "./test/placement-tests/testScoperequest.json"
86 all_policies = "./test/placement-tests/policy_response.json"
87 req_json_obj = json.loads(open(req_json_file).read())
88 req_json_obj2 = json.loads(open(all_policies).read())
89 yaml_file = "./test/placement-tests/test_by_scope.yaml"
91 with open(yaml_file) as yaml_file2:
92 policy_config_file = yaml.load(yaml_file2)
93 with patch('osdf.utils.interfaces.RestClient.request', return_value=req_json_obj2):
94 policies_list = interface.get_by_scope(RestClient, req_json_obj, policy_config_file, 'placement')
95 self.assertFalse(policies_list)
96 self.assertRaises(Exception)
98 def test_gen_demands(self):
99 actions_list, gen_demands_list = [], []
100 req_json = "./test/placement-tests/request.json"
101 req_json = json.loads(open(req_json).read())
102 # need to run this only on vnf policies
103 vnf_policies = [x for x in self.policies if x[list(x.keys())[0]]["type"] ==
104 "onap.policies.optimization.resource.VnfPolicy"]
105 gen_demands = translation.gen_demands(req_json['placementInfo']['placementDemands'], vnf_policies)
107 for action in req_json['placementInfo']['placementDemands']:
108 actions_list.append(action['resourceModuleName'])
109 for key2,value in gen_demands.items():
110 gen_demands_list.append(key2)
111 self.assertListEqual(gen_demands_list, actions_list, 'generated demands are not equal to the passed input'
112 '[placementDemand][resourceModuleName] list')
114 def test_local_policy_location(self):
115 req_json = json_from_file("./test/placement-tests/request.json")
116 return interface.local_policies_location(req_json, osdf_config, service_type="placement")
119 if __name__ == '__main__':