Skip to content

Commit

Permalink
[3.1] Test fixes to accommodate recent infra changes (#2709)
Browse files Browse the repository at this point in the history
Co-authored-by: David Engel <[email protected]>
  • Loading branch information
dauinsight and David-Engel authored Jul 24, 2024
1 parent 15a694b commit 21deeef
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
CancellationTokenSource cts = new CancellationTokenSource();

// Use Connection timeout value to cancel token acquire request after certain period of time.
cts.CancelAfter(parameters.ConnectionTimeout * 1000); // Convert to milliseconds
int timeout = parameters.ConnectionTimeout * 1000;
if (timeout > 0) // if ConnectionTimeout is 0 or the millis overflows an int, no need to set CancelAfter
{
cts.CancelAfter(timeout); // Convert to milliseconds
}

string scope = parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix;
string[] scopes = new string[] { scope };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class DataTestUtility
public static readonly string EnclaveAzureDatabaseConnString = null;
public static bool ManagedIdentitySupported = true;
public static string AADAccessToken = null;
public static bool SupportsSystemAssignedManagedIdentity = false;
public static string AADSystemIdentityAccessToken = null;
public static string AADUserIdentityAccessToken = null;
public const string UdtTestDbName = "UdtTestDb";
Expand All @@ -70,6 +71,15 @@ public static class DataTestUtility
private static Dictionary<string, bool> AvailableDatabases;
private static TraceEventListener TraceListener;

public static bool TcpConnectionStringDoesNotUseAadAuth
{
get
{
SqlConnectionStringBuilder builder = new (TCPConnectionString);
return builder.Authentication == SqlAuthenticationMethod.SqlPassword || builder.Authentication == SqlAuthenticationMethod.NotSpecified;
}
}

static DataTestUtility()
{
Config c = Config.Load();
Expand Down Expand Up @@ -472,7 +482,7 @@ public static string GetAccessToken()

public static string GetSystemIdentityAccessToken()
{
if (true == ManagedIdentitySupported && null == AADSystemIdentityAccessToken && IsAADPasswordConnStrSetup())
if (ManagedIdentitySupported && SupportsSystemAssignedManagedIdentity && null == AADSystemIdentityAccessToken && IsAADPasswordConnStrSetup())
{
AADSystemIdentityAccessToken = AADUtility.GetManagedIdentityToken().GetAwaiter().GetResult();
if (AADSystemIdentityAccessToken == null)
Expand All @@ -485,7 +495,7 @@ public static string GetSystemIdentityAccessToken()

public static string GetUserIdentityAccessToken()
{
if (true == ManagedIdentitySupported && null == AADUserIdentityAccessToken && IsAADPasswordConnStrSetup())
if (ManagedIdentitySupported && null == AADUserIdentityAccessToken && IsAADPasswordConnStrSetup())
{
// Pass User Assigned Managed Identity Client Id here.
AADUserIdentityAccessToken = AADUtility.GetManagedIdentityToken(UserManagedIdentityClientId).GetAwaiter().GetResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,14 @@ public void UpdateOffsetTest()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static bool CanRunSchemaTests()
{
return DataTestUtility.AreConnStringsSetup() &&
// Tests switch to master database, which is not guaranteed when using AAD auth
DataTestUtility.TcpConnectionStringDoesNotUseAadAuth;
}

[ConditionalFact(nameof(CanRunSchemaTests))]
public void SelectAllTest()
{
// Test exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private static void ConnectAndDisconnect(string connectionString, SqlCredential
private static bool IsAccessTokenSetup() => DataTestUtility.IsAccessTokenSetup();
private static bool IsAADConnStringsSetup() => DataTestUtility.IsAADPasswordConnStrSetup();
private static bool IsManagedIdentitySetup() => DataTestUtility.ManagedIdentitySupported;
private static bool SupportsSystemAssignedManagedIdentity() => DataTestUtility.SupportsSystemAssignedManagedIdentity;

[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenTest()
Expand Down Expand Up @@ -481,7 +482,7 @@ public static void ActiveDirectoryDefaultMustPass()
{
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=ActiveDirectoryDefault;";
$"Authentication=ActiveDirectoryDefault;User ID={DataTestUtility.UserManagedIdentityClientId};";

// Connection should be established using Managed Identity by default.
ConnectAndDisconnect(connStr);
Expand Down Expand Up @@ -537,7 +538,7 @@ public static void ConnectionSpeed()

#region Managed Identity Authentication tests

[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void SystemAssigned_ManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
Expand All @@ -555,7 +556,7 @@ public static void UserAssigned_ManagedIdentityTest()
ConnectAndDisconnect(connStr);
}

[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void AccessToken_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
Expand Down Expand Up @@ -583,7 +584,7 @@ public static void AccessToken_UserManagedIdentityTest()
}
}

[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure))]
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void Azure_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
Expand Down Expand Up @@ -613,7 +614,7 @@ public static void Azure_UserManagedIdentityTest()
}
}

[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure))]
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsAccessTokenSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void Azure_AccessToken_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ public static void ConnectionStringPersistentInfoTest()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
// ConnectionOpenDisableRetry relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
public static void ConnectionOpenDisableRetry()
{
using (SqlConnection sqlConnection = new SqlConnection((new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) { InitialCatalog = "DoesNotExist0982532435423", Pooling = false }).ConnectionString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,92 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class ConnectionSchemaTest
{
public static bool CanRunSchemaTests()
{
return DataTestUtility.AreConnStringsSetup() &&
// Tests switch to master database, which is not guaranteed when using AAD auth
DataTestUtility.TcpConnectionStringDoesNotUseAadAuth;
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetTablesFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Tables, new string[] { "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME", "TABLE_TYPE" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetProceduresFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Procedures, new string[] { "ROUTINE_SCHEMA", "ROUTINE_NAME", "ROUTINE_TYPE" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetProcedureParametersFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.ProcedureParameters, new string[] { "PARAMETER_MODE", "PARAMETER_NAME" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetDatabasesFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Databases, new string[] { "database_name", "dbid", "create_date" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetForeignKeysFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.ForeignKeys, new string[] { "CONSTRAINT_TYPE", "IS_DEFERRABLE", "INITIALLY_DEFERRED" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetIndexesFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Indexes, new string[] { "index_name", "constraint_name" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetIndexColumnsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.IndexColumns, new string[] { "index_name", "KeyType", "column_name" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetColumnsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Columns, new string[] { "IS_NULLABLE", "COLUMN_DEFAULT" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetAllColumnsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.AllColumns, new string[] { "IS_NULLABLE", "COLUMN_DEFAULT", "IS_FILESTREAM", "IS_SPARSE", "IS_COLUMN_SET" });
}
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]

[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetColumnSetColumnsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.ColumnSetColumns, new string[] { "IS_NULLABLE", "COLUMN_DEFAULT", "IS_FILESTREAM", "IS_SPARSE", "IS_COLUMN_SET" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetUsersFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Users, new string[] { "uid", "user_name" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetViewsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.Views, new string[] { "TABLE_NAME", "CHECK_OPTION", "IS_UPDATABLE" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetViewColumnsFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.ViewColumns, new string[] { "VIEW_CATALOG", "VIEW_SCHEMA", "VIEW_NAME" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetUserDefinedTypesFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.UserDefinedTypes, new string[] { "assembly_name", "version_revision", "culture_info" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static void ExceptionTests()
}

// Synapse: 110003;Invalid user or password
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void VariousExceptionTests()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString);
Expand All @@ -219,7 +219,7 @@ public static void VariousExceptionTests()
}

// Test 1 - B
badBuilder = new SqlConnectionStringBuilder(builder.ConnectionString) { Password = string.Empty, IntegratedSecurity = false };
badBuilder = new SqlConnectionStringBuilder(builder.ConnectionString) { Password = string.Empty, IntegratedSecurity = false, Authentication = SqlAuthenticationMethod.NotSpecified };
using (var sqlConnection = new SqlConnection(badBuilder.ConnectionString))
{
string errorMessage = string.Format(CultureInfo.InvariantCulture, logonFailedErrorMessage, badBuilder.UserID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class SqlConfigurationManagerReliabilityTest
{ InitialCatalog = SqlConnectionReliabilityTest.InvalidInitialCatalog, ConnectTimeout = 1 }.ConnectionString;

#region Internal Functions
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
// Test relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Fix, RetryLogicConfigHelper.RetryMethodName_Inc)]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Inc, RetryLogicConfigHelper.RetryMethodName_Exp)]
[InlineData(RetryLogicConfigHelper.RetryMethodName_Exp, RetryLogicConfigHelper.RetryMethodName_Fix)]
Expand Down Expand Up @@ -160,7 +161,8 @@ public void InvalidRetryMethodName(string methodName)
s_commandCRLTest.NoneRetriableExecuteFail(TcpCnnString, cmdProvider);
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
// Test relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
[InlineData("InvalidRetrylogicTypeName")]
[InlineData("")]
[InlineData(null)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class SqlConnectionReliabilityTest
private readonly string _cancelErrMsgPattern = RetryLogicTestHelper.s_CancelErrMsgPattern;

#region Sync
[Theory]
// Test relies on error 4060 for automatic retry, which is not reliable when using Azure or AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper))]
public void ConnectionRetryOpenInvalidCatalogFailed(string cnnString, SqlRetryLogicBaseProvider provider)
{
Expand All @@ -33,7 +34,8 @@ public void ConnectionRetryOpenInvalidCatalogFailed(string cnnString, SqlRetryLo
}
}

[Theory]
// Test relies on error 4060 for automatic retry, which is not reliable when using Azure or AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper))]
public void ConnectionCancelRetryOpenInvalidCatalog(string cnnString, SqlRetryLogicBaseProvider provider)
{
Expand Down Expand Up @@ -98,7 +100,8 @@ public void CreateDatabaseWhileTryingToConnect(string cnnString, SqlRetryLogicBa
Assert.True(currentRetries > 0);
}

[Theory]
// Test relies on AggregateException but SqlException is thrown for Azure auth (Address in #31453)
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsNotAzureServer))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper))]
public void ConcurrentExecution(string cnnString, SqlRetryLogicBaseProvider provider)
{
Expand Down Expand Up @@ -154,7 +157,8 @@ public void DefaultOpenWithoutRetry(string connectionString, SqlRetryLogicBasePr
#endregion

#region Async
[Theory]
// Test relies on error 4060 for automatic retry, which is not reliable when using Azure or AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 5 }, MemberType = typeof(RetryLogicTestHelper))]
public async void ConnectionRetryOpenAsyncInvalidCatalogFailed(string cnnString, SqlRetryLogicBaseProvider provider)
{
Expand All @@ -171,7 +175,8 @@ public async void ConnectionRetryOpenAsyncInvalidCatalogFailed(string cnnString,
}
}

[Theory]
// Test relies on error 4060 for automatic retry, which is not returned when using AAD auth
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.TcpConnectionStringDoesNotUseAadAuth))]
[MemberData(nameof(RetryLogicTestHelper.GetConnectionAndRetryStrategyInvalidCatalog), parameters: new object[] { 2 }, MemberType = typeof(RetryLogicTestHelper))]
public async void ConnectionCancelRetryOpenAsyncInvalidCatalog(string cnnString, SqlRetryLogicBaseProvider provider)
{
Expand Down

0 comments on commit 21deeef

Please sign in to comment.