Skip to content

Commit 4762e58

Browse files
Split create_basic_client into two methods (Azure#14673)
* Update azure_testcase.py * updated test_table(_async) to verify changes to azure_testcase work now, added a positional arg for create_client_from_credential for services that might have required positional args * removed the positional *args in favor of pushing towards kwargs * added private _get_real_credential with kwarg for is_async * added the async fake credential that was copied from schema registry, and removed extra code from schema registry to verify works * breaking the fake async credential into its own file and importing it when in Python 3 * fixing up comments based on laurent/adams review * removing the second boolean for is_real, using self.is_live instead which does the same thing * updating credential to be self.xxx_cred, caught in the tables live tests * fixed a create_credential in test table async that did not use kwargs correctly * Update azure_testcase.py * updated test_table(_async) to verify changes to azure_testcase work now, added a positional arg for create_client_from_credential for services that might have required positional args * removed the positional *args in favor of pushing towards kwargs * added private _get_real_credential with kwarg for is_async * added the async fake credential that was copied from schema registry, and removed extra code from schema registry to verify works * breaking the fake async credential into its own file and importing it when in Python 3 * fixing up comments based on laurent/adams review * removing the second boolean for is_real, using self.is_live instead which does the same thing * updating credential to be self.xxx_cred, caught in the tables live tests * fixed a create_credential in test table async that did not use kwargs correctly * changing a word to force check-enforcer to re-evaulate CI * Clarify async protocol * adding a logging message to warn about create_basic_client and use get_cred/create from cred * fixing up the syntax of the logger Co-authored-by: seankane-msft <seankane@microsoft.com>
1 parent b9480a5 commit 4762e58

File tree

6 files changed

+191
-154
lines changed

6 files changed

+191
-154
lines changed

sdk/schemaregistry/azure-schemaregistry/tests/async_tests/test_schema_registry_async.py

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,75 +34,13 @@
3434

3535
class SchemaRegistryAsyncTests(AzureTestCase):
3636

37-
class AsyncFakeCredential(object):
38-
async def get_token(self, *scopes, **kwargs):
39-
return AccessToken('fake_token', 2527537086)
40-
41-
async def close(self):
42-
pass
43-
44-
def create_basic_client(self, client_class, **kwargs):
45-
# This is the patch for creating client using aio identity
46-
47-
tenant_id = os.environ.get("AZURE_TENANT_ID", None)
48-
client_id = os.environ.get("AZURE_CLIENT_ID", None)
49-
secret = os.environ.get("AZURE_CLIENT_SECRET", None)
50-
51-
if tenant_id and client_id and secret and self.is_live:
52-
if _is_autorest_v3(client_class):
53-
# Create azure-identity class
54-
from azure.identity.aio import ClientSecretCredential
55-
credentials = ClientSecretCredential(
56-
tenant_id=tenant_id,
57-
client_id=client_id,
58-
client_secret=secret
59-
)
60-
else:
61-
# Create msrestazure class
62-
from msrestazure.azure_active_directory import ServicePrincipalCredentials
63-
credentials = ServicePrincipalCredentials(
64-
tenant=tenant_id,
65-
client_id=client_id,
66-
secret=secret
67-
)
68-
else:
69-
if _is_autorest_v3(client_class):
70-
credentials = self.AsyncFakeCredential()
71-
#credentials = self.settings.get_azure_core_credentials()
72-
else:
73-
credentials = self.settings.get_credentials()
74-
75-
# Real client creation
76-
# FIXME decide what is the final argument for that
77-
# if self.is_playback():
78-
# kwargs.setdefault("polling_interval", 0)
79-
if _is_autorest_v3(client_class):
80-
kwargs.setdefault("logging_enable", True)
81-
client = client_class(
82-
credential=credentials,
83-
**kwargs
84-
)
85-
else:
86-
client = client_class(
87-
credentials=credentials,
88-
**kwargs
89-
)
90-
91-
if self.is_playback():
92-
try:
93-
client._config.polling_interval = 0 # FIXME in azure-mgmt-core, make this a kwargs
94-
except AttributeError:
95-
pass
96-
97-
if hasattr(client, "config"): # Autorest v2
98-
if self.is_playback():
99-
client.config.long_running_operation_timeout = 0
100-
client.config.enable_http_logger = True
101-
return client
37+
def create_client(self, endpoint):
38+
credential = self.get_credential(SchemaRegistryClient, is_async=True)
39+
return self.create_client_from_credential(SchemaRegistryClient, credential, endpoint=endpoint, is_async=True)
10240

10341
@SchemaRegistryPreparer()
10442
async def test_schema_basic_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
105-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
43+
client = self.create_client(schemaregistry_endpoint)
10644
async with client:
10745
schema_name = self.get_resource_name('test-schema-basic-async')
10846
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
@@ -135,7 +73,7 @@ async def test_schema_basic_async(self, schemaregistry_endpoint, schemaregistry_
13573

13674
@SchemaRegistryPreparer()
13775
async def test_schema_update_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
138-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
76+
client = self.create_client(schemaregistry_endpoint)
13977
async with client:
14078
schema_name = self.get_resource_name('test-schema-update-async')
14179
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
@@ -170,7 +108,7 @@ async def test_schema_update_async(self, schemaregistry_endpoint, schemaregistry
170108

171109
@SchemaRegistryPreparer()
172110
async def test_schema_same_twice_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
173-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
111+
client = self.create_client(schemaregistry_endpoint)
174112
schema_name = self.get_resource_name('test-schema-twice-async')
175113
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"age","type":["int","null"]},{"name":"city","type":["string","null"]}]}"""
176114
serialization_type = "Avro"
@@ -193,7 +131,7 @@ async def test_schema_negative_wrong_credential_async(self, schemaregistry_endpo
193131

194132
@SchemaRegistryPreparer()
195133
async def test_schema_negative_wrong_endpoint_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
196-
client = self.create_basic_client(SchemaRegistryClient, endpoint="nonexist.servicebus.windows.net")
134+
client = self.create_client("nonexist.servicebus.windows.net")
197135
async with client:
198136
schema_name = self.get_resource_name('test-schema-nonexist-async')
199137
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
@@ -204,7 +142,7 @@ async def test_schema_negative_wrong_endpoint_async(self, schemaregistry_endpoin
204142

205143
@SchemaRegistryPreparer()
206144
async def test_schema_negative_no_schema_async(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
207-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
145+
client = self.create_client(schemaregistry_endpoint)
208146
async with client:
209147
with pytest.raises(HttpResponseError):
210148
await client.get_schema('a')

sdk/schemaregistry/azure-schemaregistry/tests/test_schema_registry.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030

3131
class SchemaRegistryTests(AzureTestCase):
3232

33+
def create_client(self, endpoint):
34+
credential = self.get_credential(SchemaRegistryClient)
35+
return self.create_client_from_credential(SchemaRegistryClient, credential, endpoint=endpoint)
36+
3337
@SchemaRegistryPreparer()
3438
def test_schema_basic(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
35-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
39+
client = self.create_client(schemaregistry_endpoint)
3640
schema_name = self.get_resource_name('test-schema-basic')
3741
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
3842
serialization_type = "Avro"
@@ -63,7 +67,7 @@ def test_schema_basic(self, schemaregistry_endpoint, schemaregistry_group, **kwa
6367

6468
@SchemaRegistryPreparer()
6569
def test_schema_update(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
66-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
70+
client = self.create_client(schemaregistry_endpoint)
6771
schema_name = self.get_resource_name('test-schema-update')
6872
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
6973
serialization_type = "Avro"
@@ -96,7 +100,7 @@ def test_schema_update(self, schemaregistry_endpoint, schemaregistry_group, **kw
96100

97101
@SchemaRegistryPreparer()
98102
def test_schema_same_twice(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
99-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
103+
client = self.create_client(schemaregistry_endpoint)
100104
schema_name = self.get_resource_name('test-schema-twice')
101105
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"age","type":["int","null"]},{"name":"city","type":["string","null"]}]}"""
102106
serialization_type = "Avro"
@@ -116,7 +120,7 @@ def test_schema_negative_wrong_credential(self, schemaregistry_endpoint, schemar
116120

117121
@SchemaRegistryPreparer()
118122
def test_schema_negative_wrong_endpoint(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
119-
client = self.create_basic_client(SchemaRegistryClient, endpoint="nonexist.servicebus.windows.net")
123+
client = self.create_client("nonexist.servicebus.windows.net")
120124
schema_name = self.get_resource_name('test-schema-nonexist')
121125
schema_str = """{"namespace":"example.avro","type":"record","name":"User","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}"""
122126
serialization_type = "Avro"
@@ -125,7 +129,7 @@ def test_schema_negative_wrong_endpoint(self, schemaregistry_endpoint, schemareg
125129

126130
@SchemaRegistryPreparer()
127131
def test_schema_negative_no_schema(self, schemaregistry_endpoint, schemaregistry_group, **kwargs):
128-
client = self.create_basic_client(SchemaRegistryClient, endpoint=schemaregistry_endpoint)
132+
client = self.create_client(schemaregistry_endpoint)
129133
with pytest.raises(HttpResponseError):
130134
client.get_schema('a')
131135

0 commit comments

Comments
 (0)