]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Drop RpcProxy usage from neutron.agent.rpc.PluginApi
authorRussell Bryant <rbryant@redhat.com>
Fri, 14 Nov 2014 14:21:05 +0000 (14:21 +0000)
committerRussell Bryant <rbryant@redhat.com>
Wed, 19 Nov 2014 17:18:34 +0000 (17:18 +0000)
This patch removes the usage of the RpcProxy compatibility class from
the neutron.agent.rpc.PluginApi class.  The equivalent use of
oslo.messaging APIs have been put in place instead.  This simple
conversion had a pretty wide impact on unit tests, as well.

The security groups API was converted in this patch as well.  It was
necessary because the security group class is used as a mixin, so it
must be implemented the same way.  Unfortunately, the way this is used
as a mix-in is not consistent, so for now it's only conditionally
converted.

Finally, some other miscellaneous plugin specific interfaces were
converted as well.  Again, these were methods mixed-in for certain
plugins.

Note that there's one very minor functional difference in this patch.
The previous code set the base version to be '1.1'.  The right pattern
is for this to be set to '1.0'.  This version is the default version
specified by the client, telling the server that it must implement at
least this version to satisfy the request.  The default should be
'1.0' and methods that require higher than that should specify it.
From looking at other parts of the code, '1.0' vs '1.1' is not
actually important, as '1.1' was actually the addition of some
security group methods defined elsewhere.  The correction is more
about establishing the right pattern to follow.

Change-Id: I391c01e79943ef179d815ea602253720925ccce1

neutron/agent/rpc.py
neutron/agent/securitygroups_rpc.py
neutron/plugins/nec/agent/nec_neutron_agent.py
neutron/plugins/ryu/agent/ryu_neutron_agent.py
neutron/tests/unit/hyperv/test_hyperv_rpcapi.py
neutron/tests/unit/ml2/test_rpcapi.py
neutron/tests/unit/mlnx/test_rpcapi.py
neutron/tests/unit/nec/test_nec_agent.py
neutron/tests/unit/ryu/test_ryu_agent.py
neutron/tests/unit/test_agent_rpc.py
neutron/tests/unit/test_security_groups_rpc.py

index 06e3ae6d42b605deb7ee631f03b7534859b7fb45..48876ccdda213972640e5c4cec702c9fdfe284a6 100644 (file)
@@ -69,7 +69,7 @@ class PluginReportStateAPI(object):
         return method(context, 'report_state', **kwargs)
 
 
-class PluginApi(n_rpc.RpcProxy):
+class PluginApi(object):
     '''Agent side of the rpc API.
 
     API version history:
@@ -79,51 +79,45 @@ class PluginApi(n_rpc.RpcProxy):
               the device port
     '''
 
-    BASE_RPC_API_VERSION = '1.1'
-
     def __init__(self, topic):
-        super(PluginApi, self).__init__(
-            topic=topic, default_version=self.BASE_RPC_API_VERSION)
+        target = messaging.Target(topic=topic, version='1.0')
+        self.client = n_rpc.get_client(target)
 
     def get_device_details(self, context, device, agent_id, host=None):
-        return self.call(context,
-                         self.make_msg('get_device_details', device=device,
-                                       agent_id=agent_id, host=host))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'get_device_details', device=device,
+                          agent_id=agent_id, host=host)
 
     def get_devices_details_list(self, context, devices, agent_id, host=None):
         res = []
         try:
-            res = self.call(context,
-                            self.make_msg('get_devices_details_list',
-                                          devices=devices,
-                                          agent_id=agent_id,
-                                          host=host),
-                            version='1.3')
+            cctxt = self.client.prepare(version='1.3')
+            res = cctxt.call(context, 'get_devices_details_list',
+                             devices=devices, agent_id=agent_id, host=host)
         except messaging.UnsupportedVersion:
             # If the server has not been upgraded yet, a DVR-enabled agent
             # may not work correctly, however it can function in 'degraded'
             # mode, in that DVR routers may not be in the system yet, and
             # it might be not necessary to retrieve info about the host.
             LOG.warn(_LW('DVR functionality requires a server upgrade.'))
