From de811229032e39d554902d9cef1e3b968ad23134 Mon Sep 17 00:00:00 2001 From: Ziyue Zheng Date: Thu, 19 May 2022 15:37:27 +0800 Subject: [PATCH] Add SupportTestRunner to replace TestController (#18188) * Add SupportTestRunner to replace TestController * Revert Az.Support document changes --- .../ScenarioTests/CommunicationTests.cs | 23 ++-- .../ProblemClassificationTests.cs | 23 ++-- .../ScenarioTests/ServiceTests.cs | 19 +-- .../ScenarioTests/SupportTestRunner.cs | 55 +++++++++ .../ScenarioTests/SupportTicketTests.cs | 45 +++---- .../ScenarioTests/TestController.cs | 112 ------------------ 6 files changed, 96 insertions(+), 181 deletions(-) create mode 100644 src/Support/Support.Test/ScenarioTests/SupportTestRunner.cs delete mode 100644 src/Support/Support.Test/ScenarioTests/TestController.cs diff --git a/src/Support/Support.Test/ScenarioTests/CommunicationTests.cs b/src/Support/Support.Test/ScenarioTests/CommunicationTests.cs index 568b7ea8969b..316caf5b38a1 100644 --- a/src/Support/Support.Test/ScenarioTests/CommunicationTests.cs +++ b/src/Support/Support.Test/ScenarioTests/CommunicationTests.cs @@ -12,64 +12,57 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Support.Test.ScenarioTests { - public class CommunicationTests + public class CommunicationTests : SupportTestRunner { - private XunitTracingInterceptor _logger; - - public CommunicationTests(Xunit.Abstractions.ITestOutputHelper output) + public CommunicationTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketCommunicationNameParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketCommunicationNameParameterSet"); + TestRunner.RunTestScript("New-AzSupportTicketCommunicationNameParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketCommunicationParentObjectParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketCommunicationParentObjectParameterSet"); + TestRunner.RunTestScript("New-AzSupportTicketCommunicationParentObjectParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketCommunicationByNameParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketCommunicationByNameParameterSet"); + TestRunner.RunTestScript("Get-AzSupportTicketCommunicationByNameParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketCommunicationFilterByCommunicationType() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketCommunicationFilterByCommunicationType"); + TestRunner.RunTestScript("Get-AzSupportTicketCommunicationFilterByCommunicationType"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketCommunicationPagingParameters() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketCommunicationPagingParameters"); + TestRunner.RunTestScript("Get-AzSupportTicketCommunicationPagingParameters"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketCommunicationByParentObjectParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketCommunicationByParentObjectParameterSet"); + TestRunner.RunTestScript("Get-AzSupportTicketCommunicationByParentObjectParameterSet"); } } } diff --git a/src/Support/Support.Test/ScenarioTests/ProblemClassificationTests.cs b/src/Support/Support.Test/ScenarioTests/ProblemClassificationTests.cs index 05f96ca80bdd..dc51a55a155b 100644 --- a/src/Support/Support.Test/ScenarioTests/ProblemClassificationTests.cs +++ b/src/Support/Support.Test/ScenarioTests/ProblemClassificationTests.cs @@ -12,64 +12,57 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Support.Test.ScenarioTests { - public class ProblemClassificationTests + public class ProblemClassificationTests : SupportTestRunner { - private XunitTracingInterceptor _logger; - - public ProblemClassificationTests(Xunit.Abstractions.ITestOutputHelper output) + public ProblemClassificationTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationServiceName() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationServiceName"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationServiceName"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationAllParametersNameAlias() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationAllParametersNameAlias"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationAllParametersNameAlias"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationAllParametersId() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationAllParametersId"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationAllParametersId"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationAllParametersResourceId() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationAllParametersResourceId"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationAllParametersResourceId"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationParentObjectServiceName() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationParentObjectServiceName"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationParentObjectServiceName"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportProblemClassificationParentObjectAllParameters() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportProblemClassificationParentObjectAllParameters"); + TestRunner.RunTestScript("Get-AzSupportProblemClassificationParentObjectAllParameters"); } } } \ No newline at end of file diff --git a/src/Support/Support.Test/ScenarioTests/ServiceTests.cs b/src/Support/Support.Test/ScenarioTests/ServiceTests.cs index 71d287c76175..b8f8039dd189 100644 --- a/src/Support/Support.Test/ScenarioTests/ServiceTests.cs +++ b/src/Support/Support.Test/ScenarioTests/ServiceTests.cs @@ -12,50 +12,43 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Support.Test.ScenarioTests { - public class ServiceTests + public class ServiceTests : SupportTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public ServiceTests(Xunit.Abstractions.ITestOutputHelper output) + public ServiceTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportServiceNoParameter() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportServiceNoParameter"); + TestRunner.RunTestScript("Get-AzSupportServiceNoParameter"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportServiceByNameParameterSetUsingNameAlias() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportServiceByNameParameterSetUsingNameAlias"); + TestRunner.RunTestScript("Get-AzSupportServiceByNameParameterSetUsingNameAlias"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportServiceByNameParameterSetUsingId() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportServiceByNameParameterSetUsingId"); + TestRunner.RunTestScript("Get-AzSupportServiceByNameParameterSetUsingId"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportServiceByNameParameterSetUsingCompleteResourceId() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportServiceByNameParameterSetUsingCompleteResourceId"); + TestRunner.RunTestScript("Get-AzSupportServiceByNameParameterSetUsingCompleteResourceId"); } } } \ No newline at end of file diff --git a/src/Support/Support.Test/ScenarioTests/SupportTestRunner.cs b/src/Support/Support.Test/ScenarioTests/SupportTestRunner.cs new file mode 100644 index 000000000000..39f83f3e11ab --- /dev/null +++ b/src/Support/Support.Test/ScenarioTests/SupportTestRunner.cs @@ -0,0 +1,55 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Support.Test.ScenarioTests +{ + public class SupportTestRunner + { + protected readonly ITestRunner TestRunner; + + protected SupportTestRunner(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.Support.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} + } + ) + .Build(); + } + } +} diff --git a/src/Support/Support.Test/ScenarioTests/SupportTicketTests.cs b/src/Support/Support.Test/ScenarioTests/SupportTicketTests.cs index 933bdd5b11b8..e60e7304bf91 100644 --- a/src/Support/Support.Test/ScenarioTests/SupportTicketTests.cs +++ b/src/Support/Support.Test/ScenarioTests/SupportTicketTests.cs @@ -12,141 +12,134 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Support.Test.ScenarioTests { - public class SupportTicketTests + public class SupportTicketTests : SupportTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public SupportTicketTests(Xunit.Abstractions.ITestOutputHelper output) + public SupportTicketTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketByNameParameterSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketByNameParameterSet"); + TestRunner.RunTestScript("Get-AzSupportTicketByNameParameterSet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketFilterByStatus() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketFilterByStatus"); + TestRunner.RunTestScript("Get-AzSupportTicketFilterByStatus"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void GetAzSupportTicketPagingParameters() { - TestController.NewInstance.RunPowerShellTest(_logger, "Get-AzSupportTicketPagingParameters"); + TestRunner.RunTestScript("Get-AzSupportTicketPagingParameters"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketWithContactObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketWithContactObject"); + TestRunner.RunTestScript("New-AzSupportTicketWithContactObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketQuotaWithContactObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketQuotaWithContactObject"); + TestRunner.RunTestScript("New-AzSupportTicketQuotaWithContactObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketTechnicalWithContactObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketTechnicalWithContactObject"); + TestRunner.RunTestScript("New-AzSupportTicketTechnicalWithContactObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketWithContactDetail() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketWithContactDetail"); + TestRunner.RunTestScript("New-AzSupportTicketWithContactDetail"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketQuotaWithContactDetail() { - TestController.NewInstance.RunPowerShellTest(_logger, "New-AzSupportTicketQuotaWithContactDetail"); + TestRunner.RunTestScript("New-AzSupportTicketQuotaWithContactDetail"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void NewAzSupportTicketTechnicalWithContactDetail() { - TestController.NewInstance.RunPowerShellTest(_logger, string.Format("New-AzSupportTicketTechnicalWithContactDetail")); + TestRunner.RunTestScript(string.Format("New-AzSupportTicketTechnicalWithContactDetail")); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketParentObjectParameterSetWithContactObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketParentObjectParameterSetWithContactObject"); + TestRunner.RunTestScript("Update-AzSupportTicketParentObjectParameterSetWithContactObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketNameParameterSetWithContactObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketNameParameterSetWithContactObject"); + TestRunner.RunTestScript("Update-AzSupportTicketNameParameterSetWithContactObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketParentObjectParameterSetWithContactDetail() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketParentObjectParameterSetWithContactDetail"); + TestRunner.RunTestScript("Update-AzSupportTicketParentObjectParameterSetWithContactDetail"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketNameParameterSetWithContactDetail() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketNameParameterSetWithContactDetail"); + TestRunner.RunTestScript("Update-AzSupportTicketNameParameterSetWithContactDetail"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketParentObjectParameterSetUpdateSeverity() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketParentObjectParameterSetUpdateSeverity"); + TestRunner.RunTestScript("Update-AzSupportTicketParentObjectParameterSetUpdateSeverity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketNameParameterSetUpdateSeverity() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketNameParameterSetUpdateSeverity"); + TestRunner.RunTestScript("Update-AzSupportTicketNameParameterSetUpdateSeverity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketParentObjectParameterSetUpdateStatus() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketParentObjectParameterSetUpdateStatus"); + TestRunner.RunTestScript("Update-AzSupportTicketParentObjectParameterSetUpdateStatus"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void UpdateAzSupportTicketNameParameterSetUpdateStatus() { - TestController.NewInstance.RunPowerShellTest(_logger, "Update-AzSupportTicketNameParameterSetUpdateStatus"); + TestRunner.RunTestScript("Update-AzSupportTicketNameParameterSetUpdateStatus"); } } } \ No newline at end of file diff --git a/src/Support/Support.Test/ScenarioTests/TestController.cs b/src/Support/Support.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 304a5e34f471..000000000000 --- a/src/Support/Support.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,112 +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.Internal.Resources; -using Microsoft.Azure.Management.Support; -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; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; - -namespace Microsoft.Azure.Commands.Support.Test.ScenarioTests -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public IMicrosoftSupportClient SupportClient { get; private set; } - - public SubscriptionClient SubscriptionClient { get; private set; } - - public ResourceManagementClient ResourceManagementClient { 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 - { - {"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(@"Az.Support.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - "AzureRM.Resources.ps1"); - - _helper.RunPowerShellTest(scripts); - } - } - - protected void SetupManagementClients(MockContext context) - { - SupportClient = GetResourceGraphClient(context); - SubscriptionClient = GetSubscriptionClient(context); - ResourceManagementClient = GetResourceManagementClient(context); - _helper.SetupManagementClients(SupportClient, SubscriptionClient, ResourceManagementClient); - } - - private static IMicrosoftSupportClient GetResourceGraphClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static SubscriptionClient GetSubscriptionClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}