diff --git a/src/Media/Media.Test/ScenarioTests/MediaTestRunner.cs b/src/Media/Media.Test/ScenarioTests/MediaTestRunner.cs new file mode 100644 index 000000000000..cbe1978c7a2c --- /dev/null +++ b/src/Media/Media.Test/ScenarioTests/MediaTestRunner.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.Media.Test.ScenarioTests +{ + public class MediaTestRunner + { + protected readonly ITestRunner TestRunner; + + protected MediaTestRunner(ITestOutputHelper output) + { + TestRunner = TestManager.CreateInstance(output) + .WithNewPsScriptFilename($"{GetType().Name}.ps1") + .WithProjectSubfolderForTests("ScenarioTests") + .WithCommonPsScripts(new[] + { + @"Common.ps1", + @"../AzureRM.Resources.ps1", + @"../AzureRM.Storage.ps1" + }) + .WithNewRmModules(helper => new[] + { + helper.RMProfileModule, + helper.GetRMModulePath("Az.Media.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/Media/Media.Test/ScenarioTests/MediaTests.cs b/src/Media/Media.Test/ScenarioTests/MediaTests.cs index 3c4c34dbc332..8d5d16a3b40b 100644 --- a/src/Media/Media.Test/ScenarioTests/MediaTests.cs +++ b/src/Media/Media.Test/ScenarioTests/MediaTests.cs @@ -20,28 +20,24 @@ namespace Microsoft.Azure.Commands.Media.Test.ScenarioTests /// /// Test for all media related cmdlets /// - public class MediaTests + public class MediaTests : MediaTestRunner { - private ServiceManagement.Common.Models.XunitTracingInterceptor _logger; - - public MediaTests(Xunit.Abstractions.ITestOutputHelper output) + public MediaTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new ServiceManagement.Common.Models.XunitTracingInterceptor(output); - ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(_logger); } [Fact(Skip = "Old ResourceManager version in test controller. Udpate and re-record.")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMedia() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-Media"); + TestRunner.RunTestScript("Test-Media"); } [Fact(Skip = "Old ResourceManager version in test controller. Udpate and re-record.")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMediaWithPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-MediaWithPiping"); + TestRunner.RunTestScript("Test-MediaWithPiping"); } } } diff --git a/src/Media/Media.Test/ScenarioTests/TestController.cs b/src/Media/Media.Test/ScenarioTests/TestController.cs deleted file mode 100644 index fb6fe0948e83..000000000000 --- a/src/Media/Media.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,135 +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.Media; -using Microsoft.Azure.Management.Storage.Version2017_10_01; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory; -using Microsoft.Azure.Management.Internal.Resources; - -namespace Microsoft.Azure.Commands.Media.Test.ScenarioTests -{ - /// - /// Setup for Scenario Tests - /// - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public MediaServicesManagementClient MediaServicesManagementClient { get; private set; } - - public StorageManagementClient StorageManagementClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(MockContext context) - { - ResourceManagementClient = context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - StorageManagementClient = context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - MediaServicesManagementClient = context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - - _helper.SetupManagementClients( - ResourceManagementClient, - StorageManagementClient, - MediaServicesManagementClient); - } - - /// - /// Methods for invoking PowerShell scripts - /// - /// - /// - 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); - } - - private 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("AzureRM.Media.psd1"), - "AzureRM.Resources.ps1", - "AzureRM.Storage.ps1"); - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - } -}