Skip to content

Commit 8e5f0dc

Browse files
committed
Beta LiveTests (#30728)
* 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
1 parent f352ba0 commit 8e5f0dc

12 files changed

+445
-202
lines changed

sdk/eventgrid/azure-eventgrid/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/eventgrid/azure-eventgrid",
5-
"Tag": "python/eventgrid/azure-eventgrid_d176c3a3b4"
5+
"Tag": "python/eventgrid/azure-eventgrid_6f56978914"
66
}

sdk/eventgrid/azure-eventgrid/tests/conftest.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,50 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26-
import platform
27-
import sys
26+
import os
2827
import pytest
29-
from devtools_testutils import test_proxy
28+
from urllib.parse import urlparse
29+
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
3030
from devtools_testutils.sanitizers import (
3131
add_remove_header_sanitizer,
3232
add_general_regex_sanitizer,
3333
set_custom_default_matcher,
3434
)
3535

36-
# Ignore async tests for Python < 3.5
37-
collect_ignore_glob = []
38-
if sys.version_info < (3, 5):
39-
collect_ignore_glob.append("*_async.py")
40-
collect_ignore_glob.append("test_cncf*")
41-
42-
4336
@pytest.fixture(scope="session", autouse=True)
44-
def add_aeg_sanitizer(test_proxy):
37+
def add_sanitizers(test_proxy):
4538
# this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
4639
set_custom_default_matcher(
4740
compare_bodies=False,
4841
excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id",
4942
)
50-
add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token")
43+
add_remove_header_sanitizer(headers="aeg-sas-key, aeg-sas-token, aeg-channel-name")
5144
add_general_regex_sanitizer(
5245
value="fakeresource",
53-
regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.eventgrid\\.azure\\.net/api/events)",
46+
regex="(?<=\\/\\/)[.*]+(?=\\.eastus-1\\.eventgrid\\.azure\\.net/api/events)",
5447
)
48+
49+
add_oauth_response_sanitizer()
50+
add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]")
51+
add_header_regex_sanitizer(key="Cookie", value="cookie;")
52+
53+
add_body_key_sanitizer(json_path="$..id", value="id")
54+
55+
client_id = os.getenv("AZURE_CLIENT_ID", "sanitized")
56+
client_secret = os.getenv("AZURE_CLIENT_SECRET", "sanitized")
57+
eventgrid_client_id = os.getenv("EVENTGRID_CLIENT_ID", "sanitized")
58+
eventgrid_client_secret = os.getenv("EVENTGRID_CLIENT_SECRET", "sanitized")
59+
tenant_id = os.getenv("AZURE_TENANT_ID", "sanitized")
60+
eventgrid_topic_endpoint = os.getenv("EVENTGRID_TOPIC_ENDPOINT", "sanitized")
61+
# Need to santize namespace for eventgrid_topic:
62+
try:
63+
eventgrid_hostname = urlparse(eventgrid_topic_endpoint).hostname
64+
add_general_string_sanitizer(target=eventgrid_hostname.upper(), value="sanitized.eastus-1.eventgrid.azure.net")
65+
except:
66+
pass
67+
add_general_string_sanitizer(target=client_id, value="00000000-0000-0000-0000-000000000000")
68+
add_general_string_sanitizer(target=client_secret, value="sanitized")
69+
add_general_string_sanitizer(target=eventgrid_client_id, value="00000000-0000-0000-0000-000000000000")
70+
add_general_string_sanitizer(target=eventgrid_client_secret, value="sanitized")
71+
add_general_string_sanitizer(target=tenant_id, value="00000000-0000-0000-0000-000000000000")
72+

sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
from devtools_testutils import PowerShellPreparer
2+
from devtools_testutils import PowerShellPreparer, EnvironmentVariableLoader
33

44
from azure.mgmt.eventgrid.models import (
55
Topic,
@@ -8,12 +8,9 @@
88
JsonField,
99
JsonFieldWithDefault,
1010
)
11-
from azure_devtools.scenario_tests.exceptions import AzureTestError
12-
13-
from devtools_testutils.resource_testcase import RESOURCE_GROUP_PARAM
1411

