Skip to content

Commit 3a6dc7e

Browse files
committed
Merge pull request #194 from jasper-schneider/gets
Batch cmdlets
2 parents 305df46 + 46f09ae commit 3a6dc7e

File tree

110 files changed

+35234
-62
lines changed

Some content is hidden

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

110 files changed

+35234
-62
lines changed

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/GetBatchAccountCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.Generic;
1919
using System.Management.Automation;
2020
using Xunit;
21+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2122

2223
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2324
{

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/GetBatchAccountKeysCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.Generic;
1919
using System.Management.Automation;
2020
using Xunit;
21+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2122

2223
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2324
{

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/NewBatchAccountCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Collections.Generic;
2020
using System.Management.Automation;
2121
using Xunit;
22+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2223

2324
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2425
{

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/RegenBatchAccountKeyCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.Generic;
1919
using System.Management.Automation;
2020
using Xunit;
21+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2122

2223
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2324
{

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/RemoveBatchAccountCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Moq;
1818
using System.Management.Automation;
1919
using Xunit;
20+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2021

2122
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2223
{

src/ResourceManager/Batch/Commands.Batch.Test/Accounts/SetBatchAccountCommandTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Collections.Generic;
2020
using System.Management.Automation;
2121
using Xunit;
22+
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2223

2324
namespace Microsoft.Azure.Commands.Batch.Test.Accounts
2425
{

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

Lines changed: 230 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,46 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System.IO;
16+
using System.Linq;
17+
using System.Net;
18+
using System.Net.Http;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Batch;
21+
using Microsoft.Azure.Batch.Common;
22+
using Microsoft.Azure.Batch.Protocol;
23+
using Microsoft.Azure.Batch.Protocol.Entities;
24+
using Microsoft.Azure.Commands.Batch.Models;
25+
using Microsoft.Azure.Commands.Batch.Test.ScenarioTests;
26+
using Microsoft.Azure.Management.Batch;
1527
using Microsoft.Azure.Management.Batch.Models;
28+
using System;
1629
using System.Collections;
1730
using System.Collections.Generic;
31+
using System.Reflection;
32+
using Microsoft.Azure.Management.Resources;
33+
using Microsoft.Azure.Management.Resources.Models;
34+
using Microsoft.Azure.Test.HttpRecorder;
35+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
36+
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
1837
using Xunit;
38+
using JobExecutionEnvironment = Microsoft.Azure.Batch.Protocol.Entities.JobExecutionEnvironment;
1939

2040
namespace Microsoft.Azure.Commands.Batch.Test
2141
{
42+
/// <summary>
43+
/// Helper methods for the Batch cmdlet tests
44+
/// </summary>
2245
public static class BatchTestHelpers
2346
{
47+
/// <summary>
48+
/// Builds an AccountResource object using the specified parameters
49+
/// </summary>
2450
public static AccountResource CreateAccountResource(string accountName, string resourceGroupName, Hashtable[] tags = null)
2551
{
2652
string tenantUrlEnding = "batch-test.windows-int.net";
2753
string endpoint = string.Format("{0}.{1}", accountName, tenantUrlEnding);
28-
string subscription = "00000000-0000-0000-0000-000000000000";
54+
string subscription = Guid.Empty.ToString();
2955
string resourceGroup = resourceGroupName;
3056

3157
AccountResource resource = new AccountResource()
@@ -39,9 +65,27 @@ public static AccountResource CreateAccountResource(string accountName, string r
3965
{
4066
resource.Tags = Microsoft.Azure.Commands.Batch.Helpers.CreateTagDictionary(tags, true);
4167
}
68+
4269
return resource;
4370
}
4471

72+
/// <summary>
73+
/// Builds a BatchAccountContext object with the keys set for testing
74+
/// </summary>
75+
public static BatchAccountContext CreateBatchContextWithKeys()
76+
{
77+
AccountResource resource = CreateAccountResource("account", "resourceGroup");
78+
BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource);
79+
string dummyKey = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
80+
SetProperty(context, "PrimaryAccountKey", dummyKey);
81+
SetProperty(context, "SecondaryAccountKey", dummyKey);
82+
83+
return context;
84+
}
85+
86+
/// <summary>
87+
/// Verifies that two BatchAccountContext objects are equal
88+
/// </summary>
4589
public static void AssertBatchAccountContextsAreEqual(BatchAccountContext context1, BatchAccountContext context2)
4690
{
4791
if (context1 == null)
@@ -67,5 +111,190 @@ public static void AssertBatchAccountContextsAreEqual(BatchAccountContext contex
67111
Assert.Equal<string>(context1.TagsTable, context2.TagsTable);
68112
Assert.Equal<string>(context1.TaskTenantUrl, context2.TaskTenantUrl);
69113
}
114+
115+
/// <summary>
116+
/// Builds a PSCloudWorkItem for testing
117+
/// </summary>
118+
public static PSCloudWorkItem CreatePSCloudWorkItem()
119+
{
120+
BatchAccountContext context = CreateBatchContextWithKeys();
121+
using (IWorkItemManager wiManager = context.BatchOMClient.OpenWorkItemManager())
122+
{
123+
ICloudWorkItem workItem = wiManager.CreateWorkItem("testWorkItem");
124+
return new PSCloudWorkItem(workItem);
125+
}
126+
}
127+
128+
/// <summary>
129+
/// Builds a GetPoolResponse object
130+
/// </summary>
131+
public static GetPoolResponse CreateGetPoolResponse(string poolName)
132+
{
133+
GetPoolResponse response = new GetPoolResponse();
134+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
135+
136+
Pool pool = new Pool();
137+
SetProperty(pool, "Name", poolName);
138+
139+
SetProperty(response, "Pool", pool);
140+
141+
return response;
142+
}
143+
144+
/// <summary>
145+
/// Builds a ListPoolsResponse object
146+
/// </summary>
147+
public static ListPoolsResponse CreateListPoolsResponse(IEnumerable<string> poolNames)
148+
{
149+
ListPoolsResponse response = new ListPoolsResponse();
150+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
151+
152+
List<Pool> pools = new List<Pool>();
153+
154+
foreach (string name in poolNames)
155+
{
156+
Pool pool = new Pool();
157+
SetProperty(pool, "Name", name);
158+
pools.Add(pool);
159+
}
160+
161+
SetProperty(response, "Pools", pools);
162+
163+
return response;
164+
}
165+
166+
/// <summary>
167+
/// Builds a GetWorkItemResponse object
168+
/// </summary>
169+
public static GetWorkItemResponse CreateGetWorkItemResponse(string workItemName)
170+
{
171+
GetWorkItemResponse response = new GetWorkItemResponse();
172+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
173+
174+
JobExecutionEnvironment jee = new JobExecutionEnvironment();
175+
176+
WorkItem workItem = new WorkItem(workItemName, jee);
177+
SetProperty(response, "WorkItem", workItem);
178+
179+
return response;
180+
}
181+
182+
/// <summary>
183+
/// Builds a ListWorkItemsResponse object
184+
/// </summary>
185+
public static ListWorkItemsResponse CreateListWorkItemsResponse(IEnumerable<string> workItemNames)
186+
{
187+
ListWorkItemsResponse response = new ListWorkItemsResponse();
188+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
189+
190+
List<WorkItem> workItems = new List<WorkItem>();
191+
JobExecutionEnvironment jee = new JobExecutionEnvironment();
192+
193+
foreach (string name in workItemNames)
194+
{
195+
workItems.Add(new WorkItem(name, jee));
196+
}
197+
198+
SetProperty(response, "WorkItems", workItems);
199+
200+
return response;
201+
}
202+
203+
/// <summary>
204+
/// Builds a GetJobResponse object
205+
/// </summary>
206+
public static GetJobResponse CreateGetJobResponse(string jobName)
207+
{
208+
GetJobResponse response = new GetJobResponse();
209+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
210+
211+
Job job = new Job();
212+
SetProperty(job, "Name", jobName);
213+
214+
SetProperty(response, "Job", job);
215+
216+
return response;
217+
}
218+
219+
/// <summary>
220+
/// Builds a ListJobsResponse object
221+
/// </summary>
222+
public static ListJobsResponse CreateListJobsResponse(IEnumerable<string> jobNames)
223+
{
224+
ListJobsResponse response = new ListJobsResponse();
225+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
226+
227+
List<Job> jobs = new List<Job>();
228+
229+
foreach (string name in jobNames)
230+
{
231+
Job job = new Job();
232+
SetProperty(job, "Name", name);
233+
jobs.Add(job);
234+
}
235+
236+
SetProperty(response, "Jobs", jobs);
237+
238+
return response;
239+
}
240+
241+
/// <summary>
242+
/// Builds a GetTaskResponse object
243+
/// </summary>
244+
public static GetTaskResponse CreateGetTaskResponse(string taskName)
245+
{
246+
GetTaskResponse response = new GetTaskResponse();
247+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
248+
249+
Azure.Batch.Protocol.Entities.Task task = new Azure.Batch.Protocol.Entities.Task();
250+
SetProperty(task, "Name", taskName);
251+
252+
SetProperty(response, "Task", task);
253+
254+
return response;
255+
}
256+
257+
/// <summary>
258+
/// Builds a ListTasksResponse object
259+
/// </summary>
260+
public static ListTasksResponse CreateListTasksResponse(IEnumerable<string> taskNames)
261+
{
262+
ListTasksResponse response = new ListTasksResponse();
263+
SetProperty(response, "StatusCode", HttpStatusCode.OK);
264+
265+
List<Azure.Batch.Protocol.Entities.Task> tasks = new List<Azure.Batch.Protocol.Entities.Task>();
266+
267+
foreach (string name in taskNames)
268+
{
269+
Azure.Batch.Protocol.Entities.Task task = new Azure.Batch.Protocol.Entities.Task();
270+
SetProperty(task, "Name", name);
271+
tasks.Add(task);
272+
}
273+
274+
SetProperty(response, "Tasks", tasks);
275+
276+
return response;
277+
}
278+
279+
280+
/// <summary>
281+
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.
282+
/// </summary>
283+
internal static void SetProperty(object obj, string propertyName, object propertyValue)
284+
{
285+
Type t = obj.GetType();
286+
PropertyInfo propInfo = t.GetProperty(propertyName);
287+
propInfo.SetValue(obj, propertyValue);
288+
}
289+
290+
/// <summary>
291+
/// Uses Reflection to set a property value on an object.
292+
/// </summary>
293+
internal static void SetField(object obj, string fieldName, object fieldValue)
294+
{
295+
Type t = obj.GetType();
296+
FieldInfo fieldInfo = t.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
297+
fieldInfo.SetValue(obj, fieldValue);
298+
}
70299
}
71300
}

0 commit comments

Comments
 (0)