diff --git a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusHistoryTests.cs b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusHistoryTests.cs index 9f929895bbfe..370633e083f5 100644 --- a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusHistoryTests.cs +++ b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusHistoryTests.cs @@ -12,80 +12,71 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.ServiceManagement.Common.Models; - namespace Microsoft.Azure.Commands.GuestConfiguration.Test.ScenarioTests { - using Microsoft.Azure.Commands.ScenarioTest; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; - - public class GetVmGuestPolicyStatusHistoryTests + public class GetVmGuestPolicyStatusHistoryTests : GuestConfigurationTestRunner { - private readonly XunitTracingInterceptor _logger; - - public GetVmGuestPolicyStatusHistoryTests(Xunit.Abstractions.ITestOutputHelper output) + public GetVmGuestPolicyStatusHistoryTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void VmNameScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-VmNameScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-VmNameScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeIdScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-InitiativeIdScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-InitiativeIdScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeNameScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-InitiativeNameScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-InitiativeNameScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void ShowOnlyChangeSwitchVmNameScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-ShowOnlyChangeSwitch-VmNameScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-ShowOnlyChangeSwitch-VmNameScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void VmNameScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-VmNameScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-VmNameScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeIdScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-InitiativeIdScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-InitiativeIdScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeNameScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-InitiativeNameScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-InitiativeNameScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void ShowOnlyChangeSwitchVmNameScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatusHistory-ShowOnlyChangeSwitch-VmNameScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatusHistory-ShowOnlyChangeSwitch-VmNameScope_Custom"); } } } \ No newline at end of file diff --git a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusTests.cs b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusTests.cs index 213949a2750f..596e3b47dba4 100644 --- a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusTests.cs +++ b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GetVmGuestPolicyStatusTests.cs @@ -12,80 +12,71 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.ServiceManagement.Common.Models; - namespace Microsoft.Azure.Commands.GuestConfiguration.Test.ScenarioTests { - using Microsoft.Azure.Commands.ScenarioTest; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; - - public class GetVmGuestPolicyStatusTests + public class GetVmGuestPolicyStatusTests : GuestConfigurationTestRunner { - private readonly XunitTracingInterceptor _logger; - - public GetVmGuestPolicyStatusTests(Xunit.Abstractions.ITestOutputHelper output) + public GetVmGuestPolicyStatusTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void VmNameScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-VmNameScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-VmNameScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void VmNameScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-VmNameScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-VmNameScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeIdScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-InitiativeIdScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-InitiativeIdScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeIdScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-InitiativeIdScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-InitiativeIdScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeNameScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-InitiativeNameScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-InitiativeNameScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void InitiativeNameScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-InitiativeNameScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-InitiativeNameScope_Custom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void ReportIdScope() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-ReportIdScope"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-ReportIdScope"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void ReportIdScope_Custom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzVMGuestPolicyStatus-ReportIdScope_Custom"); + TestRunner.RunTestScript("Get-AzVMGuestPolicyStatus-ReportIdScope_Custom"); } } } \ No newline at end of file diff --git a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GuestConfigurationTestRunner.cs b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GuestConfigurationTestRunner.cs new file mode 100644 index 000000000000..61bc403a26e7 --- /dev/null +++ b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/GuestConfigurationTestRunner.cs @@ -0,0 +1,50 @@ +// ---------------------------------------------------------------------------------- +// +// 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.GuestConfiguration.Test.ScenarioTests +{ + public class GuestConfigurationTestRunner + { + protected readonly ITestRunner TestRunner; + + protected GuestConfigurationTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"Common.ps1" + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.GuestConfiguration.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary(), + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Authorization", null} + } + ) + .Build(); + } + } +} diff --git a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/TestController.cs b/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 2698b9f75e7a..000000000000 --- a/src/GuestConfiguration/GuestConfiguration.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,109 +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.Management.Internal.ResourceManager.Version2018_05_01; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.GuestConfiguration.Test.ScenarioTests -{ - 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.GuestConfiguration; - using Microsoft.Azure.Management.PolicyInsights_2018_04; - using Microsoft.Azure.Management.PolicyInsights_2018_04.Models; - using Microsoft.Azure.Test.HttpRecorder; - using Microsoft.Rest.ClientRuntime.Azure.TestFramework; - using Microsoft.WindowsAzure.Commands.ScenarioTest; - using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; - - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public GuestConfigurationClient GuestConfigurationClient { get; private set; } - - public PolicyClient PolicyClient { get; private set; } - - public PolicyInsightsClient PolicyInsightsClient { 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.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.GuestConfiguration.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1"); - - _helper.RunPowerShellTest(scripts); - } - } - - protected void SetupManagementClients(MockContext context) - { - GuestConfigurationClient = GetGuestConfigurationClient(context); - PolicyClient = GetPolicyClient(context); - PolicyInsightsClient = GetPolicyInsightsClient(context); - _helper.SetupManagementClients(GuestConfigurationClient, PolicyClient, PolicyInsightsClient); - } - - private static GuestConfigurationClient GetGuestConfigurationClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static PolicyClient GetPolicyClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static PolicyInsightsClient GetPolicyInsightsClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}