Skip to content

Commit 8378a94

Browse files
author
jasper-schneider
committed
Tests for new cmdlets
1 parent cc03e1e commit 8378a94

File tree

15 files changed

+9821
-9
lines changed

15 files changed

+9821
-9
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,14 @@
180180
<Compile Include="Certificates\NewBatchCertificateCommandTests.cs" />
181181
<Compile Include="Certificates\RemoveBatchCertificateCommandTests.cs" />
182182
<Compile Include="Certificates\StopBatchCertificateDeletionCommandTests.cs" />
183+
<Compile Include="ComputeNodes\RemoveBatchComputeNodeCommandTests.cs" />
183184
<Compile Include="ComputeNodes\ResetBatchComputeNodeCommandTests.cs" />
184185
<Compile Include="ComputeNodes\RestartBatchComputeNodeCommandTests.cs" />
185186
<Compile Include="ComputeNodeUsers\SetBatchComputeNoderUserCommandTests.cs" />
186187
<Compile Include="Files\GetBatchRemoteDesktopProtocolFileCommandTests.cs" />
187188
<Compile Include="Files\GetBatchNodeFileCommandTests.cs" />
188189
<Compile Include="Files\GetBatchNodeFileContentCommandTests.cs" />
190+
<Compile Include="Files\RemoveBatchNodeFileCommandTests.cs" />
189191
<Compile Include="JobSchedules\DisableBatchJobScheduleCommandTests.cs" />
190192
<Compile Include="JobSchedules\EnableBatchJobScheduleCommandTests.cs" />
191193
<Compile Include="JobSchedules\SetBatchJobScheduleCommandTests.cs" />
@@ -363,6 +365,12 @@
363365
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestReimageComputeNodePipeline.json">
364366
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
365367
</None>
368+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestRemoveComputeNodeById.json">
369+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
370+
</None>
371+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestRemoveComputeNodePipeline.json">
372+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
373+
</None>
366374
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestCreateComputeNodeUser.json">
367375
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
368376
</None>
@@ -375,6 +383,18 @@
375383
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestUpdateComputeNodeUser.json">
376384
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
377385
</None>
386+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestDeleteNodeFileByComputeNodeByName.json">
387+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
388+
</None>
389+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestDeleteNodeFileByComputeNodeByPipeline.json">
390+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
391+
</None>
392+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestDeleteNodeFileByTaskByName.json">
393+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
394+
</None>
395+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestDeleteNodeFileByTaskByPipeline.json">
396+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
397+
</None>
378398
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetNodeFileByComputeNodeByName.json">
379399
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
380400
</None>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.Models;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Moq;
22+
using System.Collections.Generic;
23+
using System.Management.Automation;
24+
using System.Threading.Tasks;
25+
using Xunit;
26+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
27+
28+
namespace Microsoft.Azure.Commands.Batch.Test.Pools
29+
{
30+
public class RemoveBatchComputeNodeCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
31+
{
32+
private RemoveBatchComputeNodeCommand cmdlet;
33+
private Mock<BatchClient> batchClientMock;
34+
private Mock<ICommandRuntime> commandRuntimeMock;
35+
36+
public RemoveBatchComputeNodeCommandTests()
37+
{
38+
batchClientMock = new Mock<BatchClient>();
39+
commandRuntimeMock = new Mock<ICommandRuntime>();
40+
cmdlet = new RemoveBatchComputeNodeCommand()
41+
{
42+
CommandRuntime = commandRuntimeMock.Object,
43+
BatchClient = batchClientMock.Object,
44+
};
45+
}
46+
47+
[Fact]
48+
[Trait(Category.AcceptanceType, Category.CheckIn)]
49+
public void RemoveBatchComputeNodeParametersTest()
50+
{
51+
// Setup cmdlet to skip confirmation popup
52+
cmdlet.Force = true;
53+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
54+
55+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
56+
cmdlet.BatchContext = context;
57+
cmdlet.Id = null;
58+
59+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
60+
61+
cmdlet.PoolId = "testPool";
62+
63+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
64+
65+
cmdlet.Id = "computeNode1";
66+
67+
// Don't go to the service on a Remove ComputeNode call
68+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeRemoveParameters, ComputeNodeRemoveResponse>();
69+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
70+
71+
// Verify no exceptions when required parameter is set
72+
cmdlet.ExecuteCmdlet();
73+
}
74+
75+
[Fact]
76+
[Trait(Category.AcceptanceType, Category.CheckIn)]
77+
public void RemoveComputeNodeRequestTest()
78+
{
79+
// Setup cmdlet to skip confirmation popup
80+
cmdlet.Force = true;
81+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
82+
83+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
84+
cmdlet.BatchContext = context;
85+
86+
cmdlet.PoolId = "testPool";
87+
cmdlet.Id = "computeNode1";
88+
cmdlet.DeallocationOption = ComputeNodeDeallocationOption.Terminate;
89+
cmdlet.ResizeTimeout = TimeSpan.FromMinutes(8);
90+
91+
ComputeNodeDeallocationOption? requestDeallocationOption = null;
92+
TimeSpan? requestResizeTimeout = null;
93+
94+
// Don't go to the service on a Remove ComputeNode call
95+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
96+
{
97+
BatchRequest<ComputeNodeRemoveParameters, ComputeNodeRemoveResponse> request =
98+
(BatchRequest<ComputeNodeRemoveParameters, ComputeNodeRemoveResponse>)baseRequest;
99+
100+
request.ServiceRequestFunc = (cancellationToken) =>
101+
{
102+
// Grab the parameters from the outgoing request.
103+
requestDeallocationOption = request.TypedParameters.ComputeNodeDeallocationOption;
104+
requestResizeTimeout = request.TypedParameters.ResizeTimeout;
105+
106+
ComputeNodeRemoveResponse response = new ComputeNodeRemoveResponse();
107+
Task<ComputeNodeRemoveResponse> task = Task.FromResult(response);
108+
return task;
109+
};
110+
});
111+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
112+
113+
cmdlet.ExecuteCmdlet();
114+
115+
// Verify that the parameters were properly set on the outgoing request
116+
Assert.Equal(cmdlet.DeallocationOption, requestDeallocationOption);
117+
Assert.Equal(cmdlet.ResizeTimeout, requestResizeTimeout);
118+
}
119+
}
120+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.Protocol;
19+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
20+
using Moq;
21+
using System.Collections.Generic;
22+
using System.Management.Automation;
23+
using Xunit;
24+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
25+
using Microsoft.Azure.Batch.Protocol.Models;
26+
27+
namespace Microsoft.Azure.Commands.Batch.Test.Files
28+
{
29+
public class RemoveBatchNodeFileCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
30+
{
31+
private RemoveBatchNodeFileCommand cmdlet;
32+
private Mock<BatchClient> batchClientMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
35+
public RemoveBatchNodeFileCommandTests()
36+
{
37+
batchClientMock = new Mock<BatchClient>();
38+
commandRuntimeMock = new Mock<ICommandRuntime>();
39+
cmdlet = new RemoveBatchNodeFileCommand()
40+
{
41+
CommandRuntime = commandRuntimeMock.Object,
42+
BatchClient = batchClientMock.Object,
43+
};
44+
}
45+
46+
[Fact]
47+
[Trait(Category.AcceptanceType, Category.CheckIn)]
48+
public void RemoveBatchNodeFileParametersTest()
49+
{
50+
// Setup cmdlet to skip confirmation popup
51+
cmdlet.Force = true;
52+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
53+
54+
// Setup cmdlet without required parameters
55+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
56+
cmdlet.BatchContext = context;
57+
cmdlet.JobId = null;
58+
cmdlet.TaskId = null;
59+
cmdlet.Name = null;
60+
cmdlet.InputObject = null;
61+
62+
// Don't go to the service on a Delete NodeFile call
63+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<NodeFileDeleteParameters, NodeFileDeleteResponse>();
64+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
65+
66+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
67+
68+
// Fill required task details
69+
cmdlet.JobId = "job-1";
70+
cmdlet.TaskId = "task";
71+
cmdlet.Name = "stdout.txt";
72+
73+
// Verify no exceptions occur
74+
cmdlet.ExecuteCmdlet();
75+
76+
// Setup compute node parameters
77+
cmdlet.JobId = null;
78+
cmdlet.TaskId = null;
79+
cmdlet.PoolId = "testPool";
80+
cmdlet.ComputeNodeId = "computeNode-1";
81+
82+
// Verify no exceptions occur
83+
cmdlet.ExecuteCmdlet();
84+
}
85+
}
86+
}

src/ResourceManager/AzureBatch/Commands.Batch.Test/ScenarioTests/Common.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
1414

15+
<#
16+
.SYNOPSIS
17+
Sleeps but only during recording.
18+
#>
19+
function Start-TestSleep($milliseconds)
20+
{
21+
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback)
22+
{
23+
Start-Sleep -Milliseconds $milliseconds
24+
}
25+
}
26+
1527
<#
1628
.SYNOPSIS
1729
Gets a ScenarioTestContext for the specified account

src/ResourceManager/AzureBatch/Commands.Batch.Test/ScenarioTests/ComputeNodeTests.cs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,74 @@ public void TestListComputeNodePipeline()
123123
TestUtilities.GetCurrentMethodName());
124124
}
125125

126+
[Fact]
127+
[Trait(Category.AcceptanceType, Category.CheckIn)]
128+
public void TestRemoveComputeNodeById()
129+
{
130+
TestRemoveComputeNode(false, TestUtilities.GetCurrentMethodName());
131+
}
132+
133+
[Fact]
134+
[Trait(Category.AcceptanceType, Category.CheckIn)]
135+
public void TestRemoveComputeNodePipeline()
136+
{
137+
TestRemoveComputeNode(true, TestUtilities.GetCurrentMethodName());
138+
}
139+
126140
[Fact]
127141
[Trait(Category.AcceptanceType, Category.CheckIn)]
128142
public void TestRebootComputeNodeById()
129143
{
130-
TestRebootComputeNode(false);
144+
TestRebootComputeNode(false, TestUtilities.GetCurrentMethodName());
131145
}
132146

133147
[Fact]
134148
[Trait(Category.AcceptanceType, Category.CheckIn)]
135149
public void TestRebootComputeNodePipeline()
136150
{
137-
TestRebootComputeNode(true);
151+
TestRebootComputeNode(true, TestUtilities.GetCurrentMethodName());
138152
}
139153

140154
[Fact]
141155
[Trait(Category.AcceptanceType, Category.CheckIn)]
142156
public void TestReimageComputeNodeById()
143157
{
144-
TestReimageComputeNode(false);
158+
TestReimageComputeNode(false, TestUtilities.GetCurrentMethodName());
145159
}
146160

147161
[Fact]
148162
[Trait(Category.AcceptanceType, Category.CheckIn)]
149163
public void TestReimageComputeNodePipeline()
150164
{
151-
TestReimageComputeNode(true);
165+
TestReimageComputeNode(true, TestUtilities.GetCurrentMethodName());
166+
}
167+
168+
private void TestRemoveComputeNode(bool usePipeline, string testMethodName)
169+
{
170+
BatchController controller = BatchController.NewInstance;
171+
BatchAccountContext context = null;
172+
string computeNodeId = null;
173+
int originalDedicated = 3;
174+
controller.RunPsTestWorkflow(
175+
() => { return new string[] { string.Format("Test-RemoveComputeNode '{0}' '{1}' '{2}' '{3}'", accountName, poolId, computeNodeId, usePipeline ? 1 : 0) }; },
176+
() =>
177+
{
178+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
179+
originalDedicated = ScenarioTestHelpers.GetPoolCurrentDedicated(controller, context, poolId);
180+
ScenarioTestHelpers.WaitForSteadyPoolAllocation(controller, context, poolId);
181+
computeNodeId = ScenarioTestHelpers.GetComputeNodeId(controller, context, poolId);
182+
ScenarioTestHelpers.WaitForIdleComputeNode(controller, context, poolId, computeNodeId);
183+
},
184+
() =>
185+
{
186+
ScenarioTestHelpers.WaitForSteadyPoolAllocation(controller, context, poolId);
187+
ScenarioTestHelpers.ResizePool(controller, context, poolId, originalDedicated);
188+
},
189+
TestUtilities.GetCallingClass(),
190+
testMethodName);
152191
}
153192

154-
private void TestRebootComputeNode(bool usePipeline)
193+
private void TestRebootComputeNode(bool usePipeline, string testMethodName)
155194
{
156195
BatchController controller = BatchController.NewInstance;
157196
BatchAccountContext context = null;
@@ -166,10 +205,10 @@ private void TestRebootComputeNode(bool usePipeline)
166205
},
167206
null,
168207
TestUtilities.GetCallingClass(),
169-
usePipeline ? "TestRebootComputeNodePipeline" : "TestRebootComputeNodeById");
208+
testMethodName);
170209
}
171210

172-
private void TestReimageComputeNode(bool usePipeline)
211+
private void TestReimageComputeNode(bool usePipeline, string testMethodName)
173212
{
174213
BatchController controller = BatchController.NewInstance;
175214
BatchAccountContext context = null;
@@ -184,7 +223,7 @@ private void TestReimageComputeNode(bool usePipeline)
184223
},
185224
null,
186225
TestUtilities.GetCallingClass(),
187-
usePipeline ? "TestReimageComputeNodePipeline" : "TestReimageComputeNodeById");
226+
testMethodName);
188227
}
189228
}
190229
}

0 commit comments

Comments
 (0)