Skip to content

Commit 9ca205b

Browse files
dpwatrouspaterasMSFT
authored andcommitted
[Batch] Added Get-AzBatchTaskSlotCounts cmdlet
1 parent baba39a commit 9ca205b

File tree

8 files changed

+324
-33
lines changed

8 files changed

+324
-33
lines changed

src/Batch/Batch.Test/BatchTestHelpers.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -712,20 +712,33 @@ public static AzureOperationResponse<
712712
/// <summary>
713713
/// Builds a TaskCountsGetResponse object
714714
/// </summary>
715-
public static AzureOperationResponse<ProxyModels.TaskCounts, ProxyModels.JobGetTaskCountsHeaders> CreateTaskCountsGetResponse(
716-
int active, int running, int succeeded, int failed)
715+
public static AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders> CreateTaskCountsGetResponse(
716+
int requiredTaskSlots, int activeTasks, int runningTasks, int succeededTasks, int failedTasks)
717717
{
718-
var response = new AzureOperationResponse<ProxyModels.TaskCounts, ProxyModels.JobGetTaskCountsHeaders>();
718+
var response = new AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders>();
719719
response.Response = new HttpResponseMessage(HttpStatusCode.OK);
720720

721+
var completedTasks = succeededTasks + failedTasks;
722+
721723
ProxyModels.TaskCounts taskCounts = new ProxyModels.TaskCounts();
722-
taskCounts.Active = active;
723-
taskCounts.Running = running;
724-
taskCounts.Succeeded = succeeded;
725-
taskCounts.Failed = failed;
726-
taskCounts.Completed = succeeded + failed;
724+
taskCounts.Active = activeTasks;
725+
taskCounts.Running = runningTasks;
726+
taskCounts.Succeeded = succeededTasks;
727+
taskCounts.Failed = failedTasks;
728+
taskCounts.Completed = completedTasks;
729+
730+
ProxyModels.TaskSlotCounts slotCounts = new ProxyModels.TaskSlotCounts();
731+
slotCounts.Active = requiredTaskSlots * activeTasks;
732+
slotCounts.Running = requiredTaskSlots * runningTasks;
733+
slotCounts.Succeeded = requiredTaskSlots * succeededTasks;
734+
slotCounts.Failed = requiredTaskSlots * failedTasks;
735+
slotCounts.Completed = requiredTaskSlots * completedTasks;
736+
737+
ProxyModels.TaskCountsResult result = new ProxyModels.TaskCountsResult();
738+
result.TaskCounts = taskCounts;
739+
result.TaskSlotCounts = slotCounts;
727740

728-
response.Body = taskCounts;
741+
response.Body = result;
729742

730743
return response;
731744
}

src/Batch/Batch.Test/Tasks/GetBatchTaskCountsCommandTests.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Microsoft.Azure.Commands.Batch.Test.Tasks
2828
{
2929
public class GetBatchTaskCountsCommandTests
3030
{
31-
private GetBatchTaskCountCommand cmdlet;
31+
private GetBatchTaskCountsCommand cmdlet;
3232
private Mock<BatchClient> batchClientMock;
3333
private Mock<ICommandRuntime> commandRuntimeMock;
3434

@@ -37,7 +37,7 @@ public GetBatchTaskCountsCommandTests(Xunit.Abstractions.ITestOutputHelper outpu
3737
ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output));
3838
batchClientMock = new Mock<BatchClient>();
3939
commandRuntimeMock = new Mock<ICommandRuntime>();
40-
cmdlet = new GetBatchTaskCountCommand()
40+
cmdlet = new GetBatchTaskCountsCommand()
4141
{
4242
CommandRuntime = commandRuntimeMock.Object,
4343
BatchClient = batchClientMock.Object,
@@ -53,33 +53,37 @@ public void GetBatchTaskCountsTest()
5353
cmdlet.BatchContext = context;
5454
cmdlet.JobId = "job-1";
5555

56+
const int requiredSlots = 2;
5657
const int active = 3;
5758
const int running = 5;
5859
const int succeeded = 2;
5960
const int failed = 1;
6061

6162
// Build a TaskCounts instead of querying the service on a Get TaskCounts call
62-
AzureOperationResponse<ProxyModels.TaskCounts, ProxyModels.JobGetTaskCountsHeaders> response =
63-
BatchTestHelpers.CreateTaskCountsGetResponse(active, running, succeeded, failed);
63+
AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders> response =
64+
BatchTestHelpers.CreateTaskCountsGetResponse(requiredSlots, active, running, succeeded, failed);
65+
6466
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
6567
ProxyModels.JobGetTaskCountsOptions,
66-
AzureOperationResponse<ProxyModels.TaskCounts, ProxyModels.JobGetTaskCountsHeaders>>(response);
68+
AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders>>(response);
6769

6870
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
6971

7072
// Setup the cmdlet to write pipeline output to a list that can be examined later
7173
PSTaskCounts taskCounts = null;
72-
commandRuntimeMock.Setup(r =>
73-
r.WriteObject(It.IsAny<PSTaskCounts>()))
74-
.Callback<object>(p => taskCounts = (PSTaskCounts)p);
74+
commandRuntimeMock
75+
.Setup(r => r.WriteObject(It.IsAny<PSTaskCounts>()))
76+
.Callback<object>(p => {
77+
taskCounts = (PSTaskCounts)p;
78+
});
7579

7680
cmdlet.ExecuteCmdlet();
7781

78-
Assert.Equal(active, taskCounts.Active);
79-
Assert.Equal(running, taskCounts.Running);
80-
Assert.Equal(succeeded + failed, taskCounts.Completed);
81-
Assert.Equal(succeeded, taskCounts.Succeeded);
82-
Assert.Equal(failed, taskCounts.Failed);
82+
Assert.Equal(3, taskCounts.Active);
83+
Assert.Equal(5, taskCounts.Running);
84+
Assert.Equal(3, taskCounts.Completed);
85+
Assert.Equal(2, taskCounts.Succeeded);
86+
Assert.Equal(1, taskCounts.Failed);
8387
}
8488
}
8589
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.Batch.Protocol;
17+
using Microsoft.Azure.Commands.Batch.Models;
18+
using Microsoft.Rest.Azure;
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 ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
26+
27+
namespace Microsoft.Azure.Commands.Batch.Test.Tasks
28+
{
29+
public class GetBatchTaskSlotCountsCommandTests
30+
{
31+
private GetBatchTaskSlotCountsCommand cmdlet;
32+
private Mock<BatchClient> batchClientMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
35+
public GetBatchTaskSlotCountsCommandTests(Xunit.Abstractions.ITestOutputHelper output)
36+
{
37+
ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output));
38+
batchClientMock = new Mock<BatchClient>();
39+
commandRuntimeMock = new Mock<ICommandRuntime>();
40+
cmdlet = new GetBatchTaskSlotCountsCommand()
41+
{
42+
CommandRuntime = commandRuntimeMock.Object,
43+
BatchClient = batchClientMock.Object,
44+
};
45+
}
46+
47+
[Fact]
48+
[Trait(Category.AcceptanceType, Category.CheckIn)]
49+
public void GetBatchTaskSlotCountsTest()
50+
{
51+
// Setup cmdlet to get task counts by job id
52+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
53+
cmdlet.BatchContext = context;
54+
cmdlet.JobId = "job-1";
55+
56+
const int requiredSlots = 2;
57+
const int active = 3;
58+
const int running = 5;
59+
const int succeeded = 2;
60+
const int failed = 1;
61+
62+
// Build a TaskCountsResult instead of querying the service
63+
AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders> response =
64+
BatchTestHelpers.CreateTaskCountsGetResponse(requiredSlots, active, running, succeeded, failed);
65+
66+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
67+
ProxyModels.JobGetTaskCountsOptions,
68+
AzureOperationResponse<ProxyModels.TaskCountsResult, ProxyModels.JobGetTaskCountsHeaders>>(response);
69+
70+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
71+
72+
// Setup the cmdlet to write pipeline output to a list that can be examined later
73+
PSTaskSlotCounts slotCounts = null;
74+
commandRuntimeMock
75+
.Setup(r => r.WriteObject(It.IsAny<PSTaskSlotCounts>()))
76+
.Callback<object>(p => {
77+
slotCounts = (PSTaskSlotCounts)p;
78+
});
79+
80+
cmdlet.ExecuteCmdlet();
81+
82+
Assert.Equal(6, slotCounts.Active);
83+
Assert.Equal(10, slotCounts.Running);
84+
Assert.Equal(6, slotCounts.Completed);
85+
Assert.Equal(4, slotCounts.Succeeded);
86+
Assert.Equal(2, slotCounts.Failed);
87+
}
88+
}
89+
}

src/Batch/Batch/Tasks/GetBatchTaskCountCommand.cs renamed to src/Batch/Batch/Tasks/GetBatchTaskCountsCommand.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
namespace Microsoft.Azure.Commands.Batch
2222
{
23-
[GenericBreakingChange("Get-AzBatchTaskCounts alias will be removed in an upcoming breaking change release", "2.0.0")]
24-
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzurePrefix + "BatchTaskCount"),OutputType(typeof(PSTaskCounts))]
25-
[Alias("Get-AzBatchTaskCounts")]
26-
public class GetBatchTaskCountCommand : BatchObjectModelCmdletBase
23+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzurePrefix + "BatchTaskCounts"), OutputType(typeof(PSTaskCounts))]
24+
[Alias("Get-AzBatchTaskCount")]
25+
public class GetBatchTaskCountsCommand : BatchObjectModelCmdletBase
2726
{
2827
[Parameter(Position = 0, ParameterSetName = Constants.IdParameterSet, Mandatory = true,
2928
ValueFromPipelineByPropertyName = true, HelpMessage = "The id of the job for which to get task counts.")]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Commands.Batch.Models;
17+
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
18+
using System.Management.Automation;
19+
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
20+
21+
namespace Microsoft.Azure.Commands.Batch
22+
{
23+
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzurePrefix + "BatchTaskSlotCounts"),OutputType(typeof(PSTaskSlotCounts))]
24+
[Alias("Get-AzBatchTaskSlotCount")]
25+
public class GetBatchTaskSlotCountsCommand : BatchObjectModelCmdletBase
26+
{
27+
[Parameter(Position = 0, ParameterSetName = Constants.IdParameterSet, Mandatory = true,
28+
ValueFromPipelineByPropertyName = true, HelpMessage = "The id of the job for which to get task slot counts.")]
29+
[ValidateNotNullOrEmpty]
30+
public string JobId { get; set; }
31+
32+
[Parameter(Position = 0, ParameterSetName = Constants.ParentObjectParameterSet, ValueFromPipeline = true)]
33+
[ValidateNotNullOrEmpty]
34+
public PSCloudJob Job { get; set; }
35+
36+
protected override void ExecuteCmdletImpl()
37+
{
38+
GetTaskCountsOptions options = new GetTaskCountsOptions(this.BatchContext, this.JobId, this.Job, this.AdditionalBehaviors);
39+
40+
PSTaskSlotCounts taskSlotCounts = BatchClient.GetTaskSlotCounts(options);
41+
42+
WriteObject(taskSlotCounts);
43+
}
44+
}
45+
}

src/Batch/Batch/help/Az.Batch.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ Gets Batch supported images for a Batch account.
104104
### [Get-AzBatchTask](Get-AzBatchTask.md)
105105
Gets the Batch tasks for a job.
106106

107-
### [Get-AzBatchTaskCount](Get-AzBatchTaskCount.md)
107+
### [Get-AzBatchTaskCounts](Get-AzBatchTaskCount.md)
108108
Gets the task counts for the specified job.
109109

110+
### [Get-AzBatchTaskSlotCounts](Get-AzBatchTaskCount.md)
111+
Gets the task slot counts for the specified job.
112+
110113
### [New-AzBatchAccount](New-AzBatchAccount.md)
111114
Creates a Batch account.
112115

src/Batch/Batch/help/Get-AzBatchTaskCount.md renamed to src/Batch/Batch/help/Get-AzBatchTaskCounts.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
external help file: Microsoft.Azure.PowerShell.Cmdlets.Batch.dll-Help.xml
33
Module Name: Az.Batch
4-
online version: https://docs.microsoft.com/powershell/module/az.batch/get-azbatchtaskcount
4+
online version: https://docs.microsoft.com/powershell/module/az.batch/get-azbatchtaskcounts
55
schema: 2.0.0
66
---
77

8-
# Get-AzBatchTaskCount
8+
# Get-AzBatchTaskCounts
99

1010
## SYNOPSIS
1111
Gets the task counts for the specified job.
@@ -14,26 +14,26 @@ Gets the task counts for the specified job.
1414

1515
### Id
1616
```
17-
Get-AzBatchTaskCount [-JobId] <String> -BatchContext <BatchAccountContext>
17+
Get-AzBatchTaskCounts [-JobId] <String> -BatchContext <BatchAccountContext>
1818
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
1919
```
2020

2121
### ParentObject
2222
```
23-
Get-AzBatchTaskCount [[-Job] <PSCloudJob>] -BatchContext <BatchAccountContext>
23+
Get-AzBatchTaskCounts [[-Job] <PSCloudJob>] -BatchContext <BatchAccountContext>
2424
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
2525
```
2626

2727
## DESCRIPTION
28-
The **Get-AzBatchTaskCount** cmdlet gets the Azure Batch tasks count for a Batch job.
28+
The **Get-AzBatchTaskCounts** cmdlet gets the Azure Batch task counts for a Batch job.
2929
Specify a job by either the *JobId* parameter or the *Job* parameter.
3030
Task counts provide a count of the tasks by active, running or completed task state, and a count of tasks which succeeded or failed. Tasks in the preparing state are counted as running. If the validationStatus is unvalidated, then the Batch service has not been able to check state counts against the task states as reported in the List Tasks API. The validationStatus may be unvalidated if the job contains more than 200,000 tasks.
3131

3232
## EXAMPLES
3333

3434
### Example 1: Get task counts by ID
3535
```
36-
PS C:\> Get-AzBatchTaskCount -JobId "Job01" -Id "Task03" -BatchContext $Context
36+
PS C:\> Get-AzBatchTaskCounts -JobId "Job01" -Id "Task03" -BatchContext $Context
3737
Active : 1
3838
Completed : 0
3939
Failed : 0

0 commit comments

Comments
 (0)