diff --git a/neutron/agent/rpc.py b/neutron/agent/rpc.py index 7039c21cd5..06e3ae6d42 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 c0f7e0ddd7..e30f636b50 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):