Skip to content

Add AttestationTestRunner and replace AttestationController #18076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method invocation is not necessary. Please remove it

GetAttestationManagementClient,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method invocation is not necessary. Please remove it

Copy link
Contributor

@ziyuezh576 ziyuezh576 May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all tests can pass after removed any of them. Message is:

Exception calling "Invoke" with "2" argument(s): "TestManagementClientHelper class wasn't initialized with the ResourceManagementClient client."
---> System.ArgumentException : TestManagementClientHelper class wasn't initialized with the ResourceManagementClient client.

GetAttestationClient
)
.Build();
}

private static ResourceManagementClient GetResourceManagementClient(MockContext context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method definition is not necessary. Please remove it

{
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}

private static AttestationManagementClient GetAttestationManagementClient(MockContext context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method definition is not necessary. Please remove it

{
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