Fix circular import in nova.privsep.utils

Commit 26521718bd 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
This commit is contained in:
Matt Riedemann 2019-01-03 16:12:56 -05:00
parent 8ef3d253a0
commit a371f11835
2 changed files with 15 additions and 6 deletions

View File

@ -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

View File

@ -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)