Skip to content

Tests | Move fixtures to common project #3402

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

Merged
merged 8 commits into from
Jun 24, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@
</ContentWithTargetPath>
</ItemGroup>

<!-- Fixture-specific References -->
<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Azure.Security.KeyVault.Keys" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.using System;

using System;
using System.Collections.Generic;
using Azure.Core;
using Azure.Security.KeyVault.Keys;

namespace Microsoft.Data.SqlClient.Tests.Common.Fixtures;

/// <summary>
/// Provides a base class for managing Azure Key Vault keys in test fixtures.
/// </summary>
/// <remarks>
/// This class simplifies the creation and cleanup of RSA keys in an Azure Key Vault during testing
/// scenarios. It ensures that any keys created during the fixture's lifetime are properly deleted when the fixture is
/// disposed.
/// </remarks>
public abstract class AzureKeyVaultKeyFixtureBase : IDisposable
{
private readonly KeyClient _keyClient;
private readonly Random _randomGenerator;

private readonly List<KeyVaultKey> _createdKeys = new List<KeyVaultKey>();

protected AzureKeyVaultKeyFixtureBase(Uri keyVaultUri, TokenCredential keyVaultToken)
{
_keyClient = new KeyClient(keyVaultUri, keyVaultToken);
_randomGenerator = new Random();
}

protected Uri CreateKey(string name, int keySize)
{
CreateRsaKeyOptions createOptions = new CreateRsaKeyOptions(GenerateUniqueName(name)) { KeySize = keySize };
KeyVaultKey created = _keyClient.CreateRsaKey(createOptions);

_createdKeys.Add(created);
return created.Id;
}

private string GenerateUniqueName(string name)
{
byte[] rndBytes = new byte[16];

_randomGenerator.NextBytes(rndBytes);
return name + "-" + BitConverter.ToString(rndBytes);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
foreach (KeyVaultKey key in _createdKeys)
{
try
{
_keyClient.StartDeleteKey(key.Name).WaitForCompletion();
}
catch (Exception)
{
continue;
}
}
}
}
Loading
Loading