From 0d2a20d2b2644e85de0a512ac7ee19ad0c2b7021 Mon Sep 17 00:00:00 2001 From: Flynn Date: Mon, 23 May 2022 16:15:30 +0800 Subject: [PATCH] Add PowerBIEmbededTestRunner and replace Controller --- .../ScenarioTests/PowerBITestRunner.cs | 55 ++++++++ .../ScenarioTests/PowerBITests.cs | 15 +- .../ScenarioTests/PowerBITestsBase.cs | 128 ------------------ .../ScenarioTests/PowerBIController.cs | 128 ------------------ .../PowerBIEmbeddedTestRunner.cs | 55 ++++++++ .../ScenarioTests/PowerBITests.cs | 26 ++-- 6 files changed, 125 insertions(+), 282 deletions(-) create mode 100644 src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestRunner.cs delete mode 100644 src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestsBase.cs delete mode 100644 src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIController.cs create mode 100644 src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIEmbeddedTestRunner.cs diff --git a/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestRunner.cs b/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestRunner.cs new file mode 100644 index 000000000000..362fbc99c636 --- /dev/null +++ b/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestRunner.cs @@ -0,0 +1,55 @@ +// ---------------------------------------------------------------------------------- +// +// 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.PowerBI.Test.ScenarioTests +{ + public class PowerBITestRunner + { + protected readonly ITestRunner TestRunner; + + protected PowerBITestRunner(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.PowerBIEmbedded.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/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITests.cs b/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITests.cs index 0c0e65dbef02..694bac777b0c 100644 --- a/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITests.cs +++ b/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITests.cs @@ -15,38 +15,33 @@ namespace Microsoft.Azure.Commands.PowerBI.Test.ScenarioTests { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using ServiceManagement.Common.Models; using Xunit; - public class PowerBITests : PowerBITestsBase + public class PowerBITests : PowerBITestRunner { - public XunitTracingInterceptor _logger; - - public PowerBITests(Xunit.Abstractions.ITestOutputHelper output) + public PowerBITests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPBIECapacity() { - NewInstance.RunPsTest(_logger, "Test-PowerBIEmbeddedCapacity"); + TestRunner.RunTestScript("Test-PowerBIEmbeddedCapacity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPBIECapacityScale() { - NewInstance.RunPsTest(_logger, "Test-PowerBIEmbeddedCapacityScale"); + TestRunner.RunTestScript("Test-PowerBIEmbeddedCapacityScale"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNegativePBIECapacity() { - NewInstance.RunPsTest(_logger, "Test-NegativePowerBIEmbeddedCapacity"); + TestRunner.RunTestScript("Test-NegativePowerBIEmbeddedCapacity"); } } } diff --git a/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestsBase.cs b/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestsBase.cs deleted file mode 100644 index e108ef162882..000000000000 --- a/src/PowerBIEmbedded/PowerBI.Test/ScenarioTests/PowerBITestsBase.cs +++ /dev/null @@ -1,128 +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.PowerBIDedicated; -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.Linq; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using NewResourceManagementClient = Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using System.IO; -using Microsoft.Azure.ServiceManagement.Common.Models; - -namespace Microsoft.Azure.Commands.PowerBI.Test.ScenarioTests -{ - public class PowerBITestsBase : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public NewResourceManagementClient NewResourceManagementClient { get; private set; } - - public PowerBIDedicatedManagementClient PowerBIEmbeddedCapacityManagementClient { get; private set; } - - public static PowerBITestsBase NewInstance => new PowerBITestsBase(); - - - public PowerBITestsBase() - { - _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-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, - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.PowerBIEmbedded.psd1"), - "AzureRM.Resources.ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private void SetupManagementClients(MockContext context) - { - PowerBIEmbeddedCapacityManagementClient = GetPowerBIEmbeddedCapacityManagementClient(context); - NewResourceManagementClient = GetNewResourceManagementClient(context); - _helper.SetupManagementClients(NewResourceManagementClient, PowerBIEmbeddedCapacityManagementClient); - } - - private static NewResourceManagementClient GetNewResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static PowerBIDedicatedManagementClient GetPowerBIEmbeddedCapacityManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIController.cs b/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIController.cs deleted file mode 100644 index 04f6142d7228..000000000000 --- a/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIController.cs +++ /dev/null @@ -1,128 +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; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.Management.PowerBIEmbedded; -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.PowerBIEmbedded.Test.ScenarioTests -{ - public sealed class PowerBIController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public PowerBIEmbeddedManagementClient PowerBIClient { get; private set; } - - public string UserDomain { get; private set; } - - public static PowerBIController NewInstance => new PowerBIController(); - - public PowerBIController() - { - _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-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, - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.PowerBIEmbedded.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); - PowerBIClient = GetPowerBIClient(context); - - _helper.SetupManagementClients(ResourceManagementClient, PowerBIClient); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static PowerBIEmbeddedManagementClient GetPowerBIClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIEmbeddedTestRunner.cs b/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIEmbeddedTestRunner.cs new file mode 100644 index 000000000000..cb0367813a8b --- /dev/null +++ b/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBIEmbeddedTestRunner.cs @@ -0,0 +1,55 @@ +// ---------------------------------------------------------------------------------- +// +// 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.PowerBIEmbedded.Test.ScenarioTests +{ + public class PowerBIEmbeddedTestRunner + { + protected readonly ITestRunner TestRunner; + + protected PowerBIEmbeddedTestRunner(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.PowerBIEmbedded.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(); + } + } +} diff --git a/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBITests.cs b/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBITests.cs index 7eda47a3ec1d..07cf7512b778 100644 --- a/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBITests.cs +++ b/src/PowerBIEmbedded/PowerBIEmbedded.Test/ScenarioTests/PowerBITests.cs @@ -12,64 +12,58 @@ // 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.PowerBIEmbedded.Test.ScenarioTests { - public class PowerBITests : RMTestBase + public class PowerBITests : PowerBIEmbeddedTestRunner { - public XunitTracingInterceptor _logger; - - public PowerBITests(Xunit.Abstractions.ITestOutputHelper output) + public PowerBITests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetWorkspaceCollectionListAll() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-GetWorkspaceCollection_ListAll"); + TestRunner.RunTestScript("Test-GetWorkspaceCollection_ListAll"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetWorkspaceCollectionListByResourceGroup() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-GetWorkspaceCollection_ListByResourceGroup"); + TestRunner.RunTestScript("Test-GetWorkspaceCollection_ListByResourceGroup"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetWorkspaceCollectionByName() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-GetWorkspaceCollection_ByName"); + TestRunner.RunTestScript("Test-GetWorkspaceCollection_ByName"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetWorkspaceEmptyCollection() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-GetWorkspace_EmptyCollection"); + TestRunner.RunTestScript("Test-GetWorkspace_EmptyCollection"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestResetWorkspaceCollectionAccessKeys1() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-ResetWorkspaceCollectionAccessKeys_Key1"); + TestRunner.RunTestScript("Test-ResetWorkspaceCollectionAccessKeys_Key1"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestResetWorkspaceCollectionAccessKeys2() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-ResetWorkspaceCollectionAccessKeys_Key2"); + TestRunner.RunTestScript("Test-ResetWorkspaceCollectionAccessKeys_Key2"); } @@ -77,14 +71,14 @@ public void TestResetWorkspaceCollectionAccessKeys2() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetWorkspaceCollectionAccessKeys() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-GetWorkspaceCollectionAccessKeys"); + TestRunner.RunTestScript("Test-GetWorkspaceCollectionAccessKeys"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveWorkspaceCollection() { - PowerBIController.NewInstance.RunPsTest(_logger, "Test-RemoveWorkspaceCollection"); + TestRunner.RunTestScript("Test-RemoveWorkspaceCollection"); } } }