From 5a315f91b6416e65b0e8f7c6a571b37112a73590 Mon Sep 17 00:00:00 2001 From: Yufan Yang <95342181+CaptainFanZzz@users.noreply.github.com> Date: Fri, 13 May 2022 16:04:12 +0800 Subject: [PATCH] Add MaintenanceTestRunner and replace TestController (#18151) --- .../ScenarioTests/MaintenanceTestRunner.cs | 56 +++++++++ .../ScenarioTests/MaintenanceTests.cs | 23 ++-- .../ScenarioTests/TestController.cs | 107 ------------------ 3 files changed, 63 insertions(+), 123 deletions(-) create mode 100644 src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTestRunner.cs delete mode 100644 src/Maintenance/Maintenance.Test/ScenarioTests/TestController.cs diff --git a/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTestRunner.cs b/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTestRunner.cs new file mode 100644 index 000000000000..8274326a3083 --- /dev/null +++ b/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTestRunner.cs @@ -0,0 +1,56 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Maintenance.Test.ScenarioTests +{ + public class MaintenanceTestRunner + { + protected readonly ITestRunner TestRunner; + + protected MaintenanceTestRunner(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.Maintenance.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} + }, + resourceProviders: new Dictionary + { + { "Microsoft.Resources", null }, + { "Microsoft.Features", null }, + { "Microsoft.Authorization", null }, + { "Microsoft.Maintenance", null } + } + ) + .Build(); + } + } +} diff --git a/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.cs b/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.cs index e8a283b10c87..06d3a5aff6f6 100644 --- a/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.cs +++ b/src/Maintenance/Maintenance.Test/ScenarioTests/MaintenanceTests.cs @@ -12,59 +12,50 @@ // 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.Maintenance.Test.ScenarioTests { - public class MaintenanceTests : RMTestBase + public class MaintenanceTests : MaintenanceTestRunner { - public XunitTracingInterceptor _logger; - - public MaintenanceTests(Xunit.Abstractions.ITestOutputHelper output) + public MaintenanceTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMaintenanceConfiguration() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzMaintenanceConfiguration"); + TestRunner.RunTestScript("Test-AzMaintenanceConfiguration"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMaintenanceConfigurationInGuestPatch() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzMaintenanceConfigurationInGuestPatch"); + TestRunner.RunTestScript("Test-AzMaintenanceConfigurationInGuestPatch"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestConfigurationAssignment() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzConfigurationAssignment"); + TestRunner.RunTestScript("Test-AzConfigurationAssignment"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMaintenanceUpdate() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzMaintenanceUpdate"); + TestRunner.RunTestScript("Test-AzMaintenanceUpdate"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPublicMaintenanceConfiguration() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzMaintenancePublicConfiguration"); + TestRunner.RunTestScript("Test-AzMaintenancePublicConfiguration"); } } } diff --git a/src/Maintenance/Maintenance.Test/ScenarioTests/TestController.cs b/src/Maintenance/Maintenance.Test/ScenarioTests/TestController.cs deleted file mode 100644 index da5619ee95a3..000000000000 --- a/src/Maintenance/Maintenance.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,107 +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.Maintenance; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.Maintenance.Test.ScenarioTests -{ - public class TestController - { - private readonly EnvironmentSetupHelper _helper; - - public MaintenanceManagementClient MaintenanceClient { get; private set; } - - public ResourceManagementClient ResourceClient { get; private set; } - - public TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - public static TestController NewInstance => new TestController(); - - 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 }, - { "Microsoft.Maintenance", 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); - - var callingClassName = callingClassType?.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\" + callingClassName + ".ps1", - "AzureRM.Resources.ps1", - "ScenarioTests\\Common.ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"Az.Maintenance.psd1")); - - if (scripts != null) - { - _helper.RunPowerShellTest(scripts); - } - } - } - - private void SetupManagementClients(MockContext context) - { - ResourceClient = GetResourceManagementClient(context); - MaintenanceClient = GetMaintenanceManagementClient(context); - _helper.SetupManagementClients(ResourceClient, MaintenanceClient); - } - - private static MaintenanceManagementClient GetMaintenanceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}