Skip to content

Commit

Permalink
Beta LiveTests (Azure#30728)
Browse files Browse the repository at this point in the history
* add bicep file for tests

* update output

* update test

* secret sanitization

* refactor failing test

* update conftest

* update assets and sanitizers

* update preparer loc

* update conftest

* conftest

* update conftest

* remove variables for now

* update assets

* update tests

* try to update regex

* update recordings

* update conftest

* update preparer

* update test

* update exception test

* update tests

* update asset

* update conftest

* pr comments

* default needs to be eastus

* import
  • Loading branch information
l0lawrence committed Feb 16, 2024
1 parent d998006 commit 1c44202
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 202 deletions.
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")

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

from azure.mgmt.eventgrid.models import Topic, InputSchema, JsonInputSchemaMapping, JsonField, JsonFieldWithDefault

from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM
from azure.mgmt.eventgrid.models import (
Topic,
InputSchema,
JsonInputSchemaMapping,
JsonField,
JsonFieldWithDefault,
)

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 @@ -33,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: 18 additions & 6 deletions sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def create_eg_publisher_client(self, endpoint):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_dict(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -40,7 +42,9 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_base64_using_data(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -58,7 +62,9 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_none(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -70,7 +76,9 @@ def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endpoint):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_str(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -88,7 +96,9 @@ def callback(request):

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_as_list(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -100,7 +110,9 @@ def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_endpoin

@EventGridPreparer()
@recorded_by_proxy
def test_send_cloud_event_data_with_extensions(self, 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 = {
"type": "com.example.sampletype1",
Expand Down
24 changes: 18 additions & 6 deletions sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def create_eg_publisher_client(self, endpoint):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_dict(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -44,7 +46,9 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_base64_using_data(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -63,7 +67,9 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_none(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -76,7 +82,9 @@ async def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endp
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_str(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -95,7 +103,9 @@ def callback(request):
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_as_list(self, 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 = {
"type": "com.example.sampletype1",
Expand All @@ -108,7 +118,9 @@ async def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_e
@EventGridPreparer()
@recorded_by_proxy_async
@pytest.mark.asyncio
async def test_send_cloud_event_data_with_extensions(self, 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 = {
"type": "com.example.sampletype1",
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

0 comments on commit 1c44202

Please sign in to comment.