/
test_reporter_bitbucketserver.py
371 lines (329 loc) · 14.5 KB
/
test_reporter_bitbucketserver.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
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from __future__ import absolute_import
from __future__ import print_function
from future.utils import PY3
from mock import Mock
from twisted.internet import defer
from twisted.trial import unittest
from buildbot import config
from buildbot.process.results import FAILURE
from buildbot.process.results import SUCCESS
from buildbot.reporters.bitbucketserver import BitbucketServerPRCommentPush
from buildbot.reporters.bitbucketserver import BitbucketServerStatusPush
from buildbot.reporters.bitbucketserver import HTTP_PROCESSED
from buildbot.reporters.bitbucketserver import HTTP_CREATED
from buildbot.test.fake import httpclientservice as fakehttpclientservice
from buildbot.test.fake import fakemaster
from buildbot.test.util.logging import LoggingMixin
from buildbot.test.util.notifier import NotifierTestMixin
from buildbot.test.util.reporter import ReporterTestMixin
from buildbot.util import unicode2NativeString
HTTP_NOT_FOUND = 404
class TestBitbucketServerStatusPush(unittest.TestCase, ReporterTestMixin, LoggingMixin):
@defer.inlineCallbacks
def setupReporter(self, **kwargs):
# ignore config error if txrequests is not installed
self.patch(config, '_errors', Mock())
self.master = fakemaster.make_master(
testcase=self, wantData=True, wantDb=True, wantMq=True)
self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
self.master, self,
'serv', auth=('username', 'passwd'),
debug=None, verify=None)
self.sp = sp = BitbucketServerStatusPush(
"serv", "username", "passwd", **kwargs)
yield sp.setServiceParent(self.master)
yield self.master.startService()
@defer.inlineCallbacks
def tearDown(self):
yield self.master.stopService()
@defer.inlineCallbacks
def setupBuildResults(self, buildResults):
self.insertTestData([buildResults], buildResults)
build = yield self.master.data.get(("builds", 20))
defer.returnValue(build)
def _check_start_and_finish_build(self, build):
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'INPROGRESS', 'key': u'Builder0',
'description': 'Build started.'},
code=HTTP_PROCESSED)
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'SUCCESSFUL', 'key': u'Builder0',
'description': 'Build done.'},
code=HTTP_PROCESSED)
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'FAILED', 'key': u'Builder0',
'description': 'Build done.'})
build['complete'] = False
self.sp.buildStarted(("build", 20, "started"), build)
build['complete'] = True
self.sp.buildFinished(("build", 20, "finished"), build)
build['results'] = FAILURE
self.sp.buildFinished(("build", 20, "finished"), build)
@defer.inlineCallbacks
def test_basic(self):
self.setupReporter()
build = yield self.setupBuildResults(SUCCESS)
self._check_start_and_finish_build(build)
@defer.inlineCallbacks
def test_setting_options(self):
self.setupReporter(statusName='Build', startDescription='Build started.',
endDescription='Build finished.')
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'INPROGRESS', 'key': u'Builder0',
'name': 'Build', 'description': 'Build started.'},
code=HTTP_PROCESSED)
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'SUCCESSFUL', 'key': u'Builder0',
'name': 'Build', 'description': 'Build finished.'},
code=HTTP_PROCESSED)
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'FAILED', 'key': u'Builder0',
'name': 'Build', 'description': 'Build finished.'},
code=HTTP_PROCESSED)
build['complete'] = False
self.sp.buildStarted(("build", 20, "started"), build)
build['complete'] = True
self.sp.buildFinished(("build", 20, "finished"), build)
build['results'] = FAILURE
self.sp.buildFinished(("build", 20, "finished"), build)
@defer.inlineCallbacks
def test_error(self):
self.setupReporter()
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
u'/rest/build-status/1.0/commits/d34db33fd43db33f',
json={'url': 'http://localhost:8080/#builders/79/builds/0',
'state': 'INPROGRESS', 'key': u'Builder0',
'description': 'Build started.'},
code=HTTP_NOT_FOUND,
content_json={
"error_description": "This commit is unknown to us",
"error": "invalid_commit"})
build['complete'] = False
self.setUpLogging()
self.sp.buildStarted(("build", 20, "started"), build)
self.assertLogged('404: Unable to send Bitbucket Server status')
@defer.inlineCallbacks
def test_basic_with_no_revision(self):
yield self.setupReporter()
old_test_revision = self.TEST_REVISION
try:
self.TEST_REVISION = None
build = yield self.setupBuildResults(SUCCESS)
finally:
self.TEST_REVISION = old_test_revision
self._check_start_and_finish_build(build)
@defer.inlineCallbacks
def test_basic_with_no_revision_and_dict_got_revision(self):
yield self.setupReporter()
old_test_revision = self.TEST_REVISION
old_got_revision = self.TEST_PROPS['got_revision']
try:
self.TEST_REVISION = None
self.TEST_PROPS['got_revision'] = {'cbgerrit' : 'd34db33fd43db33f'}
build = yield self.setupBuildResults(SUCCESS)
finally:
self.TEST_REVISION = old_test_revision
self.TEST_PROPS['got_revision'] = old_got_revision
self._check_start_and_finish_build(build)
@defer.inlineCallbacks
def test_basic_with_no_revision_and_no_got_revision(self):
yield self.setupReporter()
old_test_revision = self.TEST_REVISION
old_got_revision = self.TEST_PROPS['got_revision']
try:
self.TEST_REVISION = None
self.TEST_PROPS['got_revision'] = None
build = yield self.setupBuildResults(SUCCESS)
finally:
self.TEST_REVISION = old_test_revision
self.TEST_PROPS['got_revision'] = old_got_revision
self.setUpLogging()
self.sp.buildStarted(("build", 20, "started"), build)
self.assertLogged('Unable to get the commit hash')
UNICODE_BODY = u"body: \u00E5\u00E4\u00F6 text"
EXPECTED_API = u'/rest/api/1.0/projects/PRO/repos/myrepo/pull-requests/20/comments'
PR_URL = "http://example.com/projects/PRO/repos/myrepo/pull-requests/20"
class TestBitbucketServerPRCommentPush(unittest.TestCase, NotifierTestMixin, LoggingMixin):
@defer.inlineCallbacks
def setupReporter(self, verbose=True, **kwargs):
# ignore config error if txrequests is not installed
self.patch(config, '_errors', Mock())
self.master = fakemaster.make_master(
testcase=self, wantData=True, wantDb=True, wantMq=True)
self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
verify=None)
self.cp = BitbucketServerPRCommentPush(
"serv", "username", "passwd", verbose=verbose, **kwargs)
yield self.cp.setServiceParent(self.master)
yield self.master.startService()
self.cp.messageFormatter = Mock(spec=self.cp.messageFormatter)
self.cp.messageFormatter.formatMessageForBuildResults.return_value = \
{"body": UNICODE_BODY, "type": "text"}
@defer.inlineCallbacks
def tearDown(self):
yield self.master.stopService()
@defer.inlineCallbacks
def setupBuildResults(self, buildResults, set_pr=True):
buildset, builds = yield NotifierTestMixin.setupBuildResults(self, buildResults)
if set_pr:
self.master.db.builds.setBuildProperty(
20, "pullrequesturl", PR_URL, "test")
defer.returnValue((buildset, builds))
@defer.inlineCallbacks
def test_reporter_basic(self):
yield self.setupReporter()
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=HTTP_CREATED)
build["complete"] = True
self.setUpLogging()
self.cp.buildComplete(("build", 20, "finished"), build)
self.assertLogged(unicode2NativeString(
u'{} sent to {}'.format(UNICODE_BODY, PR_URL)))
@defer.inlineCallbacks
def test_reporter_basic_without_logging(self):
yield self.setupReporter(verbose=False)
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=HTTP_CREATED)
build["complete"] = True
self.setUpLogging()
self.cp.buildComplete(("build", 20, "finished"), build)
self.assertNotLogged(unicode2NativeString(
u'{} sent to {}'.format(UNICODE_BODY, PR_URL)))
@defer.inlineCallbacks
def test_reporter_non_unicode(self):
if PY3:
raise unittest.SkipTest("not supported in Python3")
yield self.setupReporter()
self.cp.messageFormatter.formatMessageForBuildResults.return_value = \
{"body": "body text",
"type": "text"}
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
self._http.expect(
"post",
EXPECTED_API,
json={"text": "body text"},
code=HTTP_CREATED)
build["complete"] = True
self.cp.buildComplete(("build", 20, "finished"), build)
@defer.inlineCallbacks
def test_reporter_without_pullrequest(self):
yield self.setupReporter()
_, builds = yield self.setupBuildResults(SUCCESS, set_pr=False)
build = builds[0]
build["complete"] = True
# we don't expect any request
self.cp.buildComplete(("builds", 20, "finished"), build)
@defer.inlineCallbacks
def test_missing_worker_does_nothing(self):
yield self.setupReporter()
self.cp.workerMissing(("workers", 13, "missing"), 13)
@defer.inlineCallbacks
def test_reporter_with_buildset(self):
yield self.setupReporter(buildSetSummary=True)
buildset, _ = yield self.setupBuildResults(SUCCESS)
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=HTTP_CREATED)
self.cp.buildsetComplete(("buildsets", 20, "complete"), buildset)
@defer.inlineCallbacks
def test_reporter_logs_error_code_and_content_on_invalid_return_code(self):
yield self.setupReporter()
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
http_error_code = 500
error_body = {u"errors": [{u"message": u"A dataXXXbase error has occurred."}]}
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=http_error_code,
content_json=error_body)
self.setUpLogging()
build['complete'] = True
self.cp.buildComplete(("builds", 20, "finished"), build)
self.assertLogged("^{}: Unable to send a comment: ".format(http_error_code))
self.assertLogged("A dataXXXbase error has occurred")
@defer.inlineCallbacks
def test_reporter_logs_error_code_without_content_on_invalid_return_code(self):
yield self.setupReporter()
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
http_error_code = 503
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=http_error_code)
self.setUpLogging()
build['complete'] = True
self.cp.buildComplete(("builds", 20, "finished"), build)
self.assertLogged("^{}: Unable to send a comment: ".format(
http_error_code))
@defer.inlineCallbacks
def test_reporter_does_not_log_return_code_on_valid_return_code(
self):
yield self.setupReporter()
_, builds = yield self.setupBuildResults(SUCCESS)
build = builds[0]
http_code = 201
self._http.expect(
"post",
EXPECTED_API,
json={"text": UNICODE_BODY},
code=http_code)
self.setUpLogging()
build['complete'] = True
self.cp.buildComplete(("builds", 20, "finished"), build)
self.assertNotLogged("^{}:".format(http_code))