1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
import pytest
import platform
from azure.data.tables.aio import TableServiceClient, TableClient
from azure.data.tables._version import VERSION
from devtools_testutils import (
ResourceGroupPreparer,
CachedResourceGroupPreparer,
CachedStorageAccountPreparer,
AzureTestCase
)
from _shared.testcase import TableTestCase
from azure.core.exceptions import HttpResponseError
# ------------------------------------------------------------------------------
SERVICES = {
TableServiceClient: 'table',
TableClient: 'table',
}
_CONNECTION_ENDPOINTS = {'table': 'TableEndpoint'}
_CONNECTION_ENDPOINTS_SECONDARY = {'table': 'TableSecondaryEndpoint'}
class StorageTableClientTest(TableTestCase):
def setUp(self):
super(StorageTableClientTest, self).setUp()
self.sas_token = self.generate_sas_token()
self.token_credential = self.generate_oauth_token()
# --Helpers-----------------------------------------------------------------
def validate_standard_account_endpoints(self, service, account_name, account_key):
assert service is not None
assert service.account_name == account_name
assert service.credential.account_name == account_name
assert service.credential.account_key == account_key
assert ('{}.{}'.format(account_name, 'table.core.windows.net') in service.url) or ('{}.{}'.format(account_name, 'table.cosmos.azure.com') in service.url)
# --Direct Parameters Test Cases --------------------------------------------
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_key_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account, url), credential=storage_account_key, table_name='foo')
# Assert
self.validate_standard_account_endpoints(service, storage_account.name, storage_account_key)
assert service.scheme == 'https'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_async(self, resource_group, location, storage_account, storage_account_key):
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(
self.connection_string(storage_account, storage_account_key), table_name="test")
# Assert
self.validate_standard_account_endpoints(service, storage_account.name, storage_account_key)
assert service.scheme == 'https'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_sas_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
url = self.account_url(storage_account, "table")
suffix = '.table.core.windows.net'
for service_type in SERVICES:
# Act
service = service_type(
self.account_url(storage_account, "table"), credential=self.sas_token, table_name='foo')
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.url.startswith('https://' + storage_account.name + suffix)
assert service.url.endswith(self.sas_token)
assert service.credential is None
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_token_async(self, resource_group, location, storage_account, storage_account_key):
url = self.account_url(storage_account, "table")
suffix = '.table.core.windows.net'
for service_type in SERVICES:
# Act
service = service_type(url, credential=self.token_credential, table_name='foo')
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.url.startswith('https://' + storage_account.name + suffix)
assert service.credential == self.token_credential
assert not hasattr(service.credential, 'account_key')
assert hasattr(service.credential, 'get_token')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_token_and_http_async(self, resource_group, location, storage_account, storage_account_key):
for service_type in SERVICES:
# Act
with pytest.raises(ValueError):
url = self.account_url(storage_account, "table").replace('https', 'http')
service_type(url, credential=self.token_credential, table_name='foo')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_china_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
# TODO: Confirm regional cloud cosmos URLs
for service_type in SERVICES.items():
# Act
url = self.account_url(storage_account, "table").replace('core.windows.net', 'core.chinacloudapi.cn')
service = service_type[0](
url, credential=storage_account_key, table_name='foo')
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://{}.{}.core.chinacloudapi.cn'.format(storage_account.name, "table"))
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_protocol_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
# Act
url = self.account_url(storage_account, "table").replace('https', 'http')
service = service_type[0](
url, credential=storage_account_key, table_name='foo')
# Assert
self.validate_standard_account_endpoints(service, storage_account.name, storage_account_key)
assert service.scheme == 'http'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_empty_key_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
TABLE_SERVICES = [TableServiceClient, TableClient]
for service_type in TABLE_SERVICES:
# Act
with pytest.raises(ValueError) as e:
test_service = service_type('testaccount', credential='', table_name='foo')
assert str(e.value) == "You need to provide either a SAS token or an account shared key to authenticate."
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_socket_timeout_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
# Act
default_service = service_type[0](
self.account_url(storage_account, "table"), credential=storage_account_key, table_name='foo')
service = service_type[0](
self.account_url(storage_account, "table"), credential=storage_account_key,
table_name='foo', connection_timeout=22)
# Assert
self.validate_standard_account_endpoints(service, storage_account.name, storage_account_key)
assert service._client._client._pipeline._transport.connection_config.timeout == 22
assert default_service._client._client._pipeline._transport.connection_config.timeout in [20, (20, 2000)]
# --Connection String Test Cases --------------------------------------------
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_key_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
conn_string = 'AccountName={};AccountKey={};'.format(storage_account.name, storage_account_key)
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(conn_string, table_name='foo')
# Assert
self.validate_standard_account_endpoints(service, storage_account.name, storage_account_key)
assert service.scheme == 'https'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_sas_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
conn_string = 'AccountName={};SharedAccessSignature={};'.format(storage_account.name, self.sas_token)
for service_type in SERVICES:
# Act
service = service_type.from_connection_string(conn_string, table_name='foo')
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.url.startswith('https://' + storage_account.name + '.table.core.windows.net')
assert service.url.endswith(self.sas_token)
assert service.credential is None
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_cosmos_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
conn_string = 'DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};TableEndpoint=https://{0}.table.cosmos.azure.com:443/;'.format(
storage_account.name, storage_account_key)
for service_type in SERVICES:
# Act
service = service_type.from_connection_string(conn_string, table_name='foo')
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.url.startswith('https://' + storage_account.name + '.table.cosmos.azure.com')
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://' + storage_account.name + '.table.cosmos.azure.com')
assert service.scheme == 'https'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_endpoint_protocol_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
conn_string = 'AccountName={};AccountKey={};DefaultEndpointsProtocol=http;EndpointSuffix=core.chinacloudapi.cn;'.format(
storage_account.name, storage_account_key)
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(conn_string, table_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('http://{}.{}.core.chinacloudapi.cn'.format(storage_account.name, "table"))
assert service.scheme == 'http'
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_emulated_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
conn_string = 'UseDevelopmentStorage=true;'.format(storage_account.name, storage_account_key)
# Act
with pytest.raises(ValueError):
service = service_type[0].from_connection_string(conn_string, table_name="foo")
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_connection_string_custom_domain_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
conn_string = 'AccountName={};AccountKey={};TableEndpoint=www.mydomain.com;'.format(
storage_account.name, storage_account_key)
# Act
service = service_type[0].from_connection_string(conn_string, table_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://www.mydomain.com')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_conn_str_custom_domain_trailing_slash_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
conn_string = 'AccountName={};AccountKey={};TableEndpoint=www.mydomain.com/;'.format(
storage_account.name, storage_account_key)
# Act
service = service_type[0].from_connection_string(conn_string, table_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://www.mydomain.com')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_conn_str_custom_domain_sec_override_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for service_type in SERVICES.items():
conn_string = 'AccountName={};AccountKey={};TableEndpoint=www.mydomain.com/;'.format(
storage_account.name, storage_account_key)
# Act
service = service_type[0].from_connection_string(
conn_string, secondary_hostname="www-sec.mydomain.com", table_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://www.mydomain.com')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_conn_str_fails_if_sec_without_primary_async(self, resource_group, location, storage_account, storage_account_key):
for service_type in SERVICES.items():
# Arrange
conn_string = 'AccountName={};AccountKey={};{}=www.mydomain.com;'.format(
storage_account.name, storage_account_key,
_CONNECTION_ENDPOINTS_SECONDARY.get(service_type[1]))
# Fails if primary excluded
with pytest.raises(ValueError):
service = service_type[0].from_connection_string(conn_string, table_name="foo")
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_conn_str_succeeds_if_sec_with_primary_async(self, resource_group, location, storage_account, storage_account_key):
for service_type in SERVICES.items():
# Arrange
conn_string = 'AccountName={};AccountKey={};{}=www.mydomain.com;{}=www-sec.mydomain.com;'.format(
storage_account.name,
storage_account_key,
_CONNECTION_ENDPOINTS.get(service_type[1]),
_CONNECTION_ENDPOINTS_SECONDARY.get(service_type[1]))
# Act
service = service_type[0].from_connection_string(conn_string, table_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_endpoint.startswith('https://www.mydomain.com')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_service_with_custom_account_endpoint_path_async(self, resource_group, location, storage_account, storage_account_key):
custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token
for service_type in SERVICES.items():
conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format(
storage_account.name, storage_account_key, custom_account_url)
# Act
service = service_type[0].from_connection_string(conn_string, table_name="foo")
# Assert
assert service.account_name == storage_account.name
assert service.credential.account_name == storage_account.name
assert service.credential.account_key == storage_account_key
assert service._primary_hostname == 'local-machine:11002/custom/account/path'
service = TableServiceClient(account_url=custom_account_url)
assert service.account_name == None
assert service.credential == None
assert service._primary_hostname == 'local-machine:11002/custom/account/path'
assert service.url.startswith('http://local-machine:11002/custom/account/path')
service = TableClient(account_url=custom_account_url, table_name="foo")
assert service.account_name == None
assert service.table_name == "foo"
assert service.credential == None
assert service._primary_hostname == 'local-machine:11002/custom/account/path'
assert service.url.startswith('http://local-machine:11002/custom/account/path')
service = TableClient.from_table_url("http://local-machine:11002/custom/account/path/foo" + self.sas_token)
assert service.account_name == None
assert service.table_name == "foo"
assert service.credential == None
assert service._primary_hostname == 'local-machine:11002/custom/account/path'
assert service.url.startswith('http://local-machine:11002/custom/account/path')
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_user_agent_default_async(self, resource_group, location, storage_account, storage_account_key):
service = TableServiceClient(self.account_url(storage_account, "table"), credential=storage_account_key)
def callback(response):
assert 'User-Agent' in response.http_request.headers
assert response.http_request.headers['User-Agent'] in "azsdk-python-data-tables/{} Python/{} ({})".format(
VERSION,
platform.python_version(),
platform.platform())
tables = service.list_tables(raw_response_hook=callback)
assert tables is not None
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_user_agent_custom_async(self, resource_group, location, storage_account, storage_account_key):
custom_app = "TestApp/v1.0"
service = TableServiceClient(
self.account_url(storage_account, "table"), credential=storage_account_key, user_agent=custom_app)
def callback(response):
assert 'User-Agent' in response.http_request.headers
assert "TestApp/v1.0 azsdk-python-data-tables/{} Python/{} ({})".format(
VERSION,
platform.python_version(),
platform.platform()) in response.http_request.headers['User-Agent']
tables = service.list_tables(raw_response_hook=callback)
assert tables is not None
def callback(response):
assert 'User-Agent' in response.http_request.headers
assert "TestApp/v2.0 TestApp/v1.0 azsdk-python-data-tables/{} Python/{} ({})".format(
VERSION,
platform.python_version(),
platform.platform()) in response.http_request.headers['User-Agent']
tables = service.list_tables(raw_response_hook=callback, user_agent="TestApp/v2.0")
assert tables is not None
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_user_agent_append(self, resource_group, location, storage_account, storage_account_key):
# TODO: fix this one
service = TableServiceClient(self.account_url(storage_account, "table"), credential=storage_account_key)
def callback(response):
assert 'User-Agent' in response.http_request.headers
assert response.http_request.headers['User-Agent'] == "azsdk-python-data-tables/{} Python/{} ({}) customer_user_agent".format(
VERSION,
platform.python_version(),
platform.platform())
custom_headers = {'User-Agent': 'customer_user_agent'}
tables = service.list_tables(raw_response_hook=callback, headers=custom_headers)
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_table_client_with_complete_table_url_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
table_url = self.account_url(storage_account, "table") + "/foo"
service = TableClient(table_url, table_name='bar', credential=storage_account_key)
# Assert
assert service.scheme == 'https'
assert service.table_name == 'bar'
assert service.account_name == storage_account.name
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_create_table_client_with_complete_url_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
table_url = "https://{}.table.core.windows.net:443/foo".format(storage_account.name)
service = TableClient(account_url=table_url, table_name='bar', credential=storage_account_key)
# Assert
assert service.scheme == 'https'
assert service.table_name == 'bar'
assert service.account_name == storage_account.name
@AzureTestCase.await_prepared_test
async def test_create_table_client_with_invalid_name_async(self):
# Arrange
table_url = "https://{}.table.core.windows.net:443/foo".format("storage_account_name")
invalid_table_name = "my_table"
# Assert
with pytest.raises(ValueError) as excinfo:
service = TableClient(account_url=table_url, table_name=invalid_table_name, credential="storage_account_key")
assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long."in str(excinfo)
@AzureTestCase.await_prepared_test
async def test_error_with_malformed_conn_str_async(self):
# Arrange
for conn_str in ["", "foobar", "foobar=baz=foo", "foo;bar;baz", "foo=;bar=;", "=", ";", "=;=="]:
for service_type in SERVICES.items():
# Act
with pytest.raises(ValueError) as e:
service = service_type[0].from_connection_string(conn_str, table_name="test")
if conn_str in("", "foobar", "foo;bar;baz", ";"):
assert str(e.value) == "Connection string is either blank or malformed."
elif conn_str in ("foobar=baz=foo" , "foo=;bar=;", "=", "=;=="):
assert str(e.value) == "Connection string missing required connection details."
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_closing_pipeline_client_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account, "table"), credential=storage_account_key, table_name='table')
# Assert
async with service:
assert hasattr(service, 'close')
await service.close()
@CachedResourceGroupPreparer(name_prefix="tablestest")
@CachedStorageAccountPreparer(name_prefix="tablestest")
async def test_closing_pipeline_client_simple_async(self, resource_group, location, storage_account, storage_account_key):
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account, "table"), credential=storage_account_key, table_name='table')
await service.close()
# ------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|