From a397df38c748d482ece18e164084a589d7f2d362 Mon Sep 17 00:00:00 2001 From: gongysh Date: Tue, 9 Apr 2013 09:11:32 +0800 Subject: [PATCH] Don't run metadata proxy when it is not needed Bug #1159015 Change-Id: I6063e75525a2a727d063bb1745c897ac8c4ba3e8 --- quantum/agent/dhcp_agent.py | 10 +-- quantum/tests/unit/test_dhcp_agent.py | 89 ++++++++++++++++++--------- 2 files changed, 67 insertions(+), 32 deletions(-) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index 7c64d8d825..ad22e03049 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -64,8 +64,8 @@ class DhcpAgent(manager.Manager): help=_("Support Metadata requests on isolated networks.")), cfg.BoolOpt('enable_metadata_network', default=False, help=_("Allows for serving metadata requests from a " - "dedicate network. Requires " - "enable isolated_metadata = True ")), + "dedicated network. Requires " + "enable_isolated_metadata = True")), ] def __init__(self, host=None): @@ -184,7 +184,8 @@ class DhcpAgent(manager.Manager): for subnet in network.subnets: if subnet.enable_dhcp: if self.call_driver('enable', network): - if self.conf.use_namespaces: + if (self.conf.use_namespaces and + self.conf.enable_isolated_metadata): self.enable_isolated_metadata_proxy(network) self.cache.put(network) break @@ -193,7 +194,8 @@ class DhcpAgent(manager.Manager): """Disable DHCP for a network known to the agent.""" network = self.cache.get_network_by_id(network_id) if network: - if self.conf.use_namespaces: + if (self.conf.use_namespaces and + self.conf.enable_isolated_metadata): self.disable_isolated_metadata_proxy(network) if self.call_driver('disable', network): self.cache.remove(network) diff --git a/quantum/tests/unit/test_dhcp_agent.py b/quantum/tests/unit/test_dhcp_agent.py index 803958dd71..2c96d6b800 100644 --- a/quantum/tests/unit/test_dhcp_agent.py +++ b/quantum/tests/unit/test_dhcp_agent.py @@ -439,21 +439,32 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): cfg.CONF.reset() super(TestDhcpAgentEventHandler, self).tearDown() - def test_enable_dhcp_helper(self): + def _enable_dhcp_helper(self, isolated_metadata=False): + if isolated_metadata: + cfg.CONF.set_override('enable_isolated_metadata', True) self.plugin.get_network_info.return_value = fake_network self.dhcp.enable_dhcp_helper(fake_network.id) self.plugin.assert_has_calls( [mock.call.get_network_info(fake_network.id)]) self.call_driver.assert_called_once_with('enable', fake_network) self.cache.assert_has_calls([mock.call.put(fake_network)]) - self.external_process.assert_has_calls([ - mock.call( - cfg.CONF, - '12345678-1234-5678-1234567890ab', - 'sudo', - 'qdhcp-12345678-1234-5678-1234567890ab'), - mock.call().enable(mock.ANY) - ]) + if isolated_metadata: + self.external_process.assert_has_calls([ + mock.call( + cfg.CONF, + '12345678-1234-5678-1234567890ab', + 'sudo', + 'qdhcp-12345678-1234-5678-1234567890ab'), + mock.call().enable(mock.ANY) + ]) + else: + self.assertFalse(self.external_process.call_count) + + def test_enable_dhcp_helper_enable_isolated_metadata(self): + self._enable_dhcp_helper(isolated_metadata=True) + + def test_enable_dhcp_helper(self): + self._enable_dhcp_helper() def test_enable_dhcp_helper_down_network(self): self.plugin.get_network_info.return_value = fake_down_network @@ -486,30 +497,43 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): self.assertFalse(self.cache.called) self.assertFalse(self.external_process.called) - def test_disable_dhcp_helper_known_network(self): + def _disable_dhcp_helper_known_network(self, isolated_metadata=False): + if isolated_metadata: + cfg.CONF.set_override('enable_isolated_metadata', True) self.cache.get_network_by_id.return_value = fake_network self.dhcp.disable_dhcp_helper(fake_network.id) self.cache.assert_has_calls( [mock.call.get_network_by_id(fake_network.id)]) self.call_driver.assert_called_once_with('disable', fake_network) - self.external_process.assert_has_calls([ - mock.call( - cfg.CONF, - '12345678-1234-5678-1234567890ab', - 'sudo', - 'qdhcp-12345678-1234-5678-1234567890ab'), - mock.call().disable() - ]) + if isolated_metadata: + self.external_process.assert_has_calls([ + mock.call( + cfg.CONF, + '12345678-1234-5678-1234567890ab', + 'sudo', + 'qdhcp-12345678-1234-5678-1234567890ab'), + mock.call().disable() + ]) + else: + self.assertFalse(self.external_process.call_count) + + def test_disable_dhcp_helper_known_network_isolated_metadata(self): + self._disable_dhcp_helper_known_network(isolated_metadata=True) + + def test_disable_dhcp_helper_known_network(self): + self._disable_dhcp_helper_known_network() def test_disable_dhcp_helper_unknown_network(self): self.cache.get_network_by_id.return_value = None self.dhcp.disable_dhcp_helper('abcdef') self.cache.assert_has_calls( [mock.call.get_network_by_id('abcdef')]) - self.assertEqual(self.call_driver.call_count, 0) + self.assertEqual(0, self.call_driver.call_count) self.assertFalse(self.external_process.called) - def test_disable_dhcp_helper_driver_failure(self): + def _disable_dhcp_helper_driver_failure(self, isolated_metadata=False): + if isolated_metadata: + cfg.CONF.set_override('enable_isolated_metadata', True) self.cache.get_network_by_id.return_value = fake_network self.call_driver.return_value = False self.dhcp.disable_dhcp_helper(fake_network.id) @@ -518,14 +542,23 @@ class TestDhcpAgentEventHandler(base.BaseTestCase): self.call_driver.assert_called_once_with('disable', fake_network) self.cache.assert_has_calls( [mock.call.get_network_by_id(fake_network.id)]) - self.external_process.assert_has_calls([ - mock.call( - cfg.CONF, - '12345678-1234-5678-1234567890ab', - 'sudo', - 'qdhcp-12345678-1234-5678-1234567890ab'), - mock.call().disable() - ]) + if isolated_metadata: + self.external_process.assert_has_calls([ + mock.call( + cfg.CONF, + '12345678-1234-5678-1234567890ab', + 'sudo', + 'qdhcp-12345678-1234-5678-1234567890ab'), + mock.call().disable() + ]) + else: + self.assertFalse(self.external_process.call_count) + + def test_disable_dhcp_helper_driver_failure_isolated_metadata(self): + self._disable_dhcp_helper_driver_failure(isolated_metadata=True) + + def test_disable_dhcp_helper_driver_failure(self): + self._disable_dhcp_helper_driver_failure() def test_enable_isolated_metadata_proxy(self): class_path = 'quantum.agent.linux.external_process.ProcessManager'