diff --git a/src/MixedReality/MixedReality.Test/ScenarioTests/MixedRealityTestRunner.cs b/src/MixedReality/MixedReality.Test/ScenarioTests/MixedRealityTestRunner.cs new file mode 100644 index 000000000000..9f15a0303f68 --- /dev/null +++ b/src/MixedReality/MixedReality.Test/ScenarioTests/MixedRealityTestRunner.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.MixedReality.Test +{ + public class MixedRealityTestRunner + { + protected readonly ITestRunner TestRunner; + + protected MixedRealityTestRunner(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.Accounts.psd1"), + helper.GetRMModulePath("Az.MixedReality.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(); + } + } +} diff --git a/src/MixedReality/MixedReality.Test/ScenarioTests/RemoteRenderingAccountTests.cs b/src/MixedReality/MixedReality.Test/ScenarioTests/RemoteRenderingAccountTests.cs index 5fb5760fa9a2..a73accfb65a4 100644 --- a/src/MixedReality/MixedReality.Test/ScenarioTests/RemoteRenderingAccountTests.cs +++ b/src/MixedReality/MixedReality.Test/ScenarioTests/RemoteRenderingAccountTests.cs @@ -17,35 +17,31 @@ namespace Microsoft.Azure.Commands.MixedReality.Test { - public class RemoteRenderingAccountTests + public class RemoteRenderingAccountTests : MixedRealityTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public RemoteRenderingAccountTests(Xunit.Abstractions.ITestOutputHelper output) + public RemoteRenderingAccountTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new ServiceManagement.Common.Models.XunitTracingInterceptor(output); - ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoteRenderingAccountOperations() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoteRenderingAccountOperations"); + TestRunner.RunTestScript("Test-RemoteRenderingAccountOperations"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoteRenderingAccountOperationsWithPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoteRenderingAccountOperationsWithPiping"); + TestRunner.RunTestScript("Test-RemoteRenderingAccountOperationsWithPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListRemoteRenderingAccounts() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ListRemoteRenderingAccounts"); + TestRunner.RunTestScript("Test-ListRemoteRenderingAccounts"); } } } diff --git a/src/MixedReality/MixedReality.Test/ScenarioTests/SpatialAnchorsAccountTests.cs b/src/MixedReality/MixedReality.Test/ScenarioTests/SpatialAnchorsAccountTests.cs index 4817d1463f55..eea68972a5e1 100644 --- a/src/MixedReality/MixedReality.Test/ScenarioTests/SpatialAnchorsAccountTests.cs +++ b/src/MixedReality/MixedReality.Test/ScenarioTests/SpatialAnchorsAccountTests.cs @@ -17,35 +17,31 @@ namespace Microsoft.Azure.Commands.MixedReality.Test { - public class SpatialAnchorsAccountTests + public class SpatialAnchorsAccountTests : MixedRealityTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public SpatialAnchorsAccountTests(Xunit.Abstractions.ITestOutputHelper output) + public SpatialAnchorsAccountTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new ServiceManagement.Common.Models.XunitTracingInterceptor(output); - ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSpatialAnchorsAccountOperations() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-SpatialAnchorsAccountOperations"); + TestRunner.RunTestScript("Test-SpatialAnchorsAccountOperations"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSpatialAnchorsAccountOperationsWithPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-SpatialAnchorsAccountOperationsWithPiping"); + TestRunner.RunTestScript("Test-SpatialAnchorsAccountOperationsWithPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListSpatialAnchorsAccounts() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ListSpatialAnchorsAccounts"); + TestRunner.RunTestScript("Test-ListSpatialAnchorsAccounts"); } } } diff --git a/src/MixedReality/MixedReality.Test/ScenarioTests/TestController.cs b/src/MixedReality/MixedReality.Test/ScenarioTests/TestController.cs deleted file mode 100644 index c7d1c56f9bee..000000000000 --- a/src/MixedReality/MixedReality.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,133 +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; - -namespace Microsoft.Azure.Commands.MixedReality.Test -{ - using Common.Authentication; - using Management.Internal.Resources; - using Management.MixedReality; - using Microsoft.Azure.Test.HttpRecorder; - using Microsoft.Rest.ClientRuntime.Azure.TestFramework; - using Microsoft.WindowsAzure.Commands.ScenarioTest; - using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; - - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public MixedRealityClient MixedRealityClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - ResourceManagementClient = GetResourceManagementClient(context); - MixedRealityClient = GetMixedRealityClient(context); - - _helper.SetupManagementClients( - ResourceManagementClient, - MixedRealityClient); - } - - 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 d = 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, 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("Az.Accounts.psd1"), - _helper.GetRMModulePath("Az.MixedReality.psd1"), - "AzureRM.Resources.ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - protected ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static MixedRealityClient GetMixedRealityClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}