Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: client/tests/kvm/deps/whql_submission_15.cs

Issue 6246035: Merge remote branch 'cros/upstream' into master (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/autotest.git@master
Patch Set: patch Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // DTM submission automation program 1 // DTM submission automation program
2 // Author: Michael Goldish <mgoldish@redhat.com> 2 // Author: Michael Goldish <mgoldish@redhat.com>
3 // Based on sample code by Microsoft. 3 // Based on sample code by Microsoft.
4 4
5 // Note: this program has only been tested with DTM version 1.5. 5 // Note: this program has only been tested with DTM version 1.5.
6 // It might fail to work with other versions, specifically because it uses 6 // It might fail to work with other versions, specifically because it uses
7 // a few undocumented methods/attributes. 7 // a few undocumented methods/attributes.
8 8
9 using System; 9 using System;
10 using System.Collections.Generic; 10 using System.Collections.Generic;
11 using System.Text.RegularExpressions; 11 using System.Text.RegularExpressions;
12 using Microsoft.DistributedAutomation.DeviceSelection; 12 using Microsoft.DistributedAutomation.DeviceSelection;
13 using Microsoft.DistributedAutomation.SqlDataStore; 13 using Microsoft.DistributedAutomation.SqlDataStore;
14 14
15 namespace automate0 15 namespace automate0
16 { 16 {
17 class AutoJob 17 class AutoJob
18 { 18 {
19 // Wait for a machine to show up in the data store
20 static void FindMachine(IResourcePool rootPool, string machineName)
21 {
22 Console.WriteLine("Looking for machine '{0}'", machineName);
23 IResource machine = null;
24 while (true)
25 {
26 try
27 {
28 machine = rootPool.GetResourceByName(machineName);
29 }
30 catch (Exception e)
31 {
32 Console.WriteLine("Warning: " + e.Message);
33 }
34 // Make sure the machine is valid
35 if (machine != null &&
36 machine.OperatingSystem != null &&
37 machine.OperatingSystem.Length > 0 &&
38 machine.ProcessorArchitecture != null &&
39 machine.ProcessorArchitecture.Length > 0 &&
40 machine.GetDevices().Length > 0)
41 break;
42 System.Threading.Thread.Sleep(1000);
43 }
44 Console.WriteLine("Client machine '{0}' found ({1}, {2})",
45 machineName, machine.OperatingSystem, machine.ProcessorArchitect ure);
46 }
47
48 // Delete a machine pool if it exists
49 static void DeleteResourcePool(IDeviceScript script, string poolName)
50 {
51 while (true)
52 {
53 try
54 {
55 IResourcePool pool = script.GetResourcePoolByName(poolName);
56 if (pool != null)
57 script.DeleteResourcePool(pool);
58 break;
59 }
60 catch (Exception e)
61 {
62 Console.WriteLine("Warning: " + e.Message);
63 System.Threading.Thread.Sleep(1000);
64 }
65 }
66 }
67
68 // Set the machine's status to 'Reset' and optionally wait for it to bec ome ready
69 static void ResetMachine(IResourcePool rootPool, string machineName, boo l wait)
70 {
71 Console.WriteLine("Resetting machine '{0}'", machineName);
72 IResource machine;
73 while (true)
74 {
75 try
76 {
77 machine = rootPool.GetResourceByName(machineName);
78 machine.ChangeResourceStatus("Reset");
79 break;
80 }
81 catch (Exception e)
82 {
83 Console.WriteLine("Warning: " + e.Message);
84 System.Threading.Thread.Sleep(5000);
85 }
86 }
87 if (wait)
88 {
89 Console.WriteLine("Waiting for machine '{0}' to be ready", machi neName);
90 while (machine.Status != "Ready")
91 {
92 try
93 {
94 machine = rootPool.GetResourceByName(machineName);
95 }
96 catch (Exception e)
97 {
98 Console.WriteLine("Warning: " + e.Message);
99 }
100 System.Threading.Thread.Sleep(1000);
101 }
102 Console.WriteLine("Machine '{0}' is ready", machineName);
103 }
104 }
105
106 // Look for a device in a machine, and if not found, keep trying for 3 m inutes
107 static IDevice GetDevice(IResourcePool rootPool, string machineName, str ing regexStr)
108 {
109 Regex deviceRegex = new Regex(regexStr, RegexOptions.IgnoreCase);
110 int numAttempts = 1;
111 DateTime endTime = DateTime.Now.AddSeconds(180);
112 while (DateTime.Now < endTime)
113 {
114 IResource machine = rootPool.GetResourceByName(machineName);
115 Console.WriteLine("Looking for device '{0}' in machine '{1}' (ma chine has {2} devices)",
116 regexStr, machineName, machine.GetDevices().Length);
117 foreach (IDevice d in machine.GetDevices())
118 {
119 if (deviceRegex.IsMatch(d.FriendlyName))
120 {
121 Console.WriteLine("Found device '{0}'", d.FriendlyName);
122 return d;
123 }
124 }
125 Console.WriteLine("Device not found");
126 if (numAttempts % 5 == 0)
127 ResetMachine(rootPool, machineName, true);
128 else
129 System.Threading.Thread.Sleep(5000);
130 numAttempts++;
131 }
132 Console.WriteLine("Error: device '{0}' not found", deviceRegex);
133 return null;
134 }
135
19 static int Main(string[] args) 136 static int Main(string[] args)
20 { 137 {
21 if (args.Length != 5) 138 if (args.Length < 5)
22 { 139 {
23 Console.WriteLine("Error: incorrect number of command line argum ents"); 140 Console.WriteLine("Error: incorrect number of command line argum ents");
24 Console.WriteLine("Usage: {0} serverName clientName machinePoolN ame submissionName timeout", 141 Console.WriteLine("Usage: {0} serverName machinePoolName submiss ionName timeout machineName0 machineName1 ...",
25 System.Environment.GetCommandLineArgs()[0]); 142 System.Environment.GetCommandLineArgs()[0]);
26 return 1; 143 return 1;
27 } 144 }
28 string serverName = args[0]; 145 string serverName = args[0];
29 string clientName = args[1]; 146 string machinePoolName = args[1];
30 string machinePoolName = args[2]; 147 string submissionName = args[2];
31 string submissionName = args[3]; 148 double timeout = Convert.ToDouble(args[3]);
32 double timeout = Convert.ToDouble(args[4]); 149
150 List<string> machines = new List<string>();
151 for (int i = 4; i < args.Length; i++)
152 machines.Add(args[i]);
33 153
34 try 154 try
35 { 155 {
36 // Initialize DeviceScript and connect to data store 156 // Initialize DeviceScript and connect to data store
37 Console.WriteLine("Initializing DeviceScript object"); 157 Console.WriteLine("Initializing DeviceScript object");
38 DeviceScript script = new DeviceScript(); 158 DeviceScript script = new DeviceScript();
39 Console.WriteLine("Connecting to data store"); 159 Console.WriteLine("Connecting to data store");
40
41 script.ConnectToNamedDataStore(serverName); 160 script.ConnectToNamedDataStore(serverName);
42 161
43 // Find client machine 162 // Wait for client machines to become available
44 IResourcePool rootPool = script.GetResourcePoolByName("$"); 163 IResourcePool rootPool = script.GetResourcePoolByName("$");
45 Console.WriteLine("Looking for client machine '{0}'", clientName ); 164 foreach (string machineName in machines)
46 IResource machine = null; 165 FindMachine(rootPool, machineName);
47 while (true)
48 {
49 try
50 {
51 machine = rootPool.GetResourceByName(clientName);
52 }
53 catch (Exception e)
54 {
55 Console.WriteLine("Warning: " + e.Message);
56 }
57 // Make sure the machine is valid
58 if (machine != null &&
59 machine.OperatingSystem != null &&
60 machine.OperatingSystem.Length > 0 &&
61 machine.ProcessorArchitecture != null &&
62 machine.ProcessorArchitecture.Length > 0 &&
63 machine.GetDevices().Length > 0)
64 break;
65 System.Threading.Thread.Sleep(1000);
66 }
67 Console.WriteLine("Client machine '{0}' found ({1}, {2})",
68 clientName, machine.OperatingSystem, machine.ProcessorArchit ecture);
69 166
70 // Create machine pool and add client machine to it 167 // Delete the machine pool if it already exists
168 DeleteResourcePool(script, machinePoolName);
169
170 // Create the machine pool and add the client machines to it
71 // (this must be done because jobs cannot be scheduled for machi nes in the 171 // (this must be done because jobs cannot be scheduled for machi nes in the
72 // default pool) 172 // default pool)
73 try 173 try
74 { 174 {
75 script.CreateResourcePool(machinePoolName, rootPool.Resource PoolId); 175 script.CreateResourcePool(machinePoolName, rootPool.Resource PoolId);
76 } 176 }
77 catch (Exception e) 177 catch (Exception e)
78 { 178 {
79 Console.WriteLine("Warning: " + e.Message); 179 Console.WriteLine("Warning: " + e.Message);
80 } 180 }
81 IResourcePool newPool = script.GetResourcePoolByName(machinePool Name); 181 IResourcePool newPool = script.GetResourcePoolByName(machinePool Name);
82 Console.WriteLine("Moving the client machine to pool '{0}'", mac hinePoolName); 182 foreach (string machineName in machines)
83 machine.ChangeResourcePool(newPool); 183 {
184 Console.WriteLine("Moving machine '{0}' to pool '{1}'", mach ineName, machinePoolName);
185 rootPool.GetResourceByName(machineName).ChangeResourcePool(n ewPool);
186 }
84 187
85 // Reset client machine 188 // Reset client machine
86 if (machine.Status != "Ready") 189 foreach (string machineName in machines)
87 { 190 ResetMachine(rootPool, machineName, true);
88 Console.WriteLine("Changing the client machine's status to ' Reset'");
89 while (true)
90 {
91 try
92 {
93 machine = rootPool.GetResourceByName(clientName);
94 machine.ChangeResourceStatus("Unsafe");
95 System.Threading.Thread.Sleep(5000);
96 machine.ChangeResourceStatus("Reset");
97 break;
98 }
99 catch (Exception e)
100 {
101 Console.WriteLine("Warning: " + e.Message);
102 }
103 System.Threading.Thread.Sleep(5000);
104 }
105 Console.WriteLine("Waiting for client machine to be ready");
106 while (machine.Status != "Ready")
107 {
108 try
109 {
110 machine = rootPool.GetResourceByName(clientName);
111 }
112 catch (Exception e)
113 {
114 Console.WriteLine("Warning: " + e.Message);
115 }
116 System.Threading.Thread.Sleep(1000);
117 }
118 }
119 Console.WriteLine("Client machine is ready");
120 191
121 // Get requested device regex and look for a matching device 192 // Get requested device regex and look for a matching device in the first machine
122 Console.WriteLine("Device to test: "); 193 Console.WriteLine("Device to test:");
123 Regex deviceRegex = new Regex(Console.ReadLine(), RegexOptions.I gnoreCase); 194 IDevice device = GetDevice(rootPool, machines[0], Console.ReadLi ne());
124 Console.WriteLine("Looking for device '{0}'", deviceRegex); 195 if (device == null)
125 IDevice device; 196 return 1;
126 DateTime endTime = DateTime.Now.AddSeconds(120);
127 while (DateTime.Now < endTime)
128 {
129 machine = rootPool.GetResourceByName(clientName);
130 Console.WriteLine("(Client machine has {0} devices)", machin e.GetDevices().Length);
131 foreach (IDevice d in machine.GetDevices())
132 {
133 if (deviceRegex.IsMatch(d.FriendlyName))
134 {
135 device = d;
136 goto deviceFound;
137 }
138 }
139 System.Threading.Thread.Sleep(5000);
140 }
141 Console.WriteLine("Error: device '{0}' not found", deviceRegex);
142 return 1;
143
144 deviceFound:
145 Console.WriteLine("Found device '{0}'", device.FriendlyName);
146 197
147 // Get requested jobs regex 198 // Get requested jobs regex
148 Console.WriteLine("Jobs to run: "); 199 Console.WriteLine("Jobs to run:");
149 Regex jobRegex = new Regex(Console.ReadLine(), RegexOptions.Igno reCase); 200 Regex jobRegex = new Regex(Console.ReadLine(), RegexOptions.Igno reCase);
150 201
151 // Create submission 202 // Create a submission
152 Object[] existingSubmissions = script.GetSubmissionByName(submis sionName); 203 Object[] existingSubmissions = script.GetSubmissionByName(submis sionName);
153 if (existingSubmissions.Length > 0) 204 if (existingSubmissions.Length > 0)
154 { 205 {
155 Console.WriteLine("Submission '{0}' already exists -- removi ng it", 206 Console.WriteLine("Submission '{0}' already exists -- removi ng it",
156 submissionName); 207 submissionName);
157 script.DeleteSubmission(((ISubmission)existingSubmissions[0] ).Id); 208 script.DeleteSubmission(((ISubmission)existingSubmissions[0] ).Id);
158 } 209 }
159 Console.WriteLine("Creating submission '{0}'", submissionName); 210 string hardwareId = device.InstanceId.Remove(device.InstanceId.L astIndexOf("\\"));
160 ISubmission submission = script.CreateHardwareSubmission(submiss ionName, 211 Console.WriteLine("Creating submission '{0}' (hardware ID: {1})" , submissionName, hardwareId);
161 newPool.ResourcePoolId, device.InstanceId); 212 ISubmission submission = script.CreateHardwareSubmission(submiss ionName, newPool.ResourcePoolId, hardwareId);
162 213
163 // Get DeviceData objects from the user 214 // Set submission DeviceData
164 List<Object> deviceDataList = new List<Object>(); 215 List<Object> deviceDataList = new List<Object>();
165 while (true) 216 while (true)
166 { 217 {
167 ISubmissionDeviceData dd = script.CreateNewSubmissionDeviceD ata(); 218 ISubmissionDeviceData dd = script.CreateNewSubmissionDeviceD ata();
168 Console.WriteLine("DeviceData name: "); 219 Console.WriteLine("DeviceData name:");
169 dd.Name = Console.ReadLine(); 220 dd.Name = Console.ReadLine();
170 if (dd.Name.Length == 0) 221 if (dd.Name.Length == 0)
171 break; 222 break;
172 Console.WriteLine("DeviceData data: "); 223 Console.WriteLine("DeviceData data:");
173 dd.Data = Console.ReadLine(); 224 dd.Data = Console.ReadLine();
174 deviceDataList.Add(dd); 225 deviceDataList.Add(dd);
175 } 226 }
176
177 // Set the submission's DeviceData
178 submission.SetDeviceData(deviceDataList.ToArray()); 227 submission.SetDeviceData(deviceDataList.ToArray());
179 228
180 // Get descriptors from the user 229 // Set submission descriptors
181 List<Object> descriptorList = new List<Object>(); 230 List<Object> descriptorList = new List<Object>();
182 while (true) 231 while (true)
183 { 232 {
184 Console.WriteLine("Descriptor path: "); 233 Console.WriteLine("Descriptor path:");
185 string descriptorPath = Console.ReadLine(); 234 string descriptorPath = Console.ReadLine();
186 if (descriptorPath.Length == 0) 235 if (descriptorPath.Length == 0)
187 break; 236 break;
188 descriptorList.Add(script.GetDescriptorByPath(descriptorPath )); 237 descriptorList.Add(script.GetDescriptorByPath(descriptorPath ));
189 } 238 }
190
191 // Set the submission's descriptors
192 submission.SetLogoDescriptors(descriptorList.ToArray()); 239 submission.SetLogoDescriptors(descriptorList.ToArray());
193 240
194 // Create a schedule 241 // Set machine dimensions
195 ISchedule schedule = script.CreateNewSchedule(); 242 foreach (string machineName in machines)
243 {
244 IResource machine = rootPool.GetResourceByName(machineName);
245 while (true)
246 {
247 Console.WriteLine("Dimension name ({0}):", machineName);
248 string dimName = Console.ReadLine();
249 if (dimName.Length == 0)
250 break;
251 Console.WriteLine("Dimension value ({0}):", machineName) ;
252 machine.SetDimension(dimName, Console.ReadLine());
253 }
254 // Set the WDKSubmissionId dimension for all machines
255 machine.SetDimension("WDKSubmissionId", submission.Id.ToStri ng() + "_" + submission.Name);
256 }
257
258 // Get job parameters
259 List<string> paramNames = new List<string>();
260 List<string> paramValues = new List<string>();
261 foreach (string machineName in machines)
262 {
263 while (true)
264 {
265 Console.WriteLine("Parameter name ({0}):", machineName);
266 string paramName = Console.ReadLine();
267 if (paramName.Length == 0)
268 break;
269 Console.WriteLine("Device regex ({0}):", machineName);
270 IDevice d = GetDevice(rootPool, machineName, Console.Rea dLine());
271 if (d == null)
272 return 1;
273 string deviceName = d.GetAttribute("name")[0].ToString() ;
274 Console.WriteLine("Setting parameter value to '{0}'", de viceName);
275 paramNames.Add(paramName);
276 paramValues.Add(deviceName);
277 }
278 }
279
280 // Find jobs that match the requested pattern
196 Console.WriteLine("Scheduling jobs:"); 281 Console.WriteLine("Scheduling jobs:");
197 int jobCount = 0; 282 List<IJob> jobs = new List<IJob>();
198 foreach (IJob j in submission.GetJobs()) 283 foreach (IJob j in submission.GetJobs())
199 { 284 {
200 if (jobRegex.IsMatch(j.Name)) 285 if (jobRegex.IsMatch(j.Name))
201 { 286 {
202 Console.WriteLine(" " + j.Name); 287 Console.WriteLine(" " + j.Name);
203 schedule.AddDeviceJob(device, j); 288 // Set job parameters
204 jobCount++; 289 for (int i = 0; i < paramNames.Count; i++)
290 {
291 IParameter p = j.GetParameterByName(paramNames[i]);
292 if (p != null)
293 p.ScheduleValue = paramValues[i];
294 }
295 jobs.Add(j);
205 } 296 }
206 } 297 }
207 if (jobCount == 0) 298 if (jobs.Count == 0)
208 { 299 {
209 Console.WriteLine("Error: no submission jobs match pattern ' {0}'", jobRegex); 300 Console.WriteLine("Error: no submission jobs match pattern ' {0}'", jobRegex);
210 return 1; 301 return 1;
211 } 302 }
303
304 // Create a schedule, add jobs to it and run it
305 ISchedule schedule = script.CreateNewSchedule();
306 foreach (IScheduleItem item in submission.ProcessJobs(jobs.ToArr ay()))
307 {
308 item.Device = device;
309 schedule.AddScheduleItem(item);
310 }
212 schedule.AddSubmission(submission); 311 schedule.AddSubmission(submission);
213 schedule.SetResourcePool(newPool); 312 schedule.SetResourcePool(newPool);
214 script.RunSchedule(schedule); 313 script.RunSchedule(schedule);
215 314
216 // Wait for jobs to complete 315 // Wait for jobs to complete
217 Console.WriteLine("Waiting for all jobs to complete (timeout={0} )", timeout); 316 Console.WriteLine("Waiting for all jobs to complete (timeout={0} s)", timeout);
218 endTime = DateTime.Now.AddSeconds(timeout); 317 DateTime endTime = DateTime.Now.AddSeconds(timeout);
219 int numCompleted = 0, numFailed = 0; 318 int numCompleted, numFailed;
220 while (numCompleted < submission.GetResults().Length && DateTime .Now < endTime) 319 do
221 { 320 {
222 // Sleep for 30 seconds
223 System.Threading.Thread.Sleep(30000); 321 System.Threading.Thread.Sleep(30000);
224 // Count completed submission jobs 322 // Report results in a Python readable format and count comp leted and failed schedule jobs
225 numCompleted = 0; 323 numCompleted = numFailed = 0;
226 foreach (IResult r in submission.GetResults())
227 if (r.ResultStatus != "InProgress")
228 numCompleted++;
229 // Report results in a Python readable format and count fail ed schedule jobs
230 // (submission jobs are a subset of schedule jobs)
231 Console.WriteLine(); 324 Console.WriteLine();
232 Console.WriteLine("---- ["); 325 Console.WriteLine("---- [");
233 numFailed = 0;
234 foreach (IResult r in schedule.GetResults()) 326 foreach (IResult r in schedule.GetResults())
235 { 327 {
328 if (r.ResultStatus != "InProgress") numCompleted++;
329 if (r.ResultStatus == "Investigate") numFailed++;
236 Console.WriteLine(" {"); 330 Console.WriteLine(" {");
237 Console.WriteLine(" 'id': {0}, 'job': r'''{1}''',", r .Job.Id, r.Job.Name); 331 Console.WriteLine(" 'id': {0}, 'job': r'''{1}''',", r .Job.Id, r.Job.Name);
238 Console.WriteLine(" 'logs': r'''{0}''',", r.LogLocati on); 332 Console.WriteLine(" 'logs': r'''{0}''',", r.LogLocati on);
239 if (r.ResultStatus != "InProgress") 333 if (r.ResultStatus != "InProgress")
240 Console.WriteLine(" 'report': r'''{0}''',", 334 Console.WriteLine(" 'report': r'''{0}''',",
241 submission.GetSubmissionResultReport(r)); 335 submission.GetSubmissionResultReport(r));
242 Console.WriteLine(" 'status': '{0}',", r.ResultStatus ); 336 Console.WriteLine(" 'status': '{0}',", r.ResultStatus );
243 Console.WriteLine(" 'pass': {0}, 'fail': {1}, 'notrun ': {2}, 'notapplicable': {3}", 337 Console.WriteLine(" 'pass': {0}, 'fail': {1}, 'notrun ': {2}, 'notapplicable': {3}",
244 r.Pass, r.Fail, r.NotRun, r.NotApplicable); 338 r.Pass, r.Fail, r.NotRun, r.NotApplicable);
245 Console.WriteLine(" },"); 339 Console.WriteLine(" },");
246 numFailed += r.Fail;
247 } 340 }
248 Console.WriteLine("] ----"); 341 Console.WriteLine("] ----");
249 } 342 } while (numCompleted < schedule.GetResults().Length && DateTime .Now < endTime);
343
250 Console.WriteLine(); 344 Console.WriteLine();
251 345
252 // Cancel incomplete jobs 346 // Cancel incomplete jobs
253 foreach (IResult r in schedule.GetResults()) 347 foreach (IResult r in schedule.GetResults())
254 if (r.ResultStatus == "InProgress") 348 if (r.ResultStatus == "InProgress")
255 r.Cancel(); 349 r.Cancel();
256 350
257 // Set the machine's status to Unsafe and then Reset 351 // Reset the machines
258 try 352 foreach (string machineName in machines)
259 { 353 ResetMachine(rootPool, machineName, false);
260 machine = rootPool.GetResourceByName(clientName);
261 machine.ChangeResourceStatus("Unsafe");
262 System.Threading.Thread.Sleep(5000);
263 machine.ChangeResourceStatus("Reset");
264 }
265 catch (Exception e)
266 {
267 Console.WriteLine("Warning: " + e.Message);
268 }
269 354
270 // Report failures 355 // Report failures
271 if (numCompleted < submission.GetResults().Length) 356 if (numCompleted < schedule.GetResults().Length)
272 Console.WriteLine("Some jobs did not complete on time."); 357 Console.WriteLine("Some jobs did not complete on time.");
273 if (numFailed > 0) 358 if (numFailed > 0)
274 Console.WriteLine("Some jobs failed."); 359 Console.WriteLine("Some jobs failed.");
275 360 if (numFailed > 0 || numCompleted < schedule.GetResults().Length )
276 if (numFailed > 0 || numCompleted < submission.GetResults().Leng th)
277 return 1; 361 return 1;
278 362
279 Console.WriteLine("All jobs completed."); 363 Console.WriteLine("All jobs completed.");
280 return 0; 364 return 0;
281 } 365 }
282 catch (Exception e) 366 catch (Exception e)
283 { 367 {
284 Console.WriteLine("Error: " + e.Message); 368 Console.WriteLine("Error: " + e.Message);
285 return 1; 369 return 1;
286 } 370 }
287 } 371 }
288 } 372 }
289 } 373 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698