Skip to content

Commit 544d1d5

Browse files
authored
Add storage role assignment to extension tests (Azure#23643)
1 parent aa3291f commit 544d1d5

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ tools/*.dll
127127
*.pfx
128128
TestConfigurations.xml
129129
*.json.env
130+
*.bicep.env
130131

131132
# Backup & report files from converting an old project file to a newer
132133
# Visual Studio version. Backup files are not needed, because we have git ;-)

sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,26 @@ protected TestEnvironment()
6060

6161
_prefix = serviceName.ToUpperInvariant() + "_";
6262

63-
var testEnvironmentFile = Path.Combine(serviceSdkDirectory, "test-resources.json.env");
64-
if (File.Exists(testEnvironmentFile))
63+
var testEnvironmentFiles = new[]
6564
{
66-
var json = JsonDocument.Parse(
67-
ProtectedData.Unprotect(File.ReadAllBytes(testEnvironmentFile), null, DataProtectionScope.CurrentUser)
68-
);
65+
Path.Combine(serviceSdkDirectory, "test-resources.bicep.env"),
66+
Path.Combine(serviceSdkDirectory, "test-resources.json.env")
67+
};
6968

70-
foreach (var property in json.RootElement.EnumerateObject())
69+
foreach (var testEnvironmentFile in testEnvironmentFiles)
70+
{
71+
if (File.Exists(testEnvironmentFile))
7172
{
72-
_environmentFile[property.Name] = property.Value.GetString();
73+
var json = JsonDocument.Parse(
74+
ProtectedData.Unprotect(File.ReadAllBytes(testEnvironmentFile), null, DataProtectionScope.CurrentUser)
75+
);
76+
77+
foreach (var property in json.RootElement.EnumerateObject())
78+
{
79+
_environmentFile[property.Name] = property.Value.GetString();
80+
}
81+
82+
break;
7383
}
7484
}
7585
}

sdk/extensions/Azure.Extensions.AspNetCore.DataProtection.Blobs/tests/DataProtectionTestEnvironment.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
57
using Azure.Core.TestFramework;
8+
using Azure.Storage.Blobs;
69

710
namespace Azure.Extensions.AspNetCore.DataProtection.Blobs.Tests
811
{
912
public class DataProtectionTestEnvironment: TestEnvironment
1013
{
1114
public Uri BlobStorageEndpoint => new(GetVariable("BLOB_STORAGE_ENDPOINT"));
15+
16+
protected override async ValueTask<bool> IsEnvironmentReadyAsync()
17+
{
18+
try
19+
{
20+
var client = new BlobServiceClient(BlobStorageEndpoint, Credential);
21+
var container = client.GetBlobContainerClient("test");
22+
await container.CreateIfNotExistsAsync();
23+
24+
var blob = container.GetBlobClient("test-blob");
25+
await blob.UploadAsync(new MemoryStream());
26+
await blob.DeleteAsync();
27+
await container.DeleteAsync();
28+
29+
return await base.IsEnvironmentReadyAsync();
30+
}
31+
catch (RequestFailedException e) when (e is { Status: 403})
32+
{
33+
return false;
34+
}
35+
}
1236
}
1337
}

sdk/extensions/test-resources.bicep

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' = {
1616
properties: {
1717
sku: {
1818
family: 'A'
19-
name: 'premium'
19+
name: 'standard'
2020
}
2121
tenantId: tenantId
2222
accessPolicies: [
@@ -93,5 +93,15 @@ resource blobAcount 'Microsoft.Storage/storageAccounts@2019-04-01' = {
9393
}
9494
}
9595

96+
var blobDataContributorRole = 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
97+
resource blobContributorAssignment 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
98+
name: guid(resourceGroup().id, testApplicationOid, blobDataContributorRole)
99+
scope: resourceGroup()
100+
properties: {
101+
principalId: testApplicationOid
102+
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', blobDataContributorRole)
103+
}
104+
}
105+
96106
output AZURE_KEYVAULT_URL string = keyVault.properties.vaultUri
97107
output BLOB_STORAGE_ENDPOINT string = blobAcount.properties.primaryEndpoints.blob

0 commit comments

Comments
 (0)