|
| 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.Entities; |
| 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.Files |
| 31 | +{ |
| 32 | + public class GetBatchTaskFileCommandTests |
| 33 | + { |
| 34 | + private GetBatchTaskFileCommand cmdlet; |
| 35 | + private Mock<BatchClient> batchClientMock; |
| 36 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 37 | + |
| 38 | + public GetBatchTaskFileCommandTests() |
| 39 | + { |
| 40 | + batchClientMock = new Mock<BatchClient>(); |
| 41 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 42 | + cmdlet = new GetBatchTaskFileCommand() |
| 43 | + { |
| 44 | + CommandRuntime = commandRuntimeMock.Object, |
| 45 | + BatchClient = batchClientMock.Object, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 51 | + public void GetBatchTaskFileParametersTest() |
| 52 | + { |
| 53 | + // Setup cmdlet without required parameters |
| 54 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 55 | + cmdlet.BatchContext = context; |
| 56 | + cmdlet.WorkItemName = null; |
| 57 | + cmdlet.JobName = null; |
| 58 | + cmdlet.TaskName = null; |
| 59 | + cmdlet.Name = null; |
| 60 | + cmdlet.Task = null; |
| 61 | + cmdlet.Filter = null; |
| 62 | + |
| 63 | + // Build some Task files instead of querying the service on a ListTaskFiles call |
| 64 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 65 | + { |
| 66 | + if (request is ListTaskFilesRequest) |
| 67 | + { |
| 68 | + ListTaskFilesResponse response = BatchTestHelpers.CreateListTaskFilesResponse(new string[] { "stdout.txt" }); |
| 69 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 70 | + return task; |
| 71 | + } |
| 72 | + return null; |
| 73 | + }); |
| 74 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 75 | + |
| 76 | + Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet()); |
| 77 | + |
| 78 | + cmdlet.WorkItemName = "workItem"; |
| 79 | + cmdlet.JobName = "job-0000000001"; |
| 80 | + cmdlet.TaskName = "task"; |
| 81 | + |
| 82 | + // Verify no exceptions occur |
| 83 | + cmdlet.ExecuteCmdlet(); |
| 84 | + |
| 85 | + cmdlet.WorkItemName = null; |
| 86 | + cmdlet.JobName = null; |
| 87 | + cmdlet.TaskName = null; |
| 88 | + cmdlet.Task = new PSCloudTask("task", "cmd /c dir /s"); |
| 89 | + |
| 90 | + // Verify that we don't get an argument exception. We should get an InvalidOperationException though since the task is unbound |
| 91 | + Assert.Throws<InvalidOperationException>(() => cmdlet.ExecuteCmdlet()); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 96 | + public void GetBatchTaskFileTest() |
| 97 | + { |
| 98 | + // Setup cmdlet to get a Task file by name |
| 99 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 100 | + cmdlet.BatchContext = context; |
| 101 | + cmdlet.WorkItemName = "workItem"; |
| 102 | + cmdlet.JobName = "job-0000000001"; |
| 103 | + cmdlet.TaskName = "task"; |
| 104 | + cmdlet.Name = "stdout.txt"; |
| 105 | + cmdlet.Filter = null; |
| 106 | + |
| 107 | + // Build a Task file instead of querying the service on a GetTaskFileProperties call |
| 108 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 109 | + { |
| 110 | + if (request is GetTaskFilePropertiesRequest) |
| 111 | + { |
| 112 | + GetTaskFilePropertiesResponse response = BatchTestHelpers.CreateGetTaskFilePropertiesResponse(cmdlet.Name); |
| 113 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 114 | + return task; |
| 115 | + } |
| 116 | + return null; |
| 117 | + }); |
| 118 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 119 | + |
| 120 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 121 | + List<PSTaskFile> pipeline = new List<PSTaskFile>(); |
| 122 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSTaskFile>())).Callback<object>(f => pipeline.Add((PSTaskFile)f)); |
| 123 | + |
| 124 | + cmdlet.ExecuteCmdlet(); |
| 125 | + |
| 126 | + // Verify that the cmdlet wrote the Task file returned from the OM to the pipeline |
| 127 | + Assert.Equal(1, pipeline.Count); |
| 128 | + Assert.Equal(cmdlet.Name, pipeline[0].Name); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 133 | + public void ListBatchTaskFilesByODataFilterTest() |
| 134 | + { |
| 135 | + // Setup cmdlet to list Tasks using an OData filter. Use WorkItemName and JobName input. |
| 136 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 137 | + cmdlet.BatchContext = context; |
| 138 | + cmdlet.WorkItemName = "workItem"; |
| 139 | + cmdlet.JobName = "job-0000000001"; |
| 140 | + cmdlet.TaskName = "task"; |
| 141 | + cmdlet.Name = null; |
| 142 | + cmdlet.Filter = "startswith(name,'std')"; |
| 143 | + |
| 144 | + string[] namesOfConstructedTaskFiles = new[] { "stdout.txt", "stderr.txt" }; |
| 145 | + |
| 146 | + // Build some Task files instead of querying the service on a ListTaskFiles call |
| 147 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 148 | + { |
| 149 | + if (request is ListTaskFilesRequest) |
| 150 | + { |
| 151 | + ListTaskFilesResponse response = BatchTestHelpers.CreateListTaskFilesResponse(namesOfConstructedTaskFiles); |
| 152 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 153 | + return task; |
| 154 | + } |
| 155 | + return null; |
| 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<PSTaskFile> pipeline = new List<PSTaskFile>(); |
| 161 | + commandRuntimeMock.Setup(r => |
| 162 | + r.WriteObject(It.IsAny<PSTaskFile>())) |
| 163 | + .Callback<object>(f => pipeline.Add((PSTaskFile)f)); |
| 164 | + |
| 165 | + cmdlet.ExecuteCmdlet(); |
| 166 | + |
| 167 | + // Verify that the cmdlet wrote the constructed Tasks to the pipeline |
| 168 | + Assert.Equal(2, pipeline.Count); |
| 169 | + int taskCount = 0; |
| 170 | + foreach (PSTaskFile f in pipeline) |
| 171 | + { |
| 172 | + Assert.True(namesOfConstructedTaskFiles.Contains(f.Name)); |
| 173 | + taskCount++; |
| 174 | + } |
| 175 | + Assert.Equal(namesOfConstructedTaskFiles.Length, taskCount); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 180 | + public void ListBatchTaskFilesWithoutFiltersTest() |
| 181 | + { |
| 182 | + // Setup cmdlet to list Task files without filters. |
| 183 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 184 | + cmdlet.BatchContext = context; |
| 185 | + cmdlet.WorkItemName = "workItem"; |
| 186 | + cmdlet.JobName = "job-0000000001"; |
| 187 | + cmdlet.TaskName = "task"; |
| 188 | + cmdlet.Name = null; |
| 189 | + cmdlet.Filter = null; |
| 190 | + |
| 191 | + string[] namesOfConstructedTaskFiles = new[] { "stdout.txt", "stderr.txt", "wd" }; |
| 192 | + |
| 193 | + // Build some Task files instead of querying the service on a ListTaskFiles call |
| 194 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 195 | + { |
| 196 | + if (request is ListTaskFilesRequest) |
| 197 | + { |
| 198 | + ListTaskFilesResponse response = BatchTestHelpers.CreateListTaskFilesResponse(namesOfConstructedTaskFiles); |
| 199 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 200 | + return task; |
| 201 | + } |
| 202 | + return null; |
| 203 | + }); |
| 204 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 205 | + |
| 206 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 207 | + List<PSTaskFile> pipeline = new List<PSTaskFile>(); |
| 208 | + commandRuntimeMock.Setup(r => |
| 209 | + r.WriteObject(It.IsAny<PSTaskFile>())) |
| 210 | + .Callback<object>(t => pipeline.Add((PSTaskFile)t)); |
| 211 | + |
| 212 | + cmdlet.ExecuteCmdlet(); |
| 213 | + |
| 214 | + // Verify that the cmdlet wrote the constructed Task files to the pipeline |
| 215 | + Assert.Equal(3, pipeline.Count); |
| 216 | + int taskCount = 0; |
| 217 | + foreach (PSTaskFile f in pipeline) |
| 218 | + { |
| 219 | + Assert.True(namesOfConstructedTaskFiles.Contains(f.Name)); |
| 220 | + taskCount++; |
| 221 | + } |
| 222 | + Assert.Equal(namesOfConstructedTaskFiles.Length, taskCount); |
| 223 | + } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 227 | + public void ListTaskFilesMaxCountTest() |
| 228 | + { |
| 229 | + // Verify default max count |
| 230 | + Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount); |
| 231 | + |
| 232 | + // Setup cmdlet to list Task files and a max count. |
| 233 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 234 | + cmdlet.BatchContext = context; |
| 235 | + cmdlet.WorkItemName = "workItem"; |
| 236 | + cmdlet.JobName = "job-0000000001"; |
| 237 | + cmdlet.TaskName = "task"; |
| 238 | + cmdlet.Name = null; |
| 239 | + cmdlet.Filter = null; |
| 240 | + int maxCount = 2; |
| 241 | + cmdlet.MaxCount = maxCount; |
| 242 | + |
| 243 | + string[] namesOfConstructedTaskFiles = new[] { "stdout.txt", "stderr.txt", "wd" }; |
| 244 | + |
| 245 | + // Build some Task files instead of querying the service on a ListTaskFiles call |
| 246 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 247 | + { |
| 248 | + if (request is ListTaskFilesRequest) |
| 249 | + { |
| 250 | + ListTaskFilesResponse response = BatchTestHelpers.CreateListTaskFilesResponse(namesOfConstructedTaskFiles); |
| 251 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 252 | + return task; |
| 253 | + } |
| 254 | + return null; |
| 255 | + }); |
| 256 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 257 | + |
| 258 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 259 | + List<PSTaskFile> pipeline = new List<PSTaskFile>(); |
| 260 | + commandRuntimeMock.Setup(r => |
| 261 | + r.WriteObject(It.IsAny<PSTaskFile>())) |
| 262 | + .Callback<object>(t => pipeline.Add((PSTaskFile)t)); |
| 263 | + |
| 264 | + cmdlet.ExecuteCmdlet(); |
| 265 | + |
| 266 | + // Verify that the max count was respected |
| 267 | + Assert.Equal(maxCount, pipeline.Count); |
| 268 | + |
| 269 | + // Verify setting max count <= 0 doesn't return nothing |
| 270 | + cmdlet.MaxCount = -5; |
| 271 | + pipeline.Clear(); |
| 272 | + cmdlet.ExecuteCmdlet(); |
| 273 | + |
| 274 | + Assert.Equal(namesOfConstructedTaskFiles.Length, pipeline.Count); |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments