1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
import logging
import sys
import os
import pytest
import time
from datetime import datetime, timedelta
from azure.common import AzureHttpError, AzureConflictHttpError
from azure.mgmt.servicebus.models import AccessRights
from azure.servicebus import ServiceBusClient, ServiceBusSender
from azure.servicebus._base_handler import ServiceBusSharedKeyCredential
from azure.servicebus._common.message import ServiceBusMessage, ServiceBusReceivedMessage
from azure.servicebus.exceptions import (
ServiceBusError,
ServiceBusAuthenticationError,
ServiceBusAuthorizationError
)
from devtools_testutils import AzureMgmtTestCase, CachedResourceGroupPreparer
from servicebus_preparer import (
CachedServiceBusNamespacePreparer,
ServiceBusTopicPreparer,
ServiceBusQueuePreparer,
ServiceBusNamespaceAuthorizationRulePreparer,
ServiceBusQueueAuthorizationRulePreparer,
CachedServiceBusQueuePreparer
)
class ServiceBusClientTests(AzureMgmtTestCase):
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusQueuePreparer(name_prefix='servicebustest', dead_lettering_on_message_expiration=True)
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(ServiceBusAuthenticationError):
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("test"))
@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_sb_client_bad_namespace(self, **kwargs):
client = ServiceBusClient(
fully_qualified_namespace='invalid.servicebus.windows.net',
credential=ServiceBusSharedKeyCredential('invalid', 'invalid'),
logging_enable=False)
with client:
with pytest.raises(ServiceBusError):
with client.get_queue_sender('invalidqueue') as sender:
sender.send_messages(ServiceBusMessage("test"))
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
def test_sb_client_bad_entity(self, servicebus_namespace_connection_string, **kwargs):
client = ServiceBusClient.from_connection_string(servicebus_namespace_connection_string)
with client:
with pytest.raises(ServiceBusAuthenticationError):
with client.get_queue_sender("invalid") as sender:
sender.send_messages(ServiceBusMessage("test"))
fake_str = "Endpoint=sb://mock.servicebus.windows.net/;" \
"SharedAccessKeyName=mock;SharedAccessKey=mock;EntityPath=mockentity"
fake_client = ServiceBusClient.from_connection_string(fake_str)
with pytest.raises(ValueError):
fake_client.get_queue_sender('queue')
with pytest.raises(ValueError):
fake_client.get_queue_receiver('queue')
with pytest.raises(ValueError):
fake_client.get_topic_sender('topic')
with pytest.raises(ValueError):
fake_client.get_subscription_receiver('topic', 'subscription')
fake_client.get_queue_sender('mockentity')
fake_client.get_queue_receiver('mockentity')
fake_client.get_topic_sender('mockentity')
fake_client.get_subscription_receiver('mockentity', 'subscription')
fake_str = "Endpoint=sb://mock.servicebus.windows.net/;" \
"SharedAccessKeyName=mock;SharedAccessKey=mock"
fake_client = ServiceBusClient.from_connection_string(fake_str)
fake_client.get_queue_sender('queue')
fake_client.get_queue_receiver('queue')
fake_client.get_topic_sender('topic')
fake_client.get_subscription_receiver('topic', 'subscription')
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusQueuePreparer(name_prefix='servicebustest', dead_lettering_on_message_expiration=True)
@ServiceBusNamespaceAuthorizationRulePreparer(name_prefix='servicebustest', access_rights=[AccessRights.listen])
def test_sb_client_readonly_credentials(self, servicebus_authorization_rule_connection_string, servicebus_queue, **kwargs):
client = ServiceBusClient.from_connection_string(servicebus_authorization_rule_connection_string)
with client:
with client.get_queue_receiver(servicebus_queue.name) as receiver:
messages = receiver.receive_messages(max_message_count=1, max_wait_time=1)
with pytest.raises(ServiceBusAuthorizationError):
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("test"))
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusQueuePreparer(name_prefix='servicebustest', dead_lettering_on_message_expiration=True)
@ServiceBusNamespaceAuthorizationRulePreparer(name_prefix='servicebustest', access_rights=[AccessRights.send])
def test_sb_client_writeonly_credentials(self, servicebus_authorization_rule_connection_string, servicebus_queue, **kwargs):
client = ServiceBusClient.from_connection_string(servicebus_authorization_rule_connection_string)
with client:
with pytest.raises(ServiceBusError):
with client.get_queue_receiver(servicebus_queue.name) as receiver:
messages = receiver.receive_messages(max_message_count=1, max_wait_time=1)
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("test"))
with pytest.raises(TypeError):
sender.send_messages("cat")
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer(name_prefix='servicebustest')
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@ServiceBusNamespaceAuthorizationRulePreparer(name_prefix='servicebustest')
@ServiceBusQueuePreparer(name_prefix='servicebustest_qone', parameter_name='wrong_queue', dead_lettering_on_message_expiration=True)
@ServiceBusQueuePreparer(name_prefix='servicebustest_qtwo', dead_lettering_on_message_expiration=True)
@ServiceBusQueueAuthorizationRulePreparer(name_prefix='servicebustest_qtwo')
def test_sb_client_incorrect_queue_conn_str(self, servicebus_queue_authorization_rule_connection_string, servicebus_queue, wrong_queue, **kwargs):
client = ServiceBusClient.from_connection_string(servicebus_queue_authorization_rule_connection_string)
with client:
# Validate that the wrong queue with the right credentials fails.
with pytest.raises(ValueError):
with client.get_queue_sender(wrong_queue.name) as sender:
sender.send_messages(ServiceBusMessage("test"))
# But that the correct one works.
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("test"))
# Now do the same but with direct connstr initialization.
with pytest.raises(ValueError):
with ServiceBusSender._from_connection_string(
servicebus_queue_authorization_rule_connection_string,
queue_name=wrong_queue.name,
) as sender:
sender.send_messages(ServiceBusMessage("test"))
with ServiceBusSender._from_connection_string(
servicebus_queue_authorization_rule_connection_string,
queue_name=servicebus_queue.name,
) as sender:
sender.send_messages(ServiceBusMessage("test"))
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer()
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@CachedServiceBusQueuePreparer(name_prefix='servicebustest', dead_lettering_on_message_expiration=True)
def test_sb_client_close_spawned_handlers(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs):
client = ServiceBusClient.from_connection_string(servicebus_namespace_connection_string)
client.close()
# context manager
with client:
assert len(client._handlers) == 0
sender = client.get_queue_sender(servicebus_queue.name)
receiver = client.get_queue_receiver(servicebus_queue.name)
sender._open()
receiver._open()
assert sender._handler and sender._running
assert receiver._handler and receiver._running
assert len(client._handlers) == 2
assert not sender._handler and not sender._running
assert not receiver._handler and not receiver._running
assert len(client._handlers) == 0
# close operation
sender = client.get_queue_sender(servicebus_queue.name)
receiver = client.get_queue_receiver(servicebus_queue.name)
sender._open()
receiver._open()
assert sender._handler and sender._running
assert receiver._handler and receiver._running
assert len(client._handlers) == 2
client.close()
assert not sender._handler and not sender._running
assert not receiver._handler and not receiver._running
assert len(client._handlers) == 0
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer()
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@CachedServiceBusQueuePreparer(name_prefix='servicebustest')
def test_client_sas_credential(self,
servicebus_queue,
servicebus_namespace,
servicebus_namespace_key_name,
servicebus_namespace_primary_key,
servicebus_namespace_connection_string,
**kwargs):
# This should "just work" to validate known-good.
credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key)
hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name)
auth_uri = "sb://{}/{}".format(hostname, servicebus_queue.name)
token = credential.get_token(auth_uri).token
# Finally let's do it with SAS token + conn str
token_conn_str = "Endpoint=sb://{}/;SharedAccessSignature={};".format(hostname, token.decode())
client = ServiceBusClient.from_connection_string(token_conn_str)
with client:
assert len(client._handlers) == 0
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("foo"))
# This is disabled pending UAMQP fix https://github.com/Azure/azure-uamqp-python/issues/170
#
#token_conn_str_without_se = token_conn_str.split('se=')[0] + token_conn_str.split('se=')[1].split('&')[1]
#
#client = ServiceBusClient.from_connection_string(token_conn_str_without_se)
#with client:
# assert len(client._handlers) == 0
# with client.get_queue_sender(servicebus_queue.name) as sender:
# sender.send_messages(ServiceBusMessage("foo"))
@pytest.mark.liveTest
@pytest.mark.live_test_only
@CachedResourceGroupPreparer()
@CachedServiceBusNamespacePreparer(name_prefix='servicebustest')
@CachedServiceBusQueuePreparer(name_prefix='servicebustest')
def test_client_credential(self,
servicebus_queue,
servicebus_namespace,
servicebus_namespace_key_name,
servicebus_namespace_primary_key,
servicebus_namespace_connection_string,
**kwargs):
# This should "just work" to validate known-good.
credential = ServiceBusSharedKeyCredential(servicebus_namespace_key_name, servicebus_namespace_primary_key)
hostname = "{}.servicebus.windows.net".format(servicebus_namespace.name)
client = ServiceBusClient(hostname, credential)
with client:
assert len(client._handlers) == 0
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("foo"))
hostname = "sb://{}.servicebus.windows.net".format(servicebus_namespace.name)
client = ServiceBusClient(hostname, credential)
with client:
assert len(client._handlers) == 0
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("foo"))
hostname = "https://{}.servicebus.windows.net \
".format(servicebus_namespace.name)
client = ServiceBusClient(hostname, credential)
with client:
assert len(client._handlers) == 0
with client.get_queue_sender(servicebus_queue.name) as sender:
sender.send_messages(ServiceBusMessage("foo"))
|