From c003b450b34dcbb2e67b3ffb573cf68f23eb213f Mon Sep 17 00:00:00 2001 From: shihanzhang Date: Mon, 11 May 2015 17:22:40 +0800 Subject: [PATCH] Allow updating port 'binding:host_id' be None with ml2 plugin, it should allow updating port 'binding:host_id' be None directly, there is already a bug in nova#1441419. Change-Id: I93e4c513e40a7cf5740dde6c658e2470788d716a Closes-Bug: #1453715 --- neutron/plugins/ml2/plugin.py | 5 ++- neutron/tests/unit/plugins/ml2/test_plugin.py | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 2f209db77..f44a2a984 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -207,7 +207,10 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, port_id = port['id'] changes = False - host = attrs and attrs.get(portbindings.HOST_ID) + host = attributes.ATTR_NOT_SPECIFIED + if attrs and portbindings.HOST_ID in attrs: + host = attrs.get(portbindings.HOST_ID) or '' + original_host = binding.host if (attributes.is_attr_set(host) and original_host != host): diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 21b90976a..8e3ec2958 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -806,6 +806,40 @@ class TestMl2PortBinding(Ml2PluginV2TestCase, port = self._show('ports', port_id)['port'] self._check_port_binding_profile(port, profile) + def test_update_port_binding_host_id_none(self): + with self.port() as port: + plugin = manager.NeutronManager.get_plugin() + binding = ml2_db.get_locked_port_and_binding(self.context.session, + port['port']['id'])[1] + binding['host'] = 'test' + mech_context = driver_context.PortContext( + plugin, self.context, port['port'], + plugin.get_network(self.context, port['port']['network_id']), + binding, None) + with mock.patch('neutron.plugins.ml2.plugin.Ml2Plugin.' + '_update_port_dict_binding') as update_mock: + attrs = {portbindings.HOST_ID: None} + plugin._process_port_binding(mech_context, attrs) + self.assertTrue(update_mock.mock_calls) + self.assertEqual('', binding.host) + + def test_update_port_binding_host_id_not_changed(self): + with self.port() as port: + plugin = manager.NeutronManager.get_plugin() + binding = ml2_db.get_locked_port_and_binding(self.context.session, + port['port']['id'])[1] + binding['host'] = 'test' + mech_context = driver_context.PortContext( + plugin, self.context, port['port'], + plugin.get_network(self.context, port['port']['network_id']), + binding, None) + with mock.patch('neutron.plugins.ml2.plugin.Ml2Plugin.' + '_update_port_dict_binding') as update_mock: + attrs = {portbindings.PROFILE: {'e': 5}} + plugin._process_port_binding(mech_context, attrs) + self.assertTrue(update_mock.mock_calls) + self.assertEqual('test', binding.host) + def test_process_dvr_port_binding_update_router_id(self): host_id = 'host' binding = models.DVRPortBinding( -- 2.45.2