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
This commit is contained in:
Russell Bryant
2014-11-11 14:23:15 -05:00
parent 6e5901e03e
commit 6707eb203b
2 changed files with 35 additions and 27 deletions

View File

@@ -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):

View File

@@ -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):