object-storage: Fixing the errors and warnings in unittest.

Change-Id: Id22c968aefd82c4b62445b3ecc93cbabc2b35ffc
BUG: 887301
Signed-off-by: Mohammed Junaid <junaid@redhat.com>
Reviewed-on: http://review.gluster.org/4394
Reviewed-by: Kaleb KEITHLEY <kkeithle@redhat.com>
Reviewed-by: Peter Portante <pportant@redhat.com>
Tested-by: Gluster Build System <jenkins@build.gluster.com>
This commit is contained in:
Mohammed Junaid 2013-02-10 02:10:25 +05:30 committed by Vijay Bellur
parent 598ca6bbaa
commit ee5df68f08

View File

@ -66,6 +66,21 @@ def _mock_do_unlink(f):
raise ose
def _mock_do_unlink_eacces_err(f):
ose = OSError()
ose.errno = errno.EACCES
raise ose
def _mock_getsize_eaccess_err(f):
ose = OSError()
ose.errno = errno.EACCES
raise ose
def _mock_do_rmdir_eacces_err(f):
ose = OSError()
ose.errno = errno.EACCES
raise ose
class MockRenamerCalled(Exception):
pass
@ -654,13 +669,16 @@ class TestDiskFile(unittest.TestCase):
os.chmod(the_path, stats.st_mode & (~stat.S_IWUSR))
# Handle the case do_unlink() raises an OSError
__os_unlink = os.unlink
os.unlink = _mock_do_unlink_eacces_err
try:
gdf.unlinkold(normalize_timestamp(later))
except OSError as e:
assert e.errno != errno.ENOENT
assert e.errno == errno.EACCES
else:
self.fail("Excepted an OSError when unlinking file")
finally:
os.unlink = __os_unlink
os.chmod(the_path, stats.st_mode)
assert os.path.isdir(gdf.datadir)
@ -699,11 +717,14 @@ class TestDiskFile(unittest.TestCase):
stats = os.stat(gdf.datadir)
os.chmod(gdf.datadir, 0)
__os_rmdir = os.rmdir
os.rmdir = _mock_do_rmdir_eacces_err
try:
later = float(gdf.metadata['X-Timestamp']) + 1
gdf.unlinkold(normalize_timestamp(later))
finally:
os.chmod(gdf.datadir, stats.st_mode)
os.rmdir = __os_rmdir
assert os.path.isdir(gdf.datadir)
assert os.path.isdir(gdf.data_file)
finally:
@ -795,13 +816,16 @@ class TestDiskFile(unittest.TestCase):
assert not gdf._is_dir
stats = os.stat(the_path)
os.chmod(the_path, 0)
__os_path_getsize = os.path.getsize
os.path.getsize = _mock_getsize_eaccess_err
try:
s = gdf.get_data_file_size()
except OSError as err:
assert err.errno != errno.ENOENT
assert err.errno == errno.EACCES
else:
self.fail("Expected OSError exception")
finally:
os.path.getsize = __os_path_getsize
os.chmod(the_path, stats.st_mode)
finally:
shutil.rmtree(td)
@ -873,7 +897,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)