Skip to content

Commit ee7467c

Browse files
author
jasper-schneider
committed
Get Task File Content and tests
1 parent fc5bf25 commit ee7467c

File tree

13 files changed

+2351
-0
lines changed

13 files changed

+2351
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="Accounts\SetBatchAccountCommandTests.cs" />
156156
<Compile Include="BatchTestHelpers.cs" />
157157
<Compile Include="Files\GetBatchTaskFileCommandTests.cs" />
158+
<Compile Include="Files\GetBatchTaskFileContentCommandTests.cs" />
158159
<Compile Include="Jobs\GetBatchJobCommandTests.cs" />
159160
<Compile Include="Jobs\RemoveBatchJobCommandTests.cs" />
160161
<Compile Include="Models\BatchAccountContextTest.cs" />
@@ -227,6 +228,12 @@
227228
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetTaskFileByName.json">
228229
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
229230
</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>
230237
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestListAllTaskFiles.json">
231238
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
232239
</None>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 System.IO;
17+
using Microsoft.Azure.Batch;
18+
using Microsoft.Azure.Batch.Common;
19+
using Microsoft.Azure.Batch.Protocol;
20+
using Microsoft.Azure.Batch.Protocol.Entities;
21+
using Microsoft.Azure.Commands.Batch.Models;
22+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using Moq;
24+
using System.Collections.Generic;
25+
using System.Linq;
26+
using System.Management.Automation;
27+
using System.Threading.Tasks;
28+
using Xunit;
29+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
30+
31+
namespace Microsoft.Azure.Commands.Batch.Test.Files
32+
{
33+
public class GetBatchTaskFileContentCommandTests
34+
{
35+
private GetBatchTaskFileContentCommand cmdlet;
36+
private Mock<BatchClient> batchClientMock;
37+
private Mock<ICommandRuntime> commandRuntimeMock;
38+
39+
public GetBatchTaskFileContentCommandTests()
40+
{
41+
batchClientMock = new Mock<BatchClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetBatchTaskFileContentCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
BatchClient = batchClientMock.Object,
47+
};
48+
}
49+
50+
[Fact]
51+
[Trait(Category.AcceptanceType, Category.CheckIn)]
52+
public void GetBatchTaskFileParametersTest()
53+
{
54+
// Setup cmdlet without required parameters
55+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
56+
cmdlet.BatchContext = context;
57+
cmdlet.WorkItemName = null;
58+
cmdlet.JobName = null;
59+
cmdlet.TaskName = null;
60+
cmdlet.Name = null;
61+
cmdlet.InputObject = null;
62+
cmdlet.DestinationPath = null;
63+
64+
string fileName = "stdout.txt";
65+
66+
// Don't hit the file system during unit tests
67+
cmdlet.Stream = new MemoryStream();
68+
69+
// Don't go to the service on a GetTaskFile call or GetTaskFileProperties call
70+
YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
71+
{
72+
if (request is GetTaskFilePropertiesRequest)
73+
{
74+
GetTaskFilePropertiesResponse response = BatchTestHelpers.CreateGetTaskFilePropertiesResponse(fileName);
75+
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
76+
return task;
77+
}
78+
if (request is GetTaskFileRequest)
79+
{
80+
GetTaskFileResponse response = new GetTaskFileResponse();
81+
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
82+
return task;
83+
}
84+
return null;
85+
});
86+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
87+
88+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
89+
90+
// Fill required Task file details
91+
cmdlet.WorkItemName = "workItem";
92+
cmdlet.JobName = "job-0000000001";
93+
cmdlet.TaskName = "task";
94+
cmdlet.Name = fileName;
95+
96+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
97+
98+
// File in required local path
99+
cmdlet.DestinationPath = "localFile.txt";
100+
101+
try
102+
{
103+
// Verify no exceptions occur
104+
cmdlet.ExecuteCmdlet();
105+
}
106+
finally
107+
{
108+
cmdlet.Stream.Dispose();
109+
}
110+
}
111+
}
112+
}

src/ResourceManager/Batch/Commands.Batch.Test/ScenarioTests/FileTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.IO;
1617
using Microsoft.Azure.Batch;
1718
using Microsoft.Azure.Batch.Protocol.Entities;
1819
using Microsoft.Azure.Commands.Batch.Models;
@@ -202,6 +203,65 @@ public void TestListTaskFilePipeline()
202203
TestUtilities.GetCurrentMethodName());
203204
}
204205

206+
[Fact]
207+
[Trait(Category.AcceptanceType, Category.CheckIn)]
208+
public void TestGetTaskFileContentByName()
209+
{
210+
BatchController controller = BatchController.NewInstance;
211+
string workItemName = "testGetTaskFileContentWI";
212+
string jobName = null;
213+
string taskName = "testTask";
214+
string fileName = "testFile.txt";
215+
string taskFileName = string.Format("wd\\{0}", fileName);
216+
string fileContents = "test file contents";
217+
BatchAccountContext context = null;
218+
controller.RunPsTestWorkflow(
219+
() => { return new string[] { string.Format("Test-GetTaskFileContentByName '{0}' '{1}' '{2}' '{3}' '{4}' '{5}'", accountName, workItemName, jobName, taskName, taskFileName, fileContents) }; },
220+
() =>
221+
{
222+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
223+
ScenarioTestHelpers.CreateTestWorkItem(controller, context, workItemName);
224+
jobName = ScenarioTestHelpers.WaitForRecentJob(controller, context, workItemName);
225+
ScenarioTestHelpers.CreateTestTask(controller, context, workItemName, jobName, taskName, string.Format("cmd /c echo {0} > {1}", fileContents, fileName));
226+
ScenarioTestHelpers.WaitForTaskCompletion(controller, context, workItemName, jobName, taskName);
227+
},
228+
() =>
229+
{
230+
ScenarioTestHelpers.DeleteWorkItem(controller, context, workItemName);
231+
},
232+
TestUtilities.GetCallingClass(),
233+
TestUtilities.GetCurrentMethodName());
234+
}
235+
236+
[Fact]
237+
[Trait(Category.AcceptanceType, Category.CheckIn)]
238+
public void TestGetTaskFileContentPipeline()
239+
{
240+
BatchController controller = BatchController.NewInstance;
241+
string workItemName = "testGetTFContentPipeWI";
242+
string jobName = null;
243+
string taskName = "testTask";
244+
string fileName = "testFile.txt";
245+
string taskFileName = string.Format("wd\\{0}", fileName);
246+
string fileContents = "test file contents";
247+
BatchAccountContext context = null;
248+
controller.RunPsTestWorkflow(
249+
() => { return new string[] { string.Format("Test-GetTaskFileContentPipeline '{0}' '{1}' '{2}' '{3}' '{4}' '{5}'", accountName, workItemName, jobName, taskName, taskFileName, fileContents) }; },
250+
() =>
251+
{
252+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
253+
ScenarioTestHelpers.CreateTestWorkItem(controller, context, workItemName);
254+
jobName = ScenarioTestHelpers.WaitForRecentJob(controller, context, workItemName);
255+
ScenarioTestHelpers.CreateTestTask(controller, context, workItemName, jobName, taskName, string.Format("cmd /c echo {0} > {1}", fileContents, fileName));
256+
ScenarioTestHelpers.WaitForTaskCompletion(controller, context, workItemName, jobName, taskName);
257+
},
258+
() =>
259+
{
260+
ScenarioTestHelpers.DeleteWorkItem(controller, context, workItemName);
261+
},
262+
TestUtilities.GetCallingClass(),
263+
TestUtilities.GetCurrentMethodName());
264+
}
205265
}
206266

207267
// Cmdlets that use the HTTP Recorder interceptor for use with scenario tests
@@ -214,4 +274,18 @@ public override void ExecuteCmdlet()
214274
base.ExecuteCmdlet();
215275
}
216276
}
277+
278+
[Cmdlet(VerbsCommon.Get, "AzureBatchTaskFileContent_ST")]
279+
public class GetBatchTaskFileContentScenarioTestCommand : GetBatchTaskFileContentCommand
280+
{
281+
[Parameter]
282+
public MemoryStream MemStream { get; set; }
283+
284+
public override void ExecuteCmdlet()
285+
{
286+
this.Stream = MemStream;
287+
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
288+
base.ExecuteCmdlet();
289+
}
290+
}
217291
}

