Skip to content
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

Batch cert cmdlets (not for 0.9.9 release) #1079

Merged
merged 4 commits into from
Oct 13, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace Microsoft.Azure.Commands.Batch.Test
/// </summary>
public static class BatchTestHelpers
{
internal const string TestCertificateFileName1 = "Resources\\BatchTestCert01.cer";
internal const string TestCertificateFileName2 = "Resources\\BatchTestCert02.cer";
internal const string TestCertificateAlgorithm = "sha1";
internal const string TestCertificatePassword = "Passw0rd";

/// <summary>
/// Builds an AccountResource object using the specified parameters
/// </summary>
Expand Down Expand Up @@ -168,6 +173,44 @@ public static RequestInterceptor CreateFakGetFileAndPropertiesResponseIntercepto
return interceptor;
}

/// <summary>
/// Builds a CertificateGetResponse object
/// </summary>
public static ProxyModels.CertificateGetResponse CreateCertificateGetResponse(string thumbprint)
{
ProxyModels.CertificateGetResponse response = new ProxyModels.CertificateGetResponse();
response.StatusCode = HttpStatusCode.OK;

ProxyModels.Certificate cert = new ProxyModels.Certificate();
cert.Thumbprint = thumbprint;

response.Certificate = cert;

return response;
}

/// <summary>
/// Builds a CertificateListResponse object
/// </summary>
public static ProxyModels.CertificateListResponse CreateCertificateListResponse(IEnumerable<string> certThumbprints)
{
ProxyModels.CertificateListResponse response = new ProxyModels.CertificateListResponse();
response.StatusCode = HttpStatusCode.OK;

List<ProxyModels.Certificate> certs = new List<ProxyModels.Certificate>();

foreach (string t in certThumbprints)
{
ProxyModels.Certificate cert = new ProxyModels.Certificate();
cert.Thumbprint = t;
certs.Add(cert);
}

response.Certificates = certs;

return response;
}

/// <summary>
/// Builds a CloudPoolGetResponse object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// ----------------------------------------------------------------------------------
//
// 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.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Batch.Protocol.Models;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;

namespace Microsoft.Azure.Commands.Batch.Test.Certificates
{
public class GetBatchCertificateCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
{
private GetBatchCertificateCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public GetBatchCertificateCommandTests()
{
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetBatchCertificateCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetBatchCertificateTest()
{
// Setup cmdlet to get a cert by its thumbprint
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.ThumbprintAlgorithm = "sha1";
cmdlet.Thumbprint = "123456789";
cmdlet.Filter = null;

// Build a Certificate instead of querying the service on a Get Certificate call
CertificateGetResponse response = BatchTestHelpers.CreateCertificateGetResponse(cmdlet.Thumbprint);
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateGetParameters, CertificateGetResponse>(response);
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Setup the cmdlet to write pipeline output to a list that can be examined later
List<PSCertificate> pipeline = new List<PSCertificate>();
commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCertificate>())).Callback<object>(c => pipeline.Add((PSCertificate)c));

cmdlet.ExecuteCmdlet();

// Verify that the cmdlet wrote the cert returned from the OM to the pipeline
Assert.Equal(1, pipeline.Count);
Assert.Equal(cmdlet.Thumbprint, pipeline[0].Thumbprint);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetBatchCertificateODataTest()
{
// Setup cmdlet to get a single certificate
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.ThumbprintAlgorithm = "sha1";
cmdlet.Thumbprint = "123456789";
cmdlet.Select = "thumbprint,state";

string requestSelect = null;

// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
CertificateGetResponse getResponse = BatchTestHelpers.CreateCertificateGetResponse(cmdlet.Thumbprint);
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateGetParameters, CertificateGetResponse>(getResponse);
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
{
requestSelect = request.Parameters.DetailLevel.SelectClause;

return Task.FromResult(response);
});
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };

cmdlet.ExecuteCmdlet();

Assert.Equal(cmdlet.Select, requestSelect);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ListBatchCertificatesODataTest()
{
// Setup cmdlet to list certs using an OData filter
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.ThumbprintAlgorithm = null;
cmdlet.Thumbprint = null;
cmdlet.Filter = "state eq 'active'";
cmdlet.Select = "id,state";

string requestFilter = null;
string requestSelect = null;

// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>();
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
{
requestFilter = request.Parameters.DetailLevel.FilterClause;
requestSelect = request.Parameters.DetailLevel.SelectClause;

return Task.FromResult(response);
});
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };

