|
| 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 Microsoft.Azure.Batch; |
| 17 | +using Microsoft.Azure.Batch.Common; |
| 18 | +using Microsoft.Azure.Batch.Protocol; |
| 19 | +using Microsoft.Azure.Batch.Protocol.Models; |
| 20 | +using Microsoft.Azure.Commands.Batch.Models; |
| 21 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 22 | +using Moq; |
| 23 | +using System.Collections.Generic; |
| 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 | + |
| 30 | +namespace Microsoft.Azure.Commands.Batch.Test.Pools |
| 31 | +{ |
| 32 | + public class ResetBatchComputeNodeCommandTests |
| 33 | + { |
| 34 | + private ResetBatchComputeNodeCommand cmdlet; |
| 35 | + private Mock<BatchClient> batchClientMock; |
| 36 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 37 | + |
| 38 | + public ResetBatchComputeNodeCommandTests() |
| 39 | + { |
| 40 | + batchClientMock = new Mock<BatchClient>(); |
| 41 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 42 | + cmdlet = new ResetBatchComputeNodeCommand() |
| 43 | + { |
| 44 | + CommandRuntime = commandRuntimeMock.Object, |
| 45 | + BatchClient = batchClientMock.Object, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 51 | + public void ResetBatchComputeNodeParametersTest() |
| 52 | + { |
| 53 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 54 | + cmdlet.BatchContext = context; |
| 55 | + cmdlet.Id = null; |
| 56 | + |
| 57 | + Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet()); |
| 58 | + |
| 59 | + cmdlet.PoolId = "testPool"; |
| 60 | + |
| 61 | + Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet()); |
| 62 | + |
| 63 | + cmdlet.Id = "computeNode1"; |
| 64 | + |
| 65 | + // Don't go to the service on a Reimage ComputeNode call |
| 66 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 67 | + { |
| 68 | + BatchRequest<ComputeNodeReimageParameters, ComputeNodeReimageResponse> request = |
| 69 | + (BatchRequest<ComputeNodeReimageParameters, ComputeNodeReimageResponse>)baseRequest; |
| 70 | + |
| 71 | + request.ServiceRequestFunc = (cancellationToken) => |
| 72 | + { |
| 73 | + ComputeNodeReimageResponse response = new ComputeNodeReimageResponse(); |
| 74 | + Task<ComputeNodeReimageResponse> task = Task.FromResult(response); |
| 75 | + return task; |
| 76 | + }; |
| 77 | + }); |
| 78 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 79 | + |
| 80 | + // Verify no exceptions when required parameter is set |
| 81 | + cmdlet.ExecuteCmdlet(); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 86 | + public void ResetComputeNodeRequestTest() |
| 87 | + { |
| 88 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 89 | + cmdlet.BatchContext = context; |
| 90 | + |
| 91 | + cmdlet.PoolId = "testPool"; |
| 92 | + cmdlet.Id = "computeNode1"; |
| 93 | + cmdlet.ReimageOption = ComputeNodeReimageOption.Terminate; |
| 94 | + |
| 95 | + ComputeNodeReimageOption? requestReimageOption = null; |
| 96 | + |
| 97 | + // Don't go to the service on a Reimage ComputeNode call |
| 98 | + RequestInterceptor interceptor = new RequestInterceptor((baseRequest) => |
| 99 | + { |
| 100 | + BatchRequest<ComputeNodeReimageParameters, ComputeNodeReimageResponse> request = |
| 101 | + (BatchRequest<ComputeNodeReimageParameters, ComputeNodeReimageResponse>)baseRequest; |
| 102 | + |
| 103 | + request.ServiceRequestFunc = (cancellationToken) => |
| 104 | + { |
| 105 | + requestReimageOption = request.TypedParameters.ComputeNodeReimageOption; |
| 106 | + |
| 107 | + ComputeNodeReimageResponse response = new ComputeNodeReimageResponse(); |
| 108 | + Task<ComputeNodeReimageResponse> task = Task.FromResult(response); |
| 109 | + return task; |
| 110 | + }; |
| 111 | + }); |
| 112 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 113 | + |
| 114 | + cmdlet.ExecuteCmdlet(); |
| 115 | + |
| 116 | + // Verify that the reimage option was properly set on the outgoing request |
| 117 | + Assert.Equal(cmdlet.ReimageOption, requestReimageOption); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments