Skip to content

Commit b86fd4c

Browse files
[translation] convert to test proxy (Azure#21764)
* remove unittest dependency * initial work * some small changse * remove endpoint trailing slash on sanitizer value * updates for playback * test translation - convert to proxy + recordings * cancel, document status, supported formats tests - conversion + recordings * list translations/documents - convert and record * add test proxy to ci/tests.yml * fixes * rstrip endpoint * add oauth sanitizer * update conftest to automatically start proxy * update rstrip endpoint with better err msg * try pinning storage api version for sas urls * set bodiless matcher for sas urls * remove proxy from tests.yml * rerecord * rerecord with 1251067 image tag * use latest tag to get logging * go back to tag 1209124 * try rerecord * pin new version of proxy tool * patch from main, bumping proxy version * additional logging output * sanitizers to only be test level. * import appropriate deferred_send function from sanitizers.go * this will work, but is extremely inefficient. need to do better with the SAS token overrides * few test fixes * restore everything to align with main * rerecord all tests * try modifying ppe to only run with one python version * try fixing python version filtering for ppe * switch to passing kwargs based on latest test guidance * pop variables Co-authored-by: scbedd <[email protected]>
1 parent 6dfd0dd commit b86fd4c

File tree

160 files changed

+118818
-111456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+118818
-111456
lines changed

sdk/translation/azure-ai-translation-document/azure/ai/translation/document/_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ def __init__(
7474
:dedent: 4
7575
:caption: Creating the DocumentTranslationClient with a token credential.
7676
"""
77-
self._endpoint = endpoint
77+
try:
78+
self._endpoint = endpoint.rstrip("/")
79+
except AttributeError:
80+
raise ValueError("Parameter 'endpoint' must be a string.")
7881
self._credential = credential
7982
self._api_version = kwargs.pop("api_version", None)
8083

8184
authentication_policy = get_authentication_policy(credential)
8285
polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL)
8386
self._client = _BatchDocumentTranslationClient(
84-
endpoint=endpoint,
87+
endpoint=self._endpoint,
8588
credential=credential, # type: ignore
8689
api_version=self._api_version,
8790
sdk_moniker=USER_AGENT,

sdk/translation/azure-ai-translation-document/azure/ai/translation/document/aio/_client_async.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ def __init__(
8080
:dedent: 4
8181
:caption: Creating the DocumentTranslationClient with a token credential.
8282
"""
83-
self._endpoint = endpoint
83+
try:
84+
self._endpoint = endpoint.rstrip("/")
85+
except AttributeError:
86+
raise ValueError("Parameter 'endpoint' must be a string.")
8487
self._credential = credential
8588
self._api_version = kwargs.pop("api_version", None)
8689

8790
authentication_policy = get_authentication_policy(credential)
8891
polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL)
8992
self._client = _BatchDocumentTranslationClient(
90-
endpoint=endpoint,
93+
endpoint=self._endpoint,
9194
credential=credential, # type: ignore
9295
api_version=self._api_version,
9396
sdk_moniker=USER_AGENT,

sdk/translation/azure-ai-translation-document/tests/asynctestcase.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55

6-
import os
6+
from devtools_testutils import (
7+
AzureRecordedTestCase
8+
)
79
from testcase import DocumentTranslationTest, Document
810
from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget
911

10-
class AsyncDocumentTranslationTest(DocumentTranslationTest):
1112

12-
def __init__(self, method_name):
13-
super().__init__(method_name)
14-
15-
def generate_oauth_token(self):
16-
if self.is_live:
17-
from azure.identity.aio import ClientSecretCredential
18-
return ClientSecretCredential(
19-
os.getenv("TRANSLATION_TENANT_ID"),
20-
os.getenv("TRANSLATION_CLIENT_ID"),
21-
os.getenv("TRANSLATION_CLIENT_SECRET"),
22-
)
13+
class AsyncDocumentTranslationTest(DocumentTranslationTest, AzureRecordedTestCase):
2314

2415
async def _begin_and_validate_translation_async(self, async_client, translation_inputs, total_docs_count, language=None):
2516
# submit operation
2617
poller = await async_client.begin_translation(translation_inputs)
27-
self.assertIsNotNone(poller.id)
28-
self.assertIsNotNone(poller.details.id)
18+
assert poller.id is not None
19+
assert poller.details.id is not None
2920
# wait for result
3021
doc_statuses = await poller.result()
3122
# validate
@@ -36,6 +27,8 @@ async def _begin_and_validate_translation_async(self, async_client, translation_
3627

3728
# client helpers
3829
async def _begin_multiple_translations_async(self, async_client, operations_count, **kwargs):
30+
container_suffix = kwargs.pop('container_suffix', "")
31+
variables = kwargs.pop('variables', {})
3932
wait_for_operation = kwargs.pop('wait', True)
4033
language_code = kwargs.pop('language_code', "es")
4134
docs_per_operation = kwargs.pop('docs_per_operation', 2)
@@ -49,8 +42,8 @@ async def _begin_multiple_translations_async(self, async_client, operations_coun
4942
no need for async container clients!
5043
'''
5144
blob_data = Document.create_dummy_docs(docs_per_operation)
52-
source_container_sas_url = self.create_source_container(data=blob_data)
53-
target_container_sas_url = self.create_target_container()
45+
source_container_sas_url = self.create_source_container(data=blob_data, variables=variables, container_suffix=str(i)+container_suffix)
46+
target_container_sas_url = self.create_target_container(variables=variables, container_suffix=str(i)+container_suffix)
5447

5548
# prepare translation inputs
5649
translation_inputs = [
@@ -67,7 +60,7 @@ async def _begin_multiple_translations_async(self, async_client, operations_coun
6760

6861
# submit multiple operations
6962
poller = await async_client.begin_translation(translation_inputs)
70-
self.assertIsNotNone(poller.id)
63+
assert poller.id is not None
7164
if wait_for_operation:
7265
await poller.result()
7366
else:
@@ -78,13 +71,14 @@ async def _begin_multiple_translations_async(self, async_client, operations_coun
7871

7972
async def _begin_and_validate_translation_with_multiple_docs_async(self, async_client, docs_count, **kwargs):
8073
# get input params
74+
variables = kwargs.pop('variables', {})
8175
wait_for_operation = kwargs.pop('wait', False)
8276
language_code = kwargs.pop('language_code', "es")
8377

8478
# prepare containers and test data
8579
blob_data = Document.create_dummy_docs(docs_count=docs_count)
86-
source_container_sas_url = self.create_source_container(data=blob_data)
87-
target_container_sas_url = self.create_target_container()
80+
source_container_sas_url = self.create_source_container(data=blob_data, variables=variables)
81+
target_container_sas_url = self.create_target_container(variables=variables)
8882

8983
# prepare translation inputs
9084
translation_inputs = [
@@ -98,10 +92,9 @@ async def _begin_and_validate_translation_with_multiple_docs_async(self, async_c
9892
]
9993
)
10094
]
101-
10295
# submit operation
10396
poller = await async_client.begin_translation(translation_inputs)
104-
self.assertIsNotNone(poller.id)
97+
assert poller.id is not None
10598
# wait for result
10699
if wait_for_operation:
107100
result = await poller.result()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# coding: utf-8
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
import os
10+
import sys
11+
import pytest
12+
from devtools_testutils import (
13+
test_proxy,
14+
is_live,
15+
add_remove_header_sanitizer,
16+
add_general_regex_sanitizer,
17+
add_oauth_response_sanitizer
18+
)
19+
from azure.storage.blob import BlobServiceClient
20+
21+
# Ignore async tests for Python < 3.5
22+
collect_ignore_glob = []
23+
if sys.version_info < (3, 5):
24+
collect_ignore_glob.append("*_async.py")
25+
26+
27+
@pytest.fixture(scope="session", autouse=True)
28+
def add_sanitizers(test_proxy):
29+
add_remove_header_sanitizer(headers="Ocp-Apim-Subscription-Key")
30+
add_remove_header_sanitizer(headers="Retry-After")
31+
add_general_regex_sanitizer(
32+
value="fakeendpoint",
33+
regex="(?<=\\/\\/)[a-z-]+(?=\\.cognitiveservices\\.azure\\.com)"
34+
)
35+
add_general_regex_sanitizer(
36+
regex="(?<=\\/\\/)[a-z]+(?=(?:|-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)",
37+
value="fakeendpoint",
38+
)
39+
add_oauth_response_sanitizer()
40+
41+
# run tests
42+
yield
43+
44+
# Dogfood env uses a static storage account so we clean up the blob resources
45+
# This is unnecessary for AzureCloud where each storage account is deleted at the end of testing
46+
if is_live() and os.getenv("TRANSLATION_ENVIRONMENT") == "Dogfood":
47+
client = BlobServiceClient(
48+
"https://" + os.getenv("TRANSLATION_DOCUMENT_STORAGE_NAME") + ".blob.core.windows.net/",
49+
os.getenv("TRANSLATION_DOCUMENT_STORAGE_KEY")
50+
)
51+
for container in client.list_containers():
52+
client.delete_container(container)

sdk/translation/azure-ai-translation-document/tests/preparer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
DocumentTranslationPreparer = functools.partial(
1414
PowerShellPreparer,
1515
'translation',
16-
translation_document_test_endpoint="https://redacted.cognitiveservices.azure.com/",
16+
translation_document_test_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
1717
translation_document_test_api_key="fakeZmFrZV9hY29jdW50X2tleQ==",
1818
translation_document_name="redacted",
1919
translation_document_storage_name="redacted",

0 commit comments

Comments
 (0)