Skip to content

Commit

Permalink
Don't try to encode() byte arrays.
Browse files Browse the repository at this point in the history
byte arrays don't have an encode() method, and in Python 2 it
doesn't make sense to call encode() on a str either. The mocks on
this function were returning incorrect types in Python 3 (a str
instead of a byte).

This commit fixes the mocks to return correct types in both Python
versions, and fixes the composer so that it does not try to call
encode() on those responses.

fixes #2746

Signed-off-by: Randy Barlow <[email protected]>
(cherry picked from commit fa5524c)
  • Loading branch information
bowlofeggs authored and mergify[bot] committed Nov 15, 2018
1 parent a36922d commit c1172d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bodhi/server/consumers/masher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ def _wait_for_sync(self):
self.log.exception('Error fetching repomd.xml')
time.sleep(200)
continue
newsum = hashlib.sha1(masterrepomd.read().encode('utf-8')).hexdigest()
newsum = hashlib.sha1(masterrepomd.read()).hexdigest()
if newsum == checksum:
self.log.info("master repomd.xml matches!")
notifications.publish(
Expand Down
35 changes: 17 additions & 18 deletions bodhi/tests/server/consumers/test_masher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import six
import six.moves.urllib.parse as urlparse
from six.moves.urllib.error import HTTPError, URLError
from six import StringIO

from bodhi.server import buildsys, exceptions, log, push
from bodhi.server.config import config
Expand Down Expand Up @@ -2927,12 +2926,12 @@ class TestPungiComposerThread__wait_for_sync(ComposerThreadBaseTestCase):
@mock.patch('bodhi.server.consumers.masher.notifications.publish')
@mock.patch('bodhi.server.consumers.masher.time.sleep',
mock.MagicMock(side_effect=Exception('This should not happen during this test.')))
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen',
return_value=StringIO('---\nyaml: rules'))
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen')
def test_checksum_match_immediately(self, urlopen, publish, save):
"""
Assert correct operation when the repomd checksum matches immediately.
"""
urlopen.return_value.read.return_value = b'---\nyaml: rules'
t = PungiComposerThread(self.semmock, self._make_msg()['body']['msg']['composes'][0],
'bowlofeggs', log, self.Session, self.tempdir)
t.compose = self.db.query(Compose).one()
Expand Down Expand Up @@ -2972,12 +2971,12 @@ def test_checksum_match_immediately(self, urlopen, publish, save):
@mock.patch('bodhi.server.consumers.masher.notifications.publish')
@mock.patch('bodhi.server.consumers.masher.time.sleep',
mock.MagicMock(side_effect=Exception('This should not happen during this test.')))
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen',
return_value=StringIO('---\nyaml: rules'))
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen')
def test_no_checkarch(self, urlopen, publish, save):
"""
Assert error when no checkarch is found.
"""
urlopen.return_value.read.return_value = b'---\nyaml: rules'
t = PungiComposerThread(self.semmock, self._make_msg()['body']['msg']['composes'][0],
'bowlofeggs', log, self.Session, self.tempdir)
t.compose = self.db.query(Compose).one()
Expand All @@ -3003,13 +3002,12 @@ def test_no_checkarch(self, urlopen, publish, save):
@mock.patch('bodhi.server.consumers.masher.PungiComposerThread.save_state')
@mock.patch('bodhi.server.consumers.masher.notifications.publish')
@mock.patch('bodhi.server.consumers.masher.time.sleep')
@mock.patch(
'bodhi.server.consumers.masher.urllib2.urlopen',
side_effect=[StringIO('wrong'), StringIO('nope'), StringIO('---\nyaml: rules')])
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen')
def test_checksum_match_third_try(self, urlopen, sleep, publish, save):
"""
Assert correct operation when the repomd checksum matches on the third try.
"""
urlopen.return_value.read.side_effect = [b'wrong', b'nope', b'---\nyaml: rules']
t = PungiComposerThread(self.semmock, self._make_msg()['body']['msg']['composes'][0],
'bowlofeggs', log, self.Session, self.tempdir)
t.compose = self.db.query(Compose).one()
Expand All @@ -3035,8 +3033,9 @@ def test_checksum_match_third_try(self, urlopen, sleep, publish, save):
arch = 'x86_64' if 'x86_64' in urlopen.mock_calls[0][1][0] else 'aarch64'
expected_calls = [
mock.call('http://example.com/pub/fedora/linux/updates/testing/17/'
'{}/repodata.repomd.xml'.format(arch))
for i in range(3)]
'{}/repodata.repomd.xml'.format(arch)),
mock.call().read()]
expected_calls = expected_calls * 3
urlopen.assert_has_calls(expected_calls)
sleep.assert_has_calls([mock.call(200), mock.call(200)])
save.assert_called_with(ComposeState.syncing_repo)
Expand All @@ -3048,14 +3047,14 @@ def test_checksum_match_third_try(self, urlopen, sleep, publish, save):
@mock.patch('bodhi.server.consumers.masher.PungiComposerThread.save_state')
@mock.patch('bodhi.server.consumers.masher.notifications.publish')
@mock.patch('bodhi.server.consumers.masher.time.sleep')
@mock.patch(
'bodhi.server.consumers.masher.urllib2.urlopen',
side_effect=[HTTPError('url', 404, 'Not found', {}, None),
StringIO('---\nyaml: rules')])
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen')
def test_httperror(self, urlopen, sleep, publish, save):
"""
Assert that an HTTPError is properly caught and logged, and that the algorithm continues.
"""
fake_url = mock.MagicMock()
fake_url.read.return_value = b'---\nyaml: rules'
urlopen.side_effect = [HTTPError('url', 404, 'Not found', {}, None), fake_url]
t = PungiComposerThread(self.semmock, self._make_msg()['body']['msg']['composes'][0],
'bowlofeggs', log, self.Session, self.tempdir)
t.compose = self.db.query(Compose).one()
Expand Down Expand Up @@ -3157,14 +3156,14 @@ def test_missing_repomd(self, publish, save):
@mock.patch('bodhi.server.consumers.masher.PungiComposerThread.save_state')
@mock.patch('bodhi.server.consumers.masher.notifications.publish')
@mock.patch('bodhi.server.consumers.masher.time.sleep')
@mock.patch(
'bodhi.server.consumers.masher.urllib2.urlopen',
side_effect=[URLError('it broke'),
StringIO('---\nyaml: rules')])
@mock.patch('bodhi.server.consumers.masher.urllib2.urlopen')
def test_urlerror(self, urlopen, sleep, publish, save):
"""
Assert that a URLError is properly caught and logged, and that the algorithm continues.
"""
fake_url = mock.MagicMock()
fake_url.read.return_value = b'---\nyaml: rules'
urlopen.side_effect = [URLError('it broke'), fake_url]
t = PungiComposerThread(self.semmock, self._make_msg()['body']['msg']['composes'][0],
'bowlofeggs', log, self.Session, self.tempdir)
t.compose = self.db.query(Compose).one()
Expand Down

0 comments on commit c1172d6

Please sign in to comment.