]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Move declaration of int_br_device_count earlier
authorDerek Higgins <derekh@redhat.com>
Wed, 4 Sep 2013 22:05:08 +0000 (23:05 +0100)
committerDerek Higgins <derekh@redhat.com>
Thu, 5 Sep 2013 14:00:37 +0000 (15:00 +0100)
_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

neutron/plugins/openvswitch/agent/ovs_neutron_agent.py
neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py

index b89faa905f417cb438db82338926119a06f06ef3..3a2435ee5a7ebc263e22adf34462c3baef05a994 100644 (file)
@@ -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)
index 294f30ee27c14cd7aaa2df13b04421dce7eec8c6..546c56c4dc84942f9f9802769cbe5f2a24cd0e65 100644 (file)
@@ -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()