From bc6a5f6111d4a7f710cac306b249d5ba48f89370 Mon Sep 17 00:00:00 2001 From: Yufan Yang <95342181+CaptainFanZzz@users.noreply.github.com> Date: Tue, 17 May 2022 13:20:04 +0800 Subject: [PATCH] Add NotificationHubsTestRunner and replace TestsBaseClass (#18187) --- .../ScenarioTests/NHServiceTests.cs | 19 ++-- .../NotificationHubsTestRunner.cs | 54 +++++++++ .../ScenarioTests/TestBaseClass.cs | 103 ------------------ 3 files changed, 62 insertions(+), 114 deletions(-) create mode 100644 src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NotificationHubsTestRunner.cs delete mode 100644 src/NotificationHubs/NotificationHubs.Test/ScenarioTests/TestBaseClass.cs diff --git a/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NHServiceTests.cs b/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NHServiceTests.cs index d85ae32953fa..9fa1d4d76d7a 100644 --- a/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NHServiceTests.cs +++ b/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NHServiceTests.cs @@ -15,31 +15,28 @@ namespace Commands.NotificationHubs.Test { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using Microsoft.Azure.ServiceManagement.Common.Models; using Xunit; using Xunit.Abstractions; - public class NHServiceTests : TestBaseClass - { - public XunitTracingInterceptor _logger; + using Microsoft.Azure.Commands.NotificationHubs.Test.ScenarioTests; - public NHServiceTests(Xunit.Abstractions.ITestOutputHelper output) + public class NHServiceTests : NotificationHubsTestRunner + { + public NHServiceTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCRUDNamespace() { - RunPowerShellTest(_logger, "Test-CRUDNamespace"); + TestRunner.RunTestScript("Test-CRUDNamespace"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCRUDNamespaceAuth() { - RunPowerShellTest(_logger, "Test-CRUDNamespaceAuth"); + TestRunner.RunTestScript("Test-CRUDNamespaceAuth"); } [Fact(Skip = "Need service team to re-record test after changes to the ClientRuntime.")] @@ -47,7 +44,7 @@ public void TestCRUDNamespaceAuth() [Trait("Re-record", "ClientRuntime changes")] public void TestCRUDNotificationHub() { - RunPowerShellTest(_logger, "Test-CRUDNotificationHub"); + TestRunner.RunTestScript("Test-CRUDNotificationHub"); } [Fact(Skip = "Need service team to re-record test after changes to the ClientRuntime.")] @@ -55,7 +52,7 @@ public void TestCRUDNotificationHub() [Trait("Re-record", "ClientRuntime changes")] public void TestCRUDNHAuth() { - RunPowerShellTest(_logger, "Test-CRUDNHAuth"); + TestRunner.RunTestScript("Test-CRUDNHAuth"); } } } diff --git a/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NotificationHubsTestRunner.cs b/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NotificationHubsTestRunner.cs new file mode 100644 index 000000000000..4055a5086d07 --- /dev/null +++ b/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/NotificationHubsTestRunner.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 Xunit.Abstractions; + +namespace Microsoft.Azure.Commands.NotificationHubs.Test.ScenarioTests +{ + public class NotificationHubsTestRunner + { + protected readonly ITestRunner TestRunner; + + protected NotificationHubsTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"../AzureRM.Resources.ps1" + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.NotificationHubs.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(); + } + } +} \ No newline at end of file diff --git a/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/TestBaseClass.cs b/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/TestBaseClass.cs deleted file mode 100644 index 1458ea654bd4..000000000000 --- a/src/NotificationHubs/NotificationHubs.Test/ScenarioTests/TestBaseClass.cs +++ /dev/null @@ -1,103 +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.Diagnostics; -using System.Linq; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Management.NotificationHubs; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using System.Collections.Generic; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using System; -using System.IO; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Commands.NotificationHubs.Test -{ - public abstract class TestBaseClass : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - protected TestBaseClass() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - var resourceManagementClient = GetResourceManagementClient(context); - var nhManagementClient = GetNotificationHubsManagementClient(context); - - _helper.SetupManagementClients(nhManagementClient, resourceManagementClient); - } - - protected 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 d = 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, d, 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(); - - var modules = new List - { - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.NotificationHubs.psd1"), - "AzureRM.Resources.ps1" - }; - - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - _helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray()); - - if (scripts != null) - { - _helper.RunPowerShellTest(scripts); - } - } - } - - protected NotificationHubsManagementClient GetNotificationHubsManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}