Skip to content

Commit

Permalink
Add storage role assignment to extension tests (#23643)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Aug 30, 2021
1 parent aa3291f commit 544d1d5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ tools/*.dll
*.pfx
TestConfigurations.xml
*.json.env
*.bicep.env

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
Expand Down
24 changes: 17 additions & 7 deletions sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,26 @@ protected TestEnvironment()

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

var testEnvironmentFile = Path.Combine(serviceSdkDirectory, "test-resources.json.env");
if (File.Exists(testEnvironmentFile))
var testEnvironmentFiles = new[]
{
var json = JsonDocument.Parse(
ProtectedData.Unprotect(File.ReadAllBytes(testEnvironmentFile), null, DataProtectionScope.CurrentUser)
);
Path.Combine(serviceSdkDirectory, "test-resources.bicep.env"),
Path.Combine(serviceSdkDirectory, "test-resources.json.env")
};

foreach (var property in json.RootElement.EnumerateObject())
foreach (var testEnvironmentFile in testEnvironmentFiles)
{
if (File.Exists(testEnvironmentFile))
{
_environmentFile[property.Name] = property.Value.GetString();
var json = JsonDocument.Parse(
ProtectedData.Unprotect(File.ReadAllBytes(testEnvironmentFile), null, DataProtectionScope.CurrentUser)
);

foreach (var property in json.RootElement.EnumerateObject())
{
_environmentFile[property.Name] = property.Value.GetString();
}

break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@
// Licensed under the MIT License.

using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.Storage.Blobs;

namespace Azure.Extensions.AspNetCore.DataProtection.Blobs.Tests
{
public class DataProtectionTestEnvironment: TestEnvironment
{
public Uri BlobStorageEndpoint => new(GetVariable("BLOB_STORAGE_ENDPOINT"));

protected override async ValueTask<bool> IsEnvironmentReadyAsync()
{
try
{
var client = new BlobServiceClient(BlobStorageEndpoint, Credential);
var container = client.GetBlobContainerClient("test");
await container.CreateIfNotExistsAsync();

var blob = container.GetBlobClient("test-blob");
await blob.UploadAsync(new MemoryStream());
await blob.DeleteAsync();
await container.DeleteAsync();

return await base.IsEnvironmentReadyAsync();
}
catch (RequestFailedException e) when (e is { Status: 403})
{
return false;
}
}
}
}
12 changes: 11 additions & 1 deletion sdk/extensions/test-resources.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' = {
properties: {
sku: {
family: 'A'
name: 'premium'
name: 'standard'
}
tenantId: tenantId
accessPolicies: [
Expand Down Expand Up @@ -93,5 +93,15 @@ resource blobAcount 'Microsoft.Storage/storageAccounts@2019-04-01' = {
}
}

var blobDataContributorRole = 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
resource blobContributorAssignment 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
name: guid(resourceGroup().id, testApplicationOid, blobDataContributorRole)
scope: resourceGroup()
properties: {
principalId: testApplicationOid
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', blobDataContributorRole)
}
}

output AZURE_KEYVAULT_URL string = keyVault.properties.vaultUri
output BLOB_STORAGE_ENDPOINT string = blobAcount.properties.primaryEndpoints.blob

0 comments on commit 544d1d5

Please sign in to comment.