This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRegTester.cs
397 lines (344 loc) · 14.9 KB
/
RegTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* PROJECT: ReactOS System Regression Testing Utility, Windows/VirtualBox version
* LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
* PURPOSE: Processing the incoming debugging data
* COPYRIGHT: Copyright Aleksey Bragin <[email protected]>. Based around ideas and code from
* sysreg created by Christoph von Wittich <[email protected]> and
* Colin Finck <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.IO;
using VirtualBox;
using System.Runtime.InteropServices;
namespace sysreg3
{
class RegTester
{
const string machineName = "ReactOS Testbot";
const string diskFileName = "ReactOS Testbot.vdi";
public int maxRetries = 30;
public static int maxCacheHits = 1000;
const int numStages = 3;
public int vmTimeout = 60 * 1000; // 60 secs
const Int64 hddSize = (Int64)2 * 1024 * 1024 * 1024;
const string namedPipeName = @"reactos\testbot";
const string defaultLogName = "testbot.txt";
readonly string vmBaseFolder; // Directory where VM will be created if it doesn't exist yet
public string logName;
IMachine rosVM;
IVirtualBox vBox;
public RegTester()
{
/* Create VBox instance */
vBox = new VirtualBox.VirtualBox();
vmBaseFolder = Path.Combine(Environment.CurrentDirectory, "vm");
logName = defaultLogName;
}
public string debugLogFilename
{
get
{
if (logName != null)
return Path.Combine(Environment.CurrentDirectory, logName);
else
return null;
}
}
private ContinueType ProcessDebugOutput(ISession vmSession, int stage)
{
ContinueType Result = ContinueType.EXIT_DONT_CONTINUE;
bool noTimeout;
LogReader logReader = new LogReader(namedPipeName, debugLogFilename, vmSession, stage, vmTimeout);
/* Start the logger thread */
Thread t = new Thread(new ThreadStart(logReader.Run));
t.Start();
/* Wait until it terminates or exits with a timeout */
logReader.TimeOutEvent.WaitOne();
/* Get the result */
Result = logReader.Result;
if (logReader.TimedOut)
{
/* We hit the timeout, quit */
Console.WriteLine("[SYSREG] timeout");
Result = ContinueType.EXIT_CONTINUE;
/* Force thread termination */
t.Abort();
}
return Result;
}
private void CreateHardDisk(Session vmSession, IStorageController controller, int dev, int port)
{
IMedium rosHdd;
IProgress progress;
string curDir = Path.GetFullPath(Environment.CurrentDirectory);
/* Create the hdd and storage */
rosHdd = vBox.CreateHardDisk(null, Path.Combine(curDir, diskFileName));
progress = rosHdd.CreateBaseStorage(hddSize, new MediumVariant[] { MediumVariant.MediumVariant_Standard });
progress.WaitForCompletion(-1);
//String errStr;
//progress.ErrorInfo.GetDescription(out errStr);
//Console.WriteLine(errStr);
/* FIXME: Make sure there is no hdd with the same name hanging around in registered condition */
/* Attach it to the vm */
vmSession.Machine.SaveSettings();
vmSession.Machine.AttachDevice(controller.Name, port, dev, DeviceType.DeviceType_HardDisk, rosHdd);
vmSession.Machine.SaveSettings();
}
private void EmptyHardDisk(Session vmSession)
{
IProgress progress;
IStorageController controller = null;
uint inst;
IMedium rosHdd = null;
int dev = 0, port;
Boolean HddFound = false;
/* Go through storage controllers to find IDE/SATA one */
for (inst = 0; inst < 4; inst++)
{
try
{
controller = rosVM.GetStorageControllerByInstance(inst);
if (controller.Bus == StorageBus.StorageBus_IDE ||
controller.Bus == StorageBus.StorageBus_SATA)
break;
}
catch (Exception exc)
{
/* Just skip it */
}
}
/* Now check what HDD we have connected to this controller */
for (port = 0; port < controller.MaxPortCount; port++)
{
for (dev = 0; dev < controller.MaxDevicesPerPortCount; dev++)
{
try
{
rosHdd = rosVM.GetMedium(controller.Name, port, dev);
if (rosHdd.DeviceType == DeviceType.DeviceType_HardDisk)
{
/* We found the one and only harddisk */
HddFound = true;
break;
}
rosHdd.Close();
}
catch (Exception exc)
{
/* Just skip it */
}
}
if (HddFound) break;
}
/* Delete the existing hdd */
if (HddFound)
{
try
{
controller = rosVM.GetStorageControllerByInstance(inst);
vmSession.Machine.DetachDevice(controller.Name, port, dev);
vmSession.Machine.SaveSettings();
progress = rosHdd.DeleteStorage();
progress.WaitForCompletion(-1);
rosHdd.Close();
}
catch (Exception exc)
{
Console.WriteLine("Could not delete existing HDD:" + exc);
}
}
else
{
/* Connect to port 0, device 0 if there was no hdd found */
port = 0;
dev = 0;
}
/* Create a new one */
CreateHardDisk(vmSession, controller, port, dev);
}
private void EmptyDebugLog()
{
try
{
FileStream dbgFile = File.Open(debugLogFilename, FileMode.Truncate);
dbgFile.Close();
}
catch
{
/* Don't care about the exceptions here */
}
}
private void ConfigVm(Session vmSession)
{
/* Check serial port */
ISerialPort dbgPort = vmSession.Machine.GetSerialPort(0);
// Path must be set BEFORE setting HostMode!
dbgPort.Path = @"\\.\pipe\" + namedPipeName; // Name must always have this special prefix
dbgPort.HostMode = PortMode.PortMode_HostPipe;
dbgPort.Server = 1;
dbgPort.Enabled = 1;
}
private IMachine CreateVm()
{
IMachine vm = null;
IStorageController hddController;
try
{
Console.WriteLine("[SYSREG] creating VM");
// For allowed OS type values, query IVirtualBox.GuestOSTypes and look for "id" field
vm = vBox.CreateMachine(Path.Combine(vmBaseFolder, machineName + ".vbox"), machineName, null, "Windows2003", "");
hddController = vm.AddStorageController("sata0", StorageBus.StorageBus_SATA);
hddController.PortCount = 2;
vm.MemorySize = 256; // In MB
vm.VRAMSize = 16; // In MB
vm.SaveSettings();
Console.WriteLine("[SYSREG] registering VM");
vBox.RegisterMachine(vm);
}
catch (Exception exc)
{
// Creation failed.
Console.WriteLine("Creating the VM failed: " + exc);
}
return vm;
}
public void RunTests()
{
ContinueType ret = ContinueType.EXIT_DONT_CONTINUE;
IProgress vmProgress;
// TODO: Load settings
/* Open the testing machine */
Session vmSession = new Session();
try
{
rosVM = vBox.FindMachine(machineName);
}
catch (COMException exc)
{
/* Opening failed. Probably we need to create it */
Console.WriteLine("Opening the vm failed: " + exc);
rosVM = CreateVm();
}
rosVM.LockMachine(vmSession, LockType.LockType_Write);
/* Configure the virtual machine */
ConfigVm(vmSession);
/* Empty or create the HDD, prepare for the first run */
EmptyHardDisk(vmSession);
/* Close VM session */
vmSession.UnlockMachine();
/* Empty the debug log file */
if(logName != null)
EmptyDebugLog();
/* Start main testing loop */
for (int stage = 0; stage < numStages; stage++)
{
int retries;
for (retries = 0; retries < maxRetries; retries++)
{
/* Start the VM */
try
{
vmProgress = rosVM.LaunchVMProcess(vmSession, "gui", null);
vmProgress.WaitForCompletion(-1);
if (vmProgress.ResultCode != 0)
{
/* Print out error's text */
Console.WriteLine("Error starting VM: " + vmProgress.ErrorInfo.Text);
/* Close VM session */
if(vmSession.State == SessionState.SessionState_Locked)
vmSession.UnlockMachine();
break;
}
Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
Console.WriteLine("[SYSREG] Running stage {0}...", stage + 1);
Console.WriteLine("[SYSREG] Domain {0} started.\n", rosVM.Name);
ret = ProcessDebugOutput(vmSession, stage);
/* Kill the VM if it's not already powered off */
if (vmSession.State != SessionState.SessionState_Unlocked
&& rosVM.State >= MachineState.MachineState_FirstOnline
&& rosVM.State <= MachineState.MachineState_LastOnline)
{
#if TRACE
Console.WriteLine("[SYSREG] Killing VM (state " + rosVM.State.ToString()+")");
#endif
try
{
vmProgress = vmSession.Console.PowerDown();
vmProgress.WaitForCompletion(-1);
}
catch (System.Runtime.InteropServices.COMException comEx)
{
Console.WriteLine("[SYSREG] Failed to shutdown VM: " + comEx.ToString());
if (rosVM.State != MachineState.MachineState_PoweredOff)
throw;
}
}
try
{
/* Close the VM session without paying attention to any problems */
if (vmSession.State == SessionState.SessionState_Locked)
{
vmSession.UnlockMachine();
/* Wait till the machine state is actually closed (no vmProgress alas) */
int waitingTimeout = 0;
while (vmSession.State != SessionState.SessionState_Unlocked ||
waitingTimeout < 5)
{
Thread.Sleep(1000);
waitingTimeout++;
}
}
}
catch
{
}
/* If we have a checkpoint to reach for success, assume that
the application used for running the tests (probably "rosautotest")
continues with the next test after a VM restart. */
if (stage == 2 && ret == ContinueType.EXIT_CONTINUE)
Console.WriteLine("[SYSREG] Rebooting VM (retry {0})", retries + 1);
else
{
/* Empty the debug log file */
if(logName != null)
EmptyDebugLog();
break;
}
}
catch (Exception exc)
{
Console.WriteLine("[SYSREG] Running the VM failed with exception: " + exc);
//break;
}
}
/* Check for a maximum number of retries */
if (retries >= maxRetries)
{
Console.WriteLine("[SYSREG] Maximum number of allowed retries exceeded, aborting!");
break;
}
/* Stop executing if asked so */
if (ret == ContinueType.EXIT_DONT_CONTINUE) break;
}
switch (ret)
{
case ContinueType.EXIT_CHECKPOINT_REACHED:
Console.WriteLine("[SYSREG] Status: Reached the checkpoint!");
Environment.ExitCode = 0;
break;
case ContinueType.EXIT_CONTINUE:
Console.WriteLine("[SYSREG] Status: Failed to reach the checkpoint!!");
break;
case ContinueType.EXIT_DONT_CONTINUE:
Console.WriteLine("[SYSREG] Status: Testing process aborted!");
break;
}
}
}
}