From 64f926337187a597b1ff32b217ae7097da47a764 Mon Sep 17 00:00:00 2001 From: Flynn Date: Wed, 18 May 2022 13:02:43 +0800 Subject: [PATCH 1/2] Add RelayTestRunner and replace Controller --- .../ScenarioTests/HybridConnectionsTests.cs | 12 +- .../Relay.Test/ScenarioTests/Namespace.cs | 12 +- .../ScenarioTests/OperationsListTest.cs | 12 +- .../RelayAuthorizationRulesTests.cs | 12 +- .../ScenarioTests/RelayController.cs | 129 ------------------ .../ScenarioTests/RelayTestRunner.cs | 54 ++++++++ .../TestAzureRmRelayNameTests.cs | 12 +- .../Relay.Test/ScenarioTests/WcfRelayTests.cs | 12 +- 8 files changed, 72 insertions(+), 183 deletions(-) delete mode 100644 src/Relay/Relay.Test/ScenarioTests/RelayController.cs create mode 100644 src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs diff --git a/src/Relay/Relay.Test/ScenarioTests/HybridConnectionsTests.cs b/src/Relay/Relay.Test/ScenarioTests/HybridConnectionsTests.cs index 08b4eeb4666b..d154238543e1 100644 --- a/src/Relay/Relay.Test/ScenarioTests/HybridConnectionsTests.cs +++ b/src/Relay/Relay.Test/ScenarioTests/HybridConnectionsTests.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 HybridConnectionsTests : RMTestBase + public class HybridConnectionsTests : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public HybridConnectionsTests(Xunit.Abstractions.ITestOutputHelper output) + public HybridConnectionsTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void HybridConnectionsCRUD() { - RelayController.NewInstance.RunPsTest(_logger, "HybridConnectionsTests"); + TestRunner.RunTestScript("HybridConnectionsTests"); } } } diff --git a/src/Relay/Relay.Test/ScenarioTests/Namespace.cs b/src/Relay/Relay.Test/ScenarioTests/Namespace.cs index 92ca2e091bdd..6edb33d77a03 100644 --- a/src/Relay/Relay.Test/ScenarioTests/Namespace.cs +++ b/src/Relay/Relay.Test/ScenarioTests/Namespace.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 RelayNamespaceTests : RMTestBase + public class RelayNamespaceTests : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public RelayNamespaceTests(Xunit.Abstractions.ITestOutputHelper output) + public RelayNamespaceTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void RelayNamespaceCRUD() { - RelayController.NewInstance.RunPsTest(_logger, "RelayNamespaceTests"); + TestRunner.RunTestScript("RelayNamespaceTests"); } } } diff --git a/src/Relay/Relay.Test/ScenarioTests/OperationsListTest.cs b/src/Relay/Relay.Test/ScenarioTests/OperationsListTest.cs index d6d6f561ff8c..d2d525a9aaa0 100644 --- a/src/Relay/Relay.Test/ScenarioTests/OperationsListTest.cs +++ b/src/Relay/Relay.Test/ScenarioTests/OperationsListTest.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 OperationsListTest : RMTestBase + public class OperationsListTest : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public OperationsListTest(ITestOutputHelper output) + public OperationsListTest(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void Operations() { - RelayController.NewInstance.RunPsTest(_logger, "OperationsListTest"); + TestRunner.RunTestScript("OperationsListTest"); } } } diff --git a/src/Relay/Relay.Test/ScenarioTests/RelayAuthorizationRulesTests.cs b/src/Relay/Relay.Test/ScenarioTests/RelayAuthorizationRulesTests.cs index 1532a52d4805..264d10062c68 100644 --- a/src/Relay/Relay.Test/ScenarioTests/RelayAuthorizationRulesTests.cs +++ b/src/Relay/Relay.Test/ScenarioTests/RelayAuthorizationRulesTests.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 RelayAuthorizationRulesTests : RMTestBase + public class RelayAuthorizationRulesTests : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public RelayAuthorizationRulesTests(ITestOutputHelper output) + public RelayAuthorizationRulesTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void RelayAuthorizationRulesTestsCRUD() { - RelayController.NewInstance.RunPsTest(_logger, "RelayAuthTests"); + TestRunner.RunTestScript("RelayAuthTests"); } } } diff --git a/src/Relay/Relay.Test/ScenarioTests/RelayController.cs b/src/Relay/Relay.Test/ScenarioTests/RelayController.cs deleted file mode 100644 index 5bb7612d4526..000000000000 --- a/src/Relay/Relay.Test/ScenarioTests/RelayController.cs +++ /dev/null @@ -1,129 +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.Relay; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using Microsoft.Azure.Management.Internal.Resources; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.Relay.Test.ScenarioTests -{ - public class RelayController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public RelayManagementClient RelayManagementClient { get; private set; } - - public string UserDomain { get; private set; } - - public static RelayController NewInstance => new RelayController(); - - public RelayController() - { - _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; - - _helper.TracingInterceptor = logger; - - RunPsTestWorkflow( - () => scripts, - // no custom cleanup - null, - callingClassType, - mockName); - } - - - public void RunPsTestWorkflow( - Func scriptBuilder, - Action cleanup, - string callingClassType, - string mockName) - { - var d = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null} - }; - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-07-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, - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.Relay.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); - RelayManagementClient = GetRelayManagementClient(context); - _helper.SetupManagementClients(RelayManagementClient); - _helper.SetupManagementClients(ResourceManagementClient, RelayManagementClient); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static RelayManagementClient GetRelayManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs b/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs new file mode 100644 index 000000000000..e34d4229d0ae --- /dev/null +++ b/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.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.Relay.Test.ScenarioTests +{ + public class RelayTestRunner + { + protected readonly ITestRunner TestRunner; + + protected RelayTestRunner(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.Relay.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/Relay/Relay.Test/ScenarioTests/TestAzureRmRelayNameTests.cs b/src/Relay/Relay.Test/ScenarioTests/TestAzureRmRelayNameTests.cs index f2db23c2b389..7789c990f4ad 100644 --- a/src/Relay/Relay.Test/ScenarioTests/TestAzureRmRelayNameTests.cs +++ b/src/Relay/Relay.Test/ScenarioTests/TestAzureRmRelayNameTests.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 TestAzureRmRelayNameTests : RMTestBase + public class TestAzureRmRelayNameTests : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public TestAzureRmRelayNameTests(ITestOutputHelper output) + public TestAzureRmRelayNameTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void CheckNameAvailability() { - RelayController.NewInstance.RunPsTest(_logger, "TestAzureRmRelayNameTests"); + TestRunner.RunTestScript("TestAzureRmRelayNameTests"); } } } diff --git a/src/Relay/Relay.Test/ScenarioTests/WcfRelayTests.cs b/src/Relay/Relay.Test/ScenarioTests/WcfRelayTests.cs index 9e52a7ad9642..19b2550feaf0 100644 --- a/src/Relay/Relay.Test/ScenarioTests/WcfRelayTests.cs +++ b/src/Relay/Relay.Test/ScenarioTests/WcfRelayTests.cs @@ -15,25 +15,19 @@ namespace Microsoft.Azure.Commands.Relay.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 WcfRelayTests : RMTestBase + public class WcfRelayTests : RelayTestRunner { - public XunitTracingInterceptor _logger; - - public WcfRelayTests(ITestOutputHelper output) + public WcfRelayTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void WcfRelayCRUD() { - RelayController.NewInstance.RunPsTest(_logger, "WcfRelayTests"); + TestRunner.RunTestScript("WcfRelayTests"); } } } From aaa2437ad02b402b0a74c3fef3145b34c94d60da Mon Sep 17 00:00:00 2001 From: Flynn Date: Wed, 18 May 2022 13:41:23 +0800 Subject: [PATCH 2/2] update userAgentsToIgnore --- src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs b/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs index e34d4229d0ae..f85a51c9807c 100644 --- a/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs +++ b/src/Relay/Relay.Test/ScenarioTests/RelayTestRunner.cs @@ -39,7 +39,7 @@ protected RelayTestRunner(ITestOutputHelper output) .WithNewRecordMatcherArguments( userAgentsToIgnore: new Dictionary { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-07-01"} }, resourceProviders: new Dictionary {