Skip to content

Commit ca4ee43

Browse files
authored
Fix minor testfx issues and update doc. (#24952)
* Fix minor testfx issue and update doc. * Reorganize the condition for test mode validation.
1 parent 9e914e6 commit ca4ee43

File tree

4 files changed

+27
-80
lines changed

4 files changed

+27
-80
lines changed

documentation/testing-docs/using-azure-test-framework.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,19 @@ For an existing service principal, this command will respect your current settin
103103

104104
#### Record Tests
105105

106-
After creating the service principal, you will need to grant it access to Azure resources. This can be achieved using the following PowerShell command. The argument for this command is the application ID (See [Service Principal Application ID](https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key))
106+
You can use either a user account (Recommended) or a Service Principal to record test cases with appropriate permissions.
107107

108-
```powershell
109-
New-AzRoleAssignment -ApplicationId <ApplicationId> -Scope "/subscriptions/<SubscriptionId>" -RoleDefinitionName Contributor
110-
```
111-
112-
To use this option, set the following environment variables before starting Visual Studio. The following values are substituted into the below environment variables:
113-
114-
`ClientId`
108+
With user account, you may set the following environment variables:
115109

116-
* The [Service Principal Application ID](https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key)
117-
118-
`ClientSecret`
110+
```
111+
TEST_CSM_ORGID_AUTHENTICATION=Environment=Prod;SubscriptionId=<SubscriptionId>;TenantId=<TenantId>;UserId=<UserId>;
112+
AZURE_TEST_MODE=Record
113+
```
119114

120-
* The [Service Principal Authentication Key](https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key)
115+
For service principal, you may set the following environment variables:
121116

122117
```
123-
TEST_CSM_ORGID_AUTHENTICATION=Environment=Prod;SubscriptionId=<SubscriptionId>;TenantId=<TenantId>;ServicePrincipal=<ClientId>;ServicePrincipalSecret=<ClientSecret>;HttpRecorderMode=Record;
118+
TEST_CSM_ORGID_AUTHENTICATION=Environment=Prod;SubscriptionId=<SubscriptionId>;TenantId=<TenantId>;ServicePrincipal=<ClientId>;ServicePrincipalSecret=<ClientSecret>;
124119
AZURE_TEST_MODE=Record
125120
```
126121

@@ -129,7 +124,7 @@ AZURE_TEST_MODE=Record
129124
The default test mode is `Playback`, so there is no need to set up the `AZURE_TEST_MODE` variable. You may optionally set the environment variables:
130125

131126
```
132-
TEST_CSM_ORGID_AUTHENTICATION=Environment=Prod;SubscriptionId=<SubscriptionId>;TenantId=<TenantId>;ServicePrincipal=<ClientId>;ServicePrincipalSecret=<ClientSecret>;HttpRecorderMode=Playback;
127+
TEST_CSM_ORGID_AUTHENTICATION=Environment=Prod;SubscriptionId=<SubscriptionId>;TenantId=<TenantId>;ServicePrincipal=<ClientId>;ServicePrincipalSecret=<ClientSecret>;
133128
AZURE_TEST_MODE=Playback
134129
```
135130

@@ -140,8 +135,8 @@ The recommended approach for building the connection string is using a config fi
140135
- If JSON config file exists
141136
- The Test Framework will use it to build the connection string, ignoring any settings from environment variables.
142137
- If JSON config file does not exist
143-
- The Test Framework will first check the TEST_CSM_ORGID_AUTHENTICATION environment variable and use its value to build the connection string.
144-
- If `HttpRecorderMode` is not set, then the Test Framework will check the `AZURE_TEST_MODE` environment variable.
138+
- The Test Framework will check the `TEST_CSM_ORGID_AUTHENTICATION` environment variable and use its value to build the connection string.
139+
- The Test Framework will check the `AZURE_TEST_MODE` environment variable and use its value to build the test mode.
145140
- If `AZURE_TEST_MODE` is set, its value will be used as the test mode.
146141
- If `AZURE_TEST_MODE` is not set, the default test mode will be `Playback`.
147142

@@ -151,10 +146,8 @@ If you are unsure about the settings on your machine, you can run the command `G
151146

152147
- [Run the tests](https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/azure-powershell-developer-guide.md#recordingrunning-tests) and make sure that you have a generated `.json` file that corresponds to the test name, and it should be located under the `SessionRecords` folder within the test project.
153148
- If you want to switch from Record to Playback or from Playback to Record, consider below steps.
154-
- If you choose to use a JSON config file, update the value of the property `HttpRecorderMode` in the JSON file.
155-
- If you prefer environment variables
156-
- If you have `HttpRecorderMode` defined in the environment variable `TEST_CSM_ORGID_AUTHENTICATION`, update its value accordingly.
157-
- Otherwise, update the value of `AZURE_TEST_MODE`.
149+
- If you choose to use a JSON config file, update the value of the property `HttpRecorderMode` in the JSON file directly.
150+
- If you prefer environment variables, update the value of environment variable `AZURE_TEST_MODE`.
158151

159152
## Change Test Environment settings at run-time
160153

@@ -179,7 +172,7 @@ Changing the above properties at run-time has the potential to hard code few thi
179172

180173
#### Issue: exceptions in Microsoft.Azure.Test.HttpRecorder
181174

182-
Ensure that the `HttpRecorderMode` in the `TEST_CSM_ORGID_AUTHENTICATION` environment variable is consistent with the value in `AZURE_TEST_MODE` environment variable.
175+
The `HttpRecorderMode` key in the `TEST_CSM_ORGID_AUTHENTICATION` environment variable has been deprecated. Please remove this key/value pair and use `AZURE_TEST_MODE` instead for recording mode.
183176

184177
## Supported Environments in Test Framework
185178

src/Aks/Aks.Test/ScenarioTests/AksTestRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected AksTestRunner(ITestOutputHelper output)
6262
}
6363
).WithMockContextAction(mockContext =>
6464
{
65-
if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Playback)
65+
if (HttpMockServer.Mode == HttpRecorderMode.Playback)
6666
{
6767
AzureSession.Instance.DataStore = new MemoryDataStore();
6868
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
@@ -72,7 +72,7 @@ protected AksTestRunner(ITestOutputHelper output)
7272
var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""foo"",""client_secret"":""bar""}}";
7373
AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput);
7474
}
75-
else if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Record)
75+
else if (HttpMockServer.Mode == HttpRecorderMode.Record)
7676
{
7777
AzureSession.Instance.DataStore = new MemoryDataStore();
7878
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

tools/TestFx/DelegatingHandlers/HttpMockServer.cs

Lines changed: 11 additions & 34 deletions
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; internal set; }
38+
public static HttpRecorderMode Mode { get; private 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; }
@@ -45,32 +45,26 @@ private HttpMockServer()
4545
{
4646
}
4747

48-
public static void Initialize(Type callerIdentity, string testIdentity)
49-
{
50-
Initialize(callerIdentity, testIdentity, GetCurrentMode());
51-
}
52-
5348
public static void Initialize(string callerIdentity, string testIdentity)
54-
{
55-
Initialize(callerIdentity, testIdentity, GetCurrentMode());
56-
}
57-
58-
public static void Initialize(Type callerIdentity, string testIdentity, HttpRecorderMode mode)
59-
{
60-
Initialize(callerIdentity.Name, testIdentity, mode);
61-
}
62-
63-
public static void Initialize(string callerIdentity, string testIdentity, HttpRecorderMode mode)
6449
{
6550
_servers = new List<HttpMockServer>();
6651
_assetNames = new AssetNames();
6752
_records = new Records(Matcher);
6853

6954
CallerIdentity = callerIdentity;
7055
TestIdentity = testIdentity;
71-
Mode = mode;
7256
Variables = new Dictionary<string, string>();
7357

58+
string testMode = FileSystemUtilsObject?.GetEnvironmentVariable(ConnectionStringKeys.AZURE_TEST_MODE_ENVKEY);
59+
if (Enum.TryParse(testMode, true, out HttpRecorderMode recorderMode) && Enum.IsDefined(typeof(HttpRecorderMode), recorderMode))
60+
{
61+
Mode = recorderMode;
62+
}
63+
else
64+
{
65+
Mode = HttpRecorderMode.Playback;
66+
}
67+
7468
if (Mode == HttpRecorderMode.Playback)
7569
{
7670
var recordDir = Path.Combine(RecordsDirectory, CallerIdentity);
@@ -277,23 +271,6 @@ public static string Flush(string outputPath = null)
277271

278272
return fileName;
279273
}
280-
281-
public static HttpRecorderMode GetCurrentMode()
282-
{
283-
HttpRecorderMode mode;
284-
string input = FileSystemUtilsObject?.GetEnvironmentVariable(ConnectionStringKeys.AZURE_TEST_MODE_ENVKEY);
285-
286-
if (string.IsNullOrEmpty(input))
287-
{
288-
mode = HttpRecorderMode.Playback;
289-
}
290-
else
291-
{
292-
mode = (HttpRecorderMode)Enum.Parse(typeof(HttpRecorderMode), input, true);
293-
}
294-
295-
return mode;
296-
}
297274
}
298275

299276
public enum HttpRecorderMode

tools/TestFx/TestEnvironment.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public TestEnvironment(string connectionString)
131131
GraphUri = new Uri(ConnectionString.GetValue(ConnectionStringKeys.GraphUriKey));
132132
}
133133

134-
SetupHttpRecorderMode();
135134
SetupTokenDictionary();
136135
}
137136

@@ -160,28 +159,6 @@ private void LoadDefaultEnvironmentEndpoints()
160159
};
161160
}
162161

163-
private void SetupHttpRecorderMode()
164-
{
165-
string testMode = ConnectionString.GetValue(ConnectionStringKeys.HttpRecorderModeKey);
166-
if (string.IsNullOrEmpty(testMode))
167-
{
168-
testMode = Environment.GetEnvironmentVariable(ConnectionStringKeys.AZURE_TEST_MODE_ENVKEY);
169-
}
170-
171-
// Ideally we should be throwing when incompatible environment (e.g. Environment=Foo) is provided in connection string
172-
// But currently we do not throw
173-
if (Enum.TryParse(testMode, out HttpRecorderMode recorderMode))
174-
{
175-
HttpMockServer.Mode = recorderMode;
176-
}
177-
else
178-
{
179-
// Log incompatible recorder mode
180-
// Currently we set Playback as default recorder mode
181-
HttpMockServer.Mode = HttpRecorderMode.Playback;
182-
}
183-
}
184-
185162
private void SetupTokenDictionary()
186163
{
187164
TokenInfo = new Dictionary<TokenAudience, TokenCredentials>();

0 commit comments

Comments
 (0)