-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_main.py
306 lines (257 loc) · 11.1 KB
/
test_main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
from __future__ import print_function
import pytest
import unittest
import requests
from datetime import date
from mock import patch, Mock, MagicMock, ANY
from mozregression import main, errors, __version__, config
from mozregression.test_runner import ManualTestRunner, CommandTestRunner
from mozregression.download_manager import BuildDownloadManager
from mozregression.bisector import Bisector, Bisection, IntegrationHandler, \
NightlyHandler
from six.moves import range
class AppCreator(object):
def __init__(self, logger):
self.app = None
self.logs = []
logger.info = self.logs.append
logger.warning = self.logs.append
def __call__(self, argv):
config = main.cli(argv, conf_file=None)
config.validate()
self.app = main.Application(config.fetch_config, config.options)
return self.app
def find_in_log(self, msg, exact=True):
if exact:
return msg in self.logs
for m in self.logs:
if msg in m:
return True
def clear(self):
if self.app:
self.app.clear()
@pytest.yield_fixture
def create_app(mocker):
"""allow to create an Application and ensure that clear() is called"""
creator = AppCreator(mocker.patch('mozregression.main.LOG'))
yield creator
creator.clear()
def test_app_get_manual_test_runner(create_app):
app = create_app(['--profile=/prof'])
assert isinstance(app.test_runner, ManualTestRunner)
assert app.test_runner.launcher_kwargs == dict(
addons=[], profile='/prof', cmdargs=['--allow-downgrade'], preferences=[],
adb_profile_dir=None
)
def test_app_get_command_test_runner(create_app):
app = create_app(['--command=echo {binary}'])
assert isinstance(app.test_runner, CommandTestRunner)
assert app.test_runner.command == 'echo {binary}'
@pytest.mark.parametrize("argv,background_dl_policy,size_limit", [
([], "cancel", 0),
# without persist, cancel policy is forced
(['--background-dl-policy=keep'], "cancel", 0),
(['--persist=1', "--background-dl-policy=keep"], "keep", 0),
# persist limit
(['--persist-size-limit=10'], "cancel", 10 * 1073741824),
])
def test_app_get_download_manager(create_app, argv, background_dl_policy,
size_limit):
app = create_app(argv)
assert isinstance(app.build_download_manager, BuildDownloadManager)
assert app.build_download_manager.background_dl_policy == \
background_dl_policy
assert app.build_download_manager.persist_limit.size_limit == size_limit
assert app.build_download_manager.persist_limit.file_limit == 5
def test_app_get_bisector(create_app):
app = create_app([])
assert isinstance(app.bisector, Bisector)
def test_app_bisect_nightlies_finished(create_app, mocker):
app = create_app(['-g=2015-06-01', '-b=2015-06-02'])
app.bisector.bisect = Mock(return_value=Bisection.FINISHED)
app._bisect_integration = Mock(return_value=0)
NightlyHandler = mocker.patch(
"mozregression.main.NightlyHandler"
)
nh = Mock(bad_date=date.today(), good_revision='c1',
bad_revision='c2')
NightlyHandler.return_value = nh
assert app.bisect_nightlies() == 0
app.bisector.bisect.assert_called_once_with(
ANY,
date(2015, 0o6, 0o1),
date(2015, 0o6, 0o2)
)
assert create_app.find_in_log(
"Got as far as we can go bisecting nightlies..."
)
app._bisect_integration.assert_called_once_with(
'c1', 'c2', expand=config.DEFAULT_EXPAND)
def test_app_bisect_nightlies_no_data(create_app):
app = create_app(['-g=2015-06-01', '-b=2015-06-02'])
app.bisector.bisect = Mock(return_value=Bisection.NO_DATA)
assert app.bisect_nightlies() == 1
assert create_app.find_in_log(
"Unable to get valid builds within the given range.",
False
)
@pytest.mark.parametrize("same_chsets", [True, False])
def test_app_bisect_integration_finished(create_app, same_chsets):
argv = [
'--good=c1',
'--bad=%s' % ('c1' if same_chsets else 'c2')
]
app = create_app(argv)
app.bisector.bisect = Mock(return_value=Bisection.FINISHED)
assert app.bisect_integration() == 0
assert create_app.find_in_log("No more integration revisions, bisection finished.")
if same_chsets:
assert create_app.find_in_log("It seems that you used two changesets"
" that are in the same push.", False)
@pytest.mark.parametrize("argv,expected_log", [
(['--app=firefox', '--bits=64'], "--app=firefox --bits=64"),
(['--persist', 'blah stuff'], "--persist 'blah stuff'"),
(['--addon=a b c', '--addon=d'], "'--addon=a b c' --addon=d"),
(['--find-fix', '--arg=a b'], "--find-fix '--arg=a b'"),
(['--profile=pro file'], "'--profile=pro file'"),
(['-g', '2015-11-01'], "--good=2015-11-01"),
(['--bad=2015-11-03'], "--bad=2015-11-03"),
])
def test_app_bisect_nightlies_user_exit(create_app, argv, expected_log,
mocker):
Handler = mocker.patch("mozregression.main.NightlyHandler")
Handler.return_value = Mock(
build_range=[Mock(repo_name="mozilla-central")],
good_date='2015-11-01',
bad_date='2015-11-03',
spec=NightlyHandler
)
app = create_app(argv)
app.bisector.bisect = Mock(return_value=Bisection.USER_EXIT)
sys = mocker.patch('mozregression.main.sys')
sys.argv = argv
assert app.bisect_nightlies() == 0
assert create_app.find_in_log("To resume, run:")
assert create_app.find_in_log(expected_log, False)
assert create_app.find_in_log("--repo=mozilla-central", False)
def test_app_bisect_integration_user_exit(create_app, mocker):
Handler = mocker.patch("mozregression.main.IntegrationHandler")
Handler.return_value = Mock(build_range=[Mock(repo_name="mozilla-central")],
good_revision='c1',
bad_revision='c2',
spec=IntegrationHandler)
app = create_app(['--good=c1', '--bad=c2'])
app.bisector.bisect = Mock(return_value=Bisection.USER_EXIT)
assert app.bisect_integration() == 0
assert create_app.find_in_log("To resume, run:")
assert create_app.find_in_log("--repo=mozilla-central", False)
def test_app_bisect_integration_no_data(create_app):
app = create_app(['--good=c1', '--bad=c2'])
app.bisector.bisect = Mock(return_value=Bisection.NO_DATA)
assert app.bisect_integration() == 1
assert create_app.find_in_log(
"There are no build artifacts for these changesets",
False
)
def test_app_bisect_ctrl_c_exit(create_app, mocker):
app = create_app([])
app.bisector.bisect = Mock(side_effect=KeyboardInterrupt)
at_exit = mocker.patch('atexit.register')
handler = MagicMock(good_revision='c1', bad_revision='c2')
Handler = mocker.patch("mozregression.main.NightlyHandler")
Handler.return_value = handler
with pytest.raises(KeyboardInterrupt):
app.bisect_nightlies()
print(handler)
at_exit.assert_called_once_with(app._on_exit_print_resume_info, handler)
# call the atexit handler
mocker.stopall()
app._on_exit_print_resume_info(handler)
handler.print_range.assert_called_once_with()
class TestCheckMozregresionVersion(unittest.TestCase):
@patch('mozregression.main.LOG')
@patch('requests.get')
def test_version_is_upto_date(self, get, log):
response = Mock(json=lambda: {'info': {'version': __version__}})
get.return_value = response
main.check_mozregression_version()
self.assertFalse(log.critical.called)
@patch('requests.get')
def test_Exception_error(self, get):
get.side_effect = requests.RequestException
# exception is handled inside main.check_mozregression_version
main.check_mozregression_version()
self.assertRaises(requests.RequestException, get)
@patch('mozregression.main.LOG')
@patch('requests.get')
def test_warn_if_version_is_not_up_to_date(self, get, log):
response = Mock(json=lambda: {'info': {'version': 0}})
get.return_value = response
main.check_mozregression_version()
self.assertEqual(log.warning.call_count, 2)
class TestMain(unittest.TestCase):
def setUp(self):
self.app = Mock()
self.logger = Mock()
@patch('mozregression.main.LOG')
@patch('mozregression.main.check_mozregression_version')
@patch('mozlog.structured.commandline.setup_logging')
@patch('mozregression.main.set_http_session')
@patch('mozregression.main.Application')
def do_cli(self, argv, Application, set_http_session,
setup_logging, check_mozregression_version, log):
self.logger = log
def create_app(fetch_config, options):
self.app.fetch_config = fetch_config
self.app.options = options
return self.app
Application.side_effect = create_app
try:
main.main(argv)
except SystemExit as exc:
return exc.code
else:
self.fail('mozregression.main.cli did not call sys.exit')
def pop_logs(self):
logs = []
for i in range(len(self.logger.mock_calls)):
call = self.logger.mock_calls.pop(0)
logs.append((call[0], call[1][0]))
return logs
def pop_exit_error_msg(self):
for lvl, msg in reversed(self.pop_logs()):
if lvl == 'error':
return msg
def test_without_args(self):
self.app.bisect_nightlies.return_value = 0
exitcode = self.do_cli([])
# bisect_nightlies has been called
self.app.bisect_nightlies.assert_called_with()
# we exited with the return value of bisect_nightlies
self.assertEqual(exitcode, 0)
def test_bisect_integration(self):
self.app.bisect_integration.return_value = 0
exitcode = self.do_cli(['--good=a1', '--bad=b5'])
self.assertEqual(exitcode, 0)
self.app.bisect_integration.assert_called_with()
def test_handle_keyboard_interrupt(self):
# KeyboardInterrupt is handled with a nice error message.
self.app.bisect_nightlies.side_effect = KeyboardInterrupt
exitcode = self.do_cli([])
self.assertIn('Interrupted', exitcode)
def test_handle_mozregression_errors(self):
# Any MozRegressionError subclass is handled with a nice error message
self.app.bisect_nightlies.side_effect = \
errors.MozRegressionError('my error')
exitcode = self.do_cli([])
self.assertNotEqual(exitcode, 0)
self.assertIn('my error', self.pop_exit_error_msg())
def test_handle_other_errors(self):
# other exceptions are just thrown as usual
# so we have complete stacktrace
self.app.bisect_nightlies.side_effect = NameError
self.assertRaises(NameError, self.do_cli, [])