From 72e31d9a93b2219553b20367291b3da1771a7e6c Mon Sep 17 00:00:00 2001 From: Ziyue Zheng Date: Tue, 17 May 2022 16:42:52 +0800 Subject: [PATCH] Add IotHubTestRunner to replace IotHubController --- .../ScenarioTests/IotHubController.cs | 157 ------------------ .../IotHubDPConfigurationTests.cs | 12 +- .../ScenarioTests/IotHubDPDeviceTests.cs | 12 +- .../ScenarioTests/IotHubDPModuleTests.cs | 12 +- .../ScenarioTests/IotHubDPTracingTests.cs | 12 +- .../ScenarioTests/IotHubRoutingTests.cs | 12 +- .../ScenarioTests/IotHubTestRunner.cs | 60 +++++++ .../IotHub.Test/ScenarioTests/IotHubTests.cs | 14 +- 8 files changed, 79 insertions(+), 212 deletions(-) delete mode 100644 src/IotHub/IotHub.Test/ScenarioTests/IotHubController.cs create mode 100644 src/IotHub/IotHub.Test/ScenarioTests/IotHubTestRunner.cs diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubController.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubController.cs deleted file mode 100644 index 7775048b4376..000000000000 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubController.cs +++ /dev/null @@ -1,157 +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.EventHub; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Internal.Resources; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Management.IotHub; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.IotHub.Test.ScenarioTests -{ - public sealed class IotHubController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public IotHubClient IotHubClient { get; private set; } - - public StorageManagementClient StorageClient { get; private set; } - - public EventHubManagementClient EHClient { get; private set; } - - public string UserDomain { get; private set; } - - public static IotHubController NewInstance => new IotHubController(); - - public IotHubController() - { - _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 cleanup - null, - callingClassType, - mockName); - } - - - public void RunPsTestWorkflow( - XunitTracingInterceptor logger, - Func scriptBuilder, - Action cleanup, - string callingClassType, - string mockName) - { - _helper.TracingInterceptor = logger; - var d = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Storage", null }, - {"Microsoft.Authorization", null}, - {"Microsoft.EventHub", 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); - - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - - var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - _helper.SetupModules(AzureModule.AzureResourceManager, - "Assert.ps1", - "Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.IotHub.psd1"), - _helper.GetRMModulePath(@"AzureRM.EventHub.psd1"), - _helper.GetRMModulePath(@"AzureRM.Storage.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); - IotHubClient = GetIotHubClient(context); - StorageClient = GetStorageManagementClient(context); - EHClient = GetEHClient(context); - - _helper.SetupManagementClients(ResourceManagementClient, - IotHubClient, - StorageClient, - EHClient); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - - private static IotHubClient GetIotHubClient(MockContext context) - { - return context.GetServiceClient(Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - private static StorageManagementClient GetStorageManagementClient(MockContext context) - { - return context.GetServiceClient(Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - - private static EventHubManagementClient GetEHClient(MockContext context) - { - return context.GetServiceClient(Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPConfigurationTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPConfigurationTests.cs index d105b4c5ab80..7176d8ce39bc 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPConfigurationTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPConfigurationTests.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.IotHub.Test.ScenarioTests { - public class IotHubDPConfigurationTests : RMTestBase + public class IotHubDPConfigurationTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubDPConfigurationTests(ITestOutputHelper output) + public IotHubDPConfigurationTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.LiveOnly)] public void TestAzureIotHubConfigurationLifecycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubConfigurationLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubConfigurationLifecycle"); } } } diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.cs index f246cc03cf70..d5982180c3db 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.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.IotHub.Test.ScenarioTests { - public class IotHubDPDeviceTests : RMTestBase + public class IotHubDPDeviceTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubDPDeviceTests(ITestOutputHelper output) + public IotHubDPDeviceTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.LiveOnly)] public void TestAzureIotHubDeviceLifecycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubDeviceLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubDeviceLifecycle"); } } } diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.cs index 6c64158a8d64..4dd5745d45c1 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.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.IotHub.Test.ScenarioTests { - public class IotHubDPModuleTests : RMTestBase + public class IotHubDPModuleTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubDPModuleTests(ITestOutputHelper output) + public IotHubDPModuleTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.LiveOnly)] public void TestAzureIotHubModuleLifecycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubModuleLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubModuleLifecycle"); } } } diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.cs index e279903a511c..c73f7c09d6b2 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubDPTracingTests.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.IotHub.Test.ScenarioTests { - public class IotHubDPTracingTests : RMTestBase + public class IotHubDPTracingTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubDPTracingTests(ITestOutputHelper output) + public IotHubDPTracingTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.LiveOnly)] public void TestAzureIotHubTracing() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubTracing"); + TestRunner.RunTestScript("Test-AzureRmIotHubTracing"); } } } diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubRoutingTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubRoutingTests.cs index bb3f8f11f6fb..7810ee334596 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubRoutingTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubRoutingTests.cs @@ -12,22 +12,16 @@ // 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.IotHub.Test.ScenarioTests { - public class IotHubRoutingTests : RMTestBase + public class IotHubRoutingTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubRoutingTests(ITestOutputHelper output) + public IotHubRoutingTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] @@ -35,7 +29,7 @@ public IotHubRoutingTests(ITestOutputHelper output) [Trait("Re-record", "ClientRuntime changes")] public void TestAzureIotHubRoutingLifeCycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubRoutingLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubRoutingLifecycle"); } } } diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubTestRunner.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubTestRunner.cs new file mode 100644 index 000000000000..81d4810e5576 --- /dev/null +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubTestRunner.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// 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.IotHub.Test.ScenarioTests +{ + public class IotHubTestRunner + { + protected readonly ITestRunner TestRunner; + + protected IotHubTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"Assert.ps1", + @"Common.ps1", + @"../AzureRM.Resources.ps1" + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.IotHub.psd1"), + helper.GetRMModulePath("Az.EventHub.psd1"), + helper.GetRMModulePath("Az.Storage.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} + }, + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Storage", null }, + {"Microsoft.Authorization", null}, + {"Microsoft.EventHub", null} + } + ) + .Build(); + } + } +} diff --git a/src/IotHub/IotHub.Test/ScenarioTests/IotHubTests.cs b/src/IotHub/IotHub.Test/ScenarioTests/IotHubTests.cs index 6b2094856bc3..4b5fa2d7f765 100644 --- a/src/IotHub/IotHub.Test/ScenarioTests/IotHubTests.cs +++ b/src/IotHub/IotHub.Test/ScenarioTests/IotHubTests.cs @@ -12,22 +12,16 @@ // 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.IotHub.Test.ScenarioTests { - public class IotHubTests : RMTestBase + public class IotHubTests : IotHubTestRunner { - public XunitTracingInterceptor _logger; - - public IotHubTests(ITestOutputHelper output) + public IotHubTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] @@ -35,7 +29,7 @@ public IotHubTests(ITestOutputHelper output) [Trait("Re-record", "ClientRuntime changes")] public void TestAzureIotHubLifeCycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubLifecycle"); } #if NETSTANDARD @@ -49,7 +43,7 @@ public void TestAzureIotHubLifeCycle() [Trait("Requires", "Elevated Privileges")] public void TestAzureIotHubCertificateLifeCycle() { - IotHubController.NewInstance.RunPsTest(_logger, "Test-AzureRmIotHubCertificateLifecycle"); + TestRunner.RunTestScript("Test-AzureRmIotHubCertificateLifecycle"); } } }