Fix HA router initialization exception

When an HA router initialization fails early, it can lead to:

 AttributeError: 'HaRouter' object has no attribute 'process_monitor'

Add init of 'self.process_monitor' in RouterInfo init code in
case we try and cleanup early.

Change-Id: Iddeaeef13adee10f7b130e3f9e584b6e9f037030
Closes-bug: #1735557
(cherry picked from commit c62d54d0c2)
This commit is contained in:
Brian Haley 2017-11-30 16:47:43 -05:00 committed by Brian Haley
parent 29aafaf5f9
commit c3fa8f008e
3 changed files with 23 additions and 1 deletions

View File

@ -429,7 +429,8 @@ class HaRouter(router.RouterInfo):
prefix=router.EXTERNAL_DEV_PREFIX)
def delete(self):
self.destroy_state_change_monitor(self.process_monitor)
if self.process_monitor:
self.destroy_state_change_monitor(self.process_monitor)
self.disable_keepalived()
self.ha_network_removed()
super(HaRouter, self).delete()

View File

@ -77,6 +77,7 @@ class RouterInfo(object):
self.routes = []
self.agent_conf = agent_conf
self.driver = interface_driver
self.process_monitor = None
# radvd is a neutron.agent.linux.ra.DaemonMonitor
self.radvd = None

View File

@ -34,6 +34,7 @@ from neutron.agent.common import config as agent_config
from neutron.agent.l3 import agent as l3_agent
from neutron.agent.l3 import dvr_edge_router as dvr_router
from neutron.agent.l3 import dvr_snat_ns
from neutron.agent.l3 import ha_router
from neutron.agent.l3 import legacy_router
from neutron.agent.l3 import link_local_allocator as lla
from neutron.agent.l3 import namespace_manager
@ -3071,3 +3072,22 @@ class TestBasicRouterOperations(BasicRouterOperationsFramework):
ri._create_dvr_gateway(ex_gw_port, interface_name)
self._verify_address_scopes_iptables_rule(
ri.snat_iptables_manager)
@mock.patch.object(l3router.RouterInfo, 'delete')
@mock.patch.object(ha_router.HaRouter, 'destroy_state_change_monitor')
def test_delete_ha_router_initialize_fails(self, mock_dscm, mock_delete):
router = l3_test_common.prepare_router_data(enable_ha=True)
router[lib_constants.HA_INTERFACE_KEY] = None
agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
# an early failure of an HA router initiailization shouldn't try
# and cleanup a state change monitor process that was never spawned.
# Cannot use self.assertRaises(Exception, ...) as that causes an H202
# pep8 failure.
try:
agent._router_added(router['id'], router)
raise Exception("agent._router_added() should have raised an "
"exception")
except Exception:
pass
self.assertTrue(mock_delete.called)
self.assertFalse(mock_dscm.called)