From d6c287ab8cdc9b8f4a5c7f03ec0661c9155defd9 Mon Sep 17 00:00:00 2001 From: Mohammed Junaid Date: Sun, 10 Feb 2013 02:10:25 +0530 Subject: object-storage: Fixing the errors and warnings in unittest. Change-Id: Id22c968aefd82c4b62445b3ecc93cbabc2b35ffc BUG: 887301 Signed-off-by: Mohammed Junaid Reviewed-on: http://review.gluster.org/4394 Reviewed-by: Kaleb KEITHLEY Reviewed-by: Peter Portante Tested-by: Gluster Build System --- ufo/test/unit/common/test_diskfile.py | 36 +++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/ufo/test/unit/common/test_diskfile.py b/ufo/test/unit/common/test_diskfile.py index fb0dc3e..85d539a 100644 --- a/ufo/test/unit/common/test_diskfile.py +++ b/ufo/test/unit/common/test_diskfile.py @@ -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) -- cgit