From f53a43fd5e0fe80d08d4bd5f89dc4fb1e96094a9 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Sat, 18 Jul 2015 14:52:53 +0200 Subject: [PATCH] ensure_dir: move under neutron.common.utils There is nothing Linux or agent specific in the function. I need to use it outside agent code in one of depending patches, hence moving it into better location while leaving the previous symbol in place, with deprecation warning, for backwards compatibility. Change-Id: I252356a72f3c742e57c1b6127275030f0994a221 --- neutron/agent/dhcp/agent.py | 3 +-- neutron/agent/l3/ha.py | 3 ++- neutron/agent/linux/dhcp.py | 4 ++-- neutron/agent/linux/external_process.py | 3 ++- neutron/agent/linux/keepalived.py | 3 ++- neutron/agent/linux/utils.py | 17 ++++++----------- neutron/common/utils.py | 11 +++++++++++ neutron/tests/fullstack/fullstack_fixtures.py | 3 ++- neutron/tests/unit/agent/l3/test_agent.py | 3 +-- .../unit/agent/l3/test_dvr_local_router.py | 3 +-- neutron/tests/unit/agent/linux/test_dhcp.py | 2 +- .../unit/agent/linux/test_external_process.py | 4 ++-- neutron/tests/unit/agent/linux/test_utils.py | 16 ++-------------- neutron/tests/unit/agent/metadata/test_agent.py | 4 ++-- neutron/tests/unit/common/test_utils.py | 16 ++++++++++++++++ 15 files changed, 53 insertions(+), 42 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index 4d78d6428b4..d1294db6ac0 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -26,7 +26,6 @@ from oslo_utils import importutils from neutron.agent.linux import dhcp from neutron.agent.linux import external_process -from neutron.agent.linux import utils as linux_utils from neutron.agent.metadata import driver as metadata_driver from neutron.agent import rpc as agent_rpc from neutron.common import constants @@ -63,7 +62,7 @@ class DhcpAgent(manager.Manager): ctx, self.conf.use_namespaces) # create dhcp dir to store dhcp info dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path) - linux_utils.ensure_dir(dhcp_dir) + utils.ensure_dir(dhcp_dir) self.dhcp_version = self.dhcp_driver_cls.check_version() self._populate_networks_cache() self._process_monitor = external_process.ProcessMonitor( diff --git a/neutron/agent/l3/ha.py b/neutron/agent/l3/ha.py index 95f3fd76355..76363c9afba 100644 --- a/neutron/agent/l3/ha.py +++ b/neutron/agent/l3/ha.py @@ -22,6 +22,7 @@ import webob from neutron.agent.linux import keepalived from neutron.agent.linux import utils as agent_utils +from neutron.common import utils as common_utils from neutron.i18n import _LI from neutron.notifiers import batch_notifier @@ -157,4 +158,4 @@ class AgentMixin(object): def _init_ha_conf_path(self): ha_full_path = os.path.dirname("/%s/" % self.conf.ha_confs_path) - agent_utils.ensure_dir(ha_full_path) + common_utils.ensure_dir(ha_full_path) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 0a06259c1a0..bd8f439b1f4 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -174,7 +174,7 @@ class DhcpLocalProcess(DhcpBase): version, plugin) self.confs_dir = self.get_confs_dir(conf) self.network_conf_dir = os.path.join(self.confs_dir, network.id) - utils.ensure_dir(self.network_conf_dir) + commonutils.ensure_dir(self.network_conf_dir) @staticmethod def get_confs_dir(conf): @@ -199,7 +199,7 @@ class DhcpLocalProcess(DhcpBase): if self.active: self.restart() elif self._enable_dhcp(): - utils.ensure_dir(self.network_conf_dir) + commonutils.ensure_dir(self.network_conf_dir) interface_name = self.device_manager.setup(self.network) self.interface_name = interface_name self.spawn_process() diff --git a/neutron/agent/linux/external_process.py b/neutron/agent/linux/external_process.py index 7c437631852..4cf287218df 100644 --- a/neutron/agent/linux/external_process.py +++ b/neutron/agent/linux/external_process.py @@ -26,6 +26,7 @@ from oslo_utils import fileutils from neutron.agent.common import config as agent_cfg from neutron.agent.linux import ip_lib from neutron.agent.linux import utils +from neutron.common import utils as common_utils from neutron.i18n import _LE LOG = logging.getLogger(__name__) @@ -78,7 +79,7 @@ class ProcessManager(MonitoredProcess): self.service_pid_fname = 'pid' self.service = 'default-service' - utils.ensure_dir(os.path.dirname(self.get_pid_file_name())) + common_utils.ensure_dir(os.path.dirname(self.get_pid_file_name())) def enable(self, cmd_callback=None, reload_cfg=False): if not self.active: diff --git a/neutron/agent/linux/keepalived.py b/neutron/agent/linux/keepalived.py index d39f015f944..d8180110749 100644 --- a/neutron/agent/linux/keepalived.py +++ b/neutron/agent/linux/keepalived.py @@ -23,6 +23,7 @@ from oslo_log import log as logging from neutron.agent.linux import external_process from neutron.agent.linux import utils from neutron.common import exceptions +from neutron.common import utils as common_utils VALID_STATES = ['MASTER', 'BACKUP'] VALID_AUTH_TYPES = ['AH', 'PASS'] @@ -340,7 +341,7 @@ class KeepalivedManager(object): def get_full_config_file_path(self, filename, ensure_conf_dir=True): conf_dir = self.get_conf_dir() if ensure_conf_dir: - utils.ensure_dir(conf_dir) + common_utils.ensure_dir(conf_dir) return os.path.join(conf_dir, filename) def _output_config_file(self): diff --git a/neutron/agent/linux/utils.py b/neutron/agent/linux/utils.py index b646aa8f428..cddf9d5fe56 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import errno import fcntl import glob import grp @@ -25,6 +24,7 @@ import struct import tempfile import threading +from debtcollector import removals import eventlet from eventlet.green import subprocess from eventlet import greenthread @@ -189,14 +189,9 @@ def find_child_pids(pid): return [x.strip() for x in raw_pids.split('\n') if x.strip()] -def ensure_dir(dir_path): - """Ensure a directory with 755 permissions mode.""" - try: - os.makedirs(dir_path, 0o755) - except OSError as e: - # If the directory already existed, don't raise the error. - if e.errno != errno.EEXIST: - raise +@removals.remove(message='Use neutron.common.utils.ensure_dir instead.') +def ensure_dir(*args, **kwargs): + return utils.ensure_dir(*args, **kwargs) def _get_conf_base(cfg_root, uuid, ensure_conf_dir): @@ -205,7 +200,7 @@ def _get_conf_base(cfg_root, uuid, ensure_conf_dir): conf_dir = os.path.abspath(os.path.normpath(cfg_root)) conf_base = os.path.join(conf_dir, uuid) if ensure_conf_dir: - ensure_dir(conf_dir) + utils.ensure_dir(conf_dir) return conf_base @@ -338,7 +333,7 @@ def ensure_directory_exists_without_file(path): if not os.path.exists(path): ctxt.reraise = False else: - ensure_dir(dirname) + utils.ensure_dir(dirname) def is_effective_user(user_id_or_name): diff --git a/neutron/common/utils.py b/neutron/common/utils.py index bd2dccdb0d2..c3e56d75c15 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -19,6 +19,7 @@ """Utilities and helper functions.""" import datetime +import errno import functools import hashlib import logging as std_logging @@ -172,6 +173,16 @@ def find_config_file(options, config_file): return cfg_file +def ensure_dir(dir_path): + """Ensure a directory with 755 permissions mode.""" + try: + os.makedirs(dir_path, 0o755) + except OSError as e: + # If the directory already existed, don't raise the error. + if e.errno != errno.EEXIST: + raise + + def _subprocess_setup(): # Python installs a SIGPIPE handler by default. This is usually not what # non-Python subprocesses expect. diff --git a/neutron/tests/fullstack/fullstack_fixtures.py b/neutron/tests/fullstack/fullstack_fixtures.py index 690891cd550..63ae92649bf 100644 --- a/neutron/tests/fullstack/fullstack_fixtures.py +++ b/neutron/tests/fullstack/fullstack_fixtures.py @@ -25,6 +25,7 @@ from oslo_utils import timeutils from neutron.agent.linux import async_process from neutron.agent.linux import utils +from neutron.common import utils as common_utils from neutron.tests import base from neutron.tests.common import net_helpers from neutron.tests.fullstack import config_fixtures @@ -51,7 +52,7 @@ class ProcessFixture(fixtures.Fixture): def start(self): fmt = self.process_name + "--%Y-%m-%d--%H%M%S.log" log_dir = os.path.join(DEFAULT_LOG_DIR, self.test_name) - utils.ensure_dir(log_dir) + common_utils.ensure_dir(log_dir) cmd = [spawn.find_executable(self.exec_name), '--log-dir', log_dir, diff --git a/neutron/tests/unit/agent/l3/test_agent.py b/neutron/tests/unit/agent/l3/test_agent.py index 09416ba0a17..edf705b06a7 100644 --- a/neutron/tests/unit/agent/l3/test_agent.py +++ b/neutron/tests/unit/agent/l3/test_agent.py @@ -79,8 +79,7 @@ class BasicRouterOperationsFramework(base.BaseTestCase): 'neutron.agent.linux.ip_lib.device_exists') self.device_exists = self.device_exists_p.start() - self.ensure_dir = mock.patch('neutron.agent.linux.utils' - '.ensure_dir').start() + self.ensure_dir = mock.patch('neutron.common.utils.ensure_dir').start() mock.patch('neutron.agent.linux.keepalived.KeepalivedManager' '.get_full_config_file_path').start() diff --git a/neutron/tests/unit/agent/l3/test_dvr_local_router.py b/neutron/tests/unit/agent/l3/test_dvr_local_router.py index bec9168afbe..052ac68bf2e 100644 --- a/neutron/tests/unit/agent/l3/test_dvr_local_router.py +++ b/neutron/tests/unit/agent/l3/test_dvr_local_router.py @@ -65,8 +65,7 @@ class TestDvrRouterOperations(base.BaseTestCase): 'neutron.agent.linux.ip_lib.device_exists') self.device_exists = self.device_exists_p.start() - self.ensure_dir = mock.patch('neutron.agent.linux.utils' - '.ensure_dir').start() + self.ensure_dir = mock.patch('neutron.common.utils.ensure_dir').start() mock.patch('neutron.agent.linux.keepalived.KeepalivedManager' '.get_full_config_file_path').start() diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index 41a3173d1f4..21b65b66627 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -24,9 +24,9 @@ from neutron.agent.common import config from neutron.agent.dhcp import config as dhcp_config from neutron.agent.linux import dhcp from neutron.agent.linux import external_process -from neutron.agent.linux import utils from neutron.common import config as base_config from neutron.common import constants +from neutron.common import utils from neutron.extensions import extra_dhcp_opt as edo_ext from neutron.tests import base diff --git a/neutron/tests/unit/agent/linux/test_external_process.py b/neutron/tests/unit/agent/linux/test_external_process.py index 68df1a7dcd7..4cc3836306d 100644 --- a/neutron/tests/unit/agent/linux/test_external_process.py +++ b/neutron/tests/unit/agent/linux/test_external_process.py @@ -16,7 +16,7 @@ import mock import os.path from neutron.agent.linux import external_process as ep -from neutron.agent.linux import utils +from neutron.common import utils as common_utils from neutron.tests import base @@ -105,7 +105,7 @@ class TestProcessManager(base.BaseTestCase): self.delete_if_exists = mock.patch( 'oslo_utils.fileutils.delete_if_exists').start() self.ensure_dir = mock.patch.object( - utils, 'ensure_dir').start() + common_utils, 'ensure_dir').start() self.conf = mock.Mock() self.conf.external_pids = '/var/path' diff --git a/neutron/tests/unit/agent/linux/test_utils.py b/neutron/tests/unit/agent/linux/test_utils.py index 9958d0422f8..9a2e89ffa35 100644 --- a/neutron/tests/unit/agent/linux/test_utils.py +++ b/neutron/tests/unit/agent/linux/test_utils.py @@ -12,9 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. -import errno -import mock import socket + +import mock import testtools from neutron.agent.linux import utils @@ -282,18 +282,6 @@ class TestBaseOSUtils(base.BaseTestCase): getegid.assert_called_once_with() getgrgid.assert_called_once_with(self.EGID) - @mock.patch('os.makedirs') - def test_ensure_dir_no_fail_if_exists(self, makedirs): - error = OSError() - error.errno = errno.EEXIST - makedirs.side_effect = error - utils.ensure_dir("/etc/create/concurrently") - - @mock.patch('os.makedirs') - def test_ensure_dir_calls_makedirs(self, makedirs): - utils.ensure_dir("/etc/create/directory") - makedirs.assert_called_once_with("/etc/create/directory", 0o755) - class TestUnixDomainHttpConnection(base.BaseTestCase): def test_connect(self): diff --git a/neutron/tests/unit/agent/metadata/test_agent.py b/neutron/tests/unit/agent/metadata/test_agent.py index 9bef96864c8..c79f991eb1d 100644 --- a/neutron/tests/unit/agent/metadata/test_agent.py +++ b/neutron/tests/unit/agent/metadata/test_agent.py @@ -524,7 +524,7 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): self.cfg.CONF.metadata_backlog = 128 self.cfg.CONF.metadata_proxy_socket_mode = config.USER_MODE - @mock.patch.object(agent_utils, 'ensure_dir') + @mock.patch.object(utils, 'ensure_dir') def test_init_doesnot_exists(self, ensure_dir): agent.UnixDomainMetadataProxy(mock.Mock()) ensure_dir.assert_called_once_with('/the') @@ -561,7 +561,7 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): @mock.patch.object(agent, 'MetadataProxyHandler') @mock.patch.object(agent_utils, 'UnixDomainWSGIServer') - @mock.patch.object(agent_utils, 'ensure_dir') + @mock.patch.object(utils, 'ensure_dir') def test_run(self, ensure_dir, server, handler): p = agent.UnixDomainMetadataProxy(self.cfg.CONF) p.run() diff --git a/neutron/tests/unit/common/test_utils.py b/neutron/tests/unit/common/test_utils.py index 82c84904c00..81634f979e3 100644 --- a/neutron/tests/unit/common/test_utils.py +++ b/neutron/tests/unit/common/test_utils.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import errno + import eventlet import mock import netaddr @@ -663,3 +665,17 @@ class TestDelayedStringRenderer(base.BaseTestCase): LOG.logger.setLevel(logging.logging.DEBUG) LOG.debug("Hello %s", delayed) self.assertTrue(my_func.called) + + +class TestEnsureDir(base.BaseTestCase): + @mock.patch('os.makedirs') + def test_ensure_dir_no_fail_if_exists(self, makedirs): + error = OSError() + error.errno = errno.EEXIST + makedirs.side_effect = error + utils.ensure_dir("/etc/create/concurrently") + + @mock.patch('os.makedirs') + def test_ensure_dir_calls_makedirs(self, makedirs): + utils.ensure_dir("/etc/create/directory") + makedirs.assert_called_once_with("/etc/create/directory", 0o755)