Skip to content

Commit 47627c3

Browse files
authored
Merge branch 'master' into tenant-domain-name
2 parents 20c0944 + 84adc2e commit 47627c3

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
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 & 34 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,38 @@ 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;
245-
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)
268+
switch (HttpMockServer.Mode)
257269
{
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;
270+
case HttpRecorderMode.Record:
271+
var environment = TestEnvironmentFactory.GetTestEnvironment();
272+
tenantId = environment.Tenant;
273+
userDomain = string.IsNullOrEmpty(environment.UserName)
274+
? string.Empty
275+
: environment.UserName.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries).Last();
276+
277+
subscriptionId = environment.SubscriptionId;
278+
break;
279+
case HttpRecorderMode.Playback:
280+
tenantId = HttpMockServer.Variables.ContainsKey(tenantIdKey)
281+
? HttpMockServer.Variables[tenantIdKey]
282+
: dummyGuid;
283+
userDomain = HttpMockServer.Variables.ContainsKey(domainKey)
284+
? HttpMockServer.Variables[domainKey]
285+
: "testdomain.onmicrosoft.com";
286+
subscriptionId = HttpMockServer.Variables.ContainsKey(subscriptionIdKey)
287+
? HttpMockServer.Variables[subscriptionIdKey]
288+
: zeroGuid;
289+
break;
290+
case HttpRecorderMode.None:
291+
break;
292+
default:
293+
throw new ArgumentOutOfRangeException();
267294
}
268295

269296
AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id = tenantId ?? undefined;
@@ -272,20 +299,26 @@ protected void SetupAzureContext()
272299

273300
protected void SetupMockServerMatcher()
274301
{
275-
var resourceProviders = new Dictionary<string, string>
276-
{
277-
{"Microsoft.Resources", null},
278-
{"Microsoft.Features", null},
279-
{"Microsoft.Authorization", null},
280-
{"Providers.Test", null},
281-
};
282-
283-
var userAgentsToIgnore = new Dictionary<string, string>
302+
var resourceProviders = _matcherResourceProviders?.Count > 0
303+
? _matcherResourceProviders
304+
: new Dictionary<string, string> // default
305+
{
306+
{"Microsoft.Resources", null},
307+
{"Microsoft.Features", null},
308+
{"Microsoft.Authorization", null},
309+
{"Providers.Test", null},
310+
};
311+
312+
var extraUserAgentsToIgnore = new Dictionary<string, string> // default
284313
{
285314
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
286315
};
287316

288-
_userAgentsToIgnore?.Keys.ForEach(k=> userAgentsToIgnore.Add(k, _userAgentsToIgnore[k]));
317+
_matcherExtraUserAgentsToIgnore?.Keys.ForEach(k => extraUserAgentsToIgnore.Add(k, _matcherExtraUserAgentsToIgnore[k])); //extra
318+
319+
var userAgentsToIgnore = _matcherNewUserAgentsToIgnore?.Count > 0
320+
? _matcherNewUserAgentsToIgnore
321+
: extraUserAgentsToIgnore;
289322

290323
HttpMockServer.Matcher = RecordMatcher(true, resourceProviders, userAgentsToIgnore);
291324
}

0 commit comments

Comments
 (0)