osdf rearchitecture into apps and libs
[optf/osdf.git] / test / test_process_placement_opt.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 mock
19 import unittest
20
21 from flask import Response
22 from mock import patch
23
24 from apps.placement.optimizers.conductor.remote_opt_processor import process_placement_opt
25 from osdf.adapters.local_data import local_policies
26 from osdf.utils.interfaces import json_from_file, yaml_from_file
27
28
29 class TestProcessPlacementOpt(unittest.TestCase):
30
31     def setUp(self):
32         mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8')
33         self.patcher_req = patch('apps.placement.optimizers.conductor.conductor.request',
34                                  return_value={"solutionInfo": {"placementInfo": "dummy"}})
35         self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept',
36                                         return_value=mock_req_accept_message)
37         self.patcher_callback = patch(
38             'apps.placement.optimizers.conductor.remote_opt_processor.process_placement_opt',
39             return_value=mock_req_accept_message)
40         self.patcher_RestClient = patch(
41             'osdf.utils.interfaces.RestClient', return_value=mock.MagicMock())
42         self.Mock_req = self.patcher_req.start()
43         self.Mock_req_accept = self.patcher_req_accept.start()
44         self.Mock_callback = self.patcher_callback.start()
45         self.Mock_RestClient = self.patcher_RestClient.start()
46
47     def tearDown(self):
48         patch.stopall()
49
50     def test_process_placement_opt(self):
51         main_dir = ""
52         conductor_api_template = main_dir + "osdf/templates/conductor_interface.json"
53         parameter_data_file = main_dir + "test/placement-tests/request.json"
54         policy_data_path = main_dir + "test/policy-local-files/"
55         local_config_file = main_dir + "config/common_config.yaml"
56
57         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
58         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
59
60         request_json = json_from_file(parameter_data_file)
61         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
62         local_config = yaml_from_file(local_config_file)
63         templ_string = process_placement_opt(request_json, policies, local_config)
64
65
66     def test_process_placement_opt_placementDemand(self):
67         main_dir = ""
68         parameter_data_file = main_dir + "test/placement-tests/request_placement.json"
69         policy_data_path = main_dir + "test/policy-local-files/"
70         local_config_file = main_dir + "config/common_config.yaml"
71
72         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
73         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
74
75         request_json = json_from_file(parameter_data_file)
76         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
77         local_config = yaml_from_file(local_config_file)
78         templ_string = process_placement_opt(request_json, policies, local_config)        
79
80     def test_process_placement_opt_placementDemand_vfmodule(self):
81         main_dir = ""
82         parameter_data_file = main_dir + "test/placement-tests/request_vfmod.json"
83         policy_data_path = main_dir + "test/policy-local-files/"
84         local_config_file = main_dir + "config/common_config.yaml"
85
86         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
87         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
88
89         request_json = json_from_file(parameter_data_file)
90         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
91         local_config = yaml_from_file(local_config_file)
92         templ_string = process_placement_opt(request_json, policies, local_config)
93
94
95 if __name__ == "__main__":
96     unittest.main()
97