From: Russell Bryant Date: Tue, 11 Nov 2014 19:23:15 +0000 (-0500) Subject: Drop RpcProxy usage from PluginReportStateAPI X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=6707eb203bc04e65263f30094f8fe3c31275cbc5;p=openstack-build%2Fneutron-build.git Drop RpcProxy usage from PluginReportStateAPI Drop the usage of the RpcProxy compatibility class from the PluginReportStateAPI rpc client class. It now uses the appropriate oslo.messaging APIs directly, instead. Part of blueprint drop-rpc-compat. Change-Id: I7101331a556bd0a5c8f782ae6cb4103151e6c45c --- diff --git a/neutron/agent/rpc.py b/neutron/agent/rpc.py index 7039c21cd..06e3ae6d4 100644 --- a/neutron/agent/rpc.py +++ b/neutron/agent/rpc.py @@ -54,22 +54,19 @@ def create_consumers(endpoints, prefix, topic_details): return connection -class PluginReportStateAPI(n_rpc.RpcProxy): - BASE_RPC_API_VERSION = '1.0' - +class PluginReportStateAPI(object): def __init__(self, topic): - super(PluginReportStateAPI, 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 report_state(self, context, agent_state, use_call=False): - msg = self.make_msg('report_state', - agent_state={'agent_state': - agent_state}, - time=timeutils.strtime()) - if use_call: - return self.call(context, msg) - else: - return self.cast(context, msg) + cctxt = self.client.prepare() + kwargs = { + 'agent_state': {'agent_state': agent_state}, + 'time': timeutils.strtime(), + } + method = cctxt.call if use_call else cctxt.cast + return method(context, 'report_state', **kwargs) class PluginApi(n_rpc.RpcProxy): diff --git a/neutron/tests/unit/test_agent_rpc.py b/neutron/tests/unit/test_agent_rpc.py index c0f7e0ddd..e30f636b5 100644 --- a/neutron/tests/unit/test_agent_rpc.py +++ b/neutron/tests/unit/test_agent_rpc.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import contextlib import mock from oslo import messaging @@ -65,32 +66,42 @@ class AgentPluginReportState(base.BaseTestCase): topic = 'test' reportStateAPI = rpc.PluginReportStateAPI(topic) expected_agent_state = {'agent': 'test'} - with mock.patch.object(reportStateAPI, 'call') as call: + with contextlib.nested( + mock.patch.object(reportStateAPI.client, 'call'), + mock.patch.object(reportStateAPI.client, 'cast'), + mock.patch.object(reportStateAPI.client, 'prepare'), + ) as ( + mock_call, mock_cast, mock_prepare + ): + mock_prepare.return_value = reportStateAPI.client ctxt = context.RequestContext('fake_user', 'fake_project') reportStateAPI.report_state(ctxt, expected_agent_state, use_call=True) - self.assertEqual(call.call_args[0][0], ctxt) - self.assertEqual(call.call_args[0][1]['method'], - 'report_state') - self.assertEqual(call.call_args[0][1]['args']['agent_state'], + self.assertEqual(mock_call.call_args[0][0], ctxt) + self.assertEqual(mock_call.call_args[0][1], 'report_state') + self.assertEqual(mock_call.call_args[1]['agent_state'], {'agent_state': expected_agent_state}) - self.assertIsInstance(call.call_args[0][1]['args']['time'], - str) + self.assertIsInstance(mock_call.call_args[1]['time'], str) def test_plugin_report_state_cast(self): topic = 'test' reportStateAPI = rpc.PluginReportStateAPI(topic) expected_agent_state = {'agent': 'test'} - with mock.patch.object(reportStateAPI, 'cast') as cast: + with contextlib.nested( + mock.patch.object(reportStateAPI.client, 'call'), + mock.patch.object(reportStateAPI.client, 'cast'), + mock.patch.object(reportStateAPI.client, 'prepare'), + ) as ( + mock_call, mock_cast, mock_prepare + ): + mock_prepare.return_value = reportStateAPI.client ctxt = context.RequestContext('fake_user', 'fake_project') reportStateAPI.report_state(ctxt, expected_agent_state) - self.assertEqual(cast.call_args[0][0], ctxt) - self.assertEqual(cast.call_args[0][1]['method'], - 'report_state') - self.assertEqual(cast.call_args[0][1]['args']['agent_state'], + self.assertEqual(mock_cast.call_args[0][0], ctxt) + self.assertEqual(mock_cast.call_args[0][1], 'report_state') + self.assertEqual(mock_cast.call_args[1]['agent_state'], {'agent_state': expected_agent_state}) - self.assertIsInstance(cast.call_args[0][1]['args']['time'], - str) + self.assertIsInstance(mock_cast.call_args[1]['time'], str) class AgentRPCMethods(base.BaseTestCase):