Skip to content

Commit dd75640

Browse files
authored
Merge pull request Azure#9507 from shleiAmy/attestation-powershell-module-cmdlet
Public Preview of Attestation Service
2 parents 7187100 + 0900c8e commit dd75640

30 files changed

+4979
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PsModuleName>Attestation</PsModuleName>
5+
</PropertyGroup>
6+
7+
<Import Project="$(MSBuildThisFileDirectory)..\..\Az.Test.props" />
8+
9+
<PropertyGroup>
10+
<RootNamespace>Microsoft.Azure.Commands.Attestation.Test</RootNamespace>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Azure.Management.Attestation" Version="0.9.5-preview" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Reference Include="Microsoft.Azure.Management.Attestation">
19+
</Reference>
20+
</ItemGroup>
21+
22+
</Project>
23+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 System.Reflection;
16+
using System.Runtime.CompilerServices;
17+
using System.Runtime.InteropServices;
18+
using Xunit;
19+
20+
// General Information about an assembly is controlled through the following
21+
// set of attributes. Change these attribute values to modify the information
22+
// associated with an assembly.
23+
[assembly: AssemblyTitle( "Microsoft.Azure.Commands.Attestation.Test" )]
24+
[assembly: AssemblyDescription( "" )]
25+
[assembly: AssemblyConfiguration( "" )]
26+
[assembly: AssemblyCompany( "" )]
27+
[assembly: AssemblyProduct( "Microsoft.Azure.Commands.Attestation.Test" )]
28+
[assembly: AssemblyCopyright( "Copyright © 2019" )]
29+
[assembly: AssemblyTrademark( "" )]
30+
[assembly: AssemblyCulture( "" )]
31+
32+
// Setting ComVisible to false makes the types in this assembly not visible
33+
// to COM components. If you need to access a type in this assembly from
34+
// COM, set the ComVisible attribute to true on that type.
35+
[assembly: ComVisible( false )]
36+
37+
38+
// Version information for an assembly consists of the following four values:
39+
//
40+
// Major Version
41+
// Minor Version
42+
// Build Number
43+
// Revision
44+
//
45+
// You can specify all the values or you can default the Build and Revision Numbers
46+
// by using the '*' as shown below:
47+
48+
[assembly: AssemblyVersion( "1.0.0" )]
49+
[assembly: AssemblyFileVersion("1.0.0")]
50+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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.Commands.Common.Authentication;
16+
using Microsoft.Azure.Management.Attestation;
17+
using Microsoft.Azure.ServiceManagement.Common.Models;
18+
using Microsoft.Azure.Test.HttpRecorder;
19+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Diagnostics;
23+
using System.IO;
24+
using System.Linq;
25+
using Microsoft.Azure.Management.Internal.Resources;
26+
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
27+
28+
namespace Microsoft.Azure.Commands.Attestation.Test
29+
{
30+
class AttestationController
31+
{
32+
private readonly EnvironmentSetupHelper _helper;
33+
34+
35+
public ResourceManagementClient ResourceClient { get; private set; }
36+
37+
public AttestationManagementClient AttestationManagementClient { get; private set; }
38+
39+
public static AttestationController NewInstance => new AttestationController();
40+
41+
public AttestationController()
42+
{
43+
_helper = new EnvironmentSetupHelper();
44+
}
45+
46+
47+
public void RunPowerShellTest(XunitTracingInterceptor logger, params string[] scripts)
48+
{
49+
var sf = new StackTrace().GetFrame(1);
50+
var callingClassType = sf.GetMethod().ReflectedType?.ToString();
51+
var mockName = sf.GetMethod().Name;
52+
53+
logger.Information(string.Format("Test method entered: {0}.{1}", callingClassType, mockName));
54+
_helper.TracingInterceptor = logger;
55+
56+
RunPowerShellTestWorkflow(
57+
() => scripts,
58+
// no custom cleanup
59+
null,
60+
callingClassType,
61+
mockName);
62+
}
63+
64+
public void RunPowerShellTestWorkflow(
65+
Func<string[]> scriptBuilder,
66+
Action cleanup,
67+
string callingClassType,
68+
string mockName)
69+
{
70+
var providers = new Dictionary<string, string>
71+
{
72+
{"Microsoft.Resources", null},
73+
{"Microsoft.Features", null},
74+
{"Microsoft.Authorization", null}
75+
};
76+
var providersToIgnore = new Dictionary<string, string>
77+
{
78+
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
79+
{"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"}
80+
};
81+
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore);
82+
HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
83+
using (var context = MockContext.Start(callingClassType, mockName))
84+
{
85+
SetupManagementClients(context);
86+
_helper.SetupEnvironment(AzureModule.AzureResourceManager);
87+
var callingClassName =
88+
callingClassType.Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries).Last();
89+
_helper.SetupModules(AzureModule.AzureResourceManager,
90+
"ScenarioTests\\Common.ps1",
91+
"ScenarioTests\\" + callingClassName + ".ps1",
92+
_helper.RMProfileModule,
93+
_helper.GetRMModulePath("AzureRM.Attestation.psd1"),
94+
"AzureRM.Resources.ps1");
95+
96+
try
97+
{
98+
var psScripts = scriptBuilder?.Invoke();
99+
if (psScripts != null)
100+
{
101+
_helper.RunPowerShellTest(psScripts);
102+
}
103+
}
104+
finally
105+
{
106+
cleanup?.Invoke();
107+
}
108+
}
109+
}
110+
private void SetupManagementClients(MockContext context)
111+
{
112+
ResourceClient = GetResourceManagementClient(context);
113+
AttestationManagementClient = GetAttestationManagementClient(context);
114+
_helper.SetupManagementClients(ResourceClient, AttestationManagementClient);
115+
}
116+
117+
private static ResourceManagementClient GetResourceManagementClient(MockContext context)
118+
{
119+
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
120+
}
121+
122+
private static AttestationManagementClient GetAttestationManagementClient(MockContext context)
123+
{
124+
return context.GetServiceClient<AttestationManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
125+
}
126+
}
127+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.Commands.ScenarioTest;
16+
using Microsoft.Azure.ServiceManagement.Common.Models;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
19+
using Xunit;
20+
21+
namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests
22+
{
23+
public class AttstationTests : RMTestBase
24+
{
25+
public XunitTracingInterceptor _logger;
26+
27+
public AttstationTests(Xunit.Abstractions.ITestOutputHelper output)
28+
{
29+
_logger = new XunitTracingInterceptor(output);
30+
XunitTracingInterceptor.AddToContext(_logger);
31+
TestExecutionHelpers.SetUpSessionAndProfile();
32+
}
33+
34+
#region New-AzureRmAttestation
35+
[Fact]
36+
[Trait(Category.AcceptanceType, Category.CheckIn)]
37+
public void TestCreateAttestation()
38+
{
39+
AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-CreateAttestation");
40+
}
41+
#endregion
42+
43+
#region Get-AzureRmAttestation
44+
[Fact]
45+
[Trait(Category.AcceptanceType, Category.CheckIn)]
46+
public void TestGetAttestation()
47+
{
48+
AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-GetAttestation");
49+
}
50+
#endregion
51+
52+
#region Remove-AzureRmAttestation
53+
[Fact]
54+
[Trait(Category.AcceptanceType, Category.CheckIn)]
55+
public void TestDeleteAttestationByName()
56+
{
57+
AttestationController.NewInstance.RunPowerShellTest(_logger, "Test-DeleteAttestationByName");
58+
}
59+
#endregion
60+
}
61+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
<#
16+
.SYNOPSIS
17+
Test New-AzAttestation
18+
#>
19+
#------------------------------Create-AzAttestation-----------------------------------
20+
function Test-CreateAttestation
21+
{
22+
$unknownRGName = getAssetName
23+
$attestationName = getAssetName
24+
$attestationPolicy = "SgxDisableDebugMode"
25+
26+
try
27+
{
28+
$rgName = Create-ResourceGroup
29+
$attestationCreated = New-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName -AttestationPolicy $attestationPolicy
30+
31+
Assert-NotNull attestationCreated
32+
Assert-AreEqual $attestationName $attestationCreated.Name
33+
Assert-NotNull attestationCreated.AttesUri
34+
Assert-NotNull attestationCreated.Id
35+
Assert-NotNull attestationCreated.Status
36+
37+
# Test throws for existing attestation
38+
Assert-Throws { New-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName -AttestationPolicy $attestationPolicy}
39+
40+
# Test throws for resourcegroup nonexistent
41+
Assert-Throws { New-AzAttestation -Name $attestationName -ResourceGroupName $unknownRGName -AttestationPolicy $attestationPolicy}
42+
}
43+
44+
finally
45+
{
46+
Clean-ResourceGroup $rgName.ResourceGroupName
47+
}
48+
}
49+
50+
<#
51+
.SYNOPSIS
52+
Test Get-AzAttestation
53+
#>
54+
#------------------------------Get-AzAttestation-----------------------------------
55+
function Test-GetAttestation
56+
{
57+
$attestationName = getAssetName
58+
$attestationPolicy = "SgxDisableDebugMode"
59+
try
60+
{
61+
$rgName = Create-ResourceGroup
62+
New-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName -AttestationPolicy $attestationPolicy
63+
64+
$got = Get-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName
65+
Assert-NotNull got
66+
Assert-AreEqual $attestationName $got.Name
67+
}
68+
69+
finally
70+
{
71+
Clean-ResourceGroup $rgName.ResourceGroupName
72+
}
73+
}
74+
75+
<#
76+
.SYNOPSIS
77+
Test Remove-AzAttestation
78+
#>
79+
#------------------------------Remove-AzAttestation-----------------------------------
80+
function Test-DeleteAttestationByName
81+
{
82+
$attestationName = getAssetName
83+
$attestationPolicy = "SgxDisableDebugMode"
84+
try
85+
{
86+
$rgName = Create-ResourceGroup
87+
New-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName -AttestationPolicy $attestationPolicy
88+
Remove-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName
89+
90+
Assert-Throws {Get-AzAttestation -Name $attestationName -ResourceGroupName $rgName.ResourceGroupName}
91+
}
92+
93+
finally
94+
{
95+
Clean-ResourceGroup $rgName.ResourceGroupName
96+
}
97+
}

0 commit comments

Comments
 (0)