def test_get_appspec_hash(self): appspec_str = json.dumps(self.deployer._appspec_dict) encoded_appspec = compat.ensure_bytes(appspec_str) expected_hash = hashlib.sha256(encoded_appspec).hexdigest() actual_hash = self.deployer._get_appspec_hash() self.assertEqual(actual_hash, expected_hash)
def serialize(self, event): """Serializes a monitor event to the CSM format :type event: BaseMonitorEvent :param event: The event to serialize to bytes :rtype: bytes :returns: The CSM serialized form of the event """ event_dict = self._get_base_event_dict(event) event_type = self._get_event_type(event) event_dict['Type'] = event_type for attr in self._SERIALIZEABLE_EVENT_PROPERTIES: value = getattr(event, attr, None) if value is not None: getattr(self, '_serialize_' + attr)(value, event_dict, event_type=event_type) return ensure_bytes(json.dumps(event_dict, separators=(',', ':')))
def serialize(self, event): """Serializes a monitor event to the CSM format :type event: BaseMonitorEvent :param event: The event to serialize to bytes :rtype: bytes :returns: The CSM serialized form of the event """ event_dict = self._get_base_event_dict(event) event_type = self._get_event_type(event) event_dict['Type'] = event_type for attr in self._SERIALIZEABLE_EVENT_PROPERTIES: value = getattr(event, attr, None) if value is not None: getattr(self, '_serialize_' + attr)( value, event_dict, event_type=event_type) return ensure_bytes( json.dumps(event_dict, separators=(',', ':')))
def test_non_string_or_bytes_raises_error(self): value = 500 with self.assertRaises(ValueError): ensure_bytes(value)
def test_non_ascii(self): value = u'\u2713' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'\xe2\x9c\x93')
def test_unicode(self): value = u'baz' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'baz')
def test_binary(self): value = b'bar' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'bar')
def test_string(self): value = 'foo' response = ensure_bytes(value) self.assertIsInstance(response, six.binary_type) self.assertEqual(response, b'foo')
def convert_body_to_file_like_object(params, **kwargs): if 'Body' in params: if isinstance(params['Body'], six.string_types): params['Body'] = six.BytesIO(ensure_bytes(params['Body'])) elif isinstance(params['Body'], six.binary_type): params['Body'] = six.BytesIO(params['Body'])
def _get_appspec_hash(self): appspec_str = json.dumps(self._appspec_dict) appspec_encoded = compat.ensure_bytes(appspec_str) return hashlib.sha256(appspec_encoded).hexdigest()