-
Notifications
You must be signed in to change notification settings - Fork 332
[7.0.3 Cherry-pick] AE tests: create ConversionTests keys once per class to slow error 3807 identifier exhaustion #4485
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
Open
github-actions
wants to merge
2
commits into
release/7.0
Choose a base branch
from
dev/automation/pr-4478-to-7.0.3
base: release/7.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−36
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...ft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/ConversionTestFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // 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 Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup; | ||
| using Microsoft.Data.SqlClient.Tests.Common.Fixtures; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted | ||
| { | ||
| /// <summary> | ||
| /// Class fixture for <see cref="ConversionTests"/>. Creates a single Column Master Key (CMK) and | ||
| /// Column Encryption Key (CEK) once for the whole test class instead of once per test case. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// xUnit constructs a test class instance for every test case, so creating the keys in the | ||
| /// <see cref="ConversionTests"/> constructor issued a <c>CREATE COLUMN MASTER KEY</c> and | ||
| /// <c>CREATE COLUMN ENCRYPTION KEY</c> for each of the dozens of cases in the class. On a | ||
| /// long-lived shared database that rapidly advances SQL Server's internal per-database | ||
| /// identifier allocator, which is never reclaimed on <c>DROP</c>, eventually causing error 3807 | ||
| /// ("Create failed because all available identifiers have been exhausted"). Creating the keys | ||
| /// once per class via <see cref="Xunit.IClassFixture{TFixture}"/> reduces that churn by roughly | ||
| /// two orders of magnitude. | ||
| /// </remarks> | ||
| public sealed class ConversionTestFixture : ColumnMasterKeyCertificateFixture | ||
| { | ||
| private readonly ColumnMasterKey _columnMasterKey; | ||
| private readonly SqlColumnEncryptionCertificateStoreProvider _certStoreProvider = | ||
| new SqlColumnEncryptionCertificateStoreProvider(); | ||
|
|
||
| /// <summary> | ||
| /// The single Column Encryption Key shared by all cases in <see cref="ConversionTests"/>. | ||
| /// </summary> | ||
| public ColumnEncryptionKey ColumnEncryptionKey { get; } | ||
|
|
||
| public ConversionTestFixture() | ||
| { | ||
| _columnMasterKey = new CspColumnMasterKey( | ||
| DatabaseHelper.GenerateUniqueName("CMK"), | ||
| ColumnMasterKeyCertificate.Thumbprint, | ||
| _certStoreProvider, | ||
| DataTestUtility.EnclaveEnabled); | ||
|
|
||
| ColumnEncryptionKey = new ColumnEncryptionKey( | ||
| DatabaseHelper.GenerateUniqueName("CEK"), | ||
| _columnMasterKey, | ||
| _certStoreProvider); | ||
|
|
||
| foreach (string connectionStr in DataTestUtility.AEConnStringsSetup) | ||
| { | ||
| SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(connectionStr); | ||
| // The AE setup often fails with a connect timeout here; ensure a reasonable minimum. | ||
| connectionString.ConnectTimeout = Math.Max(connectionString.ConnectTimeout, 30); | ||
|
|
||
| using SqlConnection sqlConnection = new SqlConnection(connectionString.ConnectionString); | ||
| sqlConnection.Open(); | ||
| _columnMasterKey.Create(sqlConnection); | ||
| ColumnEncryptionKey.Create(sqlConnection); | ||
| } | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| try | ||
| { | ||
| foreach (string connectionStr in DataTestUtility.AEConnStringsSetup) | ||
| { | ||
| SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(connectionStr); | ||
| // Match the constructor's minimum timeout; AE teardown is prone to connect timeouts, | ||
| // and skipping cleanup would leave the shared CMK/CEK behind. | ||
| connectionString.ConnectTimeout = Math.Max(connectionString.ConnectTimeout, 30); | ||
|
|
||
| // Key drops are best-effort: a failure here must not prevent the base fixture | ||
| // from removing the test certificate from the certificate store (see finally). | ||
| try | ||
| { | ||
| using SqlConnection sqlConnection = new SqlConnection(connectionString.ConnectionString); | ||
| sqlConnection.Open(); | ||
| ColumnEncryptionKey.Drop(sqlConnection); | ||
| _columnMasterKey.Drop(sqlConnection); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine( | ||
| $"ConversionTestFixture: failed to drop keys on '{connectionString.DataSource}': {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| base.Dispose(disposing); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.