Skip to content

Commit 910c17b

Browse files
author
jasper-schneider
committed
Pool scenario tests
1 parent 3d9201a commit 910c17b

File tree

8 files changed

+8916
-0
lines changed

8 files changed

+8916
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<Compile Include="Properties\AssemblyInfo.cs" />
158158
<Compile Include="ScenarioTests\BatchAccountTests.cs" />
159159
<Compile Include="ScenarioTests\BatchController.cs" />
160+
<Compile Include="ScenarioTests\PoolTests.cs" />
160161
<Compile Include="ScenarioTests\ScenarioTestHelpers.cs" />
161162
<Compile Include="ScenarioTests\WorkItemTests.cs" />
162163
<Compile Include="Tasks\GetBatchTaskCommandTests.cs" />
@@ -173,6 +174,9 @@
173174
<None Include="ScenarioTests\Common.ps1">
174175
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
175176
</None>
177+
<None Include="ScenarioTests\PoolTests.ps1">
178+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
179+
</None>
176180
<None Include="ScenarioTests\WorkItemTests.ps1">
177181
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
178182
</None>
@@ -197,6 +201,18 @@
197201
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.BatchAccountTests\TestUpdatesExistingBatchAccount.json">
198202
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
199203
</None>
204+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestGetPoolByName.json">
205+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
206+
</None>
207+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestListAllPools.json">
208+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
209+
</None>
210+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestListPoolsByFilter.json">
211+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
212+
</None>
213+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestListPoolsWithMaxCount.json">
214+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
215+
</None>
200216
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.WorkItemTests\TestGetWorkItemByName.json">
201217
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
202218
</None>
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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.Test;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using System.Collections.Generic;
19+
using System.Management.Automation;
20+
using Xunit;
21+
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
22+
23+
namespace Microsoft.Azure.Commands.Batch.Test.ScenarioTests
24+
{
25+
public class PoolTests
26+
{
27+
[Fact]
28+
[Trait(Category.AcceptanceType, Category.CheckIn)]
29+
public void TestGetPoolByName()
30+
{
31+
BatchController controller = BatchController.NewInstance;
32+
string resourceGroupName = "test-get-pool";
33+
string accountName = "testgetpool";
34+
string location = "eastus";
35+
string poolName = "testName";
36+
BatchAccountContext context = null;
37+
controller.RunPsTestWorkflow(
38+
() => { return new string[] { string.Format("Test-GetPoolByName '{0}' '{1}'", accountName, poolName) }; },
39+
() =>
40+
{
41+
context = ScenarioTestHelpers.CreateTestAccountAndResourceGroup(controller, resourceGroupName, accountName, location);
42+
ScenarioTestHelpers.CreateTestPool(context, poolName);
43+
},
44+
() =>
45+
{
46+
ScenarioTestHelpers.DeletePool(context, poolName);
47+
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
48+
},
49+
TestUtilities.GetCallingClass(),
50+
TestUtilities.GetCurrentMethodName());
51+
}
52+
53+
[Fact]
54+
[Trait(Category.AcceptanceType, Category.CheckIn)]
55+
public void TestListPoolsByFilter()
56+
{
57+
BatchController controller = BatchController.NewInstance;
58+
string resourceGroupName = "test-list-pool-filter";
59+
string accountName = "testlistpoolfilter";
60+
string location = "eastus";
61+
string poolName1 = "testName1";
62+
string poolName2 = "testName2";
63+
string poolName3 = "thirdtestName";
64+
string poolPrefix = "testName";
65+
int matches = 2;
66+
BatchAccountContext context = null;
67+
controller.RunPsTestWorkflow(
68+
() => { return new string[] { string.Format("Test-ListPoolsByFilter '{0}' '{1}' '{2}'", accountName, poolPrefix, matches) }; },
69+
() =>
70+
{
71+
context = ScenarioTestHelpers.CreateTestAccountAndResourceGroup(controller, resourceGroupName, accountName, location);
72+
ScenarioTestHelpers.CreateTestPool(context, poolName1);
73+
ScenarioTestHelpers.CreateTestPool(context, poolName2);
74+
ScenarioTestHelpers.CreateTestPool(context, poolName3);
75+
},
76+
() =>
77+
{
78+
ScenarioTestHelpers.DeletePool(context, poolName1);
79+
ScenarioTestHelpers.DeletePool(context, poolName2);
80+
ScenarioTestHelpers.DeletePool(context, poolName3);
81+
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
82+
},
83+
TestUtilities.GetCallingClass(),
84+
TestUtilities.GetCurrentMethodName());
85+
}
86+
87+
[Fact]
88+
[Trait(Category.AcceptanceType, Category.CheckIn)]
89+
public void TestListPoolsWithMaxCount()
90+
{
91+
BatchController controller = BatchController.NewInstance;
92+
string resourceGroupName = "test-list-pool-maxcount";
93+
string accountName = "testlistpoolmaxcount";
94+
string location = "eastus";
95+
string poolName1 = "testName1";
96+
string poolName2 = "testName2";
97+
string poolName3 = "thirdtestName";
98+
int maxCount = 1;
99+
BatchAccountContext context = null;
100+
controller.RunPsTestWorkflow(
101+
() => { return new string[] { string.Format("Test-ListPoolsWithMaxCount '{0}' '{1}'", accountName, maxCount) }; },
102+
() =>
103+
{
104+
context = ScenarioTestHelpers.CreateTestAccountAndResourceGroup(controller, resourceGroupName, accountName, location);
105+
ScenarioTestHelpers.CreateTestPool(context, poolName1);
106+
ScenarioTestHelpers.CreateTestPool(context, poolName2);
107+
ScenarioTestHelpers.CreateTestPool(context, poolName3);
108+
},
109+
() =>
110+
{
111+
ScenarioTestHelpers.DeletePool(context, poolName1);
112+
ScenarioTestHelpers.DeletePool(context, poolName2);
113+
ScenarioTestHelpers.DeletePool(context, poolName3);
114+
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
115+
},
116+
TestUtilities.GetCallingClass(),
117+
TestUtilities.GetCurrentMethodName());
118+
}
119+
120+
[Fact]
121+
[Trait(Category.AcceptanceType, Category.CheckIn)]
122+
public void TestListAllPools()
123+
{
124+
BatchController controller = BatchController.NewInstance;
125+
string resourceGroupName = "test-list-pool";
126+
string accountName = "testlistpool";
127+
string location = "eastus";
128+
string poolName1 = "testName1";
129+
string poolName2 = "testName2";
130+
string poolName3 = "thirdtestName";
131+
int count = 3;
132+
BatchAccountContext context = null;
133+
controller.RunPsTestWorkflow(
134+
() => { return new string[] { string.Format("Test-ListAllPools '{0}' '{1}'", accountName, count) }; },
135+
() =>
136+
{
137+
context = ScenarioTestHelpers.CreateTestAccountAndResourceGroup(controller, resourceGroupName, accountName, location);
138+
ScenarioTestHelpers.CreateTestPool(context, poolName1);
139+
ScenarioTestHelpers.CreateTestPool(context, poolName2);
140+
ScenarioTestHelpers.CreateTestPool(context, poolName3);
141+
},
142+
() =>
143+
{
144+
ScenarioTestHelpers.DeletePool(context, poolName1);
145+
ScenarioTestHelpers.DeletePool(context, poolName2);
146+
ScenarioTestHelpers.DeletePool(context, poolName3);
147+
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
148+
},
149+
TestUtilities.GetCallingClass(),
150+
TestUtilities.GetCurrentMethodName());
151+
}
152+
153+
}
154+
155+
// Cmdlets that use the HTTP Recorder interceptor for use with scenario tests
156+
[Cmdlet(VerbsCommon.Get, "AzureBatchPool_ST", DefaultParameterSetName = Constants.ODataFilterParameterSet)]
157+
public class GetBatchPoolScenarioTestCommand : GetBatchPoolCommand
158+
{
159+
public override void ExecuteCmdlet()
160+
{
161+
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
162+
base.ExecuteCmdlet();
163+
}
164+
}
165+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
<#
16+
.SYNOPSIS
17+
Tests querying for a Batch Pool by name
18+
#>
19+
function Test-GetPoolByName
20+
{
21+
param([string]$accountName, [string]$poolName)
22+
23+
$context = Get-AzureBatchAccountKeys -Name $accountName
24+
$pool = Get-AzureBatchPool_ST -Name $poolName -BatchContext $context
25+
26+
Assert-AreEqual $poolName $pool.Name
27+
}
28+
29+
<#
30+
.SYNOPSIS
31+
Tests querying for Batch Pools using a filter
32+
#>
33+
function Test-ListPoolsByFilter
34+
{
35+
param([string]$accountName, [string]$poolPrefix, [string]$matches)
36+
37+
$context = Get-AzureBatchAccountKeys -Name $accountName
38+
$poolFilter = "startswith(name,'" + "$poolPrefix" + "')"
39+
$pools = Get-AzureBatchPool_ST -Filter $poolFilter -BatchContext $context
40+
41+
Assert-AreEqual $matches $pools.Length
42+
foreach($pool in $pools)
43+
{
44+
Assert-True { $pool.Name.StartsWith("$poolPrefix") }
45+
}
46+
}
47+
48+
<#
49+
.SYNOPSIS
50+
Tests querying for Batch Pools and supplying a max count
51+
#>
52+
function Test-ListPoolsWithMaxCount
53+
{
54+
param([string]$accountName, [string]$maxCount)
55+
56+
$context = Get-AzureBatchAccountKeys -Name $accountName
57+
$pools = Get-AzureBatchPool_ST -MaxCount $maxCount -BatchContext $context
58+
59+
Assert-AreEqual $maxCount $pools.Length
60+
}
61+
62+
<#
63+
.SYNOPSIS
64+
Tests querying for all Pools under an account
65+
#>
66+
function Test-ListAllPools
67+
{
68+
param([string]$accountName, [string]$count)
69+
70+
$context = Get-AzureBatchAccountKeys -Name $accountName
71+
$workItems = Get-AzureBatchPool_ST -BatchContext $context
72+
73+
Assert-AreEqual $count $workItems.Length
74+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ public static void CleanupTestAccount(BatchController controller, string resourc
6767
controller.ResourceManagementClient.ResourceGroups.Delete(resourceGroupName);
6868
}
6969

70+
/// <summary>
71+
/// Creates a test Pool for use in Scenario tests.
72+
/// TODO: Replace with new Pool cmdlet when it exists.
73+
/// </summary>
74+
public static void CreateTestPool(BatchAccountContext context, string poolName)
75+
{
76+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
77+
{
78+
using (IPoolManager poolManager = context.BatchOMClient.OpenPoolManager())
79+
{
80+
ICloudPool pool = poolManager.CreatePool(poolName, "4", "small", 3);
81+
pool.Commit();
82+
}
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Deletes a Pool used in a Scenario test.
88+
/// TODO: Replace with remove Pool cmdlet when it exists.
89+
/// </summary>
90+
public static void DeletePool(BatchAccountContext context, string poolName)
91+
{
92+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
93+
{
94+
using (IPoolManager poolManager = context.BatchOMClient.OpenPoolManager())
95+
{
96+
poolManager.DeletePool(poolName);
97+
}
98+
}
99+
}
100+
70101
/// <summary>
71102
/// Creates a test WorkItem for use in Scenario tests.
72103
/// TODO: Replace with new WorkItem cmdlet when it exists.

0 commit comments

Comments
 (0)