Skip to content

Commit 7af0782

Browse files
author
jasper-schneider
committed
Start-AzureBatchPoolResize with tests and help. Updating help with content from MSDN team too
1 parent cda75df commit 7af0782

File tree

14 files changed

+7447
-5775
lines changed

14 files changed

+7447
-5775
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
@@ -166,6 +166,7 @@
166166
<Compile Include="Pools\GetBatchPoolCommandTests.cs" />
167167
<Compile Include="Pools\NewBatchPoolCommandTests.cs" />
168168
<Compile Include="Pools\RemoveBatchPoolCommandTests.cs" />
169+
<Compile Include="Pools\StartBatchPoolResizeCommandTests.cs" />
169170
<Compile Include="Properties\AssemblyInfo.cs" />
170171
<Compile Include="ScenarioTests\BatchAccountTests.cs" />
171172
<Compile Include="ScenarioTests\BatchController.cs" />
@@ -339,6 +340,12 @@
339340
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestNewPool.json">
340341
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
341342
</None>
343+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestResizePoolByName.json">
344+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
345+
</None>
346+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestResizePoolByPipeline.json">
347+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
348+
</None>
342349
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestCreateTask.json">
343350
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
344351
</None>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.Protocol;
18+
using Microsoft.Azure.Batch.Protocol.Entities;
19+
using Microsoft.Azure.Commands.Batch.Models;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Moq;
22+
using System.Collections.Generic;
23+
using System.Linq;
24+
using System.Management.Automation;
25+
using System.Threading.Tasks;
26+
using Xunit;
27+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
28+
29+
namespace Microsoft.Azure.Commands.Batch.Test.Pools
30+
{
31+
public class StartBatchPoolResizeCommandTests
32+
{
33+
private StartBatchPoolResizeCommand cmdlet;
34+
private Mock<BatchClient> batchClientMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
37+
public StartBatchPoolResizeCommandTests()
38+
{
39+
batchClientMock = new Mock<BatchClient>();
40+
commandRuntimeMock = new Mock<ICommandRuntime>();
41+
cmdlet = new StartBatchPoolResizeCommand()
42+
{
43+
CommandRuntime = commandRuntimeMock.Object,
44+
BatchClient = batchClientMock.Object,
45+
};
46+
}
47+
48+
[Fact]
49+
[Trait(Category.AcceptanceType, Category.CheckIn)]
50+
public void StartPoolResizeParametersTest()
51+
{
52+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
53+
cmdlet.BatchContext = context;
54+
cmdlet.Name = null;
55+
cmdlet.Pool = null;
56+
57+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
58+
59+
cmdlet.Name = "testPool";
60+
61+
// Don't go to the service on a ResizePool call
62+
YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
63+
{
64+
if (request is ResizePoolRequest)
65+
{
66+
ResizePoolResponse response = new ResizePoolResponse();
67+
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
68+
return task;
69+
}
70+
return null;
71+
});
72+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
73+
74+
// Verify no exceptions when required parameter is set
75+
cmdlet.ExecuteCmdlet();
76+
}
77+
}
78+
}

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

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

1515
using Microsoft.Azure.Batch;
16+
using Microsoft.Azure.Commands.Batch.Models;
1617
using Microsoft.Azure.Test;
1718
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1819
using System.Collections.Generic;
@@ -24,6 +25,15 @@ namespace Microsoft.Azure.Commands.Batch.Test.ScenarioTests
2425
{
2526
public class PoolTests
2627
{
28+
// NOTE: To save time on VM allocation when recording, some of tests assume the following:
29+
// - A Batch account named 'pooltests' exists under the subscription being used for recording.
30+
// - The following commands were run to create a pool, and all 3 VMs are allocated:
31+
// $context = Get-AzureBatchAccountKeys "pooltests"
32+
// New-AzureBatchPool -Name "testPool" -VMSize "small" -OSFamily "4" -TargetOSVersion "*" -TargetDedicated 3 -BatchContext $context
33+
34+
private const string commonAccountName = "pooltests";
35+
private const string testPoolName = "testPool";
36+
2737
[Fact]
2838
[Trait(Category.AcceptanceType, Category.CheckIn)]
2939
public void TestNewPool()
@@ -224,6 +234,42 @@ public void TestDeletePoolPipeline()
224234
TestUtilities.GetCallingClass(),
225235
TestUtilities.GetCurrentMethodName());
226236
}
237+
238+
[Fact]
239+
[Trait(Category.AcceptanceType, Category.CheckIn)]
240+
public void TestResizePoolByName()
241+
{
242+
BatchController controller = BatchController.NewInstance;
243+
BatchAccountContext context = null;
244+
controller.RunPsTestWorkflow(
245+
() => { return new string[] { string.Format("Test-ResizePoolByName '{0}' '{1}'", commonAccountName, testPoolName) }; },
246+
() =>
247+
{
248+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, commonAccountName);
249+
ScenarioTestHelpers.WaitForSteadyPoolAllocation(controller, context, testPoolName);
250+
},
251+
null,
252+
TestUtilities.GetCallingClass(),
253+
TestUtilities.GetCurrentMethodName());
254+
}
255+
256+
[Fact]
257+
[Trait(Category.AcceptanceType, Category.CheckIn)]
258+
public void TestResizePoolByPipeline()
259+
{
260+
BatchController controller = BatchController.NewInstance;
261+
BatchAccountContext context = null;
262+
controller.RunPsTestWorkflow(
263+
() => { return new string[] { string.Format("Test-ResizePoolByPipeline '{0}' '{1}'", commonAccountName, testPoolName) }; },
264+
() =>
265+
{
266+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, commonAccountName);
267+
ScenarioTestHelpers.WaitForSteadyPoolAllocation(controller, context, testPoolName);
268+
},
269+
null,
270+
TestUtilities.GetCallingClass(),
271+
TestUtilities.GetCurrentMethodName());
272+
}
227273
}
228274

229275
// Cmdlets that use the HTTP Recorder interceptor for use with scenario tests
@@ -256,4 +302,14 @@ public override void ExecuteCmdlet()
256302
base.ExecuteCmdlet();
257303
}
258304
}
305+
306+
[Cmdlet(VerbsLifecycle.Start, "AzureBatchPoolResize_ST")]
307+
public class StartBatchPoolResizeScenarioTestCommand : StartBatchPoolResizeCommand
308+
{
309+
public override void ExecuteCmdlet()
310+
{
311+
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
312+
base.ExecuteCmdlet();
313+
}
314+
}
259315
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,48 @@ function Test-DeletePool
184184
# Verify the Pool was deleted
185185
$pools = Get-AzureBatchPool_ST -BatchContext $context
186186
Assert-True { $pools -eq $null -or $pools[0].State.ToString().ToLower() -eq 'deleting' }
187+
}
188+
189+
<#
190+
.SYNOPSIS
191+
Tests resizing a pool specified by name
192+
#>
193+
function Test-ResizePoolByName
194+
{
195+
param([string]$accountName, [string]$poolName)
196+
197+
$context = Get-AzureBatchAccountKeys -Name $accountName
198+
199+
# Get the initial TargetDedicated count
200+
$pool = Get-AzureBatchPool_ST -Name $poolName -BatchContext $context
201+
$initialTargetDedicated = $pool.TargetDedicated
202+
203+
$newTargetDedicated = $initialTargetDedicated + 2
204+
Start-AzureBatchPoolResize_ST -Name $poolName -TargetDedicated $newTargetDedicated -BatchContext $context
205+
206+
# Verify the TargetDedicated property was updated
207+
$pool = Get-AzureBatchPool_ST -Name $poolName -BatchContext $context
208+
Assert-AreEqual $newTargetDedicated $pool.TargetDedicated
209+
}
210+
211+
<#
212+
.SYNOPSIS
213+
Tests resizing a pool specified by pipeline object
214+
#>
215+
function Test-ResizePoolByPipeline
216+
{
217+
param([string]$accountName, [string]$poolName)
218+
219+
$context = Get-AzureBatchAccountKeys -Name $accountName
220+
221+
# Get the initial TargetDedicated count
222+
$pool = Get-AzureBatchPool_ST -Name $poolName -BatchContext $context
223+
$initialTargetDedicated = $pool.TargetDedicated
224+
225+
$newTargetDedicated = $initialTargetDedicated + 2
226+
$pool | Start-AzureBatchPoolResize_ST -TargetDedicated $newTargetDedicated -ResizeTimeout ([TimeSpan]::FromHours(1)) -DeallocationOption ([Microsoft.Azure.Batch.Common.TVMDeallocationOption]::Terminate) -BatchContext $context
227+
228+
# Verify the TargetDedicated property was updated
229+
$pool = Get-AzureBatchPool_ST -Name $poolName -BatchContext $context
230+
Assert-AreEqual $newTargetDedicated $pool.TargetDedicated
187231
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ public static void CreateTestPool(BatchController controller, BatchAccountContex
104104
client.CreatePool(parameters);
105105
}
106106

107+
public static void WaitForSteadyPoolAllocation(BatchController controller, BatchAccountContext context, string poolName)
108+
{
109+
YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor();
110+
BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor };
111+
BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);
112+
113+
ListPoolOptions options = new ListPoolOptions(context, behaviors)
114+
{
115+
PoolName = poolName
116+
};
117+
118+
DateTime timeout = DateTime.Now.AddMinutes(2);
119+
PSCloudPool pool = client.ListPools(options).First();
120+
while (pool.AllocationState != AllocationState.Steady)
121+
{
122+
if (DateTime.Now > timeout)
123+
{
124+
throw new TimeoutException("Timed out waiting for steady allocation state");
125+
}
126+
Sleep(5000);
127+
pool = client.ListPools(options).First();
128+
}
129+
}
130+
107131
/// <summary>
108132
/// Deletes a pool used in a Scenario test.
109133
/// </summary>

0 commit comments

Comments
 (0)