+            cctxt = self.client.prepare()
             res = [
-                self.call(context,
-                          self.make_msg('get_device_details', device=device,
-                                        agent_id=agent_id, host=host))
+                self.get_device_details(context, device, agent_id, host)
                 for device in devices
             ]
         return res
 
     def update_device_down(self, context, device, agent_id, host=None):
-        return self.call(context,
-                         self.make_msg('update_device_down', device=device,
-                                       agent_id=agent_id, host=host))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'update_device_down', device=device,
+                          agent_id=agent_id, host=host)
 
     def update_device_up(self, context, device, agent_id, host=None):
-        return self.call(context,
-                         self.make_msg('update_device_up', device=device,
-                                       agent_id=agent_id, host=host))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'update_device_up', device=device,
+                          agent_id=agent_id, host=host)
 
     def tunnel_sync(self, context, tunnel_ip, tunnel_type=None):
-        return self.call(context,
-                         self.make_msg('tunnel_sync', tunnel_ip=tunnel_ip,
-                                       tunnel_type=tunnel_type))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'tunnel_sync', tunnel_ip=tunnel_ip,
+                          tunnel_type=tunnel_type)
index ee898b213d4814f22af0ba283ed482248e6799d8..535a0a0dbc29a6f1cae065c56d04b8b18b5bc8d8 100644 (file)
@@ -89,18 +89,16 @@ class SecurityGroupServerRpcApiMixin(object):
     def security_group_rules_for_devices(self, context, devices):
         LOG.debug("Get security group rules "
                   "for devices via rpc %r", devices)
-        return self.call(context,
-                         self.make_msg('security_group_rules_for_devices',
-                                       devices=devices),
-                         version='1.1')
+        cctxt = self.client.prepare(version='1.1')
+        return cctxt.call(context, 'security_group_rules_for_devices',
+                          devices=devices)
 
     def security_group_info_for_devices(self, context, devices):
         LOG.debug("Get security group information for devices via rpc %r",
                   devices)
-        return self.call(context,
-                         self.make_msg('security_group_info_for_devices',
-                                       devices=devices),
-                         version='1.2')
+        cctxt = self.client.prepare(version='1.2')
+        return cctxt.call(context, 'security_group_info_for_devices',
+                          devices=devices)
 
 
 class SecurityGroupAgentRpcCallbackMixin(object):
@@ -354,6 +352,10 @@ class SecurityGroupAgentRpcMixin(object):
                 self.refresh_firewall(updated_devices)
 
 
+# NOTE(russellb) This class has been conditionally converted to use the
+# oslo.messaging APIs because it's a mix-in used in different places.  The
+# conditional usage is temporary until the whole code base has been converted
+# to stop using the RpcProxy compatibility class.
 class SecurityGroupAgentRpcApiMixin(object):
 
     def _get_security_group_topic(self):
@@ -365,25 +367,45 @@ class SecurityGroupAgentRpcApiMixin(object):
         """Notify rule updated security groups."""
         if not security_groups:
             return
-        self.fanout_cast(context,
-                         self.make_msg('security_groups_rule_updated',
-                                       security_groups=security_groups),
-                         version=SG_RPC_VERSION,
-                         topic=self._get_security_group_topic())
+        if hasattr(self, 'client'):
+            cctxt = self.client.prepare(version=SG_RPC_VERSION,
+                                        topic=self._get_security_group_topic(),
+                                        fanout=True)
+            cctxt.cast(context, 'security_groups_rule_updated',
+                       security_groups=security_groups)
+        else:
+            self.fanout_cast(context,
+                             self.make_msg('security_groups_rule_updated',
+                                           security_groups=security_groups),
+                             version=SG_RPC_VERSION,
+                             topic=self._get_security_group_topic())
 
     def security_groups_member_updated(self, context, security_groups):
         """Notify member updated security groups."""
         if not security_groups:
             return
