Skip to content

Commit 3d9201a

Browse files
author
jasper-schneider
committed
Get Task and unit tests
1 parent b0bf867 commit 3d9201a

15 files changed

+584
-26
lines changed

src/ResourceManager/Batch/Commands.Batch.Test/BatchTestHelpers.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,45 @@ public static ListJobsResponse CreateListJobsResponse(IEnumerable<string> jobNam
238238
return response;
239239
}
240240

241+
/// <summary>
242+
/// Builds a GetTaskResponse object
243+
/// </summary>
244+
public static GetTaskResponse CreateGetTaskResponse(string taskName)
245+
{
246+
GetTaskResponse response = new GetTaskResponse();
247+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
248+
249+
Azure.Batch.Protocol.Entities.Task task = new Azure.Batch.Protocol.Entities.Task();
250+
SetProperty(task, "Name", taskName);
251+
252+
SetProperty(response, "Task", task);
253+
254+
return response;
255+
}
256+
257+
/// <summary>
258+
/// Builds a ListTasksResponse object
259+
/// </summary>
260+
public static ListTasksResponse CreateListTasksResponse(IEnumerable<string> taskNames)
261+
{
262+
ListTasksResponse response = new ListTasksResponse();
263+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
264+
265+
List<Azure.Batch.Protocol.Entities.Task> tasks = new List<Azure.Batch.Protocol.Entities.Task>();
266+
267+
foreach (string name in taskNames)
268+
{
269+
Azure.Batch.Protocol.Entities.Task task = new Azure.Batch.Protocol.Entities.Task();
270+
SetProperty(task, "Name", name);
271+
tasks.Add(task);
272+
}
273+
274+
SetProperty(response, "Tasks", tasks);
275+
276+
return response;
277+
}
278+
279+
241280
/// <summary>
242281
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.
243282
/// </summary>

src/ResourceManager/Batch/Commands.Batch.Test/Commands.Batch.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
<Compile Include="ScenarioTests\BatchController.cs" />
160160
<Compile Include="ScenarioTests\ScenarioTestHelpers.cs" />
161161
<Compile Include="ScenarioTests\WorkItemTests.cs" />
162+
<Compile Include="Tasks\GetBatchTaskCommandTests.cs" />
162163
<Compile Include="WorkItems\GetBatchWorkItemCommandTests.cs" />
163164
</ItemGroup>
164165
<ItemGroup>

src/ResourceManager/Batch/Commands.Batch.Test/Jobs/GetBatchJobCommandTests.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void ListBatchJobsByODataFilterTest()
126126

127127
[Fact]
128128
[Trait(Category.AcceptanceType, Category.CheckIn)]
129-
public void ListBatchPoolWithoutFiltersTest()
129+
public void ListBatchJobsWithoutFiltersTest()
130130
{
131131
// Setup cmdlet to list Jobs without filters. Use WorkItem input.
132132
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
@@ -176,14 +176,44 @@ public void ListJobsMaxCountTest()
176176
// Verify default max count
177177
Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
178178

179-
// Verify setting max count greater than 0
180-
int maxCount = 5;
181-
cmdlet.MaxCount = maxCount;
182-
Assert.Equal(maxCount, cmdlet.MaxCount);
183-
184179
// Verify setting max count <= 0
185180
cmdlet.MaxCount = -5;
186181
Assert.Equal(int.MaxValue, cmdlet.MaxCount);
182+
183+
// Setup cmdlet to list Jobs without filters and a max count
184+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
185+
cmdlet.BatchContext = context;
186+
cmdlet.WorkItem = BatchTestHelpers.CreatePSCloudWorkItem();
187+
cmdlet.Name = null;
188+
cmdlet.Filter = null;
189+
int maxCount = 2;
190+
cmdlet.MaxCount = maxCount;
191+
192+
string[] namesOfConstructedJobs = new[] { "job-0000000001", "job-0000000002", "job-0000000003" };
193+
194+
// Build some Jobs instead of querying the service on a ListJobs call
195+
YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
196+
{
197+
if (request is ListJobsRequest)
198+
{
199+
ListJobsResponse response = BatchTestHelpers.CreateListJobsResponse(namesOfConstructedJobs);
200+
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
201+
return task;
202+
}
203+
return null;
204+
});
205+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
206+
207+
// Setup the cmdlet to write pipeline output to a list that can be examined later
208+
List<PSCloudJob> pipeline = new List<PSCloudJob>();
209+
commandRuntimeMock.Setup(r =>
210+
r.WriteObject(It.IsAny<PSCloudJob>()))
211+
.Callback<object>(j => pipeline.Add((PSCloudJob)j));
212+
213+
cmdlet.ExecuteCmdlet();
214+
215+
// Verify that the max count was respected
216+
Assert.Equal(maxCount, pipeline.Count);
187217
}
188218
}
189219
}

src/ResourceManager/Batch/Commands.Batch.Test/Pools/GetBatchPoolCommandTests.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,43 @@ public void ListPoolsMaxCountTest()
173173
// Verify default max count
174174
Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
175175

176-
// Verify setting max count greater than 0
177-
int maxCount = 5;
178-
cmdlet.MaxCount = maxCount;
179-
Assert.Equal(maxCount, cmdlet.MaxCount);
180-
181176
// Verify setting max count <= 0
182177
cmdlet.MaxCount = -5;
183178
Assert.Equal(int.MaxValue, cmdlet.MaxCount);
179+
180+
// Setup cmdlet to list Pools without filters and a max count
181+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
182+
cmdlet.BatchContext = context;
183+
cmdlet.Name = null;
184+
cmdlet.Filter = null;
185+
int maxCount = 2;
186+
cmdlet.MaxCount = maxCount;
187+
188+
string[] namesOfConstructedPools = new[] { "name1", "name2", "name3" };
189+
190+
// Build some Pools instead of querying the service on a ListPools call
191+
YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
192+
{
193+
if (request is ListPoolsRequest)
194+
{
195+
ListPoolsResponse response = BatchTestHelpers.CreateListPoolsResponse(namesOfConstructedPools);
196+
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
197+
return task;
198+
}
199+
return null;
200+
});
201+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
202+
203+
// Setup the cmdlet to write pipeline output to a list that can be examined later
204+
List<PSCloudPool> pipeline = new List<PSCloudPool>();
205+
commandRuntimeMock.Setup(r =>
206+
r.WriteObject(It.IsAny<PSCloudPool>()))
207+
.Callback<object>(p => pipeline.Add((PSCloudPool)p));
208+
209+
cmdlet.ExecuteCmdlet();
210+
211+
// Verify that the max count was respected
212+
Assert.Equal(maxCount, pipeline.Count);
184213
}
185214
}
186215
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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

Comments
 (0)