|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import mock |
| 18 | + |
| 19 | +from compatibility_lib import compatibility_checker |
| 20 | + |
| 21 | + |
| 22 | +class TestCompatibilityChecker(unittest.TestCase): |
| 23 | + |
| 24 | + def test_check(self): |
| 25 | + import json |
| 26 | + |
| 27 | + checker = compatibility_checker.CompatibilityChecker() |
| 28 | + |
| 29 | + packages = 'test_pkg' |
| 30 | + python_version = 3 |
| 31 | + |
| 32 | + data = { |
| 33 | + 'python-version': python_version, |
| 34 | + 'packages': packages |
| 35 | + } |
| 36 | + |
| 37 | + request = mock.Mock() |
| 38 | + |
| 39 | + mock_request = mock.Mock() |
| 40 | + mock_request.Request.return_value = request |
| 41 | + |
| 42 | + urlopen_res = mock.Mock() |
| 43 | + mock_request.urlopen.return_value = urlopen_res |
| 44 | + json_mock = mock.Mock() |
| 45 | + json_mock.read.return_value = b'{}' |
| 46 | + urlopen_res.__enter__ = mock.Mock(return_value=json_mock) |
| 47 | + urlopen_res.__exit__ = mock.Mock(return_value=None) |
| 48 | + |
| 49 | + patch_request = mock.patch( |
| 50 | + 'compatibility_lib.compatibility_checker.urllib.request', |
| 51 | + mock_request) |
| 52 | + |
| 53 | + with patch_request: |
| 54 | + checker.check(packages, python_version) |
| 55 | + |
| 56 | + mock_request.Request.assert_called_with( |
| 57 | + compatibility_checker.SERVER_URL, json.dumps(data).encode('utf-8')) |
| 58 | + mock_request.urlopen.assert_called_with(request) |
| 59 | + |
| 60 | + def _mock_retrying_check(self, *args): |
| 61 | + packages = args[0][0] |
| 62 | + python_version = args[0][1] |
| 63 | + return (packages, python_version, 'SUCCESS') |
| 64 | + |
| 65 | + def test_get_self_compatibility(self): |
| 66 | + checker = compatibility_checker.CompatibilityChecker() |
| 67 | + |
| 68 | + pkg_list = ['pkg1', 'pkg2'] |
| 69 | + python_version = 3 |
| 70 | + |
| 71 | + mock_config = mock.Mock() |
| 72 | + mock_config.PKG_LIST = pkg_list |
| 73 | + patch_config = mock.patch( |
| 74 | + 'compatibility_lib.compatibility_checker.configs', mock_config) |
| 75 | + |
| 76 | + patch_executor = mock.patch( |
| 77 | + 'compatibility_lib.compatibility_checker.concurrent.futures.ThreadPoolExecutor', |
| 78 | + FakeExecutor) |
| 79 | + patch_retrying_check = mock.patch.object( |
| 80 | + compatibility_checker.CompatibilityChecker, |
| 81 | + 'retrying_check', |
| 82 | + self._mock_retrying_check) |
| 83 | + |
| 84 | + res = [] |
| 85 | + with patch_config, patch_executor, patch_retrying_check: |
| 86 | + result = checker.get_self_compatibility(python_version) |
| 87 | + |
| 88 | + for item in result: |
| 89 | + res.append(item) |
| 90 | + |
| 91 | + self.assertEqual(res, |
| 92 | + [((['pkg1'], 3, 'SUCCESS'),), |
| 93 | + ((['pkg2'], 3, 'SUCCESS'),)]) |
| 94 | + |
| 95 | + def test_get_pairwise_compatibility(self): |
| 96 | + pkg_list = ['pkg1', 'pkg2', 'pkg3'] |
| 97 | + python_version = 3 |
| 98 | + |
| 99 | + mock_config = mock.Mock() |
| 100 | + mock_config.PKG_LIST = pkg_list |
| 101 | + patch_config = mock.patch( |
| 102 | + 'compatibility_lib.compatibility_checker.configs', mock_config) |
| 103 | + |
| 104 | + patch_executor = mock.patch( |
| 105 | + 'compatibility_lib.compatibility_checker.concurrent.futures.ThreadPoolExecutor', |
| 106 | + FakeExecutor) |
| 107 | + patch_retrying_check = mock.patch.object( |
| 108 | + compatibility_checker.CompatibilityChecker, |
| 109 | + 'retrying_check', |
| 110 | + self._mock_retrying_check) |
| 111 | + |
| 112 | + res = [] |
| 113 | + with patch_config, patch_executor, patch_retrying_check: |
| 114 | + checker = compatibility_checker.CompatibilityChecker() |
| 115 | + result = checker.get_pairwise_compatibility(python_version) |
| 116 | + |
| 117 | + for item in result: |
| 118 | + res.append(item) |
| 119 | + |
| 120 | + self.assertEqual(res, |
| 121 | + [((['pkg1', 'pkg2'], 3, 'SUCCESS'),), |
| 122 | + ((['pkg1', 'pkg3'], 3, 'SUCCESS'),), |
| 123 | + ((['pkg2', 'pkg3'], 3, 'SUCCESS'),)]) |
| 124 | + |
| 125 | + |
| 126 | +class FakeExecutor(object): |
| 127 | + def __init__(self, max_workers=10): |
| 128 | + self.max_workers = max_workers |
| 129 | + |
| 130 | + def map(self, check_func, pkgs): |
| 131 | + results = [] |
| 132 | + |
| 133 | + for pkg in pkgs: |
| 134 | + results.append(check_func(pkg)) |
| 135 | + |
| 136 | + return results |
| 137 | + |
| 138 | + def __enter__(self): |
| 139 | + return self |
| 140 | + |
| 141 | + def __exit__(self, exception_type, exception_value, traceback): |
| 142 | + return None |
0 commit comments