diff --git a/nova/tests/unit/test_utils.py b/nova/tests/unit/test_utils.py index 98555f680d..93154c6230 100644 --- a/nova/tests/unit/test_utils.py +++ b/nova/tests/unit/test_utils.py @@ -14,9 +14,6 @@ import datetime import hashlib -import os -import os.path -import tempfile import threading from unittest import mock @@ -147,14 +144,6 @@ class GenericUtilsTestCase(test.NoDBTestCase): self.assertTrue([c for c in password if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']) - @mock.patch('nova.privsep.path.chown') - def test_temporary_chown(self, mock_chown): - with tempfile.NamedTemporaryFile() as f: - with utils.temporary_chown(f.name, owner_uid=2): - mock_chown.assert_called_once_with(f.name, uid=2) - mock_chown.reset_mock() - mock_chown.assert_called_once_with(f.name, uid=os.getuid()) - def test_get_shortened_ipv6(self): self.assertEqual("abcd:ef01:2345:6789:abcd:ef01:c0a8:fefe", utils.get_shortened_ipv6( @@ -816,27 +805,6 @@ class SpawnTestCase(test.NoDBTestCase): self.assertEqual(ctxt, common_context.get_current()) -class UT8TestCase(test.NoDBTestCase): - def test_none_value(self): - self.assertIsInstance(utils.utf8(None), type(None)) - - def test_bytes_value(self): - some_value = b"fake data" - return_value = utils.utf8(some_value) - # check that type of returned value doesn't changed - self.assertIsInstance(return_value, type(some_value)) - self.assertEqual(some_value, return_value) - - def test_not_text_type(self): - return_value = utils.utf8(1) - self.assertEqual(b"1", return_value) - self.assertIsInstance(return_value, bytes) - - def test_text_type_with_encoding(self): - some_value = 'test\u2026config' - self.assertEqual(some_value, utils.utf8(some_value).decode("utf-8")) - - class TestObjectCallHelpers(test.NoDBTestCase): def test_with_primitives(self): tester = mock.Mock() diff --git a/nova/utils.py b/nova/utils.py index 1227146ac4..4a80ab0e48 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -43,7 +43,6 @@ from oslo_context import context as common_context from oslo_log import log as logging import oslo_messaging as messaging from oslo_utils import encodeutils -from oslo_utils import excutils from oslo_utils import importutils from oslo_utils import strutils from oslo_utils import timeutils @@ -285,23 +284,6 @@ def generate_password(length=None, symbolgroups=DEFAULT_PASSWORD_SYMBOLS): return ''.join(password) -# TODO(sfinucan): Replace this with the equivalent from oslo.utils -def utf8(value): - """Try to turn a string into utf-8 if possible. - - The original code was copied from the utf8 function in - http://github.com/facebook/tornado/blob/master/tornado/escape.py - - """ - if value is None or isinstance(value, bytes): - return value - - if not isinstance(value, str): - value = str(value) - - return value.encode('utf-8') - - def parse_server_string(server_str): """Parses the given server_string and returns a tuple of host and port. If it's not a combination of host part and port, the port element @@ -370,21 +352,6 @@ def format_remote_path(host, path): return "%s:%s" % (safe_ip_format(host), path) -def make_dev_path(dev, partition=None, base='/dev'): - """Return a path to a particular device. - - >>> make_dev_path('xvdc') - /dev/xvdc - - >>> make_dev_path('xvdc', 1) - /dev/xvdc1 - """ - path = os.path.join(base, dev) - if partition: - path += str(partition) - return path - - def sanitize_hostname(hostname, default_name=None): """Sanitize a given hostname. @@ -470,45 +437,6 @@ def temporary_mutation(obj, **kwargs): set_value(obj, attr, old_value) -def generate_mac_address(): - """Generate an Ethernet MAC address.""" - # NOTE(vish): We would prefer to use 0xfe here to ensure that linux - # bridge mac addresses don't change, but it appears to - # conflict with libvirt, so we use the next highest octet - # that has the unicast and locally administered bits set - # properly: 0xfa. - # Discussion: https://bugs.launchpad.net/nova/+bug/921838 - mac = [0xfa, 0x16, 0x3e, - random.randint(0x00, 0xff), - random.randint(0x00, 0xff), - random.randint(0x00, 0xff)] - return ':'.join(map(lambda x: "%02x" % x, mac)) - - -# NOTE(mikal): I really wanted this code to go away, but I can't find a way -# to implement what the callers of this method want with privsep. Basically, -# if we could hand off either a file descriptor or a file like object then -# we could make this go away. -@contextlib.contextmanager -def temporary_chown(path, owner_uid=None): - """Temporarily chown a path. - - :param owner_uid: UID of temporary owner (defaults to current user) - """ - if owner_uid is None: - owner_uid = os.getuid() - - orig_uid = os.stat(path).st_uid - - if orig_uid != owner_uid: - nova.privsep.path.chown(path, uid=owner_uid) - try: - yield - finally: - if orig_uid != owner_uid: - nova.privsep.path.chown(path, uid=orig_uid) - - @contextlib.contextmanager def tempdir(**kwargs): argdict = kwargs.copy() @@ -524,34 +452,6 @@ def tempdir(**kwargs): LOG.error('Could not remove tmpdir: %s', e) -class UndoManager(object): - """Provides a mechanism to facilitate rolling back a series of actions - when an exception is raised. - """ - - def __init__(self): - self.undo_stack = [] - - def undo_with(self, undo_func): - self.undo_stack.append(undo_func) - - def _rollback(self): - for undo_func in reversed(self.undo_stack): - undo_func() - - def rollback_and_reraise(self, msg=None, **kwargs): - """Rollback a series of actions then re-raise the exception. - - .. note:: (sirp) This should only be called within an - exception handler. - """ - with excutils.save_and_reraise_exception(): - if msg: - LOG.exception(msg, **kwargs) - - self._rollback() - - def metadata_to_dict(metadata, include_deleted=False): result = {} for item in metadata: