Пример #1
0
    def setupReporter(self,
                      verbose=True,
                      generator_class=BuildStatusGenerator,
                      **kwargs):
        self._http = yield fakehttpclientservice.HTTPClientService.getService(
            self.master,
            self,
            'serv',
            auth=('username', 'passwd'),
            debug=None,
            verify=None)

        formatter = Mock(spec=MessageFormatter)
        formatter.format_message_for_build.return_value = {
            "body": UNICODE_BODY,
            "type": "text",
            "subject": "subject"
        }
        formatter.wantProperties = True
        formatter.wantSteps = False
        formatter.wantLogs = False

        generator = generator_class(message_formatter=formatter)

        self.cp = BitbucketServerPRCommentPush("serv",
                                               Interpolate("username"),
                                               Interpolate("passwd"),
                                               verbose=verbose,
                                               generators=[generator],
                                               **kwargs)
        yield self.cp.setServiceParent(self.master)
 def setupReporter(self, verbose=True, **kwargs):
     self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
         self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
         verify=None)
     self.cp = BitbucketServerPRCommentPush(
         "serv", Interpolate("username"), Interpolate("passwd"), verbose=verbose, **kwargs)
     self.cp.setServiceParent(self.master)
     self.cp.messageFormatter = Mock(spec=self.cp.messageFormatter)
     self.cp.messageFormatter.formatMessageForBuildResults.return_value = \
         {"body": UNICODE_BODY, "type": "text"}
 def setupReporter(self, verbose=True, **kwargs):
     self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
         self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
         verify=None)
     self.cp = BitbucketServerPRCommentPush(
         "serv", Interpolate("username"), Interpolate("passwd"), verbose=verbose, **kwargs)
     self.cp.setServiceParent(self.master)
     self.cp.messageFormatter = Mock(spec=self.cp.messageFormatter)
     self.cp.messageFormatter.formatMessageForBuildResults.return_value = \
         {"body": UNICODE_BODY, "type": "text"}
