From becadd39df087544ac1ce97926cc3014fd286e08 Mon Sep 17 00:00:00 2001 From: Derek Higgins Date: Wed, 4 Sep 2013 23:05:08 +0100 Subject: [PATCH] Move declaration of int_br_device_count earlier _report_state is being called by setup_rpc so int_br_device_count needs to be initialized earlier. To avoid AttributeError: object has no attribute 'int_br_device_count' This wasn't caught by unit tests for 3 separate reason o The reference to self.int_br_device_count is wrapped in except Exception: log / pass - This reference has been moved outside of the try/except o Unit tests set report_interval to 0 so the heartbeat wasn't called during unit tests. - now removed o The function passed into FixedIntervalLoopingCall isn't started anyways so wasn't calling self._report_state - replaced FixedIntervalLoopingCall with a mock that calls the function once. Fixes bug #1221054 Change-Id: I572af4ee017c1f7f016c8f9981ca54d1301442d1 --- .../plugins/openvswitch/agent/ovs_neutron_agent.py | 12 ++++++------ .../unit/openvswitch/test_ovs_neutron_agent.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py b/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py index b89faa905..3a2435ee5 100644 --- a/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py +++ b/neutron/plugins/openvswitch/agent/ovs_neutron_agent.py @@ -179,6 +179,9 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin): 'agent_type': q_const.AGENT_TYPE_OVS, 'start_flag': True} + # Keep track of int_br's device count for use by _report_state() + self.int_br_device_count = 0 + self.int_br = ovs_lib.OVSBridge(integ_br, self.root_helper) self.setup_rpc() self.setup_integration_br() @@ -202,9 +205,6 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin): # Collect additional bridges to monitor self.ancillary_brs = self.setup_ancillary_bridges(integ_br, tun_br) - # Keep track of int_br's device count for use by _report_state() - self.int_br_device_count = 0 - # Security group agent supprot self.sg_agent = OVSSecurityGroupAgent(self.context, self.plugin_rpc, @@ -216,10 +216,10 @@ class OVSNeutronAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin): self.root_helper) def _report_state(self): + # How many devices are likely used by a VM + self.agent_state.get('configurations')['devices'] = ( + self.int_br_device_count) try: - # How many devices are likely used by a VM - self.agent_state.get('configurations')['devices'] = ( - self.int_br_device_count) self.state_rpc.report_state(self.context, self.agent_state) self.agent_state.pop('start_flag', None) diff --git a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py index 294f30ee2..546c56c4d 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py +++ b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py @@ -96,9 +96,15 @@ class TestOvsNeutronAgent(base.BaseTestCase): # Avoid rpc initialization for unit tests cfg.CONF.set_override('rpc_backend', 'neutron.openstack.common.rpc.impl_fake') - cfg.CONF.set_override('report_interval', 0, 'AGENT') kwargs = ovs_neutron_agent.create_agent_config_map(cfg.CONF) + class MockFixedIntervalLoopingCall(object): + def __init__(self, f): + self.f = f + + def start(self, interval=0): + self.f() + with contextlib.nested( mock.patch('neutron.plugins.openvswitch.agent.ovs_neutron_agent.' 'OVSNeutronAgent.setup_integration_br', @@ -110,7 +116,10 @@ class TestOvsNeutronAgent(base.BaseTestCase): 'get_local_port_mac', return_value='00:00:00:00:00:01'), mock.patch('neutron.agent.linux.utils.get_interface_mac', - return_value='00:00:00:00:00:01')): + return_value='00:00:00:00:00:01'), + mock.patch('neutron.openstack.common.loopingcall.' + 'FixedIntervalLoopingCall', + new=MockFixedIntervalLoopingCall)): self.agent = ovs_neutron_agent.OVSNeutronAgent(**kwargs) self.agent.tun_br = mock.Mock() self.agent.sg_agent = mock.Mock() -- 2.45.2