From dbbbe595f4c5ad880960aaa986869291ed689b64 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Sun, 9 Oct 2016 04:41:38 -0700 Subject: [PATCH] Use ensure_tree from oslo_utils.fileutils Make use of the common oslo method to implement ensure_dir. TrivialFix Change-Id: Ia9e4c581664235476f290a4b651c5a24017ce357 --- neutron/agent/dhcp/agent.py | 3 ++- neutron/agent/l3/ha.py | 4 ++-- neutron/agent/linux/dhcp.py | 5 +++-- neutron/agent/linux/external_process.py | 4 ++-- neutron/agent/linux/keepalived.py | 3 ++- neutron/agent/linux/utils.py | 5 +++-- neutron/common/utils.py | 11 ++++------- neutron/db/migration/cli.py | 3 +-- neutron/tests/base.py | 3 ++- .../exclusive_resources/resource_allocator.py | 3 ++- neutron/tests/fullstack/resources/process.py | 3 ++- neutron/tests/unit/agent/l3/test_agent.py | 5 +++-- .../tests/unit/agent/l3/test_dvr_local_router.py | 3 ++- neutron/tests/unit/agent/linux/test_dhcp.py | 10 +++++----- .../unit/agent/linux/test_external_process.py | 8 +++++--- neutron/tests/unit/agent/metadata/test_agent.py | 9 +++++---- neutron/tests/unit/common/test_utils.py | 15 --------------- neutron/tests/unit/db/test_migration.py | 2 +- 18 files changed, 46 insertions(+), 53 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index 9a9680a9fa0..469bc70f831 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -23,6 +23,7 @@ from oslo_config import cfg from oslo_log import log as logging import oslo_messaging from oslo_service import loopingcall +from oslo_utils import fileutils from oslo_utils import importutils from neutron._i18n import _, _LE, _LI, _LW @@ -61,7 +62,7 @@ class DhcpAgent(manager.Manager): self.plugin_rpc = DhcpPluginApi(topics.PLUGIN, self.conf.host) # create dhcp dir to store dhcp info dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path) - utils.ensure_dir(dhcp_dir) + fileutils.ensure_tree(dhcp_dir, mode=0o755) self.dhcp_version = self.dhcp_driver_cls.check_version() self._populate_networks_cache() # keep track of mappings between networks and routers for diff --git a/neutron/agent/l3/ha.py b/neutron/agent/l3/ha.py index c87e1745049..ad1e93ca308 100644 --- a/neutron/agent/l3/ha.py +++ b/neutron/agent/l3/ha.py @@ -18,11 +18,11 @@ import os import eventlet from oslo_log import log as logging +from oslo_utils import fileutils import webob from neutron._i18n import _LI from neutron.agent.linux import utils as agent_utils -from neutron.common import utils as common_utils from neutron.conf.agent.l3 import ha as ha_conf from neutron.notifiers import batch_notifier @@ -163,4 +163,4 @@ class AgentMixin(object): def _init_ha_conf_path(self): ha_full_path = os.path.dirname("/%s/" % self.conf.ha_confs_path) - common_utils.ensure_dir(ha_full_path) + fileutils.ensure_tree(ha_full_path, mode=0o755) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 477cbde9d65..c63586272da 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -27,6 +27,7 @@ from oslo_config import cfg from oslo_log import log as logging import oslo_messaging from oslo_utils import excutils +from oslo_utils import fileutils from oslo_utils import uuidutils import six @@ -183,7 +184,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) - common_utils.ensure_dir(self.network_conf_dir) + fileutils.ensure_tree(self.network_conf_dir, mode=0o755) @staticmethod def get_confs_dir(conf): @@ -208,7 +209,7 @@ class DhcpLocalProcess(DhcpBase): if self.active: self.restart() elif self._enable_dhcp(): - common_utils.ensure_dir(self.network_conf_dir) + fileutils.ensure_tree(self.network_conf_dir, mode=0o755) 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 d79dc8cf6d6..4dc3e642f56 100644 --- a/neutron/agent/linux/external_process.py +++ b/neutron/agent/linux/external_process.py @@ -27,7 +27,6 @@ from neutron._i18n import _, _LW, _LE 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 LOG = logging.getLogger(__name__) @@ -81,7 +80,8 @@ class ProcessManager(MonitoredProcess): self.service_pid_fname = 'pid' self.service = 'default-service' - common_utils.ensure_dir(os.path.dirname(self.get_pid_file_name())) + fileutils.ensure_tree(os.path.dirname(self.get_pid_file_name()), + mode=0o755) 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 35b4a16f16b..e00bf5f321c 100644 --- a/neutron/agent/linux/keepalived.py +++ b/neutron/agent/linux/keepalived.py @@ -20,6 +20,7 @@ import netaddr from neutron_lib import exceptions from oslo_config import cfg from oslo_log import log as logging +from oslo_utils import fileutils from neutron._i18n import _, _LE from neutron.agent.linux import external_process @@ -362,7 +363,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: - common_utils.ensure_dir(conf_dir) + fileutils.ensure_tree(conf_dir, mode=0o755) 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 5e86cf516e0..f4eef165933 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -33,6 +33,7 @@ from oslo_log import log as logging from oslo_rootwrap import client from oslo_utils import encodeutils from oslo_utils import excutils +from oslo_utils import fileutils from six import iterbytes from six.moves import http_client as httplib @@ -192,7 +193,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: - utils.ensure_dir(conf_dir) + fileutils.ensure_tree(conf_dir, mode=0o755) return conf_base @@ -308,7 +309,7 @@ def ensure_directory_exists_without_file(path): if not os.path.exists(path): ctxt.reraise = False else: - utils.ensure_dir(dirname) + fileutils.ensure_tree(dirname, mode=0o755) def is_effective_user(user_id_or_name): diff --git a/neutron/common/utils.py b/neutron/common/utils.py index b671249d1ee..9cf5d03ba0a 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -19,7 +19,6 @@ """Utilities and helper functions.""" import decimal -import errno import functools import importlib import os @@ -44,6 +43,7 @@ from oslo_config import cfg from oslo_db import exception as db_exc from oslo_log import log as logging from oslo_utils import excutils +from oslo_utils import fileutils from oslo_utils import importutils import six from stevedore import driver @@ -59,14 +59,11 @@ SYNCHRONIZED_PREFIX = 'neutron-' synchronized = lockutils.synchronized_with_prefix(SYNCHRONIZED_PREFIX) +@removals.remove( + message="Use ensure_tree(path, 0o755) from oslo_utils.fileutils") 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 + fileutils.ensure_tree(dir_path, mode=0o755) def _subprocess_setup(): diff --git a/neutron/db/migration/cli.py b/neutron/db/migration/cli.py index 3be9951be82..3f9aee1bd8f 100644 --- a/neutron/db/migration/cli.py +++ b/neutron/db/migration/cli.py @@ -29,7 +29,6 @@ import pkg_resources import six from neutron._i18n import _ -from neutron.common import utils from neutron.db import migration from neutron.db.migration.connection import DBConnection @@ -228,7 +227,7 @@ def _check_bootstrap_new_branch(branch, version_path, addn_kwargs): addn_kwargs['head'] = _get_branch_head(branch) if not os.path.exists(version_path): # Bootstrap initial directory structure - utils.ensure_dir(version_path) + fileutils.ensure_tree(version_path, mode=0o755) def do_revision(config, cmd): diff --git a/neutron/tests/base.py b/neutron/tests/base.py index f8410744225..d47e4e63f73 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -34,6 +34,7 @@ from oslo_concurrency.fixture import lockutils from oslo_config import cfg from oslo_messaging import conffixture as messaging_conffixture from oslo_utils import excutils +from oslo_utils import fileutils from oslo_utils import strutils from oslotest import base import six @@ -89,7 +90,7 @@ def bool_from_env(key, strict=False, default=False): def setup_test_logging(config_opts, log_dir, log_file_path_template): # Have each test log into its own log file config_opts.set_override('debug', True) - utils.ensure_dir(log_dir) + fileutils.ensure_tree(log_dir, mode=0o755) log_file = sanitize_log_path( os.path.join(log_dir, log_file_path_template)) config_opts.set_override('log_file', log_file) diff --git a/neutron/tests/common/exclusive_resources/resource_allocator.py b/neutron/tests/common/exclusive_resources/resource_allocator.py index 0944a224481..cbc04f45e61 100644 --- a/neutron/tests/common/exclusive_resources/resource_allocator.py +++ b/neutron/tests/common/exclusive_resources/resource_allocator.py @@ -16,6 +16,7 @@ import os import fixtures from oslo_log import log as logging +from oslo_utils import fileutils from neutron.common import utils @@ -98,7 +99,7 @@ class ResourceAllocator(object): resource, self._resource_name, allocations) def _get_allocations(self): - utils.ensure_dir(TMP_DIR) + fileutils.ensure_tree(TMP_DIR, mode=0o755) try: with open(self._state_file_path, 'r') as allocations_file: diff --git a/neutron/tests/fullstack/resources/process.py b/neutron/tests/fullstack/resources/process.py index 2d49653e016..27e2e83e278 100644 --- a/neutron/tests/fullstack/resources/process.py +++ b/neutron/tests/fullstack/resources/process.py @@ -20,6 +20,7 @@ import signal import fixtures from neutronclient.common import exceptions as nc_exc from neutronclient.v2_0 import client +from oslo_utils import fileutils from neutron.agent.linux import async_process from neutron.agent.linux import utils @@ -49,7 +50,7 @@ class ProcessFixture(fixtures.Fixture): test_name = base.sanitize_log_path(self.test_name) log_dir = os.path.join(fullstack_base.DEFAULT_LOG_DIR, test_name) - common_utils.ensure_dir(log_dir) + fileutils.ensure_tree(log_dir, mode=0o755) timestamp = datetime.datetime.now().strftime("%Y-%m-%d--%H-%M-%S-%f") log_file = "%s--%s.log" % (self.process_name, timestamp) diff --git a/neutron/tests/unit/agent/l3/test_agent.py b/neutron/tests/unit/agent/l3/test_agent.py index e0ba242648b..e2e8d2387ff 100644 --- a/neutron/tests/unit/agent/l3/test_agent.py +++ b/neutron/tests/unit/agent/l3/test_agent.py @@ -91,7 +91,8 @@ 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.common.utils.ensure_dir').start() + self.ensure_dir = mock.patch( + 'oslo_utils.fileutils.ensure_tree').start() mock.patch('neutron.agent.linux.keepalived.KeepalivedManager' '.get_full_config_file_path').start() @@ -191,7 +192,7 @@ class TestBasicRouterOperations(BasicRouterOperationsFramework): def test_init_ha_conf(self): with mock.patch('os.path.dirname', return_value='/etc/ha/'): l3_agent.L3NATAgent(HOSTNAME, self.conf) - self.ensure_dir.assert_called_once_with('/etc/ha/') + self.ensure_dir.assert_called_once_with('/etc/ha/', mode=0o755) def test_enqueue_state_change_router_not_found(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) 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 750be87cb03..61e75569a2f 100644 --- a/neutron/tests/unit/agent/l3/test_dvr_local_router.py +++ b/neutron/tests/unit/agent/l3/test_dvr_local_router.py @@ -65,7 +65,8 @@ 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.common.utils.ensure_dir').start() + self.ensure_dir = mock.patch( + 'oslo_utils.fileutils.ensure_tree').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 5115d2b157d..bdeafedfde6 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -19,12 +19,12 @@ import mock import netaddr from neutron_lib import constants from oslo_config import cfg +from oslo_utils import fileutils from neutron.agent.common import config from neutron.agent.linux import dhcp from neutron.agent.linux import external_process from neutron.common import constants as n_const -from neutron.common import utils from neutron.conf.agent import dhcp as dhcp_config from neutron.conf import common as base_config from neutron.extensions import extra_dhcp_opt as edo_ext @@ -937,11 +937,11 @@ class TestDhcpLocalProcess(TestBase): lp = LocalChild(self.conf, FakeV4Network()) self.assertEqual(lp.get_conf_file_name('dev'), tpl) - @mock.patch.object(utils, 'ensure_dir') + @mock.patch.object(fileutils, 'ensure_tree') def test_ensure_dir_called(self, ensure_dir): LocalChild(self.conf, FakeV4Network()) ensure_dir.assert_called_once_with( - '/dhcp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') + '/dhcp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', mode=0o755) def test_enable_already_active(self): with mock.patch.object(LocalChild, 'active') as patched: @@ -952,7 +952,7 @@ class TestDhcpLocalProcess(TestBase): self.assertEqual(lp.called, ['restart']) self.assertFalse(self.mock_mgr.return_value.setup.called) - @mock.patch.object(utils, 'ensure_dir') + @mock.patch.object(fileutils, 'ensure_tree') def test_enable(self, ensure_dir): attrs_to_mock = dict( [(a, mock.DEFAULT) for a in @@ -972,7 +972,7 @@ class TestDhcpLocalProcess(TestBase): self.assertEqual(lp.called, ['spawn']) self.assertTrue(mocks['interface_name'].__set__.called) ensure_dir.assert_called_with( - '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc') + '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc', mode=0o755) def _assert_disabled(self, lp): self.assertTrue(lp.process_monitor.unregister.called) diff --git a/neutron/tests/unit/agent/linux/test_external_process.py b/neutron/tests/unit/agent/linux/test_external_process.py index 8d1b2a7d3c8..e746bf83e2d 100644 --- a/neutron/tests/unit/agent/linux/test_external_process.py +++ b/neutron/tests/unit/agent/linux/test_external_process.py @@ -15,8 +15,9 @@ import mock import os.path +from oslo_utils import fileutils + from neutron.agent.linux import external_process as ep -from neutron.common import utils as common_utils from neutron.tests import base from neutron.tests import tools @@ -106,7 +107,7 @@ class TestProcessManager(base.BaseTestCase): self.delete_if_exists = mock.patch( 'oslo_utils.fileutils.delete_if_exists').start() self.ensure_dir = mock.patch.object( - common_utils, 'ensure_dir').start() + fileutils, 'ensure_tree').start() self.conf = mock.Mock() self.conf.external_pids = '/var/path' @@ -114,7 +115,8 @@ class TestProcessManager(base.BaseTestCase): def test_processmanager_ensures_pid_dir(self): pid_file = os.path.join(self.conf.external_pids, 'pid') ep.ProcessManager(self.conf, 'uuid', pid_file=pid_file) - self.ensure_dir.assert_called_once_with(self.conf.external_pids) + self.ensure_dir.assert_called_once_with(self.conf.external_pids, + mode=0o755) def test_enable_no_namespace(self): callback = mock.Mock() diff --git a/neutron/tests/unit/agent/metadata/test_agent.py b/neutron/tests/unit/agent/metadata/test_agent.py index 1083c2e1079..bac67336303 100644 --- a/neutron/tests/unit/agent/metadata/test_agent.py +++ b/neutron/tests/unit/agent/metadata/test_agent.py @@ -19,6 +19,7 @@ import webob from oslo_config import cfg from oslo_config import fixture as config_fixture +from oslo_utils import fileutils from neutron.agent.linux import utils as agent_utils from neutron.agent.metadata import agent @@ -459,10 +460,10 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): self.cfg.CONF.metadata_backlog = 128 self.cfg.CONF.metadata_proxy_socket_mode = config.USER_MODE - @mock.patch.object(utils, 'ensure_dir') + @mock.patch.object(fileutils, 'ensure_tree') def test_init_doesnot_exists(self, ensure_dir): agent.UnixDomainMetadataProxy(mock.Mock()) - ensure_dir.assert_called_once_with('/the') + ensure_dir.assert_called_once_with('/the', mode=0o755) def test_init_exists(self): with mock.patch('os.path.isdir') as isdir: @@ -496,12 +497,12 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): @mock.patch.object(agent, 'MetadataProxyHandler') @mock.patch.object(agent_utils, 'UnixDomainWSGIServer') - @mock.patch.object(utils, 'ensure_dir') + @mock.patch.object(fileutils, 'ensure_tree') def test_run(self, ensure_dir, server, handler): p = agent.UnixDomainMetadataProxy(self.cfg.CONF) p.run() - ensure_dir.assert_called_once_with('/the') + ensure_dir.assert_called_once_with('/the', mode=0o755) server.assert_has_calls([ mock.call('neutron-metadata-agent'), mock.call().start(handler.return_value, diff --git a/neutron/tests/unit/common/test_utils.py b/neutron/tests/unit/common/test_utils.py index 35c7f6126bc..dd5938e13e6 100644 --- a/neutron/tests/unit/common/test_utils.py +++ b/neutron/tests/unit/common/test_utils.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import errno import inspect import os.path import re @@ -623,20 +622,6 @@ class TestDelayedStringRenderer(base.BaseTestCase): 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) - - class TestCamelize(base.BaseTestCase): def test_camelize(self): data = {'bandwidth_limit': 'BandwidthLimit', diff --git a/neutron/tests/unit/db/test_migration.py b/neutron/tests/unit/db/test_migration.py index 586e72ad6dc..fa403b78b0c 100644 --- a/neutron/tests/unit/db/test_migration.py +++ b/neutron/tests/unit/db/test_migration.py @@ -127,7 +127,7 @@ class TestCli(base.BaseTestCase): mock_root = mock.patch.object(cli, '_get_package_root_dir').start() mock_root.side_effect = mocked_root_dir # Avoid creating fake directories - mock.patch('neutron.common.utils.ensure_dir').start() + mock.patch('oslo_utils.fileutils.ensure_tree').start() # Set up some configs and entrypoints for tests to chew on self.configs = []