Verify that version in 'prepare' is valid

Raise an exception if a caller passes a something for the
version that cannot be parsed as a version.

Change-Id: I167849b1bc6c4d946de42c6459b3577a361e9606
Related-Bug: #1467087
This commit is contained in:
Kevin Benton 2015-06-21 03:15:26 -07:00
parent a3a3c3faa8
commit ac57114c2d
3 changed files with 31 additions and 14 deletions

View File

@ -164,6 +164,14 @@ class _CallContext(object):
version=_marker, server=_marker, fanout=_marker,
timeout=_marker, version_cap=_marker, retry=_marker):
"""Prepare a method invocation context. See RPCClient.prepare()."""
if version is not None and version is not cls._marker:
# quick sanity check to make sure parsable version numbers are used
try:
utils.version_is_compatible(version, version)
except (IndexError, ValueError):
raise exceptions.MessagingException(
"Version must contain a major and minor integer. Got %s"
% version)
kwargs = dict(
exchange=exchange,
topic=topic,

View File

@ -122,21 +122,21 @@ class TestCastToTarget(test_utils.BaseTestCase):
prepare=dict(namespace='testnamespace'),
expect=dict(namespace='testnamespace'))),
('ctor_version',
dict(ctor=dict(version='testversion'),
dict(ctor=dict(version='1.1'),
prepare={},
expect=dict(version='testversion'))),
expect=dict(version='1.1'))),
('prepare_version',
dict(ctor={},
prepare=dict(version='testversion'),
expect=dict(version='testversion'))),
prepare=dict(version='1.1'),
expect=dict(version='1.1'))),
('prepare_version_none',
dict(ctor=dict(version='testversion'),
dict(ctor=dict(version='1.1'),
prepare=dict(version=None),
expect={})),
('both_version',
dict(ctor=dict(version='ctorversion'),
prepare=dict(version='testversion'),
expect=dict(version='testversion'))),
prepare=dict(version='1.1'),
expect=dict(version='1.1'))),
('ctor_server',
dict(ctor=dict(server='testserver'),
prepare={},
@ -517,3 +517,12 @@ class TestCanSendVersion(test_utils.BaseTestCase):
can_send = client.can_send_version()
self.assertEqual(self.can_send, can_send)
def test_invalid_version_type(self):
target = oslo_messaging.Target(topic='sometopic')
transport = _FakeTransport(self.conf)
client = oslo_messaging.RPCClient(transport, target)
self.assertRaises(exceptions.MessagingException,
client.prepare, version='5')
self.assertRaises(exceptions.MessagingException,
client.prepare, version='5.a')

View File

@ -122,21 +122,21 @@ class TestCastToTarget(test_utils.BaseTestCase):
prepare=dict(namespace='testnamespace'),
expect=dict(namespace='testnamespace'))),
('ctor_version',
dict(ctor=dict(version='testversion'),
dict(ctor=dict(version='1.1'),
prepare={},
expect=dict(version='testversion'))),
expect=dict(version='1.1'))),
('prepare_version',
dict(ctor={},
prepare=dict(version='testversion'),
expect=dict(version='testversion'))),
prepare=dict(version='1.1'),
expect=dict(version='1.1'))),
('prepare_version_none',
dict(ctor=dict(version='testversion'),
dict(ctor=dict(version='1.1'),
prepare=dict(version=None),
expect={})),
('both_version',
dict(ctor=dict(version='ctorversion'),
prepare=dict(version='testversion'),
expect=dict(version='testversion'))),
prepare=dict(version='1.1'),
expect=dict(version='1.1'))),
('ctor_server',
dict(ctor=dict(server='testserver'),
prepare={},