Ejemplo n.º 1
0
    def test_mgmt_queue_list_basic(self,
                                   servicebus_namespace_connection_string,
                                   servicebus_namespace,
                                   servicebus_namespace_key_name,
                                   servicebus_namespace_primary_key):
        mgmt_service = ServiceBusManagementClient.from_connection_string(
            servicebus_namespace_connection_string)

        clear_queues(mgmt_service)

        queues = list(mgmt_service.list_queues())
        assert len(queues) == 0
        mgmt_service.create_queue("test_queue")
        queues = list(mgmt_service.list_queues())
        assert len(queues) == 1 and queues[0].name == "test_queue"
        mgmt_service.delete_queue("test_queue")
        queues = list(mgmt_service.list_queues())
        assert len(queues) == 0

        fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net'
        mgmt_service = ServiceBusManagementClient(
            fully_qualified_namespace,
            credential=ServiceBusSharedKeyCredential(
                servicebus_namespace_key_name,
                servicebus_namespace_primary_key))
        queues = list(mgmt_service.list_queues())
        assert len(queues) == 0
        mgmt_service.create_queue("test_queue")
        queues = list(mgmt_service.list_queues())
        assert len(queues) == 1 and queues[0].name == "test_queue"
        mgmt_service.delete_queue("test_queue")
        queues = list(mgmt_service.list_queues())
        assert len(queues) == 0
Ejemplo n.º 2
0
    def test_mgmt_queue_list_with_negative_credential(
            self, servicebus_namespace, servicebus_namespace_key_name,
            servicebus_namespace_primary_key):
        # invalid_conn_str = 'Endpoint=sb://invalid.servicebus.windows.net/;SharedAccessKeyName=invalid;SharedAccessKey=invalid'
        # mgmt_service = ServiceBusManagementClient.from_connection_string(invalid_conn_str)
        # with pytest.raises(ServiceRequestError):
        #     list(mgmt_service.list_queues())
        # TODO: This negative test makes replay test fail. Need more investigation. Live test has no problem.

        invalid_conn_str = 'Endpoint=sb://{}.servicebus.windows.net/;SharedAccessKeyName=invalid;SharedAccessKey=invalid'.format(
            servicebus_namespace.name)
        mgmt_service = ServiceBusManagementClient.from_connection_string(
            invalid_conn_str)
        with pytest.raises(HttpResponseError):
            list(mgmt_service.list_queues())

        # fully_qualified_namespace = 'invalid.servicebus.windows.net'
        # mgmt_service = ServiceBusManagementClient(
        #     fully_qualified_namespace,
        #     credential=ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key)
        # )
        # with pytest.raises(ServiceRequestError):
        #     list(mgmt_service.list_queues())

        fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net'
        mgmt_service = ServiceBusManagementClient(
            fully_qualified_namespace,
            credential=ServiceBusSharedKeyCredential("invalid", "invalid"))
        with pytest.raises(HttpResponseError):
            list(mgmt_service.list_queues())
Ejemplo n.º 3
0
    def test_subscription_by_sas_token_credential_conn_str_send_basic(
            self, servicebus_namespace, servicebus_namespace_key_name,
            servicebus_namespace_primary_key, servicebus_topic,
            servicebus_subscription, **kwargs):
        fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net'
        with ServiceBusClient(
                fully_qualified_namespace=fully_qualified_namespace,
                credential=ServiceBusSharedKeyCredential(
                    policy=servicebus_namespace_key_name,
                    key=servicebus_namespace_primary_key),
                logging_enable=False) as sb_client:

            with sb_client.get_topic_sender(
                    topic_name=servicebus_topic.name) as sender:
                message = Message(b"Sample topic message")
                sender.send_messages(message)

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    max_wait_time=5) as receiver:
                count = 0
                for message in receiver:
                    count += 1
                    message.complete()
            assert count == 1
 def test_sb_client_bad_credentials(self, servicebus_namespace, servicebus_queue, **kwargs):
     client = ServiceBusClient(
         fully_qualified_namespace=servicebus_namespace.name + '.servicebus.windows.net',
         credential=ServiceBusSharedKeyCredential('invalid', 'invalid'),
         logging_enable=False)
     with client:
         with pytest.raises(ServiceBusError):
             with client.get_queue_sender(servicebus_queue.name) as sender:
                 sender.send_messages(Message("test"))
Ejemplo n.º 5
0
def example_create_servicebus_receiver_sync():
    servicebus_client = example_create_servicebus_client_sync()

    # [START create_servicebus_receiver_from_conn_str_sync]
    import os
    from azure.servicebus import ServiceBusReceiver
    servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR']
    queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
    queue_receiver = ServiceBusReceiver.from_connection_string(
        conn_str=servicebus_connection_str, queue_name=queue_name)
    # [END create_servicebus_receiver_from_conn_str_sync]

    # [START create_servicebus_receiver_sync]
    import os
    from azure.servicebus import ServiceBusReceiver, ServiceBusSharedKeyCredential
    fully_qualified_namespace = os.environ[
        'SERVICE_BUS_FULLY_QUALIFIED_NAMESPACE']
    shared_access_policy = os.environ['SERVICE_BUS_SAS_POLICY']
    shared_access_key = os.environ['SERVICE_BUS_SAS_KEY']
    queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
    queue_receiver = ServiceBusReceiver(
        fully_qualified_namespace=fully_qualified_namespace,
        credential=ServiceBusSharedKeyCredential(shared_access_policy,
                                                 shared_access_key),
        queue_name=queue_name)
    # [END create_servicebus_receiver_sync]

    # [START create_servicebus_receiver_from_sb_client_sync]
    import os
    from azure.servicebus import ServiceBusClient
    servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR']
    queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=servicebus_connection_str)
    with servicebus_client:
        queue_receiver = servicebus_client.get_queue_receiver(
            queue_name=queue_name)
    # [END create_servicebus_receiver_from_sb_client_sync]

    # [START create_subscription_receiver_from_sb_client_sync]
    import os
    from azure.servicebus import ServiceBusClient
    servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR']
    topic_name = os.environ["SERVICE_BUS_TOPIC_NAME"]
    subscription_name = os.environ["SERVICE_BUS_SUBSCRIPTION_NAME"]
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=servicebus_connection_str)
    with servicebus_client:
        subscription_receiver = servicebus_client.get_subscription_receiver(
            topic_name=topic_name,
            subscription_name=subscription_name,
        )
    # [END create_subscription_receiver_from_sb_client_sync]

    return queue_receiver
Ejemplo n.º 6
0
 def test_topic_by_sas_token_credential_conn_str_send_basic(
         self, servicebus_namespace, servicebus_namespace_key_name,
         servicebus_namespace_primary_key, servicebus_topic, **kwargs):
     fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net'
     with ServiceBusClient(
             fully_qualified_namespace=fully_qualified_namespace,
             credential=ServiceBusSharedKeyCredential(
                 policy=servicebus_namespace_key_name,
                 key=servicebus_namespace_primary_key),
             logging_enable=False) as sb_client:
         with sb_client.get_topic_sender(servicebus_topic.name) as sender:
             message = Message(b"Sample topic message")
             sender.send_messages(message)
Ejemplo n.º 7
0
def example_create_servicebus_client_sync():
    # [START create_sb_client_from_conn_str_sync]
    import os
    from azure.servicebus import ServiceBusClient
    servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR']
    servicebus_client = ServiceBusClient.from_connection_string(
        conn_str=servicebus_connection_str)
    # [END create_sb_client_from_conn_str_sync]

    # [START create_sb_client_sync]
    import os
    from azure.servicebus import ServiceBusClient, ServiceBusSharedKeyCredential
    fully_qualified_namespace = os.environ['SERVICE_BUS_CONNECTION_STR']
    shared_access_policy = os.environ['SERVICE_BUS_SAS_POLICY']
    shared_access_key = os.environ['SERVICE_BUS_SAS_KEY']
    servicebus_client = ServiceBusClient(
        fully_qualified_namespace=fully_qualified_namespace,
        credential=ServiceBusSharedKeyCredential(shared_access_policy,
                                                 shared_access_key))
    # [END create_sb_client_sync]
    return servicebus_client