]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
ensure_dir: move under neutron.common.utils
authorIhar Hrachyshka <ihrachys@redhat.com>
Sat, 18 Jul 2015 12:52:53 +0000 (14:52 +0200)
committerIhar Hrachyshka <ihrachys@redhat.com>
Tue, 21 Jul 2015 14:33:09 +0000 (16:33 +0200)
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

15 files changed:
neutron/agent/dhcp/agent.py
neutron/agent/l3/ha.py
neutron/agent/linux/dhcp.py
neutron/agent/linux/external_process.py
neutron/agent/linux/keepalived.py
neutron/agent/linux/utils.py
neutron/common/utils.py
neutron/tests/fullstack/fullstack_fixtures.py
neutron/tests/unit/agent/l3/test_agent.py
neutron/tests/unit/agent/l3/test_dvr_local_router.py
neutron/tests/unit/agent/linux/test_dhcp.py
neutron/tests/unit/agent/linux/test_external_process.py
neutron/tests/unit/agent/linux/test_utils.py
neutron/tests/unit/agent/metadata/test_agent.py
neutron/tests/unit/common/test_utils.py

index 4d78d6428b4d6bd9723d2609d2a120cfc9e7a7be..d1294db6ac0d7794dbedd3b33e6b27be1d614258 100644 (file)
@@ -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(
index 95f3fd7635501a4df3933c8f0e982e1acc570f72..76363c9afba7ff092f79e04017596fa0f6584b6b 100644 (file)
@@ -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)
index 0a06259c1a0d948b4fbab130a00c560dd5e16a1e..bd8f439b1f4335e54781e3c37232ef5fd27201e0 100644 (file)
@@ -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()
index 7c437631852e4b56772297c3435e0333ccc8212e..4cf287218dff81daf2b51b51fb9bb9c336b2f67b 100644 (file)
@@ -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:
index d39f015f944c54bff5b2b4d38c8c3568c407d3ce..d8180110749383bbd29367f0248b9d4909a440fe 100644 (file)
@@ -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):
index b646aa8f428494549a1e6b7bef548ca834faa3cf..cddf9d5fe56d5535882b90da56411b88fac427ba 100644 (file)
@@ -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):
index bd2dccdb0d2082f59ae06313b9601d7a6c1515b0..c3e56d75c157f6a1e21f7a98e81fa50fb050a4b1 100644 (file)
@@ -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.
index 690891cd550ea9a3d5d28dd27983fb0f96149622..63ae92649bf2b87af158a85cc5b319f9a77f756f 100644 (file)
@@ -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,
index 09416ba0a176d7dacf0755a683a7f571f69e8244..edf705b06a74dfac073e0bf78aad2ccc2fbac758 100644 (file)
@@ -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()
index bec9168afbe4e88f91935fe88b7c5acf83bf81d3..052ac68bf2eafd296263e480853dc6fe04890934 100644 (file)
@@ -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()
index 41a3173d1f4eee7eb1ac923e9bdfb2d5db916730..21b65b66627c6f7d23e073b3444d8a53f1850fe6 100644 (file)
@@ -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
 
index 68df1a7dcd75ca658a6c953f1303fe87d8142be6..4cc3836306dbdfb7d512a1a78f59ceafeca95095 100644 (file)
@@ -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'
index 9958d0422f8dc107ed09f3defaafdb9e57639ada..9a2e89ffa355fd8745f0a9d4b410829c05fda1fc 100644 (file)
@@ -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):
index 9bef96864c82e26c27848b0a097348853984cb31..c79f991eb1d3df0eb71586965de481c3de6d94b3 100644 (file)
@@ -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()
index 82c84904c0005e78717b0db28c2a43c3f89f0b9c..81634f979e3c5d4a5d9b9f2000dea5464fe56b67 100644 (file)
@@ -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)