Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "python",
"TagPrefix": "python/eventgrid/azure-eventgrid",
"Tag": "python/eventgrid/azure-eventgrid_d176c3a3b4"
"Tag": "python/eventgrid/azure-eventgrid_6f56978914"
}
44 changes: 31 additions & 13 deletions sdk/eventgrid/azure-eventgrid/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,50 @@
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
import platform
import sys
import os
import pytest
from devtools_testutils import test_proxy
from urllib.parse import urlparse
from devtools_testutils import add_general_string_sanitizer, test_proxy, add_body_key_sanitizer, add_header_regex_sanitizer, add_oauth_response_sanitizer, add_body_regex_sanitizer
from devtools_testutils.sanitizers import (
add_remove_header_sanitizer,
add_general_regex_sanitizer,
set_custom_default_matcher,
)

# Ignore async tests for Python < 3.5
collect_ignore_glob = []
if sys.version_info < (3, 5):
collect_ignore_glob.append("*_async.py")
collect_ignore_glob.append("test_cncf*")


@pytest.fixture(scope="session", autouse=True)
def add_aeg_sanitizer(test_proxy):
def add_sanitizers(test_proxy):
# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
set_custom_default_matcher(
compare_bodies=False,
excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id",
)
add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token")
add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token, aeg-channel-name")
add_general_regex_sanitizer(
value="fakeresource",
regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.eventgrid\\.azure\\.net/api/events)",
regex="(?<=\\/\\/)[.*]+(?=\\.eastus-1\\.eventgrid\\.azure\\.net/api/events)",
)

add_oauth_response_sanitizer()
add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]")
add_header_regex_sanitizer(key="Cookie", value="cookie;")

add_body_key_sanitizer(json_path="$..id", value="id")

client_id = os.getenv("AZURE_CLIENT_ID", "sanitized")
client_secret = os.getenv("AZURE_CLIENT_SECRET", "sanitized")
eventgrid_client_id = os.getenv("EVENTGRID_CLIENT_ID", "sanitized")
eventgrid_client_secret = os.getenv("EVENTGRID_CLIENT_SECRET", "sanitized")
tenant_id = os.getenv("AZURE_TENANT_ID", "sanitized")
eventgrid_topic_endpoint = os.getenv("EVENTGRID_TOPIC_ENDPOINT", "sanitized")
# Need to santize namespace for eventgrid_topic:
try:
eventgrid_hostname = urlparse(eventgrid_topic_endpoint).hostname
add_general_string_sanitizer(target=eventgrid_hostname.upper(), value="sanitized.eastus-1.eventgrid.azure.net")
except:
pass
add_general_string_sanitizer(target=client_id, value="00000000-0000-0000-0000-000000000000")
add_general_string_sanitizer(target=client_secret, value="sanitized")
add_general_string_sanitizer(target=eventgrid_client_id, value="00000000-0000-0000-0000-000000000000")
add_general_string_sanitizer(target=eventgrid_client_secret, value="sanitized")
add_general_string_sanitizer(target=tenant_id, value="00000000-0000-0000-0000-000000000000")

26 changes: 15 additions & 11 deletions sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import functools
from devtools_testutils import PowerShellPreparer
from devtools_testutils import PowerShellPreparer, EnvironmentVariableLoader

from azure.mgmt.eventgrid.models import (
Topic,
Expand All @@ -8,12 +8,9 @@
JsonField,
JsonFieldWithDefault,
)
from azure_devtools.scenario_tests.exceptions import AzureTestError

from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM

