Skip to content

Commit d716aa9

Browse files
Zero guid replaced with dummy guid for the tenantId to avoid wrong Azure env exception from KeyVault cmdlets. pre and post action params added to runScript method.
1 parent 21b5b5a commit d716aa9

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

src/TestFx/ITestRunner.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
1517
namespace Microsoft.Azure.Commands.TestFx
1618
{
1719
public interface ITestRunner
1820
{
1921
void RunTestScript(params string[] scripts);
22+
void RunTestScript(Action setUp, Action tearDown, params string[] scripts);
2023
}
2124
}

src/TestFx/ITestRunnerFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ public interface ITestRunnerFactory
2828
ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]> buildModuleList);
2929
ITestRunnerFactory WithExtraUserAgentsToIgnore(Dictionary<string, string> userAgentsToIgnore);
3030
ITestRunnerFactory WithRecordMatcher(RecordMatcherDelegate recordMatcher);
31+
ITestRunnerFactory WithNewRecordMatcherArguments(Dictionary<string, string> userAgentsToIgnore, Dictionary<string, string> resourceProviders);
3132
}
3233
}

src/TestFx/TestManager.cs

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public class TestManager : ITestRunnerFactory, ITestRunner
3737
private readonly string _callingClassName;
3838
private string _projectSubfolderForTestsName = null;
3939
private string _newPsScriptFilename = null;
40-
private Dictionary<string, string> _userAgentsToIgnore;
40+
private Dictionary<string, string> _matcherExtraUserAgentsToIgnore;
41+
private Dictionary<string, string> _matcherNewUserAgentsToIgnore;
42+
private Dictionary<string, string> _matcherResourceProviders;
4143
protected EnvironmentSetupHelper Helper;
4244
protected readonly List<string> RmModules;
4345
protected readonly List<string> CommonPsScripts = new List<string>();
@@ -140,6 +142,19 @@ public ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]
140142
return this;
141143
}
142144

145+
/// <summary>
146+
/// Set new argumets for the mock server record matcher
147+
/// </summary>
148+
/// <param name="userAgentsToIgnore">Dictionary [userAgent:apiVersion] to ignore</param>
149+
/// <param name="resourceProviders">Dictionary [resouceProvider:apiVersion] to match</param>
150+
/// <returns></returns>
151+
public ITestRunnerFactory WithNewRecordMatcherArguments(Dictionary<string, string> userAgentsToIgnore, Dictionary<string, string> resourceProviders)
152+
{
153+
_matcherNewUserAgentsToIgnore = userAgentsToIgnore;
154+
_matcherResourceProviders = resourceProviders;
155+
return this;
156+
}
157+
143158
/// <summary>
144159
/// Sets a new HttpMockServer.Matcher implementation. By defauls it's PermissiveRecordMatcherWithApiExclusion
145160
/// </summary>
@@ -161,7 +176,7 @@ public ITestRunnerFactory WithRecordMatcher(RecordMatcherDelegate recordMatcher)
161176
/// <returns>self</returns>
162177
public ITestRunnerFactory WithExtraUserAgentsToIgnore(Dictionary<string, string> userAgentsToIgnore)
163178
{
164-
_userAgentsToIgnore = userAgentsToIgnore;
179+
_matcherExtraUserAgentsToIgnore = userAgentsToIgnore;
165180
return this;
166181
}
167182

@@ -197,6 +212,13 @@ public void RunTestScript(params string[] scripts)
197212
}
198213
}
199214

215+
public void RunTestScript(Action setUp, Action tearDown, params string[] scripts)
216+
{
217+
setUp?.Invoke();
218+
RunTestScript(scripts);
219+
tearDown?.Invoke();
220+
}
221+
200222
#endregion
201223

202224
#region Helpers
@@ -237,33 +259,39 @@ protected void SetupAzureContext()
237259
const string domainKey = "Domain";
238260
const string subscriptionIdKey = "SubscriptionId";
239261
const string undefined = "Undefined";
240-
var zeroGuild = Guid.Empty.ToString();
262+
var zeroGuid = Guid.Empty.ToString();
263+
const string dummyGuid = "395544B0-BF41-429D-921F-E1CA2252FCF4";
241264

