Skip to content

Commit e9a19eb

Browse files
author
jasper-schneider
committed
Batch: Add OData Select and Expand clauses
Add Select and Expand parameters to Batch Get cmdlets Add tests for these parameters.
1 parent a59a8b3 commit e9a19eb

38 files changed

+5940
-133
lines changed

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

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

15-
using System.Linq;
16-
using System.Net;
17-
using System.Threading.Tasks;
1815
using Microsoft.Azure.Batch;
1916
using Microsoft.Azure.Batch.Protocol;
2017
using Microsoft.Azure.Management.Batch.Models;
2118
using System;
2219
using System.Collections;
2320
using System.Collections.Generic;
21+
using System.Net;
2422
using System.Reflection;
23+
using System.Threading.Tasks;
2524
using Xunit;
2625
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
2726

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@
280280
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.BatchAccountTests\TestUpdatesExistingBatchAccount.json">
281281
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
282282
</None>
283+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestGetAndListComputeNodesWithSelect.json">
284+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
285+
</None>
283286
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestGetComputeNodeById.json">
284287
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
285288
</None>
@@ -382,6 +385,9 @@
382385
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestDisableAndEnableJobSchedule.json">
383386
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
384387
</None>
388+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestGetAndListJobSchedulesWithSelect.json">
389+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
390+
</None>
385391
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestGetJobScheduleById.json">
386392
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
387393
</None>
@@ -415,6 +421,9 @@
415421
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestDisableAndEnableJob.json">
416422
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
417423
</None>
424+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestGetAndListJobsWithSelect.json">
425+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
426+
</None>
418427
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestGetJobById.json">
419428
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
420429
</None>
@@ -472,6 +481,9 @@
472481
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestEvaluateAutoScaleByPipeline.json">
473482
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
474483
</None>
484+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestGetAndListPoolsWithSelect.json">
485+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
486+
</None>
475487
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestGetPoolById.json">
476488
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
477489
</None>
@@ -511,6 +523,9 @@
511523
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestDeleteTaskPipeline.json">
512524
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
513525
</None>
526+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestGetAndListTasksWithSelect.json">
527+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
528+
</None>
514529
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestGetTaskById.json">
515530
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
516531
</None>

src/ResourceManager/AzureBatch/Commands.Batch.Test/ComputeNodes/GetBatchComputeNodeCommandTests.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using System.Linq;
2323
using System.Management.Automation;
24+
using System.Threading.Tasks;
2425
using Xunit;
2526
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2627

@@ -72,39 +73,63 @@ public void GetBatchComputeNodeTest()
7273

7374
[Fact]
7475
[Trait(Category.AcceptanceType, Category.CheckIn)]
75-
public void ListBatchComputeNodesByODataFilterTest()
76+
public void GetBatchComputeNodeODataTest()
7677
{
77-
// Setup cmdlet to list vms using an OData filter.
78+
// Setup cmdlet to get a single compute node
7879
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
7980
cmdlet.BatchContext = context;
80-
cmdlet.PoolId = "pool";
81-
cmdlet.Id = null;
82-
cmdlet.Filter = "state -eq 'idle'";
81+
cmdlet.PoolId = "testPool";
82+
cmdlet.Id = "computeNode1";
83+
cmdlet.Select = "id,state";
8384

84-
string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2" };
85+
string requestSelect = null;
8586

86-
// Build some compute nodes instead of querying the service on a List ComputeNodes call
87-
ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
88-
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeListParameters, ComputeNodeListResponse>(response);
89-
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
87+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
88+
ComputeNodeGetResponse getResponse = BatchTestHelpers.CreateComputeNodeGetResponse(cmdlet.Id);
89+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeGetParameters, ComputeNodeGetResponse>(getResponse);
90+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
91+
{
92+
requestSelect = request.Parameters.DetailLevel.SelectClause;
9093

91-
// Setup the cmdlet to write pipeline output to a list that can be examined later
92-
List<PSComputeNode> pipeline = new List<PSComputeNode>();
93-
commandRuntimeMock.Setup(r =>
94-
r.WriteObject(It.IsAny<PSComputeNode>()))
95-
.Callback<object>(c => pipeline.Add((PSComputeNode)c));
94+
return Task.FromResult(response);
95+
});
96+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
9697

9798
cmdlet.ExecuteCmdlet();
9899

99-
// Verify that the cmdlet wrote the constructed compute nodes to the pipeline
100-
Assert.Equal(2, pipeline.Count);
101-
int computeNodeCount = 0;
102-
foreach (PSComputeNode c in pipeline)
100+
Assert.Equal(cmdlet.Select, requestSelect);
101+
}
102+
103+
[Fact]
104+
[Trait(Category.AcceptanceType, Category.CheckIn)]
105+
public void ListBatchComputeNodesODataTest()
106+
{
107+
// Setup cmdlet to list compute nodes using an OData filter
108+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
109+
cmdlet.BatchContext = context;
110+
cmdlet.PoolId = "testPool";
111+
cmdlet.Id = null;
112+
cmdlet.Filter = "startswith(id,'test')";
113+
cmdlet.Select = "id,state";
114+
115+
string requestFilter = null;
116+
string requestSelect = null;
117+
118+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
119+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeListParameters, ComputeNodeListResponse>();
120+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
103121
{
104-
Assert.True(idsOfConstructedComputeNodes.Contains(c.Id));
105-
computeNodeCount++;
106-
}
107-
Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount);
122+
requestFilter = request.Parameters.DetailLevel.FilterClause;
123+
requestSelect = request.Parameters.DetailLevel.SelectClause;
124+
125+
return Task.FromResult(response);
126+
});
127+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
128+
129+
cmdlet.ExecuteCmdlet();
130+
131+
Assert.Equal(cmdlet.Filter, requestFilter);
132+
Assert.Equal(cmdlet.Select, requestSelect);
108133
}
109134

110135
[Fact]

src/ResourceManager/AzureBatch/Commands.Batch.Test/JobSchedules/GetBatchJobScheduleCommandTests.cs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using System.Linq;
2323
using System.Management.Automation;
24+
using System.Threading.Tasks;
2425
using Xunit;
2526
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2627

@@ -71,38 +72,69 @@ public void GetBatchJobScheduleTest()
7172

7273
[Fact]
7374
[Trait(Category.AcceptanceType, Category.CheckIn)]
74-
public void ListBatchJobScheduleByODataFilterTest()
75+
public void GetBatchJobScheduleODataTest()
76+
{
77+
// Setup cmdlet to get a single job schedule
78+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
79+
cmdlet.BatchContext = context;
80+
cmdlet.Id = "testJobSchedule";
81+
cmdlet.Select = "id,state";
82+
cmdlet.Expand = "stats";
83+
84+
string requestSelect = null;
85+
string requestExpand = null;
86+
87+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
88+
CloudJobScheduleGetResponse getResponse = BatchTestHelpers.CreateCloudJobScheduleGetResponse(cmdlet.Id);
89+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobScheduleGetParameters, CloudJobScheduleGetResponse>(getResponse);
90+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
91+
{
92+
requestSelect = request.Parameters.DetailLevel.SelectClause;
93+
requestExpand = request.Parameters.DetailLevel.ExpandClause;
94+
95+
return Task.FromResult(response);
96+
});
97+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
98+
99+
cmdlet.ExecuteCmdlet();
100+
101+
Assert.Equal(cmdlet.Select, requestSelect);
102+
Assert.Equal(cmdlet.Expand, requestExpand);
103+
}
104+
105+
[Fact]
106+
[Trait(Category.AcceptanceType, Category.CheckIn)]
107+
public void ListBatchJobSchedulesODataTest()
75108
{
76109
// Setup cmdlet to list job schedules using an OData filter
77110
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
78111
cmdlet.BatchContext = context;
79112
cmdlet.Id = null;
80113
cmdlet.Filter = "startswith(id,'test')";
114+
cmdlet.Select = "id,state";
115+
cmdlet.Expand = "stats";
81116

82-
string[] idsOfConstructedJobSchedules = new[] { "test1", "test2" };
117+
string requestFilter = null;
118+
string requestSelect = null;
119+
string requestExpand = null;
83120

84-
// Build some CloudJobSchedules instead of querying the service on a List CloudJobSchedules call
85-
CloudJobScheduleListResponse response = BatchTestHelpers.CreateCloudJobScheduleListResponse(idsOfConstructedJobSchedules);
86-
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobScheduleListParameters, CloudJobScheduleListResponse>(response);
87-
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
121+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
122+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobScheduleListParameters, CloudJobScheduleListResponse>();
123+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
124+
{
125+
requestFilter = request.Parameters.DetailLevel.FilterClause;
126+
requestSelect = request.Parameters.DetailLevel.SelectClause;
127+
requestExpand = request.Parameters.DetailLevel.ExpandClause;
88128

89-
// Setup the cmdlet to write pipeline output to a list that can be examined later
90-
List<PSCloudJobSchedule> pipeline = new List<PSCloudJobSchedule>();
91-
commandRuntimeMock.Setup(r =>
92-
r.WriteObject(It.IsAny<PSCloudJobSchedule>()))
93-
.Callback<object>(j => pipeline.Add((PSCloudJobSchedule)j));
129+
return Task.FromResult(response);
130+
});
131+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
94132

95133
cmdlet.ExecuteCmdlet();
96134

97-
// Verify that the cmdlet wrote the constructed job schedules to the pipeline
98-
Assert.Equal(2, pipeline.Count);
99-
int jobScheduleCount = 0;
100-
foreach (PSCloudJobSchedule j in pipeline)
101-
{
102-
Assert.True(idsOfConstructedJobSchedules.Contains(j.Id));
103-
jobScheduleCount++;
104-
}
105-
Assert.Equal(idsOfConstructedJobSchedules.Length, jobScheduleCount);
135+
Assert.Equal(cmdlet.Filter, requestFilter);
136+
Assert.Equal(cmdlet.Select, requestSelect);
137+
Assert.Equal(cmdlet.Expand, requestExpand);
106138
}
107139

108140
[Fact]

src/ResourceManager/AzureBatch/Commands.Batch.Test/Jobs/GetBatchJobCommandTests.cs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using System.Linq;
2323
using System.Management.Automation;
24+
using System.Threading.Tasks;
2425
using Xunit;
2526
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2627

@@ -71,39 +72,69 @@ public void GetBatchJobTest()
7172

7273
[Fact]
7374
[Trait(Category.AcceptanceType, Category.CheckIn)]
74-
public void ListBatchJobsByODataFilterTest()
75+
public void GetBatchJobODataTest()
7576
{
76-
// Setup cmdlet to list jobs using an OData filter. Use JobScheduleId input.
77+
// Setup cmdlet to get a single job
7778
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
7879
cmdlet.BatchContext = context;
79-
cmdlet.JobScheduleId = "jobSchedule";
80-
cmdlet.Id = null;
81-
cmdlet.Filter = "state -eq 'active'";
80+
cmdlet.Id = "testJob";
81+
cmdlet.Select = "id,state";
82+
cmdlet.Expand = "stats";
8283

83-
string[] idsOfConstructedJobs = new[] { "job-1", "job-2" };
84+
string requestSelect = null;
85+
string requestExpand = null;
8486

85-
// Build some CloudJobs instead of querying the service on a List CloudJobs call
86-
CloudJobListResponse response = BatchTestHelpers.CreateCloudJobListResponse(idsOfConstructedJobs);
87-
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobListParameters, CloudJobListResponse>(response);
88-
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
87+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
88+
CloudJobGetResponse getResponse = BatchTestHelpers.CreateCloudJobGetResponse(cmdlet.Id);
89+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobGetParameters, CloudJobGetResponse>(getResponse);
90+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
91+
{
92+
requestSelect = request.Parameters.DetailLevel.SelectClause;
93+
requestExpand = request.Parameters.DetailLevel.ExpandClause;
8994

90-
// Setup the cmdlet to write pipeline output to a list that can be examined later
91-
List<PSCloudJob> pipeline = new List<PSCloudJob>();
92-
commandRuntimeMock.Setup(r =>
93-
r.WriteObject(It.IsAny<PSCloudJob>()))
94-
.Callback<object>(j => pipeline.Add((PSCloudJob)j));
95+
return Task.FromResult(response);
96+
});
97+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
9598

9699
cmdlet.ExecuteCmdlet();
97100

98-
// Verify that the cmdlet wrote the constructed jobs to the pipeline
99-
Assert.Equal(2, pipeline.Count);
100-
int jobCount = 0;
101-
foreach (PSCloudJob j in pipeline)
101+
Assert.Equal(cmdlet.Select, requestSelect);
102+
Assert.Equal(cmdlet.Expand, requestExpand);
103+
}
104+
105+
[Fact]
106+
[Trait(Category.AcceptanceType, Category.CheckIn)]
107+
public void ListBatchJobsODataTest()
108+
{
109+
// Setup cmdlet to list job using an OData filter
110+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
111+
cmdlet.BatchContext = context;
112+
cmdlet.Id = null;
113+
cmdlet.Filter = "startswith(id,'test')";
114+
cmdlet.Select = "id,state";
115+
cmdlet.Expand = "stats";
116+
117+
string requestFilter = null;
118+
string requestSelect = null;
119+
string requestExpand = null;
120+
121+
// Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
122+
RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<CloudJobListParameters, CloudJobListResponse>();
123+
ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
102124
{
103-
Assert.True(idsOfConstructedJobs.Contains(j.Id));
104-
jobCount++;
105-
}
106-
Assert.Equal(idsOfConstructedJobs.Length, jobCount);
125+
requestFilter = request.Parameters.DetailLevel.FilterClause;
126+
requestSelect = request.Parameters.DetailLevel.SelectClause;
127+
requestExpand = request.Parameters.DetailLevel.ExpandClause;
128+
129+
return Task.FromResult(response);
130+
});
131+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
132+
133+
cmdlet.ExecuteCmdlet();
134+
135+
Assert.Equal(cmdlet.Filter, requestFilter);
136+
Assert.Equal(cmdlet.Select, requestSelect);
137+
Assert.Equal(cmdlet.Expand, requestExpand);
107138
}
108139

109140
[Fact]

0 commit comments

Comments
 (0)