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

[formrecognizer] try using existing resource in live tests #15483

Merged
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
82 changes: 52 additions & 30 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions sdk/formrecognizer/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
OSVmImage: 'ubuntu-18.04'
PythonVersion: '3.8'
EnvVars:
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)
Expand Down