From 66c9fe253c71d2713f0db894fe50b5afa0ac411c Mon Sep 17 00:00:00 2001 From: Yong Sheng Gong Date: Thu, 19 Sep 2013 12:45:31 +0800 Subject: [PATCH] add portbinding host into vip port When creating vip port, add 'host' into port data. Bug #1227091 Change-Id: I0f59b3b56a4a26561a10e5645c8ebf803b2c6a70 --- .../drivers/haproxy/plugin_driver.py | 3 ++- .../drivers/haproxy/test_plugin_driver.py | 20 +++++++++++++++++++ neutron/tests/unit/testlib_api.py | 10 ++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/neutron/services/loadbalancer/drivers/haproxy/plugin_driver.py b/neutron/services/loadbalancer/drivers/haproxy/plugin_driver.py index ee703a9011..1f402b35be 100644 --- a/neutron/services/loadbalancer/drivers/haproxy/plugin_driver.py +++ b/neutron/services/loadbalancer/drivers/haproxy/plugin_driver.py @@ -26,6 +26,7 @@ from neutron.common import rpc as q_rpc from neutron.db import agents_db from neutron.db.loadbalancer import loadbalancer_db from neutron.extensions import lbaas_agentscheduler +from neutron.extensions import portbindings from neutron.openstack.common import importutils from neutron.openstack.common import log as logging from neutron.openstack.common import rpc @@ -168,7 +169,7 @@ class LoadBalancerCallbacks(object): port['admin_state_up'] = True port['device_owner'] = 'neutron:' + constants.LOADBALANCER port['device_id'] = str(uuid.uuid5(uuid.NAMESPACE_DNS, str(host))) - + port[portbindings.HOST_ID] = host self.plugin._core_plugin.update_port( context, port_id, diff --git a/neutron/tests/unit/services/loadbalancer/drivers/haproxy/test_plugin_driver.py b/neutron/tests/unit/services/loadbalancer/drivers/haproxy/test_plugin_driver.py index 2146250717..8f18ffbb49 100644 --- a/neutron/tests/unit/services/loadbalancer/drivers/haproxy/test_plugin_driver.py +++ b/neutron/tests/unit/services/loadbalancer/drivers/haproxy/test_plugin_driver.py @@ -22,6 +22,7 @@ from neutron.common import exceptions from neutron import context from neutron.db.loadbalancer import loadbalancer_db as ldb from neutron.db import servicetype_db as st_db +from neutron.extensions import portbindings from neutron import manager from neutron.openstack.common import uuidutils from neutron.plugins.common import constants @@ -30,6 +31,7 @@ from neutron.services.loadbalancer.drivers.haproxy import ( ) from neutron.tests import base from neutron.tests.unit.db.loadbalancer import test_db_loadbalancer +from neutron.tests.unit import testlib_api class TestLoadBalancerPluginBase( @@ -275,6 +277,24 @@ class TestLoadBalancerCallbacks(TestLoadBalancerPluginBase): host='host' ) + def test_plug_vip_port_mock_with_host(self): + exp = { + 'device_owner': 'neutron:' + constants.LOADBALANCER, + 'device_id': 'c596ce11-db30-5c72-8243-15acaae8690f', + 'admin_state_up': True, + portbindings.HOST_ID: 'host' + } + with mock.patch.object( + self.plugin._core_plugin, 'update_port') as mock_update_port: + with self.pool() as pool: + with self.vip(pool=pool) as vip: + ctx = context.get_admin_context() + self.callbacks.plug_vip_port( + ctx, port_id=vip['vip']['port_id'], host='host') + mock_update_port.assert_called_once_with( + ctx, vip['vip']['port_id'], + {'port': testlib_api.SubDictMatch(exp)}) + def test_unplug_vip_port(self): exp = { 'device_owner': '', diff --git a/neutron/tests/unit/testlib_api.py b/neutron/tests/unit/testlib_api.py index 9cb6f15adb..1407ab0b80 100644 --- a/neutron/tests/unit/testlib_api.py +++ b/neutron/tests/unit/testlib_api.py @@ -72,3 +72,13 @@ class WebTestCase(base.BaseTestCase): result = wsgi.Serializer( attributes.get_attr_metadata()).serialize(data, ctype) return result + + +class SubDictMatch(object): + + def __init__(self, sub_dict): + self.sub_dict = sub_dict + + def __eq__(self, super_dict): + return all(item in super_dict.items() + for item in self.sub_dict.items())