diff --git a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertPRuleTests.cs b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertPRuleTests.cs index 18d000ea6e36..23f076dabcb5 100644 --- a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertPRuleTests.cs +++ b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertPRuleTests.cs @@ -12,45 +12,36 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using System; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests { - public class AlertPRuleTests : RMTestBase + public class AlertPRuleTests : AlertsManagementTestRunner { - public XunitTracingInterceptor _logger; - - public AlertPRuleTests(Xunit.Abstractions.ITestOutputHelper output) + public AlertPRuleTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAlertProcessingRulesByResourceGroupName() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetAlertProcessingRulesByResourceGroupName"); + TestRunner.RunTestScript("Test-GetAlertProcessingRulesByResourceGroupName"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateUpdateAndDeleteSuppressionRule() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateUpdateAndDeleteSuppressionRule"); + TestRunner.RunTestScript("Test-CreateUpdateAndDeleteSuppressionRule"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateUpdateAndDeleteActionGroupRule() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateUpdateAndDeleteActionGroupRule"); + TestRunner.RunTestScript("Test-CreateUpdateAndDeleteActionGroupRule"); } - } } \ No newline at end of file diff --git a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertTests.cs b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertTests.cs index f865b822bd90..0eb132468db8 100644 --- a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertTests.cs +++ b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertTests.cs @@ -12,43 +12,36 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using System; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Microsoft.Azure.ServiceManagement.Common.Models; namespace Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests { - public class AlertTests : RMTestBase + public class AlertTests : AlertsManagementTestRunner { - public XunitTracingInterceptor _logger; - - public AlertTests(Xunit.Abstractions.ITestOutputHelper output) + public AlertTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAlertChangeState() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AlertChangeState"); + TestRunner.RunTestScript("Test-AlertChangeState"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAlertsFilteredByParameters() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetAlertsFilteredByParameters"); + TestRunner.RunTestScript("Test-GetAlertsFilteredByParameters"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAlertsSummary() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AlertsSummary"); + TestRunner.RunTestScript("Test-AlertsSummary"); } } } \ No newline at end of file diff --git a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertsManagementTestRunner.cs b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertsManagementTestRunner.cs new file mode 100644 index 000000000000..4eb851a63a6d --- /dev/null +++ b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/AlertsManagementTestRunner.cs @@ -0,0 +1,53 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System.Collections.Generic; +using Microsoft.Azure.Commands.TestFx; +using Xunit.Abstractions; + +namespace Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests +{ + public class AlertsManagementTestRunner + { + protected readonly ITestRunner TestRunner; + + protected AlertsManagementTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"Common.ps1", + @"../AzureRM.Resources.ps1", + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.AlertsManagement.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}, + }, + resourceProviders: new Dictionary + { + {"Microsoft.AlertsManagement", null} + } + ) + .Build(); + } + } +} \ No newline at end of file diff --git a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/SmartGroupTests.cs b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/SmartGroupTests.cs index 1a8b4fe46572..7f95fed91224 100644 --- a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/SmartGroupTests.cs +++ b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/SmartGroupTests.cs @@ -12,29 +12,22 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using System; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Microsoft.Azure.ServiceManagement.Common.Models; namespace Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests { - public class SmartGroupTests : RMTestBase + public class SmartGroupTests : AlertsManagementTestRunner { - public XunitTracingInterceptor _logger; - - public SmartGroupTests(Xunit.Abstractions.ITestOutputHelper output) + public SmartGroupTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSmartGroupChangeState() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-SmartGroupChangeState"); + TestRunner.RunTestScript("Test-SmartGroupChangeState"); } } } \ No newline at end of file diff --git a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/TestController.cs b/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 9c38f3b6d0a5..000000000000 --- a/src/AlertsManagement/AlertsManagement.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,126 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// Copyright Microsoft Corporation -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ---------------------------------------------------------------------------------- - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using Microsoft.Azure.Management.AlertsManagement; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public AlertsManagementClient AlertsManagementClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - ResourceManagementClient = GetResourceManagementClient(context); - AlertsManagementClient = GetBillingManagementClient(context); - - _helper.SetupManagementClients( - ResourceManagementClient, - AlertsManagementClient); - } - - public void RunPowerShellTest(ServiceManagement.Common.Models.XunitTracingInterceptor logger, params string[] scripts) - { - var sf = new StackTrace().GetFrame(1); - var callingClassType = sf.GetMethod().ReflectedType?.ToString(); - var mockName = sf.GetMethod().Name; - - _helper.TracingInterceptor = logger; - RunPsTestWorkflow( - () => scripts, - // no custom cleanup - null, - callingClassType, - mockName); - } - - public void RunPsTestWorkflow( - Func scriptBuilder, - Action cleanup, - string callingClassType, - string mockName) - { - var providers = new Dictionary - { - {"Microsoft.AlertsManagement", null} - }; - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore); - HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords"); - - using (var context = MockContext.Start(callingClassType, mockName)) - { - SetupManagementClients(context); - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - _helper.SetupModules(AzureModule.AzureResourceManager, - _helper.RMProfileModule, - _helper.GetRMModulePath("AzureRM.AlertsManagement.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - "AzureRM.Resources.ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static AlertsManagementClient GetBillingManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}