-        self.fanout_cast(context,
-                         self.make_msg('security_groups_member_updated',
-                                       security_groups=security_groups),
-                         version=SG_RPC_VERSION,
-                         topic=self._get_security_group_topic())
+        if hasattr(self, 'client'):
+            cctxt = self.client.prepare(version=SG_RPC_VERSION,
+                                        topic=self._get_security_group_topic(),
+                                        fanout=True)
+            cctxt.cast(context, 'security_groups_member_updated',
+                       security_groups=security_groups)
+        else:
+            self.fanout_cast(context,
+                             self.make_msg('security_groups_member_updated',
+                                           security_groups=security_groups),
+                             version=SG_RPC_VERSION,
+                             topic=self._get_security_group_topic())
 
     def security_groups_provider_updated(self, context):
         """Notify provider updated security groups."""
-        self.fanout_cast(context,
-                         self.make_msg('security_groups_provider_updated'),
-                         version=SG_RPC_VERSION,
-                         topic=self._get_security_group_topic())
+        if hasattr(self, 'client'):
+            cctxt = self.client.prepare(version=SG_RPC_VERSION,
+                                        topic=self._get_security_group_topic(),
+                                        fanout=True)
+            cctxt.cast(context, 'security_groups_member_updated')
+        else:
+            self.fanout_cast(context,
+                             self.make_msg('security_groups_provider_updated'),
+                             version=SG_RPC_VERSION,
+                             topic=self._get_security_group_topic())
index 9c3ec1a496f4ae1d595be275dbc386503989da32..1623bfa8abc68b092ff60dc8c32b8931527ce235 100755 (executable)
@@ -43,7 +43,6 @@ LOG = logging.getLogger(__name__)
 
 
 class NECPluginApi(agent_rpc.PluginApi):
-    BASE_RPC_API_VERSION = '1.0'
 
     def update_ports(self, context, agent_id, datapath_id,
                      port_added, port_removed):
@@ -51,13 +50,12 @@ class NECPluginApi(agent_rpc.PluginApi):
         LOG.info(_("Update ports: added=%(added)s, "
                    "removed=%(removed)s"),
                  {'added': port_added, 'removed': port_removed})
-        self.call(context,
-                  self.make_msg('update_ports',
-                                topic=topics.AGENT,
-                                agent_id=agent_id,
-                                datapath_id=datapath_id,
-                                port_added=port_added,
-                                port_removed=port_removed))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'update_ports',
+                          agent_id=agent_id,
+                          datapath_id=datapath_id,
+                          port_added=port_added,
+                          port_removed=port_removed)
 
 
 class NECAgentRpcCallback(n_rpc.RpcCallback):
index b1e1330d0f96a568af202075bc64aa53ae5737c7..06ea0bca2e0fb7c724879e9b44f1cb4df9dcd70a 100755 (executable)
@@ -166,8 +166,8 @@ class RyuPluginApi(agent_rpc.PluginApi,
                    sg_rpc.SecurityGroupServerRpcApiMixin):
     def get_ofp_rest_api_addr(self, context):
         LOG.debug(_("Get Ryu rest API address"))
-        return self.call(context,
-                         self.make_msg('get_ofp_rest_api'))
+        cctxt = self.client.prepare()
+        return cctxt.call(context, 'get_ofp_rest_api')
 
 
 class RyuSecurityGroupAgent(sg_rpc.SecurityGroupAgentRpcMixin):
