|
| 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 | +} |
0 commit comments