Skip to content

Commit 81c1fea

Browse files
v-yuzhichenZiyue Zheng
and
Ziyue Zheng
authored
Add AttestationTestRunner and replace AttestationController (#18076)
* Add AttestationTestRunner and replace AttestationController * Add AttestationTestRunner and replace AttestationController Co-authored-by: Ziyue Zheng <[email protected]>
1 parent ec77d18 commit 81c1fea

File tree

5 files changed

+122
-233
lines changed

5 files changed

+122
-233
lines changed

src/Attestation/Attestation.Test/ScenarioTests/AttestationController.cs

Lines changed: 0 additions & 187 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

src/Attestation/Attestation.Test/ScenarioTests/AttstationPolicySignerTests.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,43 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.Commands.ScenarioTest;
16-
using Microsoft.Azure.ServiceManagement.Common.Models;
1715
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18-
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
1916
using Xunit;
2017

2118
namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests
2219
{
23-
public class AttestationPolicySignerTests : RMTestBase
20+
public class AttestationPolicySignerTests : AttestationTestRunner
2421
{
25-
public XunitTracingInterceptor _logger;
26-
27-
public AttestationPolicySignerTests(Xunit.Abstractions.ITestOutputHelper output)
22+
public AttestationPolicySignerTests(Xunit.Abstractions.ITestOutputHelper output) : base(output)
2823
{
29-
_logger = new XunitTracingInterceptor(output);
30-
XunitTracingInterceptor.AddToContext(_logger);
31-
TestExecutionHelpers.SetUpSessionAndProfile();
3224
}
3325

3426
[Fact]
3527
[Trait(Category.AcceptanceType, Category.CheckIn)]
3628
public void TestGetAttestationPolicySigners()
3729
{
38-
AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetAttestationPolicySigners");
30+
TestRunner.RunTestScript("Test-GetAttestationPolicySigners");
3931
}
4032

4133
[Fact]
4234
[Trait(Category.AcceptanceType, Category.CheckIn)]
4335
public void TestGetDefaultProviderPolicySigners()
4436
{
45-
AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-GetDefaultProviderPolicySigners");
37+
TestRunner.RunTestScript("Test-GetDefaultProviderPolicySigners");
4638
}
4739

4840
[Fact]
4941
[Trait(Category.AcceptanceType, Category.CheckIn)]
5042
public void TestAddAttestationPolicySigner()
5143
{
52-
AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-AddAttestationPolicySigner");
44+
TestRunner.RunTestScript("Test-AddAttestationPolicySigner");
5345
}
5446

5547
[Fact]
5648
[Trait(Category.AcceptanceType, Category.CheckIn)]
5749
public void TestRemoveAttestationPolicySigner()
5850
{
59-
AttestationController.NewInstance.RunDataPowerShellTest(_logger, "Test-RemoveAttestationPolicySigner");
51+
TestRunner.RunTestScript("Test-RemoveAttestationPolicySigner");
6052
}
6153
}
6254
}

0 commit comments

Comments
 (0)