Skip to content

Commit 48ccf0b

Browse files
committed
Merge pull request #960 from jasper-schneider/batchUpdateCmdlets
Batch update cmdlets
2 parents 0270be6 + ad1cf6f commit 48ccf0b

File tree

72 files changed

+7876
-149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+7876
-149
lines changed

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

Lines changed: 111 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,16 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.IO;
1615
using System.Linq;
1716
using System.Net;
18-
using System.Net.Http;
1917
using System.Threading.Tasks;
2018
using Microsoft.Azure.Batch;
21-
using Microsoft.Azure.Batch.Common;
2219
using Microsoft.Azure.Batch.Protocol;
23-
using Microsoft.Azure.Commands.Batch.Models;
24-
using Microsoft.Azure.Commands.Batch.Test.ScenarioTests;
25-
using Microsoft.Azure.Management.Batch;
2620
using Microsoft.Azure.Management.Batch.Models;
2721
using System;
2822
using System.Collections;
2923
using System.Collections.Generic;
3024
using System.Reflection;
31-
using Microsoft.Azure.Management.Resources;
32-
using Microsoft.Azure.Management.Resources.Models;
33-
using Microsoft.Azure.Test.HttpRecorder;
34-
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
3525
using Xunit;
3626
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
3727

@@ -114,16 +104,22 @@ public static void AssertBatchAccountContextsAreEqual(BatchAccountContext contex
114104
/// Creates a RequestInterceptor that does not contact the Batch Service but instead uses the supplied response body.
115105
/// </summary>
116106
/// <param name="responseToUse">The response the interceptor should return. If none is specified, then a new instance of the response type is instantiated.</param>
107+
/// <param name="requestAction">An action to perform on the request.</param>
117108
/// <typeparam name="TParameters">The type of the request parameters.</typeparam>
118109
/// <typeparam name="TResponse">The type of the expected response.</typeparam>
119-
public static RequestInterceptor CreateNoOpInterceptor<TParameters, TResponse>(TResponse responseToUse = null)
110+
public static RequestInterceptor CreateFakeServiceResponseInterceptor<TParameters, TResponse>(TResponse responseToUse = null,
111+
Action<BatchRequest<TParameters, TResponse>> requestAction = null)
120112
where TParameters : ProxyModels.BatchParameters
121113
where TResponse : ProxyModels.BatchOperationResponse, new()
122114
{
123115
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
124116
{
125-
BatchRequest<TParameters, TResponse> request =
126-
(BatchRequest<TParameters, TResponse>)baseRequest;
117+
BatchRequest<TParameters, TResponse> request = (BatchRequest<TParameters, TResponse>)baseRequest;
118+
119+
if (requestAction != null)
120+
{
121+
requestAction(request);
122+
}
127123

128124
request.ServiceRequestFunc = (cancellationToken) =>
129125
{
@@ -140,12 +136,12 @@ public static RequestInterceptor CreateNoOpInterceptor<TParameters, TResponse>(T
140136
/// The interceptor must handle both request types since it's possible for one OM node file method to perform both REST APIs.
141137
/// </summary>
142138
/// <param name="fileName">The name of the file to put in the response body.</param>
143-
public static RequestInterceptor CreateNoOpGetFileAndPropertiesInterceptor(string fileName)
139+
public static RequestInterceptor CreateFakGetFileAndPropertiesResponseInterceptor(string fileName)
144140
{
145141
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
146142
{
147143
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse> fileRequest = baseRequest as
148-
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse>;
144+
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse>;
149145

150146
if (fileRequest != null)
151147
{
@@ -401,6 +397,106 @@ public static ProxyModels.NodeFileListResponse CreateNodeFileListResponse(IEnume
401397
return response;
402398
}
403399

400+
/// <summary>
401+
/// Fabricates a CloudJob that's in the bound state
402+
/// </summary>
403+
public static CloudJob CreateFakeBoundJob(BatchAccountContext context)
404+
{
405+
string jobId = "testJob";
406+
407+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
408+
{
409+
BatchRequest<ProxyModels.CloudJobGetParameters, ProxyModels.CloudJobGetResponse> request =
410+
(BatchRequest<ProxyModels.CloudJobGetParameters, ProxyModels.CloudJobGetResponse>)baseRequest;
411+
412+
request.ServiceRequestFunc = (cancellationToken) =>
413+
{
414+
ProxyModels.CloudJobGetResponse response = new ProxyModels.CloudJobGetResponse();
415+
response.Job = new ProxyModels.CloudJob(jobId, new ProxyModels.PoolInformation());
416+
417+
Task<ProxyModels.CloudJobGetResponse> task = Task.FromResult(response);
418+
return task;
419+
};
420+
});
421+
422+
return context.BatchOMClient.JobOperations.GetJob(jobId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
423+
}
424+
425+
/// <summary>
426+
/// Fabricates a CloudJobSchedule that's in the bound state
427+
/// </summary>
428+
public static CloudJobSchedule CreateFakeBoundJobSchedule(BatchAccountContext context)
429+
{
430+
string jobScheduleId = "testJobSchedule";
431+
432+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
433+
{
434+
BatchRequest<ProxyModels.CloudJobScheduleGetParameters, ProxyModels.CloudJobScheduleGetResponse> request =
435+
(BatchRequest<ProxyModels.CloudJobScheduleGetParameters, ProxyModels.CloudJobScheduleGetResponse>)baseRequest;
436+
437+
request.ServiceRequestFunc = (cancellationToken) =>
438+
{
439+
ProxyModels.CloudJobScheduleGetResponse response = new ProxyModels.CloudJobScheduleGetResponse();
440+
response.JobSchedule = new ProxyModels.CloudJobSchedule(jobScheduleId, new ProxyModels.Schedule(), new ProxyModels.JobSpecification());
441+
442+
Task<ProxyModels.CloudJobScheduleGetResponse> task = Task.FromResult(response);
443+
return task;
444+
};
445+
});
446+
447+
return context.BatchOMClient.JobScheduleOperations.GetJobSchedule(jobScheduleId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
448+
}
449+
450+
/// <summary>
451+
/// Fabricates a CloudPool that's in the bound state
452+
/// </summary>
453+
public static CloudPool CreateFakeBoundPool(BatchAccountContext context)
454+
{
455+
string poolId = "testPool";
456+
457+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
458+
{
459+
BatchRequest<ProxyModels.CloudPoolGetParameters, ProxyModels.CloudPoolGetResponse> request =
460+
(BatchRequest<ProxyModels.CloudPoolGetParameters, ProxyModels.CloudPoolGetResponse>)baseRequest;
461+
462+
request.ServiceRequestFunc = (cancellationToken) =>
463+
{
464+
ProxyModels.CloudPoolGetResponse response = new ProxyModels.CloudPoolGetResponse();
465+
response.Pool = new ProxyModels.CloudPool(poolId, "small", "4");
466+
467+
Task<ProxyModels.CloudPoolGetResponse> task = Task.FromResult(response);
468+
return task;
469+
};
470+
});
471+
472+
return context.BatchOMClient.PoolOperations.GetPool(poolId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
473+
}
474+
475+
/// <summary>
476+
/// Fabricates a CloudTask that's in the bound state
477+
/// </summary>
478+
public static CloudTask CreateFakeBoundTask(BatchAccountContext context)
479+
{
480+
string taskId = "testTask";
481+
482+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
483+
{
484+
BatchRequest<ProxyModels.CloudTaskGetParameters, ProxyModels.CloudTaskGetResponse> request =
485+
(BatchRequest<ProxyModels.CloudTaskGetParameters, ProxyModels.CloudTaskGetResponse>)baseRequest;
486+
487+
request.ServiceRequestFunc = (cancellationToken) =>
488+
{
489+
ProxyModels.CloudTaskGetResponse response = new ProxyModels.CloudTaskGetResponse();
490+
response.Task = new ProxyModels.CloudTask(taskId, "cmd /c dir /s");
491+
492+
Task<ProxyModels.CloudTaskGetResponse> task = Task.FromResult(response);
493+
return task;
494+
};
495+
});
496+
497+
return context.BatchOMClient.JobOperations.GetTask("jobId", taskId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
498+
}
499+
404500
/// <summary>
405501
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.
406502
/// </summary>

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,28 @@
178178
<Compile Include="BatchTestHelpers.cs" />
179179
<Compile Include="ComputeNodes\ResetBatchComputeNodeCommandTests.cs" />
180180
<Compile Include="ComputeNodes\RestartBatchComputeNodeCommandTests.cs" />
181+
<Compile Include="ComputeNodeUsers\SetBatchComputeNoderUserCommandTests.cs" />
181182
<Compile Include="Files\GetBatchRemoteDesktopProtocolFileCommandTests.cs" />
182183
<Compile Include="Files\GetBatchNodeFileCommandTests.cs" />
183184
<Compile Include="Files\GetBatchNodeFileContentCommandTests.cs" />
184185
<Compile Include="JobSchedules\DisableBatchJobScheduleCommandTests.cs" />
185186
<Compile Include="JobSchedules\EnableBatchJobScheduleCommandTests.cs" />
187+
<Compile Include="JobSchedules\SetBatchJobScheduleCommandTests.cs" />
186188
<Compile Include="JobSchedules\StopBatchJobScheduleCommandTests.cs" />
187189
<Compile Include="Jobs\DisableBatchJobCommandTests.cs" />
188190
<Compile Include="Jobs\EnableBatchJobCommandTests.cs" />
189191
<Compile Include="Jobs\GetBatchJobCommandTests.cs" />
190192
<Compile Include="Jobs\NewBatchJobCommandTests.cs" />
191193
<Compile Include="Jobs\RemoveBatchJobCommandTests.cs" />
194+
<Compile Include="Jobs\SetBatchJobCommandTests.cs" />
192195
<Compile Include="Jobs\StopBatchJobCommandTests.cs" />
193196
<Compile Include="Models\BatchAccountContextTest.cs" />
194197
<Compile Include="Pools\DisableBatchAutoScaleCommandTests.cs" />
195198
<Compile Include="Pools\EnableBatchAutoScaleCommandTests.cs" />
196199
<Compile Include="Pools\GetBatchPoolCommandTests.cs" />
197200
<Compile Include="Pools\NewBatchPoolCommandTests.cs" />
198201
<Compile Include="Pools\RemoveBatchPoolCommandTests.cs" />
202+
<Compile Include="Pools\SetBatchPoolCommandTests.cs" />
199203
<Compile Include="Pools\SetBatchPoolOSVersionCommandTests.cs" />
200204
<Compile Include="Pools\StartBatchPoolResizeCommandTests.cs" />
201205
<Compile Include="Pools\StopBatchPoolResizeCommandTests.cs" />
@@ -220,6 +224,7 @@
220224
<Compile Include="JobSchedules\GetBatchJobScheduleCommandTests.cs" />
221225
<Compile Include="JobSchedules\NewBatchJobScheduleCommandTests.cs" />
222226
<Compile Include="JobSchedules\RemoveBatchJobScheduleCommandTests.cs" />
227+
<Compile Include="Tasks\SetBatchTaskCommandTests.cs" />
223228
<Compile Include="Tasks\StopBatchTaskCommandTests.cs" />
224229
</ItemGroup>
225230
<ItemGroup>
@@ -311,6 +316,9 @@
311316
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestDeleteComputeNodeUser.json">
312317
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
313318
</None>
319+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestUpdateComputeNodeUser.json">
320+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
321+
</None>
314322
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetNodeFileByComputeNodeByName.json">
315323
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
316324
</None>
@@ -395,6 +403,9 @@
395403
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestTerminateJobSchedulePipeline.json">
396404
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
397405
</None>
406+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestUpdateJobSchedule.json">
407+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
408+
</None>
398409
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestDeleteJob.json">
399410
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
400411
</None>
@@ -428,6 +439,9 @@
428439
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestTerminateJobPipeline.json">
429440
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
430441
</None>
442+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestUpdateJob.json">
443+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
444+
</None>
431445
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestChangeOSVersionById.json">
432446
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
433447
</None>
@@ -485,6 +499,9 @@
485499
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestStopResizePoolByPipeline.json">
486500
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
487501
</None>
502+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestUpdatePool.json">
503+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
504+
</None>
488505
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestCreateTask.json">
489506
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
490507
</None>
@@ -512,6 +529,9 @@
512529
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestTerminateTask.json">
513530
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
514531
</None>
532+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestUpdateTask.json">
533+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
534+
</None>
515535
</ItemGroup>
516536
<ItemGroup>
517537
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">

src/ResourceManager/AzureBatch/Commands.Batch.Test/ComputeNodeUsers/NewBatchComputeNodeUserCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void NewBatchComputeNodeUserParametersTest()
6262
cmdlet.Password = "Password1234";
6363

6464
// Don't go to the service on an Add ComputeNodeUser call
65-
RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse>();
65+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse>();
6666
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
6767

6868
// Verify no exceptions when required parameters are set

src/ResourceManager/AzureBatch/Commands.Batch.Test/ComputeNodeUsers/RemoveBatchComputeNodeUserCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void RemoveBatchComputeNodeUserParametersTest()
6161
cmdlet.Name = "testUser";
6262

6363
// Don't go to the service on a Delete ComputeNodeUser call
64-
RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse>();
64+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse>();
6565
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
6666

6767
// Verify no exceptions when required parameters are set
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.Threading.Tasks;
17+
using Microsoft.Azure.Batch;
18+
using Microsoft.Azure.Batch.Protocol;
19+
using Microsoft.Azure.Batch.Protocol.Models;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Moq;
22+
using System.Collections.Generic;
23+
using System.Management.Automation;
24+
using Xunit;
25+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
26+
27+
namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodeUsers
28+
{
29+
public class SetBatchComputeNodeUserCommandTests
30+
{
31+
private SetBatchComputeNodeUserCommand cmdlet;
32+
private Mock<BatchClient> batchClientMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
35+
public SetBatchComputeNodeUserCommandTests()
36+
{
37+
batchClientMock = new Mock<BatchClient>();
38+
commandRuntimeMock = new Mock<ICommandRuntime>();
39+
cmdlet = new SetBatchComputeNodeUserCommand()
40+
{
41+
CommandRuntime = commandRuntimeMock.Object,
42+
BatchClient = batchClientMock.Object,
43+
};
44+
}
45+
46+
[Fact]
47+
[Trait(Category.AcceptanceType, Category.CheckIn)]
48+
public void SetBatchComputeNodeUserParametersTest()
49+
{
50+
// Setup cmdlet without the required parameters
51+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
52+
cmdlet.BatchContext = context;
53+
54+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
55+
56+
cmdlet.PoolId = "testPool";
57+
cmdlet.ComputeNodeId = "computeNode1";
58+
59+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
60+
61+
cmdlet.Name = "testUser";
62+
63+
// Don't go to the service on an Update ComputeNodeUser call
64+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeUpdateUserParameters, ComputeNodeUpdateUserResponse>();
65+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
66+
67+
// Verify no exceptions when required parameters are set
68+
cmdlet.ExecuteCmdlet();
69+
}
70+
71+
[Fact]
72+
[Trait(Category.AcceptanceType, Category.CheckIn)]
73+
public void SetBatchComputeNodeUserRequestTest()
74+
{
75+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
76+
cmdlet.BatchContext = context;
77+
cmdlet.PoolId = "testPool";
78+
cmdlet.ComputeNodeId = "computeNode1";
79+
cmdlet.Name = "testUser";
80+
cmdlet.Password = "Password1234";
81+
cmdlet.ExpiryTime = DateTime.Now.AddDays(1);
82+
83+
string requestPassword = null;
84+
DateTime requestExpiryTime = DateTime.Now;
85+
86+
// Don't go to the service on an Update ComputeNodeUser call
87+
Action<BatchRequest<ComputeNodeUpdateUserParameters, ComputeNodeUpdateUserResponse>> extractUserUpdateParametersAction =
88+
(request) =>
89+
{
90+
requestPassword = request.TypedParameters.Password;
91+
requestExpiryTime = request.TypedParameters.ExpiryTime.Value;
92+
};
93+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor(requestAction: extractUserUpdateParametersAction);
94+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
95+
96+
cmdlet.ExecuteCmdlet();
97+
98+
// Verify the request parameters match expectations
99+
Assert.Equal(cmdlet.Password, requestPassword);
100+
Assert.Equal(cmdlet.ExpiryTime, requestExpiryTime);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)