242265
string tenantId = null;
243266
string userDomain = null;
244267
string subscriptionId = null;
245268

246-
if (HttpMockServer.Mode == HttpRecorderMode.Record)
247-
{
248-
var environment = TestEnvironmentFactory.GetTestEnvironment();
249-
tenantId = environment.Tenant;
250-
userDomain = string.IsNullOrEmpty(environment.UserName)
251-
? string.Empty
252-
: environment.UserName.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries).Last();
253-
254-
subscriptionId = environment.SubscriptionId;
255-
}
256-
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
269+
switch (HttpMockServer.Mode)
257270
{
258-
tenantId = HttpMockServer.Variables.ContainsKey(tenantIdKey)
259-
? HttpMockServer.Variables[tenantIdKey]
260-
: zeroGuild;
261-
userDomain = HttpMockServer.Variables.ContainsKey(domainKey)
262-
? HttpMockServer.Variables[domainKey]
263-
: "testdomain.onmicrosoft.com";
264-
subscriptionId = HttpMockServer.Variables.ContainsKey(subscriptionIdKey)
265-
? HttpMockServer.Variables[subscriptionIdKey]
266-
: zeroGuild;
271+
case HttpRecorderMode.Record:
272+
var environment = TestEnvironmentFactory.GetTestEnvironment();
273+
tenantId = environment.Tenant;
274+
userDomain = string.IsNullOrEmpty(environment.UserName)
275+
? string.Empty
276+
: environment.UserName.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries).Last();
277+
278+
subscriptionId = environment.SubscriptionId;
279+
break;
280+
case HttpRecorderMode.Playback:
281+
tenantId = HttpMockServer.Variables.ContainsKey(tenantIdKey)
282+
? HttpMockServer.Variables[tenantIdKey]
283+
: dummyGuid;
284+
userDomain = HttpMockServer.Variables.ContainsKey(domainKey)
285+
? HttpMockServer.Variables[domainKey]
286+
: "testdomain.onmicrosoft.com";
287+
subscriptionId = HttpMockServer.Variables.ContainsKey(subscriptionIdKey)
288+
? HttpMockServer.Variables[subscriptionIdKey]
289+
: zeroGuid;
290+
break;
291+
case HttpRecorderMode.None:
292+
break;
293+
default:
294+
throw new ArgumentOutOfRangeException();
267295
}
268296

269297
AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id = tenantId ?? undefined;
@@ -273,20 +301,26 @@ protected void SetupAzureContext()
273301

274302
protected void SetupMockServerMatcher()
275303
{
276-
var resourceProviders = new Dictionary<string, string>
277-
{
278-
{"Microsoft.Resources", null},
279-
{"Microsoft.Features", null},
280-
{"Microsoft.Authorization", null},
281-
{"Providers.Test", null},
282-
};
283-
284-
var userAgentsToIgnore = new Dictionary<string, string>
304+
var resourceProviders = _matcherResourceProviders?.Count > 0
305+
? _matcherResourceProviders
306+
: new Dictionary<string, string> // default
307+
{
308+
{"Microsoft.Resources", null},
309+
{"Microsoft.Features", null},
310+
{"Microsoft.Authorization", null},
311+
{"Providers.Test", null},
312+
};
313+
314+
var extraUserAgentsToIgnore = new Dictionary<string, string> // default
285315
{
286316
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
287317
};
288318

289-
_userAgentsToIgnore?.Keys.ForEach(k=> userAgentsToIgnore.Add(k, _userAgentsToIgnore[k]));
319+
_matcherExtraUserAgentsToIgnore?.Keys.ForEach(k => extraUserAgentsToIgnore.Add(k, _matcherExtraUserAgentsToIgnore[k])); //extra
320+
321+
var userAgentsToIgnore = _matcherNewUserAgentsToIgnore?.Count > 0
322+
? _matcherNewUserAgentsToIgnore
323+
: extraUserAgentsToIgnore;
290324

291325
HttpMockServer.Matcher = RecordMatcher(true, resourceProviders, userAgentsToIgnore);
292326
}

0 commit comments

Comments
 (0)