EVENTGRID_TOPIC_PARAM = "eventgrid_topic"
EVENTGRID_TOPIC_LOCATION = "westus"
EVENTGRID_TOPIC_LOCATION = "eastus"
CLOUD_EVENT_SCHEMA = InputSchema.cloud_event_schema_v1_0
CUSTOM_EVENT_SCHEMA = InputSchema.custom_event_schema
ID_JSON_FIELD = JsonField(source_field="customId")
Expand All @@ -40,14 +37,21 @@
EventGridPreparer = functools.partial(
PowerShellPreparer,
"eventgrid",
eventgrid_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
eventgrid_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
eventgrid_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_cloud_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
eventgrid_cloud_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_cloud_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_cloud_event_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
eventgrid_cloud_event_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_cloud_event_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_custom_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
eventgrid_custom_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_custom_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
)
eventgrid_partner_channel_name="fakechannel",
eventgrid_partner_namespace_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_partner_namespace_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
eventgrid_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
eventgrid_topic_name="faketopic",
eventgrid_event_subscription_name="fakesub",
)
24 changes: 12 additions & 12 deletions sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def create_eg_publisher_client(self, endpoint):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_dict(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_dict(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -42,8 +42,8 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_base64_using_data(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_base64_using_data(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -62,8 +62,8 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_none(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_none(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -76,8 +76,8 @@ def test_send_cloud_event_data_none(

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_str(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_str(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -96,8 +96,8 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_as_list(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_as_list(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -110,8 +110,8 @@ def test_send_cloud_event_data_as_list(

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_with_extensions(
self, variables, eventgrid_cloud_event_topic_endpoint
def test_send_cncf_data_with_extensions(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand Down
24 changes: 12 additions & 12 deletions sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def create_eg_publisher_client(self, endpoint):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_dict(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_dict_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -46,8 +46,8 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_base64_using_data(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_base64_using_data_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -67,8 +67,8 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_none(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_none_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -82,8 +82,8 @@ async def test_send_cloud_event_data_none(
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_str(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_str_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -103,8 +103,8 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_as_list(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_as_list_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand All @@ -118,8 +118,8 @@ async def test_send_cloud_event_data_as_list(
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_with_extensions(
self, variables, eventgrid_cloud_event_topic_endpoint
async def test_send_cncf_data_with_extensions_async(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
Expand Down
78 changes: 78 additions & 0 deletions sdk/eventgrid/azure-eventgrid/tests/test_eg_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
import os
import time
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
from azure.eventgrid import EventGridClient
from azure.eventgrid.models import *
from azure.core.messaging import CloudEvent
from azure.core.credentials import AzureKeyCredential

from eventgrid_preparer import EventGridPreparer


class TestEGClientExceptions(AzureRecordedTestCase):
def create_eg_client(self, endpoint, key):
client = EventGridClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
return client

@pytest.mark.skip("need to update conftest")
@EventGridPreparer()
@recorded_by_proxy
def test_publish_receive_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name):
client = self.create_eg_client(eventgrid_endpoint, eventgrid_key)

event = CloudEvent(
type="Contoso.Items.ItemReceived",
source="source",
subject="MySubject",
data=b'this is binary data',
)

client.publish_cloud_events(
eventgrid_topic_name, body=[event]
)

time.sleep(5)

events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name,max_events=1)
lock_token = events.value[0].broker_properties.lock_token

ack = client.acknowledge_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=AcknowledgeOptions(lock_tokens=[lock_token]))
assert len(ack.succeeded_lock_tokens) == 1
assert len(ack.failed_lock_tokens) == 0

@pytest.mark.skip("need to update conftest")
@EventGridPreparer()
@recorded_by_proxy
def test_publish_release_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name):
client = self.create_eg_client(eventgrid_endpoint, eventgrid_key)

event = CloudEvent(
type="Contoso.Items.ItemReceived",
source="source",
subject="MySubject",
data=b'this is binary data',
)

client.publish_cloud_events(
eventgrid_topic_name, body=[event]
)

time.sleep(5)

events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1)
lock_token = events.value[0].broker_properties.lock_token

ack = client.release_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=ReleaseOptions(lock_tokens=[lock_token]))
assert len(ack.succeeded_lock_tokens) == 1
assert len(ack.failed_lock_tokens) == 0

events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1)
assert events.value[0].broker_properties.delivery_count > 1
Loading