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

[5.2] Test fixes to accommodate recent infra changes #2650

Merged
merged 4 commits into from
Jul 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenti
using CancellationTokenSource cts = new();

// 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, StringComparison.Ordinal) ? 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 @@ -67,6 +67,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 ApplicationClientId = "2fd908ad-0664-4344-b9be-cd3e8b574c38";
Expand Down Expand Up @@ -112,6 +113,15 @@ public static bool IsAzureSynapse
}
}

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

public static string SQLServerVersion
{
get
Expand Down Expand Up @@ -649,7 +659,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 @@ -662,7 +672,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 @@ -1059,7 +1059,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 @@ -74,13 +74,14 @@ 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;

[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void KustoDatabaseTest()
{
// This is a sample Kusto database that can be connected by any AD account.
using SqlConnection connection = new SqlConnection("Data Source=help.kusto.windows.net; Authentication=Active Directory Default;Trust Server Certificate=True;");
using SqlConnection connection = new SqlConnection($"Data Source=help.kusto.windows.net; Authentication=Active Directory Default;Trust Server Certificate=True;User ID = {DataTestUtility.UserManagedIdentityClientId};");
connection.Open();
Assert.True(connection.State == System.Data.ConnectionState.Open);
}
Expand Down Expand Up @@ -602,7 +603,7 @@ public static void AccessTokenCallbackMustOpenPassAndChangePropertyFail()
{
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
var cred = new DefaultAzureCredential();
var cred = DataTestUtility.GetTokenCredential();
const string defaultScopeSuffix = "/.default";
using (SqlConnection conn = new SqlConnection(connStr))
{
Expand All @@ -629,7 +630,7 @@ public static void AccessTokenCallbackReceivesUsernameAndPassword()
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
$"User ID={userId}; Password={pwd}";
var cred = new DefaultAzureCredential();
var cred = DataTestUtility.GetTokenCredential();
const string defaultScopeSuffix = "/.default";
using (SqlConnection conn = new SqlConnection(connStr))
{
Expand All @@ -645,12 +646,12 @@ public static void AccessTokenCallbackReceivesUsernameAndPassword()
}
}

[ConditionalFact(nameof(IsAADConnStringsSetup))]
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
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 @@ -706,7 +707,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 @@ -724,7 +725,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 @@ -752,7 +753,7 @@ public static void AccessToken_UserManagedIdentityTest()
}
}

[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsManagedIdentitySetup))]
[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 @@ -782,7 +783,7 @@ public static void Azure_UserManagedIdentityTest()
}
}

[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsAccessTokenSetup), nameof(IsManagedIdentitySetup))]
[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 @@ -360,7 +360,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()
{
SqlConnectionStringBuilder connectionStringBuilder = new(DataTestUtility.TCPConnectionString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,98 @@ 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" });
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ConditionalFact(nameof(CanRunSchemaTests))]
public static void GetStructuredTypeMembersFromSchema()
{
VerifySchemaTable(SqlClientMetaDataCollectionNames.StructuredTypeMembers, new string[] { "TYPE_CATALOG", "TYPE_SCHEMA", "TYPE_NAME", "MEMBER_NAME", "ORDINAL_POSITION" });
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 @@ -129,7 +130,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
Loading
Loading