Use ensure_tree from oslo_utils.fileutils

Make use of the common oslo method to implement ensure_dir.

TrivialFix

Change-Id: Ia9e4c581664235476f290a4b651c5a24017ce357
This commit is contained in:
Gary Kotton 2016-10-09 04:41:38 -07:00
parent e08e06fa22
commit dbbbe595f4
18 changed files with 46 additions and 53 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 = []