From 9e178e42e46317a6f1ac7688340f0f84e4c16c80 Mon Sep 17 00:00:00 2001 From: Sergey Belous Date: Thu, 3 Sep 2015 16:53:21 +0300 Subject: [PATCH] Add ability to use custom config in DHCP-agent This patch doesn't changes behaviour of dhcp-agent but adds the opportunity to use user-defined config, that will make dhcp-agent more flexible and allows to run functional tests correctly (without changing global oslo.config CONF) Closes-Bug: #1492283 Change-Id: Ice807e8fc872b56bb3960b7a3de4110c7675d9d6 --- neutron/agent/dhcp/agent.py | 27 +++++++++++---------- neutron/agent/dhcp_agent.py | 22 ++++++++--------- neutron/cmd/sanity_check.py | 2 +- neutron/tests/unit/agent/dhcp/test_agent.py | 7 +++--- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index d1294db6ac0..551c5be41db 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -51,15 +51,16 @@ class DhcpAgent(manager.Manager): """ target = oslo_messaging.Target(version='1.0') - def __init__(self, host=None): + def __init__(self, host=None, conf=None): super(DhcpAgent, self).__init__(host=host) self.needs_resync_reasons = collections.defaultdict(list) - self.conf = cfg.CONF + self.conf = conf or cfg.CONF self.cache = NetworkCache() self.dhcp_driver_cls = importutils.import_class(self.conf.dhcp_driver) ctx = context.get_admin_context_without_session() self.plugin_rpc = DhcpPluginApi(topics.PLUGIN, - ctx, self.conf.use_namespaces) + ctx, self.conf.use_namespaces, + 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) @@ -149,7 +150,7 @@ class DhcpAgent(manager.Manager): """ only_nets = set([] if (not networks or None in networks) else networks) LOG.info(_LI('Synchronizing state')) - pool = eventlet.GreenPool(cfg.CONF.num_sync_threads) + pool = eventlet.GreenPool(self.conf.num_sync_threads) known_network_ids = set(self.cache.get_network_ids()) try: @@ -399,9 +400,9 @@ class DhcpPluginApi(object): """ - def __init__(self, topic, context, use_namespaces): + def __init__(self, topic, context, use_namespaces, host): self.context = context - self.host = cfg.CONF.host + self.host = host self.use_namespaces = use_namespaces target = oslo_messaging.Target( topic=topic, @@ -537,21 +538,21 @@ class NetworkCache(object): class DhcpAgentWithStateReport(DhcpAgent): - def __init__(self, host=None): - super(DhcpAgentWithStateReport, self).__init__(host=host) + def __init__(self, host=None, conf=None): + super(DhcpAgentWithStateReport, self).__init__(host=host, conf=conf) self.state_rpc = agent_rpc.PluginReportStateAPI(topics.PLUGIN) self.agent_state = { 'binary': 'neutron-dhcp-agent', 'host': host, 'topic': topics.DHCP_AGENT, 'configurations': { - 'dhcp_driver': cfg.CONF.dhcp_driver, - 'use_namespaces': cfg.CONF.use_namespaces, - 'dhcp_lease_duration': cfg.CONF.dhcp_lease_duration, - 'log_agent_heartbeats': cfg.CONF.AGENT.log_agent_heartbeats}, + 'dhcp_driver': self.conf.dhcp_driver, + 'use_namespaces': self.conf.use_namespaces, + 'dhcp_lease_duration': self.conf.dhcp_lease_duration, + 'log_agent_heartbeats': self.conf.AGENT.log_agent_heartbeats}, 'start_flag': True, 'agent_type': constants.AGENT_TYPE_DHCP} - report_interval = cfg.CONF.AGENT.report_interval + report_interval = self.conf.AGENT.report_interval self.use_call = True if report_interval: self.heartbeat = loopingcall.FixedIntervalLoopingCall( diff --git a/neutron/agent/dhcp_agent.py b/neutron/agent/dhcp_agent.py index 845259a2d5f..968a3d1c5b2 100644 --- a/neutron/agent/dhcp_agent.py +++ b/neutron/agent/dhcp_agent.py @@ -28,20 +28,20 @@ from neutron.common import topics from neutron import service as neutron_service -def register_options(): - config.register_interface_driver_opts_helper(cfg.CONF) - config.register_use_namespaces_opts_helper(cfg.CONF) - config.register_agent_state_opts_helper(cfg.CONF) - cfg.CONF.register_opts(dhcp_config.DHCP_AGENT_OPTS) - cfg.CONF.register_opts(dhcp_config.DHCP_OPTS) - cfg.CONF.register_opts(dhcp_config.DNSMASQ_OPTS) - cfg.CONF.register_opts(metadata_config.DRIVER_OPTS) - cfg.CONF.register_opts(metadata_config.SHARED_OPTS) - cfg.CONF.register_opts(interface.OPTS) +def register_options(conf): + config.register_interface_driver_opts_helper(conf) + config.register_use_namespaces_opts_helper(conf) + config.register_agent_state_opts_helper(conf) + conf.register_opts(dhcp_config.DHCP_AGENT_OPTS) + conf.register_opts(dhcp_config.DHCP_OPTS) + conf.register_opts(dhcp_config.DNSMASQ_OPTS) + conf.register_opts(metadata_config.DRIVER_OPTS) + conf.register_opts(metadata_config.SHARED_OPTS) + conf.register_opts(interface.OPTS) def main(): - register_options() + register_options(cfg.CONF) common_config.init(sys.argv[1:]) config.setup_logging() server = neutron_service.Service.create( diff --git a/neutron/cmd/sanity_check.py b/neutron/cmd/sanity_check.py index 6d3b3dfe7ae..0cf80a10389 100644 --- a/neutron/cmd/sanity_check.py +++ b/neutron/cmd/sanity_check.py @@ -39,7 +39,7 @@ def setup_conf(): cfg.CONF.import_group('ml2_sriov', 'neutron.plugins.ml2.drivers.mech_sriov.mech_driver.' 'mech_driver') - dhcp_agent.register_options() + dhcp_agent.register_options(cfg.CONF) cfg.CONF.register_opts(l3_hamode_db.L3_HA_OPTS) diff --git a/neutron/tests/unit/agent/dhcp/test_agent.py b/neutron/tests/unit/agent/dhcp/test_agent.py index 74b7f3b1089..3069c87728c 100644 --- a/neutron/tests/unit/agent/dhcp/test_agent.py +++ b/neutron/tests/unit/agent/dhcp/test_agent.py @@ -215,7 +215,7 @@ fake_down_network = dhcp.NetModel( class TestDhcpAgent(base.BaseTestCase): def setUp(self): super(TestDhcpAgent, self).setUp() - entry.register_options() + entry.register_options(cfg.CONF) cfg.CONF.set_override('interface_driver', 'neutron.agent.linux.interface.NullDriver') # disable setting up periodic state reporting @@ -541,7 +541,7 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): config.register_interface_driver_opts_helper(cfg.CONF) cfg.CONF.set_override('interface_driver', 'neutron.agent.linux.interface.NullDriver') - entry.register_options() # register all dhcp cfg options + entry.register_options(cfg.CONF) # register all dhcp cfg options self.plugin_p = mock.patch(DHCP_PLUGIN) plugin_cls = self.plugin_p.start() @@ -978,8 +978,7 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): class TestDhcpPluginApiProxy(base.BaseTestCase): def _test_dhcp_api(self, method, **kwargs): ctxt = context.get_admin_context() - proxy = dhcp_agent.DhcpPluginApi('foo', ctxt, None) - proxy.host = 'foo' + proxy = dhcp_agent.DhcpPluginApi('foo', ctxt, None, host='foo') with mock.patch.object(proxy.client, 'call') as rpc_mock,\ mock.patch.object(proxy.client, 'prepare') as prepare_mock: