def deserialize_msg(msg): # NOTE(russellb): Hang on to your hats, this road is about to # get a little bumpy. # # Robustness Principle: # "Be strict in what you send, liberal in what you accept." # # At this point we have to do a bit of guessing about what it # is we just received. Here is the set of possibilities: # # 1) We received a dict. This could be 2 things: # # a) Inspect it to see if it looks like a standard message envelope. # If so, great! # # b) If it doesn't look like a standard message envelope, it could either # be a notification, or a message from before we added a message # envelope (referred to as version 1.0). # Just return the message as-is. # # 2) It's any other non-dict type. Just return it and hope for the best. # This case covers return values from rpc.call() from before message # envelopes were used. (messages to call a method were always a dict) if not isinstance(msg, dict): # See #2 above. return msg base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY) if not all(map(lambda key: key in msg, base_envelope_keys)): # See #1.b above. return msg # At this point we think we have the message envelope # format we were expecting. (#1.a above) if not utils.version_is_compatible(_RPC_ENVELOPE_VERSION, msg[_VERSION_KEY]): raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY]) raw_msg = jsonutils.loads(msg[_MESSAGE_KEY]) return raw_msg
def _check_version_cap(self, version): if not utils.version_is_compatible(self.version_cap, version): raise RPCVersionCapError(version=version, version_cap=self.version_cap)
def test_version_is_compatible_no_rev_is_zero(self): self.assertTrue(utils.version_is_compatible('1.23.0', '1.23'))
def test_version_is_compatible_older_rev(self): self.assertTrue(utils.version_is_compatible('1.24', '1.23.1'))
def test_version_is_compatible_newer_minor(self): self.assertTrue(utils.version_is_compatible('1.24', '1.23'))
def _is_compatible(target, version): endpoint_version = target.version or '1.0' return utils.version_is_compatible(endpoint_version, version)
def test_version_is_compatible_same(self): self.assertTrue(utils.version_is_compatible('1.23', '1.23'))
def test_version_is_compatible_newer_rev_both(self): self.assertFalse(utils.version_is_compatible('1.23.1', '1.23.2'))
def test_version_is_compatible_major_difference2(self): self.assertFalse(utils.version_is_compatible('1.23', '2.23'))
def test_version_is_compatible_older_minor(self): self.assertFalse(utils.version_is_compatible('1.22', '1.23'))
def can_send_version(self, version=_marker): """Check to see if a version is compatible with the version cap.""" version = self.target.version if version is self._marker else version return (not self.version_cap or utils.version_is_compatible(self.version_cap, self.target.version))
def can_send_version(self, version=_marker): """Check to see if a version is compatible with the version cap.""" version = self.target.version if version is self._marker else version return (not self.version_cap or utils.version_is_compatible( self.version_cap, self.target.version))