VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testdriver/vboxwrappers.py@ 54463

Last change on this file since 54463 was 54432, checked in by vboxsync, 10 years ago

ValidationKit: use new IVirtualBox.createMedium API method which replaces createHardDisk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 103.6 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: vboxwrappers.py 54432 2015-02-24 10:46:34Z vboxsync $
3# pylint: disable=C0302
4
5"""
6VirtualBox Wrapper Classes
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2015 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 54432 $"
31
32
33# Standard Python imports.
34import array
35import os
36import socket
37import sys
38
39# Validation Kit imports.
40from common import utils;
41from testdriver import base;
42from testdriver import reporter;
43from testdriver import txsclient;
44from testdriver import vboxcon;
45from testdriver import vbox;
46from testdriver.base import TdTaskBase;
47
48
49def _ControllerNameToBus(sController):
50 """ Translate a controller name to a storage bus. """
51 if sController == "IDE Controller":
52 iType = vboxcon.StorageBus_IDE;
53 elif sController == "SATA Controller":
54 iType = vboxcon.StorageBus_SATA;
55 elif sController == "Floppy Controller":
56 iType = vboxcon.StorageBus_Floppy;
57 elif sController == "SAS Controller":
58 iType = vboxcon.StorageBus_SAS;
59 elif sController == "SCSI Controller":
60 iType = vboxcon.StorageBus_SCSI;
61 else:
62 iType = vboxcon.StorageBus_Null;
63 return iType;
64
65def _nameMachineState(eState):
66 """ Gets the name (string) of a machine state."""
67 if eState == vboxcon.MachineState_PoweredOff: return 'PoweredOff';
68 if eState == vboxcon.MachineState_Saved: return 'Saved';
69 if eState == vboxcon.MachineState_Teleported: return 'Teleported';
70 if eState == vboxcon.MachineState_Aborted: return 'Aborted';
71 if eState == vboxcon.MachineState_Running: return 'Running';
72 if eState == vboxcon.MachineState_Paused: return 'Paused';
73 if eState == vboxcon.MachineState_Stuck: return 'GuruMeditation';
74 if eState == vboxcon.MachineState_Teleporting: return 'Teleporting';
75 if eState == vboxcon.MachineState_LiveSnapshotting: return 'LiveSnapshotting';
76 if eState == vboxcon.MachineState_Starting: return 'Starting';
77 if eState == vboxcon.MachineState_Stopping: return 'Stopping';
78 if eState == vboxcon.MachineState_Saving: return 'Saving';
79 if eState == vboxcon.MachineState_Restoring: return 'Restoring';
80 if eState == vboxcon.MachineState_TeleportingPausedVM: return 'TeleportingPausedVM';
81 if eState == vboxcon.MachineState_TeleportingIn: return 'TeleportingIn';
82 if eState == vboxcon.MachineState_FaultTolerantSyncing: return 'FaultTolerantSyncing';
83 if eState == vboxcon.MachineState_DeletingSnapshotOnline: return 'DeletingSnapshotOnline';
84 if eState == vboxcon.MachineState_DeletingSnapshotPaused: return 'DeletingSnapshotPaused';
85 if eState == vboxcon.MachineState_RestoringSnapshot: return 'RestoringSnapshot';
86 if eState == vboxcon.MachineState_DeletingSnapshot: return 'DeletingSnapshot';
87 if eState == vboxcon.MachineState_SettingUp: return 'SettingUp';
88 return 'Unknown-%s' % (eState,);
89
90
91class VirtualBoxWrapper(object): # pylint: disable=R0903
92 """
93 Wrapper around the IVirtualBox object that adds some (hopefully) useful
94 utility methods
95
96 The real object can be accessed thru the o member. That said, members can
97 be accessed directly as well.
98 """
99
100 def __init__(self, oVBox, oVBoxMgr, fpApiVer, oTstDrv):
101 self.o = oVBox;
102 self.oVBoxMgr = oVBoxMgr;
103 self.fpApiVer = fpApiVer;
104 self.oTstDrv = oTstDrv;
105
106 def __getattr__(self, sName):
107 # Try ourselves first.
108 try:
109 oAttr = self.__dict__[sName];
110 except:
111 #try:
112 # oAttr = dir(self)[sName];
113 #except AttributeError:
114 oAttr = getattr(self.o, sName);
115 return oAttr;
116
117 #
118 # Utilities.
119 #
120
121 def registerDerivedEventHandler(self, oSubClass, dArgs = None):
122 """
123 Create an instance of the given VirtualBoxEventHandlerBase sub-class
124 and register it.
125
126 The new instance is returned on success. None is returned on error.
127 """
128 dArgsCopy = dArgs.copy() if dArgs is not None else dict();
129 dArgsCopy['oVBox'] = self;
130 return oSubClass.registerDerivedEventHandler(self.oVBoxMgr, self.fpApiVer, oSubClass, dArgsCopy,
131 self.o, 'IVirtualBox', 'IVirtualBoxCallback');
132
133 def deleteHdByLocation(self, sHdLocation):
134 """
135 Deletes a disk image from the host, given it's location.
136 Returns True on success and False on failure. Error information is logged.
137 """
138 try:
139 oIMedium = self.oVBox.findHardDisk(sHdLocation);
140 except:
141 try:
142 if self.fpApiVer >= 4.1:
143 oIMedium = self.oVBox.openMedium(sHdLocation, vboxcon.DeviceType_HardDisk,
144 vboxcon.AccessMode_ReadWrite, False);
145 elif self.fpApiVer >= 4.0:
146 oIMedium = self.oVBox.openMedium(sHdLocation, vboxcon.DeviceType_HardDisk,
147 vboxcon.AccessMode_ReadWrite);
148 else:
149 oIMedium = self.oVBox.openHardDisk(sHdLocation, vboxcon.AccessMode_ReadOnly, False, "", False, "");
150 except:
151 return reporter.errorXcpt('failed to open hd "%s"' % (sHdLocation));
152 return self.deleteHdByMedium(oIMedium)
153
154 def deleteHdByMedium(self, oIMedium):
155 """
156 Deletes a disk image from the host, given an IMedium reference.
157 Returns True on success and False on failure. Error information is logged.
158 """
159 try: oProgressCom = oIMedium.deleteStorage();
160 except: return reporter.errorXcpt('deleteStorage() for disk %s failed' % (oIMedium,));
161 try: oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'delete disk %s' % (oIMedium.location));
162 except: return reporter.errorXcpt();
163 oProgress.wait();
164 oProgress.logResult();
165 return oProgress.isSuccess();
166
167
168
169class ProgressWrapper(TdTaskBase):
170 """
171 Wrapper around a progress object for making it a task and providing useful
172 utility methods.
173 The real progress object can be accessed thru the o member.
174 """
175
176 def __init__(self, oProgress, oVBoxMgr, oTstDrv, sName):
177 TdTaskBase.__init__(self, utils.getCallerName());
178 self.o = oProgress;
179 self.oVBoxMgr = oVBoxMgr;
180 self.oTstDrv = oTstDrv;
181 self.sName = sName;
182
183 def toString(self):
184 return '<%s sName=%s, oProgress=%s >' \
185 % (TdTaskBase.toString(self), self.sName, self.o);
186
187 #
188 # TdTaskBase overrides.
189 #
190
191 def pollTask(self, fLocked = False):
192 """
193 Overrides TdTaskBase.pollTask().
194
195 This method returns False until the progress object has completed.
196 """
197 self.doQuickApiTest();
198 try:
199 try:
200 if self.o.completed:
201 return True;
202 except:
203 pass;
204 finally:
205 self.oTstDrv.processPendingEvents();
206 return False;
207
208 def waitForTask(self, cMsTimeout = 0):
209 """
210 Overrides TdTaskBase.waitForTask().
211 Process XPCOM/COM events while waiting.
212 """
213 msStart = base.timestampMilli();
214 fState = self.pollTask(False);
215 while not fState:
216 cMsElapsed = base.timestampMilli() - msStart;
217 if cMsElapsed > cMsTimeout:
218 break;
219 cMsToWait = cMsTimeout - cMsElapsed;
220 if cMsToWait > 500:
221 cMsToWait = 500;
222 try:
223 self.o.waitForCompletion(cMsToWait);
224 except KeyboardInterrupt: raise;
225 except: pass;
226 fState = self.pollTask(False);
227 return fState;
228
229 #
230 # Utility methods.
231 #
232
233 def isSuccess(self):
234 """
235 Tests if the progress object completed successfully.
236 Returns True on success, False on failure or incomplete.
237 """
238 if not self.isCompleted():
239 return False;
240 return self.getResult() >= 0;
241
242 def isCompleted(self):
243 """
244 Wrapper around IProgress.completed.
245 """
246 return self.pollTask();
247
248 def isCancelable(self):
249 """
250 Wrapper around IProgress.cancelable.
251 """
252 try:
253 fRc = self.o.cancelable;
254 except:
255 reporter.logXcpt();
256 fRc = False;
257 return fRc;
258
259 def wasCanceled(self):
260 """
261 Wrapper around IProgress.canceled.
262 """
263 try:
264 fRc = self.o.canceled;
265 except:
266 reporter.logXcpt(self.sName);
267 fRc = False;
268 return fRc;
269
270 def cancel(self):
271 """
272 Wrapper around IProgress.cancel()
273 Returns True on success, False on failure (logged as error).
274 """
275 try:
276 self.o.cancel();
277 except:
278 reporter.errorXcpt(self.sName);
279 return False;
280 return True;
281
282 def getResult(self):
283 """
284 Wrapper around IProgress.resultCode.
285 """
286 try:
287 iRc = self.o.resultCode;
288 except:
289 reporter.logXcpt(self.sName);
290 iRc = -1;
291 return iRc;
292
293 def getErrInfoResultCode(self):
294 """
295 Wrapper around IProgress.errorInfo.resultCode.
296
297 Returns the string on success, -1 on bad objects (logged as error), and
298 -2 on missing errorInfo object.
299 """
300 iRc = -1;
301 try:
302 oErrInfo = self.o.errorInfo;
303 except:
304 reporter.errorXcpt(self.sName);
305 else:
306 if oErrInfo is None:
307 iRc = -2;
308 else:
309 try:
310 iRc = oErrInfo.resultCode;
311 except:
312 reporter.errorXcpt();
313 return iRc;
314
315 def getErrInfoText(self):
316 """
317 Wrapper around IProgress.errorInfo.text.
318
319 Returns the string on success, None on failure. Missing errorInfo is
320 not logged as an error, all other failures are.
321 """
322 sText = None;
323 try:
324 oErrInfo = self.o.errorInfo;
325 except:
326 reporter.log2Xcpt(self.sName);
327 else:
328 if oErrInfo is not None:
329 try:
330 sText = oErrInfo.text;
331 except:
332 reporter.errorXcpt();
333 return sText;
334
335 def stringifyErrorInfo(self):
336 """
337 Formats IProgress.errorInfo into a string.
338 """
339 try:
340 oErrInfo = self.o.errorInfo;
341 except:
342 reporter.logXcpt(self.sName);
343 sErr = 'no error info';
344 else:
345 sErr = vbox.stringifyErrorInfo(oErrInfo);
346 return sErr;
347
348 def stringifyResult(self):
349 """
350 Stringify the result.
351 """
352 if self.isCompleted():
353 if self.wasCanceled():
354 sRet = 'Progress %s: Canceled, hrc=%s' % (self.sName, vbox.ComError.toString(self.getResult()));
355 elif self.getResult() == 0:
356 sRet = 'Progress %s: Success' % (self.sName,);
357 elif self.getResult() > 0:
358 sRet = 'Progress %s: Success (hrc=%s)' % (self.sName, vbox.ComError.toString(self.getResult()));
359 else:
360 sRet = 'Progress %s: Failed! %s' % (self.sName, self.stringifyErrorInfo());
361 else:
362 sRet = 'Progress %s: Not completed yet...' % (self.sName);
363 return sRet;
364
365 def logResult(self, fIgnoreErrors = False):
366 """ Logs the result. """
367 sText = self.stringifyResult();
368 if self.isCompleted() and self.getResult() < 0 \
369 and fIgnoreErrors is False:
370 return reporter.error(sText);
371 return reporter.log(sText);
372
373 def waitOnProgress(self, cMsInterval = 1000):
374 """
375 See vbox.TestDriver.waitOnProgress.
376 """
377 self.doQuickApiTest();
378 return self.oTstDrv.waitOnProgress(self.o, cMsInterval);
379
380 def wait(self, cMsTimeout = 60000, fErrorOnTimeout = True, cMsInterval = 1000):
381 """
382 Wait on the progress object for a while.
383
384 Returns the resultCode of the progress object if completed.
385 Returns -1 on timeout, logged as error if fErrorOnTimeout is set.
386 Returns -2 is the progress object is invalid or waitForCompletion
387 fails (logged as errors).
388 """
389 msStart = base.timestampMilli();
390 while True:
391 self.oTstDrv.processPendingEvents();
392 self.doQuickApiTest();
393 try:
394 if self.o.completed:
395 break;
396 except:
397 reporter.errorXcpt(self.sName);
398 return -2;
399 self.oTstDrv.processPendingEvents();
400
401 cMsElapsed = base.timestampMilli() - msStart;
402 if cMsElapsed > cMsTimeout:
403 if fErrorOnTimeout:
404 reporter.error('Timing out after waiting for %u s on "%s"' % (cMsTimeout / 1000, self.sName))
405 return -1;
406
407 try:
408 self.o.waitForCompletion(cMsInterval);
409 except:
410 reporter.errorXcpt(self.sName);
411 return -2;
412
413 try:
414 rc = self.o.resultCode;
415 except:
416 rc = -2;
417 reporter.errorXcpt(self.sName);
418 self.oTstDrv.processPendingEvents();
419 return rc;
420
421 def waitForOperation(self, iOperation, cMsTimeout = 60000, fErrorOnTimeout = True, cMsInterval = 1000, \
422 fIgnoreErrors = False):
423 """
424 Wait for the completion of a operation.
425
426 Negative iOperation values are relative to operationCount (this
427 property may changed at runtime).
428
429 Returns 0 if the operation completed normally.
430 Returns -1 on timeout, logged as error if fErrorOnTimeout is set.
431 Returns -2 is the progress object is invalid or waitForCompletion
432 fails (logged as errors).
433 Returns -3 if if the operation completed with an error, this is logged
434 as an error.
435 """
436 msStart = base.timestampMilli();
437 while True:
438 self.oTstDrv.processPendingEvents();
439 self.doQuickApiTest();
440 try:
441 iCurrentOperation = self.o.operation;
442 cOperations = self.o.operationCount;
443 if iOperation >= 0:
444 iRealOperation = iOperation;
445 else:
446 iRealOperation = cOperations + iOperation;
447
448 if iCurrentOperation > iRealOperation:
449 return 0;
450 if iCurrentOperation == iRealOperation \
451 and iRealOperation >= cOperations - 1 \
452 and self.o.completed:
453 if self.o.resultCode < 0:
454 self.logResult(fIgnoreErrors);
455 return -3;
456 return 0;
457 except:
458 if fIgnoreErrors:
459 reporter.logXcpt();
460 else:
461 reporter.errorXcpt();
462 return -2;
463 self.oTstDrv.processPendingEvents();
464
465 cMsElapsed = base.timestampMilli() - msStart;
466 if cMsElapsed > cMsTimeout:
467 if fErrorOnTimeout:
468 if fIgnoreErrors:
469 reporter.log('Timing out after waiting for %u s on "%s" operation %d' \
470 % (cMsTimeout / 1000, self.sName, iOperation))
471 else:
472 reporter.error('Timing out after waiting for %u s on "%s" operation %d' \
473 % (cMsTimeout / 1000, self.sName, iOperation))
474 return -1;
475
476 try:
477 self.o.waitForOperationCompletion(iRealOperation, cMsInterval);
478 except:
479 if fIgnoreErrors:
480 reporter.logXcpt(self.sName);
481 else:
482 reporter.errorXcpt(self.sName);
483 return -2;
484 # Not reached.
485
486 def doQuickApiTest(self):
487 """
488 Queries everything that is stable and easy to get at and checks that
489 they don't throw errors.
490 """
491 if True:
492 try:
493 iPct = self.o.operationPercent;
494 sDesc = self.o.description;
495 fCancelable = self.o.cancelable;
496 cSecsRemain = self.o.timeRemaining;
497 fCanceled = self.o.canceled;
498 fCompleted = self.o.completed;
499 iOp = self.o.operation;
500 cOps = self.o.operationCount;
501 iOpPct = self.o.operationPercent;
502 sOpDesc = self.o.operationDescription;
503 except:
504 reporter.errorXcpt('%s: %s' % (self.sName, self.o,));
505 return False;
506 try:
507 # Very noisy -- only enable for debugging purposes.
508 #reporter.log2('%s: op=%u/%u/%s: %u%%; total=%u%% cancel=%s/%s compl=%s rem=%us; desc=%s' \
509 # % (self.sName, iOp, cOps, sOpDesc, iOpPct, iPct, fCanceled, fCancelable, fCompleted, \
510 # cSecsRemain, sDesc));
511 _ = iPct; _ = sDesc; _ = fCancelable; _ = cSecsRemain; _ = fCanceled; _ = fCompleted; _ = iOp;
512 _ = cOps; _ = iOpPct; _ = sOpDesc;
513 except:
514 reporter.errorXcpt();
515 return False;
516
517 return True;
518
519
520class SessionWrapper(TdTaskBase):
521 """
522 Wrapper around a machine session. The real session object can be accessed
523 thru the o member (short is good, right :-).
524 """
525
526 def __init__(self, oSession, oVM, oVBox, oVBoxMgr, oTstDrv, fRemoteSession, sFallbackName = None, sLogFile = None):
527 """
528 Initializes the session wrapper.
529 """
530 TdTaskBase.__init__(self, utils.getCallerName());
531 self.o = oSession;
532 self.oVBox = oVBox;
533 self.oVBoxMgr = oVBoxMgr;
534 self.oVM = oVM; # Not the session machine. Useful backdoor...
535 self.oTstDrv = oTstDrv;
536 self.fpApiVer = oTstDrv.fpApiVer;
537 self.fRemoteSession = fRemoteSession;
538 self.sLogFile = sLogFile;
539 self.oConsoleEventHandler = None;
540 self.uPid = None;
541
542 try:
543 self.sName = oSession.machine.name;
544 except:
545 if sFallbackName is not None:
546 self.sName = sFallbackName;
547 else:
548 try: self.sName = str(oSession.machine);
549 except: self.sName = 'is-this-vm-already-off'
550
551 try:
552 self.sUuid = oSession.machine.id;
553 except:
554 self.sUuid = None;
555
556 # Try cache the SessionPID.
557 self.getPid();
558
559 def __del__(self):
560 """
561 Destructor that makes sure the callbacks are deregistered and
562 that the session is closed.
563 """
564 if self.oConsoleEventHandler is not None:
565 self.oConsoleEventHandler.unregister();
566 self.oConsoleEventHandler = None;
567
568 if self.o is not None:
569 try:
570 self.close();
571 reporter.log('close session %s' % (self.o));
572 except:
573 pass;
574 self.o = None;
575
576 TdTaskBase.__del__(self);
577
578 def toString(self):
579 return '<%s: sUuid=%s, sName=%s, uPid=%s, sDbgCreated=%s, fRemoteSession=%s, oSession=%s,' \
580 ' oConsoleEventHandler=%s, oVM=%s >' \
581 % (type(self).__name__, self.sUuid, self.sName, self.uPid, self.sDbgCreated, self.fRemoteSession,
582 self.o, self.oConsoleEventHandler, self.oVM,);
583
584 def __str__(self):
585 return self.toString();
586
587 #
588 # TdTaskBase overrides.
589 #
590
591 def __pollTask(self):
592 """ Internal poller """
593 # Poll for events after doing the remote GetState call, otherwise we
594 # might end up sleepless because XPCOM queues a cleanup event.
595 try:
596 try:
597 eState = self.o.machine.state;
598 except Exception, oXcpt:
599 if vbox.ComError.notEqual(oXcpt, vbox.ComError.E_UNEXPECTED):
600 reporter.logXcpt();
601 return True;
602 finally:
603 self.oTstDrv.processPendingEvents();
604
605 # Switch
606 if eState == vboxcon.MachineState_Running:
607 return False;
608 if eState == vboxcon.MachineState_Paused:
609 return False;
610 if eState == vboxcon.MachineState_Teleporting:
611 return False;
612 if eState == vboxcon.MachineState_LiveSnapshotting:
613 return False;
614 if eState == vboxcon.MachineState_Starting:
615 return False;
616 if eState == vboxcon.MachineState_Stopping:
617 return False;
618 if eState == vboxcon.MachineState_Saving:
619 return False;
620 if eState == vboxcon.MachineState_Restoring:
621 return False;
622 if eState == vboxcon.MachineState_TeleportingPausedVM:
623 return False;
624 if eState == vboxcon.MachineState_TeleportingIn:
625 return False;
626
627 # *Beeep* fudge!
628 if self.fpApiVer < 3.2 \
629 and eState == vboxcon.MachineState_PoweredOff \
630 and self.getAgeAsMs() < 3000:
631 return False;
632
633 reporter.log('SessionWrapper::pollTask: eState=%s' % (eState));
634 return True;
635
636
637 def pollTask(self, fLocked = False):
638 """
639 Overrides TdTaskBase.pollTask().
640
641 This method returns False while the VM is online and running normally.
642 """
643 fRc = self.__pollTask();
644
645 # HACK ALERT: Lazily try registering the console event handler if
646 # we're not ready.
647 if not fRc and self.oConsoleEventHandler is None:
648 self.registerEventHandlerForTask();
649
650 # HACK ALERT: Lazily try get the PID and add it to the PID file.
651 if not fRc and self.uPid is None:
652 self.getPid();
653
654 return fRc;
655
656 def waitForTask(self, cMsTimeout = 0):
657 """
658 Overrides TdTaskBase.waitForTask().
659 Process XPCOM/COM events while waiting.
660 """
661 msStart = base.timestampMilli();
662 fState = self.pollTask(False);
663 while not fState:
664 cMsElapsed = base.timestampMilli() - msStart;
665 if cMsElapsed > cMsTimeout:
666 break;
667 try: self.oVBoxMgr.waitForEvents(cMsTimeout - cMsElapsed);
668 except KeyboardInterrupt: raise;
669 except: pass;
670 fState = self.pollTask(False);
671 return fState;
672
673 def setTaskOwner(self, oOwner):
674 """
675 HACK ALERT!
676 Overrides TdTaskBase.setTaskOwner() so we can try call
677 registerEventHandlerForTask() again when when the testdriver calls
678 addTask() after VM has been spawned. Related to pollTask() above.
679
680 The testdriver must not add the task too early for this to work!
681 """
682 if oOwner is not None:
683 self.registerEventHandlerForTask()
684 return TdTaskBase.setTaskOwner(self, oOwner);
685
686
687 #
688 # Task helpers.
689 #
690
691 def registerEventHandlerForTask(self):
692 """
693 Registers the console event handlers for working the task state.
694 """
695 if self.oConsoleEventHandler is not None:
696 return True;
697 self.oConsoleEventHandler = self.registerDerivedEventHandler(vbox.SessionConsoleEventHandler, {}, False);
698 return self.oConsoleEventHandler is not None;
699
700
701 def assertPoweredOff(self):
702 """
703 Asserts that the VM is powered off, reporting an error if not.
704 Returns True if powered off, False + error msg if not.
705 """
706 try:
707 try:
708 eState = self.oVM.state;
709 except Exception:
710 reporter.errorXcpt();
711 return True;
712 finally:
713 self.oTstDrv.processPendingEvents();
714
715 if eState == vboxcon.MachineState_PoweredOff:
716 return True;
717 reporter.error('Expected machine state "PoweredOff", machine is in the "%s" state instead.'
718 % (_nameMachineState(eState),));
719 return False;
720
721
722 def getMachineStateWithName(self):
723 """
724 Gets the current machine state both as a constant number/whatever and
725 as a human readable string. On error, the constants will be set to
726 None and the string will be the error message.
727 """
728 try:
729 eState = self.oVM.state;
730 except:
731 return (None, '[error getting state: %s]' % (self.oVBoxMgr.xcptToString(),));
732 finally:
733 self.oTstDrv.processPendingEvents();
734 return (eState, _nameMachineState(eState));
735
736 def reportPrematureTermination(self, sPrefix = ''):
737 """
738 Reports a premature virtual machine termination.
739 Returns False to facilitate simpler error paths.
740 """
741
742 reporter.error(sPrefix + 'The virtual machine terminated prematurely!!');
743 (enmState, sStateNm) = self.getMachineStateWithName();
744 reporter.error(sPrefix + 'Machine state: %s' % (sStateNm,));
745
746 if enmState is not None \
747 and enmState == vboxcon.MachineState_Aborted \
748 and self.uPid is not None:
749 #
750 # Look for process crash info.
751 #
752 def addCrashFile(sLogFile, fBinary):
753 """ processCollectCrashInfo callback. """
754 reporter.addLogFile(sLogFile, 'crash/dump/vm' if fBinary else 'crash/report/vm');
755 utils.processCollectCrashInfo(self.uPid, reporter.log, addCrashFile);
756
757 return False;
758
759
760
761 #
762 # ISession / IMachine / ISomethingOrAnother wrappers.
763 #
764
765 def close(self):
766 """
767 Closes the session if it's open and removes it from the
768 vbox.TestDriver.aoRemoteSessions list.
769 Returns success indicator.
770 """
771 fRc = True;
772 if self.o is not None:
773 # Get the pid in case we need to kill the process later on.
774 self.getPid();
775
776 # Try close it.
777 try:
778 if self.fpApiVer < 3.3:
779 self.o.close();
780 else:
781 self.o.unlockMachine();
782 self.o = None;
783 except KeyboardInterrupt:
784 raise;
785 except:
786 # Kludge to ignore VBoxSVC's closing of our session when the
787 # direct session closes / VM process terminates. Fun!
788 try: fIgnore = self.o.state == vboxcon.SessionState_Unlocked;
789 except: fIgnore = False;
790 if not fIgnore:
791 reporter.errorXcpt('ISession::unlockMachine failed on %s' % (self.o));
792 fRc = False;
793
794 # Remove it from the remote session list if applicable (not 100% clean).
795 if fRc and self.fRemoteSession:
796 try:
797 if self in self.oTstDrv.aoRemoteSessions:
798 reporter.log2('SessionWrapper::close: Removing myself from oTstDrv.aoRemoteSessions');
799 self.oTstDrv.aoRemoteSessions.remove(self)
800 except:
801 reporter.logXcpt();
802
803 if self.uPid is not None:
804 self.oTstDrv.pidFileRemove(self.uPid);
805
806 self.oTstDrv.processPendingEvents();
807 return fRc;
808
809 def saveSettings(self, fClose = False):
810 """
811 Saves the settings and optionally closes the session.
812 Returns success indicator.
813 """
814 try:
815 try:
816 self.o.machine.saveSettings();
817 except:
818 reporter.errorXcpt('saveSettings failed on %s' % (self.o));
819 return False;
820 finally:
821 self.oTstDrv.processPendingEvents();
822 if fClose:
823 return self.close();
824 return True;
825
826 def discardSettings(self, fClose = False):
827 """
828 Discards the settings and optionally closes the session.
829 """
830 try:
831 try:
832 self.o.machine.discardSettings();
833 except:
834 reporter.errorXcpt('discardSettings failed on %s' % (self.o));
835 return False;
836 finally:
837 self.oTstDrv.processPendingEvents();
838 if fClose:
839 return self.close();
840 return True;
841
842 def enableVirtEx(self, fEnable):
843 """
844 Enables or disables AMD-V/VT-x.
845 Returns True on success and False on failure. Error information is logged.
846 """
847 # Enable/disable it.
848 fRc = True;
849 try:
850 self.o.machine.setHWVirtExProperty(vboxcon.HWVirtExPropertyType_Enabled, fEnable);
851 except:
852 reporter.errorXcpt('failed to set HWVirtExPropertyType_Enabled=%s for "%s"' % (fEnable, self.sName));
853 fRc = False;
854 else:
855 reporter.log('set HWVirtExPropertyType_Enabled=%s for "%s"' % (fEnable, self.sName));
856
857 # Force/unforce it.
858 if fRc and hasattr(vboxcon, 'HWVirtExPropertyType_Force'):
859 try:
860 self.o.machine.setHWVirtExProperty(vboxcon.HWVirtExPropertyType_Force, fEnable);
861 except:
862 reporter.errorXcpt('failed to set HWVirtExPropertyType_Force=%s for "%s"' % (fEnable, self.sName));
863 fRc = False;
864 else:
865 reporter.log('set HWVirtExPropertyType_Force=%s for "%s"' % (fEnable, self.sName));
866 else:
867 reporter.log('Warning! vboxcon has no HWVirtExPropertyType_Force attribute.');
868 ## @todo Modify CFGM to do the same for old VBox versions?
869
870 self.oTstDrv.processPendingEvents();
871 return fRc;
872
873 def enableNestedPaging(self, fEnable):
874 """
875 Enables or disables nested paging..
876 Returns True on success and False on failure. Error information is logged.
877 """
878 ## @todo Add/remove force CFGM thing, we don't want fallback logic when testing.
879 fRc = True;
880 try:
881 self.o.machine.setHWVirtExProperty(vboxcon.HWVirtExPropertyType_NestedPaging, fEnable);
882 except:
883 reporter.errorXcpt('failed to set HWVirtExPropertyType_NestedPaging=%s for "%s"' % (fEnable, self.sName));
884 fRc = False;
885 else:
886 reporter.log('set HWVirtExPropertyType_NestedPaging=%s for "%s"' % (fEnable, self.sName));
887 self.oTstDrv.processPendingEvents();
888 return fRc;
889
890 def enableLongMode(self, fEnable):
891 """
892 Enables or disables LongMode.
893 Returns True on success and False on failure. Error information is logged.
894 """
895 # Supported.
896 if self.fpApiVer < 4.2 or not hasattr(vboxcon, 'HWVirtExPropertyType_LongMode'):
897 return True;
898
899 # Enable/disable it.
900 fRc = True;
901 try:
902 self.o.machine.setCPUProperty(vboxcon.CPUPropertyType_LongMode, fEnable);
903 except:
904 reporter.errorXcpt('failed to set CPUPropertyType_LongMode=%s for "%s"' % (fEnable, self.sName));
905 fRc = False;
906 else:
907 reporter.log('set CPUPropertyType_LongMode=%s for "%s"' % (fEnable, self.sName));
908 self.oTstDrv.processPendingEvents();
909 return fRc;
910
911 def enablePae(self, fEnable):
912 """
913 Enables or disables PAE
914 Returns True on success and False on failure. Error information is logged.
915 """
916 fRc = True;
917 try:
918 if self.fpApiVer >= 3.2: # great, ain't it?
919 self.o.machine.setCPUProperty(vboxcon.CPUPropertyType_PAE, fEnable);
920 else:
921 self.o.machine.setCpuProperty(vboxcon.CpuPropertyType_PAE, fEnable);
922 except:
923 reporter.errorXcpt('failed to set CPUPropertyType_PAE=%s for "%s"' % (fEnable, self.sName));
924 fRc = False;
925 else:
926 reporter.log('set CPUPropertyType_PAE=%s for "%s"' % (fEnable, self.sName));
927 self.oTstDrv.processPendingEvents();
928 return fRc;
929
930 def enableIoApic(self, fEnable):
931 """
932 Enables or disables the IO-APIC
933 Returns True on success and False on failure. Error information is logged.
934 """
935 fRc = True;
936 try:
937 self.o.machine.BIOSSettings.IOAPICEnabled = fEnable;
938 except:
939 reporter.errorXcpt('failed to set BIOSSettings.IOAPICEnabled=%s for "%s"' % (fEnable, self.sName));
940 fRc = False;
941 else:
942 reporter.log('set BIOSSettings.IOAPICEnabled=%s for "%s"' % (fEnable, self.sName));
943 self.oTstDrv.processPendingEvents();
944 return fRc;
945
946 def enableHpet(self, fEnable):
947 """
948 Enables or disables the HPET
949 Returns True on success and False on failure. Error information is logged.
950 """
951 fRc = True;
952 try:
953 if self.fpApiVer >= 4.2:
954 self.o.machine.HPETEnabled = fEnable;
955 else:
956 self.o.machine.hpetEnabled = fEnable;
957 except:
958 reporter.errorXcpt('failed to set HpetEnabled=%s for "%s"' % (fEnable, self.sName));
959 fRc = False;
960 else:
961 reporter.log('set HpetEnabled=%s for "%s"' % (fEnable, self.sName));
962 self.oTstDrv.processPendingEvents();
963 return fRc;
964
965 def enableUsbHid(self, fEnable):
966 """
967 Enables or disables the USB HID
968 Returns True on success and False on failure. Error information is logged.
969 """
970 fRc = True;
971 try:
972 if fEnable:
973 if self.fpApiVer >= 4.3:
974 cOhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_OHCI);
975 if cOhciCtls == 0:
976 self.o.machine.AddUSBController('OHCI', vboxcon.USBControllerType_OHCI);
977 else:
978 self.o.machine.usbController.enabled = True;
979
980 if self.fpApiVer >= 4.2:
981 self.o.machine.pointingHIDType = vboxcon.PointingHIDType_ComboMouse;
982 self.o.machine.keyboardHIDType = vboxcon.KeyboardHIDType_ComboKeyboard;
983 else:
984 self.o.machine.pointingHidType = vboxcon.PointingHidType_ComboMouse;
985 self.o.machine.keyboardHidType = vboxcon.KeyboardHidType_ComboKeyboard;
986 else:
987 if self.fpApiVer >= 4.2:
988 self.o.machine.pointingHIDType = vboxcon.PointingHIDType_PS2Mouse;
989 self.o.machine.keyboardHIDType = vboxcon.KeyboardHIDType_PS2Keyboard;
990 else:
991 self.o.machine.pointingHidType = vboxcon.PointingHidType_PS2Mouse;
992 self.o.machine.keyboardHidType = vboxcon.KeyboardHidType_PS2Keyboard;
993 except:
994 reporter.errorXcpt('failed to change UsbHid to %s for "%s"' % (fEnable, self.sName));
995 fRc = False;
996 else:
997 reporter.log('changed UsbHid to %s for "%s"' % (fEnable, self.sName));
998 self.oTstDrv.processPendingEvents();
999 return fRc;
1000
1001 def enableUsbOhci(self, fEnable):
1002 """
1003 Enables or disables the USB OHCI controller
1004 Returns True on success and False on failure. Error information is logged.
1005 """
1006 fRc = True;
1007 try:
1008 if fEnable:
1009 if self.fpApiVer >= 4.3:
1010 cOhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_OHCI);
1011 if cOhciCtls == 0:
1012 self.o.machine.AddUSBController('OHCI', vboxcon.USBControllerType_OHCI);
1013 else:
1014 self.o.machine.usbController.enabled = True;
1015 else:
1016 if self.fpApiVer >= 4.3:
1017 cOhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_OHCI);
1018 if cOhciCtls == 1:
1019 self.o.machine.RemoveUSBController('OHCI');
1020 else:
1021 self.o.machine.usbController.enabled = False;
1022 except:
1023 reporter.errorXcpt('failed to change OHCI to %s for "%s"' % (fEnable, self.sName));
1024 fRc = False;
1025 else:
1026 reporter.log('changed OHCI to %s for "%s"' % (fEnable, self.sName));
1027 self.oTstDrv.processPendingEvents();
1028 return fRc;
1029
1030 def enableUsbEhci(self, fEnable):
1031 """
1032 Enables or disables the USB EHCI controller, enables also OHCI if it is still disabled.
1033 Returns True on success and False on failure. Error information is logged.
1034 """
1035 fRc = True;
1036 try:
1037 if fEnable:
1038 if self.fpApiVer >= 4.3:
1039 cOhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_OHCI);
1040 if cOhciCtls == 0:
1041 self.o.machine.addUSBController('OHCI', vboxcon.USBControllerType_OHCI);
1042
1043 cEhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_EHCI);
1044 if cEhciCtls == 0:
1045 self.o.machine.addUSBController('EHCI', vboxcon.USBControllerType_EHCI);
1046 else:
1047 self.o.machine.usbController.enabled = True;
1048 self.o.machine.usbController.enabledEHCI = True;
1049 else:
1050 if self.fpApiVer >= 4.3:
1051 cEhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_EHCI);
1052 if cEhciCtls == 1:
1053 self.o.machine.RemoveUSBController('EHCI');
1054 else:
1055 self.o.machine.usbController.enabledEHCI = False;
1056 except:
1057 reporter.errorXcpt('failed to change EHCI to %s for "%s"' % (fEnable, self.sName));
1058 fRc = False;
1059 else:
1060 reporter.log('changed EHCI to %s for "%s"' % (fEnable, self.sName));
1061 self.oTstDrv.processPendingEvents();
1062 return fRc;
1063
1064 def enableUsbXhci(self, fEnable):
1065 """
1066 Enables or disables the USB XHCI controller. Error information is logged.
1067 """
1068 fRc = True;
1069 try:
1070 if fEnable:
1071 cXhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_XHCI);
1072 if cXhciCtls == 0:
1073 self.o.machine.addUSBController('XHCI', vboxcon.USBControllerType_XHCI);
1074 else:
1075 cXhciCtls = self.o.machine.getUSBControllerCountByType(vboxcon.USBControllerType_XHCI);
1076 if cXhciCtls == 1:
1077 self.o.machine.RemoveUSBController('XHCI');
1078 except:
1079 reporter.errorXcpt('failed to change XHCI to %s for "%s"' % (fEnable, self.sName));
1080 fRc = False;
1081 else:
1082 reporter.log('changed XHCI to %s for "%s"' % (fEnable, self.sName));
1083 self.oTstDrv.processPendingEvents();
1084 return fRc;
1085
1086 def setFirmwareType(self, eType):
1087 """
1088 Sets the firmware type.
1089 Returns True on success and False on failure. Error information is logged.
1090 """
1091 fRc = True;
1092 try:
1093 self.o.machine.firmwareType = eType;
1094 except:
1095 reporter.errorXcpt('failed to set firmwareType=%s for "%s"' % (eType, self.sName));
1096 fRc = False;
1097 else:
1098 reporter.log('set firmwareType=%s for "%s"' % (eType, self.sName));
1099 self.oTstDrv.processPendingEvents();
1100 return fRc;
1101
1102 def setupBootLogo(self, fEnable, cMsLogoDisplay = 0):
1103 """
1104 Sets up the boot logo. fEnable toggles the fade and boot menu
1105 settings as well as the mode.
1106 """
1107 fRc = True;
1108 try:
1109 self.o.machine.BIOSSettings.logoFadeIn = not fEnable;
1110 self.o.machine.BIOSSettings.logoFadeOut = not fEnable;
1111 self.o.machine.BIOSSettings.logoDisplayTime = cMsLogoDisplay;
1112 if fEnable:
1113 self.o.machine.BIOSSettings.bootMenuMode = vboxcon.BIOSBootMenuMode_Disabled;
1114 else:
1115 self.o.machine.BIOSSettings.bootMenuMode = vboxcon.BIOSBootMenuMode_MessageAndMenu;
1116 except:
1117 reporter.errorXcpt('failed to set logoFadeIn/logoFadeOut/bootMenuMode=%s for "%s"' % (fEnable, self.sName));
1118 fRc = False;
1119 else:
1120 reporter.log('set logoFadeIn/logoFadeOut/bootMenuMode=%s for "%s"' % (fEnable, self.sName));
1121 self.oTstDrv.processPendingEvents();
1122 return fRc;
1123
1124 def setupVrdp(self, fEnable, uPort = None):
1125 """
1126 Configures VRDP.
1127 """
1128 fRc = True;
1129 try:
1130 if self.fpApiVer >= 4.0:
1131 self.o.machine.VRDEServer.enabled = fEnable;
1132 else:
1133 self.o.machine.VRDPServer.enabled = fEnable;
1134 except:
1135 reporter.errorXcpt('failed to set VRDEServer::enabled=%s for "%s"' % (fEnable, self.sName));
1136 fRc = False;
1137
1138 if uPort is not None and fRc:
1139 try:
1140 if self.fpApiVer >= 4.0:
1141 self.o.machine.VRDEServer.setVRDEProperty("TCP/Ports", str(uPort));
1142 else:
1143 self.o.machine.VRDPServer.ports = str(uPort);
1144 except:
1145 reporter.errorXcpt('failed to set VRDEServer::ports=%s for "%s"' % (uPort, self.sName));
1146 fRc = False;
1147 if fRc:
1148 reporter.log('set VRDEServer.enabled/ports=%s/%s for "%s"' % (fEnable, uPort, self.sName));
1149 self.oTstDrv.processPendingEvents();
1150 return fRc;
1151
1152 def getNicDriverNameFromType(self, eNicType):
1153 """
1154 Helper that translate the adapter type into a driver name.
1155 """
1156 if eNicType == vboxcon.NetworkAdapterType_Am79C970A \
1157 or eNicType == vboxcon.NetworkAdapterType_Am79C973:
1158 sName = 'pcnet';
1159 elif eNicType == vboxcon.NetworkAdapterType_I82540EM \
1160 or eNicType == vboxcon.NetworkAdapterType_I82543GC \
1161 or eNicType == vboxcon.NetworkAdapterType_I82545EM:
1162 sName = 'e1000';
1163 elif eNicType == vboxcon.NetworkAdapterType_Virtio:
1164 sName = 'virtio-net';
1165 else:
1166 reporter.error('Unknown adapter type "%s" (VM: "%s")' % (eNicType, self.sName));
1167 sName = 'pcnet';
1168 return sName;
1169
1170 def setupNatForwardingForTxs(self, iNic = 0, iHostPort = 5042):
1171 """
1172 Sets up NAT forwarding for port 5042 if applicable, cleans up if not.
1173 """
1174 try:
1175 oNic = self.o.machine.getNetworkAdapter(iNic);
1176 except:
1177 reporter.errorXcpt('getNetworkAdapter(%s) failed for "%s"' % (iNic, self.sName));
1178 return False;
1179
1180 # Nuke the old setup for all possible adapter types (in case we're
1181 # called after it changed).
1182 for sName in ('pcnet', 'e1000', 'virtio-net'):
1183 for sConfig in ('VBoxInternal/Devices/%s/%u/LUN#0/AttachedDriver/Config' % (sName, iNic), \
1184 'VBoxInternal/Devices/%s/%u/LUN#0/Config' % (sName, iNic)):
1185 try:
1186 self.o.machine.setExtraData('%s/txs/Protocol' % (sConfig), '');
1187 self.o.machine.setExtraData('%s/txs/HostPort' % (sConfig), '');
1188 self.o.machine.setExtraData('%s/txs/GuestPort' % (sConfig), '');
1189 except:
1190 reporter.errorXcpt();
1191
1192 # Set up port forwarding if NAT attachment.
1193 try:
1194 eAttType = oNic.attachmentType;
1195 except:
1196 reporter.errorXcpt('attachmentType on %s failed for "%s"' % (iNic, self.sName));
1197 return False;
1198 if eAttType != vboxcon.NetworkAttachmentType_NAT:
1199 return True;
1200
1201 try:
1202 eNicType = oNic.adapterType;
1203 fTraceEnabled = oNic.traceEnabled;
1204 except:
1205 reporter.errorXcpt('attachmentType/traceEnabled on %s failed for "%s"' % (iNic, self.sName));
1206 return False;
1207
1208 if self.fpApiVer >= 4.1:
1209 try:
1210 if self.fpApiVer >= 4.2:
1211 oNatEngine = oNic.NATEngine;
1212 else:
1213 oNatEngine = oNic.natDriver;
1214 except:
1215 reporter.errorXcpt('Failed to get INATEngine data on "%s"' % (self.sName));
1216 return False;
1217 try: oNatEngine.removeRedirect('txs');
1218 except: pass;
1219 try:
1220 oNatEngine.addRedirect('txs', vboxcon.NATProtocol_TCP, '127.0.0.1', '%s' % (iHostPort), '', '5042');
1221 except:
1222 reporter.errorXcpt('Failed to add a addRedirect redirect on "%s"' % (self.sName));
1223 return False;
1224
1225 else:
1226 sName = self.getNicDriverNameFromType(eNicType);
1227 if fTraceEnabled:
1228 sConfig = 'VBoxInternal/Devices/%s/%u/LUN#0/AttachedDriver/Config' % (sName, iNic)
1229 else:
1230 sConfig = 'VBoxInternal/Devices/%s/%u/LUN#0/Config' % (sName, iNic)
1231
1232 try:
1233 self.o.machine.setExtraData('%s/txs/Protocol' % (sConfig), 'TCP');
1234 self.o.machine.setExtraData('%s/txs/HostPort' % (sConfig), '%s' % (iHostPort));
1235 self.o.machine.setExtraData('%s/txs/GuestPort' % (sConfig), '5042');
1236 except:
1237 reporter.errorXcpt('Failed to set NAT extra data on "%s"' % (self.sName));
1238 return False;
1239 return True;
1240
1241 def setNicType(self, eType, iNic = 0):
1242 """
1243 Sets the NIC type of the specified NIC.
1244 Returns True on success and False on failure. Error information is logged.
1245 """
1246 try:
1247 try:
1248 oNic = self.o.machine.getNetworkAdapter(iNic);
1249 except:
1250 reporter.errorXcpt('getNetworkAdapter(%s) failed for "%s"' % (iNic, self.sName));
1251 return False;
1252 try:
1253 oNic.adapterType = eType;
1254 except:
1255 reporter.errorXcpt('failed to set NIC type on slot %s to %s for VM "%s"' % (iNic, eType, self.sName));
1256 return False;
1257 finally:
1258 self.oTstDrv.processPendingEvents();
1259
1260 if not self.setupNatForwardingForTxs(iNic):
1261 return False;
1262 reporter.log('set NIC type on slot %s to %s for VM "%s"' % (iNic, eType, self.sName));
1263 return True;
1264
1265 def setNicTraceEnabled(self, fTraceEnabled, sTraceFile, iNic = 0):
1266 """
1267 Sets the NIC trace enabled flag and file path.
1268 Returns True on success and False on failure. Error information is logged.
1269 """
1270 try:
1271 try:
1272 oNic = self.o.machine.getNetworkAdapter(iNic);
1273 except:
1274 reporter.errorXcpt('getNetworkAdapter(%s) failed for "%s"' % (iNic, self.sName));
1275 return False;
1276 try:
1277 oNic.traceEnabled = fTraceEnabled;
1278 oNic.traceFile = sTraceFile;
1279 except:
1280 reporter.errorXcpt('failed to set NIC trace flag on slot %s to %s for VM "%s"' \
1281 % (iNic, fTraceEnabled, self.sName));
1282 return False;
1283 finally:
1284 self.oTstDrv.processPendingEvents();
1285
1286 if not self.setupNatForwardingForTxs(iNic):
1287 return False;
1288 reporter.log('set NIC trace on slot %s to "%s" (path "%s") for VM "%s"' %
1289 (iNic, fTraceEnabled, sTraceFile, self.sName));
1290 return True;
1291
1292 def getDefaultNicName(self, eAttachmentType):
1293 """
1294 Return the default network / interface name for the NIC attachment type.
1295 """
1296 sRetName = '';
1297 if eAttachmentType == vboxcon.NetworkAttachmentType_Bridged:
1298 if self.oTstDrv.sDefBridgedNic is not None:
1299 sRetName = self.oTstDrv.sDefBridgedNic;
1300 else:
1301 sRetName = 'eth0';
1302 try:
1303 aoHostNics = self.oVBoxMgr.getArray(self.oVBox.host, 'networkInterfaces');
1304 for oHostNic in aoHostNics:
1305 if oHostNic.interfaceType == vboxcon.HostNetworkInterfaceType_Bridged \
1306 and oHostNic.status == vboxcon.HostNetworkInterfaceStatus_Up:
1307 sRetName = oHostNic.name;
1308 break;
1309 except:
1310 reporter.errorXcpt();
1311 elif eAttachmentType == vboxcon.NetworkAttachmentType_HostOnly:
1312 try:
1313 aoHostNics = self.oVBoxMgr.getArray(self.oVBox.host, 'networkInterfaces');
1314 for oHostNic in aoHostNics:
1315 if oHostNic.interfaceType == vboxcon.HostNetworkInterfaceType_HostOnly:
1316 if oHostNic.status == vboxcon.HostNetworkInterfaceStatus_Up:
1317 sRetName = oHostNic.name;
1318 break;
1319 if sRetName == '':
1320 sRetName = oHostNic.name;
1321 except:
1322 reporter.errorXcpt();
1323 if sRetName == '':
1324 sRetName = 'HostInterfaceNetwork-vboxnet0';
1325 elif eAttachmentType == vboxcon.NetworkAttachmentType_Internal:
1326 sRetName = 'VBoxTest';
1327 elif eAttachmentType == vboxcon.NetworkAttachmentType_NAT:
1328 sRetName = '';
1329 else:
1330 reporter.error('eAttachmentType=%s is not known' % (eAttachmentType));
1331 return sRetName;
1332
1333 def setNicAttachment(self, eAttachmentType, sName = None, iNic = 0):
1334 """
1335 Sets the attachment type of the specified NIC.
1336 Returns True on success and False on failure. Error information is logged.
1337 """
1338 try:
1339 oNic = self.o.machine.getNetworkAdapter(iNic);
1340 except:
1341 reporter.errorXcpt('getNetworkAdapter(%s) failed for "%s"' % (iNic, self.sName));
1342 return False;
1343
1344 try:
1345 if eAttachmentType is not None:
1346 try:
1347 if self.fpApiVer >= 4.1:
1348 oNic.attachmentType = eAttachmentType;
1349 else:
1350 if eAttachmentType == vboxcon.NetworkAttachmentType_NAT:
1351 oNic.attachToNAT();
1352 elif eAttachmentType == vboxcon.NetworkAttachmentType_Bridged:
1353 oNic.attachToBridgedInterface();
1354 elif eAttachmentType == vboxcon.NetworkAttachmentType_Internal:
1355 oNic.attachToInternalNetwork();
1356 elif eAttachmentType == vboxcon.NetworkAttachmentType_HostOnly:
1357 oNic.attachToHostOnlyInterface();
1358 else:
1359 raise base.GenError("eAttachmentType=%s is invalid" % (eAttachmentType));
1360 except:
1361 reporter.errorXcpt('failed to set the attachment type on slot %s to %s for VM "%s"' \
1362 % (iNic, eAttachmentType, self.sName));
1363 return False;
1364 else:
1365 try:
1366 eAttachmentType = oNic.attachmentType;
1367 except:
1368 reporter.errorXcpt('failed to get the attachment type on slot %s for VM "%s"' % (iNic, self.sName));
1369 return False;
1370 finally:
1371 self.oTstDrv.processPendingEvents();
1372
1373 if sName is not None:
1374 # Resolve the special 'default' name.
1375 if sName == 'default':
1376 sName = self.getDefaultNicName(eAttachmentType);
1377
1378 # The name translate to different attributes depending on the
1379 # attachment type.
1380 try:
1381 if eAttachmentType == vboxcon.NetworkAttachmentType_Bridged:
1382 ## @todo check this out on windows, may have to do a
1383 # translation of the name there or smth IIRC.
1384 try:
1385 if self.fpApiVer >= 4.1:
1386 oNic.bridgedInterface = sName;
1387 else:
1388 oNic.hostInterface = sName;
1389 except:
1390 reporter.errorXcpt('failed to set the hostInterface property on slot %s to "%s" for VM "%s"' \
1391 % (iNic, sName, self.sName));
1392 return False;
1393 elif eAttachmentType == vboxcon.NetworkAttachmentType_HostOnly:
1394 try:
1395 if self.fpApiVer >= 4.1:
1396 oNic.hostOnlyInterface = sName;
1397 else:
1398 oNic.hostInterface = sName;
1399 except:
1400 reporter.errorXcpt('failed to set the internalNetwork property on slot %s to "%s" for VM "%s"' \
1401 % (iNic, sName, self.sName));
1402 return False;
1403 elif eAttachmentType == vboxcon.NetworkAttachmentType_Internal:
1404 try:
1405 oNic.internalNetwork = sName;
1406 except:
1407 reporter.errorXcpt('failed to set the internalNetwork property on slot %s to "%s" for VM "%s"' \
1408 % (iNic, sName, self.sName));
1409 return False;
1410 elif eAttachmentType == vboxcon.NetworkAttachmentType_NAT:
1411 try:
1412 oNic.NATNetwork = sName;
1413 except:
1414 reporter.errorXcpt('failed to set the NATNetwork property on slot %s to "%s" for VM "%s"' \
1415 % (iNic, sName, self.sName));
1416 return False;
1417 finally:
1418 self.oTstDrv.processPendingEvents();
1419
1420 if not self.setupNatForwardingForTxs(iNic):
1421 return False;
1422 reporter.log('set NIC type on slot %s to %s for VM "%s"' % (iNic, eAttachmentType, self.sName));
1423 return True;
1424
1425 def setNicMacAddress(self, sMacAddr, iNic = 0):
1426 """
1427 Sets the MAC address of the specified NIC.
1428 Returns True on success and False on failure. Error information is logged.
1429 """
1430
1431 # Resolve missing MAC address prefix
1432 cchMacAddr = len(sMacAddr) > 0;
1433 if cchMacAddr > 0 and cchMacAddr < 12:
1434 sHostName = '';
1435 try:
1436 sHostName = socket.getfqdn();
1437 if sys.platform == 'win32' \
1438 and sHostName.endswith('.sun.com') \
1439 and not sHostName.endswith('.germany.sun.com'):
1440 sHostName = socket.gethostname(); # klugde.
1441 abHostIP = socket.inet_aton(socket.gethostbyname(sHostName));
1442 except:
1443 reporter.errorXcpt('failed to determin the host IP for "%s".' % (sHostName,));
1444 abHostIP = array.array('B', (0x80, 0x86, 0x00, 0x00)).tostring();
1445 sDefaultMac = '%02X%02X%02X%02X%02X%02X' \
1446 % (0x02, ord(abHostIP[0]), ord(abHostIP[1]), ord(abHostIP[2]), ord(abHostIP[3]), iNic);
1447 sMacAddr = sDefaultMac[0:(11 - cchMacAddr)] + sMacAddr;
1448
1449 # Get the NIC object and try set it address.
1450 try:
1451 oNic = self.o.machine.getNetworkAdapter(iNic);
1452 except:
1453 reporter.errorXcpt('getNetworkAdapter(%s) failed for "%s"' % (iNic, self.sName));
1454 return False;
1455
1456 try:
1457 oNic.MACAddress = sMacAddr;
1458 except:
1459 reporter.errorXcpt('failed to set the MAC address on slot %s to "%s" for VM "%s"' \
1460 % (iNic, sMacAddr, self.sName));
1461 return False;
1462
1463 reporter.log('set MAC address on slot %s to %s for VM "%s"' % (iNic, sMacAddr, self.sName));
1464 return True;
1465
1466 def setRamSize(self, cMB):
1467 """
1468 Set the RAM size of the VM.
1469 Returns True on success and False on failure. Error information is logged.
1470 """
1471 fRc = True;
1472 try:
1473 self.o.machine.memorySize = cMB;
1474 except:
1475 reporter.errorXcpt('failed to set the RAM size of "%s" to %s' % (self.sName, cMB));
1476 fRc = False;
1477 else:
1478 reporter.log('set the RAM size of "%s" to %s' % (self.sName, cMB));
1479 self.oTstDrv.processPendingEvents();
1480 return fRc;
1481
1482 def setVRamSize(self, cMB):
1483 """
1484 Set the RAM size of the VM.
1485 Returns True on success and False on failure. Error information is logged.
1486 """
1487 fRc = True;
1488 try:
1489 self.o.machine.VRAMSize = cMB;
1490 except:
1491 reporter.errorXcpt('failed to set the VRAM size of "%s" to %s' % (self.sName, cMB));
1492 fRc = False;
1493 else:
1494 reporter.log('set the VRAM size of "%s" to %s' % (self.sName, cMB));
1495 self.oTstDrv.processPendingEvents();
1496 return fRc;
1497
1498 def setCpuCount(self, cCpus):
1499 """
1500 Set the number of CPUs.
1501 Returns True on success and False on failure. Error information is logged.
1502 """
1503 fRc = True;
1504 try:
1505 self.o.machine.CPUCount = cCpus;
1506 except:
1507 reporter.errorXcpt('failed to set the CPU count of "%s" to %s' % (self.sName, cCpus));
1508 fRc = False;
1509 else:
1510 reporter.log('set the CPU count of "%s" to %s' % (self.sName, cCpus));
1511 self.oTstDrv.processPendingEvents();
1512 return fRc;
1513
1514 def ensureControllerAttached(self, sController):
1515 """
1516 Makes sure the specified controller is attached to the VM, attaching it
1517 if necessary.
1518 """
1519 try:
1520 try:
1521 self.o.machine.getStorageControllerByName(sController);
1522 except:
1523 iType = _ControllerNameToBus(sController);
1524 try:
1525 self.o.machine.addStorageController(sController, iType);
1526 reporter.log('added storage controller "%s" (type %s) to %s' % (sController, iType, self.sName));
1527 except:
1528 reporter.errorXcpt('addStorageController("%s",%s) failed on "%s"' % (sController, iType, self.sName) );
1529 return False;
1530 finally:
1531 self.oTstDrv.processPendingEvents();
1532 return True;
1533
1534 def setStorageControllerPortCount(self, sController, iPortCount):
1535 """
1536 Set maximum ports count for storage controller
1537 """
1538 try:
1539 oCtl = self.o.machine.getStorageControllerByName(sController)
1540 oCtl.portCount = iPortCount
1541 self.oTstDrv.processPendingEvents()
1542 reporter.log('set controller "%s" port count to value %d' % (sController, iPortCount))
1543 return True
1544 except:
1545 reporter.log('unable to set storage controller "%s" ports count to %d' % (sController, iPortCount))
1546
1547 return False
1548
1549 def setBootOrder(self, iPosition, eType):
1550 """
1551 Set guest boot order type
1552 @param iPosition boot order position
1553 @param eType device type (vboxcon.DeviceType_HardDisk,
1554 vboxcon.DeviceType_DVD, vboxcon.DeviceType_Floppy)
1555 """
1556 try:
1557 self.o.machine.setBootOrder(iPosition, eType)
1558 except:
1559 return reporter.errorXcpt('Unable to set boot order.')
1560
1561 reporter.log('Set boot order [%d] for device %s' % (iPosition, str(eType)))
1562 self.oTstDrv.processPendingEvents();
1563
1564 return True
1565
1566 def setStorageControllerType(self, eType, sController = "IDE Controller"):
1567 """
1568 Similar to ensureControllerAttached, except it will change the type.
1569 """
1570 try:
1571 oCtl = self.o.machine.getStorageControllerByName(sController);
1572 except:
1573 iType = _ControllerNameToBus(sController);
1574 try:
1575 oCtl = self.o.machine.addStorageController(sController, iType);
1576 reporter.log('added storage controller "%s" (type %s) to %s' % (sController, iType, self.sName));
1577 except:
1578 reporter.errorXcpt('addStorageController("%s",%s) failed on "%s"' % (sController, iType, self.sName) );
1579 return False;
1580 try:
1581 oCtl.controllerType = eType;
1582 except:
1583 reporter.errorXcpt('failed to set controller type of "%s" on "%s" to %s' % (sController, self.sName, eType) );
1584 return False;
1585 reporter.log('set controller type of "%s" on "%s" to %s' % (sController, self.sName, eType) );
1586 self.oTstDrv.processPendingEvents();
1587 return True;
1588
1589 def attachDvd(self, sImage = None, sController = "IDE Controller", iPort = 1, iDevice = 0):
1590 """
1591 Attaches a DVD drive to a VM, optionally with an ISO inserted.
1592 Returns True on success and False on failure. Error information is logged.
1593 """
1594 # Input validation.
1595 if sImage is not None and not self.oTstDrv.isResourceFile(sImage)\
1596 and not os.path.isabs(sImage): ## fixme - testsuite unzip ++
1597 reporter.fatal('"%s" is not in the resource set' % (sImage));
1598 return None;
1599
1600 if not self.ensureControllerAttached(sController):
1601 return False;
1602
1603 # Find/register the image if specified.
1604 oImage = None;
1605 sImageUuid = "";
1606 if sImage is not None:
1607 sFullName = self.oTstDrv.getFullResourceName(sImage)
1608 try:
1609 oImage = self.oVBox.findDVDImage(sFullName);
1610 except:
1611 try:
1612 if self.fpApiVer >= 4.1:
1613 oImage = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_DVD, vboxcon.AccessMode_ReadOnly, False);
1614 elif self.fpApiVer >= 4.0:
1615 oImage = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_DVD, vboxcon.AccessMode_ReadOnly);
1616 else:
1617 oImage = self.oVBox.openDVDImage(sFullName, "");
1618 except vbox.ComException, oXcpt:
1619 if oXcpt.errno != -1:
1620 reporter.errorXcpt('failed to open DVD image "%s" xxx' % (sFullName));
1621 else:
1622 reporter.errorXcpt('failed to open DVD image "%s" yyy' % (sFullName));
1623 return False;
1624 except:
1625 reporter.errorXcpt('failed to open DVD image "%s"' % (sFullName));
1626 return False;
1627 try:
1628 sImageUuid = oImage.id;
1629 except:
1630 reporter.errorXcpt('failed to get the UUID of "%s"' % (sFullName));
1631 return False;
1632
1633 # Attach the DVD.
1634 fRc = True;
1635 try:
1636 if self.fpApiVer >= 4.0:
1637 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_DVD, oImage);
1638 else:
1639 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_DVD, sImageUuid);
1640 except:
1641 reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
1642 % (sController, iPort, iDevice, sImageUuid, self.sName) );
1643 fRc = False;
1644 else:
1645 reporter.log('attached DVD to %s, image="%s"' % (self.sName, sImage));
1646 self.oTstDrv.processPendingEvents();
1647 return fRc;
1648
1649 def attachHd(self, sHd, sController = "IDE Controller", iPort = 0, iDevice = 0, fImmutable = True):
1650 """
1651 Attaches a HD to a VM.
1652 Returns True on success and False on failure. Error information is logged.
1653 """
1654 # Input validation.
1655 if not self.oTstDrv.isResourceFile(sHd):
1656 reporter.fatal('"%s" is not in the resource set' % (sHd,));
1657 return None;
1658
1659 if not self.ensureControllerAttached(sController):
1660 return False;
1661
1662 # Find the HD, registering it if necessary (as immutable).
1663 sFullName = self.oTstDrv.getFullResourceName(sHd);
1664 try:
1665 oHd = self.oVBox.findHardDisk(sFullName);
1666 except:
1667 try:
1668 if self.fpApiVer >= 4.1:
1669 oHd = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_HardDisk, vboxcon.AccessMode_ReadOnly, False);
1670 elif self.fpApiVer >= 4.0:
1671 oHd = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_HardDisk, vboxcon.AccessMode_ReadOnly);
1672 else:
1673 oHd = self.oVBox.openHardDisk(sFullName, vboxcon.AccessMode_ReadOnly, False, "", False, "");
1674 except:
1675 reporter.errorXcpt('failed to open hd "%s"' % (sFullName));
1676 return False;
1677 try:
1678 if fImmutable:
1679 oHd.type = vboxcon.MediumType_Immutable;
1680 else:
1681 oHd.type = vboxcon.MediumType_Normal;
1682 except:
1683 if fImmutable:
1684 reporter.errorXcpt('failed to set hd "%s" immutable' % (sHd));
1685 else:
1686 reporter.errorXcpt('failed to set hd "%s" normal' % (sHd));
1687 return False;
1688
1689 # Attach it.
1690 fRc = True;
1691 try:
1692 if self.fpApiVer >= 4.0:
1693 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd);
1694 else:
1695 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd.id);
1696 except:
1697 reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
1698 % (sController, iPort, iDevice, oHd.id, self.sName) );
1699 fRc = False;
1700 else:
1701 reporter.log('attached "%s" to %s' % (sHd, self.sName));
1702 self.oTstDrv.processPendingEvents();
1703 return fRc;
1704
1705 def createAndAttachHd(self, sHd, sFmt = "VDI", sController = "IDE Controller", cb = 10*1024*1024*1024, \
1706 iPort = 0, iDevice = 0, fImmutable = True):
1707 """
1708 Creates and attaches a HD to a VM.
1709 Returns True on success and False on failure. Error information is logged.
1710 """
1711 if not self.ensureControllerAttached(sController):
1712 return False;
1713
1714 try:
1715 if self.fpApiVer >= 4.4:
1716 oHd = self.oVBox.createMedium(sFmt, sHd, vboxcon.AccessMode_ReadWrite, vboxcon.DeviceType_HardDisk);
1717 else:
1718 oHd = self.oVBox.createHardDisk(sFmt, sHd);
1719 oProgressXpcom = oHd.createBaseStorage(cb, (vboxcon.MediumVariant_Standard, ))
1720 oProgress = ProgressWrapper(oProgressXpcom, self.oVBoxMgr, self.oTstDrv, 'create disk %s' % (sHd));
1721 oProgress.wait();
1722 oProgress.logResult();
1723 except:
1724 reporter.errorXcpt('failed to create hd "%s"' % (sHd));
1725 return False;
1726
1727 fRc = True;
1728 try:
1729 if fImmutable:
1730 oHd.type = vboxcon.MediumType_Immutable;
1731 else:
1732 oHd.type = vboxcon.MediumType_Normal;
1733 except:
1734 if fImmutable:
1735 reporter.errorXcpt('failed to set hd "%s" immutable' % (sHd));
1736 else:
1737 reporter.errorXcpt('failed to set hd "%s" normal' % (sHd));
1738 fRc = False;
1739
1740 # Attach it.
1741 if fRc is True:
1742 try:
1743 if self.fpApiVer >= 4.0:
1744 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd);
1745 else:
1746 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd.id);
1747 except:
1748 reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
1749 % (sController, iPort, iDevice, oHd.id, self.sName) );
1750 fRc = False;
1751 else:
1752 reporter.log('attached "%s" to %s' % (sHd, self.sName));
1753
1754 # Delete disk in case of an error
1755 if fRc is False:
1756 try:
1757 oProgressCom = oHd.deleteStorage();
1758 except:
1759 reporter.errorXcpt('deleteStorage() for disk %s failed' % (sHd,));
1760 else:
1761 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'delete disk %s' % (sHd));
1762 oProgress.wait();
1763 oProgress.logResult();
1764
1765 self.oTstDrv.processPendingEvents();
1766 return fRc;
1767
1768 def detachHd(self, sController = "IDE Controller", iPort = 0, iDevice = 0):
1769 """
1770 Detaches a HD, if attached, and returns a reference to it (IMedium).
1771
1772 In order to delete the detached medium, the caller must first save
1773 the changes made in this session.
1774
1775 Returns (fRc, oHd), where oHd is None unless fRc is True, and fRc is
1776 your standard success indicator. Error information is logged.
1777 """
1778
1779 # What's attached?
1780 try:
1781 oHd = self.o.machine.getMedium(sController, iPort, iDevice);
1782 except:
1783 if self.oVBoxMgr.xcptIsOurXcptKind() \
1784 and self.oVBoxMgr.xcptIsEqual(None, self.oVBoxMgr.constants.VBOX_E_OBJECT_NOT_FOUND):
1785 reporter.log('No HD attached (to %s %s:%s)' % (sController, iPort, iDevice));
1786 return (True, None);
1787 return (reporter.errorXcpt('Error getting media at port %s, device %s, on %s.'
1788 % (iPort, iDevice, sController)), None);
1789 # Detach it.
1790 try:
1791 self.o.machine.detachDevice(sController, iPort, iDevice);
1792 except:
1793 return (reporter.errorXcpt('detachDevice("%s",%s,%s) failed on "%s"' \
1794 % (sController, iPort, iDevice, self.sName) ), None);
1795 reporter.log('detached HD ("%s",%s,%s) from %s' % (sController, iPort, iDevice, self.sName));
1796 return (True, oHd);
1797
1798 def attachFloppy(self, sFloppy, sController = "Floppy Controller", iPort = 0, iDevice = 0):
1799 """
1800 Attaches a floppy image to a VM.
1801 Returns True on success and False on failure. Error information is logged.
1802 """
1803 # Input validation.
1804 ## @todo Fix this wrt to bootsector-xxx.img from the validationkit.zip.
1805 ##if not self.oTstDrv.isResourceFile(sFloppy):
1806 ## reporter.fatal('"%s" is not in the resource set' % (sFloppy));
1807 ## return None;
1808
1809 if not self.ensureControllerAttached(sController):
1810 return False;
1811
1812 # Find the floppy image, registering it if necessary (as immutable).
1813 sFullName = self.oTstDrv.getFullResourceName(sFloppy);
1814 try:
1815 oFloppy = self.oVBox.findFloppyImage(sFullName);
1816 except:
1817 try:
1818 if self.fpApiVer >= 4.1:
1819 oFloppy = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_Floppy, vboxcon.AccessMode_ReadOnly, False);
1820 elif self.fpApiVer >= 4.0:
1821 oFloppy = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_Floppy, vboxcon.AccessMode_ReadOnly);
1822 else:
1823 oFloppy = self.oVBox.openFloppyImage(sFullName, "");
1824 except:
1825 reporter.errorXcpt('failed to open floppy "%s"' % (sFullName));
1826 return False;
1827 ## @todo the following works but causes trouble below (asserts in main).
1828 #try:
1829 # oFloppy.type = vboxcon.MediumType_Immutable;
1830 #except:
1831 # reporter.errorXcpt('failed to make floppy "%s" immutable' % (sFullName));
1832 # return False;
1833
1834 # Attach it.
1835 fRc = True;
1836 try:
1837 if self.fpApiVer >= 4.0:
1838 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_Floppy, oFloppy);
1839 else:
1840 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_Floppy, oFloppy.id);
1841 except:
1842 reporter.errorXcpt('attachDevice("%s",%s,%s,Floppy,"%s") failed on "%s"' \
1843 % (sController, iPort, iDevice, oFloppy.id, self.sName) );
1844 fRc = False;
1845 else:
1846 reporter.log('attached "%s" to %s' % (sFloppy, self.sName));
1847 self.oTstDrv.processPendingEvents();
1848 return fRc;
1849
1850 def setupNic(self, sType, sXXX):
1851 """
1852 Attaches a HD to a VM.
1853 Returns True on success and False on failure. Error information is logged.
1854 """
1855 if sType == "PCNet": enmType = vboxcon.NetworkAdapterType_Am79C973;
1856 elif sType == "PCNetOld": enmType = vboxcon.NetworkAdapterType_Am79C970A;
1857 elif sType == "E1000": enmType = vboxcon.NetworkAdapterType_I82545EM; # MT Server
1858 elif sType == "E1000Desk": enmType = vboxcon.NetworkAdapterType_I82540EM; # MT Desktop
1859 elif sType == "E1000Srv2": enmType = vboxcon.NetworkAdapterType_I82543GC; # T Server
1860 elif sType == "Virtio": enmType = vboxcon.NetworkAdapterType_Virtio;
1861 else:
1862 reporter.error('Invalid NIC type: "%s" (sXXX=%s)' % (sType, sXXX));
1863 return False;
1864 ## @todo Implement me!
1865 if enmType is not None: pass
1866 return True;
1867
1868 def setupPreferredConfig(self): # pylint: disable=R0914
1869 """
1870 Configures the VM according to the preferences of the guest type.
1871 """
1872 try:
1873 sOsTypeId = self.o.machine.OSTypeId;
1874 except:
1875 reporter.errorXcpt('failed to obtain the OSTypeId for "%s"' % (self.sName));
1876 return False;
1877
1878 try:
1879 oOsType = self.oVBox.getGuestOSType(sOsTypeId);
1880 except:
1881 reporter.errorXcpt('getGuestOSType("%s") failed for "%s"' % (sOsTypeId, self.sName));
1882 return False;
1883
1884 # get the attributes.
1885 try:
1886 #sFamilyId = oOsType.familyId;
1887 #f64Bit = oOsType.is64Bit;
1888 fIoApic = oOsType.recommendedIOAPIC;
1889 fVirtEx = oOsType.recommendedVirtEx;
1890 cMBRam = oOsType.recommendedRAM;
1891 cMBVRam = oOsType.recommendedVRAM;
1892 #cMBHdd = oOsType.recommendedHDD;
1893 eNicType = oOsType.adapterType;
1894 if self.fpApiVer >= 3.2:
1895 if self.fpApiVer >= 4.2:
1896 fPae = oOsType.recommendedPAE;
1897 fUsbHid = oOsType.recommendedUSBHID;
1898 fHpet = oOsType.recommendedHPET;
1899 eStorCtlType = oOsType.recommendedHDStorageController;
1900 else:
1901 fPae = oOsType.recommendedPae;
1902 fUsbHid = oOsType.recommendedUsbHid;
1903 fHpet = oOsType.recommendedHpet;
1904 eStorCtlType = oOsType.recommendedHdStorageController;
1905 eFirmwareType = oOsType.recommendedFirmware;
1906 else:
1907 fPae = False;
1908 fUsbHid = False;
1909 fHpet = False;
1910 eFirmwareType = -1;
1911 eStorCtlType = vboxcon.StorageControllerType_PIIX4;
1912 except:
1913 reporter.errorXcpt('exception reading IGuestOSType(%s) attribute' % (sOsTypeId));
1914 self.oTstDrv.processPendingEvents();
1915 return False;
1916 self.oTstDrv.processPendingEvents();
1917
1918 # Do the setting. Continue applying settings on error in case the
1919 # caller ignores the return code
1920 fRc = True;
1921 if not self.enableIoApic(fIoApic): fRc = False;
1922 if not self.enableVirtEx(fVirtEx): fRc = False;
1923 if not self.enablePae(fPae): fRc = False;
1924 if not self.setRamSize(cMBRam): fRc = False;
1925 if not self.setVRamSize(cMBVRam): fRc = False;
1926 if not self.setNicType(eNicType, 0): fRc = False;
1927 if self.fpApiVer >= 3.2:
1928 if not self.setFirmwareType(eFirmwareType): fRc = False;
1929 if not self.enableUsbHid(fUsbHid): fRc = False;
1930 if not self.enableHpet(fHpet): fRc = False;
1931 if eStorCtlType == vboxcon.StorageControllerType_PIIX3 \
1932 or eStorCtlType == vboxcon.StorageControllerType_PIIX4 \
1933 or eStorCtlType == vboxcon.StorageControllerType_ICH6:
1934 if not self.setStorageControllerType(eStorCtlType, "IDE Controller"):
1935 fRc = False;
1936
1937 return fRc;
1938
1939 def addUsbDeviceFilter(self, sName, sVendorId, sProductId):
1940 """
1941 Creates a USB device filter and inserts it into the VM.
1942 Returns True on success.
1943 Returns False on failure (logged).
1944 """
1945 fRc = True;
1946
1947 try:
1948 usbDevFilter = self.o.machine.USBDeviceFilters.createDeviceFilter(sName);
1949 usbDevFilter.active = True;
1950 usbDevFilter.vendorId = sVendorId;
1951 usbDevFilter.productId = sProductId;
1952 try:
1953 self.o.machine.USBDeviceFilters.insertDeviceFilter(0, usbDevFilter);
1954 except:
1955 reporter.errorXcpt('insertDeviceFilter(%s) failed on "%s"' \
1956 % (0, self.sName) );
1957 fRc = False;
1958 else:
1959 reporter.log('inserted USB device filter "%s" to %s' % (sName, self.sName));
1960 except:
1961 reporter.errorXcpt('createDeviceFilter("%s") failed on "%s"' \
1962 % (sName, self.sName) );
1963 fRc = False;
1964 return fRc;
1965
1966 def getGuestPropertyValue(self, sName):
1967 """
1968 Gets a guest property value.
1969 Returns the value on success, None on failure (logged).
1970 """
1971 try:
1972 sValue = self.o.machine.getGuestPropertyValue(sName);
1973 except:
1974 reporter.errorXcpt('IMachine::getGuestPropertyValue("%s") failed' % (sName));
1975 return None;
1976 return sValue;
1977
1978 def setGuestPropertyValue(self, sName, sValue):
1979 """
1980 Sets a guest property value.
1981 Returns the True on success, False on failure (logged).
1982 """
1983 try:
1984 self.o.machine.setGuestPropertyValue(sName, sValue);
1985 except:
1986 reporter.errorXcpt('IMachine::setGuestPropertyValue("%s","%s") failed' % (sName, sValue));
1987 return False;
1988 return True;
1989
1990 def delGuestPropertyValue(self, sName):
1991 """
1992 Deletes a guest property value.
1993 Returns the True on success, False on failure (logged).
1994 """
1995 try:
1996 oMachine = self.o.machine;
1997 if self.fpApiVer >= 4.2:
1998 oMachine.deleteGuestProperty(sName);
1999 else:
2000 oMachine.setGuestPropertyValue(sName, '');
2001 except:
2002 reporter.errorXcpt('Unable to delete guest property "%s"' % (sName,));
2003 return False;
2004 return True;
2005
2006 def setExtraData(self, sKey, sValue):
2007 """
2008 Sets extra data.
2009 Returns the True on success, False on failure (logged).
2010 """
2011 try:
2012 self.o.machine.setExtraData(sKey, sValue);
2013 except:
2014 reporter.errorXcpt('IMachine::setExtraData("%s","%s") failed' % (sKey, sValue));
2015 return False;
2016 return True;
2017
2018 def getExtraData(self, sKey):
2019 """
2020 Gets extra data.
2021 Returns value on success, None on failure.
2022 """
2023 try:
2024 sValue = self.o.machine.getExtraData(sKey)
2025 except:
2026 reporter.errorXcpt('IMachine::setExtraData("%s","%s") failed' % (sKey, sValue))
2027 return None
2028 return sValue
2029
2030 def setupTeleporter(self, fEnabled=True, uPort = 6500, sAddress = '', sPassword = ''):
2031 """
2032 Sets up the teleporter for the VM.
2033 Returns True on success, False on failure (logged).
2034 """
2035 try:
2036 self.o.machine.teleporterAddress = sAddress;
2037 self.o.machine.teleporterPort = uPort;
2038 self.o.machine.teleporterPassword = sPassword;
2039 self.o.machine.teleporterEnabled = fEnabled;
2040 except:
2041 reporter.errorXcpt('setupTeleporter(%s, %s, %s, %s)' % (fEnabled, sPassword, uPort, sAddress));
2042 return False;
2043 return True;
2044
2045 def enableTeleporter(self, fEnable=True):
2046 """
2047 Enables or disables the teleporter of the VM.
2048 Returns True on success, False on failure (logged).
2049 """
2050 try:
2051 self.o.machine.teleporterEnabled = fEnable;
2052 except:
2053 reporter.errorXcpt('IMachine::teleporterEnabled=%s failed' % (fEnable));
2054 return False;
2055 return True;
2056
2057 def teleport(self, sHostname = 'localhost', uPort = 6500, sPassword = 'password', cMsMaxDowntime = 250):
2058 """
2059 Wrapper around the IConsole::teleport() method.
2060 Returns a progress object on success, None on failure (logged).
2061 """
2062 reporter.log2('"%s"::teleport(%s,%s,%s,%s)...' % (self.sName, sHostname, uPort, sPassword, cMsMaxDowntime));
2063 try:
2064 oProgress = self.o.console.teleport(sHostname, uPort, sPassword, cMsMaxDowntime)
2065 except:
2066 reporter.errorXcpt('IConsole::teleport(%s,%s,%s,%s) failed' % (sHostname, uPort, sPassword, cMsMaxDowntime));
2067 return None;
2068 return ProgressWrapper(oProgress, self.oVBoxMgr, self.oTstDrv, 'teleport %s' % (self.sName,));
2069
2070 def getOsType(self):
2071 """
2072 Gets the IGuestOSType interface for the machine.
2073
2074 return IGuestOSType interface on success, None + errorXcpt on failure.
2075 No exceptions raised.
2076 """
2077 try:
2078 sOsTypeId = self.o.machine.OSTypeId;
2079 except:
2080 reporter.errorXcpt('failed to obtain the OSTypeId for "%s"' % (self.sName));
2081 return None;
2082
2083 try:
2084 oOsType = self.oVBox.getGuestOSType(sOsTypeId);
2085 except:
2086 reporter.errorXcpt('getGuestOSType("%s") failed for "%s"' % (sOsTypeId, self.sName));
2087 return None;
2088
2089 return oOsType;
2090
2091 def setOsType(self, sNewTypeId):
2092 """
2093 Changes the OS type.
2094
2095 returns True on success, False + errorXcpt on failure.
2096 No exceptions raised.
2097 """
2098 try:
2099 self.o.machine.OSTypeId = sNewTypeId;
2100 except:
2101 reporter.errorXcpt('failed to set the OSTypeId for "%s" to "%s"' % (self.sName, sNewTypeId));
2102 return False;
2103 return True;
2104
2105
2106 def setParavirtProvider(self, iProvider):
2107 """
2108 Sets a paravirtualisation provider.
2109 Returns the True on success, False on failure (logged).
2110 """
2111 try:
2112 self.o.machine.paravirtProvider = iProvider
2113 except:
2114 reporter.errorXcpt('Unable to set paravirtualisation provider "%s"' % (iProvider,))
2115 return False;
2116 return True;
2117
2118
2119
2120 #
2121 # IConsole wrappers.
2122 #
2123
2124 def powerOff(self, fFudgeOnFailure = True):
2125 """
2126 Powers off the VM.
2127
2128 Returns True on success.
2129 Returns False on IConsole::powerDown() failure.
2130 Returns None if the progress object returns failure.
2131 """
2132 try:
2133 oProgress = self.o.console.powerDown();
2134 except:
2135 reporter.logXcpt('IConsole::powerDown failed on %s' % (self.sName));
2136 if fFudgeOnFailure:
2137 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2138 self.waitForTask(1000); # fudge
2139 return False;
2140
2141 rc = self.oTstDrv.waitOnProgress(oProgress);
2142 if rc < 0:
2143 self.close();
2144 if fFudgeOnFailure:
2145 vbox.reportError(oProgress, 'powerDown for "%s" failed' % (self.sName));
2146 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2147 return None;
2148
2149 # Wait for the VM to really power off or we'll fail to open a new session to it.
2150 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2151 return self.waitForTask(30 * 1000); # fudge
2152
2153 def restoreSnapshot(self, oSnapshot, fFudgeOnFailure = True):
2154 """
2155 Restores the given snapshot.
2156
2157 Returns True on success.
2158 Returns False on IConsole::restoreSnapshot() failure.
2159 Returns None if the progress object returns failure.
2160 """
2161 try:
2162 oProgress = self.o.console.restoreSnapshot(oSnapshot);
2163 except:
2164 reporter.logXcpt('IConsole::restoreSnapshot failed on %s' % (self.sName));
2165 if fFudgeOnFailure:
2166 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2167 self.waitForTask(1000); # fudge
2168 return False;
2169
2170 rc = self.oTstDrv.waitOnProgress(oProgress);
2171 if rc < 0:
2172 self.close();
2173 if fFudgeOnFailure:
2174 vbox.reportError(oProgress, 'restoreSnapshot for "%s" failed' % (self.sName));
2175 return None;
2176
2177 return self.waitForTask(30 * 1000);
2178
2179 def deleteSnapshot(self, oSnapshot, fFudgeOnFailure = True, cMsTimeout = 30 * 1000):
2180 """
2181 Deletes the given snapshot merging the diff image into the base.
2182
2183 Returns True on success.
2184 Returns False on IConsole::deleteSnapshot() failure.
2185 """
2186 try:
2187 oProgressCom = self.o.console.deleteSnapshot(oSnapshot);
2188 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'Delete Snapshot %s' % (oSnapshot));
2189 oProgress.wait(cMsTimeout);
2190 oProgress.logResult();
2191 except:
2192 reporter.logXcpt('IConsole::deleteSnapshot failed on %s' % (self.sName));
2193 if fFudgeOnFailure:
2194 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2195 self.waitForTask(1000); # fudge
2196 return False;
2197
2198 return True;
2199
2200 def takeSnapshot(self, sName, sDescription = '', fPause = True, fFudgeOnFailure = True, cMsTimeout = 30 * 1000):
2201 """
2202 Takes a snapshot with the given name
2203
2204 Returns True on success.
2205 Returns False on IConsole::takeSnapshot() or VM state change failure.
2206 """
2207 try:
2208 if fPause is True \
2209 and self.oVM.state is vboxcon.MachineState_Running:
2210 self.o.console.pause();
2211
2212 oProgressCom = self.o.console.takeSnapshot(sName, sDescription);
2213 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'Take Snapshot %s' % (sName));
2214 oProgress.wait(cMsTimeout);
2215 oProgress.logResult();
2216 except:
2217 reporter.logXcpt('IConsole::takeSnapshot failed on %s' % (self.sName));
2218 if fFudgeOnFailure:
2219 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2220 self.waitForTask(1000); # fudge
2221 return False;
2222
2223 if fPause is True \
2224 and self.oVM.state is vboxcon.MachineState_Paused:
2225 self.o.console.resume();
2226
2227 return True;
2228
2229 def findSnapshot(self, sName):
2230 """
2231 Returns the snapshot object with the given name
2232
2233 Returns snapshot object on success.
2234 Returns None if there is no snapshot with the given name.
2235 """
2236 return self.oVM.findSnapshot(sName);
2237
2238 def takeScreenshot(self, sFilename, iScreenId=0):
2239 """
2240 Take screenshot from the given display and save it to specified file.
2241
2242 Returns True on success
2243 Returns False on failure.
2244 """
2245 try:
2246 if self.fpApiVer >= 4.4:
2247 iWidth, iHeight, _, _, _, _ = self.o.console.display.getScreenResolution(iScreenId)
2248 aPngData = self.o.console.display.takeScreenShotToArray(iScreenId, iWidth, iHeight,
2249 vboxcon.BitmapFormat_PNG)
2250 else:
2251 iWidth, iHeight, _, _, _ = self.o.console.display.getScreenResolution(iScreenId)
2252 aPngData = self.o.console.display.takeScreenShotPNGToArray(iScreenId, iWidth, iHeight)
2253 except:
2254 reporter.logXcpt("Unable to take screenshot")
2255 return False
2256
2257 oFile = open(sFilename, 'wb')
2258 oFile.write(aPngData)
2259 oFile.close()
2260
2261 return True
2262
2263 #
2264 # Other methods.
2265 #
2266
2267 def getPrimaryIp(self):
2268 """
2269 Tries to obtain the primary IP address of the guest via the guest
2270 properties.
2271
2272 Returns IP address on success.
2273 Returns empty string on failure.
2274 """
2275 sIpAddr = self.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2276 if vbox.isIpAddrValid(sIpAddr):
2277 return sIpAddr;
2278 return '';
2279
2280 def getPid(self):
2281 """
2282 Gets the process ID for the direct session unless it's ourselves.
2283 """
2284 if self.uPid is None and self.o is not None and self.fRemoteSession:
2285 try:
2286 if self.fpApiVer >= 4.2:
2287 uPid = self.o.machine.sessionPID;
2288 else:
2289 uPid = self.o.machine.sessionPid;
2290 if uPid != os.getpid() and uPid != 0xffffffff:
2291 self.uPid = uPid;
2292 except Exception, oXcpt:
2293 if vbox.ComError.equal(oXcpt, vbox.ComError.E_UNEXPECTED):
2294 try:
2295 if self.fpApiVer >= 4.2:
2296 uPid = self.oVM.sessionPID;
2297 else:
2298 uPid = self.oVM.sessionPid;
2299 if uPid != os.getpid() and uPid != 0xffffffff:
2300 self.uPid = uPid;
2301 except:
2302 reporter.log2Xcpt();
2303 else:
2304 reporter.log2Xcpt();
2305 if self.uPid is not None:
2306 reporter.log2('getPid: %u' % (self.uPid,));
2307 self.oTstDrv.pidFileAdd(self.uPid);
2308 return self.uPid;
2309
2310 def addLogsToReport(self, cReleaseLogs = 1):
2311 """
2312 Retrieves and adds the release and debug logs to the test report.
2313 """
2314 fRc = True;
2315
2316 # Add each of the requested release logs to the report.
2317 for iLog in range(0, cReleaseLogs):
2318 try:
2319 if self.fpApiVer >= 3.2:
2320 sLogFile = self.oVM.queryLogFilename(iLog);
2321 elif iLog > 0:
2322 sLogFile = '%s/VBox.log' % (self.oVM.logFolder,);
2323 else:
2324 sLogFile = '%s/VBox.log.%u' % (self.oVM.logFolder, iLog);
2325 except:
2326 reporter.logXcpt('iLog=%s' % (iLog,));
2327 fRc = False;
2328 else:
2329 if sLogFile is not None and sLogFile != '': # the None bit is for a 3.2.0 bug.
2330 reporter.addLogFile(sLogFile, 'log/release/vm', '%s #%u' % (self.sName, iLog),
2331 sAltName = '%s-%s' % (self.sName, os.path.basename(sLogFile),));
2332
2333 # Now for the hardened windows startup log.
2334 try:
2335 sLogFile = os.path.join(self.oVM.logFolder, 'VBoxStartup.log');
2336 except:
2337 reporter.logXcpt();
2338 fRc = False;
2339 else:
2340 if os.path.isfile(sLogFile):
2341 reporter.addLogFile(sLogFile, 'log/release/vm', '%s startup log' % (self.sName, ),
2342 sAltName = '%s-%s' % (self.sName, os.path.basename(sLogFile),));
2343
2344 # Now for the debug log.
2345 if self.sLogFile is not None and os.path.isfile(self.sLogFile):
2346 reporter.addLogFile(self.sLogFile, 'log/debug/vm', '%s debug' % (self.sName, ),
2347 sAltName = '%s-%s' % (self.sName, os.path.basename(self.sLogFile),));
2348
2349 return fRc;
2350
2351 def registerDerivedEventHandler(self, oSubClass, dArgs = None, fMustSucceed = True):
2352 """
2353 Create an instance of the given ConsoleEventHandlerBase sub-class and
2354 register it.
2355
2356 The new instance is returned on success. None is returned on error.
2357 """
2358
2359 # We need a console object.
2360 try:
2361 oConsole = self.o.console;
2362 except Exception, oXcpt:
2363 if fMustSucceed or vbox.ComError.notEqual(oXcpt, vbox.ComError.E_UNEXPECTED):
2364 reporter.errorXcpt('Failed to get ISession::console for "%s"' % (self.sName, ));
2365 return None;
2366
2367 # Add the base class arguments.
2368 dArgsCopy = dArgs.copy() if dArgs is not None else dict();
2369 dArgsCopy['oSession'] = self;
2370 dArgsCopy['oConsole'] = oConsole;
2371 sLogSuffix = 'on %s' % (self.sName,)
2372 return oSubClass.registerDerivedEventHandler(self.oVBoxMgr, self.fpApiVer, oSubClass, dArgsCopy,
2373 oConsole, 'IConsole', 'IConsoleCallback',
2374 fMustSucceed = fMustSucceed, sLogSuffix = sLogSuffix);
2375
2376 def enableVmmDevTestingPart(self, fEnabled, fEnableMMIO = False):
2377 """
2378 Enables the testing part of the VMMDev.
2379
2380 Returns True on success and False on failure. Error information is logged.
2381 """
2382 fRc = True;
2383 try:
2384 self.o.machine.setExtraData('VBoxInternal/Devices/VMMDev/0/Config/TestingEnabled',
2385 '1' if fEnabled else '');
2386 self.o.machine.setExtraData('VBoxInternal/Devices/VMMDev/0/Config/TestingMMIO',
2387 '1' if fEnableMMIO and fEnabled else '');
2388 except:
2389 reporter.errorXcpt('VM name "%s", fEnabled=%s' % (self.sName, fEnabled));
2390 fRc = False;
2391 else:
2392 reporter.log('set VMMDevTesting=%s for "%s"' % (fEnabled, self.sName));
2393 self.oTstDrv.processPendingEvents();
2394 return fRc;
2395
2396 #
2397 # Test eXecution Service methods.
2398 #
2399
2400 def txsConnectViaTcp(self, cMsTimeout = 10*60000, sIpAddr = None, sMacAddr = None, fNatForwardingForTxs = False):
2401 """
2402 Connects to the TXS using TCP/IP as transport. If no IP or MAC is
2403 addresses are specified, we'll get the IP from the guest additions.
2404
2405 Returns a TxsConnectTask object on success, None + log on failure.
2406 """
2407 # If the VM is configured with a NAT interface, connect to local host.
2408 fReversedSetup = False;
2409 fUseNatForTxs = False;
2410 if sIpAddr == None:
2411 try:
2412 oNic = self.oVM.getNetworkAdapter(0);
2413 if oNic.attachmentType == vboxcon.NetworkAttachmentType_NAT:
2414 fUseNatForTxs = True;
2415 except:
2416 reporter.errorXcpt();
2417 return None;
2418 if fUseNatForTxs:
2419 fReversedSetup = not fNatForwardingForTxs;
2420 sIpAddr = '127.0.0.1';
2421
2422 # Kick off the task.
2423 try:
2424 oTask = TxsConnectTask(self, cMsTimeout, sIpAddr, sMacAddr, fReversedSetup);
2425 except:
2426 reporter.errorXcpt();
2427 oTask = None;
2428 return oTask;
2429
2430 def txsTryConnectViaTcp(self, cMsTimeout, sHostname, fReversed = False):
2431 """
2432 Attempts to connect to a TXS instance.
2433
2434 Returns True if a connection was established, False if not (only grave
2435 failures are logged as errors).
2436
2437 Note! The timeout is more of a guideline...
2438 """
2439
2440 if sHostname is None or sHostname.strip() == '':
2441 raise base.GenError('Empty sHostname is not implemented yet');
2442
2443 oTxsSession = txsclient.tryOpenTcpSession(cMsTimeout, sHostname, fReversedSetup = fReversed,
2444 cMsIdleFudge = cMsTimeout / 2);
2445 if oTxsSession is None:
2446 return False;
2447
2448 # Wait for the connect task to time out.
2449 self.oTstDrv.addTask(oTxsSession);
2450 self.oTstDrv.processPendingEvents();
2451 oRc = self.oTstDrv.waitForTasks(cMsTimeout);
2452 self.oTstDrv.removeTask(oTxsSession);
2453 if oRc != oTxsSession:
2454 if oRc is not None:
2455 reporter.log('oRc=%s, expected %s' % (oRc, oTxsSession));
2456 self.oTstDrv.processPendingEvents();
2457 oTxsSession.cancelTask(); # this is synchronous
2458 return False;
2459
2460 # Check the status.
2461 reporter.log2('TxsSession is ready, isSuccess() -> %s.' % (oTxsSession.isSuccess(),));
2462 if not oTxsSession.isSuccess():
2463 return False;
2464
2465 reporter.log2('Disconnecting from TXS...');
2466 return oTxsSession.syncDisconnect();
2467
2468
2469
2470class TxsConnectTask(TdTaskBase):
2471 """
2472 Class that takes care of connecting to a VM.
2473 """
2474
2475 class TxsConnectTaskVBoxCallback(vbox.VirtualBoxEventHandlerBase):
2476 """ Class for looking for IPv4 address changes on interface 0."""
2477 def __init__(self, dArgs):
2478 vbox.VirtualBoxEventHandlerBase.__init__(self, dArgs); # pylint: disable=W0233
2479 self.oParentTask = dArgs['oParentTask'];
2480 self.sMachineId = dArgs['sMachineId'];
2481
2482 def onGuestPropertyChange(self, sMachineId, sName, sValue, sFlags):
2483 """Look for IP address."""
2484 reporter.log2('onGuestPropertyChange(,%s,%s,%s,%s)' % (sMachineId, sName, sValue, sFlags));
2485 if sMachineId == self.sMachineId \
2486 and sName == '/VirtualBox/GuestInfo/Net/0/V4/IP':
2487 self.oParentTask._setIp(sValue); # pylint: disable=W0212
2488
2489
2490 def __init__(self, oSession, cMsTimeout, sIpAddr, sMacAddr, fReversedSetup):
2491 TdTaskBase.__init__(self, utils.getCallerName());
2492 self.cMsTimeout = cMsTimeout;
2493 self.sIpAddr = None;
2494 self.sNextIpAddr = None;
2495 self.sMacAddr = sMacAddr;
2496 self.fReversedSetup = fReversedSetup;
2497 self.oVBox = oSession.oVBox;
2498 self.oVBoxEventHandler = None;
2499 self.oTxsSession = None;
2500 self.fpApiVer = oSession.fpApiVer;
2501
2502 # Skip things we don't implement.
2503 if sMacAddr is not None:
2504 reporter.error('TxsConnectTask does not implement sMacAddr yet');
2505 raise base.GenError();
2506
2507 reporter.log2('TxsConnectTask: sIpAddr=%s fReversedSetup=%s' % (sIpAddr, fReversedSetup))
2508 if fReversedSetup is True:
2509 self._openTcpSession(sIpAddr, fReversedSetup = True);
2510 elif sIpAddr is not None and sIpAddr.strip() != '':
2511 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2512 else:
2513 #
2514 # If we've got no IP address, register callbacks that listens for
2515 # the primary network adaptor of the VM to set a IPv4 guest prop.
2516 # Note! The order in which things are done here is kind of important.
2517 #
2518
2519 # 0. The caller zaps the property before starting the VM.
2520 #try:
2521 # oSession.delGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2522 #except:
2523 # reporter.logXcpt();
2524
2525 # 1. Register the callback / event listener object.
2526 dArgs = {'oParentTask':self, 'sMachineId':oSession.o.machine.id};
2527 self.oVBoxEventHandler = self.oVBox.registerDerivedEventHandler(self.TxsConnectTaskVBoxCallback, dArgs);
2528
2529 # 2. Query the guest properties.
2530 try:
2531 sIpAddr = oSession.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2532 except:
2533 reporter.errorXcpt('IMachine::getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP") failed');
2534 self._deregisterEventHandler();
2535 raise;
2536 else:
2537 if sIpAddr is not None:
2538 self._setIp(sIpAddr);
2539 # end __init__
2540
2541 def __del__(self):
2542 """ Make sure we deregister the callback. """
2543 self._deregisterEventHandler();
2544 return TdTaskBase.__del__(self);
2545
2546 def toString(self):
2547 return '<%s cMsTimeout=%s, sIpAddr=%s, sNextIpAddr=%s, sMacAddr=%s, fReversedSetup=%s,' \
2548 ' oTxsSession=%s oVBoxEventHandler=%s, oVBox=%s>' \
2549 % (TdTaskBase.toString(self), self.cMsTimeout, self.sIpAddr, self.sNextIpAddr, self.sMacAddr, self.fReversedSetup,
2550 self.oTxsSession, self.oVBoxEventHandler, self.oVBox);
2551
2552 def _deregisterEventHandler(self):
2553 """Deregisters the event handler."""
2554 fRc = True;
2555 if self.oVBoxEventHandler is not None:
2556 fRc = self.oVBoxEventHandler.unregister();
2557 self.oVBoxEventHandler = None;
2558 return fRc;
2559
2560 def _setIp(self, sIpAddr, fInitCall = False):
2561 """Called when we get an IP. Will create a TXS session and signal the task."""
2562 sIpAddr = sIpAddr.strip();
2563
2564 if sIpAddr is not None \
2565 and sIpAddr != '':
2566 if vbox.isIpAddrValid(sIpAddr) or fInitCall:
2567 try:
2568 for s in sIpAddr.split('.'):
2569 i = int(s);
2570 if str(i) != s:
2571 raise Exception();
2572 except:
2573 reporter.fatalXcpt();
2574 else:
2575 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2576 return None;
2577
2578 reporter.log('TxsConnectTask: Ignoring Bad ip "%s"' % (sIpAddr));
2579 else:
2580 reporter.log2('TxsConnectTask: Ignoring empty ip "%s"' % (sIpAddr));
2581 return None;
2582
2583 def _openTcpSession(self, sIpAddr, uPort = None, fReversedSetup = False, cMsIdleFudge = 0):
2584 """
2585 Calls txsclient.openTcpSession and switches our task to reflect the
2586 state of the subtask.
2587 """
2588 self.oCv.acquire();
2589 if self.oTxsSession is None:
2590 reporter.log2('_openTcpSession: sIpAddr=%s, uPort=%d, fReversedSetup=%s' % \
2591 (sIpAddr, uPort if uPort is not None else 0, fReversedSetup));
2592 self.sIpAddr = sIpAddr;
2593 self.oTxsSession = txsclient.openTcpSession(self.cMsTimeout, sIpAddr, uPort, \
2594 fReversedSetup, cMsIdleFudge);
2595 self.oTxsSession.setTaskOwner(self);
2596 else:
2597 self.sNextIpAddr = sIpAddr;
2598 reporter.log2('_openTcpSession: sNextIpAddr=%s' % (sIpAddr,));
2599 self.oCv.release();
2600 return None;
2601
2602 def notifyAboutReadyTask(self, oTxsSession):
2603 """
2604 Called by the TXS session task when it's done.
2605
2606 We'll signal the task completed or retry depending on the result.
2607 """
2608
2609 self.oCv.acquire();
2610
2611 # Disassociate ourselves with the session (avoid cyclic ref)
2612 oTxsSession.setTaskOwner(None);
2613 fSuccess = oTxsSession.isSuccess();
2614 if self.oTxsSession is not None:
2615 if not fSuccess:
2616 self.oTxsSession = None;
2617 if fSuccess and self.fReversedSetup:
2618 self.sIpAddr = oTxsSession.oTransport.sHostname;
2619 else:
2620 fSuccess = False;
2621
2622 # Signal done, or retry?
2623 if fSuccess \
2624 or self.fReversedSetup \
2625 or self.getAgeAsMs() >= self.cMsTimeout:
2626 self.signalTaskLocked();
2627 else:
2628 sIpAddr = self.sNextIpAddr if self.sNextIpAddr is not None else self.sIpAddr;
2629 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2630
2631 self.oCv.release();
2632 return True;
2633
2634 #
2635 # Public methods
2636 #
2637
2638 def getResult(self):
2639 """
2640 Returns the connected TXS session object on success.
2641 Returns None on failure or if the task has not yet completed.
2642 """
2643 self.oCv.acquire();
2644 oTxsSession = self.oTxsSession;
2645 self.oCv.release();
2646
2647 if oTxsSession is not None and not oTxsSession.isSuccess():
2648 oTxsSession = None;
2649 return oTxsSession;
2650
2651 def cancelTask(self):
2652 """ Cancels the task. """
2653 self.oCv.acquire();
2654 if not self.fSignalled:
2655 oTxsSession = self.oTxsSession;
2656 if oTxsSession is not None:
2657 self.oCv.release();
2658 oTxsSession.setTaskOwner(None);
2659 oTxsSession.cancelTask();
2660 oTxsSession.waitForTask(1000);
2661 self.oCv.acquire();
2662 self.signalTaskLocked();
2663 self.oCv.release();
2664 return True;
2665
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette