Skip to content

Commit

Permalink
update live_eventhub_config to create resources
Browse files Browse the repository at this point in the history
  • Loading branch information
swathipil committed Aug 16, 2022
1 parent c56cde4 commit 825e2b7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 14 deletions.
12 changes: 12 additions & 0 deletions .azure-pipelines/client.test.live.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ jobs:
pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
displayName: 'Run tests'
env:
AZURE_SUBSCRIPTION_ID: $(python-eh-livetest-event-hub-subscription-id)
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)
EVENT_HUB_HOSTNAME: $(python-eh-livetest-event-hub-hostname)
EVENT_HUB_NAME: $(python-eh-livetest-event-hub-name)
EVENT_HUB_SAS_POLICY: $(python-eh-livetest-event-hub-sas-policy)
Expand Down Expand Up @@ -184,6 +188,10 @@ jobs:
pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
displayName: 'Run tests'
env:
AZURE_SUBSCRIPTION_ID: $(python-eh-livetest-event-hub-subscription-id)
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)
EVENT_HUB_HOSTNAME: $(python-eh-livetest-event-hub-hostname)
EVENT_HUB_NAME: $(python-eh-livetest-event-hub-name)
EVENT_HUB_SAS_POLICY: $(python-eh-livetest-event-hub-sas-policy)
Expand Down Expand Up @@ -259,6 +267,10 @@ jobs:
pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
displayName: 'Run tests'
env:
AZURE_SUBSCRIPTION_ID: $(python-eh-livetest-event-hub-subscription-id)
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)
EVENT_HUB_HOSTNAME: $(python-eh-livetest-event-hub-hostname)
EVENT_HUB_NAME: $(python-eh-livetest-event-hub-name)
EVENT_HUB_SAS_POLICY: $(python-eh-livetest-event-hub-sas-policy)
Expand Down
3 changes: 3 additions & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pytest-asyncio==0.12.0; python_version >= '3.7'
docutils>=0.14
pygments>=2.2.0
pylint==2.3.1; python_version >= '3.7'
azure-mgmt-eventhub==10.0.0
azure-mgmt-resource==20.0.0
azure-identity
108 changes: 94 additions & 14 deletions samples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,111 @@
import os
import pytest
import sys
import uuid
import warnings
import datetime

from azure.identity import EnvironmentCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.eventhub import EventHubManagementClient


# Ignore async tests for Python < 3.5
collect_ignore = []
if sys.version_info < (3, 5):
collect_ignore.append("asynctests")

PARTITION_COUNT = 2
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): # pylint: disable=missing-function-docstring
parser.addoption(
"--sleep", action="store", default="True", help="sleep on reconnect test: True or False"
)

@pytest.fixture(scope="session")
def resource_group(): # pylint: disable=redefined-outer-name disable=missing-function-docstring
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())
parameters = {"location": LOCATION}
expiry = datetime.datetime.utcnow() + datetime.timedelta(days=1)
parameters['tags'] = {'DeleteAfter': expiry.replace(microsecond=0).isoformat()}
try:
rg = resource_client.resource_groups.create_or_update(
resource_group_name,
parameters
)
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 eventhub_namespace(resource_group): # pylint: disable=redefined-outer-name disable=missing-function-docstring
try:
SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"]
except KeyError:
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_config():
def live_eventhub_config(resource_group, eventhub_namespace): # pylint: disable=redefined-outer-name disable=missing-function-docstring
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['consumer_group'] = "$Default"
config['partition'] = "0"
SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"]
except KeyError:
pytest.skip("Live EventHub configuration not found.")
else:
if not all(config.values()):
pytest.skip("Live EventHub configuration empty.")
return config
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:
eventhub = resource_client.event_hubs.create_or_update(
resource_group.name, eventhub_ns_name, eventhub_name, {"partition_count": PARTITION_COUNT}
)
live_eventhub_config = {
'hostname': "{}.servicebus.windows.net".format(eventhub_ns_name),
'key_name': key_name,
'access_key': primary_key,
'event_hub': eventhub.name,
'consumer_group': '$Default',
'partition': '0',
}
yield live_eventhub_config
finally:
try:
resource_client.event_hubs.delete(resource_group.name, eventhub_ns_name, eventhub_name)
except:
warnings.warn(UserWarning("eventhub teardown failed"))


@pytest.fixture()
Expand Down Expand Up @@ -63,4 +144,3 @@ def rabbit_mq_config():
pytest.skip("Live RabbitMQ configuration not found.")
else:
return config

0 comments on commit 825e2b7

Please sign in to comment.