def test_message_base64_decode_fails(self, resource_group, location, storage_account, storage_account_key): # Arrange qsc = QueueServiceClient(self._account_url(storage_account.name), storage_account_key) queue = QueueClient(queue_url=self._account_url(storage_account.name), queue=self.get_resource_name(TEST_QUEUE_PREFIX), credential=storage_account_key, message_encode_policy=TextXMLEncodePolicy(), message_decode_policy=BinaryBase64DecodePolicy()) try: queue.create_queue() except ResourceExistsError: pass message = u'xyz' queue.enqueue_message(message) # Action. with self.assertRaises(DecodeError) as e: queue.peek_messages() # Asserts self.assertNotEqual( -1, str(e.exception).find('Message content is not valid base 64'))
def test_sas_add(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return # Arrange qsc = QueueServiceClient(self._account_url(storage_account.name), storage_account_key) queue_client = self._get_queue_reference(qsc) queue_client.create_queue() token = queue_client.generate_shared_access_signature( QueueSasPermissions(add=True), datetime.utcnow() + timedelta(hours=1), ) # Act service = QueueClient( queue_url=queue_client.url, credential=token, ) result = service.enqueue_message(u'addedmessage') # Assert result = next(queue_client.receive_messages()) self.assertEqual(u'addedmessage', result.content)
def test_message_text_fails(self): # Arrange queue_url = self._get_queue_url() credentials = self._get_shared_key_credential() queue = QueueClient( queue_url=queue_url, queue=self.get_resource_name(TEST_QUEUE_PREFIX), credential=credentials, message_encode_policy=BinaryBase64EncodePolicy(), message_decode_policy=BinaryBase64DecodePolicy()) # Action. with self.assertRaises(TypeError) as e: message = u'xyz' queue.enqueue_message(message) # Asserts self.assertTrue(str(e.exception).startswith('Message content must be bytes'))
def test_message_text_fails(self, resource_group, location, storage_account, storage_account_key): # Arrange qsc = QueueServiceClient(self._account_url(storage_account.name), storage_account_key) queue = QueueClient(queue_url=self._account_url(storage_account.name), queue=self.get_resource_name(TEST_QUEUE_PREFIX), credential=storage_account_key, message_encode_policy=BinaryBase64EncodePolicy(), message_decode_policy=BinaryBase64DecodePolicy()) # Action. with self.assertRaises(TypeError) as e: message = u'xyz' queue.enqueue_message(message) # Asserts self.assertTrue( str(e.exception).startswith('Message content must be bytes'))
def upload(globpath, container, queue, sas_token_env, storage_account_uri): try: sas_token_env = sas_token_env sas_token = os.getenv(sas_token_env) if sas_token is None: getLogger().error( "Sas token environment variable {} was not defined.".format( sas_token_env)) return 1 files = glob(globpath, recursive=True) for infile in files: blob_name = get_unique_name(infile, os.getenv('HELIX_WORKITEM_ID')) getLogger().info("uploading {}".format(infile)) blob_client = BlobClient( account_url=storage_account_uri.format('blob'), container_name=container, blob_name=blob_name, credential=sas_token) with open(infile, "rb") as data: blob_client.upload_blob(data, blob_type="BlockBlob", content_settings=ContentSettings( content_type="application/json")) if queue is not None: queue_client = QueueClient( storage_account_uri.format('queue'), queue=queue, credential=sas_token, message_encode_policy=TextBase64EncodePolicy()) queue_client.enqueue_message(blob_client.url) getLogger().info("upload complete") except Exception as ex: getLogger().error('{0}: {1}'.format(type(ex), str(ex))) getLogger().error(format_exc()) return 1
def test_message_base64_decode_fails(self): # Arrange queue_url = self._get_queue_url() credentials = self._get_shared_key_credential() queue = QueueClient( queue_url=queue_url, queue=self.get_resource_name(TEST_QUEUE_PREFIX), credential=credentials, message_encode_policy=TextXMLEncodePolicy(), message_decode_policy=BinaryBase64DecodePolicy()) try: queue.create_queue() except ResourceExistsError: pass message = u'xyz' queue.enqueue_message(message) # Action. with self.assertRaises(DecodeError) as e: queue.peek_messages() # Asserts self.assertNotEqual(-1, str(e.exception).find('Message content is not valid base 64'))
def test_sas_add(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange queue_client = self._create_queue() token = queue_client.generate_shared_access_signature( QueuePermissions.ADD, datetime.utcnow() + timedelta(hours=1), ) # Act service = QueueClient( queue_url=queue_client.url, credential=token, ) result = service.enqueue_message(u'addedmessage') # Assert result = next(queue_client.receive_messages()) self.assertEqual(u'addedmessage', result.content)