src/ResourceManager/Batch/Commands.Batch.Test/ScenarioTests/FileTests.ps1

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,93 @@ function Test-ListTaskFilePipeline
142142
# Get WorkItem into Get Job into Get Task into Get Task file
143143
$taskFiles = Get-AzureBatchWorkItem_ST -Name $workItemName -BatchContext $context | Get-AzureBatchJob_ST -BatchContext $context | Get-AzureBatchTask_ST -BatchContext $context | Get-AzureBatchTaskFile_ST -BatchContext $context
144144
Assert-AreEqual $count $taskFiles.Length
145+
}
146+
147+
<#
148+
.SYNOPSIS
149+
Tests downloading Task file contents by name
150+
#>
151+
function Test-GetTaskFileContentByName
152+
{
153+
param([string]$accountName, [string]$wiName, [string]$jobName, [string]$taskName, [string]$taskFileName, [string]$fileContent)
154+
155+
$context = Get-AzureBatchAccountKeys -Name $accountName
156+
$path = "localFile.txt"
157+
$stream = New-Object System.IO.MemoryStream
158+
159+
try
160+
{
161+
Get-AzureBatchTaskFileContent_ST -WorkItemName $wiName -JobName $jobName -TaskName $taskName -Name $taskFileName -DestinationPath $path -BatchContext $context -MemStream $stream
162+
163+
$stream.Position = 0
164+
$sr = New-Object System.IO.StreamReader $stream
165+
$downloadedContents = $sr.ReadToEnd()
166+
167+
# Don't do strict equality check since extra newline characters get added to the end of the file
168+
Assert-True { $downloadedContents.Contains($fileContent) }
169+
}
170+
finally
171+
{
172+
if ($sr -ne $null)
173+
{
174+
$sr.Dispose()
175+
}
176+
$stream.Dispose()
177+
}
178+
179+
# Verify positional parameters also work
180+
$stream = New-Object System.IO.MemoryStream
181+
try
182+
{
183+
Get-AzureBatchTaskFileContent_ST -WorkItemName $wiName -JobName $jobName -TaskName $taskName -Name $taskFileName -DestinationPath $path -BatchContext $context -MemStream $stream
184+
185+
$stream.Position = 0
186+
$sr = New-Object System.IO.StreamReader $stream
187+
$downloadedContents = $sr.ReadToEnd()
188+
189+
# Don't do strict equality check since extra newline characters get added to the end of the file
190+
Assert-True { $downloadedContents.Contains($fileContent) }
191+
}
192+
finally
193+
{
194+
if ($sr -ne $null)
195+
{
196+
$sr.Dispose()
197+
}
198+
$stream.Dispose()
199+
}
200+
}
201+
202+
<#
203+
.SYNOPSIS
204+
Tests downloading Task file contents using the pipeline
205+
#>
206+
function Test-GetTaskFileContentPipeline
207+
{
208+
param([string]$accountName, [string]$wiName, [string]$jobName, [string]$taskName, [string]$taskFileName, [string]$fileContent)
209+
210+
$context = Get-AzureBatchAccountKeys -Name $accountName
211+
$path = "localFile.txt"
212+
$stream = New-Object System.IO.MemoryStream
213+
214+
try
215+
{
216+
$taskFile = Get-AzureBatchTaskFile_ST -WorkItemName $wiName -JobName $jobName -TaskName $taskName -Name $taskFileName -BatchContext $context
217+
$taskFile | Get-AzureBatchTaskFileContent_ST -DestinationPath $path -BatchContext $context -MemStream $stream
218+
219+
$stream.Position = 0
220+
$sr = New-Object System.IO.StreamReader $stream
221+
$downloadedContents = $sr.ReadToEnd()
222+
223+
# Don't do strict equality check since extra newline characters get added to the end of the file
224+
Assert-True { $downloadedContents.Contains($fileContent) }
225+
}
226+
finally
227+
{
228+
if ($sr -ne $null)
229+
{
230+
$sr.Dispose()
231+
}
232+
$stream.Dispose()
233+
}
145234
}

src/ResourceManager/Batch/Commands.Batch.Test/ScenarioTests/ScenarioTestHelpers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,15 @@ private static object GenerateBatchResponse(BatchRequest batchRequest, HttpWebRe
436436
{
437437
responseStreamField.SetValue(command, responseBody);
438438
}
439+
FieldInfo destinationStreamField = command.GetType().GetField("DestinationStream", BindingFlags.Public | BindingFlags.Instance);
440+
if (destinationStreamField != null)
441+
{
442+
Stream destinationStream = destinationStreamField.GetValue(command) as Stream;
443+
if (destinationStream != null)
444+
{
445+
responseBody.CopyTo(destinationStream);
446+
}
447+
}
439448
batchResponse = postProcessDelegate.DynamicInvoke(command, webResponse, null, null);
440449
}
441450
}

0 commit comments

Comments
 (0)