From 1c29fab7cb3e586be72dd7910e2022b45c809c5f Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Thu, 4 Jun 2015 23:54:31 -0400 Subject: [PATCH] Change ensure_dir to not check directory exists first 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 | 14 ++++++-------- neutron/tests/unit/agent/linux/test_dhcp.py | 2 -- neutron/tests/unit/agent/linux/test_utils.py | 8 ++++++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/neutron/agent/linux/utils.py b/neutron/agent/linux/utils.py index dc22a0e06..f57a1c919 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -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): diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index 483680326..0e5014bd6 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -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( diff --git a/neutron/tests/unit/agent/linux/test_utils.py b/neutron/tests/unit/agent/linux/test_utils.py index aa510f96d..9958d0422 100644 --- a/neutron/tests/unit/agent/linux/test_utils.py +++ b/neutron/tests/unit/agent/linux/test_utils.py @@ -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): -- 2.45.2