1512
EVENTGRID_TOPIC_PARAM = "eventgrid_topic"
16-
EVENTGRID_TOPIC_LOCATION = "westus"
13+
EVENTGRID_TOPIC_LOCATION = "eastus"
1714
CLOUD_EVENT_SCHEMA = InputSchema.cloud_event_schema_v1_0
1815
CUSTOM_EVENT_SCHEMA = InputSchema.custom_event_schema
1916
ID_JSON_FIELD = JsonField(source_field="customId")
@@ -40,14 +37,21 @@
4037
EventGridPreparer = functools.partial(
4138
PowerShellPreparer,
4239
"eventgrid",
43-
eventgrid_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
40+
eventgrid_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
4441
eventgrid_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
45-
eventgrid_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
42+
eventgrid_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
4643
eventgrid_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
47-
eventgrid_cloud_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
44+
eventgrid_cloud_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
4845
eventgrid_cloud_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
49-
eventgrid_cloud_event_domain_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
46+
eventgrid_cloud_event_domain_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
5047
eventgrid_cloud_event_domain_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
51-
eventgrid_custom_event_topic_endpoint="https://fakeresource.westus2-1.eventgrid.azure.net/api/events",
48+
eventgrid_custom_event_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
5249
eventgrid_custom_event_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
53-
)
50+
eventgrid_partner_channel_name="fakechannel",
51+
eventgrid_partner_namespace_topic_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
52+
eventgrid_partner_namespace_topic_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
53+
eventgrid_endpoint="https://fakeresource.eastus-1.eventgrid.azure.net/api/events",
54+
eventgrid_key="fakekeyfakekeyfakekeyfakekeyfakekeyfakekeyA=",
55+
eventgrid_topic_name="faketopic",
56+
eventgrid_event_subscription_name="fakesub",
57+
)

sdk/eventgrid/azure-eventgrid/tests/test_cncf_events.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def create_eg_publisher_client(self, endpoint):
2020

2121
@EventGridPreparer()
2222
@recorded_by_proxy
23-
def test_send_cloud_event_data_dict(self, eventgrid_cloud_event_topic_endpoint):
23+
def test_send_cncf_data_dict(
24+
self, eventgrid_cloud_event_topic_endpoint
25+
):
2426
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
2527
attributes = {
2628
"type": "com.example.sampletype1",
@@ -40,7 +42,9 @@ def callback(request):
4042

4143
@EventGridPreparer()
4244
@recorded_by_proxy
43-
def test_send_cloud_event_data_base64_using_data(self, eventgrid_cloud_event_topic_endpoint):
45+
def test_send_cncf_data_base64_using_data(
46+
self, eventgrid_cloud_event_topic_endpoint
47+
):
4448
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
4549
attributes = {
4650
"type": "com.example.sampletype1",
@@ -58,7 +62,9 @@ def callback(request):
5862

5963
@EventGridPreparer()
6064
@recorded_by_proxy
61-
def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endpoint):
65+
def test_send_cncf_data_none(
66+
self, eventgrid_cloud_event_topic_endpoint
67+
):
6268
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
6369
attributes = {
6470
"type": "com.example.sampletype1",
@@ -70,7 +76,9 @@ def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endpoint):
7076

7177
@EventGridPreparer()
7278
@recorded_by_proxy
73-
def test_send_cloud_event_data_str(self, eventgrid_cloud_event_topic_endpoint):
79+
def test_send_cncf_data_str(
80+
self, eventgrid_cloud_event_topic_endpoint
81+
):
7482
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
7583
attributes = {
7684
"type": "com.example.sampletype1",
@@ -88,7 +96,9 @@ def callback(request):
8896

8997
@EventGridPreparer()
9098
@recorded_by_proxy
91-
def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_endpoint):
99+
def test_send_cncf_data_as_list(
100+
self, eventgrid_cloud_event_topic_endpoint
101+
):
92102
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
93103
attributes = {
94104
"type": "com.example.sampletype1",
@@ -100,7 +110,9 @@ def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_endpoin
100110

101111
@EventGridPreparer()
102112
@recorded_by_proxy
103-
def test_send_cloud_event_data_with_extensions(self, eventgrid_cloud_event_topic_endpoint):
113+
def test_send_cncf_data_with_extensions(
114+
self, eventgrid_cloud_event_topic_endpoint
115+
):
104116
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
105117
attributes = {
106118
"type": "com.example.sampletype1",

sdk/eventgrid/azure-eventgrid/tests/test_cncf_events_async.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def create_eg_publisher_client(self, endpoint):
2323
@EventGridPreparer()
2424
@recorded_by_proxy_async
2525
@pytest.mark.asyncio
26-
async def test_send_cloud_event_data_dict(self, eventgrid_cloud_event_topic_endpoint):
26+
async def test_send_cncf_data_dict_async(
27+
self, eventgrid_cloud_event_topic_endpoint
28+
):
2729
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
2830
attributes = {
2931
"type": "com.example.sampletype1",
@@ -44,7 +46,9 @@ def callback(request):
4446
@EventGridPreparer()
4547
@recorded_by_proxy_async
4648
@pytest.mark.asyncio
47-
async def test_send_cloud_event_data_base64_using_data(self, eventgrid_cloud_event_topic_endpoint):
49+
async def test_send_cncf_data_base64_using_data_async(
50+
self, eventgrid_cloud_event_topic_endpoint
51+
):
4852
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
4953
attributes = {
5054
"type": "com.example.sampletype1",
@@ -63,7 +67,9 @@ def callback(request):
6367
@EventGridPreparer()
6468
@recorded_by_proxy_async
6569
@pytest.mark.asyncio
66-
async def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endpoint):
70+
async def test_send_cncf_data_none_async(
71+
self, eventgrid_cloud_event_topic_endpoint
72+
):
6773
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
6874
attributes = {
6975
"type": "com.example.sampletype1",
@@ -76,7 +82,9 @@ async def test_send_cloud_event_data_none(self, eventgrid_cloud_event_topic_endp
7682
@EventGridPreparer()
7783
@recorded_by_proxy_async
7884
@pytest.mark.asyncio
79-
async def test_send_cloud_event_data_str(self, eventgrid_cloud_event_topic_endpoint):
85+
async def test_send_cncf_data_str_async(
86+
self, eventgrid_cloud_event_topic_endpoint
87+
):
8088
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
8189
attributes = {
8290
"type": "com.example.sampletype1",
@@ -95,7 +103,9 @@ def callback(request):
95103
@EventGridPreparer()
96104
@recorded_by_proxy_async
97105
@pytest.mark.asyncio
98-
async def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_endpoint):
106+
async def test_send_cncf_data_as_list_async(
107+
self, eventgrid_cloud_event_topic_endpoint
108+
):
99109
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
100110
attributes = {
101111
"type": "com.example.sampletype1",
@@ -108,7 +118,9 @@ async def test_send_cloud_event_data_as_list(self, eventgrid_cloud_event_topic_e
108118
@EventGridPreparer()
109119
@recorded_by_proxy_async
110120
@pytest.mark.asyncio
111-
async def test_send_cloud_event_data_with_extensions(self, eventgrid_cloud_event_topic_endpoint):
121+
async def test_send_cncf_data_with_extensions_async(
122+
self, eventgrid_cloud_event_topic_endpoint
123+
):
112124
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
113125
attributes = {
114126
"type": "com.example.sampletype1",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# --------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
import pytest
7+
import os
8+
import time
9+
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
10+
from azure.eventgrid import EventGridClient
11+
from azure.eventgrid.models import *
12+
from azure.core.messaging import CloudEvent
13+
from azure.core.credentials import AzureKeyCredential
14+
15+
from eventgrid_preparer import EventGridPreparer
16+
17+
18+
class TestEGClientExceptions(AzureRecordedTestCase):
19+
def create_eg_client(self, endpoint, key):
20+
client = EventGridClient(
21+
endpoint=endpoint, credential=AzureKeyCredential(key)
22+
)
23+
return client
24+
25+
@pytest.mark.skip("need to update conftest")
26+
@EventGridPreparer()
27+
@recorded_by_proxy
28+
def test_publish_receive_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name):
29+
client = self.create_eg_client(eventgrid_endpoint, eventgrid_key)
30+
31+
event = CloudEvent(
32+
type="Contoso.Items.ItemReceived",
33+
source="source",
34+
subject="MySubject",
35+
data=b'this is binary data',
36+
)
37+
38+
client.publish_cloud_events(
39+
eventgrid_topic_name, body=[event]
40+
)
41+
42+
time.sleep(5)
43+
44+
events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name,max_events=1)
45+
lock_token = events.value[0].broker_properties.lock_token
46+
47+
ack = client.acknowledge_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=AcknowledgeOptions(lock_tokens=[lock_token]))
48+
assert len(ack.succeeded_lock_tokens) == 1
49+
assert len(ack.failed_lock_tokens) == 0
50+
51+
@pytest.mark.skip("need to update conftest")
52+
@EventGridPreparer()
53+
@recorded_by_proxy
54+
def test_publish_release_cloud_event(self, eventgrid_endpoint, eventgrid_key, eventgrid_topic_name, eventgrid_event_subscription_name):
55+
client = self.create_eg_client(eventgrid_endpoint, eventgrid_key)
56+
57+
event = CloudEvent(
58+
type="Contoso.Items.ItemReceived",
59+
source="source",
60+
subject="MySubject",
61+
data=b'this is binary data',
62+
)
63+
64+
client.publish_cloud_events(
65+
eventgrid_topic_name, body=[event]
66+
)
67+
68+
time.sleep(5)
69+
70+
events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1)
71+
lock_token = events.value[0].broker_properties.lock_token
72+
73+
ack = client.release_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, lock_tokens=ReleaseOptions(lock_tokens=[lock_token]))
74+
assert len(ack.succeeded_lock_tokens) == 1
75+
assert len(ack.failed_lock_tokens) == 0
76+
77+
events = client.receive_cloud_events(eventgrid_topic_name, eventgrid_event_subscription_name, max_events=1)
78+
assert events.value[0].broker_properties.delivery_count > 1

0 commit comments

Comments
 (0)