|
| 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.Entities; |
| 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.Tasks |
| 29 | +{ |
| 30 | + public class GetBatchTaskCommandTests |
| 31 | + { |
| 32 | + private GetBatchTaskCommand cmdlet; |
| 33 | + private Mock<BatchClient> batchClientMock; |
| 34 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 35 | + |
| 36 | + public GetBatchTaskCommandTests() |
| 37 | + { |
| 38 | + batchClientMock = new Mock<BatchClient>(); |
| 39 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 40 | + cmdlet = new GetBatchTaskCommand() |
| 41 | + { |
| 42 | + CommandRuntime = commandRuntimeMock.Object, |
| 43 | + BatchClient = batchClientMock.Object, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 49 | + public void GetBatchTaskTest() |
| 50 | + { |
| 51 | + // Setup cmdlet to get a Task by name |
| 52 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 53 | + cmdlet.BatchContext = context; |
| 54 | + cmdlet.WorkItemName = "workItem"; |
| 55 | + cmdlet.JobName = "job-0000000001"; |
| 56 | + cmdlet.Name = "task1"; |
| 57 | + cmdlet.Filter = null; |
| 58 | + |
| 59 | + // Build a Task instead of querying the service on a GetJob call |
| 60 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 61 | + { |
| 62 | + if (request is GetTaskRequest) |
| 63 | + { |
| 64 | + GetTaskResponse response = BatchTestHelpers.CreateGetTaskResponse(cmdlet.Name); |
| 65 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 66 | + return task; |
| 67 | + } |
| 68 | + return null; |
| 69 | + }); |
| 70 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 71 | + |
| 72 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 73 | + List<PSCloudTask> pipeline = new List<PSCloudTask>(); |
| 74 | + commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCloudTask>())).Callback<object>(t => pipeline.Add((PSCloudTask)t)); |
| 75 | + |
| 76 | + cmdlet.ExecuteCmdlet(); |
| 77 | + |
| 78 | + // Verify that the cmdlet wrote the Task returned from the OM to the pipeline |
| 79 | + Assert.Equal(1, pipeline.Count); |
| 80 | + Assert.Equal(cmdlet.Name, pipeline[0].Name); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 85 | + public void ListBatchTasksByODataFilterTest() |
| 86 | + { |
| 87 | + // Setup cmdlet to list Tasks using an OData filter. Use WorkItemName and JobName input. |
| 88 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 89 | + cmdlet.BatchContext = context; |
| 90 | + cmdlet.WorkItemName = "workItem"; |
| 91 | + cmdlet.JobName = "job-0000000001"; |
| 92 | + cmdlet.Name = null; |
| 93 | + cmdlet.Filter = "startswith(name,'test')"; |
| 94 | + |
| 95 | + string[] namesOfConstructedTasks = new[] { "testTask1", "testTask2" }; |
| 96 | + |
| 97 | + // Build some Tasks instead of querying the service on a ListTasks call |
| 98 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 99 | + { |
| 100 | + if (request is ListTasksRequest) |
| 101 | + { |
| 102 | + ListTasksResponse response = BatchTestHelpers.CreateListTasksResponse(namesOfConstructedTasks); |
| 103 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 104 | + return task; |
| 105 | + } |
| 106 | + return null; |
| 107 | + }); |
| 108 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 109 | + |
| 110 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 111 | + List<PSCloudTask> pipeline = new List<PSCloudTask>(); |
| 112 | + commandRuntimeMock.Setup(r => |
| 113 | + r.WriteObject(It.IsAny<PSCloudTask>())) |
| 114 | + .Callback<object>(t => pipeline.Add((PSCloudTask)t)); |
| 115 | + |
| 116 | + cmdlet.ExecuteCmdlet(); |
| 117 | + |
| 118 | + // Verify that the cmdlet wrote the constructed Tasks to the pipeline |
| 119 | + Assert.Equal(2, pipeline.Count); |
| 120 | + int taskCount = 0; |
| 121 | + foreach (PSCloudTask t in pipeline) |
| 122 | + { |
| 123 | + Assert.True(namesOfConstructedTasks.Contains(t.Name)); |
| 124 | + taskCount++; |
| 125 | + } |
| 126 | + Assert.Equal(namesOfConstructedTasks.Length, taskCount); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 131 | + public void ListBatchTasksWithoutFiltersTest() |
| 132 | + { |
| 133 | + // Setup cmdlet to list Tasks without filters. Use WorkItemName and JobName. A PSCloudJob object is difficult to construct, so save that for the scenario tests. |
| 134 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 135 | + cmdlet.BatchContext = context; |
| 136 | + cmdlet.WorkItemName = "workItem"; |
| 137 | + cmdlet.JobName = "job-0000000001"; |
| 138 | + cmdlet.Name = null; |
| 139 | + cmdlet.Filter = null; |
| 140 | + |
| 141 | + string[] namesOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" }; |
| 142 | + |
| 143 | + // Build some Tasks instead of querying the service on a ListTasks call |
| 144 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 145 | + { |
| 146 | + if (request is ListTasksRequest) |
| 147 | + { |
| 148 | + ListTasksResponse response = BatchTestHelpers.CreateListTasksResponse(namesOfConstructedTasks); |
| 149 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 150 | + return task; |
| 151 | + } |
| 152 | + return null; |
| 153 | + }); |
| 154 | + cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor }; |
| 155 | + |
| 156 | + // Setup the cmdlet to write pipeline output to a list that can be examined later |
| 157 | + List<PSCloudTask> pipeline = new List<PSCloudTask>(); |
| 158 | + commandRuntimeMock.Setup(r => |
| 159 | + r.WriteObject(It.IsAny<PSCloudTask>())) |
| 160 | + .Callback<object>(t => pipeline.Add((PSCloudTask)t)); |
| 161 | + |
| 162 | + cmdlet.ExecuteCmdlet(); |
| 163 | + |
| 164 | + // Verify that the cmdlet wrote the constructed Tasks to the pipeline |
| 165 | + Assert.Equal(3, pipeline.Count); |
| 166 | + int taskCount = 0; |
| 167 | + foreach (PSCloudTask t in pipeline) |
| 168 | + { |
| 169 | + Assert.True(namesOfConstructedTasks.Contains(t.Name)); |
| 170 | + taskCount++; |
| 171 | + } |
| 172 | + Assert.Equal(namesOfConstructedTasks.Length, taskCount); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 177 | + public void ListTasksMaxCountTest() |
| 178 | + { |
| 179 | + // Verify default max count |
| 180 | + Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount); |
| 181 | + |
| 182 | + // Verify setting max count <= 0 |
| 183 | + cmdlet.MaxCount = -5; |
| 184 | + Assert.Equal(int.MaxValue, cmdlet.MaxCount); |
| 185 | + |
| 186 | + // Setup cmdlet to list Tasks without filters and a max count |
| 187 | + BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys(); |
| 188 | + cmdlet.BatchContext = context; |
| 189 | + cmdlet.WorkItemName = "workItem"; |
| 190 | + cmdlet.JobName = "job-0000000001"; |
| 191 | + cmdlet.Name = null; |
| 192 | + cmdlet.Filter = null; |
| 193 | + int maxCount = 2; |
| 194 | + cmdlet.MaxCount = maxCount; |
| 195 | + |
| 196 | + string[] namesOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" }; |
| 197 | + |
| 198 | + // Build some Tasks instead of querying the service on a ListTasks call |
| 199 | + YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) => |
| 200 | + { |
| 201 | + if (request is ListTasksRequest) |
| 202 | + { |
| 203 | + ListTasksResponse response = BatchTestHelpers.CreateListTasksResponse(namesOfConstructedTasks); |
| 204 | + Task<object> task = Task<object>.Factory.StartNew(() => { return response; }); |
| 205 | + return task; |
| 206 | + } |
| 207 | + return null; |
| 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<PSCloudTask> pipeline = new List<PSCloudTask>(); |
| 213 | + commandRuntimeMock.Setup(r => |
| 214 | + r.WriteObject(It.IsAny<PSCloudTask>())) |
| 215 | + .Callback<object>(t => pipeline.Add((PSCloudTask)t)); |
| 216 | + |
| 217 | + cmdlet.ExecuteCmdlet(); |
| 218 | + |
| 219 | + // Verify that the max count was respected |
| 220 | + Assert.Equal(maxCount, pipeline.Count); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments