Skip to content

Commit

Permalink
Increase test coverage of swift/common/db.py
Browse files Browse the repository at this point in the history
The most low hanging fruit is _preallocate(). As it turns out,
we never excercise the calculations because we never give it
a file that exists.

This version uses mock.patch everywhere.

Change-Id: I5df03aff295d2a1bca252a02b3985a6bc3eecb26
  • Loading branch information
zaitcev committed Aug 6, 2013
1 parent 3741fbe commit 0e96911
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions test/unit/common/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def setUp(self):
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)

def tearDown(self):
rmtree(self.testdir, ignore_errors=1)

def test_DB_PREALLOCATION_setting(self):
u = uuid4().hex
b = DatabaseBroker(u)
Expand All @@ -105,9 +108,6 @@ def test_DB_PREALLOCATION_setting(self):
swift.common.db.DB_PREALLOCATION = True
self.assertRaises(OSError, b._preallocate)

def tearDown(self):
rmtree(self.testdir, ignore_errors=1)

def test_memory_db_init(self):
broker = DatabaseBroker(':memory:')
self.assertEqual(broker.db_file, ':memory:')
Expand All @@ -120,6 +120,21 @@ def test_disk_db_init(self):
self.assertEqual(broker.db_file, db_file)
self.assert_(broker.conn is None)

def test_disk_preallocate(self):
test_size = [-1]
def fallocate_stub(fd, size):
test_size[0] = size
with patch('swift.common.db.fallocate', fallocate_stub):
db_file = os.path.join(self.testdir, 'pre.db')
# Write 1 byte and hope that the fs will allocate less than 1 MB.
f = open(db_file, "w")
f.write('@')
f.close()
b = DatabaseBroker(db_file)
b._preallocate()
# We only wrote 1 byte, so we should end with the 1st step or 1 MB.
self.assertEquals(test_size[0], 1024*1024)

def test_initialize(self):
self.assertRaises(AttributeError,
DatabaseBroker(':memory:').initialize,
Expand Down Expand Up @@ -235,9 +250,7 @@ def stub(*args, **kwargs):
with broker.get() as conn:
self.assertEquals(
[r[0] for r in conn.execute('SELECT * FROM test')], ['1'])
orig_renamer = swift.common.db.renamer
try:
swift.common.db.renamer = lambda a, b: b
with patch('swift.common.db.renamer', lambda a, b: b):
qpath = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.dirname(self.testdir))))
if qpath:
Expand Down Expand Up @@ -274,8 +287,6 @@ def stub(*args, **kwargs):
self.assertEquals(str(exc),
'Quarantined %s to %s due to corrupted database' %
(self.testdir, qpath))
finally:
swift.common.db.renamer = orig_renamer

def test_lock(self):
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'), timeout=.1)
Expand Down

0 comments on commit 0e96911

Please sign in to comment.