Skip to content

Commit 7e77b73

Browse files
committed
Merge pull request #286 from jasper-schneider/getTaskFile
Get Batch task files
2 parents 87a25a0 + b7ff41e commit 7e77b73

28 files changed

+8485
-56
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,43 @@ public static ListTasksResponse CreateListTasksResponse(IEnumerable<string> task
276276
return response;
277277
}
278278

279+
/// <summary>
280+
/// Builds a GetTaskFilePropertiesResponse object
281+
/// </summary>
282+
public static GetTaskFilePropertiesResponse CreateGetTaskFilePropertiesResponse(string fileName)
283+
{
284+
GetTaskFilePropertiesResponse response = new GetTaskFilePropertiesResponse();
285+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
286+
287+
Azure.Batch.Protocol.Entities.File file = new Azure.Batch.Protocol.Entities.File();
288+
SetProperty(file, "Name", fileName);
289+
290+
SetProperty(response, "File", file);
291+
292+
return response;
293+
}
294+
295+
/// <summary>
296+
/// Builds a ListTaskFilesResponse object
297+
/// </summary>
298+
public static ListTaskFilesResponse CreateListTaskFilesResponse(IEnumerable<string> fileNames)
299+
{
300+
ListTaskFilesResponse response = new ListTaskFilesResponse();
301+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
302+
303+
List<Azure.Batch.Protocol.Entities.File> files = new List<Azure.Batch.Protocol.Entities.File>();
304+
305+
foreach (string name in fileNames)
306+
{
307+
Azure.Batch.Protocol.Entities.File file = new Azure.Batch.Protocol.Entities.File();
308+
SetProperty(file, "Name", name);
309+
files.Add(file);
310+
}
311+
312+
SetProperty(response, "Files", files);
313+
314+
return response;
315+
}
279316

280317
/// <summary>
281318
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
<Compile Include="Accounts\RemoveBatchAccountCommandTests.cs" />
155155
<Compile Include="Accounts\SetBatchAccountCommandTests.cs" />
156156
<Compile Include="BatchTestHelpers.cs" />
157+
<Compile Include="Files\GetBatchTaskFileCommandTests.cs" />
158+
<Compile Include="Files\GetBatchTaskFileContentCommandTests.cs" />
157159
<Compile Include="Jobs\GetBatchJobCommandTests.cs" />
158160
<Compile Include="Jobs\RemoveBatchJobCommandTests.cs" />
159161
<Compile Include="Models\BatchAccountContextTest.cs" />
@@ -163,6 +165,7 @@
163165
<Compile Include="Properties\AssemblyInfo.cs" />
164166
<Compile Include="ScenarioTests\BatchAccountTests.cs" />
165167
<Compile Include="ScenarioTests\BatchController.cs" />
168+
<Compile Include="ScenarioTests\FileTests.cs" />
166169
<Compile Include="ScenarioTests\JobTests.cs" />
167170
<Compile Include="ScenarioTests\PoolTests.cs" />
168171
<Compile Include="ScenarioTests\ScenarioTestHelpers.cs" />
@@ -186,6 +189,9 @@
186189
<None Include="ScenarioTests\Common.ps1">
187190
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
188191
</None>
192+
<None Include="ScenarioTests\FileTests.ps1">
193+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
194+
</None>
189195
<None Include="ScenarioTests\JobTests.ps1">
190196
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
191197
</None>
@@ -219,6 +225,30 @@
219225
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.BatchAccountTests\TestUpdatesExistingBatchAccount.json">
220226
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
221227
</None>
228+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetTaskFileByName.json">
229+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
230+
</None>
231+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetTaskFileContentByName.json">
232+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
233+
</None>
234+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetTaskFileContentPipeline.json">
235+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
236+
</None>
237+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListAllTaskFiles.json">
238+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
239+
</None>
240+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListTaskFilePipeline.json">
241+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
242+
</None>
243+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListTaskFilesByFilter.json">
244+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
245+
</None>
246+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListTaskFilesRecursive.json">
247+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
248+
</None>
249+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListTaskFilesWithMaxCount.json">
250+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
251+
</None>
222252
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestDeleteJob.json">
223253
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
224254
</None>
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
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

Comments
 (0)