diff --git a/src/Search/Search.Test/ScenarioTests/SearchServiceTests.cs b/src/Search/Search.Test/ScenarioTests/SearchServiceTests.cs index 50f0cc457e59..a238ef14cf3f 100644 --- a/src/Search/Search.Test/ScenarioTests/SearchServiceTests.cs +++ b/src/Search/Search.Test/ScenarioTests/SearchServiceTests.cs @@ -12,136 +12,128 @@ // 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.Management.Search.Test.ScenarioTests { - public class SearchServiceTests : RMTestBase + public class SearchServiceTests : SearchTestRunner { - private readonly XunitTracingInterceptor traceInterceptor; - - public SearchServiceTests(ITestOutputHelper output) + public SearchServiceTests(ITestOutputHelper output) : base(output) { - traceInterceptor = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(this.traceInterceptor); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchService() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchService"); + TestRunner.RunTestScript("Test-NewAzSearchService"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchServiceBasic() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchServiceBasic"); + TestRunner.RunTestScript("Test-NewAzSearchServiceBasic"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchServiceL1() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchServiceL1"); + TestRunner.RunTestScript("Test-NewAzSearchServiceL1"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchServiceIdentity() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchServiceIdentity"); + TestRunner.RunTestScript("Test-NewAzSearchServiceIdentity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchServicePublicNetworkAccessDisabled() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchServicePublicNetworkAccessDisabled"); + TestRunner.RunTestScript("Test-NewAzSearchServicePublicNetworkAccessDisabled"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewSearchServiceIpRules() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzSearchServiceIpRules"); + TestRunner.RunTestScript("Test-NewAzSearchServiceIpRules"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetSearchService() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzSearchService"); + TestRunner.RunTestScript("Test-GetAzSearchService"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveSearchService() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-RemoveAzSearchService"); + TestRunner.RunTestScript("Test-RemoveAzSearchService"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetSearchService() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-SetAzSearchService"); + TestRunner.RunTestScript("Test-SetAzSearchService"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManageSearchServiceAdminKey() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManageAzSearchServiceAdminKey"); + TestRunner.RunTestScript("Test-ManageAzSearchServiceAdminKey"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManageSearchServiceQueryKey() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManageAzSearchServiceQueryKey"); + TestRunner.RunTestScript("Test-ManageAzSearchServiceQueryKey"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetPrivateLinkResource() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzSearchPrivateLinkResource"); + TestRunner.RunTestScript("Test-GetAzSearchPrivateLinkResource"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetPrivateLinkResourcePipeline() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzSearchPrivateLinkResourcePipeline"); + TestRunner.RunTestScript("Test-GetAzSearchPrivateLinkResourcePipeline"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManageSharedPrivateLinkResources() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManageAzSearchSharedPrivateLinkResources"); + TestRunner.RunTestScript("Test-ManageAzSearchSharedPrivateLinkResources"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManageSharedPrivateLinkResourcesPipeline() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManageAzSearchSharedPrivateLinkResourcePipeline"); + TestRunner.RunTestScript("Test-ManageAzSearchSharedPrivateLinkResourcePipeline"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManageSharedPrivateLinkResourcesJob() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManageAzSearchSharedPrivateLinkResourceJob"); + TestRunner.RunTestScript("Test-ManageAzSearchSharedPrivateLinkResourceJob"); } } } diff --git a/src/Search/Search.Test/ScenarioTests/SearchTestRunner.cs b/src/Search/Search.Test/ScenarioTests/SearchTestRunner.cs new file mode 100644 index 000000000000..0c90dafb6afd --- /dev/null +++ b/src/Search/Search.Test/ScenarioTests/SearchTestRunner.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.Management.Search.Test.ScenarioTests +{ + public class SearchTestRunner + { + protected readonly ITestRunner TestRunner; + + protected SearchTestRunner(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.Search.psd1"), + helper.GetRMModulePath("Az.Storage.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.Storage", null} + } + ) + .Build(); + } + } +} diff --git a/src/Search/Search.Test/TestController.cs b/src/Search/Search.Test/TestController.cs deleted file mode 100644 index 0b519881be53..000000000000 --- a/src/Search/Search.Test/TestController.cs +++ /dev/null @@ -1,139 +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.Internal.Resources; -using Microsoft.Azure.Management.Search; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.Azure.Management.Storage; - -namespace Microsoft.Azure.Commands.Management.Search.Test.ScenarioTests -{ - public class TestController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public SearchManagementClient SearchClient { get; private set; } - - public StorageManagementClient StorageManagementClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - public TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - public void RunPsTest(XunitTracingInterceptor traceInterceptor, params string[] scripts) - { - _helper.TracingInterceptor = traceInterceptor; - var sf = new StackTrace().GetFrame(1); - var callingClassType = sf.GetMethod().ReflectedType?.ToString(); - var mockName = sf.GetMethod().Name; - - RunPsTestWorkflow( - () => scripts, - // no custom cleanup - null, - callingClassType, - mockName); - } - - public void RunPsTestWorkflow( - Func scriptBuilder, - Action cleanup, - string callingClassType, - string mockName) - { - var providersToInclude = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null}, - {"Microsoft.Storage", null} - }; - - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providersToInclude, 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.Search.psd1"), - _helper.GetRMModulePath("AzureRM.Storage.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - "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); - SearchClient = GetAzureSearchManagementClient(context); - StorageManagementClient = GetStorageManagementClient(context); - - _helper.SetupManagementClients( - ResourceManagementClient, - SearchClient, - StorageManagementClient); - } - - private StorageManagementClient GetStorageManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static SearchManagementClient GetAzureSearchManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - } -}