Bring DiskFile module coverge to 100%

To bring the DiskFile module coverage to 100%, we first recognize that
do_fsync() will invoke fsync() in a separate thread, which gives
coverage fits (see the commit history at
https://github.com/portante/coverage/commits/master). To avoid that,
we mock out do_fsync to make it a no-op and avoid the problem.

The second thing we recognize is that mkstemp() relies on do_unlink
from the fs_utils module, which already consumes ENOENT errors, so we
don't need that code path in mkstemp() itself. The unused mock routine
for do_unlink was removed as well, and we renamed the other do_unlink
mock routine to os_unlink since it was mocking out os.unlink directly.

Lastly, we rejigger the error on close test for mkstemp() to
prematurely close the fd to cause an error which should just be
squelched, completing the full coverage.

Change-Id: I98283c17cf139f92282f8afd7083d567d3dd9a79
Signed-off-by: Peter Portante <peter.portante@redhat.com>
Reviewed-on: http://review.gluster.org/5082
Reviewed-by: Luis Pabon <lpabon@redhat.com>
Tested-by: Luis Pabon <lpabon@redhat.com>
This commit is contained in:
Peter Portante 2013-05-23 20:52:19 -04:00 committed by Luis Pabon
parent 833d7fc7df
commit e2b9e5e06c
2 changed files with 19 additions and 21 deletions

View File

@ -332,8 +332,4 @@ class Gluster_DiskFile(DiskFile):
except OSError:
pass
tmppath, self.tmppath = self.tmppath, None
try:
do_unlink(tmppath)
except OSError as err:
if err.errno != errno.ENOENT:
raise
do_unlink(tmppath)

View File

@ -57,13 +57,10 @@ class MockException(Exception):
def _mock_rmdirs(p):
raise MockException("gluster.swift.common.DiskFile.rmdirs() called")
def _mock_do_unlink(f):
ose = OSError()
ose.errno = errno.ENOENT
raise ose
def _mock_do_fsync(fd):
return
def _mock_do_unlink_eacces_err(f):
def _mock_os_unlink_eacces_err(f):
ose = OSError()
ose.errno = errno.EACCES
raise ose
@ -101,6 +98,8 @@ class TestDiskFile(unittest.TestCase):
self._saved_ut_rm = gluster.swift.common.utils.read_metadata
gluster.swift.common.utils.write_metadata = _mock_write_metadata
gluster.swift.common.utils.read_metadata = _mock_read_metadata
self._saved_do_fsync = gluster.swift.common.DiskFile.do_fsync
gluster.swift.common.DiskFile.do_fsync = _mock_do_fsync
def tearDown(self):
self.lg = None
@ -109,6 +108,7 @@ class TestDiskFile(unittest.TestCase):
gluster.swift.common.DiskFile.read_metadata = self._saved_df_rm
gluster.swift.common.utils.write_metadata = self._saved_ut_wm
gluster.swift.common.utils.read_metadata = self._saved_ut_rm
gluster.swift.common.DiskFile.do_fsync = self._saved_do_fsync
def test_constructor_no_slash(self):
assert not os.path.exists("/tmp/foo")
@ -659,9 +659,9 @@ class TestDiskFile(unittest.TestCase):
stats = os.stat(the_path)
os.chmod(the_path, stats.st_mode & (~stat.S_IWUSR))
# Handle the case do_unlink() raises an OSError
# Handle the case os_unlink() raises an OSError
__os_unlink = os.unlink
os.unlink = _mock_do_unlink_eacces_err
os.unlink = _mock_os_unlink_eacces_err
try:
gdf.unlinkold(normalize_timestamp(later))
except OSError as e:
@ -868,6 +868,15 @@ class TestDiskFile(unittest.TestCase):
assert os.path.basename(saved_tmppath)[:3] == '.z.'
assert os.path.exists(saved_tmppath)
os.write(fd, "123")
# At the end of previous with block a close on fd is called.
# Calling os.close on the same fd will raise an OSError
# exception and we must catch it.
try:
os.close(fd)
except OSError as err:
pass
else:
self.fail("Exception expected")
assert not os.path.exists(saved_tmppath)
finally:
shutil.rmtree(td)
@ -888,15 +897,8 @@ class TestDiskFile(unittest.TestCase):
assert os.path.basename(saved_tmppath)[:3] == '.z.'
assert os.path.exists(saved_tmppath)
os.write(fd, "123")
# At the end of previous with block a close on fd is called.
# Calling os.close on the same fd will raise an OSError
# exception and we must catch it.
try:
# Closing the fd prematurely should not raise any exceptions.
os.close(fd)
except OSError as err:
pass
else:
self.fail("Exception expected")
assert not os.path.exists(saved_tmppath)
finally:
shutil.rmtree(td)