-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathrequirements_test.py
160 lines (130 loc) · 4.66 KB
/
requirements_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2016 PerfKitBenchmarker Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for perfkitbenchmarker.requirements."""
from collections import deque
import contextlib
import unittest
import mock
from perfkitbenchmarker import errors
from perfkitbenchmarker import requirements
import pkg_resources
import six
_PATH = 'dir/file'
class _MockOpenRequirementsFile:
def __init__(self, *args):
self._io = deque(six.StringIO(a) for a in args)
def __enter__(self):
return self._io.popleft()
def __exit__(self, *unused_args, **unused_kwargs):
pass
class CheckRequirementsTestCase(unittest.TestCase):
@contextlib.contextmanager
def _MockOpen(self, *args):
mocked_file = _MockOpenRequirementsFile(*args)
with mock.patch.object(requirements, 'open', return_value=mocked_file) as m:
yield m
def testFulfilledRequirement(self):
requirements_content = """
# Comment line, blank line, and a fulfilled requirement.
absl-py
"""
with self._MockOpen(requirements_content) as mocked_open:
requirements._CheckRequirements(_PATH)
mocked_open.assert_called_once_with('dir/file')
def testMissingPackage(self):
requirements_content = """
# This is not a real package.
perfkitbenchmarker-fake-package>=1.2
"""
with self._MockOpen(requirements_content) as mocked_open:
with self.assertRaises(errors.Setup.PythonPackageRequirementUnfulfilled):
requirements._CheckRequirements(_PATH)
mocked_open.assert_called_once_with('dir/file')
def testInstalledVersionLowerThanRequirement(self):
requirements_content = """
# The version of the installed absl-py will be less than 42.
absl-py>=42
"""
with self._MockOpen(requirements_content) as mocked_open:
with self.assertRaises(errors.Setup.PythonPackageRequirementUnfulfilled):
requirements._CheckRequirements(_PATH)
mocked_open.assert_called_once_with('dir/file')
def testInstalledVersionGreaterThanRequirement(self):
requirements_content = """
# The version of the installed absl-py will be greater than 0.0.1.
absl-py==0.0.1
"""
with self._MockOpen(requirements_content) as mocked_open:
with self.assertRaises(errors.Setup.PythonPackageRequirementUnfulfilled):
requirements._CheckRequirements(_PATH)
mocked_open.assert_called_once_with('dir/file')
def testIncludedFiles(self):
top_file = """
package-0
-rsubfile0
package-1>=2.0
-rsubfile1
package-2
"""
subfile0 = """
package-3
-rsubdir/subfile2
package-4
-r../subfile3
package-5
"""
subfile1 = """
package-6
"""
with self._MockOpen(top_file, subfile0, '', '', subfile1) as mocked_open:
with mock.patch.object(pkg_resources, 'require') as mocked_require:
requirements._CheckRequirements(_PATH)
mocked_open.assert_has_calls((
mock.call('dir/file'),
mock.call('dir/subfile0'),
mock.call('dir/subdir/subfile2'),
mock.call('dir/../subfile3'),
mock.call('dir/subfile1'),
))
mocked_require.assert_has_calls(
list(
map(
mock.call,
(
'package-0',
'package-3',
'package-4',
'package-5',
'package-1>=2.0',
'package-6',
'package-2',
),
)
)
)
class CheckBasicRequirementsTestCase(unittest.TestCase):
def testAllRequirementsFulfilled(self):
requirements.CheckBasicRequirements()
class CheckProviderRequirementsTestCase(unittest.TestCase):
def testNoRequirementsFile(self):
# If a provider does not have a requirements file, then there can be no
# unfulfilled requirement.
requirements.CheckProviderRequirements('fakeprovider')
def testUnfulfilledRequirements(self):
# AWS does have a requirements file, but it contains packages that are not
# installed as part of the test environment.
with self.assertRaises(errors.Setup.PythonPackageRequirementUnfulfilled):
requirements.CheckProviderRequirements('aws')
if __name__ == '__main__':
unittest.main()