|
| 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.Management.Automation; |
| 24 | +using Xunit; |
| 25 | +using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient; |
| 26 | +using ProxyModels = Microsoft.Azure.Batch.Protocol.Models; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodes |
| 29 | +{ |
| 30 | + public class StartBatchComputeNodeServiceLogUploadCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase |
| 31 | + { |
| 32 | + private StartBatchComputeNodeServiceLogUploadCommand startComputeNodeServiceLogUploadCommand; |
| 33 | + private GetBatchComputeNodeCommand getComputeNodeCommand; |
| 34 | + private Mock<BatchClient> batchClientMock; |
| 35 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 36 | + private const string fakeUrl = "https://containerUrl?sv=="; |
| 37 | + |
| 38 | + public StartBatchComputeNodeServiceLogUploadCommandTests(Xunit.Abstractions.ITestOutputHelper output) |
| 39 | + { |
| 40 | + ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output)); |
| 41 | + batchClientMock = new Mock<BatchClient>(); |
| 42 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 43 | + |
| 44 | + getComputeNodeCommand = new GetBatchComputeNodeCommand() |
| 45 | + { |
| 46 | + CommandRuntime = commandRuntimeMock.Object, |
| 47 | + BatchClient = batchClientMock.Object, |
| 48 | + }; |
| 49 | + |
| 50 | + startComputeNodeServiceLogUploadCommand = new StartBatchComputeNodeServiceLogUploadCommand() |
| 51 | + { |
| 52 | + CommandRuntime = commandRuntimeMock.Object, |
| 53 | + BatchClient = batchClientMock.Object, |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 59 | + public void WhenStartBatchComputeNodeServiceLogUploadCommandIsCalledWithPoolIdAndComputeNodeId_ShouldSucceed() |
| 60 | + { |
| 61 | + // Setup cmdlet to get a compute node by id |
| 62 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 63 | + startComputeNodeServiceLogUploadCommand.BatchContext = context; |
| 64 | + startComputeNodeServiceLogUploadCommand.PoolId = "pool"; |
| 65 | + startComputeNodeServiceLogUploadCommand.ComputeNodeId = "tvm"; |
| 66 | + startComputeNodeServiceLogUploadCommand.ContainerUrl = fakeUrl; |
| 67 | + startComputeNodeServiceLogUploadCommand.StartTime = DateTime.UtcNow; |
| 68 | + |
| 69 | + const int numberOfFilesUploaded = 2; |
| 70 | + const string virtualDirectoryName = "pool1/tvm"; |
| 71 | + |
| 72 | + // Build a compute node instead of querying the service on a Get ComputeNode call |
| 73 | + AzureOperationResponse<ProxyModels.UploadBatchServiceLogsResult, ProxyModels.ComputeNodeUploadBatchServiceLogsHeaders> response = |
| 74 | + BatchTestHelpers.CreateComputeNodeServiceLogsAddResponse(numberOfFilesUploaded, virtualDirectoryName); |
| 75 | + |
| 76 | + RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 77 | + ProxyModels.ComputeNodeUploadBatchServiceLogsOptions, |
| 78 | + AzureOperationResponse<ProxyModels.UploadBatchServiceLogsResult, ProxyModels.ComputeNodeUploadBatchServiceLogsHeaders>>(response); |
| 79 | + |
| 80 | + startComputeNodeServiceLogUploadCommand.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 81 | + |
| 82 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 83 | + PSStartComputeNodeServiceLogUploadResult result = null; |
| 84 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSStartComputeNodeServiceLogUploadResult>())).Callback<object>(c => result = (PSStartComputeNodeServiceLogUploadResult)c); |
| 85 | + |
| 86 | + startComputeNodeServiceLogUploadCommand.ExecuteCmdlet(); |
| 87 | + |
| 88 | + Assert.NotNull(result); |
| 89 | + Assert.Equal(result.NumberOfFilesUploaded, numberOfFilesUploaded); |
| 90 | + Assert.Equal(result.VirtualDirectoryName, virtualDirectoryName); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 95 | + public void WhenStartBatchComputeNodeServiceLogUploadCommandIsCalledWithComputeNode_ShouldSucceed() |
| 96 | + { |
| 97 | + // First get a fake tvm |
| 98 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 99 | + getComputeNodeCommand.BatchContext = context; |
| 100 | + getComputeNodeCommand.PoolId = "Pool1"; |
| 101 | + getComputeNodeCommand.Id = null; |
| 102 | + getComputeNodeCommand.Filter = null; |
| 103 | + |
| 104 | + AzureOperationResponse<IPage<ProxyModels.ComputeNode>, ProxyModels.ComputeNodeListHeaders> response1 = |
| 105 | + BatchTestHelpers.CreateComputeNodeListResponse(new[] { "tvm1" }); |
| 106 | + |
| 107 | + RequestInterceptor interceptor1 = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 108 | + ProxyModels.ComputeNodeListOptions, |
| 109 | + AzureOperationResponse<IPage<ProxyModels.ComputeNode>, ProxyModels.ComputeNodeListHeaders>>(response1); |
| 110 | + |
| 111 | + var computeNodes = new List<PSComputeNode>(); |
| 112 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSComputeNode>())) |
| 113 | + .Callback<object>(c => computeNodes.Add((PSComputeNode) c)); |
| 114 | + |
| 115 | + getComputeNodeCommand.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor1 }; |
| 116 | + |
| 117 | + getComputeNodeCommand.ExecuteCmdlet(); |
| 118 | + |
| 119 | + // test StartBatchComputeNodeServiceLogUploadCommand |
| 120 | + startComputeNodeServiceLogUploadCommand.BatchContext = context; |
| 121 | + startComputeNodeServiceLogUploadCommand.ComputeNode = computeNodes[0]; |
| 122 | + startComputeNodeServiceLogUploadCommand.ContainerUrl = fakeUrl; |
| 123 | + |
| 124 | + var utcNow = DateTime.UtcNow; |
| 125 | + startComputeNodeServiceLogUploadCommand.StartTime = utcNow.AddDays(-1); |
| 126 | + startComputeNodeServiceLogUploadCommand.EndTime = utcNow; |
| 127 | + |
| 128 | + const int numberOfFilesUploaded = 2; |
| 129 | + const string virtualDirectoryName = "pool1/tvm"; |
| 130 | + |
| 131 | + AzureOperationResponse<ProxyModels.UploadBatchServiceLogsResult, ProxyModels.ComputeNodeUploadBatchServiceLogsHeaders> response2 = |
| 132 | + BatchTestHelpers.CreateComputeNodeServiceLogsAddResponse(numberOfFilesUploaded, virtualDirectoryName); |
| 133 | + |
| 134 | + RequestInterceptor interceptor2 = BatchTestHelpers.CreateFakeServiceResponseInterceptor< |
| 135 | + ProxyModels.ComputeNodeUploadBatchServiceLogsOptions, |
| 136 | + AzureOperationResponse<ProxyModels.UploadBatchServiceLogsResult, ProxyModels.ComputeNodeUploadBatchServiceLogsHeaders>>(response2); |
| 137 | + |
| 138 | + startComputeNodeServiceLogUploadCommand.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor2 }; |
| 139 | + |
| 140 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 141 | + PSStartComputeNodeServiceLogUploadResult result = null; |
| 142 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSStartComputeNodeServiceLogUploadResult>())).Callback<object>(c => result = (PSStartComputeNodeServiceLogUploadResult)c); |
| 143 | + |
| 144 | + startComputeNodeServiceLogUploadCommand.ExecuteCmdlet(); |
| 145 | + |
| 146 | + Assert.NotNull(result); |
| 147 | + Assert.Equal(result.NumberOfFilesUploaded, numberOfFilesUploaded); |
| 148 | + Assert.Equal(result.VirtualDirectoryName, virtualDirectoryName); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments