diff --git a/nova/privsep/utils.py b/nova/privsep/utils.py index 2b9d9703acdd..b51bcf5926ed 100644 --- a/nova/privsep/utils.py +++ b/nova/privsep/utils.py @@ -23,23 +23,32 @@ import errno import mmap import os +import random +import sys from oslo_log import log as logging from oslo_utils import excutils -from nova import utils as nova_utils +# NOTE(mriedem): Avoid importing nova.utils since that can cause a circular +# import with the privsep code. In fact, avoid importing anything outside +# of nova/privsep/ if possible. LOG = logging.getLogger(__name__) +def generate_random_string(): + return str(random.randint(0, sys.maxsize)) + + def supports_direct_io(dirpath): if not hasattr(os, 'O_DIRECT'): LOG.debug("This python runtime does not support direct I/O") return False - file_name = "%s.%s" % (".directio.test", - nova_utils.generate_random_string()) + # Use a random filename to avoid issues with $dirpath being on shared + # storage. + file_name = "%s.%s" % (".directio.test", generate_random_string()) testfile = os.path.join(dirpath, file_name) hasDirectIO = True diff --git a/nova/tests/unit/privsep/test_utils.py b/nova/tests/unit/privsep/test_utils.py index cdb64331fa31..84d0767c2963 100644 --- a/nova/tests/unit/privsep/test_utils.py +++ b/nova/tests/unit/privsep/test_utils.py @@ -36,15 +36,15 @@ class SupportDirectIOTestCase(test.NoDBTestCase): self.einval.errno = errno.EINVAL self.enoent = OSError() self.enoent.errno = errno.ENOENT - self.test_path = os.path.join('.', '.directio.test.abc123') + self.test_path = os.path.join('.', '.directio.test.123') self.io_flags = os.O_CREAT | os.O_WRONLY | os.O_DIRECT open_patcher = mock.patch('os.open') write_patcher = mock.patch('os.write') close_patcher = mock.patch('os.close') unlink_patcher = mock.patch('os.unlink') - random_string_patcher = mock.patch('nova.utils.generate_random_string', - return_value='abc123') + random_string_patcher = mock.patch( + 'nova.privsep.utils.generate_random_string', return_value='123') self.addCleanup(open_patcher.stop) self.addCleanup(write_patcher.stop) self.addCleanup(close_patcher.stop)