diff --git a/sdk/eventhub/azure-eventhub/conftest.py b/sdk/eventhub/azure-eventhub/conftest.py index 6377d9a062d6..41a52d7790ed 100644 --- a/sdk/eventhub/azure-eventhub/conftest.py +++ b/sdk/eventhub/azure-eventhub/conftest.py @@ -3,15 +3,22 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. #-------------------------------------------------------------------------- - +import sys import os import pytest import logging -import sys import uuid import warnings from logging.handlers import RotatingFileHandler +from azure.identity import EnvironmentCredential +from azure.mgmt.resource import ResourceManagementClient +from azure.mgmt.eventhub import EventHubManagementClient +from azure.eventhub import EventHubProducerClient +from uamqp import ReceiveClient +from uamqp.authentication import SASTokenAuth + + # Ignore async tests for Python < 3.5 collect_ignore = [] if sys.version_info < (3, 5): @@ -19,15 +26,14 @@ collect_ignore.append("tests/unittest/asynctests") collect_ignore.append("features") collect_ignore.append("samples/async_samples") - collect_ignore.append("examples/async_examples") - -from azure.servicebus._control_client import ServiceBusService, EventHub -from azure.eventhub import EventHubProducerClient -import uamqp -from uamqp import authentication - PARTITION_COUNT = 2 CONN_STR = "Endpoint=sb://{}/;SharedAccessKeyName={};SharedAccessKey={};EntityPath={}" +RES_GROUP_PREFIX = "eh-res-group" +NAMESPACE_PREFIX = "eh-ns" +EVENTHUB_PREFIX = "eh" +EVENTHUB_DEFAULT_AUTH_RULE_NAME = 'RootManageSharedAccessKey' +LOCATION = "westus" + def pytest_addoption(parser): parser.addoption( @@ -66,59 +72,81 @@ def get_logger(filename, level=logging.INFO): log = get_logger(None, logging.DEBUG) -def create_eventhub(eventhub_config, client=None): - hub_name = str(uuid.uuid4()) - hub_value = EventHub(partition_count=PARTITION_COUNT) - client = client or ServiceBusService( - service_namespace=eventhub_config['namespace'], - shared_access_key_name=eventhub_config['key_name'], - shared_access_key_value=eventhub_config['access_key']) - if client.create_event_hub(hub_name, hub=hub_value, fail_on_exist=True): - return hub_name - raise ValueError("EventHub creation failed.") - - -def cleanup_eventhub(eventhub_config, hub_name, client=None): - client = client or ServiceBusService( - service_namespace=eventhub_config['namespace'], - shared_access_key_name=eventhub_config['key_name'], - shared_access_key_value=eventhub_config['access_key']) - client.delete_event_hub(hub_name) +@pytest.fixture(scope="session") +def resource_group(): + try: + SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"] + except KeyError: + pytest.skip('AZURE_SUBSCRIPTION_ID undefined') + return + resource_client = ResourceManagementClient(EnvironmentCredential(), SUBSCRIPTION_ID) + resource_group_name = RES_GROUP_PREFIX + str(uuid.uuid4()) + try: + rg = resource_client.resource_groups.create_or_update( + resource_group_name, {"location": LOCATION} + ) + yield rg + finally: + try: + resource_client.resource_groups.begin_delete(resource_group_name) + except: + warnings.warn(UserWarning("resource group teardown failed")) @pytest.fixture(scope="session") -def live_eventhub_config(): +def eventhub_namespace(resource_group): try: - config = {} - config['hostname'] = os.environ['EVENT_HUB_HOSTNAME'] - config['event_hub'] = os.environ['EVENT_HUB_NAME'] - config['key_name'] = os.environ['EVENT_HUB_SAS_POLICY'] - config['access_key'] = os.environ['EVENT_HUB_SAS_KEY'] - config['namespace'] = os.environ['EVENT_HUB_NAMESPACE'] - config['consumer_group'] = "$Default" - config['partition'] = "0" - config['connection_str'] = CONN_STR + SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"] except KeyError: - pytest.skip("Live EventHub configuration not found.") - else: - return config + pytest.skip('AZURE_SUBSCRIPTION_ID defined') + return + resource_client = EventHubManagementClient(EnvironmentCredential(), SUBSCRIPTION_ID) + namespace_name = NAMESPACE_PREFIX + str(uuid.uuid4()) + try: + namespace = resource_client.namespaces.begin_create_or_update( + resource_group.name, namespace_name, {"location": LOCATION} + ).result() + key = resource_client.namespaces.list_keys(resource_group.name, namespace_name, EVENTHUB_DEFAULT_AUTH_RULE_NAME) + connection_string = key.primary_connection_string + key_name = key.key_name + primary_key = key.primary_key + yield namespace.name, connection_string, key_name, primary_key + finally: + try: + resource_client.namespaces.begin_delete(resource_group.name, namespace_name).wait() + except: + warnings.warn(UserWarning("eventhub namespace teardown failed")) @pytest.fixture() -def live_eventhub(live_eventhub_config): # pylint: disable=redefined-outer-name - client = ServiceBusService( - service_namespace=live_eventhub_config['namespace'], - shared_access_key_name=live_eventhub_config['key_name'], - shared_access_key_value=live_eventhub_config['access_key']) +def live_eventhub(resource_group, eventhub_namespace): # pylint: disable=redefined-outer-name + try: + SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"] + except KeyError: + pytest.skip('AZURE_SUBSCRIPTION_ID defined') + return + resource_client = EventHubManagementClient(EnvironmentCredential(), SUBSCRIPTION_ID) + eventhub_name = EVENTHUB_PREFIX + str(uuid.uuid4()) + eventhub_ns_name, connection_string, key_name, primary_key = eventhub_namespace try: - hub_name = create_eventhub(live_eventhub_config, client=client) - print("Created EventHub {}".format(hub_name)) - live_eventhub_config['event_hub'] = hub_name + eventhub = resource_client.event_hubs.create_or_update( + resource_group.name, eventhub_ns_name, eventhub_name, {"partition_count": PARTITION_COUNT} + ) + live_eventhub_config = { + 'resource_group': resource_group.name, + 'hostname': "{}.servicebus.windows.net".format(eventhub_ns_name), + 'key_name': key_name, + 'access_key': primary_key, + 'namespace': eventhub_ns_name, + 'event_hub': eventhub.name, + 'consumer_group': '$Default', + 'partition': '0', + 'connection_str': connection_string + ";EntityPath="+eventhub.name + } yield live_eventhub_config finally: try: - cleanup_eventhub(live_eventhub_config, hub_name, client=client) - print("Deleted EventHub {}".format(hub_name)) + resource_client.event_hubs.delete(resource_group.name, eventhub_ns_name, eventhub_name) except: warnings.warn(UserWarning("eventhub teardown failed")) @@ -133,55 +161,48 @@ def connection_str(live_eventhub): @pytest.fixture() -def invalid_hostname(live_eventhub_config): +def invalid_hostname(live_eventhub): return CONN_STR.format( "invalid123.servicebus.windows.net", - live_eventhub_config['key_name'], - live_eventhub_config['access_key'], - live_eventhub_config['event_hub']) + live_eventhub['key_name'], + live_eventhub['access_key'], + live_eventhub['event_hub']) @pytest.fixture() -def invalid_key(live_eventhub_config): +def invalid_key(live_eventhub): return CONN_STR.format( - live_eventhub_config['hostname'], - live_eventhub_config['key_name'], + live_eventhub['hostname'], + live_eventhub['key_name'], "invalid", - live_eventhub_config['event_hub']) + live_eventhub['event_hub']) @pytest.fixture() -def invalid_policy(live_eventhub_config): +def invalid_policy(live_eventhub): return CONN_STR.format( - live_eventhub_config['hostname'], + live_eventhub['hostname'], "invalid", - live_eventhub_config['access_key'], - live_eventhub_config['event_hub']) - - -@pytest.fixture() -def aad_credential(): - try: - return os.environ['AZURE_CLIENT_ID'], os.environ['AZURE_CLIENT_SECRET'], os.environ['AZURE_TENANT_ID'] - except KeyError: - pytest.skip('No Azure Active Directory credential found') + live_eventhub['access_key'], + live_eventhub['event_hub']) @pytest.fixture() -def connstr_receivers(connection_str, live_eventhub_config): +def connstr_receivers(live_eventhub): + connection_str = live_eventhub["connection_str"] partitions = [str(i) for i in range(PARTITION_COUNT)] receivers = [] for p in partitions: - uri = "sb://{}/{}".format(live_eventhub_config['hostname'], live_eventhub_config['event_hub']) - sas_auth = authentication.SASTokenAuth.from_shared_access_key( - uri, live_eventhub_config['key_name'], live_eventhub_config['access_key']) + uri = "sb://{}/{}".format(live_eventhub['hostname'], live_eventhub['event_hub']) + sas_auth = SASTokenAuth.from_shared_access_key( + uri, live_eventhub['key_name'], live_eventhub['access_key']) source = "amqps://{}/{}/ConsumerGroups/{}/Partitions/{}".format( - live_eventhub_config['hostname'], - live_eventhub_config['event_hub'], - live_eventhub_config['consumer_group'], + live_eventhub['hostname'], + live_eventhub['event_hub'], + live_eventhub['consumer_group'], p) - receiver = uamqp.ReceiveClient(source, auth=sas_auth, debug=False, timeout=0, prefetch=500) + receiver = ReceiveClient(source, auth=sas_auth, debug=False, timeout=0, prefetch=500) receiver.open() receivers.append(receiver) yield connection_str, receivers @@ -190,7 +211,8 @@ def connstr_receivers(connection_str, live_eventhub_config): @pytest.fixture() -def connstr_senders(connection_str): +def connstr_senders(live_eventhub): + connection_str = live_eventhub["connection_str"] client = EventHubProducerClient.from_connection_string(connection_str) partitions = client.get_partition_ids() diff --git a/sdk/eventhub/azure-eventhub/dev_requirements.txt b/sdk/eventhub/azure-eventhub/dev_requirements.txt index b7dc2798b680..d068041b7bd9 100644 --- a/sdk/eventhub/azure-eventhub/dev_requirements.txt +++ b/sdk/eventhub/azure-eventhub/dev_requirements.txt @@ -1,7 +1,8 @@ -e ../../../tools/azure-sdk-tools ../../core/azure-core -e ../../identity/azure-identity --e ../../servicebus/azure-servicebus +-e ../azure-mgmt-eventhub +-e ../../resources/azure-mgmt-resource aiohttp>=3.0; python_version >= '3.5' docutils>=0.14 pygments>=2.2.0 diff --git a/sdk/eventhub/azure-eventhub/tests/eventhub_preparer.py b/sdk/eventhub/azure-eventhub/tests/eventhub_preparer.py new file mode 100644 index 000000000000..d9e99ed2c1ab --- /dev/null +++ b/sdk/eventhub/azure-eventhub/tests/eventhub_preparer.py @@ -0,0 +1,294 @@ +#------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +#-------------------------------------------------------------------------- + +# This EventHubs resource preparer is for future use after the tests are all compatible with +# The resource preparer. + +import functools + +from azure.mgmt.eventhub import EventHubManagementClient +from azure.mgmt.eventhub.models import Eventhub, AccessRights + +from azure_devtools.scenario_tests.exceptions import AzureTestError + +from devtools_testutils import ( + ResourceGroupPreparer, AzureMgmtPreparer, FakeResource +) + +from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM + +EVENTHUB_DEFAULT_AUTH_RULE_NAME = 'RootManageSharedAccessKey' +EVENTHUB_NAMESPACE_PARAM = 'eventhub_namespace' +EVENTHUB_PARAM = 'eventhub' +EVENTHUB_AUTHORIZATION_RULE_PARAM = 'eventhub_authorization_rule' + +# Service Bus Namespace Preparer and its shorthand decorator +class EventHubNamespacePreparer(AzureMgmtPreparer): + def __init__(self, + name_prefix='', + use_cache=False, + sku='Standard', location='westus', + parameter_name=EVENTHUB_NAMESPACE_PARAM, + resource_group_parameter_name=RESOURCE_GROUP_PARAM, + disable_recording=True, playback_fake_resource=None, + client_kwargs=None, random_name_enabled=True): + super(EventHubNamespacePreparer, self).__init__(name_prefix, 24, + random_name_enabled=random_name_enabled, + disable_recording=disable_recording, + playback_fake_resource=playback_fake_resource, + client_kwargs=client_kwargs) + self.location = location + self.sku = sku + self.resource_group_parameter_name = resource_group_parameter_name + self.parameter_name = parameter_name + self.connection_string = '' + if random_name_enabled: + self.resource_moniker = self.name_prefix + "ehname" + + self.set_cache(use_cache, sku, location) + + def create_resource(self, name, **kwargs): + if self.is_live: + self.client = self.create_mgmt_client(EventHubManagementClient) # type: EventHubManagementClient + group = self._get_resource_group(**kwargs) + namespace_async_operation = self.client.namespaces.begin_create_or_update( + group.name, + name, + { + 'sku': {'name': self.sku}, + 'location': self.location, + } + ) + self.resource = namespace_async_operation.result() + + key = self.client.namespaces.list_keys(group.name, name, EVENTHUB_DEFAULT_AUTH_RULE_NAME) + self.connection_string = key.primary_connection_string + self.key_name = key.key_name + self.primary_key = key.primary_key + else: + self.resource = FakeResource(name=name, id=name) + self.connection_string = 'Endpoint=sb://{}.servicebus.windows.net/;SharedAccessKeyName=test;SharedAccessKey=THISISATESTKEYXXXXXXXXXXXXXXXXXXXXXXXXXXXX='.format(name) + self.key_name = EVENTHUB_DEFAULT_AUTH_RULE_NAME + self.primary_key = 'ZmFrZV9hY29jdW50X2tleQ==' + return { + self.parameter_name: self.resource, + '{}_connection_string'.format(self.parameter_name): self.connection_string, + '{}_key_name'.format(self.parameter_name): self.key_name, + '{}_primary_key'.format(self.parameter_name): self.primary_key, + '{}_management_client'.format(self.parameter_name): self.client + } + + def remove_resource(self, name, **kwargs): + if self.is_live: + group = self._get_resource_group(**kwargs) + self.client.namespaces.begin_delete(group.name, name).wait() + + def _get_resource_group(self, **kwargs): + try: + return kwargs.get(self.resource_group_parameter_name) + except KeyError: + template = 'To create an event hub a resource group is required. Please add ' \ + 'decorator @{} in front of this event hub preparer.' + raise AzureTestError(template.format(ResourceGroupPreparer.__name__)) + + +# Shared base class for event hub sub-resources that require a namespace and RG to exist. +class _EventHubChildResourcePreparer(AzureMgmtPreparer): + def __init__(self, + name_prefix='', + resource_group_parameter_name=RESOURCE_GROUP_PARAM, + eventhub_namespace_parameter_name=EVENTHUB_NAMESPACE_PARAM, + disable_recording=True, playback_fake_resource=None, + client_kwargs=None, random_name_enabled=True): + super(_EventHubChildResourcePreparer, self).__init__(name_prefix, 24, + random_name_enabled=random_name_enabled, + disable_recording=disable_recording, + playback_fake_resource=playback_fake_resource, + client_kwargs=client_kwargs) + self.resource_group_parameter_name = resource_group_parameter_name + self.eventhub_namespace_parameter_name = eventhub_namespace_parameter_name + + def _get_resource_group(self, **kwargs): + try: + return kwargs.get(self.resource_group_parameter_name) + except KeyError: + template = 'To create this event hub child resource event hub a resource group is required. Please add ' \ + 'decorator @{} in front of this event hub preparer.' + raise AzureTestError(template.format(ResourceGroupPreparer.__name__)) + + def _get_namespace(self, **kwargs): + try: + return kwargs.get(self.eventhub_namespace_parameter_name) + except KeyError: + template = 'To create this event hub child resource a event hub namespace is required. Please add ' \ + 'decorator @{} in front of this event hub preparer.' + raise AzureTestError(template.format(EventHubNamespacePreparer.__name__)) + + +class EventHubPreparer(_EventHubChildResourcePreparer): + def __init__(self, + name_prefix='', + use_cache=False, + parameter_name=EVENTHUB_PARAM, + resource_group_parameter_name=RESOURCE_GROUP_PARAM, + eventhub_namespace_parameter_name=EVENTHUB_NAMESPACE_PARAM, + disable_recording=True, playback_fake_resource=None, + client_kwargs=None, random_name_enabled=True): + super(EventHubPreparer, self).__init__(name_prefix, + random_name_enabled=random_name_enabled, + resource_group_parameter_name=resource_group_parameter_name, + eventhub_namespace_parameter_name=eventhub_namespace_parameter_name, + disable_recording=disable_recording, + playback_fake_resource=playback_fake_resource, + client_kwargs=client_kwargs) + self.parameter_name = parameter_name + if random_name_enabled: + self.resource_moniker = self.name_prefix + "eventhub" + self.set_cache(use_cache) + + def create_resource(self, name, **kwargs): + if self.is_live: + self.client = self.create_mgmt_client(EventHubManagementClient) + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + self.resource = self.client.event_hubs.create_or_update( + group.name, + namespace.name, + name, + {} + ) + else: + self.resource = FakeResource(name=name, id=name) + return { + self.parameter_name: self.resource, + } + + def remove_resource(self, name, **kwargs): + if self.is_live: + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + self.client.event_hubs.delete(group.name, namespace.name, name, polling=False) + + +class EventHubNamespaceAuthorizationRulePreparer(_EventHubChildResourcePreparer): + def __init__(self, + name_prefix='', + use_cache=False, + access_rights=[AccessRights.manage, AccessRights.send, AccessRights.listen], + parameter_name=EVENTHUB_AUTHORIZATION_RULE_PARAM, + resource_group_parameter_name=RESOURCE_GROUP_PARAM, + eventhub_namespace_parameter_name=EVENTHUB_NAMESPACE_PARAM, + disable_recording=True, playback_fake_resource=None, + client_kwargs=None, random_name_enabled=True): + super(EventHubNamespaceAuthorizationRulePreparer, self).__init__(name_prefix, + random_name_enabled=random_name_enabled, + resource_group_parameter_name=resource_group_parameter_name, + eventhub_namespace_parameter_name=eventhub_namespace_parameter_name, + disable_recording=disable_recording, + playback_fake_resource=playback_fake_resource, + client_kwargs=client_kwargs) + self.parameter_name = parameter_name + self.access_rights = access_rights + if random_name_enabled: + self.resource_moniker = self.name_prefix + "sbnameauth" + self.set_cache(use_cache, access_rights) + + def create_resource(self, name, **kwargs): + if self.is_live: + self.client = self.create_mgmt_client(EventHubManagementClient) + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + self.resource = self.client.namespaces.create_or_update_authorization_rule( + group.name, + namespace.name, + name, + self.access_rights + ) + + key = self.client.namespaces.list_keys(group.name, namespace.name, name) + connection_string = key.primary_connection_string + else: + self.resource = FakeResource(name=name, id=name) + connection_string = 'https://microsoft.com' + return { + self.parameter_name: self.resource, + '{}_connection_string'.format(self.parameter_name): connection_string, + } + + def remove_resource(self, name, **kwargs): + if self.is_live: + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + self.client.namespaces.delete_authorization_rule(group.name, namespace.name, name, polling=False) + + +class EventHubAuthorizationRulePreparer(_EventHubChildResourcePreparer): + def __init__(self, + name_prefix='', + use_cache=False, + access_rights=[AccessRights.manage, AccessRights.send, AccessRights.listen], + parameter_name=EVENTHUB_AUTHORIZATION_RULE_PARAM, + resource_group_parameter_name=RESOURCE_GROUP_PARAM, + eventhub_namespace_parameter_name=EVENTHUB_NAMESPACE_PARAM, + eventhub_parameter_name=EVENTHUB_PARAM, + disable_recording=True, playback_fake_resource=None, + client_kwargs=None, random_name_enabled=True): + super(EventHubAuthorizationRulePreparer, self).__init__(name_prefix, + random_name_enabled=random_name_enabled, + resource_group_parameter_name=resource_group_parameter_name, + eventhub_namespace_parameter_name=eventhub_namespace_parameter_name, + disable_recording=disable_recording, + playback_fake_resource=playback_fake_resource, + client_kwargs=client_kwargs) + self.parameter_name = parameter_name + self.access_rights = access_rights + self.eventhub_parameter_name = eventhub_parameter_name + if random_name_enabled: + self.resource_moniker = self.name_prefix + "evnethubauth" + self.set_cache(use_cache, access_rights) + + def create_resource(self, name, **kwargs): + if self.is_live: + self.client = self.create_mgmt_client(EventHubManagementClient) + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + eventhub = self._get_eventhub(**kwargs) + self.resource = self.client.event_hubs.create_or_update_authorization_rule( + group.name, + namespace.name, + eventhub.name, + name, + self.access_rights + ) + + key = self.client.event_hubs.list_keys(group.name, namespace.name, eventhub.name, name) + connection_string = key.primary_connection_string + else: + self.resource = FakeResource(name=name, id=name) + connection_string = 'https://microsoft.com' + return { + self.parameter_name: self.resource, + '{}_connection_string'.format(self.parameter_name): connection_string, + } + + def remove_resource(self, name, **kwargs): + if self.is_live: + group = self._get_resource_group(**kwargs) + namespace = self._get_namespace(**kwargs) + eventhub = self._get_eventhub(**kwargs) + self.client.event_hubs.delete_authorization_rule(group.name, namespace.name, eventhub.name, name, polling=False) + + def _get_eventhub(self, **kwargs): + try: + return kwargs.get(self.eventhub_parameter_name) + except KeyError: + template = 'To create this event hub authorization rule a event hub is required. Please add ' \ + 'decorator @{} in front of this event hub preparer.' + raise AzureTestError(template.format(EventHubPreparer.__name__)) + + +CachedEventHubNamespacePreparer = functools.partial(EventHubNamespacePreparer, use_cache=True) +CachedEventHubPreparer = functools.partial(EventHubPreparer, use_cache=True) diff --git a/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_auth_async.py b/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_auth_async.py index 96842a2298fa..1a055feacfde 100644 --- a/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_auth_async.py +++ b/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_auth_async.py @@ -7,19 +7,14 @@ import pytest import asyncio +from azure.identity.aio import EnvironmentCredential from azure.eventhub import EventData from azure.eventhub.aio import EventHubConsumerClient, EventHubProducerClient @pytest.mark.liveTest @pytest.mark.asyncio -async def test_client_secret_credential_async(aad_credential, live_eventhub): - try: - from azure.identity.aio import EnvironmentCredential - except ImportError: - pytest.skip("No azure identity library") - return - +async def test_client_secret_credential_async(live_eventhub): credential = EnvironmentCredential() producer_client = EventHubProducerClient(fully_qualified_namespace=live_eventhub['hostname'], eventhub_name=live_eventhub['event_hub'], diff --git a/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_negative_async.py b/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_negative_async.py index 09ef3a14be4d..eb1fc0beb663 100644 --- a/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_negative_async.py +++ b/sdk/eventhub/azure-eventhub/tests/livetest/asynctests/test_negative_async.py @@ -40,11 +40,11 @@ async def test_send_with_invalid_hostname_async(invalid_hostname, connstr_receiv ["hostname", "key_name", "access_key", "event_hub", "partition"]) @pytest.mark.liveTest @pytest.mark.asyncio -async def test_receive_with_invalid_param_async(live_eventhub_config, invalid_place): - eventhub_config = live_eventhub_config.copy() +async def test_receive_with_invalid_param_async(live_eventhub, invalid_place): + eventhub_config = live_eventhub.copy() if invalid_place != "partition": eventhub_config[invalid_place] = "invalid " + invalid_place - conn_str = live_eventhub_config["connection_str"].format( + conn_str = live_eventhub["connection_str"].format( eventhub_config['hostname'], eventhub_config['key_name'], eventhub_config['access_key'], diff --git a/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_auth.py b/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_auth.py index aba361ed645f..3959164d902d 100644 --- a/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_auth.py +++ b/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_auth.py @@ -7,15 +7,12 @@ import time import threading +from azure.identity import EnvironmentCredential from azure.eventhub import EventData, EventHubProducerClient, EventHubConsumerClient @pytest.mark.liveTest -def test_client_secret_credential(aad_credential, live_eventhub): - try: - from azure.identity import EnvironmentCredential - except: - pytest.skip("No azure identity library") +def test_client_secret_credential(live_eventhub): credential = EnvironmentCredential() producer_client = EventHubProducerClient(fully_qualified_namespace=live_eventhub['hostname'], eventhub_name=live_eventhub['event_hub'], diff --git a/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_negative.py b/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_negative.py index 5228f1aa4148..d3b775381796 100644 --- a/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_negative.py +++ b/sdk/eventhub/azure-eventhub/tests/livetest/synctests/test_negative.py @@ -50,13 +50,8 @@ def on_event(partition_context, event): @pytest.mark.liveTest -def test_send_batch_with_invalid_key(live_eventhub): - conn_str = live_eventhub["connection_str"].format( - live_eventhub['hostname'], - live_eventhub['key_name'], - 'invalid', - live_eventhub['event_hub']) - client = EventHubProducerClient.from_connection_string(conn_str) +def test_send_batch_with_invalid_key(invalid_key): + client = EventHubProducerClient.from_connection_string(invalid_key) try: with pytest.raises(ConnectError): batch = EventDataBatch() diff --git a/sdk/eventhub/tests.yml b/sdk/eventhub/tests.yml index 8b3513f01bad..d4f7da2e5f17 100644 --- a/sdk/eventhub/tests.yml +++ b/sdk/eventhub/tests.yml @@ -33,3 +33,4 @@ jobs: AZURE_CLIENT_ID: $(python-eh-livetest-event-hub-aad-client-id) AZURE_TENANT_ID: $(python-eh-livetest-event-hub-aad-tenant-id) AZURE_CLIENT_SECRET: $(python-eh-livetest-event-hub-aad-secret) + AZURE_SUBSCRIPTION_ID: $(python-eh-livetest-event-hub-subscription-id)