buildbot-transifex/buildbot_gitea/test/test_reporter.py

217 lines
8.5 KiB
Python
Raw Normal View History

2018-09-27 14:46:35 +02:00
# Based on TestGitLabStatusPush from buildbot
from __future__ import absolute_import
from __future__ import print_function
from mock import Mock
from twisted.internet import defer
from twisted.trial import unittest
from buildbot import config
from buildbot.process.properties import Interpolate
from buildbot.process.results import FAILURE
from buildbot.process.results import SUCCESS
from buildbot_gitea.reporter import GiteaStatusPush
from buildbot.test.fake import fakemaster
from buildbot.test.fake import httpclientservice as fakehttpclientservice
from buildbot.test.util import logging
from buildbot.test.util.reporter import ReporterTestMixin
from buildbot.test.util.misc import TestReactorMixin
2018-09-27 14:46:35 +02:00
class TestGiteaStatusPush(
unittest.TestCase,
ReporterTestMixin,
logging.LoggingMixin,
TestReactorMixin):
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def setUp(self):
self.setUpTestReactor()
2021-03-09 20:37:00 +01:00
self.setup_reporter_test()
# repository must be in the form http://gitea/<owner>/<project>
self.reporter_test_repo = u'http://gitea/buildbot/buildbot'
2018-09-27 14:46:35 +02:00
# 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()
2021-03-09 20:37:00 +01:00
self._http = yield fakehttpclientservice.HTTPClientService.getService(
2018-09-27 14:46:35 +02:00
self.master, self,
"http://gitea", headers={'Authorization': 'token XXYYZZ'},
2018-09-27 14:46:35 +02:00
debug=None, verify=None)
2021-03-09 20:37:00 +01:00
self.sp = GiteaStatusPush("http://gitea/", Interpolate('XXYYZZ'))
2018-09-27 14:46:35 +02:00
2021-03-09 20:37:00 +01:00
yield self.sp.setServiceParent(self.master)
2018-09-27 14:46:35 +02:00
def tearDown(self):
return self.master.stopService()
def setupProps(self):
2021-03-09 20:37:00 +01:00
self.reporter_test_props['owner'] = "buildbot"
self.reporter_test_props['repository_name'] = "buildbot"
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def setupBuildResults(self, buildResults):
self.insertTestData([buildResults], buildResults)
build = yield self.master.data.get(("builds", 20))
defer.returnValue(build)
@defer.inlineCallbacks
def test_basic(self):
self.setupProps()
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'pending',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build started.', 'context': 'buildbot/Builder0'})
2018-09-27 14:46:35 +02:00
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'success',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build done.', 'context': 'buildbot/Builder0'})
2018-09-27 14:46:35 +02:00
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'failure',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build done.', 'context': 'buildbot/Builder0'})
2018-09-27 14:46:35 +02:00
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'new'), build)
2018-09-27 14:46:35 +02:00
build['complete'] = True
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'finished'), build)
2018-09-27 14:46:35 +02:00
build['results'] = FAILURE
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'finished'), build)
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def test_pullrequest(self):
self.setupProps()
self.reporter_test_props["pr_id"] = 42
self.reporter_test_props["head_owner"] = 'foo'
self.reporter_test_props["head_reponame"] = 'bar'
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
'/api/v1/repos/foo/bar/statuses/d34db33fd43db33f',
json={'state': 'success',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
'description': 'Build done.', 'context': 'buildbot/pull_request/Builder0'})
build['complete'] = True
self.sp._got_event(('builds', 20, 'finished'), build)
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def test_sshurl(self):
self.setupProps()
self.TEST_REPO = u'git@gitea:buildbot/buildbot.git'
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'pending',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build started.', 'context': 'buildbot/Builder0'})
2018-09-27 14:46:35 +02:00
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'new'), build)
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def test_sshurl_noprops(self):
2021-03-09 20:37:00 +01:00
self.reporter_test_repo = u'git@gitea:buildbot/buildbot.git'
build = yield self.setupBuildResults(SUCCESS)
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'pending',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build started.', 'context': 'buildbot/Builder0'})
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'new'), build)
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def test_noowner(self):
self.setUpLogging()
self.setupProps()
2021-03-09 20:37:00 +01:00
del self.reporter_test_props["owner"]
2018-09-27 14:46:35 +02:00
self.TEST_REPO = u''
build = yield self.setupBuildResults(SUCCESS)
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'new'), build)
2018-09-27 14:46:35 +02:00
# implicit check that no http request is done
self.assertLogged("Could not send status, "
"build has no owner property for Gitea.")
@defer.inlineCallbacks
def test_noreponame(self):
self.setUpLogging()
self.setupProps()
2021-03-09 20:37:00 +01:00
del self.reporter_test_props["repository_name"]
2018-09-27 14:46:35 +02:00
self.TEST_REPO = u''
build = yield self.setupBuildResults(SUCCESS)
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(('builds', 20, 'new'), build)
2018-09-27 14:46:35 +02:00
# implicit check that no http request is done
self.assertLogged("Could not send status, "
"build has no repository_name property for Gitea.")
@defer.inlineCallbacks
def test_senderror(self):
self.setupProps()
self.setUpLogging()
2021-03-09 20:37:00 +01:00
build = yield self.insert_build_new()
2018-09-27 14:46:35 +02:00
# we make sure proper calls to txrequests have been made
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={'state': 'pending',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
2018-09-27 20:57:39 +02:00
'description': 'Build started.', 'context': 'buildbot/Builder0'},
content_json={
"message": "sha1 not found: d34db33fd43db33f",
"url": "https://godoc.org/github.com/go-gitea/go-sdk/gitea"
},
code=500)
2018-09-27 14:46:35 +02:00
build['complete'] = False
2021-03-09 20:37:00 +01:00
self.sp._got_event(("builds", 20, "new"), build)
2018-09-27 14:46:35 +02:00
self.assertLogged(
"Could not send status \"pending\" for "
"http://gitea/buildbot/buildbot at d34db33fd43db33f:"
" 500 : sha1 not found: d34db33fd43db33f")
2018-09-27 14:46:35 +02:00
@defer.inlineCallbacks
def test_badchange(self):
self.setupProps()
self.setUpLogging()
2021-03-09 20:37:00 +01:00
build = yield self.insert_build_new()
2018-09-27 14:46:35 +02:00
# we make sure proper calls to txrequests have been made
2021-03-09 20:37:00 +01:00
self._http.expect(
'post',
'/api/v1/repos/buildbot/buildbot/statuses/d34db33fd43db33f',
json={
'state': 'pending',
'description': 'Build started.',
'target_url': 'http://localhost:8080/#builders/79/builds/0',
'context': 'buildbot/Builder0'
},
content_json={"message": "Not found"},
code=404,
)
2018-09-27 14:46:35 +02:00
build['complete'] = False
2021-03-09 20:37:00 +01:00
yield self.sp._got_event(("builds", 20, "new"), build)
self.assertLogged("Could not send status \"pending\" for"
2018-09-27 14:46:35 +02:00
" http://gitea/buildbot/buildbot at d34db33fd43db33f")
self.flushLoggedErrors(AssertionError)