]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Change ensure_dir to not check directory exists first
authorBrian Haley <brian.haley@hp.com>
Fri, 5 Jun 2015 03:54:31 +0000 (23:54 -0400)
committerBrian Haley <brian.haley@hp.com>
Thu, 11 Jun 2015 04:11:06 +0000 (00:11 -0400)
I224be69168ede8a496a5f7d59b04b722f4de7192 added an EEXIST
check, so no need to check if the directory is already
there, just try and create it.

Change-Id: Iba51fc8263bf59326489319d0dd3f69af00a8eeb

neutron/agent/linux/utils.py
neutron/tests/unit/agent/linux/test_dhcp.py
neutron/tests/unit/agent/linux/test_utils.py

index dc22a0e069bc9c777442e0b67945eb1dc67a20b2..f57a1c919e76bc9fd70c18116623b85b71a31fa5 100644 (file)
@@ -191,14 +191,12 @@ def find_child_pids(pid):
 
 def ensure_dir(dir_path):
     """Ensure a directory with 755 permissions mode."""
-    if not os.path.isdir(dir_path):
-        try:
-            os.makedirs(dir_path, 0o755)
-        except OSError as e:
-            # Make sure that the error was that the directory was created
-            # by a different (concurrent) worker. If not, raise the error.
-            if e.errno != errno.EEXIST:
-                raise
+    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 _get_conf_base(cfg_root, uuid, ensure_conf_dir):
index 483680326a4df3f80a49cc6d2688d885011eefe1..0e5014bd6e17b97615bf8e08a98e8c4cd6571bf4 100644 (file)
@@ -661,8 +661,6 @@ class TestBase(base.BaseTestCase):
         self.execute = self.execute_p.start()
 
         self.makedirs = mock.patch('os.makedirs').start()
-        self.isdir = mock.patch('os.path.isdir').start()
-        self.isdir.return_value = False
         self.rmtree = mock.patch('shutil.rmtree').start()
 
         self.external_process = mock.patch(
index aa510f96de7b3f49529d39cc61ca2c9f9f78edd1..9958d0422f8dc107ed09f3defaafdb9e57639ada 100644 (file)
@@ -283,13 +283,17 @@ class TestBaseOSUtils(base.BaseTestCase):
         getgrgid.assert_called_once_with(self.EGID)
 
     @mock.patch('os.makedirs')
-    @mock.patch('os.path.exists', return_value=False)
-    def test_ensure_dir_no_fail_if_exists(self, path_exists, 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):