From 0109231fcdb3c9e850afef82ba27090a4a0178af Mon Sep 17 00:00:00 2001 From: gongysh Date: Mon, 4 Mar 2013 21:21:04 +0800 Subject: [PATCH] Log the configuration options for metadata-proxy and agent. Bug #1144370 Change-Id: I35ac570dd55006d0133736588f0302d6f70294de --- quantum/agent/common/config.py | 23 +++++++ quantum/agent/dhcp_agent.py | 14 ++-- quantum/agent/l3_agent.py | 14 ++-- quantum/agent/metadata/agent.py | 3 +- quantum/agent/metadata/namespace_proxy.py | 6 +- quantum/common/utils.py | 5 ++ quantum/tests/unit/test_dhcp_agent.py | 67 +++++++++++++++++-- quantum/tests/unit/test_metadata_agent.py | 16 +++-- .../unit/test_metadata_namespace_proxy.py | 59 +++++++++------- 9 files changed, 155 insertions(+), 52 deletions(-) diff --git a/quantum/agent/common/config.py b/quantum/agent/common/config.py index fe7b19d355..5e6a6a2617 100644 --- a/quantum/agent/common/config.py +++ b/quantum/agent/common/config.py @@ -15,6 +15,8 @@ # License for the specific language governing permissions and limitations # under the License. +import os + from oslo.config import cfg from quantum.common import config @@ -35,6 +37,27 @@ AGENT_STATE_OPTS = [ ] +def get_log_args(conf, log_file_name): + cmd_args = [] + if conf.debug: + cmd_args.append('--debug') + if conf.verbose: + cmd_args.append('--verbose') + if (conf.log_dir or conf.log_file): + cmd_args.append('--log-file=%s' % log_file_name) + log_dir = None + if conf.log_dir and conf.log_file: + log_dir = os.path.dirname( + os.path.join(conf.log_dir, conf.log_file)) + elif conf.log_dir: + log_dir = conf.log_dir + elif conf.log_file: + log_dir = os.path.dirname(conf.log_file) + if log_dir: + cmd_args.append('--log-dir=%s' % log_dir) + return cmd_args + + def register_root_helper(conf): # The first call is to ensure backward compatibility conf.register_opts(ROOT_HELPER_OPTS) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index cea7896f08..0397ae1985 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -282,11 +282,15 @@ class DhcpAgent(manager.Manager): router_ports[0].device_id) def callback(pid_file): - return ['quantum-ns-metadata-proxy', - '--pid_file=%s' % pid_file, - quantum_lookup_param, - '--state_path=%s' % self.conf.state_path, - '--metadata_port=%d' % METADATA_PORT] + proxy_cmd = ['quantum-ns-metadata-proxy', + '--pid_file=%s' % pid_file, + quantum_lookup_param, + '--state_path=%s' % self.conf.state_path, + '--metadata_port=%d' % METADATA_PORT] + proxy_cmd.extend(config.get_log_args( + cfg.CONF, 'quantum-ns-metadata-proxy%s.log' % network.id)) + return proxy_cmd + pm = external_process.ProcessManager( self.conf, network.id, diff --git a/quantum/agent/l3_agent.py b/quantum/agent/l3_agent.py index f2823926a4..0464bc4e12 100644 --- a/quantum/agent/l3_agent.py +++ b/quantum/agent/l3_agent.py @@ -254,10 +254,14 @@ class L3NATAgent(manager.Manager): def _spawn_metadata_proxy(self, router_info): def callback(pid_file): - return ['quantum-ns-metadata-proxy', - '--pid_file=%s' % pid_file, - '--router_id=%s' % router_info.router_id, - '--state_path=%s' % self.conf.state_path] + proxy_cmd = ['quantum-ns-metadata-proxy', + '--pid_file=%s' % pid_file, + '--router_id=%s' % router_info.router_id, + '--state_path=%s' % self.conf.state_path] + proxy_cmd.extend(config.get_log_args( + cfg.CONF, 'quantum-ns-metadata-proxy%s.log' % + router_info.router_id)) + return proxy_cmd pm = external_process.ProcessManager( self.conf, @@ -742,7 +746,7 @@ def main(): config.register_root_helper(conf) conf.register_opts(interface.OPTS) conf.register_opts(external_process.OPTS) - conf() + conf(project='quantum') config.setup_logging(conf) server = quantum_service.Service.create( binary='quantum-l3-agent', diff --git a/quantum/agent/metadata/agent.py b/quantum/agent/metadata/agent.py index 4163be8535..4ceb4d0e44 100644 --- a/quantum/agent/metadata/agent.py +++ b/quantum/agent/metadata/agent.py @@ -29,6 +29,7 @@ from quantumclient.v2_0 import client import webob from quantum.common import config +from quantum.common import utils from quantum.openstack.common import log as logging from quantum import wsgi @@ -221,6 +222,6 @@ def main(): cfg.CONF.register_opts(MetadataProxyHandler.OPTS) cfg.CONF(project='quantum') config.setup_logging(cfg.CONF) - + utils.log_opt_values(LOG) proxy = UnixDomainMetadataProxy(cfg.CONF) proxy.run() diff --git a/quantum/agent/metadata/namespace_proxy.py b/quantum/agent/metadata/namespace_proxy.py index b289464537..47f47db526 100644 --- a/quantum/agent/metadata/namespace_proxy.py +++ b/quantum/agent/metadata/namespace_proxy.py @@ -27,6 +27,7 @@ import webob from quantum.agent.linux import daemon from quantum.common import config +from quantum.common import utils from quantum.openstack.common import log as logging from quantum import wsgi @@ -151,9 +152,10 @@ def main(): ] cfg.CONF.register_cli_opts(opts) - cfg.CONF(project='quantum') + # Don't get the default configuration file + cfg.CONF(project='quantum', default_config_files=[]) config.setup_logging(cfg.CONF) - + utils.log_opt_values(LOG) proxy = ProxyDaemon(cfg.CONF.pid_file, cfg.CONF.metadata_port, network_id=cfg.CONF.network_id, diff --git a/quantum/common/utils.py b/quantum/common/utils.py index 5522e9a7ff..6df3cce898 100644 --- a/quantum/common/utils.py +++ b/quantum/common/utils.py @@ -21,6 +21,7 @@ """Utilities and helper functions.""" +import logging as std_logging import os import signal import socket @@ -188,3 +189,7 @@ def diff_list_of_dict(old_list, new_list): def is_extension_supported(plugin, ext_alias): return ext_alias in getattr( plugin, "supported_extension_aliases", []) + + +def log_opt_values(log): + cfg.CONF.log_opt_values(log, std_logging.DEBUG) diff --git a/quantum/tests/unit/test_dhcp_agent.py b/quantum/tests/unit/test_dhcp_agent.py index 721e1319b9..9ac8dea758 100644 --- a/quantum/tests/unit/test_dhcp_agent.py +++ b/quantum/tests/unit/test_dhcp_agent.py @@ -325,6 +325,55 @@ class TestDhcpAgent(base.BaseTestCase): self.assertFalse(dhcp.needs_resync) +class TestLogArgs(base.BaseTestCase): + + def test_log_args_without_log_dir_and_file(self): + conf_dict = {'debug': True, + 'verbose': False, + 'log_dir': None, + 'log_file': None} + conf = dhcp_agent.DictModel(conf_dict) + expected_args = ['--debug'] + args = config.get_log_args(conf, 'log_file_name') + self.assertEqual(expected_args, args) + + def test_log_args_without_log_file(self): + conf_dict = {'debug': True, + 'verbose': True, + 'log_dir': '/etc/tests', + 'log_file': None} + conf = dhcp_agent.DictModel(conf_dict) + expected_args = ['--debug', '--verbose', + '--log-file=log_file_name', + '--log-dir=/etc/tests'] + args = config.get_log_args(conf, 'log_file_name') + self.assertEqual(expected_args, args) + + def test_log_args_with_log_dir_and_file(self): + conf_dict = {'debug': True, + 'verbose': False, + 'log_dir': '/etc/tests', + 'log_file': 'tests/filelog'} + conf = dhcp_agent.DictModel(conf_dict) + expected_args = ['--debug', + '--log-file=log_file_name', + '--log-dir=/etc/tests/tests'] + args = config.get_log_args(conf, 'log_file_name') + self.assertEqual(expected_args, args) + + def test_log_args_without_log_dir(self): + conf_dict = {'debug': True, + 'verbose': False, + 'log_file': 'tests/filelog', + 'log_dir': None} + conf = dhcp_agent.DictModel(conf_dict) + expected_args = ['--debug', + '--log-file=log_file_name', + '--log-dir=tests'] + args = config.get_log_args(conf, 'log_file_name') + self.assertEqual(expected_args, args) + + class TestDhcpAgentEventHandler(base.BaseTestCase): def setUp(self): super(TestDhcpAgentEventHandler, self).setUp() @@ -359,6 +408,7 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): self.call_driver_p.stop() self.cache_p.stop() self.plugin_p.stop() + cfg.CONF.reset() super(TestDhcpAgentEventHandler, self).tearDown() def test_enable_dhcp_helper(self): @@ -477,6 +527,8 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): def test_enable_isolated_metadata_proxy_with_metadata_network(self): cfg.CONF.set_override('enable_metadata_network', True) + cfg.CONF.set_override('debug', True) + cfg.CONF.set_override('log_file', 'test.log') class_path = 'quantum.agent.linux.ip_lib.IPWrapper' self.external_process_p.stop() # Ensure the mock is restored if this test fail @@ -486,15 +538,18 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): ip_wrapper.assert_has_calls([mock.call( 'sudo', 'qdhcp-12345678-1234-5678-1234567890ab'), - mock.call().netns.execute(['quantum-ns-metadata-proxy', - mock.ANY, - '--router_id=forzanapoli', - mock.ANY, - mock.ANY]) + mock.call().netns.execute([ + 'quantum-ns-metadata-proxy', + mock.ANY, + '--router_id=forzanapoli', + mock.ANY, + mock.ANY, + '--debug', + ('--log-file=quantum-ns-metadata-proxy%s.log' % + fake_meta_network.id)]) ]) finally: self.external_process_p.start() - cfg.CONF.set_override('enable_metadata_network', False) def test_network_create_end(self): payload = dict(network=dict(id=fake_network.id)) diff --git a/quantum/tests/unit/test_metadata_agent.py b/quantum/tests/unit/test_metadata_agent.py index ce42edd7a4..8a0b02d395 100644 --- a/quantum/tests/unit/test_metadata_agent.py +++ b/quantum/tests/unit/test_metadata_agent.py @@ -24,6 +24,7 @@ import webob from quantum.agent.metadata import agent from quantum.tests import base +from quantum.common import utils class FakeConf(object): @@ -354,11 +355,12 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase): with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(agent, 'config') as config: with mock.patch.object(agent, 'cfg') as cfg: - agent.main() + with mock.patch.object(utils, 'cfg') as utils_cfg: + agent.main() - self.assertTrue(eventlet.called) - self.assertTrue(config.setup_logging.called) - proxy.assert_has_calls([ - mock.call(cfg.CONF), - mock.call().run()] - ) + self.assertTrue(eventlet.called) + self.assertTrue(config.setup_logging.called) + proxy.assert_has_calls([ + mock.call(cfg.CONF), + mock.call().run()] + ) diff --git a/quantum/tests/unit/test_metadata_namespace_proxy.py b/quantum/tests/unit/test_metadata_namespace_proxy.py index 6de98fe92f..4ad29645c5 100644 --- a/quantum/tests/unit/test_metadata_namespace_proxy.py +++ b/quantum/tests/unit/test_metadata_namespace_proxy.py @@ -24,6 +24,7 @@ import webob from quantum.agent.metadata import namespace_proxy as ns_proxy from quantum.tests import base +from quantum.common import utils class FakeConf(object): @@ -256,37 +257,43 @@ class TestProxyDaemon(base.BaseTestCase): with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(ns_proxy, 'config') as config: with mock.patch.object(ns_proxy, 'cfg') as cfg: - cfg.CONF.router_id = 'router_id' - cfg.CONF.network_id = None - cfg.CONF.metadata_port = 9697 - cfg.CONF.pid_file = 'pidfile' - cfg.CONF.daemonize = True - ns_proxy.main() + with mock.patch.object(utils, 'cfg') as utils_cfg: + cfg.CONF.router_id = 'router_id' + cfg.CONF.network_id = None + cfg.CONF.metadata_port = 9697 + cfg.CONF.pid_file = 'pidfile' + cfg.CONF.daemonize = True + utils_cfg.CONF.log_opt_values.return_value = None + ns_proxy.main() - self.assertTrue(eventlet.called) - self.assertTrue(config.setup_logging.called) - daemon.assert_has_calls([ - mock.call('pidfile', 9697, router_id='router_id', - network_id=None), - mock.call().start()] - ) + self.assertTrue(eventlet.called) + self.assertTrue(config.setup_logging.called) + daemon.assert_has_calls([ + mock.call('pidfile', 9697, + router_id='router_id', + network_id=None), + mock.call().start()] + ) def test_main_dont_fork(self): with mock.patch.object(ns_proxy, 'ProxyDaemon') as daemon: with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(ns_proxy, 'config') as config: with mock.patch.object(ns_proxy, 'cfg') as cfg: - cfg.CONF.router_id = 'router_id' - cfg.CONF.network_id = None - cfg.CONF.metadata_port = 9697 - cfg.CONF.pid_file = 'pidfile' - cfg.CONF.daemonize = False - ns_proxy.main() + with mock.patch.object(utils, 'cfg') as utils_cfg: + cfg.CONF.router_id = 'router_id' + cfg.CONF.network_id = None + cfg.CONF.metadata_port = 9697 + cfg.CONF.pid_file = 'pidfile' + cfg.CONF.daemonize = False + utils_cfg.CONF.log_opt_values.return_value = None + ns_proxy.main() - self.assertTrue(eventlet.called) - self.assertTrue(config.setup_logging.called) - daemon.assert_has_calls([ - mock.call('pidfile', 9697, router_id='router_id', - network_id=None), - mock.call().run()] - ) + self.assertTrue(eventlet.called) + self.assertTrue(config.setup_logging.called) + daemon.assert_has_calls([ + mock.call('pidfile', 9697, + router_id='router_id', + network_id=None), + mock.call().run()] + )