From afd75081c44c311331b1f469c38ca2c982dddc11 Mon Sep 17 00:00:00 2001 From: Yufan Yang <95342181+CaptainFanZzz@users.noreply.github.com> Date: Wed, 18 May 2022 13:27:03 +0800 Subject: [PATCH] Add RedisCacheTestRunner and replace Controller (#18222) --- .../ScenarioTests/RedisCacheController.cs | 122 ------------------ .../ScenarioTests/RedisCacheTestRunner.cs | 52 ++++++++ .../ScenarioTests/RedisCacheTests.cs | 32 ++--- 3 files changed, 65 insertions(+), 141 deletions(-) delete mode 100644 src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheController.cs create mode 100644 src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTestRunner.cs diff --git a/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheController.cs b/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheController.cs deleted file mode 100644 index 109b6b9b1df3..000000000000 --- a/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheController.cs +++ /dev/null @@ -1,122 +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.Management.Insights; -using Microsoft.Azure.Management.Redis; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using System; -using System.IO; -using System.Linq; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using Microsoft.Azure.Management.Storage.Version2017_10_01; -using System.Collections.Generic; -using System.Diagnostics; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.RedisCache.Test.ScenarioTests -{ - public class RedisCacheController - { - private readonly EnvironmentSetupHelper _helper; - - public Management.Internal.Resources.ResourceManagementClient NewResourceManagementClient { get; private set; } - - public InsightsManagementClient InsightsManagementClient { get; private set; } - - public RedisManagementClient RedisManagementClient { get; private set; } - - public StorageManagementClient StorageClient { get; private set; } - - public RedisCacheController() - { - _helper = new EnvironmentSetupHelper(); - } - - public static RedisCacheController NewInstance => new RedisCacheController(); - - private void SetupManagementClients(MockContext context) - { - RedisManagementClient = GetRedisManagementClient(context); - InsightsManagementClient = GetInsightsManagementClient(context); - NewResourceManagementClient = GetResourceManagementClient(context); - StorageClient = GetStorageManagementClient(context); - _helper.SetupManagementClients( - RedisManagementClient, - StorageClient, - NewResourceManagementClient, - InsightsManagementClient); - } - - 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 d = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null} - }; - var providersToIgnore = new Dictionary(); - 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(); - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - "AzureRM.Storage.ps1", - _helper.GetRMModulePath(@"AzureRM.RedisCache.psd1"), - "AzureRM.Resources.ps1"); - - if (scripts != null) - { - _helper.RunPowerShellTest(scripts); - } - } - } - - private static StorageManagementClient GetStorageManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static RedisManagementClient GetRedisManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static InsightsManagementClient GetInsightsManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static Management.Internal.Resources.ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTestRunner.cs b/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTestRunner.cs new file mode 100644 index 000000000000..4c72cc1d0485 --- /dev/null +++ b/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTestRunner.cs @@ -0,0 +1,52 @@ +// ---------------------------------------------------------------------------------- +// +// 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.RedisCache.Test.ScenarioTests +{ + public class RedisCacheTestRunner + { + protected readonly ITestRunner TestRunner; + + protected RedisCacheTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"../AzureRM.Storage.ps1", + @"../AzureRM.Resources.ps1" + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.RedisCache.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary(), + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Authorization", null} + } + ) + .Build(); + } + } +} \ No newline at end of file diff --git a/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTests.cs b/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTests.cs index f27a49b16997..6af8c3e77ffc 100644 --- a/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTests.cs +++ b/src/RedisCache/RedisCache.Test/ScenarioTests/RedisCacheTests.cs @@ -15,61 +15,55 @@ namespace Microsoft.Azure.Commands.RedisCache.Test.ScenarioTests { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; - using ServiceManagement.Common.Models; using Xunit; using Xunit.Abstractions; - public class RedisCacheTests : RMTestBase + public class RedisCacheTests : RedisCacheTestRunner { - public XunitTracingInterceptor _logger; - - public RedisCacheTests(ITestOutputHelper output) + public RedisCacheTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRedisCache() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-RedisCache"); + TestRunner.RunTestScript("Test-RedisCache"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetNonExistingRedisCacheTest() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-SetNonExistingRedisCacheTest"); + TestRunner.RunTestScript("Test-SetNonExistingRedisCacheTest"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRedisCachePipeline() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-RedisCachePipeline"); + TestRunner.RunTestScript("Test-RedisCachePipeline"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRedisCacheClustering() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-RedisCacheClustering"); + TestRunner.RunTestScript("Test-RedisCacheClustering"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRedisCachePatchSchedules() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-RedisCachePatchSchedules"); + TestRunner.RunTestScript("Test-RedisCachePatchSchedules"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestImportExportReboot() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-ImportExportReboot"); + TestRunner.RunTestScript("Test-ImportExportReboot"); } #if NETSTANDARD @@ -80,35 +74,35 @@ public void TestImportExportReboot() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDiagnosticOperations() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-DiagnosticOperations"); + TestRunner.RunTestScript("Test-DiagnosticOperations"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGeoReplication() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-GeoReplication"); + TestRunner.RunTestScript("Test-GeoReplication"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestFirewallRule() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-FirewallRule"); + TestRunner.RunTestScript("Test-FirewallRule"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestZones() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-Zones"); + TestRunner.RunTestScript("Test-Zones"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManagedIdentity() { - RedisCacheController.NewInstance.RunPowerShellTest(_logger, "Test-ManagedIdentity"); + TestRunner.RunTestScript("Test-ManagedIdentity"); } } }