Skip to content

Commit eefe01a

Browse files
authored
[Test Proxy] Add RecordedByProxy decorator and AzureRecordedTestCase (#20138)
1 parent a21d368 commit eefe01a

File tree

12 files changed

+504
-7
lines changed

12 files changed

+504
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,6 @@ sdk/cosmos/azure-cosmos/test/test_config.py
114114

115115
# env vars
116116
.env
117+
118+
# local SSL certificate folder
119+
.certificate

sdk/tables/azure-data-tables/tests/test_table_service_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from preparers import tables_decorator
1111

1212
# --Test Class -----------------------------------------------------------------
13-
class TableServiceStatsTest(AzureTestCase, TableTestCase):
13+
class TestTableServiceStats(AzureTestCase, TableTestCase):
1414

1515
# --Test cases per service ---------------------------------------
1616
@tables_decorator

sdk/tables/azure-data-tables/tests/test_table_service_stats_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'></StorageServiceStats> '
2020

2121

22-
class TableServiceStatsTest(AzureTestCase, AsyncTableTestCase):
22+
class TestTableServiceStats(AzureTestCase, AsyncTableTestCase):
2323

2424
@staticmethod
2525
def override_response_body_with_unavailable_status(response):

tools/azure-devtools/src/azure_devtools/scenario_tests/preparers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import contextlib
77
import functools
88
import logging
9-
import sys
109
from collections import namedtuple
1110
from threading import Lock
1211

@@ -135,7 +134,14 @@ def _preparer_wrapper(test_class_instance, **kwargs):
135134
)
136135

137136
if test_class_instance.is_live:
138-
test_class_instance.scrubber.register_name_pair(resource_name, self.moniker)
137+
# Adding this for new proxy testcase
138+
if hasattr(test_class_instance, "scrubber"):
139+
test_class_instance.scrubber.register_name_pair(resource_name, self.moniker)
140+
else:
141+
_logger.info(
142+
"This test class instance has no scrubber, so the AbstractPreparer will not scrub any values "
143+
"in recordings."
144+
)
139145

140146
# We shouldn't trim the same kwargs that we use for deletion,
141147
# we may remove some of the variables we needed to do the delete.

tools/azure-sdk-tools/devtools_testutils/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .mgmt_testcase import AzureMgmtTestCase, AzureMgmtPreparer
2+
from .azure_recorded_testcase import AzureRecordedTestCase
23
from .azure_testcase import AzureTestCase, is_live, get_region_override
34
from .resource_testcase import (
45
FakeResource,
@@ -14,12 +15,15 @@
1415
)
1516
from .keyvault_preparer import KeyVaultPreparer
1617
from .powershell_preparer import PowerShellPreparer
18+
from .proxy_testcase import RecordedByProxy
19+
from .enums import ProxyRecordingSanitizer
1720
from .helpers import ResponseCallback, RetryCounter
1821
from .fake_credential import FakeTokenCredential
1922

2023
__all__ = [
2124
"AzureMgmtTestCase",
2225
"AzureMgmtPreparer",
26+
"AzureRecordedTestCase",
2327
"FakeResource",
2428
"ResourceGroupPreparer",
2529
"StorageAccountPreparer",
@@ -33,6 +37,8 @@
3337
"RandomNameResourceGroupPreparer",
3438
"CachedResourceGroupPreparer",
3539
"PowerShellPreparer",
40+
"ProxyRecordingSanitizer",
41+
"RecordedByProxy",
3642
"ResponseCallback",
3743
"RetryCounter",
3844
"FakeTokenCredential",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .proxy_testcase_async import RecordedByProxyAsync
2+
3+
__all__ = ["RecordedByProxyAsync"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
from azure.core.pipeline.transport import AioHttpTransport
7+
8+
from azure_devtools.scenario_tests.utilities import trim_kwargs_from_test_function
9+
from ..proxy_testcase import (
10+
get_test_id,
11+
start_record_or_playback,
12+
transform_request,
13+
stop_record_or_playback,
14+
)
15+
16+
17+
def RecordedByProxyAsync(func):
18+
async def record_wrap(*args, **kwargs):
19+
test_id = get_test_id()
20+
recording_id = start_record_or_playback(test_id)
21+
22+
def transform_args(*args, **kwargs):
23+
copied_positional_args = list(args)
24+
request = copied_positional_args[1]
25+
26+
transform_request(request, recording_id)
27+
28+
return tuple(copied_positional_args), kwargs
29+
30+
trimmed_kwargs = {k: v for k, v in kwargs.items()}
31+
trim_kwargs_from_test_function(func, trimmed_kwargs)
32+
33+
original_func = AioHttpTransport.send
34+
35+
async def combined_call(*args, **kwargs):
36+
adjusted_args, adjusted_kwargs = transform_args(*args, **kwargs)
37+
return await original_func(*adjusted_args, **adjusted_kwargs)
38+
39+
AioHttpTransport.send = combined_call
40+
41+
# call the modified function.
42+
try:
43+
value = await func(*args, **trimmed_kwargs)
44+
finally:
45+
AioHttpTransport.send = original_func
46+
stop_record_or_playback(test_id, recording_id)
47+
48+
return value
49+
50+
return record_wrap

0 commit comments

Comments
 (0)