Skip to content

Commit

Permalink
Add tests for logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Apr 6, 2013
1 parent b2fc201 commit 05b1569
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion master/buildbot/status/github.py
Expand Up @@ -210,7 +210,7 @@ def _sendGitHubStatus(self, status):
error_message = (
'Fail to send status "%(state)s" for '
'%(repoOwner)s/%(repoName)s at %(sha)s.'
)
) % status
d.addCallback(log.msg, success_message)
d.addErrback(log.err, error_message)
return d
57 changes: 52 additions & 5 deletions master/buildbot/test/unit/test_status_github.py
Expand Up @@ -25,22 +25,29 @@
from buildbot.status.builder import SUCCESS, FAILURE
from buildbot.status.github import GitHubStatus
from buildbot.test.fake.fakebuild import FakeBuild
from buildbot.test.fake.sourcestamp import FakeSourceStamp
from buildbot.test.util import logging


class TestGitHubStatus(unittest.TestCase):
class TestGitHubStatus(unittest.TestCase, logging.LoggingMixin):
"""
Unit tests for `GitHubStatus`.
"""

def setUp(self):
super(TestGitHubStatus, self).setUp()
self.setUpLogging()
self.build = FakeBuild()
self.props = self.build.build_status.properties
self.sourcestamp = FakeSourceStamp()
self.status = GitHubStatus(
token='token', repoOwner='owner', repoName='name')

def tearDown(self):
self.assertEqual(
0,
len(self._logEvents),
'There are still logs not validated:\n%s' % self._logEvents,
)
super(TestGitHubStatus, self).tearDown()

def test_initialization_required_arguments(self):
"""
Status can be initialized by only specifying GitHub API token
Expand Down Expand Up @@ -225,6 +232,12 @@ def test_getGitHubRepoProperties_skip_no_sha(self):
d.addCallback(result.append)
self.assertEqual({}, result[0])

# Check log.
log_event = self._logEvents.pop()
self.assertFalse(log_event['isError'])
self.assertEqual(
('GitHubStatus: No revision found.',), log_event['message'])

def test_getGitHubRepoProperties_skip_no_owner(self):
self.status._repoOwner = Interpolate('')
self.status._repoName = Interpolate('name')
Expand Down Expand Up @@ -285,7 +298,7 @@ def test_getGitHubState(self):
self.assertEqual(
'error', self.status._getGitHubState('anything-else'))

def test_sendGitHubStatus(self):
def test_sendGitHubStatus_success(self):
"""
sendGitHubStatus will call the txgithub createStatus and encode
all data.
Expand Down Expand Up @@ -314,3 +327,37 @@ def test_sendGitHubStatus(self):
target_url='targetURL-resum\xc3\xa9',
description='description-resum\xc3\xa9',
)

# Check log.
log_event = self._logEvents.pop()
self.assertFalse(log_event['isError'])
self.assertEqual(
(None,
u'Status "state-resum\xe9" sent for '
u'owner-resum\xe9/name-resum\xe9 at sha-resum\xe9.'),
log_event['message'])

def test_sendGitHubStatus_error(self):
"""
sendGitHubStatus will log an error if txgithub sendGitHubStatus fails.
"""
status = {
'repoOwner': u'owner',
'repoName': u'name',
'sha': u'sha',
'state': u'state',
'targetURL': u'targetURL',
'description': u'description',
}
self.status._github.repos.createStatus = Mock(
return_value=defer.fail(AssertionError('fail-send-status')))

self.status._sendGitHubStatus(status)

# Check error log.
log_event = self._logEvents.pop()
self.assertTrue(log_event['isError'])
self.assertEqual(
u'Fail to send status "state" for owner/name at sha.',
log_event['why'],
)
3 changes: 2 additions & 1 deletion master/buildbot/test/util/logging.py
Expand Up @@ -29,4 +29,5 @@ def assertLogged(self, regexp):
msg = log.textFromEventDict(event)
if msg is not None and r.search(msg):
return
self.fail("%r not matched in log output" % regexp)
self.fail(
"%r not matched in log output.\n%s " % (regexp, self._logEvents))

0 comments on commit 05b1569

Please sign in to comment.