|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Attestation; |
| 16 | +using Microsoft.Azure.Management.Attestation; |
| 17 | +using Microsoft.Azure.Test.HttpRecorder; |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using Microsoft.Azure.Management.Internal.Resources; |
| 21 | +using Microsoft.IdentityModel.Clients.ActiveDirectory; |
| 22 | +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| 23 | +using Microsoft.Azure.Commands.TestFx; |
| 24 | +using Xunit.Abstractions; |
| 25 | + |
| 26 | +namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests |
| 27 | +{ |
| 28 | + public class AttestationTestRunner |
| 29 | + { |
| 30 | + protected readonly ITestRunner TestRunner; |
| 31 | + |
| 32 | + protected AttestationTestRunner(ITestOutputHelper output) |
| 33 | + { |
| 34 | + TestRunner = TestManager.CreateInstance(output) |
| 35 | + .WithNewPsScriptFilename($"{GetType().Name}.ps1") |
| 36 | + .WithProjectSubfolderForTests("ScenarioTests") |
| 37 | + .WithCommonPsScripts(new[] |
| 38 | + { |
| 39 | + @"Common.ps1", |
| 40 | + @"../AzureRM.Resources.ps1" |
| 41 | + }) |
| 42 | + .WithNewRmModules(helper => new[] |
| 43 | + { |
| 44 | + helper.RMProfileModule, |
| 45 | + helper.GetRMModulePath("Az.Attestation.psd1") |
| 46 | + }) |
| 47 | + .WithNewRecordMatcherArguments( |
| 48 | + userAgentsToIgnore: new Dictionary<string, string> |
| 49 | + { |
| 50 | + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}, |
| 51 | + {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} |
| 52 | + }, |
| 53 | + resourceProviders: new Dictionary<string, string> |
| 54 | + { |
| 55 | + {"Microsoft.Resources", null}, |
| 56 | + {"Microsoft.Features", null}, |
| 57 | + {"Microsoft.Authorization", null} |
| 58 | + } |
| 59 | + ).WithManagementClients( |
| 60 | + GetResourceManagementClient, |
| 61 | + GetAttestationManagementClient, |
| 62 | + GetAttestationClient |
| 63 | + ) |
| 64 | + .Build(); |
| 65 | + } |
| 66 | + |
| 67 | + private static ResourceManagementClient GetResourceManagementClient(MockContext context) |
| 68 | + { |
| 69 | + return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
| 70 | + } |
| 71 | + |
| 72 | + private static AttestationManagementClient GetAttestationManagementClient(MockContext context) |
| 73 | + { |
| 74 | + return context.GetServiceClient<AttestationManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
| 75 | + } |
| 76 | + |
| 77 | + private static AttestationClient GetAttestationClient(MockContext context) |
| 78 | + { |
| 79 | + string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"); |
| 80 | + string accessToken = "fakefakefake"; |
| 81 | + |
| 82 | + // When recording, we should have a connection string passed into the code from the environment |
| 83 | + if (!string.IsNullOrEmpty(environmentConnectionString)) |
| 84 | + { |
| 85 | + // Gather test client credential information from the environment |
| 86 | + var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION")); |
| 87 | + string servicePrincipal = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalKey); |
| 88 | + string servicePrincipalSecret = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalSecretKey); |
| 89 | + string aadTenant = connectionInfo.GetValue<string>(ConnectionStringKeys.AADTenantKey); |
| 90 | + |
| 91 | + // Create credentials |
| 92 | + var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret); |
| 93 | + var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared); |
| 94 | + accessToken = authContext.AcquireTokenAsync("https://attest.azure.net", clientCredentials).Result.AccessToken; |
| 95 | + } |
| 96 | + |
| 97 | + return new AttestationClient(new AttestationCredentials(accessToken), HttpMockServer.CreateInstance()); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments