From a6e84ef4a5b0a431aabb7720a934fa3ffd057293 Mon Sep 17 00:00:00 2001 From: Flynn Date: Thu, 19 May 2022 16:06:28 +0800 Subject: [PATCH] Add SubscriptionTestRunner and replace TestController --- .../ScenarioTests/SubscriptionTestRunner.cs | 51 ++++++++ .../ScenarioTests/SubscriptionTests.cs | 23 ++-- .../ScenarioTests/TestController.cs | 115 ------------------ 3 files changed, 59 insertions(+), 130 deletions(-) create mode 100644 src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTestRunner.cs delete mode 100644 src/Subscription/Subscription.Test/ScenarioTests/TestController.cs diff --git a/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTestRunner.cs b/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTestRunner.cs new file mode 100644 index 000000000000..730437e40340 --- /dev/null +++ b/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTestRunner.cs @@ -0,0 +1,51 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Subscription.Test.ScenarioTests +{ + public class SubscriptionTestRunner + { + protected readonly ITestRunner TestRunner; + + protected SubscriptionTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.Subscription.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.Compute", null} + } + ) + .Build(); + } + } +} \ No newline at end of file diff --git a/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTests.cs b/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTests.cs index 73921cd24bf1..408db9545605 100644 --- a/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTests.cs +++ b/src/Subscription/Subscription.Test/ScenarioTests/SubscriptionTests.cs @@ -12,9 +12,7 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.Commands.Subscription.Test.ScenarioTests.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; +using Microsoft.Azure.Commands.Subscription.Test.ScenarioTests; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; @@ -24,50 +22,45 @@ namespace Subscription.Test.ScenarioTests /// The following tests require four subscriptions to be created manually. This can be done by /// calling New-AzureRmSubscription. At this time, this can only be done by EA customers. /// - public class SubscriptionTests + public class SubscriptionTests : SubscriptionTestRunner { - private XunitTracingInterceptor _logger; - - public SubscriptionTests(Xunit.Abstractions.ITestOutputHelper output) + public SubscriptionTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUpdateRenameSubscription() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-UpdateRenameSubscription"); + TestRunner.RunTestScript("Test-UpdateRenameSubscription"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUpdateCancelSubscription() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-UpdateCancelSubscription"); + TestRunner.RunTestScript("Test-UpdateCancelSubscription"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSubscriptionAlias() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewSubscriptionAlias"); + TestRunner.RunTestScript("Test-NewSubscriptionAlias"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetSubscriptionAlias() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetSubscriptionAlias"); + TestRunner.RunTestScript("Test-GetSubscriptionAlias"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveSubscriptionAlias() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoveSubscriptionAlias"); + TestRunner.RunTestScript("Test-RemoveSubscriptionAlias"); } } } diff --git a/src/Subscription/Subscription.Test/ScenarioTests/TestController.cs b/src/Subscription/Subscription.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 70aee393c9d1..000000000000 --- a/src/Subscription/Subscription.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,115 +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.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 Microsoft.Azure.Management.Subscription; - -namespace Microsoft.Azure.Commands.Subscription.Test.ScenarioTests.ScenarioTest -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public SubscriptionClient SubscriptionClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - SubscriptionClient = GetSubscriptionClient(context); - _helper.SetupManagementClients(SubscriptionClient); - } - - 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.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null}, - {"Microsoft.Compute", 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.Subscription.psd1"), - "ScenarioTests\\" + callingClassName + ".ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private static SubscriptionClient GetSubscriptionClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}