22// Licensed under the MIT License.
33
44using System ;
5+ using System . Diagnostics ;
56using System . IO ;
67using System . Threading . Tasks ;
78using Azure . Storage . Blobs ;
89using Azure . Storage . Blobs . Specialized ;
10+ using Azure . Storage . Sas ;
911using Azure . Storage . Test ;
1012using Azure . Storage . Test . Shared ;
1113using NUnit . Framework ;
@@ -21,68 +23,89 @@ protected override async ValueTask<bool> IsEnvironmentReadyAsync()
2123
2224 private async Task < bool > DoesOAuthWorkAsync ( )
2325 {
24- TestContext . Error . WriteLine ( "Datalake Probing OAuth" ) ;
26+ TestContext . Error . WriteLine ( $ "Datalake Probing OAuth { Process . GetCurrentProcess ( ) . Id } ") ;
2527
2628 try
2729 {
28- // Check flat account. For some reason we observe failures if that one doesn't work before we start datalake run.
30+ for ( int i = 0 ; i < 10 ; i ++ )
2931 {
30- BlobServiceClient serviceClient = new BlobServiceClient (
31- new Uri ( TestConfigurations . DefaultTargetOAuthTenant . BlobServiceEndpoint ) ,
32- GetOAuthCredential ( TestConfigurations . DefaultTargetOAuthTenant ) ) ;
33- await serviceClient . GetPropertiesAsync ( ) ;
34- var containerName = Guid . NewGuid ( ) . ToString ( ) ;
35- var containerClient = serviceClient . GetBlobContainerClient ( containerName ) ;
36- await containerClient . CreateIfNotExistsAsync ( ) ;
37- try
32+ // Check flat account. For some reason we observe failures if that one doesn't work before we start datalake run.
3833 {
39- await containerClient . GetPropertiesAsync ( ) ;
40- var blobName = Guid . NewGuid ( ) . ToString ( ) ;
41- var blobClient = containerClient . GetAppendBlobClient ( blobName ) ;
42- await blobClient . CreateIfNotExistsAsync ( ) ;
43- await blobClient . GetPropertiesAsync ( ) ;
44- }
45- finally
46- {
47- await containerClient . DeleteIfExistsAsync ( ) ;
48- }
49- }
34+ BlobServiceClient serviceClient = new BlobServiceClient (
35+ new Uri ( TestConfigurations . DefaultTargetOAuthTenant . BlobServiceEndpoint ) ,
36+ GetOAuthCredential ( TestConfigurations . DefaultTargetOAuthTenant ) ) ;
37+ await serviceClient . GetPropertiesAsync ( ) ;
38+ var containerName = Guid . NewGuid ( ) . ToString ( ) ;
39+ var containerClient = serviceClient . GetBlobContainerClient ( containerName ) ;
40+ await containerClient . CreateIfNotExistsAsync ( ) ;
41+ try
42+ {
43+ await containerClient . GetPropertiesAsync ( ) ;
44+ var blobName = Guid . NewGuid ( ) . ToString ( ) ;
45+ var blobClient = containerClient . GetAppendBlobClient ( blobName ) ;
46+ await blobClient . CreateIfNotExistsAsync ( ) ;
47+ await blobClient . GetPropertiesAsync ( ) ;
5048
51- // Check hierarchical account.
52- {
53- DataLakeServiceClient serviceClient = new DataLakeServiceClient (
54- new Uri ( TestConfigurations . DefaultTargetHierarchicalNamespaceTenant . BlobServiceEndpoint ) ,
55- GetOAuthCredential ( TestConfigurations . DefaultTargetHierarchicalNamespaceTenant ) ) ;
56- await serviceClient . GetPropertiesAsync ( ) ;
57- var fileSystemName = Guid . NewGuid ( ) . ToString ( ) ;
58- var fileSystemClient = serviceClient . GetFileSystemClient ( fileSystemName ) ;
59- await fileSystemClient . CreateIfNotExistsAsync ( ) ;
60- try
61- {
62- var directoryName = Guid . NewGuid ( ) . ToString ( ) ;
63- var directoryClient = fileSystemClient . GetDirectoryClient ( directoryName ) ;
64- await directoryClient . CreateIfNotExistsAsync ( ) ;
65- await directoryClient . GetPropertiesAsync ( ) ;
66- var fileName = Guid . NewGuid ( ) . ToString ( ) ;
67- var fileClient = directoryClient . GetFileClient ( fileName ) ;
68- await fileClient . CreateIfNotExistsAsync ( ) ;
69- await fileClient . GetPropertiesAsync ( ) ;
70- // call some APIs that talk to DFS endoint as well.
71- await fileClient . AppendAsync ( new MemoryStream ( new byte [ ] { 1 } ) , 0 ) ;
72- await fileClient . GetAccessControlAsync ( ) ;
49+ var userDelegationKey = await serviceClient . GetUserDelegationKeyAsync ( startsOn : null , expiresOn : DateTimeOffset . UtcNow . AddHours ( 1 ) ) ;
50+ var sasBuilder = new BlobSasBuilder ( BlobSasPermissions . All , DateTimeOffset . UtcNow . AddHours ( 1 ) )
51+ {
52+ BlobContainerName = containerName ,
53+ BlobName = blobName ,
54+ } ;
55+ var sas = sasBuilder . ToSasQueryParameters ( userDelegationKey . Value , serviceClient . AccountName ) . ToString ( ) ;
56+ await new BlobBaseClient ( blobClient . Uri , new AzureSasCredential ( sas ) ) . GetPropertiesAsync ( ) ;
57+ }
58+ finally
59+ {
60+ await containerClient . DeleteIfExistsAsync ( ) ;
61+ }
7362 }
74- finally
63+
64+ // Check hierarchical account.
7565 {
76- await fileSystemClient . DeleteIfExistsAsync ( ) ;
66+ DataLakeServiceClient serviceClient = new DataLakeServiceClient (
67+ new Uri ( TestConfigurations . DefaultTargetHierarchicalNamespaceTenant . BlobServiceEndpoint ) ,
68+ GetOAuthCredential ( TestConfigurations . DefaultTargetHierarchicalNamespaceTenant ) ) ;
69+ await serviceClient . GetPropertiesAsync ( ) ;
70+ var fileSystemName = Guid . NewGuid ( ) . ToString ( ) ;
71+ var fileSystemClient = serviceClient . GetFileSystemClient ( fileSystemName ) ;
72+ await fileSystemClient . CreateIfNotExistsAsync ( ) ;
73+ try
74+ {
75+ var directoryName = Guid . NewGuid ( ) . ToString ( ) ;
76+ var directoryClient = fileSystemClient . GetDirectoryClient ( directoryName ) ;
77+ await directoryClient . CreateIfNotExistsAsync ( ) ;
78+ await directoryClient . GetPropertiesAsync ( ) ;
79+ var fileName = Guid . NewGuid ( ) . ToString ( ) ;
80+ var fileClient = directoryClient . GetFileClient ( fileName ) ;
81+ await fileClient . CreateIfNotExistsAsync ( ) ;
82+ await fileClient . GetPropertiesAsync ( ) ;
83+ // call some APIs that talk to DFS endoint as well.
84+ await fileClient . AppendAsync ( new MemoryStream ( new byte [ ] { 1 } ) , 0 ) ;
85+ await fileClient . GetAccessControlAsync ( ) ;
86+
87+ var userDelegationKey = await serviceClient . GetUserDelegationKeyAsync ( startsOn : null , expiresOn : DateTimeOffset . UtcNow . AddHours ( 1 ) ) ;
88+ var sasBuilder = new DataLakeSasBuilder ( DataLakeSasPermissions . All , DateTimeOffset . UtcNow . AddHours ( 1 ) )
89+ {
90+ FileSystemName = fileSystemName ,
91+ Path = fileClient . Path ,
92+ } ;
93+ var sas = sasBuilder . ToSasQueryParameters ( userDelegationKey . Value , serviceClient . AccountName ) . ToString ( ) ;
94+ await new DataLakeFileClient ( fileClient . Uri , new AzureSasCredential ( sas ) ) . GetPropertiesAsync ( ) ;
95+ }
96+ finally
97+ {
98+ await fileSystemClient . DeleteIfExistsAsync ( ) ;
99+ }
77100 }
78101 }
79102 }
80103 catch ( RequestFailedException e ) when ( e . Status == 403 && e . ErrorCode == "AuthorizationPermissionMismatch" )
81104 {
82- TestContext . Error . WriteLine ( "Datalake Probing OAuth - not ready" ) ;
105+ TestContext . Error . WriteLine ( $ "Datalake Probing OAuth - not ready { Process . GetCurrentProcess ( ) . Id } ") ;
83106 return false ;
84107 }
85- TestContext . Error . WriteLine ( "Datalake Probing OAuth - ready" ) ;
108+ TestContext . Error . WriteLine ( $ "Datalake Probing OAuth - ready { Process . GetCurrentProcess ( ) . Id } ") ;
86109 return true ;
87110 }
88111 }
0 commit comments