cmdlet.ExecuteCmdlet();

Assert.Equal(cmdlet.Filter, requestFilter);
Assert.Equal(cmdlet.Select, requestSelect);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ListBatchCertificatesWithoutFiltersTest()
{
// Setup cmdlet to list certs without filters
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.ThumbprintAlgorithm = null;
cmdlet.Thumbprint = null;
cmdlet.Filter = null;

string[] thumbprintsOfConstructedCerts = new[] { "12345", "67890", "ABCDE" };

// Build some Certificates instead of querying the service on a List Certificates call
CertificateListResponse response = BatchTestHelpers.CreateCertificateListResponse(thumbprintsOfConstructedCerts);
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>(response);
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Setup the cmdlet to write pipeline output to a list that can be examined later
List<PSCertificate> pipeline = new List<PSCertificate>();
commandRuntimeMock.Setup(r =>
r.WriteObject(It.IsAny<PSCertificate>()))
.Callback<object>(c => pipeline.Add((PSCertificate)c));

cmdlet.ExecuteCmdlet();

// Verify that the cmdlet wrote the constructed certs to the pipeline
Assert.Equal(3, pipeline.Count);
int poolCount = 0;
foreach (PSCertificate c in pipeline)
{
Assert.True(thumbprintsOfConstructedCerts.Contains(c.Thumbprint));
poolCount++;
}
Assert.Equal(thumbprintsOfConstructedCerts.Length, poolCount);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ListCertificatesMaxCountTest()
{
// Verify default max count
Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);

// Setup cmdlet to list pools without filters and a max count
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.ThumbprintAlgorithm = null;
cmdlet.Thumbprint = null;
cmdlet.Filter = null;
int maxCount = 2;
cmdlet.MaxCount = maxCount;

string[] thumbprintsOfConstructedCerts = new[] { "12345", "67890", "ABCDE" };

// Build some Certificates instead of querying the service on a List Certificates call
CertificateListResponse response = BatchTestHelpers.CreateCertificateListResponse(thumbprintsOfConstructedCerts);
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>(response);
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Setup the cmdlet to write pipeline output to a list that can be examined later
List<PSCertificate> pipeline = new List<PSCertificate>();
commandRuntimeMock.Setup(r =>
r.WriteObject(It.IsAny<PSCertificate>()))
.Callback<object>(c => pipeline.Add((PSCertificate)c));

cmdlet.ExecuteCmdlet();

// Verify that the max count was respected
Assert.Equal(maxCount, pipeline.Count);

// Verify setting max count <= 0 doesn't return nothing
cmdlet.MaxCount = -5;
pipeline.Clear();
cmdlet.ExecuteCmdlet();

Assert.Equal(thumbprintsOfConstructedCerts.Length, pipeline.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ----------------------------------------------------------------------------------
//
// 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 System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Batch.Protocol.Models;
using Microsoft.Azure.Commands.Batch.Test.ScenarioTests;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System.Collections.Generic;
using System.Management.Automation;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;

namespace Microsoft.Azure.Commands.Batch.Test.Certificates
{
public class NewBatchCertificateCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
{
private NewBatchCertificateCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public NewBatchCertificateCommandTests()
{
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new NewBatchCertificateCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void NewBatchCertificateParametersTest()
{
// Setup cmdlet without the required parameters
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;

Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());

cmdlet.FilePath = BatchTestHelpers.TestCertificateFileName1;

// Don't go to the service on an Add Certificate call
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateAddParameters, CertificateAddResponse>();
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameters are set
cmdlet.ExecuteCmdlet();

// Use the RawData parameter set next
cmdlet.FilePath = null;
X509Certificate2 cert = new X509Certificate2(BatchTestHelpers.TestCertificateFileName1);
cmdlet.RawData = cert.RawData;

// Verify no exceptions when required parameters are set
cmdlet.ExecuteCmdlet();
}
}
}
Loading