update link to upper-constraints.txt
[optf/osdf.git] / test / test_process_pci_anr_opt.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 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 unittest
19
20 import mock
21 from flask import Response
22 from mock import patch
23
24 import osdf.config.loader as config_loader
25 from apps.pci.optimizers.pci_opt_processor import process_pci_optimation
26 from osdf.adapters.local_data import local_policies
27 from osdf.utils.interfaces import json_from_file
28 from osdf.utils.programming_utils import DotDict
29
30
31 class TestProcessPlacementOpt(unittest.TestCase):
32
33     def setUp(self):
34         mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8')
35         self.patcher_req = patch('apps.pci.optimizers.config_request.request',
36                                  return_value={"solutionInfo": {"placementInfo": "dummy"}})
37         self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept',
38                                         return_value=mock_req_accept_message)
39         self.patcher_callback = patch(
40             'apps.pci.optimizers.pci_opt_processor.process_pci_optimation',
41             return_value=mock_req_accept_message)
42
43         mock_mzn_response = [{'pci': {0: 0, 1: 1, 2: 2, 3: 3, 4: 0}, 'used_ignorables': [0]}]
44
45         self.patcher_minizinc_callback = patch(
46             'apps.pci.optimizers.solver.optimizer.solve',
47             return_value=mock_mzn_response)
48         self.patcher_RestClient = patch(
49             'osdf.utils.interfaces.RestClient', return_value=mock.MagicMock())
50         self.Mock_req = self.patcher_req.start()
51         self.Mock_req_accept = self.patcher_req_accept.start()
52         self.Mock_callback = self.patcher_callback.start()
53         self.Mock_RestClient = self.patcher_RestClient.start()
54         self.Mock_mzn_callback = self.patcher_minizinc_callback.start()
55
56     def tearDown(self):
57         patch.stopall()
58
59     def test_process_pci_anr_opt_solutions(self):
60         main_dir = ""
61         parameter_data_file = main_dir + "test/pci-optimization-tests/pci_anr_request.json"
62         policy_data_path = main_dir + "test/policy-local-files/"
63         self.config_spec = {
64             "deployment": "test/functest/simulators/simulated-config/osdf_config.yaml",
65             "core": "test/functest/simulators/simulated-config/common_config.yaml"
66         }
67         self.osdf_config = DotDict(config_loader.all_configs(**self.config_spec))
68
69         valid_policies_list_file = policy_data_path + '/' + 'meta-valid-policies.txt'
70         valid_policies_files = local_policies.get_policy_names_from_file(valid_policies_list_file)
71
72         request_json = json_from_file(parameter_data_file)
73         policies = [json_from_file(policy_data_path + '/' + name) for name in valid_policies_files]
74
75         templ_string = process_pci_optimation(request_json, self.osdf_config, policies)
76
77
78 if __name__ == "__main__":
79     unittest.main()