diff --git a/src/TrafficManager/TrafficManager.Test/ScenarioTests/EndpointTests.cs b/src/TrafficManager/TrafficManager.Test/ScenarioTests/EndpointTests.cs index 522fbcbee394..19b7aa8413f6 100644 --- a/src/TrafficManager/TrafficManager.Test/ScenarioTests/EndpointTests.cs +++ b/src/TrafficManager/TrafficManager.Test/ScenarioTests/EndpointTests.cs @@ -15,164 +15,159 @@ namespace Microsoft.Azure.Commands.TrafficManager.Test.ScenarioTests { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using ServiceManagement.Common.Models; using Xunit; using Xunit.Abstractions; - public class EndpointTests + public class EndpointTests : TrafficManagerTestRunner { - public XunitTracingInterceptor _logger; - - public EndpointTests(ITestOutputHelper output) + public EndpointTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAddEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AddEndpoint"); + TestRunner.RunTestScript("Test-AddEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDeleteEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-DeleteEndpoint"); + TestRunner.RunTestScript("Test-DeleteEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEndpointCrud() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCrud"); + TestRunner.RunTestScript("Test-EndpointCrud"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEndpointCrudGeo() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCrudGeo"); + TestRunner.RunTestScript("Test-EndpointCrudGeo"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEndpointCrudPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointCrudPiping"); + TestRunner.RunTestScript("Test-EndpointCrudPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateExistingEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateExistingEndpoint"); + TestRunner.RunTestScript("Test-CreateExistingEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateExistingEndpointFromNonExistingProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateExistingEndpointFromNonExistingProfile"); + TestRunner.RunTestScript("Test-CreateExistingEndpointFromNonExistingProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveExistingEndpointFromNonExistingProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoveExistingEndpointFromNonExistingProfile"); + TestRunner.RunTestScript("Test-RemoveExistingEndpointFromNonExistingProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetExistingEndpointFromNonExistingProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-GetExistingEndpointFromNonExistingProfile"); + TestRunner.RunTestScript("Test-GetExistingEndpointFromNonExistingProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveNonExistingEndpointFromProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-RemoveNonExistingEndpointFromProfile"); + TestRunner.RunTestScript("Test-RemoveNonExistingEndpointFromProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEnableEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EnableEndpoint"); + TestRunner.RunTestScript("Test-EnableEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDisableEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-DisableEndpoint"); + TestRunner.RunTestScript("Test-DisableEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEnableEndpointUsingPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EnableEndpointUsingPiping"); + TestRunner.RunTestScript("Test-EnableEndpointUsingPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEnableEndpointUsingPipingFromGetProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EnableEndpointUsingPipingFromGetProfile"); + TestRunner.RunTestScript("Test-EnableEndpointUsingPipingFromGetProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDisableEndpointUsingPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-DisableEndpointUsingPiping"); + TestRunner.RunTestScript("Test-DisableEndpointUsingPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEnableNonExistingEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EnableNonExistingEndpoint"); + TestRunner.RunTestScript("Test-EnableNonExistingEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDisableNonExistingEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-DisableNonExistingEndpoint"); + TestRunner.RunTestScript("Test-DisableNonExistingEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEndpointTypeCaseInsensitive() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-EndpointTypeCaseInsensitive"); + TestRunner.RunTestScript("Test-EndpointTypeCaseInsensitive"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPipeEndpointFromGetEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-PipeEndpointFromGetEndpoint"); + TestRunner.RunTestScript("Test-PipeEndpointFromGetEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPipeEndpointFromGetProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-PipeEndpointFromGetProfile"); + TestRunner.RunTestScript("Test-PipeEndpointFromGetProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAddAndRemoveCustomHeadersFromEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AddAndRemoveCustomHeadersFromEndpoint"); + TestRunner.RunTestScript("Test-AddAndRemoveCustomHeadersFromEndpoint"); } // This scenario is not supported with current API specs. Commenting this test. diff --git a/src/TrafficManager/TrafficManager.Test/ScenarioTests/NestedEndpointsTests.cs b/src/TrafficManager/TrafficManager.Test/ScenarioTests/NestedEndpointsTests.cs index 41ffd9797dbf..40a0a4fe3b92 100644 --- a/src/TrafficManager/TrafficManager.Test/ScenarioTests/NestedEndpointsTests.cs +++ b/src/TrafficManager/TrafficManager.Test/ScenarioTests/NestedEndpointsTests.cs @@ -15,31 +15,26 @@ namespace Microsoft.Azure.Commands.TrafficManager.Test.ScenarioTests { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using ServiceManagement.Common.Models; using Xunit; using Xunit.Abstractions; - public class NestedEndpointsTests + public class NestedEndpointsTests : TrafficManagerTestRunner { - public XunitTracingInterceptor _logger; - - public NestedEndpointsTests(ITestOutputHelper output) + public NestedEndpointsTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNestedEndpointsCreateUpdate() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NestedEndpointsCreateUpdate"); + TestRunner.RunTestScript("Test-NestedEndpointsCreateUpdate"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileWithNestedEndpointsGetPut() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileWithNestedEndpointsGetPut"); + TestRunner.RunTestScript("Test-ProfileWithNestedEndpointsGetPut"); } } } diff --git a/src/TrafficManager/TrafficManager.Test/ScenarioTests/ProfileTests.cs b/src/TrafficManager/TrafficManager.Test/ScenarioTests/ProfileTests.cs index 12bde5095091..5d220be984e1 100644 --- a/src/TrafficManager/TrafficManager.Test/ScenarioTests/ProfileTests.cs +++ b/src/TrafficManager/TrafficManager.Test/ScenarioTests/ProfileTests.cs @@ -15,171 +15,166 @@ namespace Microsoft.Azure.Commands.TrafficManager.Test.ScenarioTests { using Microsoft.WindowsAzure.Commands.ScenarioTest; - using ServiceManagement.Common.Models; using Xunit; using Xunit.Abstractions; - public class ProfileTests + public class ProfileTests : TrafficManagerTestRunner { - public XunitTracingInterceptor _logger; - - public ProfileTests(ITestOutputHelper output) + public ProfileTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileCrud() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileCrud"); + TestRunner.RunTestScript("Test-ProfileCrud"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileCrudWithPiping() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileCrudWithPiping"); + TestRunner.RunTestScript("Test-ProfileCrudWithPiping"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateDeleteUsingProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CreateDeleteUsingProfile"); + TestRunner.RunTestScript("Test-CreateDeleteUsingProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCrudWithEndpoint() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CrudWithEndpoint"); + TestRunner.RunTestScript("Test-CrudWithEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCrudWithEndpointGeo() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-CrudWithEndpointGeo"); + TestRunner.RunTestScript("Test-CrudWithEndpointGeo"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListProfilesInResourceGroup() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ListProfilesInResourceGroup"); + TestRunner.RunTestScript("Test-ListProfilesInResourceGroup"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListProfilesInSubscription() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ListProfilesInSubscription"); + TestRunner.RunTestScript("Test-ListProfilesInSubscription"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListProfilesWhereObject() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ListProfilesWhereObject"); + TestRunner.RunTestScript("Test-ListProfilesWhereObject"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileNewAlreadyExists() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileNewAlreadyExists"); + TestRunner.RunTestScript("Test-ProfileNewAlreadyExists"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileRemoveNonExisting() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileRemoveNonExisting"); + TestRunner.RunTestScript("Test-ProfileRemoveNonExisting"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileEnable() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileEnable"); + TestRunner.RunTestScript("Test-ProfileEnable"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileEnablePipeline() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileEnablePipeline"); + TestRunner.RunTestScript("Test-ProfileEnablePipeline"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileEnableNonExisting() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileEnableNonExisting"); + TestRunner.RunTestScript("Test-ProfileEnableNonExisting"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileDisable() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileDisable"); + TestRunner.RunTestScript("Test-ProfileDisable"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileDisablePipeline() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileDisablePipeline"); + TestRunner.RunTestScript("Test-ProfileDisablePipeline"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileDisableNonExisting() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileDisableNonExisting"); + TestRunner.RunTestScript("Test-ProfileDisableNonExisting"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileMonitorDefault() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileMonitorDefaults"); + TestRunner.RunTestScript("Test-ProfileMonitorDefaults"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileMonitorCustom() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileMonitorCustom"); + TestRunner.RunTestScript("Test-ProfileMonitorCustom"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileMonitorProtocol() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileMonitorProtocol"); + TestRunner.RunTestScript("Test-ProfileMonitorProtocol"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestProfileMonitorParameterAliases() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ProfileMonitorParameterAliases"); + TestRunner.RunTestScript("Test-ProfileMonitorParameterAliases"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAddAndRemoveCustomHeadersFromProfile() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AddAndRemoveCustomHeadersFromProfile"); + TestRunner.RunTestScript("Test-AddAndRemoveCustomHeadersFromProfile"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAddAndRemoveExpectedStatusCodeRanges() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-AddAndRemoveExpectedStatusCodeRanges"); + TestRunner.RunTestScript("Test-AddAndRemoveExpectedStatusCodeRanges"); } } } diff --git a/src/TrafficManager/TrafficManager.Test/ScenarioTests/TestController.cs b/src/TrafficManager/TrafficManager.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 78def7d7077b..000000000000 --- a/src/TrafficManager/TrafficManager.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,131 +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.Test.HttpRecorder; -using System.Collections.Generic; -using System.Diagnostics; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.Management.TrafficManager; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; -using System; -using System.IO; -using System.Linq; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework; - -namespace Microsoft.Azure.Commands.TrafficManager.Test.ScenarioTests -{ - public class TestController : RMTestBase - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public TrafficManagerManagementClient TrafficManagerManagementClient { get; private set; } - - public static TestController NewInstance => new TestController(); - - protected TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - protected void SetupManagementClients(RestTestFramework.MockContext context) - { - ResourceManagementClient = GetResourceManagementClient(context); - TrafficManagerManagementClient = GetFeatureClient(context); - - _helper.SetupManagementClients( - ResourceManagementClient, - TrafficManagerManagementClient); - } - - public void RunPowerShellTest(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 = RestTestFramework.MockContext.Start(callingClassType, mockName)) - { - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - - SetupManagementClients(context); - - var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.TrafficManager.psd1"), - "AzureRM.Resources.ps1"); - - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - protected ResourceManagementClient GetResourceManagementClient(RestTestFramework.MockContext context) - { - return context.GetServiceClient(RestTestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - - private static TrafficManagerManagementClient GetFeatureClient(RestTestFramework.MockContext context) - { - return context.GetServiceClient(RestTestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - } -} diff --git a/src/TrafficManager/TrafficManager.Test/ScenarioTests/TrafficManagerTestRunner.cs b/src/TrafficManager/TrafficManager.Test/ScenarioTests/TrafficManagerTestRunner.cs new file mode 100644 index 000000000000..89faf38abab1 --- /dev/null +++ b/src/TrafficManager/TrafficManager.Test/ScenarioTests/TrafficManagerTestRunner.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.TrafficManager.Test.ScenarioTests +{ + public class TrafficManagerTestRunner + { + protected readonly ITestRunner TestRunner; + + protected TrafficManagerTestRunner(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.TrafficManager.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(); + } + } +}