From ee78012181f30d5498e9b2c0635abcf514fbcab5 Mon Sep 17 00:00:00 2001 From: v-yuzhichen <96757176+v-yuzhichen@users.noreply.github.com> Date: Fri, 13 May 2022 11:40:48 +0800 Subject: [PATCH] Add IotCentralTestRunner to replace IotCentralController (#18130) --- .../ScenarioTests/IotCentralController.cs | 134 ------------------ .../ScenarioTests/IotCentralTestRunner.cs | 57 ++++++++ .../ScenarioTests/IotCentralTests.cs | 12 +- 3 files changed, 60 insertions(+), 143 deletions(-) delete mode 100644 src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralController.cs create mode 100644 src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTestRunner.cs diff --git a/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralController.cs b/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralController.cs deleted file mode 100644 index d22d1abd8f54..000000000000 --- a/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralController.cs +++ /dev/null @@ -1,134 +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 Microsoft.Azure.Management.Internal.Resources; - -namespace Microsoft.Azure.Commands.IotCentral.Test.ScenarioTests -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using Microsoft.Azure.Commands.Common.Authentication; - using Microsoft.Azure.Test.HttpRecorder; - using Microsoft.Rest.ClientRuntime.Azure.TestFramework; - using Microsoft.WindowsAzure.Commands.ScenarioTest; - using ServiceManagement.Common.Models; - using Microsoft.Azure.Management.IotCentral; - using TestEnvironmentFactory = Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; - - public sealed class IotCentralController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public IotCentralClient IotCentralClient { get; private set; } - - public static IotCentralController NewInstance => new IotCentralController(); - - public IotCentralController() - { - _helper = new EnvironmentSetupHelper(); - } - - public void RunPsTest(XunitTracingInterceptor logger, params string[] scripts) - { - var sf = new StackTrace().GetFrame(1); - var callingClassType = sf.GetMethod().ReflectedType?.ToString(); - var mockName = sf.GetMethod().Name; - - RunPsTestWorkflow( - logger, - () => scripts, - // no custom initializer - null, - // no custom cleanup - null, - callingClassType, - mockName); - } - - - public void RunPsTestWorkflow( - XunitTracingInterceptor logger, - Func scriptBuilder, - Action initialize, - Action cleanup, - string callingClassType, - string mockName) - { - _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", "2021_04_01"}, - {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"}, - {"Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient", "2016-09-01"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore); - HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords"); - - using (var context = MockContext.Start(callingClassType, mockName)) - { - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - - SetupManagementClients(context); - var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.IotCentral.psd1"), - "AzureRM.Resources.ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private void SetupManagementClients(MockContext context) - { - ResourceManagementClient = GetResourceManagementClient(context); - IotCentralClient = GetIotCentralClient(context); - - _helper.SetupManagementClients(ResourceManagementClient,IotCentralClient); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static IotCentralClient GetIotCentralClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTestRunner.cs b/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTestRunner.cs new file mode 100644 index 000000000000..b521863cda8d --- /dev/null +++ b/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTestRunner.cs @@ -0,0 +1,57 @@ +// ---------------------------------------------------------------------------------- +// +// 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.IotCentral.Test.ScenarioTests +{ + public class IotCentralTestRunner + { + protected readonly ITestRunner TestRunner; + + protected IotCentralTestRunner(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.IotCentral.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2021_04_01"}, + {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"}, + {"Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient", "2016-09-01"} + }, + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Authorization", null} + } + ) + .Build(); + } + } +} diff --git a/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTests.cs b/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTests.cs index 02e133527d3d..1de3dc7e13b1 100644 --- a/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTests.cs +++ b/src/IotCentral/IotCentral.Test/ScenarioTests/IotCentralTests.cs @@ -12,29 +12,23 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -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.IotCentral.Test.ScenarioTests { - public class IotCentralTests : RMTestBase + public class IotCentralTests : IotCentralTestRunner { - public XunitTracingInterceptor _logger; - - public IotCentralTests(ITestOutputHelper output) + public IotCentralTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestIotCentralAppLifecycleManagement() { - IotCentralController.NewInstance.RunPsTest(_logger, "Test-IotCentralAppLifecycleManagement"); + TestRunner.RunTestScript("Test-IotCentralAppLifecycleManagement"); } } }