From a371f11835e422c06be038d8ce79f38e8f64ab0d Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 3 Jan 2019 16:12:56 -0500 Subject: [PATCH] Fix circular import in nova.privsep.utils Commit 26521718bdba3bccbf6270e26b76754c26304658 imported nova.utils into nova.privsep.utils which can cause an ImportError due to an ArgsAlreadyParseError because of nova.utils importing nova.conf which registers config options. For some obscure reason, this is only being noticed when using [libvirt]/image_type=lvm, so something in the libvirt lvm image backend using privsep is tickling this import error, but regardless the nova.privsep code should avoid importing stuff from the rest of nova, so this change simply adds a simple "generate_random_string" utility to nova.privsep.utils to avoid the import. Change-Id: I3799869fd4217d12b92d79e27484043ef5b8dc13 Closes-Bug: #1808247 --- nova/privsep/utils.py | 15 ++++++++++++--- nova/tests/unit/privsep/test_utils.py | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) 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)