diff --git a/swh/objstorage/backends/pathslicing.py b/swh/objstorage/backends/pathslicing.py --- a/swh/objstorage/backends/pathslicing.py +++ b/swh/objstorage/backends/pathslicing.py @@ -267,7 +267,7 @@ checksums = hashutil.MultiHash.from_file( f, hash_names=[ID_HASH_ALGO], length=length).digest() actual_obj_id = checksums[ID_HASH_ALGO] - if obj_id != actual_obj_id: + if hex_obj_id != actual_obj_id.hex(): raise Error( 'Corrupt object %s should have id %s' % (hashutil.hash_to_hex(obj_id), @@ -275,7 +275,8 @@ ) except (OSError, IOError): # IOError is for compatibility with older python versions - raise Error('Corrupt object %s is not a gzip file' % obj_id) + raise Error('Corrupt object %s is not a gzip file' % + hashutil.hash_to_hex(obj_id)) def delete(self, obj_id): super().delete(obj_id) # Check delete permission diff --git a/swh/objstorage/tests/test_objstorage_pathslicing.py b/swh/objstorage/tests/test_objstorage_pathslicing.py --- a/swh/objstorage/tests/test_objstorage_pathslicing.py +++ b/swh/objstorage/tests/test_objstorage_pathslicing.py @@ -6,6 +6,7 @@ import shutil import tempfile import unittest +import gzip from swh.model import hashutil from swh.objstorage import exc, get_objstorage, ID_HASH_LENGTH @@ -44,21 +45,34 @@ self.storage.add(content, obj_id=obj_id) self.assertEqual(len(self.storage), 1) + def test_check_ok(self): + content, obj_id = self.hash_content(b'check_ok') + self.storage.add(content, obj_id=obj_id) + self.storage.check(obj_id) + self.storage.check(obj_id.hex()) + def test_check_not_gzip(self): content, obj_id = self.hash_content(b'check_not_gzip') self.storage.add(content, obj_id=obj_id) with open(self.content_path(obj_id), 'ab') as f: # Add garbage. f.write(b'garbage') - with self.assertRaises(exc.Error): + with self.assertRaises(exc.Error) as error: self.storage.check(obj_id) + self.assertEquals(( + 'Corrupt object %s is not a gzip file' % obj_id.hex(),), + error.exception.args) def test_check_id_mismatch(self): content, obj_id = self.hash_content(b'check_id_mismatch') self.storage.add(content, obj_id=obj_id) - with open(self.content_path(obj_id), 'wb') as f: + with gzip.open(self.content_path(obj_id), 'wb') as f: f.write(b'unexpected content') - with self.assertRaises(exc.Error): + with self.assertRaises(exc.Error) as error: self.storage.check(obj_id) + self.assertEquals(( + 'Corrupt object %s should have id ' + '12ebb2d6c81395bcc5cab965bdff640110cb67ff' % obj_id.hex(),), + error.exception.args) def test_get_random_contents(self): content, obj_id = self.hash_content(b'get_random_content')