diff --git a/src/Advisor/Advisor.Test/ScenarioTests/AdvisorTestRunner.cs b/src/Advisor/Advisor.Test/ScenarioTests/AdvisorTestRunner.cs new file mode 100644 index 000000000000..a84b297f128c --- /dev/null +++ b/src/Advisor/Advisor.Test/ScenarioTests/AdvisorTestRunner.cs @@ -0,0 +1,54 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Internal.Subscriptions; +using Microsoft.Azure.Management.Advisor; +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; +using Xunit.Abstractions; + +namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests +{ + public class AdvisorTestRunner + { + protected readonly ITestRunner TestRunner; + + protected AdvisorTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance (output) + .WithNewPsScriptFilename ($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests ("ScenarioTests") + .WithCommonPsScripts (new[] + { + @"Common.ps1" + }) + .WithNewRmModules (helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.Advisor.psd1") + }) + .WithNewRecordMatcherArguments ( + userAgentsToIgnore: new Dictionary(), + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Authorization", null} + } + ) + .Build(); + } + } +} diff --git a/src/Advisor/Advisor.Test/ScenarioTests/DisableAzAdvisorRecommendation.cs b/src/Advisor/Advisor.Test/ScenarioTests/DisableAzAdvisorRecommendation.cs index 3302a14e044c..703c8e05ef3c 100644 --- a/src/Advisor/Advisor.Test/ScenarioTests/DisableAzAdvisorRecommendation.cs +++ b/src/Advisor/Advisor.Test/ScenarioTests/DisableAzAdvisorRecommendation.cs @@ -12,54 +12,44 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests { - public class DisableAzAdvisorRecommendation + public class DisableAzAdvisorRecommendation : AdvisorTestRunner { - private readonly XunitTracingInterceptor _logger; - - public DisableAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) + public DisableAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } - [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void DisableAzAdvisorRecommendationBadUserInput() { - TestController.NewInstance.RunPowerShellTest(_logger, "Disable-AzAdvisorRecommendationBadUserInput-Negative"); + TestRunner.RunTestScript("Disable-AzAdvisorRecommendationBadUserInput-Negative"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void DisableAzAdvisorRecommendationByNameParameter() { - TestController.NewInstance.RunPowerShellTest(_logger, "Disable-AzAdvisorRecommendationByNameParameter"); + TestRunner.RunTestScript("Disable-AzAdvisorRecommendationByNameParameter"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void DisableAzAdvisorRecommendationByIdParameter() { - TestController.NewInstance.RunPowerShellTest(_logger, "Disable-AzAdvisorRecommendationByIdParameter"); + TestRunner.RunTestScript("Disable-AzAdvisorRecommendationByIdParameter"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void DisableAzAdvisorRecommendationPipelineScenario() { - TestController.NewInstance.RunPowerShellTest(_logger, "Disable-AzAdvisorRecommendationPipelineScenario"); + TestRunner.RunTestScript("Disable-AzAdvisorRecommendationPipelineScenario"); } } } diff --git a/src/Advisor/Advisor.Test/ScenarioTests/EnableAzAdvisorRecommendation.cs b/src/Advisor/Advisor.Test/ScenarioTests/EnableAzAdvisorRecommendation.cs index 514308fc5965..56bbae4de0a8 100644 --- a/src/Advisor/Advisor.Test/ScenarioTests/EnableAzAdvisorRecommendation.cs +++ b/src/Advisor/Advisor.Test/ScenarioTests/EnableAzAdvisorRecommendation.cs @@ -12,46 +12,37 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests { - public class EnableAzAdvisorRecommendation + public class EnableAzAdvisorRecommendation : AdvisorTestRunner { - private readonly XunitTracingInterceptor _logger; - - public EnableAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) + public EnableAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void EnableAzAdvisorRecommendationByNameParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Enable-AzAdvisorRecommendationByNameParameterSet"); + TestRunner.RunTestScript("Enable-AzAdvisorRecommendationByNameParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void EnableAzAdvisorRecommendationByIdParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Enable-AzAdvisorRecommendationByIdParameterSet"); + TestRunner.RunTestScript("Enable-AzAdvisorRecommendationByIdParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void EnableAzAdvisorRecommendationPipeline() { - TestController.NewInstance.RunPowerShellTest(_logger, "Enable-AzAdvisorRecommendationPipeline"); + TestRunner.RunTestScript("Enable-AzAdvisorRecommendationPipeline"); } } } diff --git a/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorConfiguration.cs b/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorConfiguration.cs index afa42de45c5b..d7b5e43498f2 100644 --- a/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorConfiguration.cs +++ b/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorConfiguration.cs @@ -12,32 +12,23 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests { - public class GetAzAdvisorConfiguration + public class GetAzAdvisorConfiguration : AdvisorTestRunner { - private readonly XunitTracingInterceptor _logger; - - public GetAzAdvisorConfiguration(Xunit.Abstractions.ITestOutputHelper output) + public GetAzAdvisorConfiguration(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetConfigurationQuery() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzAdvisorConfigurationNoParameter"); + TestRunner.RunTestScript("Get-AzAdvisorConfigurationNoParameter"); } } } diff --git a/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorRecommendation.cs b/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorRecommendation.cs index 12bf61c158d1..d6f91bdba64a 100644 --- a/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorRecommendation.cs +++ b/src/Advisor/Advisor.Test/ScenarioTests/GetAzAdvisorRecommendation.cs @@ -12,52 +12,43 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests { - public class GetAzAdvisorRecommendation + public class GetAzAdvisorRecommendation : AdvisorTestRunner { - private readonly XunitTracingInterceptor _logger; - - public GetAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) + public GetAzAdvisorRecommendation(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzAdvisorRecommendationNoParameter() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzAdvisorRecommendationNoParameter"); + TestRunner.RunTestScript("Get-AzAdvisorRecommendationNoParameter"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzAdvisorRecommendationByCategory() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzAdvisorRecommendationByCategory"); + TestRunner.RunTestScript("Get-AzAdvisorRecommendationByCategory"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzAdvisorRecommendationByNameParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzAdvisorRecommendationByNameParameterSet"); + TestRunner.RunTestScript("Get-AzAdvisorRecommendationByNameParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzAdvisorRecommendationByIdParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzAdvisorRecommendationByIdParameterSet"); + TestRunner.RunTestScript("Get-AzAdvisorRecommendationByIdParameterSet"); } } } diff --git a/src/Advisor/Advisor.Test/ScenarioTests/SetAzAdvisorConfiguration.cs b/src/Advisor/Advisor.Test/ScenarioTests/SetAzAdvisorConfiguration.cs index 53b0f595cbbc..583bf5b0bc0f 100644 --- a/src/Advisor/Advisor.Test/ScenarioTests/SetAzAdvisorConfiguration.cs +++ b/src/Advisor/Advisor.Test/ScenarioTests/SetAzAdvisorConfiguration.cs @@ -12,53 +12,44 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; -using Xunit.Abstractions; namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests { - public class SetAzAdvisorConfiguration + public class SetAzAdvisorConfiguration : AdvisorTestRunner { - private readonly XunitTracingInterceptor _logger; - - public SetAzAdvisorConfiguration(Xunit.Abstractions.ITestOutputHelper output) + public SetAzAdvisorConfiguration(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationBadUserInputLowCpu() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationBadUserInputLowCpu-Negative"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationBadUserInputLowCpu-Negative"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationByLowCpu() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationWithLowCpu"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationWithLowCpu"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationByLowCpuExclude() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationByLowCpuExclude"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationByLowCpuExclude"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationPipelineByLowCpuExclude() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationPipelineByLowCpuExclude"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationPipelineByLowCpuExclude"); } // ResourceGroupParameterSets @@ -66,14 +57,14 @@ public void SetAzAdvisorConfigurationPipelineByLowCpuExclude() [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationByRg() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationWithRg"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationWithRg"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void SetAzAdvisorConfigurationByRgExclude() { - TestController.NewInstance.RunPowerShellTest(_logger, "Set-AzAdvisorConfigurationByRgExclude"); + TestRunner.RunTestScript("Set-AzAdvisorConfigurationByRgExclude"); } } diff --git a/src/Advisor/Advisor.Test/ScenarioTests/TestController.cs b/src/Advisor/Advisor.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 94cb86cf8ab6..000000000000 --- a/src/Advisor/Advisor.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,99 +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 Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Internal.Subscriptions; -using Microsoft.Azure.Management.Advisor; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; - -namespace Microsoft.Azure.Commands.Advisor.Test.ScenarioTests -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public IAdvisorManagementClient AdvisorClient { get; private set; } - - public SubscriptionClient SubscriptionClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - public void RunPowerShellTest(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; - var providers = new Dictionary - { - { "Microsoft.Resources", null }, - { "Microsoft.Features", null }, - { "Microsoft.Authorization", null } - }; - var providersToIgnore = new Dictionary(); - 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(@"Az.Advisor.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1"); - - _helper.RunPowerShellTest(scripts); - } - } - - protected void SetupManagementClients(MockContext context) - { - AdvisorClient = GetResourceGraphClient(context); - SubscriptionClient = GetSubscriptionClient(context); - _helper.SetupManagementClients(AdvisorClient, SubscriptionClient); - } - - private static IAdvisorManagementClient GetResourceGraphClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static SubscriptionClient GetSubscriptionClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}