Skip to content

Commit 606603a

Browse files
azure-powershell-botazurepowershell
andauthored
Sync tools folder from main branch to generation branch (#24931)
Co-authored-by: azurepowershell <[email protected]>
1 parent 85f6920 commit 606603a

File tree

8 files changed

+175
-167
lines changed

8 files changed

+175
-167
lines changed

.ci-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@
323323
"Compute",
324324
"Functions",
325325
"KeyVault",
326-
"KubernetersConfiguration",
326+
"KubernetesConfiguration",
327327
"Network",
328328
"PostgreSql",
329329
"Purview",

tools/Modules/TestFx-Tasks.psm1

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $script:TestFxEnvExtraPropKeys = @(
1717
)
1818

1919
function Set-TestFxEnvironment {
20-
[CmdletBinding(DefaultParameterSetName = "NewServicePrincipal")]
20+
[CmdletBinding(DefaultParameterSetName = "UserAccount")]
2121
param(
2222
[Parameter(Mandatory)]
2323
[ValidateNotNullOrEmpty()]
@@ -27,6 +27,10 @@ function Set-TestFxEnvironment {
2727
[ValidateNotNullOrEmpty()]
2828
[guid] $TenantId,
2929

30+
[Parameter(Mandatory, ParameterSetName = "UserAccount")]
31+
[ValidateNotNullOrEmpty()]
32+
[guid] $UserId,
33+
3034
[Parameter(Mandatory, ParameterSetName = "NewServicePrincipal")]
3135
[ValidateNotNullOrEmpty()]
3236
[string] $ServicePrincipalDisplayName,
@@ -108,11 +112,23 @@ function Set-TestFxEnvironment {
108112
}
109113
}
110114

115+
$testFxEnvProps = [PSCustomObject]@{
116+
Environment = $TargetEnvironment
117+
SubscriptionId = $SubscriptionId
118+
TenantId = $TenantId
119+
HttpRecorderMode = $RecorderMode
120+
}
121+
111122
switch ($PSCmdlet.ParameterSetName) {
123+
"UserAccount" {
124+
$testFxEnvProps | Add-Member -NotePropertyName UserId -NotePropertyValue $UserId
125+
}
112126
"NewServicePrincipal" {
113127
$sp = New-TestFxServicePrincipal -SubscriptionId $SubscriptionId -ServicePrincipalDisplayName $ServicePrincipalDisplayName -Force:$Force
114128
$spAppId = $sp.AppId
115129
$spSecret = $sp.PasswordCredentials.SecretText
130+
$testFxEnvProps | Add-Member -NotePropertyName ServicePrincipal -NotePropertyValue $spAppId
131+
$testFxEnvProps | Add-Member -NotePropertyName ServicePrincipalSecret -NotePropertyValue $spSecret
116132
}
117133
"ExistingServicePrincipal" {
118134
$sp = Get-AzADServicePrincipal -ApplicationId $ServicePrincipalId
@@ -122,18 +138,11 @@ function Set-TestFxEnvironment {
122138

123139
$spAppId = $ServicePrincipalId
124140
$spSecret = $ServicePrincipalSecret
141+
$testFxEnvProps | Add-Member -NotePropertyName ServicePrincipal -NotePropertyValue $spAppId
142+
$testFxEnvProps | Add-Member -NotePropertyName ServicePrincipalSecret -NotePropertyValue $spSecret
125143
}
126144
}
127145

128-
$testFxEnvProps = [PSCustomObject]@{
129-
Environment = $TargetEnvironment
130-
SubscriptionId = $SubscriptionId
131-
TenantId = $TenantId
132-
ServicePrincipal = $spAppId
133-
ServicePrincipalSecret = $spSecret
134-
HttpRecorderMode = $RecorderMode
135-
}
136-
137146
$script:testFxEnvExtraPropKeys | ForEach-Object {
138147
if ($PSBoundParameters.ContainsKey($_)) {
139148
$testFxEnvProps | Add-Member -NotePropertyName $_ -NotePropertyValue $PSBoundParameters[$_]

tools/TestFx/ConnectionString.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class ConnectionString
2626
private Dictionary<string, string> _keyValuePairs;
2727
private string _connString;
2828
private StringBuilder _parseErrorSb;
29-
private string DEFAULT_TENANTID = "72f988bf-86f1-41af-91ab-2d7cd011db47";
3029

3130
public Dictionary<string, string> KeyValuePairs
3231
{
@@ -75,46 +74,13 @@ public ConnectionString(string connString) : this()
7574
{
7675
_connString = connString;
7776
Parse(_connString); //Keyvalue pairs are normalized and is called from Parse(string) function
78-
NormalizeKeyValuePairs();
79-
}
80-
81-
private void NormalizeKeyValuePairs()
82-
{
83-
string clientId, spn, password, spnSecret, userId, aadTenantId;
84-
KeyValuePairs.TryGetValue(ConnectionStringKeys.AADClientIdKey, out clientId);
85-
KeyValuePairs.TryGetValue(ConnectionStringKeys.ServicePrincipalKey, out spn);
86-
87-
KeyValuePairs.TryGetValue(ConnectionStringKeys.UserIdKey, out userId);
88-
KeyValuePairs.TryGetValue(ConnectionStringKeys.PasswordKey, out password);
89-
KeyValuePairs.TryGetValue(ConnectionStringKeys.ServicePrincipalSecretKey, out spnSecret);
90-
KeyValuePairs.TryGetValue(ConnectionStringKeys.TenantIdKey, out aadTenantId);
91-
92-
//ClientId was provided and servicePrincipal was empty, we want ServicePrincipal to be initialized
93-
//At some point we will deprecate ClientId keyName
94-
if (!string.IsNullOrEmpty(clientId) && (string.IsNullOrEmpty(spn)))
95-
{
96-
KeyValuePairs[ConnectionStringKeys.ServicePrincipalKey] = clientId;
97-
}
98-
99-
//Set the value of PasswordKey to ServicePrincipalSecret ONLY if userId is empty
100-
//If UserId is not empty, we are not sure if it's a password for inter active login or ServicePrincipal SecretKey
101-
if (!string.IsNullOrEmpty(password) && (string.IsNullOrEmpty(spnSecret)) && (string.IsNullOrEmpty(userId)))
102-
{
103-
KeyValuePairs[ConnectionStringKeys.ServicePrincipalSecretKey] = password;
104-
}
105-
106-
//Initialize default value for AADTenent
107-
if (string.IsNullOrEmpty(aadTenantId))
108-
{
109-
KeyValuePairs[ConnectionStringKeys.TenantIdKey] = DEFAULT_TENANTID;
110-
}
11177
}
11278

11379
public void Parse(string connString)
11480
{
11581
string parseRegEx = @"(?<KeyName>[^=]+)=(?<KeyValue>.+)";
11682

117-
if (_parseErrorSb != null) _parseErrorSb.Clear();
83+
_parseErrorSb?.Clear();
11884

11985
if (string.IsNullOrEmpty(connString))
12086
{
@@ -161,10 +127,6 @@ public void Parse(string connString)
161127
ParseErrors = string.Format("Incorrect '{0}' keyValue pair format", pair);
162128
}
163129
}
164-
165-
//Adjust key-value pairs and normalize values across multiple keys
166-
//We need to do this here because Connection string can be parsed multiple time within same instance
167-
NormalizeKeyValuePairs();
168130
}
169131
}
170132

tools/TestFx/DelegatingHandlers/HttpMockServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class HttpMockServer : DelegatingHandler
3535

3636
public static string CallerIdentity { get; set; }
3737
public static string TestIdentity { get; set; }
38-
public static HttpRecorderMode Mode { get; set; }
38+
public static HttpRecorderMode Mode { get; internal set; }
3939
public static IRecordMatcher Matcher { get; set; }
4040
public static string RecordsDirectory { get; set; }
4141
public static Dictionary<string, string> Variables { get; private set; }

tools/TestFx/EnvironmentSetupHelper.cs

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
using System;
2727
using System.Collections.Generic;
2828
using System.Collections.ObjectModel;
29-
using System.Diagnostics;
3029
using System.IO;
3130
using System.Linq;
3231
using System.Management.Automation;
@@ -40,9 +39,9 @@ namespace Microsoft.Azure.Commands.TestFx
4039
{
4140
public class EnvironmentSetupHelper
4241
{
43-
private const string TestEnvironmentName = "__test-environment";
42+
private const string TestFxEnvironmentName = "__testfx-environment";
4443

45-
private const string TestSubscriptionName = "__test-subscriptions";
44+
private const string TestFxSubscriptionName = "__testfx-subscription";
4645

4746
private static string PackageDirectoryFromCommon { get; } = GetConfigDirectory();
4847

@@ -134,9 +133,6 @@ public EnvironmentSetupHelper()
134133
// Ignore SSL errors
135134
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => true;
136135

137-
// Set RunningMocked
138-
TestMockSupport.RunningMocked = HttpMockServer.GetCurrentMode() == HttpRecorderMode.Playback;
139-
140136
if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), Resources.AzureDirectoryName, "testcredentials.json")))
141137
{
142138
SetEnvironmentVariableFromCredentialFile();
@@ -388,62 +384,67 @@ public void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)
388384
throw new NotSupportedException("RDFE environment is not supported in .Net Core");
389385
}
390386

391-
if (currentEnvironment.UserName == null)
392-
{
393-
currentEnvironment.UserName = "[email protected]";
394-
}
395-
396387
SetAuthenticationFactory(currentEnvironment);
397-
AzureEnvironment environment = new AzureEnvironment { Name = TestEnvironmentName };
398-
Debug.Assert(currentEnvironment != null);
399-
environment.ActiveDirectoryAuthority = currentEnvironment.Endpoints.AADAuthUri.AbsoluteUri;
400-
environment.GalleryUrl = currentEnvironment.Endpoints.GalleryUri?.AbsoluteUri;
401-
environment.ServiceManagementUrl = currentEnvironment.BaseUri.AbsoluteUri;
402-
environment.ResourceManagerUrl = currentEnvironment.Endpoints.ResourceManagementUri.AbsoluteUri;
403-
environment.GraphUrl = currentEnvironment.Endpoints.GraphUri.AbsoluteUri;
404-
environment.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = currentEnvironment.Endpoints.DataLakeAnalyticsJobAndCatalogServiceUri.OriginalString.Replace("https://", ""); // because it is just a sufix
405-
environment.AzureDataLakeStoreFileSystemEndpointSuffix = currentEnvironment.Endpoints.DataLakeStoreServiceUri.OriginalString.Replace("https://", ""); // because it is just a sufix
406-
environment.StorageEndpointSuffix = AzureEnvironmentConstants.AzureStorageEndpointSuffix;
407-
environment.AzureKeyVaultDnsSuffix = AzureEnvironmentConstants.AzureKeyVaultDnsSuffix;
408-
environment.AzureKeyVaultServiceEndpointResourceId = AzureEnvironmentConstants.AzureKeyVaultServiceEndpointResourceId;
409-
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.MicrosoftGraphUrl, currentEnvironment.Endpoints.GraphUri.AbsoluteUri);
410-
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint, "https://api.loganalytics.io/v1");
411-
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId, "https://api.loganalytics.io");
412-
if (!AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().EnvironmentTable.ContainsKey(TestEnvironmentName))
413-
{
414-
AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().EnvironmentTable[TestEnvironmentName] = environment;
415-
}
416-
417-
if (currentEnvironment.SubscriptionId != null)
418-
{
419-
var testSubscription = new AzureSubscription
420-
{
421-
Id = currentEnvironment.SubscriptionId,
422-
Name = TestSubscriptionName,
423-
};
424388

425-
testSubscription.SetEnvironment(TestEnvironmentName);
426-
testSubscription.SetAccount(currentEnvironment.UserName);
389+
AzureEnvironment testEnvironment = new AzureEnvironment
390+
{
391+
Name = TestFxEnvironmentName,
392+
ActiveDirectoryAuthority = currentEnvironment.Endpoints.AADAuthUri.AbsoluteUri,
393+
ActiveDirectoryServiceEndpointResourceId = currentEnvironment.Endpoints.AADTokenAudienceUri.AbsoluteUri,
394+
GraphUrl = currentEnvironment.Endpoints.GraphUri.AbsoluteUri,
395+
GraphEndpointResourceId = currentEnvironment.Endpoints.GraphTokenAudienceUri.AbsoluteUri,
396+
ResourceManagerUrl = currentEnvironment.Endpoints.ResourceManagementUri.AbsoluteUri,
397+
ServiceManagementUrl = currentEnvironment.Endpoints.ServiceManagementUri.AbsoluteUri,
398+
GalleryUrl = currentEnvironment.Endpoints.GalleryUri?.AbsoluteUri,
399+
AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = currentEnvironment.Endpoints.DataLakeAnalyticsJobAndCatalogServiceUri.OriginalString.Replace("https://", ""), // because it is just a sufix
400+
AzureDataLakeStoreFileSystemEndpointSuffix = currentEnvironment.Endpoints.DataLakeStoreServiceUri.OriginalString.Replace("https://", ""), // because it is just a sufix
401+
StorageEndpointSuffix = AzureEnvironmentConstants.AzureStorageEndpointSuffix,
402+
AzureKeyVaultDnsSuffix = AzureEnvironmentConstants.AzureKeyVaultDnsSuffix,
403+
AzureKeyVaultServiceEndpointResourceId = AzureEnvironmentConstants.AzureKeyVaultServiceEndpointResourceId
404+
};
405+
testEnvironment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.MicrosoftGraphUrl, currentEnvironment.Endpoints.GraphUri.AbsoluteUri);
406+
testEnvironment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint, "https://api.loganalytics.io/v1");
407+
testEnvironment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId, "https://api.loganalytics.io");
408+
if (!AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().EnvironmentTable.ContainsKey(TestFxEnvironmentName))
409+
{
410+
AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().EnvironmentTable[TestFxEnvironmentName] = testEnvironment;
411+
}
412+
413+
AzureSubscription testSubscription = new AzureSubscription();
414+
if (!string.IsNullOrEmpty(currentEnvironment.SubscriptionId))
415+
{
416+
testSubscription.Id = currentEnvironment.SubscriptionId;
417+
testSubscription.Name = TestFxSubscriptionName;
418+
testSubscription.SetEnvironment(TestFxEnvironmentName);
419+
testSubscription.SetTenant(currentEnvironment.TenantId);
420+
testSubscription.SetAccount(currentEnvironment.UserId);
427421
testSubscription.SetDefault();
428422
testSubscription.SetStorageAccount(Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"));
423+
}
429424

430-
var testAccount = new AzureAccount()
431-
{
432-
Id = currentEnvironment.UserName,
433-
Type = AzureAccount.AccountType.User,
434-
};
425+
AzureTenant testTenant = new AzureTenant();
426+
if (!string.IsNullOrEmpty(currentEnvironment.TenantId))
427+
{
428+
testTenant.Id = currentEnvironment.TenantId;
429+
}
435430

436-
testAccount.SetSubscriptions(currentEnvironment.SubscriptionId);
437-
var testTenant = new AzureTenant() { Id = Guid.NewGuid().ToString() };
438-
if (!string.IsNullOrEmpty(currentEnvironment.TenantId))
439-
{
440-
if (Guid.TryParse(currentEnvironment.TenantId, out _))
441-
{
442-
testTenant.Id = currentEnvironment.TenantId;
443-
}
444-
}
445-
AzureRmProfileProvider.Instance.Profile.DefaultContext = new AzureContext(testSubscription, testAccount, environment, testTenant);
431+
AzureAccount testAccount = new AzureAccount();
432+
if (!string.IsNullOrEmpty(currentEnvironment.UserId))
433+
{
434+
testAccount.Id = currentEnvironment.UserId;
435+
testAccount.Type = AzureAccount.AccountType.User;
446436
}
437+
else if (!string.IsNullOrEmpty(currentEnvironment.ServicePrincipalClientId) && !string.IsNullOrEmpty(currentEnvironment.ServicePrincipalSecret))
438+
{
439+
testAccount.Id = currentEnvironment.ServicePrincipalClientId;
440+
testAccount.Type = AzureAccount.AccountType.ServicePrincipal;
441+
}
442+
443+
testAccount.SetAccessToken(string.Empty);
444+
testAccount.SetSubscriptions(currentEnvironment.SubscriptionId);
445+
testAccount.SetTenants(currentEnvironment.TenantId);
446+
447+
AzureRmProfileProvider.Instance.Profile.DefaultContext = new AzureContext(testSubscription, testAccount, testEnvironment, testTenant);
447448
}
448449

