From 050536c66e4f8cdc6c7aa3ff77c0e9995db5717a Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Fri, 19 May 2023 16:52:16 +0000 Subject: [PATCH] Stop the RPC connections when the agent exits This behaviour is already present in the OVS agent RPC plugin; this patch is imitating that in the ``CommonAgentLoop`` class. Closes-Bug: #2019314 Change-Id: I8c87e5ff7b948455c62e2fa2a3c89b8b1e45875c --- neutron/plugins/ml2/drivers/agent/_common_agent.py | 3 +++ .../plugins/ml2/drivers/agent/test__common_agent.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/neutron/plugins/ml2/drivers/agent/_common_agent.py b/neutron/plugins/ml2/drivers/agent/_common_agent.py index 24b2f5cc041..12c14db87dd 100644 --- a/neutron/plugins/ml2/drivers/agent/_common_agent.py +++ b/neutron/plugins/ml2/drivers/agent/_common_agent.py @@ -67,6 +67,7 @@ class CommonAgentLoop(service.Service): self.quitting_rpc_timeout = quitting_rpc_timeout self.agent_type = agent_type self.agent_binary = agent_binary + self.connection = None def _validate_manager_class(self): if not isinstance(self.mgr, @@ -115,6 +116,8 @@ class CommonAgentLoop(service.Service): LOG.info("Stopping %s agent.", self.agent_type) if graceful and self.quitting_rpc_timeout: self.set_rpc_timeout(self.quitting_rpc_timeout) + if self.connection: + self.connection.close() super(CommonAgentLoop, self).stop(graceful) def reset(self): diff --git a/neutron/tests/unit/plugins/ml2/drivers/agent/test__common_agent.py b/neutron/tests/unit/plugins/ml2/drivers/agent/test__common_agent.py index 84ea4b443ac..3e55b516c9e 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/agent/test__common_agent.py +++ b/neutron/tests/unit/plugins/ml2/drivers/agent/test__common_agent.py @@ -25,6 +25,7 @@ from oslo_config import cfg import testtools from neutron.agent.linux import bridge_lib +from neutron.conf.plugins.ml2.drivers import linuxbridge as conf_lb from neutron.plugins.ml2.drivers.agent import _agent_manager_base as amb from neutron.plugins.ml2.drivers.agent import _common_agent as ca from neutron.tests import base @@ -48,7 +49,8 @@ PORT_DATA = { class TestCommonAgentLoop(base.BaseTestCase): def setUp(self): - super(TestCommonAgentLoop, self).setUp() + super().setUp() + conf_lb.register_linuxbridge_opts(cfg=cfg.CONF) # disable setting up periodic state reporting cfg.CONF.set_override('report_interval', 0, 'AGENT') cfg.CONF.set_default('firewall_driver', @@ -613,3 +615,10 @@ class TestCommonAgentLoop(base.BaseTestCase): ) self.assertNotIn(NETWORK_ID, self.agent.network_ports.keys()) self.assertEqual(port_2_data['port_id'], cleaned_port_id) + + def test_stop(self): + mock_connection = mock.Mock() + self.agent.connection = mock_connection + with mock.patch.object(self.agent, 'set_rpc_timeout'): + self.agent.stop() + mock_connection.close.assert_called_once()