Skip to content

Commit 265891c

Browse files
committed
make storage batch tests non-live so CI can catch issues, some test only passes at live test, need to check recording issue
1 parent 9e863de commit 265891c

File tree

7 files changed

+79830
-6722
lines changed

7 files changed

+79830
-6722
lines changed

src/azure-cli/azure/cli/command_modules/storage/operations/blob.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from azure.cli.command_modules.storage.util import (filter_none, collect_blobs, collect_blob_objects,
1414
collect_files_track2, mkdir_p, guess_content_type,
1515
normalize_blob_file_path, check_precondition_success)
16+
from azure.cli.command_modules.storage._client_factory import cf_share_service
1617
from azure.core.exceptions import ResourceExistsError, ResourceModifiedError, HttpResponseError
1718

1819
from knack.log import get_logger
@@ -374,6 +375,8 @@ def storage_blob_copy_batch(cmd, client, source_client, container_name=None, des
374375
if source_container:
375376
# copy blobs for blob container, skip empty dir
376377
# pylint: disable=inconsistent-return-statements
378+
if source_client is None:
379+
source_client = client
377380
def action_blob_copy(blob_name):
378381
if dryrun:
379382
logger.warning(' - copy blob %s', blob_name)
@@ -392,12 +395,24 @@ def action_blob_copy(blob_name):
392395
if source_share:
393396
# copy blob from file share, skip empty dir
394397
# pylint: disable=inconsistent-return-statements
398+
if source_client is None:
399+
t_share_service = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
400+
'_share_service_client#ShareServiceClient')
401+
account_url = client.url.replace('blob', 'file')
402+
if client.credential and client.credential.account_key:
403+
credential = {
404+
"account_name": client.credential.account_name,
405+
"account_key": client.credential.account_key
406+
}
407+
source_client = t_share_service(account_url=account_url, credential=credential)
408+
else:
409+
source_client = t_share_service(account_url=account_url, credential=client.credential)
395410
def action_file_copy(file_info):
396411
dir_name, file_name = file_info
397412
if dryrun:
398-
logger.warning(' - copy file %s', os.path.join(dir_name, file_name))
413+
logger.warning(' - copy file %s', '/'.join(dir_name, file_name))
399414
else:
400-
return _copy_file_to_blob_container(client, source_client, container_name, destination_path,
415+
return _copy_file_to_blob_container(cmd, client, source_client, container_name, destination_path,
401416
source_share, source_sas, dir_name, file_name)
402417

403418
return list(filter_none(action_file_copy(file) for file in collect_files_track2(source_client,
@@ -908,16 +923,20 @@ def create_blob_url(client, container_name, blob_name, snapshot, protocol='https
908923
def _copy_blob_to_blob_container(cmd, blob_service, source_blob_service, destination_container, destination_path,
909924
source_container, source_blob_name, source_sas, **kwargs):
910925
t_blob_client = cmd.get_models('_blob_client#BlobClient')
911-
# generate sas for oauth copy source
912-
if not source_sas:
913-
from ..util import create_short_lived_blob_sas_v2
914-
start = datetime.utcnow()
915-
expiry = datetime.utcnow() + timedelta(hours=1)
916-
source_user_delegation_key = source_blob_service.get_user_delegation_key(start, expiry)
917-
source_sas = create_short_lived_blob_sas_v2(cmd, source_blob_service.account_name, source_container,
918-
source_blob_name, user_delegation_key=source_user_delegation_key)
919-
source_client = t_blob_client(account_url=source_blob_service.url, container_name=source_container,
920-
blob_name=source_blob_name, credential=source_sas)
926+
# if blob_service and source_blob_service are the same
927+
if blob_service == source_blob_service:
928+
source_client = source_blob_service.get_blob_client(container=source_container, blob=source_blob_name)
929+
else:
930+
# generate sas for oauth copy source
931+
if not source_sas:
932+
from ..util import create_short_lived_blob_sas_v2
933+
start = datetime.utcnow()
934+
expiry = datetime.utcnow() + timedelta(days=1)
935+
source_user_delegation_key = source_blob_service.get_user_delegation_key(start, expiry)
936+
source_sas = create_short_lived_blob_sas_v2(cmd, source_blob_service.account_name, source_container,
937+
source_blob_name, user_delegation_key=source_user_delegation_key)
938+
source_client = t_blob_client(account_url=source_blob_service.url, container_name=source_container,
939+
blob_name=source_blob_name, credential=source_sas)
921940
source_blob_url = source_client.url
922941

923942
destination_blob_name = normalize_blob_file_path(destination_path, source_blob_name)
@@ -935,10 +954,23 @@ def _copy_blob_to_blob_container(cmd, blob_service, source_blob_service, destina
935954
raise CLIError(error_template.format(source_blob_name, destination_container, ex))
936955

937956

938-
def _copy_file_to_blob_container(blob_service, source_file_service, destination_container, destination_path,
957+
def _copy_file_to_blob_container(cmd, blob_service, source_file_service, destination_container, destination_path,
939958
source_share, source_sas, source_file_dir, source_file_name):
940959
t_share_client = source_file_service.get_share_client(source_share)
941960
t_file_client = t_share_client.get_file_client(os.path.join(source_file_dir, source_file_name))
961+
if source_sas is None:
962+
t_generate_share_sas = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
963+
'_shared_access_signature#generate_share_sas')
964+
t_file_permissions = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
965+
'_models#FileSasPermissions')
966+
start = datetime.utcnow()
967+
expiry = datetime.utcnow() + timedelta(days=1)
968+
source_sas = t_generate_share_sas(account_name=t_file_client.account_name, share_name=source_share,
969+
account_key=t_file_client.credential.account_key,
970+
permission=t_file_permissions(read=True),
971+
expiry=expiry, start=start)
972+
from urllib.parse import quote
973+
source_sas = quote(source_sas, safe='&%()$=\',~')
942974
if '?' not in t_file_client.url:
943975
source_file_url = '{}?{}'.format(t_file_client.url, source_sas)
944976
else:

src/azure-cli/azure/cli/command_modules/storage/operations/file.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def _upload_action(src, dst2):
184184
dir_name = os.path.dirname(dst2)
185185
file_name = os.path.basename(dst2)
186186

187-
_make_directory_in_files_share(client, destination, dir_name, V2=True)
187+
_make_directory_in_files_share(client, dir_name)
188188

189189
logger.warning('uploading %s', src)
190190
storage_file_upload(client.get_file_client(dst2), src, content_settings, metadata, validate_content,
@@ -298,6 +298,19 @@ def storage_file_copy_batch(cmd, client, source_client, share_name=None, destina
298298
# repeatedly create existing directory so as to optimize the performance.
299299
existing_dirs = set()
300300

301+
if source_client is None:
302+
t_blob_service_client = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_BLOB,
303+
'_blob_service_client#BlobServiceClient')
304+
account_url = '/'.join(client.url.replace('file', 'blob').split('/')[:-1])
305+
if client.credential and client.credential.account_key:
306+
credential = {
307+
"account_name": client.credential.account_name,
308+
"account_key": client.credential.account_key
309+
}
310+
source_client = t_blob_service_client(account_url=account_url, credential=credential)
311+
else:
312+
source_client = t_blob_service_client(account_url=account_url, credential=client.credential)
313+
301314
# pylint: disable=inconsistent-return-statements
302315
def action_blob_copy(blob_name):
303316
if dryrun:
@@ -318,6 +331,12 @@ def action_blob_copy(blob_name):
318331
# repeatedly create existing directory so as to optimize the performance.
319332
existing_dirs = set()
320333

334+
if source_client is None:
335+
t_share_service = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
336+
'_share_service_client#ShareServiceClient')
337+
account_url = '/'.join(client.url.split('/')[:-1])
338+
source_client = t_share_service(account_url=account_url, credential=client.credential)
339+
321340
# pylint: disable=inconsistent-return-statements
322341
def action_file_copy(file_info):
323342
dir_name, file_name = file_info
@@ -371,13 +390,29 @@ def _create_file_and_directory_from_blob(cmd, file_service, blob_service, share,
371390
from azure.cli.command_modules.storage.util import normalize_blob_file_path
372391

373392
t_blob_client = cmd.get_models('_blob_client#BlobClient', resource_type=ResourceType.DATA_STORAGE_BLOB)
393+
if sas is None:
394+
t_generate_blob_sas = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_BLOB,
395+
'_shared_access_signature#generate_blob_sas')
396+
t_blob_permissions = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_BLOB,
397+
'_models#BlobSasPermissions')
398+
from datetime import datetime, timedelta
399+
start = datetime.utcnow()
400+
expiry = datetime.utcnow() + timedelta(days=1)
401+
source_sas = t_generate_blob_sas(account_name=blob_service.account_name, container_name=container,
402+
blob_name=blob_name,
403+
account_key=blob_service.credential.account_key,
404+
permission=t_blob_permissions(read=True),
405+
expiry=expiry, start=start)
406+
from urllib.parse import quote
407+
sas = quote(source_sas, safe='&%()$=\',~')
408+
374409
source_client = t_blob_client(account_url=blob_service.url, container_name=container,
375410
blob_name=blob_name, credential=sas)
376411
blob_url = source_client.url
377412

378413
full_path = normalize_blob_file_path(destination_dir, blob_name)
379414
dir_name = os.path.dirname(full_path)
380-
_make_directory_in_files_share(file_service, share, dir_name, existing_dirs, V2=True)
415+
_make_directory_in_files_share(file_service, dir_name, existing_dirs)
381416

382417
try:
383418
file_client = file_service.get_file_client(full_path)
@@ -403,14 +438,29 @@ def _create_file_and_directory_from_file(cmd, file_service, source_file_service,
403438
if source_file_dir:
404439
file_path = source_file_dir + '/' + file_path
405440
t_file_client = cmd.get_models('_file_client#ShareFileClient', resource_type=ResourceType.DATA_STORAGE_FILESHARE)
441+
if sas is None:
442+
t_generate_share_sas = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
443+
'_shared_access_signature#generate_share_sas')
444+
t_file_permissions = get_sdk(cmd.cli_ctx, ResourceType.DATA_STORAGE_FILESHARE,
445+
'_models#FileSasPermissions')
446+
from datetime import datetime, timedelta
447+
start = datetime.utcnow()
448+
expiry = datetime.utcnow() + timedelta(days=1)
449+
source_sas = t_generate_share_sas(account_name=source_file_service.account_name, share_name=source_share,
450+
account_key=source_file_service.credential.account_key,
451+
permission=t_file_permissions(read=True),
452+
expiry=expiry, start=start)
453+
from urllib.parse import quote
454+
sas = quote(source_sas, safe='&%()$=\',~')
455+
406456
source_client = t_file_client(account_url=source_file_service.url, share_name=source_share, file_path=file_path,
407457
credential=sas)
408458
file_url = source_client.url
409459

410460
full_path = normalize_blob_file_path(destination_dir, os.path.join(source_file_dir, source_file_name))
411461
file_name = os.path.basename(full_path)
412462
dir_name = os.path.dirname(full_path)
413-
_make_directory_in_files_share(file_service, share, dir_name, existing_dirs, V2=True)
463+
_make_directory_in_files_share(file_service, dir_name, existing_dirs)
414464

415465
try:
416466
file_client = file_service.get_file_client(full_path)
@@ -423,7 +473,7 @@ def _create_file_and_directory_from_file(cmd, file_service, source_file_service,
423473
raise CLIError(error_template.format(file_name, source_share, share))
424474

425475

426-
def _make_directory_in_files_share(file_service, file_share, directory_path, existing_dirs=None, V2=False):
476+
def _make_directory_in_files_share(file_service, directory_path, existing_dirs=None):
427477
"""
428478
Create directories recursively.
429479
This method accept a existing_dirs set which serves as the cache of existing directory. If the
@@ -446,10 +496,7 @@ def _make_directory_in_files_share(file_service, file_share, directory_path, exi
446496
continue
447497

448498
try:
449-
if V2:
450-
file_service.create_directory(directory_name=dir_name)
451-
else:
452-
file_service.create_directory(share_name=file_share, directory_name=dir_name, fail_on_exist=False)
499+
file_service.create_directory(directory_name=dir_name)
453500
except ResourceExistsError:
454501
pass
455502
except AzureHttpError:

0 commit comments

Comments
 (0)