449450
private void SetAuthenticationFactory(TestEnvironment environment)
@@ -457,7 +458,7 @@ private void SetAuthenticationFactory(TestEnvironment environment)
457458
.GetAwaiter()
458459
.GetResult();
459460

460-
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory(environment.UserName, httpMessage.Headers.Authorization.Parameter);
461+
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory(environment.UserId, httpMessage.Headers.Authorization.Parameter);
461462
}
462463
}
463464

@@ -524,6 +525,7 @@ public virtual Collection<PSObject> RunPowerShellTest(params string[] scripts)
524525
Collection<PSObject> output = null;
525526
foreach (var script in scripts)
526527
{
528+
Console.WriteLine($"Executing test: {script}");
527529
TracingInterceptor?.Information(script);
528530
powershell.AddScript(script);
529531
}

tools/TestFx/Mocks/MockClientFactory.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.Azure.Commands.Common.Authentication.Factories;
1818
using Microsoft.Azure.Commands.Common.Authentication.Models;
1919
using Microsoft.Rest.Azure;
20-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2120
using System;
2221
using System.Collections.Generic;
2322
using System.Diagnostics;
@@ -28,7 +27,6 @@
2827
using System.Reflection;
2928
using System.Threading;
3029
using System.Threading.Tasks;
31-
using Microsoft.Azure.Commands.TestFx.DelegatingHandlers;
3230
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
3331
using Microsoft.Azure.Test.HttpRecorder;
3432
using Microsoft.Azure.Commands.Common.MSGraph.Version1_0;
@@ -104,7 +102,7 @@ public TClient CreateCustomArmClient<TClient>(params object[] parameters) where
104102
client = realClientFactory.CreateCustomArmClient<TClient>(newParameters);
105103
}
106104

107-
if (TestMockSupport.RunningMocked && HttpMockServer.GetCurrentMode() != HttpRecorderMode.Record)
105+
if (HttpMockServer.Mode == HttpRecorderMode.Playback)
108106
{
109107
if (client is IAzureClient azureClient)
110108
{

tools/TestFx/Mocks/MockContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.TestFx.DelegatingHandlers;
1717
using Microsoft.Azure.Commands.TestFx.Recorder;
1818
using Microsoft.Azure.Test.HttpRecorder;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1920
using System;
2021
using System.Collections.Generic;
2122
using System.Net.Http;
@@ -66,6 +67,8 @@ public static MockContext Start(
6667
}
6768
HttpMockServer.Initialize(className, methodName);
6869

70+
TestMockSupport.RunningMocked = HttpMockServer.Mode == HttpRecorderMode.Playback;
71+
6972
return context;
7073
}
7174

0 commit comments

Comments
 (0)