Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage role assignment to extension tests #23643

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
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
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"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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
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