index 4dc69d965e0e88e556c32581965c6b061083d22d..ccf534f4ddcacbe4065bbd3ef4b280cabfbb6d04 100644 (file)
@@ -18,6 +18,7 @@
 Unit Tests for hyperv neutron rpc
 """
 
+import contextlib
 import mock
 
 from neutron.agent import rpc as agent_rpc
@@ -31,8 +32,10 @@ from neutron.tests import base
 
 class rpcHyperVApiTestCase(base.BaseTestCase):
 
-    def _test_hyperv_neutron_api(
+    def _test_hyperv_neutron_api_legacy(
             self, rpcapi, topic, method, rpc_method, **kwargs):
+        # NOTE(russellb) This version of the test method is used for interfaces
+        # not yet converted away from using the RpcProxy compatibility class.
         ctxt = context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_version = kwargs.pop('version', None)
@@ -54,9 +57,34 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
         ]
         rpc_method_mock.assert_has_calls(expected)
 
+    def _test_hyperv_neutron_api(
+            self, rpcapi, topic, method, rpc_method, **kwargs):
+        ctxt = context.RequestContext('fake_user', 'fake_project')
+        expected_retval = 'foo' if rpc_method == 'call' else None
+        expected_version = kwargs.pop('version', None)
+
+        with contextlib.nested(
+            mock.patch.object(rpcapi.client, rpc_method),
+            mock.patch.object(rpcapi.client, 'prepare'),
+        ) as (
+            rpc_mock, prepare_mock
+        ):
+            prepare_mock.return_value = rpcapi.client
+            rpc_mock.return_value = expected_retval
+            retval = getattr(rpcapi, method)(ctxt, **kwargs)
+
+        self.assertEqual(retval, expected_retval)
+
+        prepare_args = {}
+        if expected_version:
+            prepare_args['version'] = expected_version
+        prepare_mock.assert_called_once_with(**prepare_args)
+
+        rpc_mock.assert_called_once_with(ctxt, method, **kwargs)
+
     def test_delete_network(self):
         rpcapi = ana.AgentNotifierApi(topics.AGENT)
-        self._test_hyperv_neutron_api(
+        self._test_hyperv_neutron_api_legacy(
             rpcapi,
             topics.get_topic_name(
                 topics.AGENT,
@@ -67,7 +95,7 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
 
     def test_port_update(self):
         rpcapi = ana.AgentNotifierApi(topics.AGENT)
-        self._test_hyperv_neutron_api(
+        self._test_hyperv_neutron_api_legacy(
             rpcapi,
             topics.get_topic_name(
                 topics.AGENT,
@@ -81,7 +109,7 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
 
     def test_port_delete(self):
         rpcapi = ana.AgentNotifierApi(topics.AGENT)
-        self._test_hyperv_neutron_api(
+        self._test_hyperv_neutron_api_legacy(
             rpcapi,
             topics.get_topic_name(
                 topics.AGENT,
@@ -92,7 +120,7 @@ class rpcHyperVApiTestCase(base.BaseTestCase):
 
     def test_tunnel_update(self):
         rpcapi = ana.AgentNotifierApi(topics.AGENT)
-        self._test_hyperv_neutron_api(
+        self._test_hyperv_neutron_api_legacy(
             rpcapi,
             topics.get_topic_name(
                 topics.AGENT,
index ff6233bba0739555090699a734f817287b929c8c..a05d1dabf46768966b36ab426f65382aec3e9467 100644 (file)
@@ -18,6 +18,7 @@ Unit Tests for ml2 rpc
 """
 
 import collections
+import contextlib
 
 import mock
 
@@ -165,7 +166,10 @@ class RpcCallbacksTestCase(base.BaseTestCase):
 
 class RpcApiTestCase(base.BaseTestCase):
 
-    def _test_rpc_api(self, rpcapi, topic, method, rpc_method, **kwargs):
+    def _test_rpc_api_legacy(self, rpcapi, topic, method, rpc_method,
+                             **kwargs):
+        # NOTE(russellb) This can be removed once AgentNotifierApi has been
+        # converted over to no longer use the RpcProxy compatibility class.
         ctxt = context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_version = kwargs.pop('version', None)
@@ -187,35 +191,61 @@ class RpcApiTestCase(base.BaseTestCase):
         ]
         rpc_method_mock.assert_has_calls(expected)
 
+    def _test_rpc_api(self, rpcapi, topic, method, rpc_method, **kwargs):
+        ctxt = context.RequestContext('fake_user', 'fake_project')
+        expected_retval = 'foo' if rpc_method == 'call' else None
+        expected_version = kwargs.pop('version', None)
+
+        with contextlib.nested(
+            mock.patch.object(rpcapi.client, rpc_method),
+            mock.patch.object(rpcapi.client, 'prepare'),
+        ) as (
+            rpc_mock, prepare_mock
+        ):
+            prepare_mock.return_value = rpcapi.client
+            rpc_mock.return_value = expected_retval
+            retval = getattr(rpcapi, method)(ctxt, **kwargs)
+
+        prepare_args = {}
+        if expected_version:
+            prepare_args['version'] = expected_version
+        prepare_mock.assert_called_once_with(**prepare_args)
+
+        self.assertEqual(retval, expected_retval)
+        rpc_mock.assert_called_once_with(ctxt, method, **kwargs)
+
     def test_delete_network(self):
         rpcapi = plugin_rpc.AgentNotifierApi(topics.AGENT)
