Skip to content

Commit 754b757

Browse files
committed
Merge pull request #157 from huangpf/dev
Dev
2 parents f30d495 + a1611e1 commit 754b757

File tree

45 files changed

+18298
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+18298
-29
lines changed

src/ResourceManager/AzureBatch/Commands.Batch.Test/BatchTestHelpers.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace Microsoft.Azure.Commands.Batch.Test
3131
/// </summary>
3232
public static class BatchTestHelpers
3333
{
34+
internal const string TestCertificateFileName1 = "Resources\\BatchTestCert01.cer";
35+
internal const string TestCertificateFileName2 = "Resources\\BatchTestCert02.cer";
36+
internal const string TestCertificateAlgorithm = "sha1";
37+
internal const string TestCertificatePassword = "Passw0rd";
38+
3439
/// <summary>
3540
/// Builds an AccountResource object using the specified parameters
3641
/// </summary>
@@ -168,6 +173,44 @@ public static RequestInterceptor CreateFakGetFileAndPropertiesResponseIntercepto
168173
return interceptor;
169174
}
170175

176+
/// <summary>
177+
/// Builds a CertificateGetResponse object
178+
/// </summary>
179+
public static ProxyModels.CertificateGetResponse CreateCertificateGetResponse(string thumbprint)
180+
{
181+
ProxyModels.CertificateGetResponse response = new ProxyModels.CertificateGetResponse();
182+
response.StatusCode = HttpStatusCode.OK;
183+
184+
ProxyModels.Certificate cert = new ProxyModels.Certificate();
185+
cert.Thumbprint = thumbprint;
186+
187+
response.Certificate = cert;
188+
189+
return response;
190+
}
191+
192+
/// <summary>
193+
/// Builds a CertificateListResponse object
194+
/// </summary>
195+
public static ProxyModels.CertificateListResponse CreateCertificateListResponse(IEnumerable<string> certThumbprints)
196+
{
197+
ProxyModels.CertificateListResponse response = new ProxyModels.CertificateListResponse();
198+
response.StatusCode = HttpStatusCode.OK;
199+
200+
List<ProxyModels.Certificate> certs = new List<ProxyModels.Certificate>();
201+
202+
foreach (string t in certThumbprints)
203+
{
204+
ProxyModels.Certificate cert = new ProxyModels.Certificate();
205+
cert.Thumbprint = t;
206+
certs.Add(cert);
207+
}
208+
209+
response.Certificates = certs;
210+
211+
return response;
212+
}
213+
171214
/// <summary>
172215
/// Builds a CloudPoolGetResponse object
173216
/// </summary>
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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.Batch;
16+
using Microsoft.Azure.Batch.Protocol;
17+
using Microsoft.Azure.Batch.Protocol.Models;
18+
using Microsoft.Azure.Commands.Batch.Models;
19+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
20+
using Moq;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Linq;
24+
using System.Management.Automation;
25+
using System.Threading.Tasks;
26+
using Xunit;
27+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
28+
29+
namespace Microsoft.Azure.Commands.Batch.Test.Certificates
30+
{
31+
public class GetBatchCertificateCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
32+
{
33+
private GetBatchCertificateCommand cmdlet;
34+
private Mock<BatchClient> batchClientMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
37+
public GetBatchCertificateCommandTests()
38+
{
39+
batchClientMock = new Mock<BatchClient>();
40+
commandRuntimeMock = new Mock<ICommandRuntime>();
41+
cmdlet = new GetBatchCertificateCommand()
42+
{
43+
CommandRuntime = commandRuntimeMock.Object,
44+
BatchClient = batchClientMock.Object,
45+
};
46+
}
47+
48+
[Fact]
49+
[Trait(Category.AcceptanceType, Category.CheckIn)]
50+
public void GetBatchCertificateTest()
51+
{
52+
// Setup cmdlet to get a cert by its thumbprint
53+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
54+
cmdlet.BatchContext = context;
55+
cmdlet.ThumbprintAlgorithm = "sha1";
56+
cmdlet.Thumbprint = "123456789";
57+
cmdlet.Filter = null;
58+
59+
// Build a Certificate instead of querying the service on a Get Certificate call
60+
CertificateGetResponse response = BatchTestHelpers.CreateCertificateGetResponse(cmdlet.Thumbprint);
61+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateGetParameters, CertificateGetResponse>(response);
62+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
63+
64+
// Setup the cmdlet to write pipeline output to a list that can be examined later
65+
List<PSCertificate> pipeline = new List<PSCertificate>();
66+
commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCertificate>())).Callback<object>(c => pipeline.Add((PSCertificate)c));
67+
68+
cmdlet.ExecuteCmdlet();
69+
70+
// Verify that the cmdlet wrote the cert returned from the OM to the pipeline
71+
Assert.Equal(1, pipeline.Count);
72+
Assert.Equal(cmdlet.Thumbprint, pipeline[0].Thumbprint);
73+
}
74+
75+
[Fact]
76+
[Trait(Category.AcceptanceType, Category.CheckIn)]
77+
public void GetBatchCertificateODataTest()
78+
{
79+
// Setup cmdlet to get a single certificate
80+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
81+
cmdlet.BatchContext = context;
82+
cmdlet.ThumbprintAlgorithm = "sha1";
83+
cmdlet.Thumbprint = "123456789";
84+
cmdlet.Select = "thumbprint,state";
85+
86+
string requestSelect = null;
87+
88+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
89+
CertificateGetResponse getResponse = BatchTestHelpers.CreateCertificateGetResponse(cmdlet.Thumbprint);
90+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateGetParameters, CertificateGetResponse>(getResponse);
91+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
92+
{
93+
requestSelect = request.Parameters.DetailLevel.SelectClause;
94+
95+
return Task.FromResult(response);
96+
});
97+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
98+
99+
cmdlet.ExecuteCmdlet();
100+
101+
Assert.Equal(cmdlet.Select, requestSelect);
102+
}
103+
104+
[Fact]
105+
[Trait(Category.AcceptanceType, Category.CheckIn)]
106+
public void ListBatchCertificatesODataTest()
107+
{
108+
// Setup cmdlet to list certs using an OData filter
109+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
110+
cmdlet.BatchContext = context;
111+
cmdlet.ThumbprintAlgorithm = null;
112+
cmdlet.Thumbprint = null;
113+
cmdlet.Filter = "state eq 'active'";
114+
cmdlet.Select = "id,state";
115+
116+
string requestFilter = null;
117+
string requestSelect = null;
118+
119+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
120+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>();
121+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
122+
{
123+
requestFilter = request.Parameters.DetailLevel.FilterClause;
124+
requestSelect = request.Parameters.DetailLevel.SelectClause;
125+
126+
return Task.FromResult(response);
127+
});
128+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
129+
130+
cmdlet.ExecuteCmdlet();
131+
132+
Assert.Equal(cmdlet.Filter, requestFilter);
133+
Assert.Equal(cmdlet.Select, requestSelect);
134+
}
135+
136+
[Fact]
137+
[Trait(Category.AcceptanceType, Category.CheckIn)]
138+
public void ListBatchCertificatesWithoutFiltersTest()
139+
{
140+
// Setup cmdlet to list certs without filters
141+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
142+
cmdlet.BatchContext = context;
143+
cmdlet.ThumbprintAlgorithm = null;
144+
cmdlet.Thumbprint = null;
145+
cmdlet.Filter = null;
146+
147+
string[] thumbprintsOfConstructedCerts = new[] { "12345", "67890", "ABCDE" };
148+
149+
// Build some Certificates instead of querying the service on a List Certificates call
150+
CertificateListResponse response = BatchTestHelpers.CreateCertificateListResponse(thumbprintsOfConstructedCerts);
151+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>(response);
152+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
153+
154+
// Setup the cmdlet to write pipeline output to a list that can be examined later
155+
List<PSCertificate> pipeline = new List<PSCertificate>();
156+
commandRuntimeMock.Setup(r =>
157+
r.WriteObject(It.IsAny<PSCertificate>()))
158+
.Callback<object>(c => pipeline.Add((PSCertificate)c));
159+
160+
cmdlet.ExecuteCmdlet();
161+
162+
// Verify that the cmdlet wrote the constructed certs to the pipeline
163+
Assert.Equal(3, pipeline.Count);
164+
int poolCount = 0;
165+
foreach (PSCertificate c in pipeline)
166+
{
167+
Assert.True(thumbprintsOfConstructedCerts.Contains(c.Thumbprint));
168+
poolCount++;
169+
}
170+
Assert.Equal(thumbprintsOfConstructedCerts.Length, poolCount);
171+
}
172+
173+
[Fact]
174+
[Trait(Category.AcceptanceType, Category.CheckIn)]
175+
public void ListCertificatesMaxCountTest()
176+
{
177+
// Verify default max count
178+
Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
179+
180+
// Setup cmdlet to list pools without filters and a max count
181+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
182+
cmdlet.BatchContext = context;
183+
cmdlet.ThumbprintAlgorithm = null;
184+
cmdlet.Thumbprint = null;
185+
cmdlet.Filter = null;
186+
int maxCount = 2;
187+
cmdlet.MaxCount = maxCount;
188+
189+
string[] thumbprintsOfConstructedCerts = new[] { "12345", "67890", "ABCDE" };
190+
191+
// Build some Certificates instead of querying the service on a List Certificates call
192+
CertificateListResponse response = BatchTestHelpers.CreateCertificateListResponse(thumbprintsOfConstructedCerts);
193+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateListParameters, CertificateListResponse>(response);
194+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
195+
196+
// Setup the cmdlet to write pipeline output to a list that can be examined later
197+
List<PSCertificate> pipeline = new List<PSCertificate>();
198+
commandRuntimeMock.Setup(r =>
199+
r.WriteObject(It.IsAny<PSCertificate>()))
200+
.Callback<object>(c => pipeline.Add((PSCertificate)c));
201+
202+
cmdlet.ExecuteCmdlet();
203+
204+
// Verify that the max count was respected
205+
Assert.Equal(maxCount, pipeline.Count);
206+
207+
// Verify setting max count <= 0 doesn't return nothing
208+
cmdlet.MaxCount = -5;
209+
pipeline.Clear();
210+
cmdlet.ExecuteCmdlet();
211+
212+
Assert.Equal(thumbprintsOfConstructedCerts.Length, pipeline.Count);
213+
}
214+
}
215+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
16+
using System.Security.Cryptography.X509Certificates;
17+
using Microsoft.Azure.Batch;
18+
using Microsoft.Azure.Batch.Protocol;
19+
using Microsoft.Azure.Batch.Protocol.Models;
20+
using Microsoft.Azure.Commands.Batch.Test.ScenarioTests;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Moq;
23+
using System.Collections.Generic;
24+
using System.Management.Automation;
25+
using Xunit;
26+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
27+
28+
namespace Microsoft.Azure.Commands.Batch.Test.Certificates
29+
{
30+
public class NewBatchCertificateCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
31+
{
32+
private NewBatchCertificateCommand cmdlet;
33+
private Mock<BatchClient> batchClientMock;
34+
private Mock<ICommandRuntime> commandRuntimeMock;
35+
36+
public NewBatchCertificateCommandTests()
37+
{
38+
batchClientMock = new Mock<BatchClient>();
39+
commandRuntimeMock = new Mock<ICommandRuntime>();
40+
cmdlet = new NewBatchCertificateCommand()
41+
{
42+
CommandRuntime = commandRuntimeMock.Object,
43+
BatchClient = batchClientMock.Object,
44+
};
45+
}
46+
47+
[Fact]
48+
[Trait(Category.AcceptanceType, Category.CheckIn)]
49+
public void NewBatchCertificateParametersTest()
50+
{
51+
// Setup cmdlet without the required parameters
52+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
53+
cmdlet.BatchContext = context;
54+
55+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
56+
57+
cmdlet.FilePath = BatchTestHelpers.TestCertificateFileName1;
58+
59+
// Don't go to the service on an Add Certificate call
60+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CertificateAddParameters, CertificateAddResponse>();
61+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
62+
63+
// Verify no exceptions when required parameters are set
64+
cmdlet.ExecuteCmdlet();
65+
66+
// Use the RawData parameter set next
67+
cmdlet.FilePath = null;
68+
X509Certificate2 cert = new X509Certificate2(BatchTestHelpers.TestCertificateFileName1);
69+
cmdlet.RawData = cert.RawData;
70+
71+
// Verify no exceptions when required parameters are set
72+
cmdlet.ExecuteCmdlet();
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)