def test_unicode_update_message_unicode_data(self, resource_group,
                                                 location, storage_account,
                                                 storage_account_key):
        # Action
        pytest.skip("Uncomment after msrest fix")
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.send_message(u'message1')
        messages = queue_client.receive_messages()

        list_result1 = next(messages)
        list_result1.content = u'啊齄丂狛狜'
        queue_client.update_message(list_result1, visibility_timeout=0)

        # Asserts
        message = next(messages)
        self.assertIsNotNone(message)
        self.assertEqual(list_result1.id, message.id)
        self.assertEqual(u'啊齄丂狛狜', message.content)
        self.assertNotEqual('', message.pop_receipt)
        self.assertEqual(2, message.dequeue_count)
        self.assertIsInstance(message.inserted_on, datetime)
        self.assertIsInstance(message.expires_on, datetime)
        self.assertIsInstance(message.next_visible_on, datetime)
    def test_receive_one_message(self, resource_group, location,
                                 storage_account, storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        self.assertIsNone(queue_client.receive_message())

        queue_client.send_message(u'message1')
        queue_client.send_message(u'message2')
        queue_client.send_message(u'message3')

        message1 = queue_client.receive_message()
        message2 = queue_client.receive_message()
        peeked_message3 = queue_client.peek_messages()[0]

        # Asserts
        self.assertIsNotNone(message1)
        self.assertNotEqual('', message1.id)
        self.assertEqual(u'message1', message1.content)
        self.assertNotEqual('', message1.pop_receipt)
        self.assertEqual(1, message1.dequeue_count)

        self.assertIsNotNone(message2)
        self.assertNotEqual('', message2.id)
        self.assertEqual(u'message2', message2.content)
        self.assertNotEqual('', message2.pop_receipt)
        self.assertEqual(1, message2.dequeue_count)

        self.assertEqual(u'message3', peeked_message3.content)
        self.assertEqual(0, peeked_message3.dequeue_count)
    def test_set_queue_acl_with_signed_identifiers(self, resource_group,
                                                   location, storage_account,
                                                   storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()

        # Act
        access_policy = AccessPolicy(
            permission=QueuePermissions.READ,
            expiry=datetime.utcnow() + timedelta(hours=1),
            start=datetime.utcnow() - timedelta(minutes=5))
        identifiers = {'testid': access_policy}

        resp = queue_client.set_queue_access_policy(
            signed_identifiers=identifiers)

        # Assert
        self.assertIsNone(resp)
        acl = queue_client.get_queue_access_policy()
        self.assertIsNotNone(acl)
        self.assertEqual(len(acl), 1)
        self.assertTrue('testid' in acl)
    def test_queues_in_account(self):
        # Instantiate the QueueServiceClient from a connection string
        from azure.storage.queue import QueueServiceClient
        queue_service = QueueServiceClient.from_connection_string(
            self.connection_string)

        # [START qsc_create_queue]
        queue_service.create_queue("testqueue")
        # [END qsc_create_queue]

        try:
            # [START qsc_list_queues]
            # List all the queues in the service
            list_queues = queue_service.list_queues()
            for queue in list_queues:
                print(queue)

            # List the queues in the service that start with the name "test"
            list_test_queues = queue_service.list_queues(
                name_starts_with="test")
            for queue in list_test_queues:
                print(queue)
            # [END qsc_list_queues]

        finally:
            # [START qsc_delete_queue]
            queue_service.delete_queue("testqueue")
    def test_sas_update(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()
        queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.UPDATE,
            datetime.utcnow() + timedelta(hours=1),
        )
        messages = queue_client.receive_messages()
        result = next(messages)

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        service.update_message(
            result.id,
            pop_receipt=result.pop_receipt,
            visibility_timeout=0,
            content=u'updatedmessage1',
        )

        # Assert
        result = next(messages)
        self.assertEqual(u'updatedmessage1', result.content)
    def test_sas_process(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()
        queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.PROCESS,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        message = next(service.receive_messages())

        # Assert
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    def test_peek_messages(self, resource_group, location, storage_account,
                           storage_account_key):
        # Action
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.enqueue_message(u'message1')
        queue_client.enqueue_message(u'message2')
        queue_client.enqueue_message(u'message3')
        queue_client.enqueue_message(u'message4')
        result = queue_client.peek_messages()

        # Asserts
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertNotEqual('', message.content)
        self.assertIsNone(message.pop_receipt)
        self.assertEqual(0, message.dequeue_count)
        self.assertNotEqual('', message.insertion_time)
        self.assertNotEqual('', message.expiration_time)
        self.assertIsNone(message.time_next_visible)
    def test_sas_read(self, resource_group, location, storage_account,
                      storage_account_key):
        # SAS URL is calculated from storage key, so this test runs live only

        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.send_message(u'message1')
        token = generate_queue_sas(queue_client.account_name,
                                   queue_client.queue_name,
                                   queue_client.credential.account_key,
                                   QueueSasPermissions(read=True),
                                   datetime.utcnow() + timedelta(hours=1),
                                   datetime.utcnow() - timedelta(minutes=5))

        # Act
        service = QueueClient.from_queue_url(
            queue_url=queue_client.url,
            credential=token,
        )
        result = service.peek_messages()

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
Exemple #9
0
    def run_all_samples(self, connection_string):
        try:
            print('Azure Storage Basic Queue samples - Starting.')

            # declare variables
            queuename = "queuesample" + self.random_data.get_random_name(6)
            queuename2 = "queuesample" + self.random_data.get_random_name(6)

            # create a new queue service that can be passed to all methods
            queue_service = QueueServiceClient.from_connection_string(
                conn_str=connection_string)

            # Basic queue operations such as creating a queue and listing all queues in your account
            print('\n\n* Basic queue operations *\n')
            self.basic_queue_operations(queue_service, queuename, queuename2)

            # Add a message to a queue in your account
            print('\n\n* Basic message operations *\n')
            self.basic_queue_message_operations(queue_service, queuename)

        except Exception as e:
            print('Error occurred in the sample.', e)
        finally:
            # Delete the queues from your account
            self.delete_queue(queue_service, queuename)
            self.delete_queue(queue_service, queuename2)
            print('\nAzure Storage Basic Queue samples - Completed.\n')
    def test_get_messages_with_options(self, resource_group, location,
                                       storage_account, storage_account_key):
        # Action
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.enqueue_message(u'message1')
        queue_client.enqueue_message(u'message2')
        queue_client.enqueue_message(u'message3')
        queue_client.enqueue_message(u'message4')
        pager = queue_client.receive_messages(messages_per_page=4,
                                              visibility_timeout=20)
        result = list(pager)

        # Asserts
        self.assertIsNotNone(result)
        self.assertEqual(4, len(result))

        for message in result:
            self.assertIsNotNone(message)
            self.assertNotEqual('', message.id)
            self.assertNotEqual('', message.content)
            self.assertNotEqual('', message.pop_receipt)
            self.assertEqual(1, message.dequeue_count)
            self.assertNotEqual('', message.insertion_time)
            self.assertNotEqual('', message.expiration_time)
            self.assertNotEqual('', message.time_next_visible)
    def run_all_samples(self, connection_string):
        try:
            print('Azure Storage Advanced Queue samples - Starting.')

            # create a new queue service that can be passed to all methods
            queue_service = QueueServiceClient.from_connection_string(
                conn_str=connection_string)

            print('\n\n* List queues *\n')
            self.list_queues(queue_service)

            print('\n\n* Set cors Rules *\n')
            self.set_cors_rules(queue_service)

            print('\n\n* ACL operations *\n')
            self.queue_acl_operations(queue_service)

            print('\n\n* Set service logging and metrics properties *\n')
            self.set_service_properties(queue_service)

            print('\n\n* Set queue metadata *\n')
            self.metadata_operations(queue_service)

        except Exception as e:
            print('Error occurred in the sample.', e)
        finally:
            print('\nAzure Storage Advanced Queue samples - Completed\n')
Exemple #12
0
    def test_get_messages_encrypted_resolver(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account.name, "queue"), storage_account_key)
        qsc.key_encryption_key = KeyWrapper('key1')
        queue = self._create_queue(qsc)
        queue.send_message(u'encrypted_message_2')
        key_resolver = KeyResolver()
        key_resolver.put_key(qsc.key_encryption_key)
        queue.key_resolver_function = key_resolver.resolve_key
        queue.key_encryption_key = None  # Ensure that the resolver is used

        # Act
        li = next(queue.receive_messages())

        # Assert
        self.assertEqual(li.content, u'encrypted_message_2')
Exemple #13
0
    def test_missing_attribute_kek_wrap(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account.name, "queue"), storage_account_key)
        queue = self._create_queue(qsc)

        valid_key = KeyWrapper('key1')

        # Act
        invalid_key_1 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_1.get_key_wrap_algorithm = valid_key.get_key_wrap_algorithm
        invalid_key_1.get_kid = valid_key.get_kid
        # No attribute wrap_key
        queue.key_encryption_key = invalid_key_1
        with self.assertRaises(AttributeError):
            queue.send_message(u'message')

        invalid_key_2 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_2.wrap_key = valid_key.wrap_key
        invalid_key_2.get_kid = valid_key.get_kid
        # No attribute get_key_wrap_algorithm
        queue.key_encryption_key = invalid_key_2
        with self.assertRaises(AttributeError):
            queue.send_message(u'message')

        invalid_key_3 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_3.get_key_wrap_algorithm = valid_key.get_key_wrap_algorithm
        invalid_key_3.wrap_key = valid_key.wrap_key
        # No attribute get_kid
        queue.key_encryption_key = invalid_key_3
        with self.assertRaises(AttributeError):
            queue.send_message(u'message')
Exemple #14
0
    def test_update_encrypted_binary_message(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account.name, "queue"), storage_account_key)
        queue = self._create_queue(qsc, message_encode_policy=BinaryBase64EncodePolicy(), message_decode_policy=BinaryBase64DecodePolicy())
        queue.key_encryption_key = KeyWrapper('key1')

        binary_message = self.get_random_bytes(100)
        queue.send_message(binary_message)
        messages = []
        for m in queue.receive_messages():
            messages.append(m)
        list_result1 = messages[0]

        # Act
        binary_message = self.get_random_bytes(100)
        list_result1.content = binary_message
        queue.update_message(list_result1)

        for m in queue.receive_messages():
            messages.append(m)
        list_result2 = messages[0]


        # Assert
        self.assertEqual(binary_message, list_result2.content)
    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(
            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)
    def test_update_message(self, resource_group, location, storage_account,
                            storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.send_message(u'message1')
        messages = queue_client.receive_messages()
        list_result1 = next(messages)
        message = queue_client.update_message(
            list_result1.id,
            pop_receipt=list_result1.pop_receipt,
            visibility_timeout=0)
        list_result2 = next(messages)

        # Asserts
        # Update response
        self.assertIsNotNone(message)
        self.assertIsNotNone(message.pop_receipt)
        self.assertIsNotNone(message.next_visible_on)
        self.assertIsInstance(message.next_visible_on, datetime)

        # Get response
        self.assertIsNotNone(list_result2)
        message = list_result2
        self.assertIsNotNone(message)
        self.assertEqual(list_result1.id, message.id)
        self.assertEqual(u'message1', message.content)
        self.assertEqual(2, message.dequeue_count)
        self.assertIsNotNone(message.pop_receipt)
        self.assertIsNotNone(message.inserted_on)
        self.assertIsNotNone(message.expires_on)
        self.assertIsNotNone(message.next_visible_on)
    def test_peek_messages_with_options(self, resource_group, location,
                                        storage_account, storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.send_message(u'message1')
        queue_client.send_message(u'message2')
        queue_client.send_message(u'message3')
        queue_client.send_message(u'message4')
        result = queue_client.peek_messages(max_messages=4)

        # Asserts
        self.assertIsNotNone(result)
        self.assertEqual(4, len(result))
        for message in result:
            self.assertIsNotNone(message)
            self.assertNotEqual('', message.id)
            self.assertNotEqual('', message.content)
            self.assertIsNone(message.pop_receipt)
            self.assertEqual(0, message.dequeue_count)
            self.assertNotEqual('', message.inserted_on)
            self.assertNotEqual('', message.expires_on)
            self.assertIsNone(message.next_visible_on)
Exemple #18
0
    def test_peek_messages_encrypted_kek_RSA(self, resource_group, location, storage_account, storage_account_key):

        # We can only generate random RSA keys, so this must be run live or
        # the playback test will fail due to a change in kek values.

        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account.name, "queue"), storage_account_key)
        qsc.key_encryption_key = RSAKeyWrapper('key2')
        queue = self._create_queue(qsc)
        queue.send_message(u'encrypted_message_3')

        # Act
        li = queue.peek_messages()

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_3')
    def test_response_callback(self, storage_account_name,
                               storage_account_key):
        # Arrange
        service = QueueServiceClient(self.account_url(storage_account_name,
                                                      "queue"),
                                     credential=storage_account_key)
        name = self.get_resource_name('cont')
        queue = service.get_queue_client(name)

        # Act
        def callback(response):
            response.http_response.status_code = 200
            response.http_response.headers.clear()

        # Assert
        exists = queue.get_queue_properties(raw_response_hook=callback)
        self.assertTrue(exists)
Exemple #20
0
    def test_set_minute_metrics(self, resource_group, location,
                                storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key)
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=5))

        # Act
        qsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = qsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'],
                                   minute_metrics)
Exemple #21
0
    def auth_active_directory(self):
        # [START create_queue_service_client_token]
        # Get a token credential for authentication
        from azure.identity import ClientSecretCredential
        token_credential = ClientSecretCredential(
            self.active_directory_tenant_id,
            self.active_directory_application_id,
            self.active_directory_application_secret)

        # Instantiate a QueueServiceClient using a token credential
        from azure.storage.queue import QueueServiceClient
        queue_service = QueueServiceClient(account_url=self.account_url,
                                           credential=token_credential)
        # [END create_queue_service_client_token]

        # Get information for the Queue Service
        properties = queue_service.get_service_properties()
Exemple #22
0
    def __init__(self, config, storage_config):
        logger.debug("Creating Azure Functions client")
        self.name = 'azure_fa'
        self.azure_config = config
        self.resource_group = self.azure_config['resource_group']
        self.storage_account = self.azure_config['storage_account']
        self.account_key = self.azure_config['storage_account_key']
        self.location = self.azure_config['location']
        self.functions_version = self.azure_config['functions_version']

        self.queue_service_url = 'https://{}.queue.core.windows.net'.format(
            self.storage_account)
        self.queue_service = QueueServiceClient(
            account_url=self.queue_service_url, credential=self.account_key)

        msg = COMPUTE_CLI_MSG.format('Azure Functions')
        logger.info("{} - Location: {}".format(msg, self.location))
    def get_queue_client(self):
        # Instantiate the QueueServiceClient from a connection string
        from azure.storage.queue import QueueServiceClient, QueueClient
        queue_service = QueueServiceClient.from_connection_string(conn_str=self.connection_string)

        # [START get_queue_client]
        # Get the queue client to interact with a specific queue
        queue = queue_service.get_queue_client(queue="myqueue2")