-        self._test_rpc_api(rpcapi,
-                           topics.get_topic_name(topics.AGENT,
-                                                 topics.NETWORK,
-                                                 topics.DELETE),
-                           'network_delete', rpc_method='fanout_cast',
-                           network_id='fake_request_spec')
+        self._test_rpc_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.NETWORK,
+                                      topics.DELETE),
+                'network_delete', rpc_method='fanout_cast',
+                network_id='fake_request_spec')
 
     def test_port_update(self):
         rpcapi = plugin_rpc.AgentNotifierApi(topics.AGENT)
-        self._test_rpc_api(rpcapi,
-                           topics.get_topic_name(topics.AGENT,
-                                                 topics.PORT,
-                                                 topics.UPDATE),
-                           'port_update', rpc_method='fanout_cast',
-                           port='fake_port',
-                           network_type='fake_network_type',
-                           segmentation_id='fake_segmentation_id',
-                           physical_network='fake_physical_network')
+        self._test_rpc_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.PORT,
+                                      topics.UPDATE),
+                'port_update', rpc_method='fanout_cast',
+                port='fake_port',
+                network_type='fake_network_type',
+                segmentation_id='fake_segmentation_id',
+                physical_network='fake_physical_network')
 
     def test_tunnel_update(self):
         rpcapi = plugin_rpc.AgentNotifierApi(topics.AGENT)
-        self._test_rpc_api(rpcapi,
-                           topics.get_topic_name(topics.AGENT,
-                                                 type_tunnel.TUNNEL,
-                                                 topics.UPDATE),
-                           'tunnel_update', rpc_method='fanout_cast',
-                           tunnel_ip='fake_ip', tunnel_type='gre')
+        self._test_rpc_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      type_tunnel.TUNNEL,
+                                      topics.UPDATE),
+                'tunnel_update', rpc_method='fanout_cast',
+                tunnel_ip='fake_ip', tunnel_type='gre')
 
     def test_device_details(self):
         rpcapi = agent_rpc.PluginApi(topics.PLUGIN)
