]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Allow updating port 'binding:host_id' be None
authorshihanzhang <shihanzhang@huawei.com>
Mon, 11 May 2015 09:22:40 +0000 (17:22 +0800)
committershihanzhang <shihanzhang@huawei.com>
Thu, 14 May 2015 00:59:19 +0000 (08:59 +0800)
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
neutron/tests/unit/plugins/ml2/test_plugin.py

index 2f209db7723a58d27d69a9d8ce9882c69e05a5aa..f44a2a984f6754ef40dbbcf7eed1b6e8ddbc6503 100644 (file)
@@ -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):
index 21b90976a321b0639067c050d85924754c30fd30..8e3ec29589217de1d9a72c9801cb3d5455740a0c 100644 (file)
@@ -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(