diff --git a/src/Aks/Aks.Test/ScenarioTests/AksTestRunner.cs b/src/Aks/Aks.Test/ScenarioTests/AksTestRunner.cs new file mode 100644 index 000000000000..778f1b10bea6 --- /dev/null +++ b/src/Aks/Aks.Test/ScenarioTests/AksTestRunner.cs @@ -0,0 +1,101 @@ +// ---------------------------------------------------------------------------------- +// +// 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 Microsoft.Azure.Commands.Common.Authentication; +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; +using System.Collections.Generic; +using Microsoft.Azure.Test.HttpRecorder; +using Microsoft.Azure.Commands.Common.Authentication.Models; +using System.Reflection; +using System.IO; +using Microsoft.Azure.Management.Internal.Resources; +using Microsoft.Azure.Management.ContainerService; +using Microsoft.Azure.Management.Authorization.Version2015_07_01; +using Microsoft.Azure.Commands.Common.MSGraph.Version1_0; +using Microsoft.Azure.Commands.TestFx; +using Xunit.Abstractions; + +namespace Commands.Aks.Test.ScenarioTests +{ + public class AksTestRunner + { + protected readonly ITestRunner TestRunner; + + protected AksTestRunner(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.Aks.psd1") + }) + .WithNewRecordMatcherArguments ( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2017-05-10"}, + {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} + }, + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Authorization", null} + } + ).WithMockContextAction(() => + { + if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Playback) + { + AzureSession.Instance.DataStore = new MemoryDataStore(); + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); + var subscription = HttpMockServer.Variables["SubscriptionId"]; + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); + var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""foo"",""client_secret"":""bar""}}"; + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); + } + else if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Record) + { + AzureSession.Instance.DataStore = new MemoryDataStore(); + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); + var subscription = HttpMockServer.Variables["SubscriptionId"]; + var currentEnvironment = TestEnvironmentFactory.GetTestEnvironment(); + string spn = null; + string spnSecret = null; + if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipal")) + { + spn = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipal"]; + } + if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipalSecret")) + { + spnSecret = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipalSecret"]; + } + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); + var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""" + spn + @""",""client_secret"":"""+ spnSecret + @"""}}"; + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); + } + } + ) + .Build(); + } + } +} diff --git a/src/Aks/Aks.Test/ScenarioTests/KubernetesTests.cs b/src/Aks/Aks.Test/ScenarioTests/KubernetesTests.cs index 33c514dd25e3..551723bbffea 100644 --- a/src/Aks/Aks.Test/ScenarioTests/KubernetesTests.cs +++ b/src/Aks/Aks.Test/ScenarioTests/KubernetesTests.cs @@ -1,83 +1,76 @@ -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 Commands.Aks.Test.ScenarioTests { - public class KubernetesTests : RMTestBase + public class KubernetesTests : AksTestRunner { - XunitTracingInterceptor _logger; - public KubernetesTests(ITestOutputHelper output) + public KubernetesTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestSimpleAzureKubernetes() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksSimple"); + TestRunner.RunTestScript("Test-NewAzAksSimple"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAzureKubernetes() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAks"); + TestRunner.RunTestScript("Test-NewAzAks"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAzureKubernetesByServicePrincipal() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksByServicePrincipal"); + TestRunner.RunTestScript("Test-NewAzAksByServicePrincipal"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAzureKubernetesAddons() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksAddons"); + TestRunner.RunTestScript("Test-NewAzAksAddons"); } [Fact(Skip = "Please make sure you have graph directory.read permission which is required for grant acrpull permission.")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestNewAzAksWithAcr() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksWithAcr"); + TestRunner.RunTestScript("Test-NewAzAksWithAcr"); } [Fact(Skip = "Updating service principal profile is not allowed on MSI cluster.")] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestResetAzureKubernetesServicePrincipal() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ResetAzureKubernetesServicePrincipal"); + TestRunner.RunTestScript("Test-ResetAzureKubernetesServicePrincipal"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestUpgradeKubernetesVersion() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-UpgradeKubernetesVersion"); + TestRunner.RunTestScript("Test-UpgradeKubernetesVersion"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestLoadBalancer() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-LoadBalancer"); + TestRunner.RunTestScript("Test-LoadBalancer"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestApiServiceAccess() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-ApiServiceAccess"); + TestRunner.RunTestScript("Test-ApiServiceAccess"); } } } \ No newline at end of file diff --git a/src/Aks/Aks.Test/ScenarioTests/NodePoolTests.cs b/src/Aks/Aks.Test/ScenarioTests/NodePoolTests.cs index 6a0494a0a77f..996be3d33276 100644 --- a/src/Aks/Aks.Test/ScenarioTests/NodePoolTests.cs +++ b/src/Aks/Aks.Test/ScenarioTests/NodePoolTests.cs @@ -1,27 +1,20 @@ -using Microsoft.Azure.Commands.ScenarioTest; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; +using Microsoft.WindowsAzure.Commands.ScenarioTest; using Xunit; using Xunit.Abstractions; namespace Commands.Aks.Test.ScenarioTests { - public class NodePoolTests : RMTestBase + public class NodePoolTests : AksTestRunner { - XunitTracingInterceptor _logger; - public NodePoolTests(ITestOutputHelper output) + public NodePoolTests(ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAksNodePool() { - TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewNodePool"); + TestRunner.RunTestScript("Test-NewNodePool"); } } } diff --git a/src/Aks/Aks.Test/ScenarioTests/TestController.cs b/src/Aks/Aks.Test/ScenarioTests/TestController.cs deleted file mode 100644 index 60ddfa320cae..000000000000 --- a/src/Aks/Aks.Test/ScenarioTests/TestController.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Diagnostics; -using System.Linq; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; -using System.Collections.Generic; -using Microsoft.Azure.Test.HttpRecorder; -using Microsoft.Azure.Commands.Common.Authentication.Models; -using System.Reflection; -using System.IO; -using Microsoft.Azure.Management.Internal.Resources; -using Microsoft.Azure.ServiceManagement.Common.Models; -using Microsoft.Azure.Management.ContainerService; -using Microsoft.Azure.Management.Authorization.Version2015_07_01; -using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.Azure.Commands.Common.MSGraph.Version1_0; - -namespace Commands.Aks.Test.ScenarioTests -{ - public class TestController - { - private readonly EnvironmentSetupHelper _helper; - - public ContainerServiceClient ContainerServiceClient { get; private set; } - - public TestController() - { - _helper = new EnvironmentSetupHelper(); - } - - public static TestController NewInstance => new TestController(); - - public string UserDomain { get; private set; } - public MicrosoftGraphClient InternalGraphManagementClient { get; private set; } - - public ResourceManagementClient InternalResourceManagementClient { get; private set; } - - public AuthorizationManagementClient InternalAuthorizationManagementClient { get; private set; } - - 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; - - var d = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null} - }; - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2017-05-10"}, - {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d, providersToIgnore); - HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords"); - - using (var context = MockContext.Start(callingClassType, mockName)) - { - SetupManagementClients(context); - var callingClassName = callingClassType?.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last(); - - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - _helper.SetupModules(AzureModule.AzureResourceManager, - _helper.RMProfileModule, - _helper.GetRMModulePath(@"AzureRM.Aks.psd1"), - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - "AzureRM.Resources.ps1"); - - if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Playback) - { - AzureSession.Instance.DataStore = new MemoryDataStore(); - var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); - var subscription = HttpMockServer.Variables["SubscriptionId"]; - AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); - var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""foo"",""client_secret"":""bar""}}"; - AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); - } - else if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Record) - { - AzureSession.Instance.DataStore = new MemoryDataStore(); - var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); - var subscription = HttpMockServer.Variables["SubscriptionId"]; - var currentEnvironment = TestEnvironmentFactory.GetTestEnvironment(); - string spn = null; - string spnSecret = null; - if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipal")) - { - spn = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipal"]; - } - if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipalSecret")) - { - spnSecret = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipalSecret"]; - } - AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); - var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""" + spn + @""",""client_secret"":"""+ spnSecret + @"""}}"; - AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); - } - - _helper.RunPowerShellTest(scripts); - } - } - - private void SetupManagementClients(MockContext context) - { - ContainerServiceClient = GetContainerServiceClient(context); - InternalResourceManagementClient = GetInternalResourceManagementClient(context); - InternalAuthorizationManagementClient = GetAuthorizationManagementClient(context); - InternalGraphManagementClient = GetGraphManagementClient(context); - _helper.SetupManagementClients(ContainerServiceClient, - InternalResourceManagementClient, - InternalAuthorizationManagementClient, - InternalGraphManagementClient); - } - - private static ContainerServiceClient GetContainerServiceClient(MockContext context) - { - return context.GetServiceClient(); - } - private MicrosoftGraphClient GetGraphManagementClient(MockContext context) - { - return context.GetServiceClient(); - } - private static AuthorizationManagementClient GetAuthorizationManagementClient(MockContext context) - { - return context.GetServiceClient(); - } - private static ResourceManagementClient GetInternalResourceManagementClient(MockContext context) - { - return context.GetServiceClient(); - } - } -} \ No newline at end of file