Fixed TD policies
[optf/osdf.git] / test / test_PolicyCalls.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2017-2018 AT&T Intellectual Property
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # -------------------------------------------------------------------------
17 #
18 import json
19 import unittest
20
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
25 import yaml
26 from mock import patch
27 from osdf.optimizers.placementopt.conductor import translation
28 from osdf.operation.exceptions import BusinessException
29
30
31 class TestPolicyCalls(unittest.TestCase):
32
33     def setUp(self):
34         main_dir = ""
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"
38
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)
41
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]
44
45     def tearDown(self):
46         pass
47
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
53
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)
60
61     def test_policy_api_call2(self):
62         req_json, policy_response = self.get_req_resp("test/placement-tests/request.json",
63                                                       "test/placement-tests/policy_response2.json")
64         with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
65             policy_list = interface.remote_api(req_json, osdf_config, service_type="placement")
66             policy_type = [policy['content']['policyType'] for policy in policy_list]
67             #self.assertEqual(set(policy_type), {'hpaPolicy', 'SubscriberPolicy'})
68
69     def failure_policy_call(self, req_json_file, resp_json_file):
70         req_json, policy_response = self.get_req_resp(req_json_file, resp_json_file)
71         with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
72             self.assertRaises(BusinessException,
73                               lambda: interface.remote_api(req_json, osdf_config, service_type="placement"))
74
75     # def test_policy_api_call_failed_multi(self):
76     #     prefix = "./test/placement-tests"
77     #     fail_cases = [("request_error1.json", "policy_response.json"),
78     #                   ("request.json", "policy_response_error1.json"),
79     #                   ("request.json", "policy_response_error2.json")]
80     #     for req, resp in fail_cases:
81     #         self.failure_policy_call(prefix + "/" + req, prefix + "/" + resp)
82
83     def test_get_by_scope(self):
84         req_json_file = "./test/placement-tests/testScoperequest.json"
85         all_policies = "./test/placement-tests/policy_response.json"
86         req_json_obj = json.loads(open(req_json_file).read())
87         req_json_obj2 = json.loads(open(all_policies).read())
88         yaml_file = "./test/placement-tests/test_by_scope.yaml"
89
90         with open(yaml_file) as yaml_file2:
91             policy_config_file = yaml.load(yaml_file2)
92             with patch('osdf.utils.interfaces.RestClient.request', return_value=req_json_obj2):
93                 policies_list = interface.get_by_scope(RestClient, req_json_obj, policy_config_file, 'placement')
94                 self.assertTrue(policies_list, 'is null')
95                 self.assertRaises(Exception)
96
97     def test_gen_demands(self):
98         actions_list, gen_demands_list = [], []
99         req_json = "./test/placement-tests/request.json"
100         req_json = json.loads(open(req_json).read())
101         # need to run this only on vnf policies
102         vnf_policies = [x for x in self.policies if x["content"]["policyType"] == "vnfPolicy"]
103         gen_demands = translation.gen_demands(req_json, vnf_policies)
104         for action in req_json['placementInfo']['placementDemands']:
105             actions_list.append(action['resourceModuleName'])
106         for key2,value in gen_demands.items():
107             gen_demands_list.append(key2)
108         self.assertListEqual(gen_demands_list, actions_list, 'generated demands are not equal to the passed input'
109                                                         '[placementDemand][resourceModuleName] list')
110
111     def test_local_policy_location(self):
112         req_json = json_from_file("./test/placement-tests/request.json")
113         return interface.local_policies_location(req_json, osdf_config, service_type="placement")
114
115
116 if __name__ == '__main__':
117     unittest.main()