Exemple #24
0
    def test_user_agent_append(self, resource_group, location, storage_account,
                               storage_account_key):
        service = QueueServiceClient(self.account_url(storage_account,
                                                      "queue"),
                                     credential=storage_account_key)

        def callback(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            self.assertEqual(
                response.http_request.headers['User-Agent'],
                "azsdk-python-storage-queue/{} Python/{} ({}) customer_user_agent"
                .format(VERSION, platform.python_version(),
                        platform.platform()))

        custom_headers = {'User-Agent': 'customer_user_agent'}
        service.get_service_properties(raw_response_hook=callback,
                                       headers=custom_headers)
    def create_client_with_connection_string(self):
        # Instantiate the QueueServiceClient from a connection string
        from azure.storage.queue import QueueServiceClient
        queue_service = QueueServiceClient.from_connection_string(
            conn_str=self.connection_string)

        # Get queue service properties
        properties = queue_service.get_service_properties()
Exemple #26
0
    def test_set_queue_acl_with_non_existing_queue(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key)
        queue_client = self._get_queue_reference(qsc)

        # Act
        with self.assertRaises(ResourceNotFoundError):
            queue_client.set_queue_access_policy(signed_identifiers=dict())
Exemple #27
0
    def test_create_queue(self, resource_group, location, storage_account, storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        created = queue_client.create_queue()

        # Asserts
        self.assertTrue(created)
Exemple #28
0
    def test_delete_non_existing_queue(self, resource_group, location, storage_account, storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key)
        queue_client = self._get_queue_reference(qsc)

        # Asserts
        with self.assertRaises(ResourceNotFoundError):
            queue_client.delete_queue()
    def test_get_queue_acl_with_non_existing_queue(self, storage_account_name, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account_name, "queue"), storage_account_key)
        queue_client = self._get_queue_reference(qsc)

        # Act
        with self.assertRaises(ResourceNotFoundError):
            queue_client.get_queue_access_policy()
    def test_create_queue_if_not_exists_without_existing_queue(self, storage_account_name, storage_account_key):
        # Action
        qsc = QueueServiceClient(self.account_url(storage_account_name, "queue"), storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        created = queue_client.create_queue_if_not_exists()

        # Asserts
        self.assertTrue(created)