From d78eba5dfb7c764076f237a40d00d22d5e97b9b2 Mon Sep 17 00:00:00 2001 From: Flynn Date: Mon, 9 May 2022 15:31:27 +0800 Subject: [PATCH 1/2] Add ContainerRegistryTestRunner and replace TestController --- ...erRegistryCreateWithNetworkRuleSetTests.cs | 15 +-- .../ContainerRegistryTestRunner.cs | 57 +++++++++++ .../ScenarioTests/ContainerRegistryTests.cs | 23 ++--- .../ScenarioTests/ImportImageTests.cs | 15 +-- .../ScenarioTests/TestController.cs | 95 ------------------- 5 files changed, 70 insertions(+), 135 deletions(-) create mode 100644 src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs delete mode 100644 src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/TestController.cs diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryCreateWithNetworkRuleSetTests.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryCreateWithNetworkRuleSetTests.cs index 359f63eab2f4..ddaf720beb5e 100644 --- a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryCreateWithNetworkRuleSetTests.cs +++ b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryCreateWithNetworkRuleSetTests.cs @@ -12,31 +12,22 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -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.ContainerRegistry.Test.ScenarioTests { - public class ContainerRegistryCreateWithNetworkRuleSetTests : RMTestBase + public class ContainerRegistryCreateWithNetworkRuleSetTests : ContainerRegistryTestRunner { - public XunitTracingInterceptor _logger; - - public ContainerRegistryCreateWithNetworkRuleSetTests(Xunit.Abstractions.ITestOutputHelper output) + public ContainerRegistryCreateWithNetworkRuleSetTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateWithNetworkRuleSet() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateWithNetworkRuleSet"); + TestRunner.RunTestScript("Test-CreateWithNetworkRuleSet"); } } } diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs new file mode 100644 index 000000000000..373a62b1f389 --- /dev/null +++ b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.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.ContainerRegistry.Test.ScenarioTests +{ + public class ContainerRegistryTestRunner + { + protected readonly ITestRunner TestRunner; + + protected ContainerRegistryTestRunner(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.Network.psd1"), + helper.GetRMModulePath("Az.ContainerRegistry.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}, + {"Microsoft.Network", null} + } + ) + .Build(); + } + } +} diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTests.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTests.cs index 6cecaec38760..3140a6123e9c 100644 --- a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTests.cs +++ b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTests.cs @@ -12,45 +12,36 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -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.ContainerRegistry.Test.ScenarioTests { - public class ContainerRegistryTests : RMTestBase + public class ContainerRegistryTests : ContainerRegistryTestRunner { - public XunitTracingInterceptor _logger; - - public ContainerRegistryTests(Xunit.Abstractions.ITestOutputHelper output) + public ContainerRegistryTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestContainerReg() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzureContainerRegistry"); + TestRunner.RunTestScript("Test-AzureContainerRegistry"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestContainerRegCredential() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzureContainerRegistryCredential"); + TestRunner.RunTestScript("Test-AzureContainerRegistryCredential"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestContainerRegNameAvailability() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzureContainerRegistryNameAvailability"); + TestRunner.RunTestScript("Test-AzureContainerRegistryNameAvailability"); } [Fact(Skip = "Need service team to re-record test after changes to the ClientRuntime.")] @@ -58,7 +49,7 @@ public void TestContainerRegNameAvailability() [Trait("Re-record", "ClientRuntime changes")] public void TestContainerRegReplication() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzureContainerRegistryReplication"); + TestRunner.RunTestScript("Test-AzureContainerRegistryReplication"); } [Fact(Skip = "Need service team to re-record test after changes to the ClientRuntime.")] @@ -66,7 +57,7 @@ public void TestContainerRegReplication() [Trait("Re-record", "ClientRuntime changes")] public void TestContainerRegWebhook() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AzureContainerRegistryWebhook"); + TestRunner.RunTestScript("Test-AzureContainerRegistryWebhook"); } } } diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ImportImageTests.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ImportImageTests.cs index adab622fad53..6daf16a7ad0c 100644 --- a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ImportImageTests.cs +++ b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ImportImageTests.cs @@ -12,31 +12,22 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.ScenarioTest; -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.ContainerRegistry.Test.ScenarioTests { - public class ImportImageTests : RMTestBase + public class ImportImageTests : ContainerRegistryTestRunner { - public XunitTracingInterceptor _logger; - - public ImportImageTests(Xunit.Abstractions.ITestOutputHelper output) + public ImportImageTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestImportImage() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ImportImage"); + TestRunner.RunTestScript("Test-ImportImage"); } } } diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/TestController.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/TestController.cs deleted file mode 100644 index ee19a8639a3c..000000000000 --- a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,95 +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.ContainerRegistry; -using Microsoft.Azure.ServiceManagement.Common.Models; -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 InternalResourceManagementClient = Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient; -using ResourceManagementClient = Microsoft.Azure.Management.ResourceManager.ResourceManagementClient; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using NetworkManagementClient = Microsoft.Azure.Management.Network.NetworkManagementClient; - -namespace Microsoft.Azure.Commands.ContainerRegistry.Test.ScenarioTests -{ - public class TestController - { - private readonly EnvironmentSetupHelper _helper; - - public TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - public static TestController NewInstance => new TestController(); - - private void SetupManagementClients(MockContext context) - { - _helper.SetupManagementClients( - context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()), - context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()), - context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()), - context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment())); - } - - 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}, - {"Microsoft.Network", 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(); - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\" + callingClassName + ".ps1", - "AzureRM.Resources.ps1", - "ScenarioTests\\Common.ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath("AzureRM.Network.psd1"), - _helper.GetRMModulePath("AzureRM.ContainerRegistry.psd1")); - - if (scripts != null) - { - _helper.RunPowerShellTest(scripts); - } - } - } - } -} From 6d06e5657e7abc2f1b861b969971fd36000e54d1 Mon Sep 17 00:00:00 2001 From: Ziyue Zheng Date: Mon, 9 May 2022 17:39:46 +0800 Subject: [PATCH 2/2] Add ContainerRegistryTestRunner and replace TestController --- .../ScenarioTests/ContainerRegistryTestRunner.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs index 373a62b1f389..26a174dbe629 100644 --- a/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs +++ b/src/ContainerRegistry/ContainerRegistry.Test/ScenarioTests/ContainerRegistryTestRunner.cs @@ -30,10 +30,10 @@ protected ContainerRegistryTestRunner(ITestOutputHelper output) .WithCommonPsScripts(new[] { @"Common.ps1", - @"../AzureRM.Resources.ps1", + @"../AzureRM.Resources.ps1" }) .WithNewRmModules(helper => new[] - { + { helper.RMProfileModule, helper.GetRMModulePath("Az.Network.psd1"), helper.GetRMModulePath("Az.ContainerRegistry.psd1")