Updates to address new HPA 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 test_policy_api_call(self):
49         req_json_file = "./test/placement-tests/request.json"
50         req_json = json.loads(open(req_json_file).read())
51         policy_response_file = "./test/placement-tests/policy_response.json"
52         policy_response = json.loads(open(policy_response_file).read())
53         with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
54             policy_list = interface.remote_api(req_json, osdf_config, service_type="placement")
55             self.assertIsNotNone(policy_list)
56
57     def test_policy_api_call_failed_1(self):
58         req_json_file = "./test/placement-tests/request_error1.json"
59         req_json = json.loads(open(req_json_file).read())
60         policy_response_file = "./test/placement-tests/policy_response.json"
61         policy_response = json.loads(open(policy_response_file).read())
62         with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
63             self.assertRaises(BusinessException,
64                               lambda: interface.remote_api(req_json, osdf_config, service_type="placement"))
65
66     def test_policy_api_call_failed_2(self):
67         req_json_file = "./test/placement-tests/request.json"
68         req_json = json.loads(open(req_json_file).read())
69         policy_response_file = "./test/placement-tests/policy_response_error1.json"
70         policy_response = json.loads(open(policy_response_file).read())
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_3(self):
76         req_json_file = "./test/placement-tests/request.json"
77         req_json = json.loads(open(req_json_file).read())
78         policy_response_file = "./test/placement-tests/policy_response_error2.json"
79         policy_response = json.loads(open(policy_response_file).read())
80         with patch('osdf.adapters.policy.interface.policy_api_call', return_value=policy_response):
81             self.assertRaises(BusinessException,
82                               lambda: interface.remote_api(req_json, osdf_config, service_type="placement"))
83
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"
90
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.assertTrue(policies_list, 'is null')
96                 self.assertRaises(Exception)
97
98     def test_gen_demands(self):
99         actionsList = []
100         genDemandslist = []
101         req_json = "./test/placement-tests/request.json"
102         req_json = json.loads(open(req_json).read())
103         # need to run this only on vnf policies
104         vnf_policies = [x for x in self.policies if x["content"]["policyType"] == "vnfPolicy"]
105         genDemands = translation.gen_demands(req_json, vnf_policies)
106         for action in req_json['placementInfo']['placementDemands']:
107             actionsList.append(action['resourceModuleName'])
108         for key2,value in genDemands.items():
109             genDemandslist.append(key2)
110         self.assertListEqual(genDemandslist, actionsList, 'generated demands are not equal to the passed input'
111                                                           '[placementDemand][resourceModuleName] list')
112            
113 if __name__ == '__main__':
114     unittest.main()