|
| 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.Collections.Generic; |
| 22 | +using System.Linq; |
| 23 | +using System.Management.Automation; |
| 24 | +using System.Threading.Tasks; |
| 25 | +using Xunit; |
| 26 | +using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodes |
| 29 | +{ |
| 30 | + public class GetBatchComputeNodeCommandTests |
| 31 | + { |
| 32 | + private GetBatchComputeNodeCommand cmdlet; |
| 33 | + private Mock<BatchClient> batchClientMock; |
| 34 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 35 | + |
| 36 | + public GetBatchComputeNodeCommandTests() |
| 37 | + { |
| 38 | + batchClientMock = new Mock<BatchClient>(); |
| 39 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 40 | + cmdlet = new GetBatchComputeNodeCommand() |
| 41 | + { |
| 42 | + CommandRuntime = commandRuntimeMock.Object, |
| 43 | + BatchClient = batchClientMock.Object, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 49 | + public void GetBatchComputeNodeTest() |
| 50 | + { |
| 51 | + // Setup cmdlet to get a compute node by id |
| 52 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 53 | + cmdlet.BatchContext = context; |
| 54 | + cmdlet.PoolId = "pool"; |
| 55 | + cmdlet.Id = "computeNode1"; |
| 56 | + cmdlet.Filter = null; |
| 57 | + |
| 58 | + // Build a compute node instead of querying the service on a Get ComputeNode call |
| 59 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 60 | + { |
| 61 | + BatchRequest<ComputeNodeGetParameters, ComputeNodeGetResponse> request = |
| 62 | + (BatchRequest<ComputeNodeGetParameters, ComputeNodeGetResponse>)baseRequest; |
| 63 | + |
| 64 | + request.ServiceRequestFunc = () => |
| 65 | + { |
| 66 | + ComputeNodeGetResponse response = BatchTestHelpers.CreateComputeNodeGetResponse(cmdlet.Id); |
| 67 | + Task<ComputeNodeGetResponse> task = Task.FromResult(response); |
| 68 | + return task; |
| 69 | + }; |
| 70 | + }); |
| 71 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 72 | + |
| 73 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 74 | + List<PSComputeNode> pipeline = new List<PSComputeNode>(); |
| 75 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSComputeNode>())).Callback<object>(c => pipeline.Add((PSComputeNode)c)); |
| 76 | + |
| 77 | + cmdlet.ExecuteCmdlet(); |
| 78 | + |
| 79 | + // Verify that the cmdlet wrote the compute node returned from the OM to the pipeline |
| 80 | + Assert.Equal(1, pipeline.Count); |
| 81 | + Assert.Equal(cmdlet.Id, pipeline[0].Id); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 86 | + public void ListBatchComputeNodesByODataFilterTest() |
| 87 | + { |
| 88 | + // Setup cmdlet to list vms using an OData filter. |
| 89 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 90 | + cmdlet.BatchContext = context; |
| 91 | + cmdlet.PoolId = "pool"; |
| 92 | + cmdlet.Id = null; |
| 93 | + cmdlet.Filter = "state -eq 'idle'"; |
| 94 | + |
| 95 | + string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2" }; |
| 96 | + |
| 97 | + // Build some compute nodes instead of querying the service on a List ComputeNodes call |
| 98 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 99 | + { |
| 100 | + BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request = |
| 101 | + (BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest; |
| 102 | + |
| 103 | + request.ServiceRequestFunc = () => |
| 104 | + { |
| 105 | + ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes); |
| 106 | + Task<ComputeNodeListResponse> task = Task.FromResult(response); |
| 107 | + return task; |
| 108 | + }; |
| 109 | + }); |
| 110 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 111 | + |
| 112 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 113 | + List<PSComputeNode> pipeline = new List<PSComputeNode>(); |
| 114 | + commandRuntimeMock.Setup(r => |
| 115 | + r.WriteObject(It.IsAny<PSComputeNode>())) |
| 116 | + .Callback<object>(c => pipeline.Add((PSComputeNode)c)); |
| 117 | + |
| 118 | + cmdlet.ExecuteCmdlet(); |
| 119 | + |
| 120 | + // Verify that the cmdlet wrote the constructed compute nodes to the pipeline |
| 121 | + Assert.Equal(2, pipeline.Count); |
| 122 | + int computeNodeCount = 0; |
| 123 | + foreach (PSComputeNode c in pipeline) |
| 124 | + { |
| 125 | + Assert.True(idsOfConstructedComputeNodes.Contains(c.Id)); |
| 126 | + computeNodeCount++; |
| 127 | + } |
| 128 | + Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 133 | + public void ListBatchComputeNodesWithoutFiltersTest() |
| 134 | + { |
| 135 | + // Setup cmdlet to list compute nodes without filters. |
| 136 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 137 | + cmdlet.BatchContext = context; |
| 138 | + cmdlet.PoolId = "testPool"; |
| 139 | + cmdlet.Id = null; |
| 140 | + cmdlet.Filter = null; |
| 141 | + |
| 142 | + string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" }; |
| 143 | + |
| 144 | + // Build some compute nodes instead of querying the service on a List ComputeNodes call |
| 145 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 146 | + { |
| 147 | + BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request = |
| 148 | + (BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest; |
| 149 | + |
| 150 | + request.ServiceRequestFunc = () => |
| 151 | + { |
| 152 | + ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes); |
| 153 | + Task<ComputeNodeListResponse> task = Task.FromResult(response); |
| 154 | + return task; |
| 155 | + }; |
| 156 | + }); |
| 157 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 158 | + |
| 159 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 160 | + List<PSComputeNode> pipeline = new List<PSComputeNode>(); |
| 161 | + commandRuntimeMock.Setup(r => |
| 162 | + r.WriteObject(It.IsAny<PSComputeNode>())) |
| 163 | + .Callback<object>(c => pipeline.Add((PSComputeNode)c)); |
| 164 | + |
| 165 | + cmdlet.ExecuteCmdlet(); |
| 166 | + |
| 167 | + // Verify that the cmdlet wrote the constructed compute nodes to the pipeline |
| 168 | + Assert.Equal(3, pipeline.Count); |
| 169 | + int computeNodeCount = 0; |
| 170 | + foreach (PSComputeNode c in pipeline) |
| 171 | + { |
| 172 | + Assert.True(idsOfConstructedComputeNodes.Contains(c.Id)); |
| 173 | + computeNodeCount++; |
| 174 | + } |
| 175 | + Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 180 | + public void ListComputeNodesMaxCountTest() |
| 181 | + { |
| 182 | + // Verify default max count |
| 183 | + Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount); |
| 184 | + |
| 185 | + // Setup cmdlet to list compute nodes without filters and a max count |
| 186 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 187 | + cmdlet.BatchContext = context; |
| 188 | + cmdlet.PoolId = "testPool"; |
| 189 | + cmdlet.Id = null; |
| 190 | + cmdlet.Filter = null; |
| 191 | + int maxCount = 2; |
| 192 | + cmdlet.MaxCount = maxCount; |
| 193 | + |
| 194 | + string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" }; |
| 195 | + |
| 196 | + // Build some compute nodes instead of querying the service on a List ComputeNodes call |
| 197 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 198 | + { |
| 199 | + BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request = |
| 200 | + (BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest; |
| 201 | + |
| 202 | + request.ServiceRequestFunc = () => |
| 203 | + { |
| 204 | + ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes); |
| 205 | + Task<ComputeNodeListResponse> task = Task.FromResult(response); |
| 206 | + return task; |
| 207 | + }; |
| 208 | + }); |
| 209 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 210 | + |
| 211 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 212 | + List<PSComputeNode> pipeline = new List<PSComputeNode>(); |
| 213 | + commandRuntimeMock.Setup(r => |
| 214 | + r.WriteObject(It.IsAny<PSComputeNode>())) |
| 215 | + .Callback<object>(c => pipeline.Add((PSComputeNode)c)); |
| 216 | + |
| 217 | + cmdlet.ExecuteCmdlet(); |
| 218 | + |
| 219 | + // Verify that the max count was respected |
| 220 | + Assert.Equal(maxCount, pipeline.Count); |
| 221 | + |
| 222 | + // Verify setting max count <= 0 doesn't return nothing |
| 223 | + cmdlet.MaxCount = -5; |
| 224 | + pipeline.Clear(); |
| 225 | + cmdlet.ExecuteCmdlet(); |
| 226 | + |
| 227 | + Assert.Equal(idsOfConstructedComputeNodes.Length, pipeline.Count); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments