Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test Proxy] Make add_sanitizer a module-level method #20701

Merged
merged 3 commits into from
Sep 16, 2021
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
3 changes: 2 additions & 1 deletion tools/azure-sdk-tools/devtools_testutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .mgmt_testcase import AzureMgmtTestCase, AzureMgmtPreparer
from .azure_recorded_testcase import AzureRecordedTestCase
from .azure_recorded_testcase import add_sanitizer, AzureRecordedTestCase
from .azure_testcase import AzureTestCase, is_live, get_region_override
from .resource_testcase import (
FakeResource,
Expand All @@ -21,6 +21,7 @@
from .fake_credential import FakeTokenCredential

__all__ = [
"add_sanitizer",
"AzureMgmtTestCase",
"AzureMgmtPreparer",
"AzureRecordedTestCase",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,42 @@
pass

if TYPE_CHECKING:
from typing import Optional
from typing import Any


load_dotenv(find_dotenv())


def add_sanitizer(sanitizer, **kwargs):
# type: (ProxyRecordingSanitizer, **Any) -> None
"""Registers a sanitizer, matcher, or transform with the test proxy.

:param sanitizer: The name of the sanitizer, matcher, or transform you want to add.
:type sanitizer: ProxyRecordingSanitizer or str

:keyword str value: The substitution value.
:keyword str regex: A regex for a sanitizer. Can be defined as a simple regex, or if a ``group_for_replace`` is
provided, a substitution operation.
:keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking
a simple replacement operation.
"""
request_args = {}
request_args["value"] = kwargs.get("value") or "fakevalue"
request_args["regex"] = kwargs.get("regex") or "[a-z]+(?=(?:-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)"
request_args["group_for_replace"] = kwargs.get("group_for_replace")

if sanitizer == ProxyRecordingSanitizer.URI:
requests.post(
"{}/Admin/AddSanitizer".format(PROXY_URL),
headers={"x-abstraction-identifier": ProxyRecordingSanitizer.URI.value},
json={
"regex": request_args["regex"],
"value": request_args["value"],
"groupForReplace": request_args["group_for_replace"]
},
)


def is_live():
"""A module version of is_live, that could be used in pytest marker."""
if not hasattr(is_live, "_cache"):
Expand Down Expand Up @@ -81,18 +111,6 @@ def in_recording(self):
def recording_processors(self):
return []

def add_sanitizer(self, sanitizer, regex=None, value=None):
# type: (ProxyRecordingSanitizer, Optional[str], Optional[str]) -> None
if sanitizer == ProxyRecordingSanitizer.URI:
requests.post(
"{}/Admin/AddSanitizer".format(PROXY_URL),
headers={"x-abstraction-identifier": ProxyRecordingSanitizer.URI.value},
json={
"regex": regex or "[a-z]+(?=(?:-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)",
"value": value or "fakevalue"
},
)

def is_playback(self):
return not self.is_live

Expand Down