"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.3.1/buildbot/test/unit/test_reporter_bitbucketserver.py" (23 May 2019, 13477 Bytes) of package /linux/misc/buildbot-2.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_reporter_bitbucketserver.py": 2.1.0_vs_2.2.0.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 from mock import Mock
   17 
   18 from twisted.internet import defer
   19 from twisted.trial import unittest
   20 
   21 from buildbot import config
   22 from buildbot.process.properties import Interpolate
   23 from buildbot.process.results import FAILURE
   24 from buildbot.process.results import SUCCESS
   25 from buildbot.reporters.bitbucketserver import HTTP_CREATED
   26 from buildbot.reporters.bitbucketserver import HTTP_PROCESSED
   27 from buildbot.reporters.bitbucketserver import BitbucketServerPRCommentPush
   28 from buildbot.reporters.bitbucketserver import BitbucketServerStatusPush
   29 from buildbot.test.fake import fakemaster
   30 from buildbot.test.fake import httpclientservice as fakehttpclientservice
   31 from buildbot.test.util.logging import LoggingMixin
   32 from buildbot.test.util.misc import TestReactorMixin
   33 from buildbot.test.util.notifier import NotifierTestMixin
   34 from buildbot.test.util.reporter import ReporterTestMixin
   35 
   36 HTTP_NOT_FOUND = 404
   37 
   38 
   39 class TestBitbucketServerStatusPush(TestReactorMixin, unittest.TestCase,
   40                                     ReporterTestMixin, LoggingMixin):
   41 
   42     @defer.inlineCallbacks
   43     def setupReporter(self, **kwargs):
   44         self.setUpTestReactor()
   45         # ignore config error if txrequests is not installed
   46         self.patch(config, '_errors', Mock())
   47         self.master = fakemaster.make_master(self, wantData=True, wantDb=True,
   48                                              wantMq=True)
   49 
   50         self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
   51             self.master, self,
   52             'serv', auth=('username', 'passwd'),
   53             debug=None, verify=None)
   54         self.sp = sp = BitbucketServerStatusPush(
   55             "serv", Interpolate("username"), Interpolate("passwd"), **kwargs)
   56         yield sp.setServiceParent(self.master)
   57         yield self.master.startService()
   58 
   59     @defer.inlineCallbacks
   60     def tearDown(self):
   61         yield self.master.stopService()
   62 
   63     @defer.inlineCallbacks
   64     def setupBuildResults(self, buildResults):
   65         self.insertTestData([buildResults], buildResults)
   66         build = yield self.master.data.get(("builds", 20))
   67         return build
   68 
   69     def _check_start_and_finish_build(self, build):
   70         # we make sure proper calls to txrequests have been made
   71         self._http.expect(
   72             'post',
   73             '/rest/build-status/1.0/commits/d34db33fd43db33f',
   74             json={'url': 'http://localhost:8080/#builders/79/builds/0',
   75                   'state': 'INPROGRESS', 'key': 'Builder0',
   76                   'description': 'Build started.'},
   77             code=HTTP_PROCESSED)
   78         self._http.expect(
   79             'post',
   80             '/rest/build-status/1.0/commits/d34db33fd43db33f',
   81             json={'url': 'http://localhost:8080/#builders/79/builds/0',
   82                   'state': 'SUCCESSFUL', 'key': 'Builder0',
   83                   'description': 'Build done.'},
   84             code=HTTP_PROCESSED)
   85         self._http.expect(
   86             'post',
   87             '/rest/build-status/1.0/commits/d34db33fd43db33f',
   88             json={'url': 'http://localhost:8080/#builders/79/builds/0',
   89                   'state': 'FAILED', 'key': 'Builder0',
   90                   'description': 'Build done.'})
   91         build['complete'] = False
   92         self.sp.buildStarted(("build", 20, "started"), build)
   93         build['complete'] = True
   94         self.sp.buildFinished(("build", 20, "finished"), build)
   95         build['results'] = FAILURE
   96         self.sp.buildFinished(("build", 20, "finished"), build)
   97 
   98     @defer.inlineCallbacks
   99     def test_basic(self):
  100         self.setupReporter()
  101         build = yield self.setupBuildResults(SUCCESS)
  102         self._check_start_and_finish_build(build)
  103 
  104     @defer.inlineCallbacks
  105     def test_setting_options(self):
  106         self.setupReporter(statusName='Build', startDescription='Build started.',
  107                            endDescription='Build finished.')
  108         build = yield self.setupBuildResults(SUCCESS)
  109         # we make sure proper calls to txrequests have been made
  110         self._http.expect(
  111             'post',
  112             '/rest/build-status/1.0/commits/d34db33fd43db33f',
  113             json={'url': 'http://localhost:8080/#builders/79/builds/0',
  114                   'state': 'INPROGRESS', 'key': 'Builder0',
  115                   'name': 'Build', 'description': 'Build started.'},
  116             code=HTTP_PROCESSED)
  117         self._http.expect(
  118             'post',
  119             '/rest/build-status/1.0/commits/d34db33fd43db33f',
  120             json={'url': 'http://localhost:8080/#builders/79/builds/0',
  121                   'state': 'SUCCESSFUL', 'key': 'Builder0',
  122                   'name': 'Build', 'description': 'Build finished.'},
  123             code=HTTP_PROCESSED)
  124         self._http.expect(
  125             'post',
  126             '/rest/build-status/1.0/commits/d34db33fd43db33f',
  127             json={'url': 'http://localhost:8080/#builders/79/builds/0',
  128                   'state': 'FAILED', 'key': 'Builder0',
  129                   'name': 'Build', 'description': 'Build finished.'},
  130             code=HTTP_PROCESSED)
  131         build['complete'] = False
  132         self.sp.buildStarted(("build", 20, "started"), build)
  133         build['complete'] = True
  134         self.sp.buildFinished(("build", 20, "finished"), build)
  135         build['results'] = FAILURE
  136         self.sp.buildFinished(("build", 20, "finished"), build)
  137 
  138     @defer.inlineCallbacks
  139     def test_error(self):
  140         self.setupReporter()
  141         build = yield self.setupBuildResults(SUCCESS)
  142         # we make sure proper calls to txrequests have been made
  143         self._http.expect(
  144             'post',
  145             '/rest/build-status/1.0/commits/d34db33fd43db33f',
  146             json={'url': 'http://localhost:8080/#builders/79/builds/0',
  147                   'state': 'INPROGRESS', 'key': 'Builder0',
  148                   'description': 'Build started.'},
  149             code=HTTP_NOT_FOUND,
  150             content_json={
  151                 "error_description": "This commit is unknown to us",
  152                 "error": "invalid_commit"})
  153         build['complete'] = False
  154         self.setUpLogging()
  155         self.sp.buildStarted(("build", 20, "started"), build)
  156         self.assertLogged('404: Unable to send Bitbucket Server status')
  157 
  158     @defer.inlineCallbacks
  159     def test_basic_with_no_revision(self):
  160         yield self.setupReporter()
  161         old_test_revision = self.TEST_REVISION
  162         try:
  163             self.TEST_REVISION = None
  164             build = yield self.setupBuildResults(SUCCESS)
  165         finally:
  166             self.TEST_REVISION = old_test_revision
  167         self.setUpLogging()
  168         # we don't expect any request
  169         build['complete'] = False
  170         self.sp.buildStarted(("build", 20, "started"), build)
  171         self.assertLogged("Unable to get the commit hash")
  172         build['complete'] = True
  173         self.sp.buildFinished(("build", 20, "finished"), build)
  174         build['results'] = FAILURE
  175         self.sp.buildFinished(("build", 20, "finished"), build)
  176 
  177 
  178 UNICODE_BODY = "body: \u00E5\u00E4\u00F6 text"
  179 EXPECTED_API = '/rest/api/1.0/projects/PRO/repos/myrepo/pull-requests/20/comments'
  180 PR_URL = "http://example.com/projects/PRO/repos/myrepo/pull-requests/20"
  181 
  182 
  183 class TestBitbucketServerPRCommentPush(TestReactorMixin, unittest.TestCase,
  184                                        NotifierTestMixin, LoggingMixin):
  185 
  186     @defer.inlineCallbacks
  187     def setUp(self):
  188         self.setUpTestReactor()
  189         # ignore config error if txrequests is not installed
  190         self.patch(config, '_errors', Mock())
  191         self.master = fakemaster.make_master(self, wantData=True, wantDb=True,
  192                                              wantMq=True)
  193         yield self.master.startService()
  194 
  195     @defer.inlineCallbacks
  196     def setupReporter(self, verbose=True, **kwargs):
  197         self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
  198             self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
  199             verify=None)
  200         self.cp = BitbucketServerPRCommentPush(
  201             "serv", Interpolate("username"), Interpolate("passwd"), verbose=verbose, **kwargs)
  202         self.cp.setServiceParent(self.master)
  203         self.cp.messageFormatter = Mock(spec=self.cp.messageFormatter)
  204         self.cp.messageFormatter.formatMessageForBuildResults.return_value = \
  205             {"body": UNICODE_BODY, "type": "text"}
  206 
  207     @defer.inlineCallbacks
  208     def tearDown(self):
  209         yield self.master.stopService()
  210 
  211     @defer.inlineCallbacks
  212     def setupBuildResults(self, buildResults, set_pr=True):
  213         buildset, builds = yield super().setupBuildResults(buildResults)
  214         if set_pr:
  215             yield self.master.db.builds.setBuildProperty(
  216                 20, "pullrequesturl", PR_URL, "test")
  217         return (buildset, builds)
  218 
  219     @defer.inlineCallbacks
  220     def test_reporter_basic(self):
  221         yield self.setupReporter()
  222         _, builds = yield self.setupBuildResults(SUCCESS)
  223         build = builds[0]
  224         self._http.expect(
  225             "post",
  226             EXPECTED_API,
  227             json={"text": UNICODE_BODY},
  228             code=HTTP_CREATED)
  229         build["complete"] = True
  230         self.setUpLogging()
  231         yield self.cp.buildComplete(("build", 20, "finished"), build)
  232         self.assertLogged('Comment sent to {}'.format(PR_URL))
  233 
  234     @defer.inlineCallbacks
  235     def test_reporter_basic_without_logging(self):
  236         yield self.setupReporter(verbose=False)
  237         _, builds = yield self.setupBuildResults(SUCCESS)
  238         build = builds[0]
  239         self._http.expect(
  240             "post",
  241             EXPECTED_API,
  242             json={"text": UNICODE_BODY},
  243             code=HTTP_CREATED)
  244         build["complete"] = True
  245         self.setUpLogging()
  246         yield self.cp.buildComplete(("build", 20, "finished"), build)
  247 
  248         self.assertNotLogged('Comment sent to {}'.format(PR_URL))
  249 
  250     @defer.inlineCallbacks
  251     def test_reporter_without_pullrequest(self):
  252         yield self.setupReporter()
  253         _, builds = yield self.setupBuildResults(SUCCESS, set_pr=False)
  254         build = builds[0]
  255         build["complete"] = True
  256         # we don't expect any request
  257         yield self.cp.buildComplete(("builds", 20, "finished"), build)
  258 
  259     @defer.inlineCallbacks
  260     def test_missing_worker_does_nothing(self):
  261         yield self.setupReporter()
  262         self.cp.workerMissing(("workers", 13, "missing"), 13)
  263 
  264     @defer.inlineCallbacks
  265     def test_reporter_with_buildset(self):
  266         yield self.setupReporter(buildSetSummary=True)
  267         buildset, _ = yield self.setupBuildResults(SUCCESS)
  268         self._http.expect(
  269             "post",
  270             EXPECTED_API,
  271             json={"text": UNICODE_BODY},
  272             code=HTTP_CREATED)
  273         yield self.cp.buildsetComplete(("buildsets", 20, "complete"), buildset)
  274 
  275     @defer.inlineCallbacks
  276     def test_reporter_logs_error_code_and_content_on_invalid_return_code(self):
  277         yield self.setupReporter()
  278         _, builds = yield self.setupBuildResults(SUCCESS)
  279         build = builds[0]
  280 
  281         http_error_code = 500
  282         error_body = {"errors": [
  283             {"message": "A dataXXXbase error has occurred."}]}
  284 
  285         self._http.expect(
  286             "post",
  287             EXPECTED_API,
  288             json={"text": UNICODE_BODY},
  289             code=http_error_code,
  290             content_json=error_body)
  291         self.setUpLogging()
  292         build['complete'] = True
  293         yield self.cp.buildComplete(("builds", 20, "finished"), build)
  294 
  295         self.assertLogged(
  296             "^{}: Unable to send a comment: ".format(http_error_code))
  297         self.assertLogged("A dataXXXbase error has occurred")
  298 
  299     @defer.inlineCallbacks
  300     def test_reporter_logs_error_code_without_content_on_invalid_return_code(self):
  301         yield self.setupReporter()
  302         _, builds = yield self.setupBuildResults(SUCCESS)
  303         build = builds[0]
  304         http_error_code = 503
  305         self._http.expect(
  306             "post",
  307             EXPECTED_API,
  308             json={"text": UNICODE_BODY},
  309             code=http_error_code)
  310         self.setUpLogging()
  311         build['complete'] = True
  312         yield self.cp.buildComplete(("builds", 20, "finished"), build)
  313         self.assertLogged("^{}: Unable to send a comment: ".format(
  314             http_error_code))
  315 
  316     @defer.inlineCallbacks
  317     def test_reporter_does_not_log_return_code_on_valid_return_code(
  318             self):
  319         yield self.setupReporter()
  320         _, builds = yield self.setupBuildResults(SUCCESS)
  321         build = builds[0]
  322         http_code = 201
  323         self._http.expect(
  324             "post",
  325             EXPECTED_API,
  326             json={"text": UNICODE_BODY},
  327             code=http_code)
  328         self.setUpLogging()
  329         build['complete'] = True
  330         yield self.cp.buildComplete(("builds", 20, "finished"), build)
  331         self.assertNotLogged("^{}:".format(http_code))