|
| 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.Commands.Batch.Models; |
| 18 | +using Microsoft.Rest.Azure; |
| 19 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 20 | +using Moq; |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Diagnostics; |
| 24 | +using System.Linq; |
| 25 | +using System.Management.Automation; |
| 26 | +using System.Threading.Tasks; |
| 27 | +using Xunit; |
| 28 | +using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient; |
| 29 | +using ProxyModels = Microsoft.Azure.Batch.Protocol.Models; |
| 30 | + |
| 31 | +namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodeExtensions |
| 32 | +{ |
| 33 | + public class GetBatchComputeNodeExtensionCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase |
| 34 | + { |
| 35 | + private GetBatchComputeNodeExtensionCommand cmdlet; |
| 36 | + private Mock<BatchClient> batchClientMock; |
| 37 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 38 | + |
| 39 | + public GetBatchComputeNodeExtensionCommandTests(Xunit.Abstractions.ITestOutputHelper output) |
| 40 | + { |
| 41 | + ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output)); |
| 42 | + batchClientMock = new Mock<BatchClient>(); |
| 43 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 44 | + cmdlet = new GetBatchComputeNodeExtensionCommand() |
| 45 | + { |
| 46 | + CommandRuntime = commandRuntimeMock.Object, |
| 47 | + BatchClient = batchClientMock.Object, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 53 | + public void GetBatchComputeNodeExtensionTest() |
| 54 | + { |
| 55 | + string extensionName = "testExtension"; |
| 56 | + string publisher = "testPublisher"; |
| 57 | + string type = "testType"; |
| 58 | + |
| 59 | + // Setup cmdlet to get a compute node by id |
| 60 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 61 | + cmdlet.BatchContext = context; |
| 62 | + cmdlet.PoolId = "testPool"; |
| 63 | + cmdlet.ComputeNodeId = "testComputeNode"; |
| 64 | + cmdlet.Name = extensionName; |
| 65 | + |
| 66 | + VMExtension extension = new VMExtension(extensionName, publisher, type); |
| 67 | + |
| 68 | + // Build an extension instead of querying the service on a Get ComputeNodeExtension call |
| 69 | + AzureOperationResponse<ProxyModels.NodeVMExtension, ProxyModels.ComputeNodeExtensionGetHeaders> response = BatchTestHelpers.CreateComputeNodeExtensionGetResponse(extension); |
| 70 | + RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 71 | + ProxyModels.ComputeNodeExtensionGetOptions, |
| 72 | + AzureOperationResponse<ProxyModels.NodeVMExtension, ProxyModels.ComputeNodeExtensionGetHeaders>>(response); |
| 73 | + |
| 74 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 75 | + |
| 76 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 77 | + List<PSNodeVMExtension> pipeline = new List<PSNodeVMExtension>(); |
| 78 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSNodeVMExtension>())).Callback<object>(c => pipeline.Add((PSNodeVMExtension)c)); |
| 79 | + |
| 80 | + cmdlet.ExecuteCmdlet(); |
| 81 | + |
| 82 | + // Verify that the cmdlet wrote the compute node returned from the OM to the pipeline |
| 83 | + Assert.Single(pipeline); |
| 84 | + |
| 85 | + PSVMExtension pipelineExtension = pipeline[0].VmExtension; |
| 86 | + Assert.NotNull(pipelineExtension); |
| 87 | + Assert.Equal("testExtension", pipelineExtension.Name); |
| 88 | + Assert.Equal("testPublisher", pipelineExtension.Publisher); |
| 89 | + Assert.Equal("testType", pipelineExtension.Type); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 94 | + public void GetBatchComputeNodeExtensionODataTest() |
| 95 | + { |
| 96 | + string extensionName = "testExtension"; |
| 97 | + string publisher = "testPublisher"; |
| 98 | + string type = "testType"; |
| 99 | + |
| 100 | + // Setup cmdlet to get a compute node by id |
| 101 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 102 | + cmdlet.BatchContext = context; |
| 103 | + cmdlet.PoolId = "testPool"; |
| 104 | + cmdlet.ComputeNodeId = "testComputeNode"; |
| 105 | + cmdlet.Name = extensionName; |
| 106 | + cmdlet.Select = "ExtensionName,Publisher"; |
| 107 | + |
| 108 | + VMExtension extension = new VMExtension(extensionName, publisher, type); |
| 109 | + |
| 110 | + // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used. |
| 111 | + AzureOperationResponse<ProxyModels.NodeVMExtension, ProxyModels.ComputeNodeExtensionGetHeaders> getResponse = BatchTestHelpers.CreateComputeNodeExtensionGetResponse(extension); |
| 112 | + RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 113 | + ProxyModels.ComputeNodeExtensionGetOptions, |
| 114 | + AzureOperationResponse<ProxyModels.NodeVMExtension, ProxyModels.ComputeNodeExtensionGetHeaders>>(getResponse); |
| 115 | + |
| 116 | + string requestSelect = null; |
| 117 | + |
| 118 | + ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) => |
| 119 | + { |
| 120 | + ProxyModels.ComputeNodeExtensionGetOptions options = (ProxyModels.ComputeNodeExtensionGetOptions)request.Options; |
| 121 | + requestSelect = options.Select; |
| 122 | + |
| 123 | + return Task.FromResult(response); |
| 124 | + }); |
| 125 | + |
| 126 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor }; |
| 127 | + |
| 128 | + cmdlet.ExecuteCmdlet(); |
| 129 | + |
| 130 | + Assert.Equal(cmdlet.Select, requestSelect); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 135 | + public void ListBatchComputeNodeExtensionsTest() |
| 136 | + { |
| 137 | + // Setup cmdlet to get a compute node by id |
| 138 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 139 | + cmdlet.BatchContext = context; |
| 140 | + cmdlet.PoolId = "testPool"; |
| 141 | + cmdlet.ComputeNodeId = "testComputeNode"; |
| 142 | + |
| 143 | + int count = 5; |
| 144 | + |
| 145 | + // Build an extension instead of querying the service on a Get ComputeNodeExtension call |
| 146 | + AzureOperationResponse<IPage<ProxyModels.NodeVMExtension>, ProxyModels.ComputeNodeExtensionListHeaders> response = BatchTestHelpers.CreateComputeNodeExtensionListResponse(CreateTestExtensions(count)); |
| 147 | + RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 148 | + ProxyModels.ComputeNodeExtensionListOptions, |
| 149 | + AzureOperationResponse<IPage<ProxyModels.NodeVMExtension>, ProxyModels.ComputeNodeExtensionListHeaders>>(response); |
| 150 | + |
| 151 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 152 | + |
| 153 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 154 | + List<PSNodeVMExtension> pipeline = new List<PSNodeVMExtension>(); |
| 155 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSNodeVMExtension>())).Callback<object>(c => pipeline.Add((PSNodeVMExtension)c)); |
| 156 | + |
| 157 | + cmdlet.ExecuteCmdlet(); |
| 158 | + |
| 159 | + // Verify that the cmdlet wrote the compute node returned from the OM to the pipeline |
| 160 | + Assert.Equal(count, pipeline.Count); |
| 161 | + for (int i = 1; i <= count; i++) |
| 162 | + { |
| 163 | + PSVMExtension extension = pipeline[i-1].VmExtension; |
| 164 | + Assert.Equal($"testExtension{i}", extension.Name); |
| 165 | + Assert.Equal($"testPublisher{i}", extension.Publisher); |
| 166 | + Assert.Equal($"testType{i}", extension.Type); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private IEnumerable<VMExtension> CreateTestExtensions(int count) |
| 171 | + { |
| 172 | + for (int i = 1; i <= count; i++) |
| 173 | + { |
| 174 | + yield return new VMExtension($"testExtension{i}", $"testPublisher{i}", $"testType{i}"); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 180 | + public void ListBatchComputeNodeExtensionsWithMaxCountTest() |
| 181 | + { |
| 182 | + int maxCount = 3; |
| 183 | + |
| 184 | + // Setup cmdlet to get a compute node by id |
| 185 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 186 | + cmdlet.BatchContext = context; |
| 187 | + cmdlet.PoolId = "testPool"; |
| 188 | + cmdlet.ComputeNodeId = "testComputeNode"; |
| 189 | + cmdlet.MaxCount = maxCount; |
| 190 | + |
| 191 | + int count = 5; |
| 192 | + |
| 193 | + // Build an extension instead of querying the service on a Get ComputeNodeExtension call |
| 194 | + AzureOperationResponse<IPage<ProxyModels.NodeVMExtension>, ProxyModels.ComputeNodeExtensionListHeaders> response = BatchTestHelpers.CreateComputeNodeExtensionListResponse(CreateTestExtensions(count)); |
| 195 | + RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 196 | + ProxyModels.ComputeNodeExtensionListOptions, |
| 197 | + AzureOperationResponse<IPage<ProxyModels.NodeVMExtension>, ProxyModels.ComputeNodeExtensionListHeaders>>(response); |
| 198 | + |
| 199 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 200 | + |
| 201 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 202 | + List<PSNodeVMExtension> pipeline = new List<PSNodeVMExtension>(); |
| 203 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSNodeVMExtension>())).Callback<object>(c => pipeline.Add((PSNodeVMExtension)c)); |
| 204 | + |
| 205 | + cmdlet.ExecuteCmdlet(); |
| 206 | + |
| 207 | + // Verify that the cmdlet wrote the compute node returned from the OM to the pipeline |
| 208 | + Assert.Equal(maxCount, pipeline.Count); |
| 209 | + for (int i = 1; i <= maxCount; i++) |
| 210 | + { |
| 211 | + PSVMExtension extension = pipeline[i - 1].VmExtension; |
| 212 | + Assert.Equal($"testExtension{i}", extension.Name); |
| 213 | + Assert.Equal($"testPublisher{i}", extension.Publisher); |
| 214 | + Assert.Equal($"testType{i}", extension.Type); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments