From acb5fba408f7a8e4a429a91a46ed5c764ade57a4 Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Mon, 9 Sep 2019 09:51:30 -0400 Subject: [PATCH] Only notify nova of port status changes if configured Although notify_nova_on_port_status_changes defaults to true, it could be to false, making the nova_notifier attribute unsafe to use without checking. This patch checks both the config option and that the attribute exists, since the config could be changed after the plugin is already initialized without the nova_notifier attribute being set. Change-Id: Ide0f93275e60dffda10b7da59f6d81c5582c3849 Closes-bug: #1843269 (cherry picked from commit ab4320edb4661f74465ec49166f8d35b8216035f) --- neutron/plugins/ml2/rpc.py | 8 +++++++- neutron/tests/unit/plugins/ml2/test_rpc.py | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/neutron/plugins/ml2/rpc.py b/neutron/plugins/ml2/rpc.py index ddcc87dc7a9..95c9c96b3d9 100644 --- a/neutron/plugins/ml2/rpc.py +++ b/neutron/plugins/ml2/rpc.py @@ -23,6 +23,7 @@ from neutron_lib.plugins import directory from neutron_lib.plugins.ml2 import api from neutron_lib import rpc as n_rpc from neutron_lib.services.qos import constants as qos_consts +from oslo_config import cfg from oslo_log import log import oslo_messaging from sqlalchemy.orm import exc @@ -277,7 +278,12 @@ class RpcCallbacks(type_tunnel.TunnelRpcCallbackMixin): else: if port.device_owner.startswith( n_const.DEVICE_OWNER_COMPUTE_PREFIX): - plugin.nova_notifier.notify_port_active_direct(port) + # NOTE(haleyb): It is possible for a test to override a + # config option after the plugin has been initialized so + # the nova_notifier attribute is not set on the plugin. + if (cfg.CONF.notify_nova_on_port_status_changes and + hasattr(plugin, 'nova_notifier')): + plugin.nova_notifier.notify_port_active_direct(port) return else: self.update_port_status_to_active(port, rpc_context, port_id, host) diff --git a/neutron/tests/unit/plugins/ml2/test_rpc.py b/neutron/tests/unit/plugins/ml2/test_rpc.py index 4fa7d263f3a..4a644b616b0 100644 --- a/neutron/tests/unit/plugins/ml2/test_rpc.py +++ b/neutron/tests/unit/plugins/ml2/test_rpc.py @@ -240,6 +240,12 @@ class RpcCallbacksTestCase(base.BaseTestCase): (self.plugin.nova_notifier.notify_port_active_direct. assert_called_once_with(port)) + def test_update_device_up_with_device_not_bound_to_host_no_notify(self): + cfg.CONF.set_override('notify_nova_on_port_status_changes', False) + self.assertIsNone(self._test_update_device_not_bound_to_host( + self.callbacks.update_device_up)) + self.plugin.nova_notifier.notify_port_active_direct.assert_not_called() + def test_update_device_down_with_device_not_bound_to_host(self): self.assertEqual( {'device': 'fake_device', 'exists': True},