This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_hgpoller.py
465 lines (383 loc) · 16.8 KB
/
test_hgpoller.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from twisted.trial import unittest
import threading
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from buildbot.util import json
if not hasattr(json.decoder, 'JSONDecodeError'):
JSONDecodeError = ValueError
else:
JSONDecodeError = json.JSONDecodeError
from buildbotcustom.changes.hgpoller import BasePoller, BaseHgPoller, HgPoller, \
HgLocalePoller, HgAllLocalesPoller, _parse_changes
class VerySimpleHTTPRequestHandler(BaseHTTPRequestHandler):
# This class requires the consumer to set contents, because we
# cannot override __init__ due to the way HTTPServer works
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(self.contents)
return
def log_message(self, fmt, *args):
pass
def startHTTPServer(contents):
# Starts up a simple HTTPServer that processes requests with
# VerySimpleHTTPRequestHandler (subclassed to make sure it's unique
# for each instance), and serving the contents passed as contents
# Returns a tuple containing the HTTPServer instance and the port it is
# listening on. The caller is responsible for shutting down the HTTPServer.
class OurHandler(VerySimpleHTTPRequestHandler):
pass
OurHandler.contents = contents
server = HTTPServer(('', 0), OurHandler)
ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
return (server, port)
class UrlCreation(unittest.TestCase):
def testSimpleUrl(self):
correctUrl = 'http://hg.mozilla.org/mozilla-central/json-pushes?full=1'
poller = BaseHgPoller(
hgURL='http://hg.mozilla.org', branch='mozilla-central')
url = poller._make_url()
self.failUnlessEqual(url, correctUrl)
def testUrlWithLastChangeset(self):
correctUrl = 'http://hg.mozilla.org/mozilla-central/json-pushes?full=1&fromchange=123456'
poller = BaseHgPoller(
hgURL='http://hg.mozilla.org', branch='mozilla-central')
poller.lastChangeset = '123456'
url = poller._make_url()
self.failUnlessEqual(url, correctUrl)
def testTipsOnlyUrl(self):
correctUrl = 'http://hg.mozilla.org/mozilla-central/json-pushes?full=1&tipsonly=1'
poller = BaseHgPoller(
hgURL='http://hg.mozilla.org', branch='mozilla-central',
tipsOnly=True)
url = poller._make_url()
self.failUnlessEqual(url, correctUrl)
def testTipsOnlyWithLastChangeset(self):
# there's two possible correct URLs in this case
correctUrls = [
'http://hg.mozilla.org/releases/mozilla-1.9.1/json-pushes?full=1&fromchange=123456&tipsonly=1',
'http://hg.mozilla.org/releases/mozilla-1.9.1/json-pushes?full=1&tipsonly=1&fromchange=123456'
]
poller = BaseHgPoller(hgURL='http://hg.mozilla.org',
branch='releases/mozilla-1.9.1', tipsOnly=True)
poller.lastChangeset = '123456'
url = poller._make_url()
self.failUnlessIn(url, correctUrls)
def testOverrideUrl(self):
correctUrl = 'http://hg.mozilla.org/other_repo/json-pushes?full=1&fromchange=123456'
poller = BaseHgPoller(
hgURL='http://hg.mozilla.org', branch='mozilla-central',
pushlogUrlOverride='http://hg.mozilla.org/other_repo/json-pushes?full=1')
poller.lastChangeset = '123456'
url = poller._make_url()
self.failUnlessEqual(url, correctUrl)
def testUrlWithUnicodeLastChangeset(self):
correctUrl = 'http://hg.mozilla.org/mozilla-central/json-pushes?full=1&fromchange=123456'
poller = BaseHgPoller(
hgURL='http://hg.mozilla.org', branch='mozilla-central')
poller.lastChangeset = u'123456'
url = poller._make_url()
self.failUnlessEqual(url, correctUrl)
self.failUnless(isinstance(url, str))
fakeLocalesFile = """/l10n-central/af/
/l10n-central/be/
/l10n-central/de/
/l10n-central/hi/
/l10n-central/kk/
/l10n-central/zh-TW/"""
class FakeHgAllLocalesPoller(HgAllLocalesPoller):
def __init__(self):
HgAllLocalesPoller.__init__(
self, hgURL='fake', repositoryIndex='fake', branch='fake')
def pollNextLocale(self):
pass
class RepositoryIndexParsing(unittest.TestCase):
def testRepositoryIndexParsing(self):
correctLocales = [('af', 'l10n-central'), ('be', 'l10n-central'),
('de', 'l10n-central'), ('hi', 'l10n-central'),
('kk', 'l10n-central'), ('zh-TW', 'l10n-central')]
poller = FakeHgAllLocalesPoller()
poller.processData(fakeLocalesFile)
self.failUnlessEqual(poller.pendingLocales, correctLocales)
class TestPolling(unittest.TestCase):
def setUp(self):
self.server, self.portnum = startHTTPServer('testcontents')
def tearDown(self):
self.server.server_close()
self.portnum = None
self.server = None
def success(self, res):
self.failUnless(self.fp.success)
def failure(self, res):
print res
self.fail()
def doPollingTest(self, poller, **kwargs):
class FakePoller(poller):
def __init__(self):
poller.__init__(self, **kwargs)
self.success = False
def stopLoad(self, res):
self.success = True
def processData(self, res):
pass
def dataFinished(self, res):
pass
def dataFailed(self, res):
pass
def pollDone(self, res):
pass
self.fp = FakePoller()
d = self.fp.poll()
d.addCallbacks(self.success, self.failure)
return d
def testHgPoller(self):
url = 'http://localhost:%s' % str(self.portnum)
return self.doPollingTest(HgPoller, hgURL=url, branch='whatever')
def testHgLocalePoller(self):
url = 'http://localhost:%s' % str(self.portnum)
return self.doPollingTest(HgLocalePoller, locale='fake', parent='fake',
hgURL=url, branch='whatever')
def testHgAllLocalesPoller(self):
url = 'http://localhost:%s' % str(self.portnum)
return self.doPollingTest(HgAllLocalesPoller, hgURL=url,
repositoryIndex='foobar')
validPushlog = """
{
"15226": {
"date": 1282358416,
"changesets": [
{
"node": "4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6",
"files": [
"embedding/android/GeckoInputConnection.java",
"embedding/android/GeckoSurfaceView.java",
"widget/src/android/nsWindow.cpp",
"widget/src/android/nsWindow.h"
],
"tags": [],
"author": "Jim Chen <jchen@mozilla.com>",
"branch": "GECKO20b5pre_20100820_RELBRANCH",
"desc": "Bug 588456 - Properly commit Android IME composition on blur; r=mwu a=blocking-fennec"
}
],
"user": "dougt@mozilla.com"
},
"15227": {
"date": 1282362551,
"changesets": [
{
"node": "ee6fb954cbc3de0f76e84cad6bdff452116e1b03",
"files": [
"browser/base/content/browser.js",
"browser/components/privatebrowsing/content/aboutPrivateBrowsing.xhtml",
"browser/locales/en-US/chrome/overrides/netError.dtd",
"build/automation.py.in",
"docshell/resources/content/netError.xhtml",
"dom/locales/en-US/chrome/netErrorApp.dtd",
"extensions/cookie/nsPermissionManager.cpp"
],
"tags": [],
"author": "Bobby Holley <bobbyholley@gmail.com>",
"branch": "default",
"desc": "Backout of changesets c866e73f3209 and baff7b7b32bc because of sicking's push-and-run bustage. a=backout"
},
{
"node": "33be08836cb164f9e546231fc59e9e4cf98ed991",
"files": [
"modules/libpref/src/init/all.js"
],
"tags": [],
"author": "Bobby Holley <bobbyholley@gmail.com>",
"branch": "default",
"desc": "Bug 563088 - Re-enable image discarding.r=joe,a=blocker"
}
],
"user": "bobbyholley@stanford.edu"
}
}
"""
malformedPushlog = """
{
"15226": {
"date": 1282358416,
"changesets": [
{
"node": "4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6",
"files": [
"embedding/android/GeckoInputConnection.java",
"embedding/android/GeckoSurfaceView.java",
"widget/src/android/nsWindow.cpp",
"widget/src/android/nsWindow.h"
],
"tags": [],
"author": "Jim Chen <jchen@mozilla.com>",
"branch": "GECKO20b5pre_20100820_RELBRANCH",
"desc": "Bug 588456 - Properly commit Android IME composition on blur; r=mwu a=blocking-fennec"
}
],
"user": "dougt@mozilla.com"
"""
class PushlogParsing(unittest.TestCase):
def testValidPushlog(self):
pushes = _parse_changes(validPushlog)
self.failUnlessEqual(len(pushes), 2)
self.failUnlessEqual(pushes[0]['changesets'][0]['node'],
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
self.failUnlessEqual(pushes[0]['date'], 1282358416)
self.failUnlessEqual(len(pushes[0]['changesets'][0]['files']), 4)
self.failUnlessEqual(pushes[0]['changesets'][0]['branch'],
'GECKO20b5pre_20100820_RELBRANCH')
self.failUnlessEqual(pushes[0]['changesets'][0]['author'],
'Jim Chen <jchen@mozilla.com>')
self.failUnlessEqual(pushes[0]['user'], 'dougt@mozilla.com')
self.failUnlessEqual(pushes[1]['changesets'][0]['node'],
'ee6fb954cbc3de0f76e84cad6bdff452116e1b03')
self.failUnlessEqual(pushes[1]['date'], 1282362551)
self.failUnlessEqual(len(pushes[1]['changesets'][0]['files']), 7)
self.failUnlessEqual(pushes[1]['changesets'][0]['branch'], 'default')
self.failUnlessEqual(pushes[1]['user'], 'bobbyholley@stanford.edu')
self.failUnlessEqual(pushes[1]['changesets'][0]['author'],
'Bobby Holley <bobbyholley@gmail.com>')
self.failUnlessEqual(pushes[1]['changesets'][1]['node'],
'33be08836cb164f9e546231fc59e9e4cf98ed991')
self.failUnlessEqual(len(pushes[1]['changesets'][1]['files']), 1)
self.failUnlessEqual(pushes[1]['changesets'][1]['branch'], 'default')
self.failUnlessEqual(pushes[1]['changesets'][1]['author'],
'Bobby Holley <bobbyholley@gmail.com>')
def testMalformedPushlog(self):
self.failUnlessRaises(
JSONDecodeError, _parse_changes, malformedPushlog)
def testEmptyPushlog(self):
self.failUnlessRaises(JSONDecodeError, _parse_changes, "")
class RepoBranchHandling(unittest.TestCase):
def setUp(self):
self.changes = []
def doTest(self, repo_branch):
changes = self.changes
class TestPoller(BaseHgPoller):
def __init__(self):
BaseHgPoller.__init__(self, 'http://localhost', 'whatever',
repo_branch=repo_branch)
self.emptyRepo = True
class parent:
def addChange(self, change):
changes.append(change)
p = TestPoller()
p.parent = parent()
p.processData(validPushlog)
def testNoRepoBranch(self):
self.doTest(None)
self.assertEquals(len(self.changes), 2)
def testDefaultRepoBranch(self):
self.doTest('default')
# mergePushChanges is on by default, so we end up with a single change
# here
self.assertEquals(len(self.changes), 1)
self.assertEquals(self.changes[0].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testRelbranch(self):
self.doTest('GECKO20b5pre_20100820_RELBRANCH')
self.assertEquals(len(self.changes), 1)
self.assertEquals(self.changes[0].revision,
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
class MaxChangesHandling(unittest.TestCase):
def setUp(self):
self.changes = []
def doTest(self, repo_branch, maxChanges, mergePushChanges):
changes = self.changes
class TestPoller(BaseHgPoller):
def __init__(self):
BaseHgPoller.__init__(self, 'http://localhost', 'whatever',
repo_branch=repo_branch, maxChanges=maxChanges, mergePushChanges=mergePushChanges)
self.emptyRepo = True
class parent:
def addChange(self, change):
changes.append(change)
p = TestPoller()
p.parent = parent()
p.processData(validPushlog)
def testNoRepoBigMax(self):
# Test that we get all of the changes when maxChanges is large enough
self.doTest(None, 10, False)
self.assertEquals(len(self.changes), 3)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
self.assertEquals(self.changes[1].revision,
'ee6fb954cbc3de0f76e84cad6bdff452116e1b03')
self.assertEquals(self.changes[2].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testMergingNoRepoBigMax(self):
# Test that we get all of the changes when maxChanges is large enough
self.doTest(None, 10, True)
self.assertEquals(len(self.changes), 2)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
self.assertEquals(self.changes[1].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testNoRepoUnlimited(self):
# Test that we get all of the changes when maxChanges is large enough
self.doTest(None, None, False)
self.assertEquals(len(self.changes), 3)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
self.assertEquals(self.changes[1].revision,
'ee6fb954cbc3de0f76e84cad6bdff452116e1b03')
self.assertEquals(self.changes[2].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testMergingNoRepoUnlimited(self):
# Test that we get all of the changes when maxChanges is large enough
self.doTest(None, None, True)
self.assertEquals(len(self.changes), 2)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'4c23e51a484f077ea27af3ea4a4ee13da5aeb5e6')
self.assertEquals(self.changes[1].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testNoRepoSmallMax(self):
# Test that we get only 2 changes if maxChanges is set to 2
self.doTest(None, 2, False)
# The extra change is the overflow indicator
self.assertEquals(len(self.changes), 3)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision, None)
self.assertEquals(self.changes[0].files, ['overflow'])
self.assertEquals(self.changes[1].revision,
'ee6fb954cbc3de0f76e84cad6bdff452116e1b03')
self.assertEquals(self.changes[2].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testMergingNoRepoSmallMax(self):
# Test that we get only 1 change if maxChanges is set to 1
self.doTest(None, 1, True)
self.assertEquals(len(self.changes), 1)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
self.assert_(
'overflow' in self.changes[0].files, self.changes[0].files)
self.assert_('widget/src/android/nsWindow.h' not in self.changes[
0].files, self.changes[0].files)
def testDefaultRepoBigMax(self):
self.doTest('default', 10, False)
self.assertEquals(len(self.changes), 2)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision,
'ee6fb954cbc3de0f76e84cad6bdff452116e1b03')
self.assertEquals(self.changes[1].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testDefaultRepoSmallMax(self):
self.doTest('default', 1, False)
self.assertEquals(len(self.changes), 2)
# Check that we got the right changes
self.assertEquals(self.changes[0].revision, None)
self.assertEquals(self.changes[0].files, ['overflow'])
self.assertEquals(self.changes[1].revision,
'33be08836cb164f9e546231fc59e9e4cf98ed991')
def testRelbranchSmallMax(self):
self.doTest('GECKO20b5pre_20100820_RELBRANCH', 1, False)
self.assertEquals(len(self.changes), 1)