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
ctx, self.conf.use_namespaces)
# create dhcp dir to store dhcp info
dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path)
- if not os.path.isdir(dhcp_dir):
- os.makedirs(dhcp_dir, 0o755)
+ linux_utils.ensure_dir(dhcp_dir)
self.dhcp_version = self.dhcp_driver_cls.check_version()
self._populate_networks_cache()
self._process_monitor = external_process.ProcessMonitor(
from oslo_config import cfg
from neutron.agent.linux import keepalived
+from neutron.agent.linux import utils
from neutron.common import constants as l3_constants
from neutron.i18n import _LE
from neutron.openstack.common import log as logging
def _init_ha_conf_path(self):
ha_full_path = os.path.dirname("/%s/" % self.conf.ha_confs_path)
- if not os.path.isdir(ha_full_path):
- os.makedirs(ha_full_path, 0o755)
+ utils.ensure_dir(ha_full_path)
def process_ha_router_added(self, ri):
ha_port = ri.router.get(l3_constants.HA_INTERFACE_KEY)
version, plugin)
self.confs_dir = self.get_confs_dir(conf)
self.network_conf_dir = os.path.join(self.confs_dir, network.id)
- self._ensure_network_conf_dir()
+ utils.ensure_dir(self.network_conf_dir)
@staticmethod
def get_confs_dir(conf):
"""Returns the file name for a given kind of config file."""
return os.path.join(self.network_conf_dir, kind)
- def _ensure_network_conf_dir(self):
- """Ensures the directory for configuration files is created."""
- if not os.path.isdir(self.network_conf_dir):
- os.makedirs(self.network_conf_dir, 0o755)
-
def _remove_config_files(self):
shutil.rmtree(self.network_conf_dir, ignore_errors=True)
if self.active:
self.restart()
elif self._enable_dhcp():
- self._ensure_network_conf_dir()
+ utils.ensure_dir(self.network_conf_dir)
interface_name = self.device_manager.setup(self.network)
self.interface_name = interface_name
self.spawn_process()
def _get_full_config_file_path(self, filename, ensure_conf_dir=True):
conf_dir = self.get_conf_dir()
- if ensure_conf_dir and not os.path.isdir(conf_dir):
- os.makedirs(conf_dir, 0o755)
+ if ensure_conf_dir:
+ utils.ensure_dir(conf_dir)
return os.path.join(conf_dir, filename)
import six.moves.urllib.parse as urlparse
import webob
+from neutron.agent.linux import utils as linux_utils
from neutron.agent import rpc as agent_rpc
from neutron.common import constants as n_const
from neutron.common import rpc as n_rpc
if not os.path.exists(cfg.CONF.metadata_proxy_socket):
ctxt.reraise = False
else:
- os.makedirs(dirname, 0o755)
+ linux_utils.ensure_dir(dirname)
self._init_state_reporting()
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
+import os
+
import mock
import testtools
self.assertFalse(utils.cmdlines_are_equal(
['ping', '8.8.8.8'],
['/usr/bin/ping6', '8.8.8.8']))
+
+
+class TestBaseOSUtils(base.BaseTestCase):
+ @mock.patch.object(os.path, 'isdir', return_value=False)
+ @mock.patch.object(os, 'makedirs')
+ def test_ensure_dir_not_exist(self, makedirs, isdir):
+ utils.ensure_dir('/the')
+ isdir.assert_called_once_with('/the')
+ makedirs.assert_called_once_with('/the', 0o755)
+
+ @mock.patch.object(os.path, 'isdir', return_value=True)
+ @mock.patch.object(os, 'makedirs')
+ def test_ensure_dir_exist(self, makedirs, isdir):
+ utils.ensure_dir('/the')
+ isdir.assert_called_once_with('/the')
+ self.assertFalse(makedirs.called)
'neutron.agent.linux.ip_lib.device_exists')
self.device_exists = self.device_exists_p.start()
- mock.patch('neutron.agent.l3.ha.AgentMixin'
- '._init_ha_conf_path').start()
+ self.ensure_dir = mock.patch('neutron.agent.linux.utils'
+ '.ensure_dir').start()
+
mock.patch('neutron.agent.linux.keepalived.KeepalivedNotifierMixin'
'._get_full_config_file_path').start()
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/')
+
def test_periodic_sync_routers_task_raise_exception(self):
agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
self.plugin_api.get_routers.side_effect = ValueError
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.openstack.common import log as logging
lp = LocalChild(self.conf, FakeV4Network())
self.assertEqual(lp.get_conf_file_name('dev'), tpl)
- def test_ensure_network_conf_dir(self):
+ @mock.patch.object(utils, 'ensure_dir')
+ def test_ensure_dir_called(self, ensure_dir):
LocalChild(self.conf, FakeV4Network())
- self.makedirs.assert_called_once_with(
- '/dhcp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', mock.ANY)
-
- def test_ensure_network_conf_existing_dir(self):
- self.isdir.return_value = True
- LocalChild(self.conf, FakeV4Network())
- self.assertFalse(self.makedirs.called)
+ ensure_dir.assert_called_once_with(
+ '/dhcp/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')
def test_enable_already_active(self):
with mock.patch.object(LocalChild, 'active') as patched:
self.assertEqual(lp.called, ['restart'])
self.assertFalse(self.mock_mgr.return_value.setup.called)
- def test_enable(self):
+ @mock.patch.object(utils, 'ensure_dir')
+ def test_enable(self, ensure_dir):
attrs_to_mock = dict(
[(a, mock.DEFAULT) for a in
- ['active', 'get_conf_file_name', 'interface_name',
- '_ensure_network_conf_dir']]
+ ['active', 'interface_name']]
)
with mock.patch.multiple(LocalChild, **attrs_to_mock) as mocks:
mocks['active'].__get__ = mock.Mock(return_value=False)
- mocks['get_conf_file_name'].return_value = '/dir'
mocks['interface_name'].__set__ = mock.Mock()
lp = LocalChild(self.conf,
FakeDualNetwork())
mock.call().setup(mock.ANY)])
self.assertEqual(lp.called, ['spawn'])
self.assertTrue(mocks['interface_name'].__set__.called)
- self.assertTrue(mocks['_ensure_network_conf_dir'].called)
+ ensure_dir.assert_called_with(
+ '/dhcp/cccccccc-cccc-cccc-cccc-cccccccccccc')
def _assert_disabled(self, lp):
self.assertTrue(lp.process_monitor.unregister.called)
import os.path
from neutron.agent.linux import external_process as ep
+from neutron.agent.linux import utils
from neutron.tests import base
self.execute = self.execute_p.start()
self.delete_if_exists = mock.patch(
'neutron.openstack.common.fileutils.delete_if_exists').start()
- self.makedirs = mock.patch('os.makedirs').start()
+ self.ensure_dir = mock.patch.object(
+ utils, 'ensure_dir').start()
self.conf = mock.Mock()
self.conf.external_pids = '/var/path'
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.makedirs.assert_called_once_with(self.conf.external_pids, 0o755)
+ self.ensure_dir.assert_called_once_with(self.conf.external_pids)
def test_enable_no_namespace(self):
callback = mock.Mock()
import testtools
import webob
+from neutron.agent.linux import utils as linux_utils
from neutron.agent.metadata import agent
from neutron.agent import metadata_agent
from neutron.common import constants
self.cfg.CONF.metadata_workers = 0
self.cfg.CONF.metadata_backlog = 128
- def test_init_doesnot_exists(self):
- with mock.patch('os.path.isdir') as isdir:
- with mock.patch('os.makedirs') as makedirs:
- isdir.return_value = False
- agent.UnixDomainMetadataProxy(mock.Mock())
- makedirs.assert_called_once_with('/the', 0o755)
+ @mock.patch.object(linux_utils, 'ensure_dir')
+ def test_init_doesnot_exists(self, ensure_dir):
+ agent.UnixDomainMetadataProxy(mock.Mock())
+ ensure_dir.assert_called_once_with('/the')
def test_init_exists(self):
with mock.patch('os.path.isdir') as isdir:
agent.UnixDomainMetadataProxy(mock.Mock())
unlink.assert_called_once_with('/the/path')
- def test_run(self):
- with mock.patch.object(agent, 'MetadataProxyHandler') as handler:
- with mock.patch.object(agent, 'UnixDomainWSGIServer') as server:
- with mock.patch('os.path.isdir') as isdir:
- with mock.patch('os.makedirs') as makedirs:
- isdir.return_value = False
-
- p = agent.UnixDomainMetadataProxy(self.cfg.CONF)
- p.run()
-
- makedirs.assert_called_once_with('/the', 0o755)
- server.assert_has_calls([
- mock.call('neutron-metadata-agent'),
- mock.call().start(handler.return_value,
- '/the/path', workers=0,
- backlog=128),
- mock.call().wait()]
- )
+ @mock.patch.object(agent, 'MetadataProxyHandler')
+ @mock.patch.object(agent, 'UnixDomainWSGIServer')
+ @mock.patch.object(linux_utils, 'ensure_dir')
+ def test_run(self, ensure_dir, server, handler):
+ p = agent.UnixDomainMetadataProxy(self.cfg.CONF)
+ p.run()
+
+ ensure_dir.assert_called_once_with('/the')
+ server.assert_has_calls([
+ mock.call('neutron-metadata-agent'),
+ mock.call().start(handler.return_value,
+ '/the/path', workers=0,
+ backlog=128),
+ mock.call().wait()]
+ )
def test_main(self):
with mock.patch.object(agent, 'UnixDomainMetadataProxy') as proxy: