12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
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 ;
15
27
using Microsoft . Azure . Management . Batch . Models ;
28
+ using System ;
16
29
using System . Collections ;
17
30
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 ;
18
37
using Xunit ;
38
+ using JobExecutionEnvironment = Microsoft . Azure . Batch . Protocol . Entities . JobExecutionEnvironment ;
19
39
20
40
namespace Microsoft . Azure . Commands . Batch . Test
21
41
{
42
+ /// <summary>
43
+ /// Helper methods for the Batch cmdlet tests
44
+ /// </summary>
22
45
public static class BatchTestHelpers
23
46
{
47
+ /// <summary>
48
+ /// Builds an AccountResource object using the specified parameters
49
+ /// </summary>
24
50
public static AccountResource CreateAccountResource ( string accountName , string resourceGroupName , Hashtable [ ] tags = null )
25
51
{
26
52
string tenantUrlEnding = "batch-test.windows-int.net" ;
27
53
string endpoint = string . Format ( "{0}.{1}" , accountName , tenantUrlEnding ) ;
28
- string subscription = "00000000-0000-0000-0000-000000000000" ;
54
+ string subscription = Guid . Empty . ToString ( ) ;
29
55
string resourceGroup = resourceGroupName ;
30
56
31
57
AccountResource resource = new AccountResource ( )
@@ -39,9 +65,27 @@ public static AccountResource CreateAccountResource(string accountName, string r
39
65
{
40
66
resource . Tags = Microsoft . Azure . Commands . Batch . Helpers . CreateTagDictionary ( tags , true ) ;
41
67
}
68
+
42
69
return resource ;
43
70
}
44
71
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>
45
89
public static void AssertBatchAccountContextsAreEqual ( BatchAccountContext context1 , BatchAccountContext context2 )
46
90
{
47
91
if ( context1 == null )
@@ -67,5 +111,190 @@ public static void AssertBatchAccountContextsAreEqual(BatchAccountContext contex
67
111
Assert . Equal < string > ( context1 . TagsTable , context2 . TagsTable ) ;
68
112
Assert . Equal < string > ( context1 . TaskTenantUrl , context2 . TaskTenantUrl ) ;
69
113
}
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
+ }
70
299
}
71
300
}
0 commit comments