diff --git a/src/Attestation/Attestation.Test/ScenarioTests/AttestationController.cs b/src/Attestation/Attestation.Test/ScenarioTests/AttestationController.cs deleted file mode 100644 index 21bd6571dbbf..000000000000 --- a/src/Attestation/Attestation.Test/ScenarioTests/AttestationController.cs +++ /dev/null @@ -1,187 +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.Attestation; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Management.Attestation; -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.Azure.Management.Internal.Resources; -using Microsoft.IdentityModel.Clients.ActiveDirectory; -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; - -namespace Microsoft.Azure.Commands.Attestation.Test -{ - class AttestationController - { - private readonly EnvironmentSetupHelper _helper; - - public static AttestationController NewInstance => new AttestationController(); - - public AttestationController() - { - _helper = new EnvironmentSetupHelper(); - } - - 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; - - logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); - _helper.TracingInterceptor = logger; - - RunPowerShellTestWorkflow( - () => scripts, - // no custom cleanup - null, - callingClassType, - mockName, - true, - false); - } - - public void RunDataPowerShellTest(XunitTracingInterceptor logger, params string[] scripts) - { - var sf = new StackTrace().GetFrame(1); - var callingClassType = sf.GetMethod().ReflectedType?.ToString(); - var mockName = sf.GetMethod().Name; - - logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName)); - _helper.TracingInterceptor = logger; - - RunPowerShellTestWorkflow( - () => scripts, - // no custom cleanup - null, - callingClassType, - mockName, - false, - true); - } - - public void RunPowerShellTestWorkflow( - Func scriptBuilder, - Action cleanup, - string callingClassType, - string mockName, - bool setupManagementClients, - bool setupDataClient) - { - var providers = new Dictionary - { - {"Microsoft.Resources", null}, - {"Microsoft.Features", null}, - {"Microsoft.Authorization", null} - }; - var providersToIgnore = new Dictionary - { - {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}, - {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} - }; - HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore); - HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords"); - using (var context = MockContext.Start(callingClassType, mockName)) - { - if (setupManagementClients) - { - SetupManagementClients(context); - _helper.SetupEnvironment(AzureModule.AzureResourceManager); - } - - if (setupDataClient) - { - SetupDataClient(context); - } - - var callingClassName = - callingClassType.Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries).Last(); - _helper.SetupModules(AzureModule.AzureResourceManager, - "ScenarioTests\\Common.ps1", - "ScenarioTests\\" + callingClassName + ".ps1", - _helper.RMProfileModule, - _helper.GetRMModulePath("AzureRM.Attestation.psd1"), - "AzureRM.Resources.ps1"); - - try - { - var psScripts = scriptBuilder?.Invoke(); - if (psScripts != null) - { - _helper.RunPowerShellTest(psScripts); - } - } - finally - { - cleanup?.Invoke(); - } - } - } - private void SetupManagementClients(MockContext context) - { - _helper.SetupManagementClients( - GetResourceManagementClient(context), - GetAttestationManagementClient(context) - ); - } - - private void SetupDataClient(MockContext context) - { - _helper.SetupManagementClients( - GetResourceManagementClient(context), - GetAttestationManagementClient(context), - GetAttestationClient(context) - ); - } - - private static ResourceManagementClient GetResourceManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static AttestationManagementClient GetAttestationManagementClient(MockContext context) - { - return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); - } - - private static AttestationClient GetAttestationClient(MockContext context) - { - string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"); - string accessToken = "fakefakefake"; - - // When recording, we should have a connection string passed into the code from the environment - if (!string.IsNullOrEmpty(environmentConnectionString)) - { - // Gather test client credential information from the environment - var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION")); - string servicePrincipal = connectionInfo.GetValue(ConnectionStringKeys.ServicePrincipalKey); - string servicePrincipalSecret = connectionInfo.GetValue(ConnectionStringKeys.ServicePrincipalSecretKey); - string aadTenant = connectionInfo.GetValue(ConnectionStringKeys.AADTenantKey); - - // Create credentials - var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret); - var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared); - accessToken = authContext.AcquireTokenAsync("https://attest.azure.net", clientCredentials).Result.AccessToken; - } - - return new AttestationClient(new AttestationCredentials(accessToken), HttpMockServer.CreateInstance()); - } - } -} diff --git a/src/Attestation/Attestation.Test/ScenarioTests/AttestationTestRunner.cs b/src/Attestation/Attestation.Test/ScenarioTests/AttestationTestRunner.cs new file mode 100644 index 000000000000..7cb8bba2c3b5 --- /dev/null +++ b/src/Attestation/Attestation.Test/ScenarioTests/AttestationTestRunner.cs @@ -0,0 +1,100 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Attestation; +using Microsoft.Azure.Management.Attestation; +using Microsoft.Azure.Test.HttpRecorder; +using System; +using System.Collections.Generic; +using Microsoft.Azure.Management.Internal.Resources; +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; +using Microsoft.Azure.Commands.TestFx; +using Xunit.Abstractions; + +namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests +{ + public class AttestationTestRunner + { + protected readonly ITestRunner TestRunner; + + protected AttestationTestRunner(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.Attestation.psd1") + }) + .WithNewRecordMatcherArguments( + userAgentsToIgnore: new Dictionary + { + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}, + {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} + }, + resourceProviders: new Dictionary + { + {"Microsoft.Resources", null}, + {"Microsoft.Features", null}, + {"Microsoft.Authorization", null} + } + ).WithManagementClients( + GetResourceManagementClient, + GetAttestationManagementClient, + GetAttestationClient + ) + .Build(); + } + + private static ResourceManagementClient GetResourceManagementClient(MockContext context) + { + return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); + } + + private static AttestationManagementClient GetAttestationManagementClient(MockContext context) + { + return context.GetServiceClient(TestEnvironmentFactory.GetTestEnvironment()); + } + + private static AttestationClient GetAttestationClient(MockContext context) + { + string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"); + string accessToken = "fakefakefake"; + + // When recording, we should have a connection string passed into the code from the environment + if (!string.IsNullOrEmpty(environmentConnectionString)) + { + // Gather test client credential information from the environment + var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION")); + string servicePrincipal = connectionInfo.GetValue(ConnectionStringKeys.ServicePrincipalKey); + string servicePrincipalSecret = connectionInfo.GetValue(ConnectionStringKeys.ServicePrincipalSecretKey); + string aadTenant = connectionInfo.GetValue(ConnectionStringKeys.AADTenantKey); + + // Create credentials + var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret); + var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared); + accessToken = authContext.AcquireTokenAsync("https://attest.azure.net", clientCredentials).Result.AccessToken; + } + + return new AttestationClient(new AttestationCredentials(accessToken), HttpMockServer.CreateInstance()); + } + } +} diff --git a/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicySignerTests.cs b/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicySignerTests.cs index 0a545e8ec9d0..44dec12ce16e 100644 --- a/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicySignerTests.cs +++ b/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicySignerTests.cs @@ -12,51 +12,43 @@ // 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; namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests { - public class AttestationPolicySignerTests : RMTestBase + public class AttestationPolicySignerTests : AttestationTestRunner { - public XunitTracingInterceptor _logger; - - public AttestationPolicySignerTests(Xunit.Abstractions.ITestOutputHelper output) + public AttestationPolicySignerTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAttestationPolicySigners() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetAttestationPolicySigners"); + TestRunner.RunTestScript("Test-GetAttestationPolicySigners"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetDefaultProviderPolicySigners() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetDefaultProviderPolicySigners"); + TestRunner.RunTestScript("Test-GetDefaultProviderPolicySigners"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestAddAttestationPolicySigner() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-AddAttestationPolicySigner"); + TestRunner.RunTestScript("Test-AddAttestationPolicySigner"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestRemoveAttestationPolicySigner() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-RemoveAttestationPolicySigner"); + TestRunner.RunTestScript("Test-RemoveAttestationPolicySigner"); } } } diff --git a/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicyTests.cs b/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicyTests.cs index 4503efd95bee..07f7b10d04fc 100644 --- a/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicyTests.cs +++ b/src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicyTests.cs @@ -12,44 +12,36 @@ // 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; namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests { - public class AttstationPolicyTests : RMTestBase + public class AttstationPolicyTests : AttestationTestRunner { - public XunitTracingInterceptor _logger; - - public AttstationPolicyTests(Xunit.Abstractions.ITestOutputHelper output) + public AttstationPolicyTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAttestationPolicy() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetAttestationPolicy"); + TestRunner.RunTestScript("Test-GetAttestationPolicy"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetDefaultProviderPolicy() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetDefaultProviderPolicy"); + TestRunner.RunTestScript("Test-GetDefaultProviderPolicy"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestResetAttestationPolicy() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-ResetAttestationPolicy"); + TestRunner.RunTestScript("Test-ResetAttestationPolicy"); } /// @@ -73,7 +65,7 @@ public void TestResetAttestationPolicy() [Trait(Category.AcceptanceType, Category.LiveOnly)] public void TestSetAttestationPolicy() { - AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-SetAttestationPolicy"); + TestRunner.RunTestScript("Test-SetAttestationPolicy"); } } } diff --git a/src/Attestation/Attestation.Test/ScenarioTests/AttstationTests.cs b/src/Attestation/Attestation.Test/ScenarioTests/AttstationTests.cs index 9254ad58f944..2a5a43029848 100644 --- a/src/Attestation/Attestation.Test/ScenarioTests/AttstationTests.cs +++ b/src/Attestation/Attestation.Test/ScenarioTests/AttstationTests.cs @@ -12,45 +12,37 @@ // 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; namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests { - public class AttstationTests : RMTestBase + public class AttstationTests : AttestationTestRunner { - public XunitTracingInterceptor _logger; - - public AttstationTests(Xunit.Abstractions.ITestOutputHelper output) + public AttstationTests(Xunit.Abstractions.ITestOutputHelper output) : base(output) { - _logger = new XunitTracingInterceptor(output); - XunitTracingInterceptor.AddToContext(_logger); - TestExecutionHelpers.SetUpSessionAndProfile(); } - #region New-AzureRmAttestation + #region New-AzureRmAttestation [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateAttestation() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-CreateAttestation"); + TestRunner.RunTestScript("Test-CreateAttestation"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateAttestationWithPolicySigningCertificate() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-CreateAttestationWithPolicySigningCertificate"); + TestRunner.RunTestScript("Test-CreateAttestationWithPolicySigningCertificate"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestCreateAttestationWithTags() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-CreateAttestationWithTags"); + TestRunner.RunTestScript("Test-CreateAttestationWithTags"); } #endregion @@ -59,21 +51,21 @@ public void TestCreateAttestationWithTags() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetAttestation() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-GetAttestation"); + TestRunner.RunTestScript("Test-GetAttestation"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetDefaultProviders() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-GetDefaultProviders"); + TestRunner.RunTestScript("Test-GetDefaultProviders"); } [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestGetDefaultProviderByLocation() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-GetDefaultProviderByLocation"); + TestRunner.RunTestScript("Test-GetDefaultProviderByLocation"); } #endregion @@ -82,7 +74,7 @@ public void TestGetDefaultProviderByLocation() [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestDeleteAttestationByName() { - AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-DeleteAttestationByName"); + TestRunner.RunTestScript("Test-DeleteAttestationByName"); } #endregion }