From d4be0f93112af15764886d955f91385be4f10f09 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Fri, 20 Nov 2020 11:55:21 -0800 Subject: [PATCH 1/2] try using existing FR resource --- .../azure-ai-formrecognizer/tests/testcase.py | 82 ++++++++++++------- sdk/formrecognizer/tests.yml | 2 + 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py index 9b7da2648a2e..d4af5bab93e6 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py @@ -12,6 +12,7 @@ import six import pytest import logging +from collections import namedtuple from azure.core.credentials import AzureKeyCredential, AccessToken from azure.ai.formrecognizer._helpers import ( adjust_value_type, @@ -35,6 +36,15 @@ LOGGING_FORMAT = '%(asctime)s %(name)-20s %(levelname)-5s %(message)s' ENABLE_LOGGER = os.getenv('ENABLE_LOGGER', "False") REGION = os.getenv('REGION', 'centraluseuap') +ENDPOINT = os.getenv('ENDPOINT', 'None') +NAME = os.getenv('NAME', 'None') +RESOURCE_GROUP = os.getenv('RESOURCE_GROUP', 'None') + + +ResourceGroup = namedtuple( + 'ResourceGroup', + ['name'] +) class RequestBodyReplacer(RecordingProcessor): @@ -97,6 +107,7 @@ class FormRecognizerTest(AzureTestCase): def __init__(self, method_name): super(FormRecognizerTest, self).__init__(method_name) + self.vcr.match_on = ["path", "method", "query"] self.recording_processors.append(AccessTokenReplacer()) self.recording_processors.append(RequestBodyReplacer()) self.configure_logging() @@ -712,35 +723,46 @@ def create_form_client(self, **kwargs): @pytest.fixture(scope="session") def form_recognizer_account(): - test_case = AzureTestCase("__init__") - rg_preparer = ResourceGroupPreparer(random_name_enabled=True, name_prefix='pycog', location=REGION) - form_recognizer_preparer = CognitiveServicesAccountPreparer( - random_name_enabled=True, - kind="formrecognizer", - name_prefix='pycog', - location=REGION - ) - - try: - rg_name, rg_kwargs = rg_preparer._prepare_create_resource(test_case) - FormRecognizerTest._RESOURCE_GROUP = rg_kwargs['resource_group'] + # temp allow an existing resource to be used instead of creating an FR resource on the fly + if ENDPOINT != "None": + FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT = ENDPOINT + if REGION == "centraluseuap": + FormRecognizerTest._FORM_RECOGNIZER_KEY = os.getenv("FORM_RECOGNIZER_PYTHON_CANARY_API_KEY") + else: + FormRecognizerTest._FORM_RECOGNIZER_KEY = os.getenv("FORM_RECOGNIZER_PYTHON_API_KEY") + FormRecognizerTest._FORM_RECOGNIZER_NAME = NAME + FormRecognizerTest._RESOURCE_GROUP = ResourceGroup(name=RESOURCE_GROUP) + yield + else: + test_case = AzureTestCase("__init__") + rg_preparer = ResourceGroupPreparer(random_name_enabled=True, name_prefix='pycog', location=REGION) + form_recognizer_preparer = CognitiveServicesAccountPreparer( + random_name_enabled=True, + kind="formrecognizer", + name_prefix='pycog', + location=REGION + ) + try: - form_recognizer_name, form_recognizer_kwargs = form_recognizer_preparer._prepare_create_resource( - test_case, **rg_kwargs) - if test_case.is_live: - time.sleep(60) # current ask until race condition bug fixed - FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT = form_recognizer_kwargs['cognitiveservices_account'] - FormRecognizerTest._FORM_RECOGNIZER_KEY = form_recognizer_kwargs['cognitiveservices_account_key'] - FormRecognizerTest._FORM_RECOGNIZER_NAME = form_recognizer_name - yield - finally: - form_recognizer_preparer.remove_resource( - form_recognizer_name, - resource_group=rg_kwargs['resource_group'] - ) - FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT = None - FormRecognizerTest._FORM_RECOGNIZER_KEY = None + rg_name, rg_kwargs = rg_preparer._prepare_create_resource(test_case) + FormRecognizerTest._RESOURCE_GROUP = rg_kwargs['resource_group'] + try: + form_recognizer_name, form_recognizer_kwargs = form_recognizer_preparer._prepare_create_resource( + test_case, **rg_kwargs) + if test_case.is_live: + time.sleep(60) # current ask until race condition bug fixed + FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT = form_recognizer_kwargs['cognitiveservices_account'] + FormRecognizerTest._FORM_RECOGNIZER_KEY = form_recognizer_kwargs['cognitiveservices_account_key'] + FormRecognizerTest._FORM_RECOGNIZER_NAME = form_recognizer_name + yield + finally: + form_recognizer_preparer.remove_resource( + form_recognizer_name, + resource_group=rg_kwargs['resource_group'] + ) + FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT = None + FormRecognizerTest._FORM_RECOGNIZER_KEY = None - finally: - rg_preparer.remove_resource(rg_name) - FormRecognizerTest._RESOURCE_GROUP = None + finally: + rg_preparer.remove_resource(rg_name) + FormRecognizerTest._RESOURCE_GROUP = None diff --git a/sdk/formrecognizer/tests.yml b/sdk/formrecognizer/tests.yml index 7b6a3a59b82e..b9705437c2c6 100644 --- a/sdk/formrecognizer/tests.yml +++ b/sdk/formrecognizer/tests.yml @@ -37,6 +37,8 @@ jobs: OSVmImage: 'ubuntu-18.04' PythonVersion: '3.8' EnvVars: + FORM_RECOGNIZER_PYTHON_CANARY_API_KEY: $(python-formrecognizer-test-canary-api-key) + FORM_RECOGNIZER_PYTHON_API_KEY: $(python-formrecognizer-test-api-key) AZURE_SUBSCRIPTION_ID: $(provisioner-subscription) AZURE_TENANT_ID: $(aad-azure-sdk-test-tenant-id) AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret) From f2ebf54e4ec6194f8cb04a819aa6ef0b7c43ff44 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Fri, 20 Nov 2020 12:04:37 -0800 Subject: [PATCH 2/2] rename --- sdk/formrecognizer/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/formrecognizer/tests.yml b/sdk/formrecognizer/tests.yml index b9705437c2c6..418f48f21969 100644 --- a/sdk/formrecognizer/tests.yml +++ b/sdk/formrecognizer/tests.yml @@ -37,8 +37,8 @@ jobs: OSVmImage: 'ubuntu-18.04' PythonVersion: '3.8' EnvVars: - FORM_RECOGNIZER_PYTHON_CANARY_API_KEY: $(python-formrecognizer-test-canary-api-key) - FORM_RECOGNIZER_PYTHON_API_KEY: $(python-formrecognizer-test-api-key) + AZURE_FORM_RECOGNIZER_PYTHON_CANARY_API_KEY: $(python-formrecognizer-test-canary-api-key) + AZURE_FORM_RECOGNIZER_PYTHON_API_KEY: $(python-formrecognizer-test-api-key) AZURE_SUBSCRIPTION_ID: $(provisioner-subscription) AZURE_TENANT_ID: $(aad-azure-sdk-test-tenant-id) AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret)