@@ -37,7 +37,9 @@ public class TestManager : ITestRunnerFactory, ITestRunner
37
37
private readonly string _callingClassName ;
38
38
private string _projectSubfolderForTestsName = null ;
39
39
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 ;
41
43
protected EnvironmentSetupHelper Helper ;
42
44
protected readonly List < string > RmModules ;
43
45
protected readonly List < string > CommonPsScripts = new List < string > ( ) ;
@@ -140,6 +142,19 @@ public ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]
140
142
return this ;
141
143
}
142
144
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
+
143
158
/// <summary>
144
159
/// Sets a new HttpMockServer.Matcher implementation. By defauls it's PermissiveRecordMatcherWithApiExclusion
145
160
/// </summary>
@@ -161,7 +176,7 @@ public ITestRunnerFactory WithRecordMatcher(RecordMatcherDelegate recordMatcher)
161
176
/// <returns>self</returns>
162
177
public ITestRunnerFactory WithExtraUserAgentsToIgnore ( Dictionary < string , string > userAgentsToIgnore )
163
178
{
164
- _userAgentsToIgnore = userAgentsToIgnore ;
179
+ _matcherExtraUserAgentsToIgnore = userAgentsToIgnore ;
165
180
return this ;
166
181
}
167
182
@@ -197,6 +212,13 @@ public void RunTestScript(params string[] scripts)
197
212
}
198
213
}
199
214
215
+ public void RunTestScript ( Action setUp , Action tearDown , params string [ ] scripts )
216
+ {
217
+ setUp ? . Invoke ( ) ;
218
+ RunTestScript ( scripts ) ;
219
+ tearDown ? . Invoke ( ) ;
220
+ }
221
+
200
222
#endregion
201
223
202
224
#region Helpers
@@ -237,33 +259,39 @@ protected void SetupAzureContext()
237
259
const string domainKey = "Domain" ;
238
260
const string subscriptionIdKey = "SubscriptionId" ;
239
261
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" ;
241
264
242
265
string tenantId = null ;
243
266
string userDomain = null ;
244
267
string subscriptionId = null ;
245
268
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 )
257
270
{
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 ( ) ;
267
295
}
268
296
269
297
AzureRmProfileProvider . Instance . Profile . DefaultContext . Tenant . Id = tenantId ?? undefined ;
@@ -273,20 +301,26 @@ protected void SetupAzureContext()
273
301
274
302
protected void SetupMockServerMatcher ( )
275
303
{
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
285
315
{
286
316
{ "Microsoft.Azure.Management.Resources.ResourceManagementClient" , "2016-02-01" } ,
287
317
} ;
288
318
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 ;
290
324
291
325
HttpMockServer . Matcher = RecordMatcher ( true , resourceProviders , userAgentsToIgnore ) ;
292
326
}
0 commit comments