Merge "Fix file_has_content function for Py3"

This commit is contained in:
Jenkins 2017-03-18 06:08:37 +00:00 committed by Gerrit Code Review
commit f65067fe81
2 changed files with 5 additions and 3 deletions

View File

@ -295,7 +295,7 @@ def file_has_content(path, content, hash_algo='md5'):
:returns: True if the hash of reference content is the same as
the hash of file's content, False otherwise
"""
with open(path) as existing:
with open(path, 'rb') as existing:
file_hash_hex = hash_file(existing, hash_algo=hash_algo)
ref_hash = _get_hash_object(hash_algo)
ref_hash.update(content)

View File

@ -250,15 +250,17 @@ class GenericUtilsTestCase(base.TestCase):
data = b'Mary had a little lamb, its fleece as white as snow'
ref = data
with mock.patch('ironic.common.utils.open',
mock.mock_open(read_data=data)):
mock.mock_open(read_data=data)) as mopen:
self.assertTrue(utils.file_has_content('foo', ref))
mopen.assert_called_once_with('foo', 'rb')
def test_file_has_content_differ(self):
data = b'Mary had a little lamb, its fleece as white as snow'
ref = data + b'!'
with mock.patch('ironic.common.utils.open',
mock.mock_open(read_data=data)):
mock.mock_open(read_data=data)) as mopen:
self.assertFalse(utils.file_has_content('foo', ref))
mopen.assert_called_once_with('foo', 'rb')
def test_is_valid_datapath_id(self):
self.assertTrue(utils.is_valid_datapath_id("525400cf2d319fdf"))