Skip to content

Test controller update #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/TestFx/ITestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;

namespace Microsoft.Azure.Commands.TestFx
{
public interface ITestRunner
{
void RunTestScript(params string[] scripts);
void RunTestScript(Action setUp, Action tearDown, params string[] scripts);
}
}
1 change: 1 addition & 0 deletions src/TestFx/ITestRunnerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public interface ITestRunnerFactory
ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]> buildModuleList);
ITestRunnerFactory WithExtraUserAgentsToIgnore(Dictionary<string, string> userAgentsToIgnore);
ITestRunnerFactory WithRecordMatcher(RecordMatcherDelegate recordMatcher);
ITestRunnerFactory WithNewRecordMatcherArguments(Dictionary<string, string> userAgentsToIgnore, Dictionary<string, string> resourceProviders);
}
}
100 changes: 67 additions & 33 deletions src/TestFx/TestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class TestManager : ITestRunnerFactory, ITestRunner
private readonly string _callingClassName;
private string _projectSubfolderForTestsName = null;
private string _newPsScriptFilename = null;
private Dictionary<string, string> _userAgentsToIgnore;
private Dictionary<string, string> _matcherExtraUserAgentsToIgnore;
private Dictionary<string, string> _matcherNewUserAgentsToIgnore;
private Dictionary<string, string> _matcherResourceProviders;
protected EnvironmentSetupHelper Helper;
protected readonly List<string> RmModules;
protected readonly List<string> CommonPsScripts = new List<string>();
Expand Down Expand Up @@ -140,6 +142,19 @@ public ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]
return this;
}

/// <summary>
/// Set new argumets for the mock server record matcher
/// </summary>
/// <param name="userAgentsToIgnore">Dictionary [userAgent:apiVersion] to ignore</param>
/// <param name="resourceProviders">Dictionary [resouceProvider:apiVersion] to match</param>
/// <returns></returns>
public ITestRunnerFactory WithNewRecordMatcherArguments(Dictionary<string, string> userAgentsToIgnore, Dictionary<string, string> resourceProviders)
{
_matcherNewUserAgentsToIgnore = userAgentsToIgnore;
_matcherResourceProviders = resourceProviders;
return this;
}

/// <summary>
/// Sets a new HttpMockServer.Matcher implementation. By defauls it's PermissiveRecordMatcherWithApiExclusion
/// </summary>
Expand All @@ -161,7 +176,7 @@ public ITestRunnerFactory WithRecordMatcher(RecordMatcherDelegate recordMatcher)
/// <returns>self</returns>
public ITestRunnerFactory WithExtraUserAgentsToIgnore(Dictionary<string, string> userAgentsToIgnore)
{
_userAgentsToIgnore = userAgentsToIgnore;
_matcherExtraUserAgentsToIgnore = userAgentsToIgnore;
return this;
}

Expand Down Expand Up @@ -197,6 +212,13 @@ public void RunTestScript(params string[] scripts)
}
}

public void RunTestScript(Action setUp, Action tearDown, params string[] scripts)
{
setUp?.Invoke();
RunTestScript(scripts);
tearDown?.Invoke();
}

#endregion

#region Helpers
Expand Down Expand Up @@ -237,33 +259,39 @@ protected void SetupAzureContext()
const string domainKey = "Domain";
const string subscriptionIdKey = "SubscriptionId";
const string undefined = "Undefined";
var zeroGuild = Guid.Empty.ToString();
var zeroGuid = Guid.Empty.ToString();
const string dummyGuid = "395544B0-BF41-429D-921F-E1CA2252FCF4";

string tenantId = null;
string userDomain = null;
string subscriptionId = null;

if (HttpMockServer.Mode == HttpRecorderMode.Record)
{
var environment = TestEnvironmentFactory.GetTestEnvironment();
tenantId = environment.Tenant;
userDomain = string.IsNullOrEmpty(environment.UserName)
? string.Empty
: environment.UserName.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries).Last();

subscriptionId = environment.SubscriptionId;
}
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
switch (HttpMockServer.Mode)
{
tenantId = HttpMockServer.Variables.ContainsKey(tenantIdKey)
? HttpMockServer.Variables[tenantIdKey]
: zeroGuild;
userDomain = HttpMockServer.Variables.ContainsKey(domainKey)
? HttpMockServer.Variables[domainKey]
: "testdomain.onmicrosoft.com";
subscriptionId = HttpMockServer.Variables.ContainsKey(subscriptionIdKey)
? HttpMockServer.Variables[subscriptionIdKey]
: zeroGuild;
case HttpRecorderMode.Record:
var environment = TestEnvironmentFactory.GetTestEnvironment();
tenantId = environment.Tenant;
userDomain = string.IsNullOrEmpty(environment.UserName)
? string.Empty
: environment.UserName.Split(new[] { "@" }, StringSplitOptions.RemoveEmptyEntries).Last();

subscriptionId = environment.SubscriptionId;
break;
case HttpRecorderMode.Playback:
tenantId = HttpMockServer.Variables.ContainsKey(tenantIdKey)
? HttpMockServer.Variables[tenantIdKey]
: dummyGuid;
userDomain = HttpMockServer.Variables.ContainsKey(domainKey)
? HttpMockServer.Variables[domainKey]
: "testdomain.onmicrosoft.com";
subscriptionId = HttpMockServer.Variables.ContainsKey(subscriptionIdKey)
? HttpMockServer.Variables[subscriptionIdKey]
: zeroGuid;
break;
case HttpRecorderMode.None:
break;
default:
throw new ArgumentOutOfRangeException();
}

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

protected void SetupMockServerMatcher()
{
var resourceProviders = new Dictionary<string, string>
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Providers.Test", null},
};

var userAgentsToIgnore = new Dictionary<string, string>
var resourceProviders = _matcherResourceProviders?.Count > 0
? _matcherResourceProviders
: new Dictionary<string, string> // default
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Providers.Test", null},
};

var extraUserAgentsToIgnore = new Dictionary<string, string> // default
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
};

_userAgentsToIgnore?.Keys.ForEach(k=> userAgentsToIgnore.Add(k, _userAgentsToIgnore[k]));
_matcherExtraUserAgentsToIgnore?.Keys.ForEach(k => extraUserAgentsToIgnore.Add(k, _matcherExtraUserAgentsToIgnore[k])); //extra

var userAgentsToIgnore = _matcherNewUserAgentsToIgnore?.Count > 0
? _matcherNewUserAgentsToIgnore
: extraUserAgentsToIgnore;

HttpMockServer.Matcher = RecordMatcher(true, resourceProviders, userAgentsToIgnore);
}
Expand Down