class TestBitbucketServerPRCommentPush(unittest.TestCase, NotifierTestMixin,
                                       LoggingMixin):
    @defer.inlineCallbacks
    def setUp(self):
        # 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)
        yield self.master.startService()

    @defer.inlineCallbacks
    def setupReporter(self, verbose=True, **kwargs):
        self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
            self.master,
            self,
            'serv',
            auth=('username', 'passwd'),
            debug=None,
            verify=None)
        self.cp = BitbucketServerPRCommentPush("serv",
                                               Interpolate("username"),
                                               Interpolate("passwd"),
                                               verbose=verbose,
                                               **kwargs)
        self.cp.setServiceParent(self.master)
        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:
            yield 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()
        yield self.cp.buildComplete(("build", 20, "finished"), build)
        self.assertLogged(
            unicode2NativeString(u'Comment sent to {}'.format(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()
        yield self.cp.buildComplete(("build", 20, "finished"), build)

        self.assertNotLogged(
            unicode2NativeString(u'Comment sent to {}'.format(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
        yield 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
        yield 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)
        yield 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
        yield 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
        yield 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
        yield self.cp.buildComplete(("builds", 20, "finished"), build)
        self.assertNotLogged("^{}:".format(http_code))
Пример #5
0
class TestBitbucketServerPRCommentPush(TestReactorMixin, unittest.TestCase,
                                       ReporterTestMixin, LoggingMixin):

    @defer.inlineCallbacks
    def setUp(self):
        self.setUpTestReactor()
        self.setup_reporter_test()
        # ignore config error if txrequests is not installed
        self.patch(config, '_errors', Mock())
        self.master = fakemaster.make_master(self, wantData=True, wantDb=True,
                                             wantMq=True)
        yield self.master.startService()

    @defer.inlineCallbacks
    def setupReporter(self, verbose=True, generator_class=BuildStatusGenerator, **kwargs):
        self._http = yield fakehttpclientservice.HTTPClientService.getService(
            self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
            verify=None)

        formatter = Mock(spec=MessageFormatter)
        formatter.format_message_for_build.return_value = {
            "body": UNICODE_BODY,
            "type": "text",
            "subject": "subject"
        }
        formatter.wantProperties = True
        formatter.wantSteps = False
        formatter.wantLogs = False

        generator = generator_class(message_formatter=formatter)

        self.cp = BitbucketServerPRCommentPush("serv", Interpolate("username"),
                                               Interpolate("passwd"), verbose=verbose,
                                               generators=[generator], **kwargs)
        yield self.cp.setServiceParent(self.master)

    @defer.inlineCallbacks
    def tearDown(self):
        yield self.master.stopService()

    @defer.inlineCallbacks
    def setupBuildResults(self, buildResults, set_pr=True):
        yield super().insertTestData([buildResults], buildResults)
        build = yield self.master.data.get(('builds', 20))
        if set_pr:
            yield self.master.db.builds.setBuildProperty(
                20, "pullrequesturl", PR_URL, "test")
        return build

    @defer.inlineCallbacks
    def test_reporter_basic(self):
        yield self.setupReporter()
        build = yield self.setupBuildResults(SUCCESS)
        self._http.expect(
            "post",
            EXPECTED_API,
            json={"text": UNICODE_BODY},
            code=HTTP_CREATED)
        build["complete"] = True
        self.setUpLogging()
        yield self.cp._got_event(('builds', 20, 'finished'), build)
        self.assertLogged('Comment sent to {}'.format(PR_URL))

    @defer.inlineCallbacks
    def test_reporter_basic_without_logging(self):
        yield self.setupReporter(verbose=False)
        build = yield self.setupBuildResults(SUCCESS)
        self._http.expect(
            "post",
            EXPECTED_API,
            json={"text": UNICODE_BODY},
            code=HTTP_CREATED)
        build["complete"] = True
        self.setUpLogging()
        yield self.cp._got_event(('builds', 20, 'finished'), build)

        self.assertNotLogged('Comment sent to {}'.format(PR_URL))

    @defer.inlineCallbacks
    def test_reporter_without_pullrequest(self):
        yield self.setupReporter()
        build = yield self.setupBuildResults(SUCCESS, set_pr=False)
        build["complete"] = True
        # we don't expect any request
        yield self.cp._got_event(('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(generator_class=BuildSetStatusGenerator)
        yield self.setupBuildResults(SUCCESS)
        buildset = yield self.master.data.get(('buildsets', 98))
        self._http.expect(
            "post",
            EXPECTED_API,
            json={"text": UNICODE_BODY},
            code=HTTP_CREATED)
        yield self.cp._got_event(("buildsets", 98, "complete"), buildset)

    @defer.inlineCallbacks
    def test_reporter_logs_error_code_and_content_on_invalid_return_code(self):
        yield self.setupReporter()
        build = yield self.setupBuildResults(SUCCESS)

        http_error_code = 500
        error_body = {"errors": [
            {"message": "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
        yield self.cp._got_event(('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()
        build = yield self.setupBuildResults(SUCCESS)
        http_error_code = 503
        self._http.expect(
            "post",
            EXPECTED_API,
            json={"text": UNICODE_BODY},
            code=http_error_code)
        self.setUpLogging()
        build['complete'] = True
        yield self.cp._got_event(('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()
        build = yield self.setupBuildResults(SUCCESS)
        http_code = 201
        self._http.expect(
            "post",
            EXPECTED_API,
            json={"text": UNICODE_BODY},
            code=http_code)
        self.setUpLogging()
        build['complete'] = True
        yield self.cp._got_event(('builds', 20, 'finished'), build)
        self.assertNotLogged("^{}:".format(http_code))
class TestBitbucketServerPRCommentPush(unittest.TestCase, NotifierTestMixin, LoggingMixin):

    @defer.inlineCallbacks
    def setUp(self):
        # 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)
        yield self.master.startService()

    @defer.inlineCallbacks
    def setupReporter(self, verbose=True, **kwargs):
        self._http = yield fakehttpclientservice.HTTPClientService.getFakeService(
            self.master, self, 'serv', auth=('username', 'passwd'), debug=None,
            verify=None)
        self.cp = BitbucketServerPRCommentPush(
            "serv", Interpolate("username"), Interpolate("passwd"), verbose=verbose, **kwargs)
        self.cp.setServiceParent(self.master)
        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 super().setupBuildResults(buildResults)
        if set_pr:
            yield self.master.db.builds.setBuildProperty(
                20, "pullrequesturl", PR_URL, "test")
        return (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()
        yield self.cp.buildComplete(("build", 20, "finished"), build)
        self.assertLogged('Comment sent to {}'.format(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()
        yield self.cp.buildComplete(("build", 20, "finished"), build)

        self.assertNotLogged('Comment sent to {}'.format(PR_URL))

    @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
        yield 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)
        yield 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 = {"errors": [
            {"message": "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
        yield 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
        yield 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
        yield self.cp.buildComplete(("builds", 20, "finished"), build)
        self.assertNotLogged("^{}:".format(http_code))