diff --git a/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesAccountTests.cs b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesAccountTests.cs index 3661527934bd..0afa02b3a59d 100644 --- a/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesAccountTests.cs +++ b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesAccountTests.cs @@ -14,53 +14,44 @@ using Microsoft.Azure.Commands.Management.CognitiveServices.Test.ScenarioTests; -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using System; using Xunit; using Xunit.Abstractions; namespace CognitiveServices.Test.ScenarioTests { - public class CognitiveServicesAccountTests : RMTestBase + public class CognitiveServicesAccountTests : CognitiveServicesTestRunner { - XunitTracingInterceptor traceInterceptor; - - public CognitiveServicesAccountTests(ITestOutputHelper output) + public CognitiveServicesAccountTests(ITestOutputHelper output) : base(output) { - this.traceInterceptor = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(this.traceInterceptor); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAccount() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzureRmCognitiveServicesAccount"); + TestRunner.RunTestScript("Test-NewAzureRmCognitiveServicesAccount"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAccountWithCustomDomain() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzureRmCognitiveServicesAccountWithCustomDomain"); + TestRunner.RunTestScript("Test-NewAzureRmCognitiveServicesAccountWithCustomDomain"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAccountWithVnet() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzureRmCognitiveServicesAccountWithVnet"); + TestRunner.RunTestScript("Test-NewAzureRmCognitiveServicesAccountWithVnet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveAccount() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-RemoveAzureRmCognitiveServicesAccount"); + TestRunner.RunTestScript("Test-RemoveAzureRmCognitiveServicesAccount"); } @@ -68,42 +59,42 @@ public void TestRemoveAccount() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAccounts() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzureCognitiveServiceAccount"); + TestRunner.RunTestScript("Test-GetAzureCognitiveServiceAccount"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAsyncAccountOperations() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-AsyncAccountOperations"); + TestRunner.RunTestScript("Test-AsyncAccountOperations"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetAccount() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-SetAzureRmCognitiveServicesAccount"); + TestRunner.RunTestScript("Test-SetAzureRmCognitiveServicesAccount"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetAccountWithCustomDomain() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-SetAzureRmCognitiveServicesAccountWithCustomDomain"); + TestRunner.RunTestScript("Test-SetAzureRmCognitiveServicesAccountWithCustomDomain"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSetAccountWithVnet() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-SetAzureRmCognitiveServicesAccountWithVnet"); + TestRunner.RunTestScript("Test-SetAzureRmCognitiveServicesAccountWithVnet"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNetworkRuleSet() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NetworkRuleSet"); + TestRunner.RunTestScript("Test-NetworkRuleSet"); } @@ -111,56 +102,56 @@ public void TestNetworkRuleSet() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNetworkRuleSetDefaultActions() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NetworkRuleSetDefaultActions"); + TestRunner.RunTestScript("Test-NetworkRuleSetDefaultActions"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAccountKeys() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzureRmCognitiveServicesAccountKey"); + TestRunner.RunTestScript("Test-GetAzureRmCognitiveServicesAccountKey"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAccountKey() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-NewAzureRmCognitiveServicesAccountKey"); + TestRunner.RunTestScript("Test-NewAzureRmCognitiveServicesAccountKey"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAccountSkus() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzureRmCognitiveServicesAccountSkus"); + TestRunner.RunTestScript("Test-GetAzureRmCognitiveServicesAccountSkus"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAccountType() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetAzureRmCognitiveServicesAccountType"); + TestRunner.RunTestScript("Test-GetAzureRmCognitiveServicesAccountType"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPipingToGetKey() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-PipingGetAccountToGetKey"); + TestRunner.RunTestScript("Test-PipingGetAccountToGetKey"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPipingToSetAccount() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-PipingToSetAzureAccount"); + TestRunner.RunTestScript("Test-PipingToSetAzureAccount"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestMinMaxAccountNames() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-MinMaxAccountName"); + TestRunner.RunTestScript("Test-MinMaxAccountName"); } @@ -168,105 +159,105 @@ public void TestMinMaxAccountNames() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetUsages() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-GetUsages"); + TestRunner.RunTestScript("Test-GetUsages"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestManagedIdentity() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ManagedIdentity"); + TestRunner.RunTestScript("Test-ManagedIdentity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUserAssignedIdentity() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-UserAssignedIdentity"); + TestRunner.RunTestScript("Test-UserAssignedIdentity"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestEncryption() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-Encryption"); + TestRunner.RunTestScript("Test-Encryption"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUserOwnedStorage() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-UserOwnedStorage"); + TestRunner.RunTestScript("Test-UserOwnedStorage"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPrivateEndpoint() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-PrivateEndpoint"); + TestRunner.RunTestScript("Test-PrivateEndpoint"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestPublicNetworkAccess() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-PublicNetworkAccess"); + TestRunner.RunTestScript("Test-PublicNetworkAccess"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRestrictOutboundNetworkAccess() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-RestrictOutboundNetworkAccess"); + TestRunner.RunTestScript("Test-RestrictOutboundNetworkAccess"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDisableLocalAuth() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-DisableLocalAuth"); + TestRunner.RunTestScript("Test-DisableLocalAuth"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCapabilities() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-Capabilities"); + TestRunner.RunTestScript("Test-Capabilities"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestApiProperties() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ApiProperties"); + TestRunner.RunTestScript("Test-ApiProperties"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSoftDelete() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-SoftDelete"); + TestRunner.RunTestScript("Test-SoftDelete"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCommitmentPlan() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-CommitmentPlan"); + TestRunner.RunTestScript("Test-CommitmentPlan"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDeployment() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-Deployment"); + TestRunner.RunTestScript("Test-Deployment"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestListModels() { - TestController.NewInstance.RunPsTest(traceInterceptor, "Test-ListModels"); + TestRunner.RunTestScript("Test-ListModels"); } } } diff --git a/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesTestRunner.cs b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesTestRunner.cs new file mode 100644 index 000000000000..51326f0fd615 --- /dev/null +++ b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesTestRunner.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.CognitiveServices.Test.ScenarioTests +{ + public class CognitiveServicesTestRunner + { + protected readonly ITestRunner TestRunner; + + protected CognitiveServicesTestRunner(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.RMNetworkModule, + helper.GetRMModulePath("Az.CognitiveServices.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.Network", null} + } + ) + .Build(); + } + } +} diff --git a/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesUtilsTests.cs b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesUtilsTests.cs index 27c0e5bd99cf..ed64b4d9c79b 100644 --- a/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesUtilsTests.cs +++ b/src/CognitiveServices/CognitiveServices.Test/ScenarioTests/CognitiveServicesUtilsTests.cs @@ -12,27 +12,20 @@ // limitations under the License. // ---------------------------------------------------------------------------------- - using Microsoft.Azure.Commands.Management.CognitiveServices; -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; +using Microsoft.Azure.Commands.Management.CognitiveServices.Test.ScenarioTests; using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Xunit; using Xunit.Abstractions; namespace CognitiveServices.Test.ScenarioTests { - public class CognitiveServicesUtilsTests : RMTestBase + public class CognitiveServicesUtilsTests : CognitiveServicesTestRunner { - XunitTracingInterceptor traceInterceptor; - - public CognitiveServicesUtilsTests(ITestOutputHelper output) + public CognitiveServicesUtilsTests(ITestOutputHelper output) : base(output) { - this.traceInterceptor = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(this.traceInterceptor); - TestExecutionHelpers.SetUpSessionAndProfile(); } + [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUtils() diff --git a/src/CognitiveServices/CognitiveServices.Test/TestController.cs b/src/CognitiveServices/CognitiveServices.Test/TestController.cs deleted file mode 100644 index 7e0d931a3ea3..000000000000 --- a/src/CognitiveServices/CognitiveServices.Test/TestController.cs +++ /dev/null @@ -1,149 +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.CognitiveServices; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.IO; -using Microsoft.Azure.Management.Internal.Resources; -using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Microsoft.Azure.Management.Network; - -namespace Microsoft.Azure.Commands.Management.CognitiveServices.Test.ScenarioTests -{ - public class TestController - { - private readonly EnvironmentSetupHelper _helper; - - public ResourceManagementClient ResourceManagementClient { get; private set; } - - public CognitiveServicesManagementClient CognitiveServicesClient { get; private set; } - - public NetworkManagementClient NetworkClient { get; private set; } - - public string UserDomain { 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 d = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null}, - {"Microsoft.Network", 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)) - { - 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.RMNetworkModule, - "AzureRM.Resources.ps1", - _helper.GetRMModulePath("AzureRM.CognitiveServices.psd1") - ); - - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - - private void SetupManagementClients(RestTestFramework.MockContext context) - { - ResourceManagementClient = GetResourceManagementClient(context); - NetworkClient = GetNetworkClient(context); - CognitiveServicesClient = GetCognitiveServicesManagementClient(context); - - _helper.SetupManagementClients( - ResourceManagementClient, - NetworkClient, - CognitiveServicesClient); - } - - private static ResourceManagementClient GetResourceManagementClient(RestTestFramework.MockContext context) - { - return context.GetServiceClient(RestTestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - - private static CognitiveServicesManagementClient GetCognitiveServicesManagementClient(RestTestFramework.MockContext context) - { - return context.GetServiceClient(RestTestFramework.TestEnvironmentFactory.GetTestEnvironment()); - } - - protected NetworkManagementClient GetNetworkClient(RestTestFramework.MockContext context) - { - NetworkManagementClient client = - context.GetServiceClient( - RestTestFramework.TestEnvironmentFactory.GetTestEnvironment()); - return client; - } - } -}