Skip to content

Commit

Permalink
Add AttestationTestRunner and replace AttestationController (#18076)
Browse files Browse the repository at this point in the history
* Add AttestationTestRunner and replace AttestationController

* Add AttestationTestRunner and replace AttestationController

Co-authored-by: Ziyue Zheng <[email protected]>
  • Loading branch information
v-yuzhichen and ziyuezh576 authored May 10, 2022
1 parent ec77d18 commit 81c1fea
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 233 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
{"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"}
},
resourceProviders: new Dictionary<string, string>
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null}
}
).WithManagementClients(
GetResourceManagementClient,
GetAttestationManagementClient,
GetAttestationClient
)
.Build();
}

private static ResourceManagementClient GetResourceManagementClient(MockContext context)
{
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}

private static AttestationManagementClient GetAttestationManagementClient(MockContext context)
{
return context.GetServiceClient<AttestationManagementClient>(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<string>(ConnectionStringKeys.ServicePrincipalKey);
string servicePrincipalSecret = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalSecretKey);
string aadTenant = connectionInfo.GetValue<string>(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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Loading

0 comments on commit 81c1fea

Please sign in to comment.