update link to upper-constraints.txt
[optf/osdf.git] / test / test_process_placement_opt.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2017-2018 AT&T Intellectual Property
3 #   Copyright (C) 2020 Wipro Limited.
4 #
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
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17 # -------------------------------------------------------------------------
18 #
19 import mock
20 import unittest
21
22 from flask import Response
23 from mock import patch
24
25 from apps.placement.optimizers.conductor.remote_opt_processor import process_placement_opt
26 from osdf.adapters.local_data import local_policies
27 from osdf.utils.interfaces import json_from_file, yaml_from_file
28
29
30 class TestProcessPlacementOpt(unittest.TestCase):
31
32     def setUp(self):
33         mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8')
34         conductor_response_file = 'test/placement-tests/conductor_response.json'
35         conductor_response = json_from_file(conductor_response_file)
36         self.patcher_req = patch('osdf.adapters.conductor.conductor.request',
37                                  return_value=conductor_response)
38         self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept',
39                                         return_value=mock_req_accept_message)
40         self.patcher_callback = patch(
41             'apps.placement.optimizers.conductor.remote_opt_processor.process_placement_opt',
42             return_value=mock_req_accept_message)
43         self.patcher_RestClient = patch(
44             'osdf.utils.interfaces.RestClient', return_value=mock.MagicMock())
45         self.Mock_req = self.patcher_req.start()
46         self.Mock_req_accept = self.patcher_req_accept.start()
47         self.Mock_callback = self.patcher_callback.start()
48         self.Mock_RestClient = self.patcher_RestClient.start()
49
50     def tearDown(self):
51         patch.stopall()
52
53     def test_process_placement_opt(self):
54         main_dir = ""
55         conductor_api_template = main_dir + "osdf/templates/conductor_interface.json"
56         parameter_data_file = main_dir + "test/placement-tests/request.json"
57         policy_data_path = main_dir + "test/policy-local-files/"
58         local_config_file = main_dir + "config/common_config.yaml"
59
60         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
61         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
62
63         request_json = json_from_file(parameter_data_file)
64         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
65         local_config = yaml_from_file(local_config_file)
66         templ_string = process_placement_opt(request_json, policies, local_config)
67
68
69     def test_process_placement_opt_placementDemand(self):
70         main_dir = ""
71         parameter_data_file = main_dir + "test/placement-tests/request_placement.json"
72         policy_data_path = main_dir + "test/policy-local-files/"
73         local_config_file = main_dir + "config/common_config.yaml"
74
75         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
76         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
77
78         request_json = json_from_file(parameter_data_file)
79         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
80         local_config = yaml_from_file(local_config_file)
81         templ_string = process_placement_opt(request_json, policies, local_config)        
82
83     def test_process_placement_opt_placementDemand_vfmodule(self):
84         main_dir = ""
85         parameter_data_file = main_dir + "test/placement-tests/request_vfmod.json"
86         policy_data_path = main_dir + "test/policy-local-files/"
87         local_config_file = main_dir + "config/common_config.yaml"
88
89         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
90         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
91
92         request_json = json_from_file(parameter_data_file)
93         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
94         local_config = yaml_from_file(local_config_file)
95         templ_string = process_placement_opt(request_json, policies, local_config)
96
97
98 if __name__ == "__main__":
99     unittest.main()
100