index fc2d32d1b9824d2618e0c74c72da2d7bcb5cfdad..4a39cb6ec2369a74d28167ca0f52dd865beb7ae9 100644 (file)
@@ -17,6 +17,9 @@
 Unit Tests for Mellanox RPC (major reuse of linuxbridge rpc unit tests)
 """
 
+import contextlib
+import mock
+
 import fixtures
 from oslo.config import cfg
 
@@ -29,8 +32,10 @@ from neutron.tests import base
 
 class rpcApiTestCase(base.BaseTestCase):
 
-    def _test_mlnx_api(self, rpcapi, topic, method, rpc_method,
-                       expected_msg=None, **kwargs):
+    def _test_mlnx_api_legacy(self, rpcapi, topic, method, rpc_method,
+                              expected_msg=None, **kwargs):
+        # NOTE(russellb) This method can be removed once the AgentNotifierApi
+        # has been converted to no longer use the RpcProxy class.
         ctxt = context.RequestContext('fake_user', 'fake_project')
         expected_retval = 'foo' if rpc_method == 'call' else None
         expected_kwargs = {}
@@ -64,14 +69,38 @@ class rpcApiTestCase(base.BaseTestCase):
             self.assertEqual(expected_arg, arg)
         self.assertEqual(expected_kwargs, self.fake_kwargs)
 
+    def _test_mlnx_api(self, rpcapi, topic, method, rpc_method, **kwargs):
+        ctxt = context.RequestContext('fake_user', 'fake_project')
+        expected_retval = 'foo' if rpc_method == 'call' else None
+        expected_version = kwargs.pop('version', None)
+
+        with contextlib.nested(
+            mock.patch.object(rpcapi.client, rpc_method),
+            mock.patch.object(rpcapi.client, 'prepare'),
+        ) as (
+            rpc_mock, prepare_mock
+        ):
+            prepare_mock.return_value = rpcapi.client
+            rpc_mock.return_value = expected_retval
+            retval = getattr(rpcapi, method)(ctxt, **kwargs)
+
+        prepare_args = {}
+        if expected_version:
+            prepare_args['version'] = expected_version
+        prepare_mock.assert_called_once_with(**prepare_args)
+
+        self.assertEqual(retval, expected_retval)
+        rpc_mock.assert_called_once_with(ctxt, method, **kwargs)
+
     def test_delete_network(self):
         rpcapi = agent_notify_api.AgentNotifierApi(topics.AGENT)
-        self._test_mlnx_api(rpcapi,
-                            topics.get_topic_name(topics.AGENT,
-                                                  topics.NETWORK,
-                                                  topics.DELETE),
-                            'network_delete', rpc_method='fanout_cast',
-                            network_id='fake_request_spec')
+        self._test_mlnx_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.NETWORK,
+                                      topics.DELETE),
+                'network_delete', rpc_method='fanout_cast',
+                network_id='fake_request_spec')
 
     def test_port_update(self):
         cfg.CONF.set_override('rpc_support_old_agents', False, 'AGENT')
@@ -81,16 +110,17 @@ class rpcApiTestCase(base.BaseTestCase):
                                        network_type='vlan',
                                        physical_network='fake_net',
                                        segmentation_id='fake_vlan_id')
-        self._test_mlnx_api(rpcapi,
-                            topics.get_topic_name(topics.AGENT,
-                                                  topics.PORT,
-                                                  topics.UPDATE),
-                            'port_update', rpc_method='fanout_cast',
-                            expected_msg=expected_msg,
-                            port='fake_port',
-                            network_type='vlan',
-                            physical_network='fake_net',
-                            vlan_id='fake_vlan_id')
+        self._test_mlnx_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.PORT,
+                                      topics.UPDATE),
+                'port_update', rpc_method='fanout_cast',
+                expected_msg=expected_msg,
+                port='fake_port',
+                network_type='vlan',
+                physical_network='fake_net',
+                vlan_id='fake_vlan_id')
 
     def test_port_update_ib(self):
         cfg.CONF.set_override('rpc_support_old_agents', False, 'AGENT')
@@ -100,16 +130,17 @@ class rpcApiTestCase(base.BaseTestCase):
                                        network_type='ib',
                                        physical_network='fake_net',
                                        segmentation_id='fake_vlan_id')
-        self._test_mlnx_api(rpcapi,
-                            topics.get_topic_name(topics.AGENT,
-                                                  topics.PORT,
-                                                  topics.UPDATE),
-                            'port_update', rpc_method='fanout_cast',
-                            expected_msg=expected_msg,
-                            port='fake_port',
-                            network_type='ib',
-                            physical_network='fake_net',
-                            vlan_id='fake_vlan_id')
+        self._test_mlnx_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.PORT,
+                                      topics.UPDATE),
+                'port_update', rpc_method='fanout_cast',
+                expected_msg=expected_msg,
+                port='fake_port',
+                network_type='ib',
+                physical_network='fake_net',
+                vlan_id='fake_vlan_id')
 
     def test_port_update_old_agent(self):
         cfg.CONF.set_override('rpc_support_old_agents', True, 'AGENT')
@@ -120,16 +151,17 @@ class rpcApiTestCase(base.BaseTestCase):
                                        physical_network='fake_net',
                                        segmentation_id='fake_vlan_id',
                                        vlan_id='fake_vlan_id')
-        self._test_mlnx_api(rpcapi,
-                            topics.get_topic_name(topics.AGENT,
-                                                  topics.PORT,
-                                                  topics.UPDATE),
-                            'port_update', rpc_method='fanout_cast',
-                            expected_msg=expected_msg,
-                            port='fake_port',
-                            network_type='vlan',
-                            physical_network='fake_net',
-                            vlan_id='fake_vlan_id')
+        self._test_mlnx_api_legacy(
+                rpcapi,
+                topics.get_topic_name(topics.AGENT,
+                                      topics.PORT,
+                                      topics.UPDATE),
+                'port_update', rpc_method='fanout_cast',
+                expected_msg=expected_msg,
+                port='fake_port',
+                network_type='vlan',
+                physical_network='fake_net',
+                vlan_id='fake_vlan_id')
 
     def test_device_details(self):
         rpcapi = agent_rpc.PluginApi(topics.PLUGIN)
index a2125e1929869a006bb353741e218dd518579dcf..27db851a231aa10424466b23cbebd9f1873e3229 100644 (file)
@@ -306,40 +306,26 @@ class TestNecAgentCallback(TestNecAgentBase):
 
 class TestNecAgentPluginApi(TestNecAgentBase):
 
-    def _test_plugin_api(self, expected_failure=False):
+    def test_plugin_api(self):
         with contextlib.nested(
-            mock.patch.object(nec_neutron_agent.NECPluginApi, 'make_msg'),
-            mock.patch.object(nec_neutron_agent.NECPluginApi, 'call'),
-            mock.patch.object(nec_neutron_agent, 'LOG')
-        ) as (make_msg, apicall, log):
+            mock.patch.object(self.agent.plugin_rpc.client, 'prepare'),
+            mock.patch.object(self.agent.plugin_rpc.client, 'call'),
+        ) as (mock_prepare, mock_call):
+            mock_prepare.return_value = self.agent.plugin_rpc.client
+
             agent_id = 'nec-q-agent.dummy-host'
-            if expected_failure:
-                apicall.side_effect = Exception()
+            port_added = [{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
+                          {'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}]
+            port_removed = ['id-3', 'id-4', 'id-5']
 
             self.agent.plugin_rpc.update_ports(
                 mock.sentinel.ctx, agent_id, OVS_DPID_0X,
-                # port_added
-                [{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
-                 {'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
-                # port_removed
-                ['id-3', 'id-4', 'id-5'])
-
-            make_msg.assert_called_once_with(
-                'update_ports', topic='q-agent-notifier',
-                agent_id=agent_id, datapath_id=OVS_DPID_0X,
-                port_added=[{'id': 'id-1', 'mac': 'mac-1', 'port_no': '1'},
-                            {'id': 'id-2', 'mac': 'mac-2', 'port_no': '2'}],
-                port_removed=['id-3', 'id-4', 'id-5'])
-
-            apicall.assert_called_once_with(mock.sentinel.ctx,
-                                            make_msg.return_value)
-
-            self.assertTrue(log.info.called)
-            if expected_failure:
-                self.assertTrue(log.warn.called)
+                port_added, port_removed)
 
-    def test_plugin_api(self):
-        self._test_plugin_api()
+            mock_call.assert_called_once_with(
+                    mock.sentinel.ctx, 'update_ports',
+                    agent_id=agent_id, datapath_id=OVS_DPID_0X,
+                    port_added=port_added, port_removed=port_removed)
 
 
 class TestNecAgentMain(base.BaseTestCase):
index b1609fc59d2bb0f39ef18f56611363bf5043488f..0fb42d5e250186513b2402d4190426471f2b9aa1 100644 (file)
@@ -226,22 +226,19 @@ class TestOVSNeutronOFPRyuAgent(RyuAgentTestCase):
 
 class TestRyuPluginApi(RyuAgentTestCase):
     def test_get_ofp_rest_api_addr(self):
+        rpcapi = self.mod_agent.RyuPluginApi('foo')
         with contextlib.nested(
-            mock.patch(self._AGENT_NAME + '.RyuPluginApi.make_msg',
-                       return_value='msg'),
-            mock.patch(self._AGENT_NAME + '.RyuPluginApi.call',
-                       return_value='10.0.0.1')
-        ) as (mock_msg, mock_call):
-            api = self.mod_agent.RyuPluginApi('topics')
-            addr = api.get_ofp_rest_api_addr('context')
-
-        self.assertEqual(addr, '10.0.0.1')
-        mock_msg.assert_has_calls([
-            mock.call('get_ofp_rest_api')
-        ])
-        mock_call.assert_has_calls([
-            mock.call('context', 'msg')
-        ])
+            mock.patch.object(rpcapi.client, 'call'),
+            mock.patch.object(rpcapi.client, 'prepare'),
+        ) as (
+            rpc_mock, prepare_mock
+        ):
+            prepare_mock.return_value = rpcapi.client
+            rpc_mock.return_value = 'return'
+            addr = rpcapi.get_ofp_rest_api_addr('context')
+
+        self.assertEqual('return', addr)
+        rpc_mock.assert_called_once_with('context', 'get_ofp_rest_api')
 
 
 class TestVifPortSet(RyuAgentTestCase):
index e30f636b50c4792a5df10b9bf330999defb7d4a7..ccabce16120c9762074cad5d0acf3f3a468adf07 100644 (file)
@@ -27,8 +27,14 @@ class AgentRPCPluginApi(base.BaseTestCase):
         agent = rpc.PluginApi('fake_topic')
         ctxt = context.RequestContext('fake_user', 'fake_project')
         expect_val = 'foo'
-        with mock.patch('neutron.common.rpc.RpcProxy.call') as rpc_call:
-            rpc_call.return_value = expect_val
+        with contextlib.nested(
+            mock.patch.object(agent.client, 'call'),
+            mock.patch.object(agent.client, 'prepare'),
+        ) as (
+            mock_call, mock_prepare
+        ):
+            mock_prepare.return_value = agent.client
+            mock_call.return_value = expect_val
             func_obj = getattr(agent, method)
             if method == 'tunnel_sync':
                 actual_val = func_obj(ctxt, 'fake_tunnel_ip')
@@ -47,8 +53,14 @@ class AgentRPCPluginApi(base.BaseTestCase):
         ctxt = context.RequestContext('fake_user', 'fake_project')
         expect_val_get_device_details = 'foo'
         expect_val = [expect_val_get_device_details]
-        with mock.patch('neutron.common.rpc.RpcProxy.call') as rpc_call:
-            rpc_call.side_effect = [messaging.UnsupportedVersion('1.2'),
+        with contextlib.nested(
+            mock.patch.object(agent.client, 'call'),
+            mock.patch.object(agent.client, 'prepare'),
+        ) as (
+            mock_call, mock_prepare
+        ):
+            mock_prepare.return_value = agent.client
+            mock_call.side_effect = [messaging.UnsupportedVersion('1.2'),
                                     expect_val_get_device_details]
             func_obj = getattr(agent, 'get_devices_details_list')
             actual_val = func_obj(ctxt, ['fake_device'], 'fake_agent_id')
index dfad04414f13de4b2a3be6b7fe02d64899d9523e..096839a0459e6be7a82ba2c0b50a271646f5bf1f 100644 (file)
@@ -1490,20 +1490,22 @@ class FakeSGRpcApi(agent_rpc.PluginApi,
 
 
 class SecurityGroupServerRpcApiTestCase(base.BaseTestCase):
-    def setUp(self):
-        super(SecurityGroupServerRpcApiTestCase, self).setUp()
-        self.rpc = FakeSGRpcApi('fake_topic')
-        self.rpc.call = mock.Mock()
-
     def test_security_group_rules_for_devices(self):
-        self.rpc.security_group_rules_for_devices(None, ['fake_device'])
-        self.rpc.call.assert_has_calls(
-            [mock.call(None,
-             {'args':
-                 {'devices': ['fake_device']},
-              'method': 'security_group_rules_for_devices',
-              'namespace': None},
-             version='1.1')])
+        rpcapi = FakeSGRpcApi('fake_topic')
+
+        with contextlib.nested(
+            mock.patch.object(rpcapi.client, 'call'),
+            mock.patch.object(rpcapi.client, 'prepare'),
+        ) as (
+            rpc_mock, prepare_mock
+        ):
+            prepare_mock.return_value = rpcapi.client
+            rpcapi.security_group_rules_for_devices('context', ['fake_device'])
+
+        rpc_mock.assert_called_once_with(
+                'context',
+                'security_group_rules_for_devices',
+                devices=['fake_device'])
 
 
 class FakeSGNotifierAPI(n_rpc.RpcProxy,