1 # -------------------------------------------------------------------------
2 # Copyright (c) 2017-2018 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
21 from osdf.adapters.local_data import local_policies
22 from osdf.config.base import osdf_config
23 from osdf.adapters.policy import interface
24 from osdf.utils.interfaces import RestClient, json_from_file
26 from mock import patch
27 from osdf.optimizers.placementopt.conductor import translation
28 from osdf.operation.exceptions import BusinessException
31 class TestPolicyCalls(unittest.TestCase):
35 parameter_data_file = main_dir + "test/placement-tests/request.json"
36 policy_data_path = main_dir + "test/policy-local-files/"
37 local_config_file = main_dir + "config/common_config.yaml"
39 valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
40 valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
42 self.request_json = json_from_file(parameter_data_file)
43 self.policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
48 def get_req_resp(self, req_file, resp_file):
49 """Get request/response from files"""
50 req_json = json_from_file(req_file)
51 resp_json = json_from_file(resp_file)
52 return req_json, resp_json
54 def test_policy_api_call(self):
55 req_json, policy_response = self.get_req_resp("./test/placement-tests/request.json",
56 "./test/placement-tests/policy_response.json")
57 with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
58 policy_list = interface.remote_api(req_json, osdf_config, service_type="placement")
59 self.assertIsNotNone(policy_list)
61 def failure_policy_call(self, req_json_file, resp_json_file):
62 req_json, policy_response = self.get_req_resp(req_json_file, resp_json_file)
63 with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
64 self.assertRaises(BusinessException,
65 lambda: interface.remote_api(req_json, osdf_config, service_type="placement"))
67 def test_policy_api_call_failed_multi(self):
68 prefix = "./test/placement-tests"
69 fail_cases = [("request_error1.json", "policy_response.json"),
70 ("request.json", "policy_response_error1.json"),
71 ("request.json", "policy_response_error2.json")]
72 for req, resp in fail_cases:
73 self.failure_policy_call(prefix + "/" + req, prefix + "/" + resp)
75 def test_get_by_scope(self):
76 req_json_file = "./test/placement-tests/testScoperequest.json"
77 all_policies = "./test/placement-tests/policy_response.json"
78 req_json_obj = json.loads(open(req_json_file).read())
79 req_json_obj2 = json.loads(open(all_policies).read())
80 yaml_file = "./test/placement-tests/test_by_scope.yaml"
82 with open(yaml_file) as yaml_file2:
83 policy_config_file = yaml.load(yaml_file2)
84 with patch('osdf.utils.interfaces.RestClient.request', return_value=req_json_obj2):
85 policies_list = interface.get_by_scope(RestClient, req_json_obj, policy_config_file, 'placement')
86 self.assertTrue(policies_list, 'is null')
87 self.assertRaises(Exception)
89 def test_gen_demands(self):
92 req_json = "./test/placement-tests/request.json"
93 req_json = json.loads(open(req_json).read())
94 # need to run this only on vnf policies
95 vnf_policies = [x for x in self.policies if x["content"]["policyType"] == "vnfPolicy"]
96 genDemands = translation.gen_demands(req_json, vnf_policies)
97 for action in req_json['placementInfo']['placementDemands']:
98 actionsList.append(action['resourceModuleName'])
99 for key2,value in genDemands.items():
100 genDemandslist.append(key2)
101 self.assertListEqual(genDemandslist, actionsList, 'generated demands are not equal to the passed input'
102 '[placementDemand][resourceModuleName] list')
104 def test_local_policy_location(self):
105 req_json = json_from_file("./test/placement-tests/request.json")
106 return interface.local_policies_location(req_json, osdf_config, service_type="placement")
109 if __name__ == '__main__':