]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add ability to use custom config in DHCP-agent
authorSergey Belous <sbelous@mirantis.com>
Thu, 3 Sep 2015 13:53:21 +0000 (16:53 +0300)
committerSergey Belous <sbelous@mirantis.com>
Mon, 7 Sep 2015 13:11:10 +0000 (16:11 +0300)
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
neutron/agent/dhcp_agent.py
neutron/cmd/sanity_check.py
neutron/tests/unit/agent/dhcp/test_agent.py

index d1294db6ac0d7794dbedd3b33e6b27be1d614258..551c5be41db00bdb64453511267562d76e3f6ec9 100644 (file)
@@ -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(
index 845259a2d5f48e6dbbb45e8610e72af38744e4a9..968a3d1c5b28e6f9cbfaf3c971c347e3d91dd9b4 100644 (file)
@@ -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(
index 6d3b3dfe7ae47eb433880ac0abf97913f6c23b15..0cf80a10389d5aaf8174f162461892e7aeaaf885 100644 (file)
@@ -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)
 
 
index 74b7f3b1089723eef9b9a47e4f78f3e37bed12ad..3069c87728c5b53deecd82032c6fbe11ac0455ab 100644 (file)
@@ -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: