Skip to content

Commit 01a1ed5

Browse files
author
jasper-schneider
committed
Update/record Batch cmdlet tests after API update
1 parent bf992bd commit 01a1ed5

File tree

132 files changed

+55455
-20518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+55455
-20518
lines changed

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

Lines changed: 99 additions & 162 deletions
Large diffs are not rendered by default.

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

Lines changed: 81 additions & 85 deletions
Large diffs are not rendered by default.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.Models;
19+
using Microsoft.Azure.Commands.Batch.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.ComputeNodeUsers
29+
{
30+
public class NewBatchComputeNodeUserCommandTests
31+
{
32+
private NewBatchComputeNodeUserCommand cmdlet;
33+
private Mock<BatchClient> batchClientMock;
34+
private Mock<ICommandRuntime> commandRuntimeMock;
35+
36+
public NewBatchComputeNodeUserCommandTests()
37+
{
38+
batchClientMock = new Mock<BatchClient>();
39+
commandRuntimeMock = new Mock<ICommandRuntime>();
40+
cmdlet = new NewBatchComputeNodeUserCommand()
41+
{
42+
CommandRuntime = commandRuntimeMock.Object,
43+
BatchClient = batchClientMock.Object,
44+
};
45+
}
46+
47+
[Fact]
48+
[Trait(Category.AcceptanceType, Category.CheckIn)]
49+
public void NewBatchComputeNodeUserParametersTest()
50+
{
51+
// Setup cmdlet without the required parameters
52+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
53+
cmdlet.BatchContext = context;
54+
55+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
56+
57+
cmdlet.PoolId = "testPool";
58+
cmdlet.ComputeNodeId = "computeNode1";
59+
60+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
61+
cmdlet.Name = "testUser";
62+
63+
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
64+
cmdlet.Password = "Password1234";
65+
66+
// Don't go to the service on an Add ComputeNodeUser call
67+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
68+
{
69+
BatchRequest<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse> request =
70+
(BatchRequest<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse>)baseRequest;
71+
72+
request.ServiceRequestFunc = () =>
73+
{
74+
ComputeNodeAddUserResponse response = new ComputeNodeAddUserResponse();
75+
Task<ComputeNodeAddUserResponse> task = Task.FromResult(response);
76+
return task;
77+
};
78+
});
79+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
80+
81+
// Verify no exceptions when required parameters are set
82+
cmdlet.ExecuteCmdlet();
83+
}
84+
}
85+
}

src/ResourceManager/Batch/Commands.Batch.Test/VMUsers/RemoveBatchVMUserCommandTests.cs renamed to src/ResourceManager/Batch/Commands.Batch.Test/ComputeNodeUsers/RemoveBatchComputeNodeUserCommandTests.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using System;
1616
using Microsoft.Azure.Batch;
1717
using Microsoft.Azure.Batch.Protocol;
18-
using Microsoft.Azure.Batch.Protocol.Entities;
18+
using Microsoft.Azure.Batch.Protocol.Models;
1919
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2020
using Moq;
2121
using System.Collections.Generic;
@@ -24,19 +24,19 @@
2424
using Xunit;
2525
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2626

27-
namespace Microsoft.Azure.Commands.Batch.Test.Users
27+
namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodeUsers
2828
{
29-
public class RemoveBatchVMUserCommandTests
29+
public class RemoveBatchComputeNodeUserCommandTests
3030
{
31-
private RemoveBatchVMUserCommand cmdlet;
31+
private RemoveBatchComputeNodeUserCommand cmdlet;
3232
private Mock<BatchClient> batchClientMock;
3333
private Mock<ICommandRuntime> commandRuntimeMock;
3434

35-
public RemoveBatchVMUserCommandTests()
35+
public RemoveBatchComputeNodeUserCommandTests()
3636
{
3737
batchClientMock = new Mock<BatchClient>();
3838
commandRuntimeMock = new Mock<ICommandRuntime>();
39-
cmdlet = new RemoveBatchVMUserCommand()
39+
cmdlet = new RemoveBatchComputeNodeUserCommand()
4040
{
4141
CommandRuntime = commandRuntimeMock.Object,
4242
BatchClient = batchClientMock.Object,
@@ -45,7 +45,7 @@ public RemoveBatchVMUserCommandTests()
4545

4646
[Fact]
4747
[Trait(Category.AcceptanceType, Category.CheckIn)]
48-
public void RemoveBatchUserParametersTest()
48+
public void RemoveBatchComputeNodeUserParametersTest()
4949
{
5050
// Setup cmdlet without the required parameters
5151
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
@@ -57,20 +57,22 @@ public void RemoveBatchUserParametersTest()
5757

5858
Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
5959

60-
cmdlet.PoolName = "testPool";
61-
cmdlet.VMName = "vm1";
60+
cmdlet.PoolId = "testPool";
61+
cmdlet.ComputeNodeId = "computeNode1";
6262
cmdlet.Name = "testUser";
6363

6464
// Don't go to the service on a DeleteTVMUser call
65-
YieldInjectionInterceptor interceptor = new YieldInjectionInterceptor((opContext, request) =>
65+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
6666
{
67-
if (request is DeleteTVMUserRequest)
67+
BatchRequest<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse> request =
68+
(BatchRequest<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse>)baseRequest;
69+
70+
request.ServiceRequestFunc = () =>
6871
{
69-
DeleteTVMUserResponse response = new DeleteTVMUserResponse();
70-
Task<object> task = Task<object>.Factory.StartNew(() => { return response; });
72+
ComputeNodeDeleteUserResponse response = new ComputeNodeDeleteUserResponse();
73+
Task<ComputeNodeDeleteUserResponse> task = Task.FromResult(response);
7174
return task;
72-
}
73-
return null;
75+
};
7476
});
7577
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
7678

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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.Batch.Protocol.Models;
18+
using Microsoft.Azure.Commands.Batch.Models;
19+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
20+
using Moq;
21+
using System.Collections.Generic;
22+
using System.Linq;
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.ComputeNodes
29+
{
30+
public class GetBatchComputeNodeCommandTests
31+
{
32+
private GetBatchComputeNodeCommand cmdlet;
33+
private Mock<BatchClient> batchClientMock;
34+
private Mock<ICommandRuntime> commandRuntimeMock;
35+
36+
public GetBatchComputeNodeCommandTests()
37+
{
38+
batchClientMock = new Mock<BatchClient>();
39+
commandRuntimeMock = new Mock<ICommandRuntime>();
40+
cmdlet = new GetBatchComputeNodeCommand()
41+
{
42+
CommandRuntime = commandRuntimeMock.Object,
43+
BatchClient = batchClientMock.Object,
44+
};
45+
}
46+
47+
[Fact]
48+
[Trait(Category.AcceptanceType, Category.CheckIn)]
49+
public void GetBatchComputeNodeTest()
50+
{
51+
// Setup cmdlet to get a compute node by id
52+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
53+
cmdlet.BatchContext = context;
54+
cmdlet.PoolId = "pool";
55+
cmdlet.Id = "computeNode1";
56+
cmdlet.Filter = null;
57+
58+
// Build a compute node instead of querying the service on a Get ComputeNode call
59+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
60+
{
61+
BatchRequest<ComputeNodeGetParameters, ComputeNodeGetResponse> request =
62+
(BatchRequest<ComputeNodeGetParameters, ComputeNodeGetResponse>)baseRequest;
63+
64+
request.ServiceRequestFunc = () =>
65+
{
66+
ComputeNodeGetResponse response = BatchTestHelpers.CreateComputeNodeGetResponse(cmdlet.Id);
67+
Task<ComputeNodeGetResponse> task = Task.FromResult(response);
68+
return task;
69+
};
70+
});
71+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
72+
73+
// Setup the cmdlet to write pipeline output to a list that can be examined later
74+
List<PSComputeNode> pipeline = new List<PSComputeNode>();
75+
commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSComputeNode>())).Callback<object>(c => pipeline.Add((PSComputeNode)c));
76+
77+
cmdlet.ExecuteCmdlet();
78+
79+
// Verify that the cmdlet wrote the compute node returned from the OM to the pipeline
80+
Assert.Equal(1, pipeline.Count);
81+
Assert.Equal(cmdlet.Id, pipeline[0].Id);
82+
}
83+
84+
[Fact]
85+
[Trait(Category.AcceptanceType, Category.CheckIn)]
86+
public void ListBatchComputeNodesByODataFilterTest()
87+
{
88+
// Setup cmdlet to list vms using an OData filter.
89+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
90+
cmdlet.BatchContext = context;
91+
cmdlet.PoolId = "pool";
92+
cmdlet.Id = null;
93+
cmdlet.Filter = "state -eq 'idle'";
94+
95+
string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2" };
96+
97+
// Build some compute nodes instead of querying the service on a List ComputeNodes call
98+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
99+
{
100+
BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request =
101+
(BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest;
102+
103+
request.ServiceRequestFunc = () =>
104+
{
105+
ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
106+
Task<ComputeNodeListResponse> task = Task.FromResult(response);
107+
return task;
108+
};
109+
});
110+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
111+
112+
// Setup the cmdlet to write pipeline output to a list that can be examined later
113+
List<PSComputeNode> pipeline = new List<PSComputeNode>();
114+
commandRuntimeMock.Setup(r =>
115+
r.WriteObject(It.IsAny<PSComputeNode>()))
116+
.Callback<object>(c => pipeline.Add((PSComputeNode)c));
117+
118+
cmdlet.ExecuteCmdlet();
119+
120+
// Verify that the cmdlet wrote the constructed compute nodes to the pipeline
121+
Assert.Equal(2, pipeline.Count);
122+
int computeNodeCount = 0;
123+
foreach (PSComputeNode c in pipeline)
124+
{
125+
Assert.True(idsOfConstructedComputeNodes.Contains(c.Id));
126+
computeNodeCount++;
127+
}
128+
Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount);
129+
}
130+
131+
[Fact]
132+
[Trait(Category.AcceptanceType, Category.CheckIn)]
133+
public void ListBatchComputeNodesWithoutFiltersTest()
134+
{
135+
// Setup cmdlet to list compute nodes without filters.
136+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
137+
cmdlet.BatchContext = context;
138+
cmdlet.PoolId = "testPool";
139+
cmdlet.Id = null;
140+
cmdlet.Filter = null;
141+
142+
string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" };
143+
144+
// Build some compute nodes instead of querying the service on a List ComputeNodes call
145+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
146+
{
147+
BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request =
148+
(BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest;
149+
150+
request.ServiceRequestFunc = () =>
151+
{
152+
ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
153+
Task<ComputeNodeListResponse> task = Task.FromResult(response);
154+
return task;
155+
};
156+
});
157+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
158+
159+
// Setup the cmdlet to write pipeline output to a list that can be examined later
160+
List<PSComputeNode> pipeline = new List<PSComputeNode>();
161+
commandRuntimeMock.Setup(r =>
162+
r.WriteObject(It.IsAny<PSComputeNode>()))
163+
.Callback<object>(c => pipeline.Add((PSComputeNode)c));
164+
165+
cmdlet.ExecuteCmdlet();
166+
167+
// Verify that the cmdlet wrote the constructed compute nodes to the pipeline
168+
Assert.Equal(3, pipeline.Count);
169+
int computeNodeCount = 0;
170+
foreach (PSComputeNode c in pipeline)
171+
{
172+
Assert.True(idsOfConstructedComputeNodes.Contains(c.Id));
173+
computeNodeCount++;
174+
}
175+
Assert.Equal(idsOfConstructedComputeNodes.Length, computeNodeCount);
176+
}
177+
178+
[Fact]
179+
[Trait(Category.AcceptanceType, Category.CheckIn)]
180+
public void ListComputeNodesMaxCountTest()
181+
{
182+
// Verify default max count
183+
Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
184+
185+
// Setup cmdlet to list compute nodes without filters and a max count
186+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
187+
cmdlet.BatchContext = context;
188+
cmdlet.PoolId = "testPool";
189+
cmdlet.Id = null;
190+
cmdlet.Filter = null;
191+
int maxCount = 2;
192+
cmdlet.MaxCount = maxCount;
193+
194+
string[] idsOfConstructedComputeNodes = new[] { "computeNode1", "computeNode2", "computeNode3" };
195+
196+
// Build some compute nodes instead of querying the service on a List ComputeNodes call
197+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
198+
{
199+
BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse> request =
200+
(BatchRequest<ComputeNodeListParameters, ComputeNodeListResponse>)baseRequest;
201+
202+
request.ServiceRequestFunc = () =>
203+
{
204+
ComputeNodeListResponse response = BatchTestHelpers.CreateComputeNodeListResponse(idsOfConstructedComputeNodes);
205+
Task<ComputeNodeListResponse> task = Task.FromResult(response);
206+
return task;
207+
};
208+
});
209+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
210+
211+
// Setup the cmdlet to write pipeline output to a list that can be examined later
212+
List<PSComputeNode> pipeline = new List<PSComputeNode>();
213+
commandRuntimeMock.Setup(r =>
214+
r.WriteObject(It.IsAny<PSComputeNode>()))
215+
.Callback<object>(c => pipeline.Add((PSComputeNode)c));
216+
217+
cmdlet.ExecuteCmdlet();
218+
219+
// Verify that the max count was respected
220+
Assert.Equal(maxCount, pipeline.Count);
221+
222+
// Verify setting max count <= 0 doesn't return nothing
223+
cmdlet.MaxCount = -5;
224+
pipeline.Clear();
225+
cmdlet.ExecuteCmdlet();
226+
227+
Assert.Equal(idsOfConstructedComputeNodes.Length, pipeline.Count);
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)