Merge "Quarantine malformed database schema SQLite errors"
This commit is contained in:
commit
351e4566bd
@ -331,6 +331,8 @@ class DatabaseBroker(object):
|
|||||||
"""
|
"""
|
||||||
if 'database disk image is malformed' in str(exc_value):
|
if 'database disk image is malformed' in str(exc_value):
|
||||||
exc_hint = 'malformed'
|
exc_hint = 'malformed'
|
||||||
|
elif 'malformed database schema' in str(exc_value):
|
||||||
|
exc_hint = 'malformed'
|
||||||
elif 'file is encrypted or is not a database' in str(exc_value):
|
elif 'file is encrypted or is not a database' in str(exc_value):
|
||||||
exc_hint = 'corrupted'
|
exc_hint = 'corrupted'
|
||||||
elif 'disk I/O error' in str(exc_value):
|
elif 'disk I/O error' in str(exc_value):
|
||||||
|
BIN
test/unit/common/malformed_schema_example.db
Normal file
BIN
test/unit/common/malformed_schema_example.db
Normal file
Binary file not shown.
@ -749,6 +749,22 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
exc = None
|
exc = None
|
||||||
|
try:
|
||||||
|
with broker.get() as conn:
|
||||||
|
conn.execute('SELECT * FROM test')
|
||||||
|
except Exception as err:
|
||||||
|
exc = err
|
||||||
|
self.assertEqual(
|
||||||
|
str(exc),
|
||||||
|
'Quarantined %s to %s due to malformed database' %
|
||||||
|
(dbpath, qpath))
|
||||||
|
# Test malformed schema database
|
||||||
|
copy(os.path.join(os.path.dirname(__file__),
|
||||||
|
'malformed_schema_example.db'),
|
||||||
|
os.path.join(dbpath, '1.db'))
|
||||||
|
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
||||||
|
broker.db_type = 'test'
|
||||||
|
exc = None
|
||||||
try:
|
try:
|
||||||
with broker.get() as conn:
|
with broker.get() as conn:
|
||||||
conn.execute('SELECT * FROM test')
|
conn.execute('SELECT * FROM test')
|
||||||
@ -1214,27 +1230,34 @@ class TestDatabaseBroker(unittest.TestCase):
|
|||||||
message = str(e)
|
message = str(e)
|
||||||
self.assertEqual(message, '400 Bad Request')
|
self.assertEqual(message, '400 Bad Request')
|
||||||
|
|
||||||
def test_possibly_quarantine_disk_error(self):
|
def test_possibly_quarantine_db_errors(self):
|
||||||
dbpath = os.path.join(self.testdir, 'dev', 'dbs', 'par', 'pre', 'db')
|
dbpath = os.path.join(self.testdir, 'dev', 'dbs', 'par', 'pre', 'db')
|
||||||
mkdirs(dbpath)
|
|
||||||
qpath = os.path.join(self.testdir, 'dev', 'quarantined', 'tests', 'db')
|
qpath = os.path.join(self.testdir, 'dev', 'quarantined', 'tests', 'db')
|
||||||
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
|
# Data is a list of Excpetions to be raised and expected values in the
|
||||||
|
# log
|
||||||
|
data = [
|
||||||
|
(sqlite3.DatabaseError('database disk image is malformed'),
|
||||||
|
'malformed'),
|
||||||
|
(sqlite3.DatabaseError('malformed database schema'), 'malformed'),
|
||||||
|
(sqlite3.DatabaseError('file is encrypted or is not a database'),
|
||||||
|
'corrupted'),
|
||||||
|
(sqlite3.OperationalError('disk I/O error'),
|
||||||
|
'disk error while accessing')]
|
||||||
|
|
||||||
|
for i, (ex, hint) in enumerate(data):
|
||||||
|
mkdirs(dbpath)
|
||||||
|
broker = DatabaseBroker(os.path.join(dbpath, '%d.db' % (i)))
|
||||||
broker.db_type = 'test'
|
broker.db_type = 'test'
|
||||||
|
|
||||||
def stub():
|
|
||||||
raise sqlite3.OperationalError('disk I/O error')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stub()
|
raise ex
|
||||||
except Exception:
|
except (sqlite3.DatabaseError, DatabaseConnectionError):
|
||||||
try:
|
try:
|
||||||
broker.possibly_quarantine(*sys.exc_info())
|
broker.possibly_quarantine(*sys.exc_info())
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(exc),
|
str(exc),
|
||||||
'Quarantined %s to %s due to disk error '
|
'Quarantined %s to %s due to %s database' %
|
||||||
'while accessing database' %
|
(dbpath, qpath, hint))
|
||||||
(dbpath, qpath))
|
|
||||||
else:
|
else:
|
||||||
self.fail('Expected an exception to be raised')
|
self.fail('Expected an exception to be raised')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user