From 181c9a2e40eb3bfb741bad5f34336683a46b44ca Mon Sep 17 00:00:00 2001 From: Flynn Date: Mon, 9 May 2022 12:40:21 +0800 Subject: [PATCH] Add BlueprintTestRunner and replace TestController --- .../ScenarioTests/BlueprintAssignmentTests.cs | 39 ++--- .../ScenarioTests/BlueprintTestRunner.cs | 52 +++++++ .../ScenarioTests/BlueprintTests.cs | 37 ++--- .../ManagementGroupAssignmentTests.cs | 38 +++-- .../ScenarioTests/TestController.cs | 137 ------------------ 5 files changed, 114 insertions(+), 189 deletions(-) create mode 100644 src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTestRunner.cs delete mode 100644 src/Blueprint/Blueprint.Test/ScenarioTests/TestController.cs diff --git a/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintAssignmentTests.cs b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintAssignmentTests.cs index db07d3375793..37b69fcb794f 100644 --- a/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintAssignmentTests.cs +++ b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintAssignmentTests.cs @@ -1,58 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Azure.Commands.ScenarioTest; +// ---------------------------------------------------------------------------------- +// +// 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.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests { - public class BlueprintAssignmentTests + public class BlueprintAssignmentTests : BlueprintTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public BlueprintAssignmentTests(Xunit.Abstractions.ITestOutputHelper output) + public BlueprintAssignmentTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new ServiceManagement.Common.Models.XunitTracingInterceptor(output); - ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetBlueprintAssignment() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetBlueprintAssignment"); + TestRunner.RunTestScript("Test-GetBlueprintAssignment"); } [Fact(Skip = "Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewBlueprintAssignmentWithSystemAssignedIdentity() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewBlueprintAssignmentWithSystemAssignedIdentity"); + TestRunner.RunTestScript("Test-NewBlueprintAssignmentWithSystemAssignedIdentity"); } [Fact(Skip="Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewBlueprintAssignment() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewBlueprintAssignment"); + TestRunner.RunTestScript("Test-NewBlueprintAssignment"); } [Fact(Skip = "Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetBlueprintAssignment() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-SetBlueprintAssignment"); + TestRunner.RunTestScript("Test-SetBlueprintAssignment"); } [Fact(Skip = "Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveBlueprintAssignment() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoveBlueprintAssignment"); + TestRunner.RunTestScript("Test-RemoveBlueprintAssignment"); } } diff --git a/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTestRunner.cs b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTestRunner.cs new file mode 100644 index 000000000000..47cf2637bd09 --- /dev/null +++ b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTestRunner.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.Blueprint.Test.ScenarioTests +{ + public class BlueprintTestRunner + { + protected readonly ITestRunner TestRunner; + + protected BlueprintTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.Blueprint.psd1"), + helper.GetRMModulePath("Az.Resources.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.Compute", null} + } + ) + .Build(); + } + } +} \ No newline at end of file diff --git a/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTests.cs b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTests.cs index 414e39a6a7ca..65d77d45adad 100644 --- a/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTests.cs +++ b/src/Blueprint/Blueprint.Test/ScenarioTests/BlueprintTests.cs @@ -1,46 +1,47 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests; -using Microsoft.Azure.Commands.ScenarioTest; +// ---------------------------------------------------------------------------------- +// +// 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.WindowsAzure.Commands.ScenarioTest; using Xunit; namespace Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests { - public class BlueprintTests + public class BlueprintTests : BlueprintTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - - public BlueprintTests(Xunit.Abstractions.ITestOutputHelper output) + public BlueprintTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new ServiceManagement.Common.Models.XunitTracingInterceptor(output); - ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact(Skip = "There is a framework issue where HttpClient calls are not recorded in session records causing tests to fail in playback mode")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetBlueprint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetBlueprint"); + TestRunner.RunTestScript("Test-GetBlueprint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetBlueprintWithDefinitionLocationNameAndVersion() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetBlueprintWithDefinitionLocationNameAndVersion"); + TestRunner.RunTestScript("Test-GetBlueprintWithDefinitionLocationNameAndVersion"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetBlueprintWithDefinitionLocationNameAndLatestPublished() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetBlueprintWithDefinitionLocationNameAndLatestPublished"); + TestRunner.RunTestScript("Test-GetBlueprintWithDefinitionLocationNameAndLatestPublished"); } } } diff --git a/src/Blueprint/Blueprint.Test/ScenarioTests/ManagementGroupAssignmentTests.cs b/src/Blueprint/Blueprint.Test/ScenarioTests/ManagementGroupAssignmentTests.cs index df982fa11af1..9ca7487fa70d 100644 --- a/src/Blueprint/Blueprint.Test/ScenarioTests/ManagementGroupAssignmentTests.cs +++ b/src/Blueprint/Blueprint.Test/ScenarioTests/ManagementGroupAssignmentTests.cs @@ -1,56 +1,62 @@ -namespace Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests +// ---------------------------------------------------------------------------------- +// +// 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. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests { - using Microsoft.Azure.Commands.ScenarioTest; - using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; using Xunit.Abstractions; - public class ManagementGroupAssignmentTests + public class ManagementGroupAssignmentTests : BlueprintTestRunner { - private XunitTracingInterceptor logger; - - public ManagementGroupAssignmentTests(ITestOutputHelper output) + public ManagementGroupAssignmentTests(ITestOutputHelper output) : base(output) { - - this.logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(this.logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAssignmentsInManagementGroup() { - TestController.NewInstance.RunPowerShellTest(this.logger, "Test-GetAssignmentsInManagementGroup"); + TestRunner.RunTestScript("Test-GetAssignmentsInManagementGroup"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetSingleAssignmentInManagementGroup() { - TestController.NewInstance.RunPowerShellTest(this.logger, "Test-GetSingleAssignmentInManagementGroup"); + TestRunner.RunTestScript("Test-GetSingleAssignmentInManagementGroup"); } [Fact(Skip = "Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateAssignmentInManagementGroup() { - TestController.NewInstance.RunPowerShellTest(this.logger, "Test-CreateAssignmentInManagementGroup"); + TestRunner.RunTestScript("Test-CreateAssignmentInManagementGroup"); } [Fact(Skip = "Investigate auto-registration for RP")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUpdateAssignmentInManagementGroup() { - TestController.NewInstance.RunPowerShellTest(this.logger, "Test-UpdateAssignmentInManagementGroup"); + TestRunner.RunTestScript("Test-UpdateAssignmentInManagementGroup"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveAssignmentInManagementGroup() { - TestController.NewInstance.RunPowerShellTest(this.logger, "Test-RemoveAssignmentInManagementGroup"); + TestRunner.RunTestScript("Test-RemoveAssignmentInManagementGroup"); } } } diff --git a/src/Blueprint/Blueprint.Test/ScenarioTests/TestController.cs b/src/Blueprint/Blueprint.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 805efacb5526..000000000000 --- a/src/Blueprint/Blueprint.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net.Http; -using Microsoft.Azure.Commands.Blueprint.Cmdlets; -using Microsoft.Azure.Commands.Blueprint.Common; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using Microsoft.Azure.Management.Internal.ResourceManager.Version2018_05_01; -using Microsoft.Azure.Graph.RBAC.Version1_6; -using Microsoft.Azure.Management.Authorization.Version2015_07_01; -using Microsoft.Azure.Management.Blueprint; -using LegacyResourceManagementClient = Microsoft.Azure.Management.ResourceManager.ResourceManagementClient; -using Microsoft.Rest; - - -namespace Microsoft.Azure.Commands.Blueprint.Test.ScenarioTests -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public BlueprintManagementClient BlueprintManagementClient { get; private set; } - public ServiceClientCredentials ClientCredentials { get; private set; } - public AuthorizationManagementClient AuthorizationManagementClient { get; private set; } - public ResourceManagementClient ResourceManagerClient { get; private set; } - public LegacyResourceManagementClient LegacyResourceManagementClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - BlueprintManagementClient = GetBlueprintManagementClient(context); - AuthorizationManagementClient = GetAuthorizationManagementClient(context); - ResourceManagerClient = GetResourceManagementClient(context); - LegacyResourceManagementClient = GetLegacyResourceManagementClient(context); - - _helper.SetupManagementClients( - BlueprintManagementClient, - AuthorizationManagementClient, - ResourceManagerClient, - LegacyResourceManagementClient); - } - - public void RunPowerShellTest(ServiceManagement.Common.Models.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 providers = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null}, - {"Microsoft.Compute", null} - }; - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, 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, - _helper.RMProfileModule, - _helper.GetRMModulePath("AzureRM.Blueprint.psd1"), - _helper.GetRMModulePath("AzureRM.Resources.psd1"), - "ScenarioTests\\" + callingClassName + ".ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private static BlueprintManagementClient GetBlueprintManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static AuthorizationManagementClient GetAuthorizationManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private LegacyResourceManagementClient GetLegacyResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}