From 772eace020f4d11348f9f2a00695c519ef85014c Mon Sep 17 00:00:00 2001 From: Prashanth Pai Date: Thu, 9 Oct 2014 21:55:35 +0530 Subject: [PATCH] Fix unit tests failing in some cases TestZeroCopy used to fail when 'localhost' resolved to an ipv6 address. https://github.com/eventlet/eventlet/issues/8 Also, "test_container_sync_realms.py:TestUtils.test_os_error" used to fail when unit tests were run as root user. This is because despite os.chmod(), a root user still has permission to access the file and hence OSError is not raised. Change-Id: Ife80b203358557999734515261814ce76c0e00cd Signed-off-by: Prashanth Pai --- .../unit/common/test_container_sync_realms.py | 29 +++++++++++-------- test/unit/obj/test_server.py | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/test/unit/common/test_container_sync_realms.py b/test/unit/common/test_container_sync_realms.py index 1ce8d489b8..438811a168 100644 --- a/test/unit/common/test_container_sync_realms.py +++ b/test/unit/common/test_container_sync_realms.py @@ -13,10 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +import errno import os import unittest import uuid +from mock import patch from swift.common.container_sync_realms import ContainerSyncRealms from test.unit import FakeLogger, temptree @@ -41,18 +43,21 @@ class TestUtils(unittest.TestCase): with temptree([fname], [fcontents]) as tempdir: logger = FakeLogger() fpath = os.path.join(tempdir, fname) - os.chmod(tempdir, 0) - csr = ContainerSyncRealms(fpath, logger) - try: - self.assertEqual( - logger.all_log_lines(), - {'error': [ - "Could not load '%s': [Errno 13] Permission denied: " - "'%s'" % (fpath, fpath)]}) - self.assertEqual(csr.mtime_check_interval, 300) - self.assertEqual(csr.realms(), []) - finally: - os.chmod(tempdir, 0700) + + def _mock_getmtime(path): + raise OSError(errno.EACCES, + os.strerror(errno.EACCES) + + ": '%s'" % (fpath)) + with patch('os.path.getmtime', _mock_getmtime): + csr = ContainerSyncRealms(fpath, logger) + + self.assertEqual( + logger.all_log_lines(), + {'error': [ + "Could not load '%s': [Errno 13] Permission denied: " + "'%s'" % (fpath, fpath)]}) + self.assertEqual(csr.mtime_check_interval, 300) + self.assertEqual(csr.realms(), []) def test_empty(self): fname = 'container-sync-realms.conf' diff --git a/test/unit/obj/test_server.py b/test/unit/obj/test_server.py index 60b9bf385b..3226682a52 100755 --- a/test/unit/obj/test_server.py +++ b/test/unit/obj/test_server.py @@ -4414,7 +4414,7 @@ class TestZeroCopy(unittest.TestCase): self.wsgi_greenlet = spawn( wsgi.server, listener, self.object_controller, NullLogger()) - self.http_conn = httplib.HTTPConnection('localhost', port) + self.http_conn = httplib.HTTPConnection('127.0.0.1', port) self.http_conn.connect() def tearDown(self):