VirtualBox

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

Last change on this file since 54391 was 54122, checked in by vboxsync, 10 years ago

Fix pylint errors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 103.4 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: vboxwrappers.py 54122 2015-02-10 11:03:11Z vboxsync $
3# pylint: disable=C0302
4
5"""
6VirtualBox Wrapper Classes
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2014 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: 54122 $"
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 oHd = self.oVBox.createHardDisk(sFmt, sHd);
1716 oProgressXpcom = oHd.createBaseStorage(cb, (vboxcon.MediumVariant_Standard, ))
1717 oProgress = ProgressWrapper(oProgressXpcom, self.oVBoxMgr, self.oTstDrv, 'create disk %s' % (sHd));
1718 oProgress.wait();
1719 oProgress.logResult();
1720 except:
1721 reporter.errorXcpt('failed to create hd "%s"' % (sHd));
1722 return False;
1723
1724 fRc = True;
1725 try:
1726 if fImmutable:
1727 oHd.type = vboxcon.MediumType_Immutable;
1728 else:
1729 oHd.type = vboxcon.MediumType_Normal;
1730 except:
1731 if fImmutable:
1732 reporter.errorXcpt('failed to set hd "%s" immutable' % (sHd));
1733 else:
1734 reporter.errorXcpt('failed to set hd "%s" normal' % (sHd));
1735 fRc = False;
1736
1737 # Attach it.
1738 if fRc is True:
1739 try:
1740 if self.fpApiVer >= 4.0:
1741 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd);
1742 else:
1743 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_HardDisk, oHd.id);
1744 except:
1745 reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
1746 % (sController, iPort, iDevice, oHd.id, self.sName) );
1747 fRc = False;
1748 else:
1749 reporter.log('attached "%s" to %s' % (sHd, self.sName));
1750
1751 # Delete disk in case of an error
1752 if fRc is False:
1753 try:
1754 oProgressCom = oHd.deleteStorage();
1755 except:
1756 reporter.errorXcpt('deleteStorage() for disk %s failed' % (sHd,));
1757 else:
1758 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'delete disk %s' % (sHd));
1759 oProgress.wait();
1760 oProgress.logResult();
1761
1762 self.oTstDrv.processPendingEvents();
1763 return fRc;
1764
1765 def detachHd(self, sController = "IDE Controller", iPort = 0, iDevice = 0):
1766 """
1767 Detaches a HD, if attached, and returns a reference to it (IMedium).
1768
1769 In order to delete the detached medium, the caller must first save
1770 the changes made in this session.
1771
1772 Returns (fRc, oHd), where oHd is None unless fRc is True, and fRc is
1773 your standard success indicator. Error information is logged.
1774 """
1775
1776 # What's attached?
1777 try:
1778 oHd = self.o.machine.getMedium(sController, iPort, iDevice);
1779 except:
1780 if self.oVBoxMgr.xcptIsOurXcptKind() \
1781 and self.oVBoxMgr.xcptIsEqual(None, self.oVBoxMgr.constants.VBOX_E_OBJECT_NOT_FOUND):
1782 reporter.log('No HD attached (to %s %s:%s)' % (sController, iPort, iDevice));
1783 return (True, None);
1784 return (reporter.errorXcpt('Error getting media at port %s, device %s, on %s.'
1785 % (iPort, iDevice, sController)), None);
1786 # Detach it.
1787 try:
1788 self.o.machine.detachDevice(sController, iPort, iDevice);
1789 except:
1790 return (reporter.errorXcpt('detachDevice("%s",%s,%s) failed on "%s"' \
1791 % (sController, iPort, iDevice, self.sName) ), None);
1792 reporter.log('detached HD ("%s",%s,%s) from %s' % (sController, iPort, iDevice, self.sName));
1793 return (True, oHd);
1794
1795 def attachFloppy(self, sFloppy, sController = "Floppy Controller", iPort = 0, iDevice = 0):
1796 """
1797 Attaches a floppy image to a VM.
1798 Returns True on success and False on failure. Error information is logged.
1799 """
1800 # Input validation.
1801 ## @todo Fix this wrt to bootsector-xxx.img from the validationkit.zip.
1802 ##if not self.oTstDrv.isResourceFile(sFloppy):
1803 ## reporter.fatal('"%s" is not in the resource set' % (sFloppy));
1804 ## return None;
1805
1806 if not self.ensureControllerAttached(sController):
1807 return False;
1808
1809 # Find the floppy image, registering it if necessary (as immutable).
1810 sFullName = self.oTstDrv.getFullResourceName(sFloppy);
1811 try:
1812 oFloppy = self.oVBox.findFloppyImage(sFullName);
1813 except:
1814 try:
1815 if self.fpApiVer >= 4.1:
1816 oFloppy = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_Floppy, vboxcon.AccessMode_ReadOnly, False);
1817 elif self.fpApiVer >= 4.0:
1818 oFloppy = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_Floppy, vboxcon.AccessMode_ReadOnly);
1819 else:
1820 oFloppy = self.oVBox.openFloppyImage(sFullName, "");
1821 except:
1822 reporter.errorXcpt('failed to open floppy "%s"' % (sFullName));
1823 return False;
1824 ## @todo the following works but causes trouble below (asserts in main).
1825 #try:
1826 # oFloppy.type = vboxcon.MediumType_Immutable;
1827 #except:
1828 # reporter.errorXcpt('failed to make floppy "%s" immutable' % (sFullName));
1829 # return False;
1830
1831 # Attach it.
1832 fRc = True;
1833 try:
1834 if self.fpApiVer >= 4.0:
1835 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_Floppy, oFloppy);
1836 else:
1837 self.o.machine.attachDevice(sController, iPort, iDevice, vboxcon.DeviceType_Floppy, oFloppy.id);
1838 except:
1839 reporter.errorXcpt('attachDevice("%s",%s,%s,Floppy,"%s") failed on "%s"' \
1840 % (sController, iPort, iDevice, oFloppy.id, self.sName) );
1841 fRc = False;
1842 else:
1843 reporter.log('attached "%s" to %s' % (sFloppy, self.sName));
1844 self.oTstDrv.processPendingEvents();
1845 return fRc;
1846
1847 def setupNic(self, sType, sXXX):
1848 """
1849 Attaches a HD to a VM.
1850 Returns True on success and False on failure. Error information is logged.
1851 """
1852 if sType == "PCNet": enmType = vboxcon.NetworkAdapterType_Am79C973;
1853 elif sType == "PCNetOld": enmType = vboxcon.NetworkAdapterType_Am79C970A;
1854 elif sType == "E1000": enmType = vboxcon.NetworkAdapterType_I82545EM; # MT Server
1855 elif sType == "E1000Desk": enmType = vboxcon.NetworkAdapterType_I82540EM; # MT Desktop
1856 elif sType == "E1000Srv2": enmType = vboxcon.NetworkAdapterType_I82543GC; # T Server
1857 elif sType == "Virtio": enmType = vboxcon.NetworkAdapterType_Virtio;
1858 else:
1859 reporter.error('Invalid NIC type: "%s" (sXXX=%s)' % (sType, sXXX));
1860 return False;
1861 ## @todo Implement me!
1862 if enmType is not None: pass
1863 return True;
1864
1865 def setupPreferredConfig(self): # pylint: disable=R0914
1866 """
1867 Configures the VM according to the preferences of the guest type.
1868 """
1869 try:
1870 sOsTypeId = self.o.machine.OSTypeId;
1871 except:
1872 reporter.errorXcpt('failed to obtain the OSTypeId for "%s"' % (self.sName));
1873 return False;
1874
1875 try:
1876 oOsType = self.oVBox.getGuestOSType(sOsTypeId);
1877 except:
1878 reporter.errorXcpt('getGuestOSType("%s") failed for "%s"' % (sOsTypeId, self.sName));
1879 return False;
1880
1881 # get the attributes.
1882 try:
1883 #sFamilyId = oOsType.familyId;
1884 #f64Bit = oOsType.is64Bit;
1885 fIoApic = oOsType.recommendedIOAPIC;
1886 fVirtEx = oOsType.recommendedVirtEx;
1887 cMBRam = oOsType.recommendedRAM;
1888 cMBVRam = oOsType.recommendedVRAM;
1889 #cMBHdd = oOsType.recommendedHDD;
1890 eNicType = oOsType.adapterType;
1891 if self.fpApiVer >= 3.2:
1892 if self.fpApiVer >= 4.2:
1893 fPae = oOsType.recommendedPAE;
1894 fUsbHid = oOsType.recommendedUSBHID;
1895 fHpet = oOsType.recommendedHPET;
1896 eStorCtlType = oOsType.recommendedHDStorageController;
1897 else:
1898 fPae = oOsType.recommendedPae;
1899 fUsbHid = oOsType.recommendedUsbHid;
1900 fHpet = oOsType.recommendedHpet;
1901 eStorCtlType = oOsType.recommendedHdStorageController;
1902 eFirmwareType = oOsType.recommendedFirmware;
1903 else:
1904 fPae = False;
1905 fUsbHid = False;
1906 fHpet = False;
1907 eFirmwareType = -1;
1908 eStorCtlType = vboxcon.StorageControllerType_PIIX4;
1909 except:
1910 reporter.errorXcpt('exception reading IGuestOSType(%s) attribute' % (sOsTypeId));
1911 self.oTstDrv.processPendingEvents();
1912 return False;
1913 self.oTstDrv.processPendingEvents();
1914
1915 # Do the setting. Continue applying settings on error in case the
1916 # caller ignores the return code
1917 fRc = True;
1918 if not self.enableIoApic(fIoApic): fRc = False;
1919 if not self.enableVirtEx(fVirtEx): fRc = False;
1920 if not self.enablePae(fPae): fRc = False;
1921 if not self.setRamSize(cMBRam): fRc = False;
1922 if not self.setVRamSize(cMBVRam): fRc = False;
1923 if not self.setNicType(eNicType, 0): fRc = False;
1924 if self.fpApiVer >= 3.2:
1925 if not self.setFirmwareType(eFirmwareType): fRc = False;
1926 if not self.enableUsbHid(fUsbHid): fRc = False;
1927 if not self.enableHpet(fHpet): fRc = False;
1928 if eStorCtlType == vboxcon.StorageControllerType_PIIX3 \
1929 or eStorCtlType == vboxcon.StorageControllerType_PIIX4 \
1930 or eStorCtlType == vboxcon.StorageControllerType_ICH6:
1931 if not self.setStorageControllerType(eStorCtlType, "IDE Controller"):
1932 fRc = False;
1933
1934 return fRc;
1935
1936 def addUsbDeviceFilter(self, sName, sVendorId, sProductId):
1937 """
1938 Creates a USB device filter and inserts it into the VM.
1939 Returns True on success.
1940 Returns False on failure (logged).
1941 """
1942 fRc = True;
1943
1944 try:
1945 usbDevFilter = self.o.machine.USBDeviceFilters.createDeviceFilter(sName);
1946 usbDevFilter.active = True;
1947 usbDevFilter.vendorId = sVendorId;
1948 usbDevFilter.productId = sProductId;
1949 try:
1950 self.o.machine.USBDeviceFilters.insertDeviceFilter(0, usbDevFilter);
1951 except:
1952 reporter.errorXcpt('insertDeviceFilter(%s) failed on "%s"' \
1953 % (0, self.sName) );
1954 fRc = False;
1955 else:
1956 reporter.log('inserted USB device filter "%s" to %s' % (sName, self.sName));
1957 except:
1958 reporter.errorXcpt('createDeviceFilter("%s") failed on "%s"' \
1959 % (sName, self.sName) );
1960 fRc = False;
1961 return fRc;
1962
1963 def getGuestPropertyValue(self, sName):
1964 """
1965 Gets a guest property value.
1966 Returns the value on success, None on failure (logged).
1967 """
1968 try:
1969 sValue = self.o.machine.getGuestPropertyValue(sName);
1970 except:
1971 reporter.errorXcpt('IMachine::getGuestPropertyValue("%s") failed' % (sName));
1972 return None;
1973 return sValue;
1974
1975 def setGuestPropertyValue(self, sName, sValue):
1976 """
1977 Sets a guest property value.
1978 Returns the True on success, False on failure (logged).
1979 """
1980 try:
1981 self.o.machine.setGuestPropertyValue(sName, sValue);
1982 except:
1983 reporter.errorXcpt('IMachine::setGuestPropertyValue("%s","%s") failed' % (sName, sValue));
1984 return False;
1985 return True;
1986
1987 def delGuestPropertyValue(self, sName):
1988 """
1989 Deletes a guest property value.
1990 Returns the True on success, False on failure (logged).
1991 """
1992 try:
1993 oMachine = self.o.machine;
1994 if self.fpApiVer >= 4.2:
1995 oMachine.deleteGuestProperty(sName);
1996 else:
1997 oMachine.setGuestPropertyValue(sName, '');
1998 except:
1999 reporter.errorXcpt('Unable to delete guest property "%s"' % (sName,));
2000 return False;
2001 return True;
2002
2003 def setExtraData(self, sKey, sValue):
2004 """
2005 Sets extra data.
2006 Returns the True on success, False on failure (logged).
2007 """
2008 try:
2009 self.o.machine.setExtraData(sKey, sValue);
2010 except:
2011 reporter.errorXcpt('IMachine::setExtraData("%s","%s") failed' % (sKey, sValue));
2012 return False;
2013 return True;
2014
2015 def getExtraData(self, sKey):
2016 """
2017 Gets extra data.
2018 Returns value on success, None on failure.
2019 """
2020 try:
2021 sValue = self.o.machine.getExtraData(sKey)
2022 except:
2023 reporter.errorXcpt('IMachine::setExtraData("%s","%s") failed' % (sKey, sValue))
2024 return None
2025 return sValue
2026
2027 def setupTeleporter(self, fEnabled=True, uPort = 6500, sAddress = '', sPassword = ''):
2028 """
2029 Sets up the teleporter for the VM.
2030 Returns True on success, False on failure (logged).
2031 """
2032 try:
2033 self.o.machine.teleporterAddress = sAddress;
2034 self.o.machine.teleporterPort = uPort;
2035 self.o.machine.teleporterPassword = sPassword;
2036 self.o.machine.teleporterEnabled = fEnabled;
2037 except:
2038 reporter.errorXcpt('setupTeleporter(%s, %s, %s, %s)' % (fEnabled, sPassword, uPort, sAddress));
2039 return False;
2040 return True;
2041
2042 def enableTeleporter(self, fEnable=True):
2043 """
2044 Enables or disables the teleporter of the VM.
2045 Returns True on success, False on failure (logged).
2046 """
2047 try:
2048 self.o.machine.teleporterEnabled = fEnable;
2049 except:
2050 reporter.errorXcpt('IMachine::teleporterEnabled=%s failed' % (fEnable));
2051 return False;
2052 return True;
2053
2054 def teleport(self, sHostname = 'localhost', uPort = 6500, sPassword = 'password', cMsMaxDowntime = 250):
2055 """
2056 Wrapper around the IConsole::teleport() method.
2057 Returns a progress object on success, None on failure (logged).
2058 """
2059 reporter.log2('"%s"::teleport(%s,%s,%s,%s)...' % (self.sName, sHostname, uPort, sPassword, cMsMaxDowntime));
2060 try:
2061 oProgress = self.o.console.teleport(sHostname, uPort, sPassword, cMsMaxDowntime)
2062 except:
2063 reporter.errorXcpt('IConsole::teleport(%s,%s,%s,%s) failed' % (sHostname, uPort, sPassword, cMsMaxDowntime));
2064 return None;
2065 return ProgressWrapper(oProgress, self.oVBoxMgr, self.oTstDrv, 'teleport %s' % (self.sName,));
2066
2067 def getOsType(self):
2068 """
2069 Gets the IGuestOSType interface for the machine.
2070
2071 return IGuestOSType interface on success, None + errorXcpt on failure.
2072 No exceptions raised.
2073 """
2074 try:
2075 sOsTypeId = self.o.machine.OSTypeId;
2076 except:
2077 reporter.errorXcpt('failed to obtain the OSTypeId for "%s"' % (self.sName));
2078 return None;
2079
2080 try:
2081 oOsType = self.oVBox.getGuestOSType(sOsTypeId);
2082 except:
2083 reporter.errorXcpt('getGuestOSType("%s") failed for "%s"' % (sOsTypeId, self.sName));
2084 return None;
2085
2086 return oOsType;
2087
2088 def setOsType(self, sNewTypeId):
2089 """
2090 Changes the OS type.
2091
2092 returns True on success, False + errorXcpt on failure.
2093 No exceptions raised.
2094 """
2095 try:
2096 self.o.machine.OSTypeId = sNewTypeId;
2097 except:
2098 reporter.errorXcpt('failed to set the OSTypeId for "%s" to "%s"' % (self.sName, sNewTypeId));
2099 return False;
2100 return True;
2101
2102
2103 def setParavirtProvider(self, iProvider):
2104 """
2105 Sets a paravirtualisation provider.
2106 Returns the True on success, False on failure (logged).
2107 """
2108 try:
2109 self.o.machine.paravirtProvider = iProvider
2110 except:
2111 reporter.errorXcpt('Unable to set paravirtualisation provider "%s"' % (iProvider,))
2112 return False;
2113 return True;
2114
2115
2116
2117 #
2118 # IConsole wrappers.
2119 #
2120
2121 def powerOff(self, fFudgeOnFailure = True):
2122 """
2123 Powers off the VM.
2124
2125 Returns True on success.
2126 Returns False on IConsole::powerDown() failure.
2127 Returns None if the progress object returns failure.
2128 """
2129 try:
2130 oProgress = self.o.console.powerDown();
2131 except:
2132 reporter.logXcpt('IConsole::powerDown failed on %s' % (self.sName));
2133 if fFudgeOnFailure:
2134 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2135 self.waitForTask(1000); # fudge
2136 return False;
2137
2138 rc = self.oTstDrv.waitOnProgress(oProgress);
2139 if rc < 0:
2140 self.close();
2141 if fFudgeOnFailure:
2142 vbox.reportError(oProgress, 'powerDown for "%s" failed' % (self.sName));
2143 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2144 return None;
2145
2146 # Wait for the VM to really power off or we'll fail to open a new session to it.
2147 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2148 return self.waitForTask(30 * 1000); # fudge
2149
2150 def restoreSnapshot(self, oSnapshot, fFudgeOnFailure = True):
2151 """
2152 Restores the given snapshot.
2153
2154 Returns True on success.
2155 Returns False on IConsole::restoreSnapshot() failure.
2156 Returns None if the progress object returns failure.
2157 """
2158 try:
2159 oProgress = self.o.console.restoreSnapshot(oSnapshot);
2160 except:
2161 reporter.logXcpt('IConsole::restoreSnapshot failed on %s' % (self.sName));
2162 if fFudgeOnFailure:
2163 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2164 self.waitForTask(1000); # fudge
2165 return False;
2166
2167 rc = self.oTstDrv.waitOnProgress(oProgress);
2168 if rc < 0:
2169 self.close();
2170 if fFudgeOnFailure:
2171 vbox.reportError(oProgress, 'restoreSnapshot for "%s" failed' % (self.sName));
2172 return None;
2173
2174 return self.waitForTask(30 * 1000);
2175
2176 def deleteSnapshot(self, oSnapshot, fFudgeOnFailure = True, cMsTimeout = 30 * 1000):
2177 """
2178 Deletes the given snapshot merging the diff image into the base.
2179
2180 Returns True on success.
2181 Returns False on IConsole::deleteSnapshot() failure.
2182 """
2183 try:
2184 oProgressCom = self.o.console.deleteSnapshot(oSnapshot);
2185 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'Delete Snapshot %s' % (oSnapshot));
2186 oProgress.wait(cMsTimeout);
2187 oProgress.logResult();
2188 except:
2189 reporter.logXcpt('IConsole::deleteSnapshot failed on %s' % (self.sName));
2190 if fFudgeOnFailure:
2191 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2192 self.waitForTask(1000); # fudge
2193 return False;
2194
2195 return True;
2196
2197 def takeSnapshot(self, sName, sDescription = '', fPause = True, fFudgeOnFailure = True, cMsTimeout = 30 * 1000):
2198 """
2199 Takes a snapshot with the given name
2200
2201 Returns True on success.
2202 Returns False on IConsole::takeSnapshot() or VM state change failure.
2203 """
2204 try:
2205 if fPause is True \
2206 and self.oVM.state is vboxcon.MachineState_Running:
2207 self.o.console.pause();
2208
2209 oProgressCom = self.o.console.takeSnapshot(sName, sDescription);
2210 oProgress = ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oTstDrv, 'Take Snapshot %s' % (sName));
2211 oProgress.wait(cMsTimeout);
2212 oProgress.logResult();
2213 except:
2214 reporter.logXcpt('IConsole::takeSnapshot failed on %s' % (self.sName));
2215 if fFudgeOnFailure:
2216 self.oTstDrv.waitOnDirectSessionClose(self.oVM, 5000); # fudge
2217 self.waitForTask(1000); # fudge
2218 return False;
2219
2220 if fPause is True \
2221 and self.oVM.state is vboxcon.MachineState_Paused:
2222 self.o.console.resume();
2223
2224 return True;
2225
2226 def findSnapshot(self, sName):
2227 """
2228 Returns the snapshot object with the given name
2229
2230 Returns snapshot object on success.
2231 Returns None if there is no snapshot with the given name.
2232 """
2233 return self.oVM.findSnapshot(sName);
2234
2235 def takeScreenshot(self, sFilename, iScreenId=0):
2236 """
2237 Take screenshot from the given display and save it to specified file.
2238
2239 Returns True on success
2240 Returns False on failure.
2241 """
2242 try:
2243 if self.fpApiVer >= 4.4:
2244 iWidth, iHeight, _, _, _, _ = self.o.console.display.getScreenResolution(iScreenId)
2245 aPngData = self.o.console.display.takeScreenShotToArray(iScreenId, iWidth, iHeight,
2246 vboxcon.BitmapFormat_PNG)
2247 else:
2248 iWidth, iHeight, _, _, _ = self.o.console.display.getScreenResolution(iScreenId)
2249 aPngData = self.o.console.display.takeScreenShotPNGToArray(iScreenId, iWidth, iHeight)
2250 except:
2251 reporter.logXcpt("Unable to take screenshot")
2252 return False
2253
2254 oFile = open(sFilename, 'wb')
2255 oFile.write(aPngData)
2256 oFile.close()
2257
2258 return True
2259
2260 #
2261 # Other methods.
2262 #
2263
2264 def getPrimaryIp(self):
2265 """
2266 Tries to obtain the primary IP address of the guest via the guest
2267 properties.
2268
2269 Returns IP address on success.
2270 Returns empty string on failure.
2271 """
2272 sIpAddr = self.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2273 if vbox.isIpAddrValid(sIpAddr):
2274 return sIpAddr;
2275 return '';
2276
2277 def getPid(self):
2278 """
2279 Gets the process ID for the direct session unless it's ourselves.
2280 """
2281 if self.uPid is None and self.o is not None and self.fRemoteSession:
2282 try:
2283 if self.fpApiVer >= 4.2:
2284 uPid = self.o.machine.sessionPID;
2285 else:
2286 uPid = self.o.machine.sessionPid;
2287 if uPid != os.getpid() and uPid != 0xffffffff:
2288 self.uPid = uPid;
2289 except Exception, oXcpt:
2290 if vbox.ComError.equal(oXcpt, vbox.ComError.E_UNEXPECTED):
2291 try:
2292 if self.fpApiVer >= 4.2:
2293 uPid = self.oVM.sessionPID;
2294 else:
2295 uPid = self.oVM.sessionPid;
2296 if uPid != os.getpid() and uPid != 0xffffffff:
2297 self.uPid = uPid;
2298 except:
2299 reporter.log2Xcpt();
2300 else:
2301 reporter.log2Xcpt();
2302 if self.uPid is not None:
2303 reporter.log2('getPid: %u' % (self.uPid,));
2304 self.oTstDrv.pidFileAdd(self.uPid);
2305 return self.uPid;
2306
2307 def addLogsToReport(self, cReleaseLogs = 1):
2308 """
2309 Retrieves and adds the release and debug logs to the test report.
2310 """
2311 fRc = True;
2312
2313 # Add each of the requested release logs to the report.
2314 for iLog in range(0, cReleaseLogs):
2315 try:
2316 if self.fpApiVer >= 3.2:
2317 sLogFile = self.oVM.queryLogFilename(iLog);
2318 elif iLog > 0:
2319 sLogFile = '%s/VBox.log' % (self.oVM.logFolder,);
2320 else:
2321 sLogFile = '%s/VBox.log.%u' % (self.oVM.logFolder, iLog);
2322 except:
2323 reporter.logXcpt('iLog=%s' % (iLog,));
2324 fRc = False;
2325 else:
2326 if sLogFile is not None and sLogFile != '': # the None bit is for a 3.2.0 bug.
2327 reporter.addLogFile(sLogFile, 'log/release/vm', '%s #%u' % (self.sName, iLog),
2328 sAltName = '%s-%s' % (self.sName, os.path.basename(sLogFile),));
2329
2330 # Now for the hardened windows startup log.
2331 try:
2332 sLogFile = os.path.join(self.oVM.logFolder, 'VBoxStartup.log');
2333 except:
2334 reporter.logXcpt();
2335 fRc = False;
2336 else:
2337 if os.path.isfile(sLogFile):
2338 reporter.addLogFile(sLogFile, 'log/release/vm', '%s startup log' % (self.sName, ),
2339 sAltName = '%s-%s' % (self.sName, os.path.basename(sLogFile),));
2340
2341 # Now for the debug log.
2342 if self.sLogFile is not None and os.path.isfile(self.sLogFile):
2343 reporter.addLogFile(self.sLogFile, 'log/debug/vm', '%s debug' % (self.sName, ),
2344 sAltName = '%s-%s' % (self.sName, os.path.basename(self.sLogFile),));
2345
2346 return fRc;
2347
2348 def registerDerivedEventHandler(self, oSubClass, dArgs = None, fMustSucceed = True):
2349 """
2350 Create an instance of the given ConsoleEventHandlerBase sub-class and
2351 register it.
2352
2353 The new instance is returned on success. None is returned on error.
2354 """
2355
2356 # We need a console object.
2357 try:
2358 oConsole = self.o.console;
2359 except Exception, oXcpt:
2360 if fMustSucceed or vbox.ComError.notEqual(oXcpt, vbox.ComError.E_UNEXPECTED):
2361 reporter.errorXcpt('Failed to get ISession::console for "%s"' % (self.sName, ));
2362 return None;
2363
2364 # Add the base class arguments.
2365 dArgsCopy = dArgs.copy() if dArgs is not None else dict();
2366 dArgsCopy['oSession'] = self;
2367 dArgsCopy['oConsole'] = oConsole;
2368 sLogSuffix = 'on %s' % (self.sName,)
2369 return oSubClass.registerDerivedEventHandler(self.oVBoxMgr, self.fpApiVer, oSubClass, dArgsCopy,
2370 oConsole, 'IConsole', 'IConsoleCallback',
2371 fMustSucceed = fMustSucceed, sLogSuffix = sLogSuffix);
2372
2373 def enableVmmDevTestingPart(self, fEnabled, fEnableMMIO = False):
2374 """
2375 Enables the testing part of the VMMDev.
2376
2377 Returns True on success and False on failure. Error information is logged.
2378 """
2379 fRc = True;
2380 try:
2381 self.o.machine.setExtraData('VBoxInternal/Devices/VMMDev/0/Config/TestingEnabled',
2382 '1' if fEnabled else '');
2383 self.o.machine.setExtraData('VBoxInternal/Devices/VMMDev/0/Config/TestingMMIO',
2384 '1' if fEnableMMIO and fEnabled else '');
2385 except:
2386 reporter.errorXcpt('VM name "%s", fEnabled=%s' % (self.sName, fEnabled));
2387 fRc = False;
2388 else:
2389 reporter.log('set VMMDevTesting=%s for "%s"' % (fEnabled, self.sName));
2390 self.oTstDrv.processPendingEvents();
2391 return fRc;
2392
2393 #
2394 # Test eXecution Service methods.
2395 #
2396
2397 def txsConnectViaTcp(self, cMsTimeout = 10*60000, sIpAddr = None, sMacAddr = None, fNatForwardingForTxs = False):
2398 """
2399 Connects to the TXS using TCP/IP as transport. If no IP or MAC is
2400 addresses are specified, we'll get the IP from the guest additions.
2401
2402 Returns a TxsConnectTask object on success, None + log on failure.
2403 """
2404 # If the VM is configured with a NAT interface, connect to local host.
2405 fReversedSetup = False;
2406 fUseNatForTxs = False;
2407 if sIpAddr == None:
2408 try:
2409 oNic = self.oVM.getNetworkAdapter(0);
2410 if oNic.attachmentType == vboxcon.NetworkAttachmentType_NAT:
2411 fUseNatForTxs = True;
2412 except:
2413 reporter.errorXcpt();
2414 return None;
2415 if fUseNatForTxs:
2416 fReversedSetup = not fNatForwardingForTxs;
2417 sIpAddr = '127.0.0.1';
2418
2419 # Kick off the task.
2420 try:
2421 oTask = TxsConnectTask(self, cMsTimeout, sIpAddr, sMacAddr, fReversedSetup);
2422 except:
2423 reporter.errorXcpt();
2424 oTask = None;
2425 return oTask;
2426
2427 def txsTryConnectViaTcp(self, cMsTimeout, sHostname, fReversed = False):
2428 """
2429 Attempts to connect to a TXS instance.
2430
2431 Returns True if a connection was established, False if not (only grave
2432 failures are logged as errors).
2433
2434 Note! The timeout is more of a guideline...
2435 """
2436
2437 if sHostname is None or sHostname.strip() == '':
2438 raise base.GenError('Empty sHostname is not implemented yet');
2439
2440 oTxsSession = txsclient.tryOpenTcpSession(cMsTimeout, sHostname, fReversedSetup = fReversed,
2441 cMsIdleFudge = cMsTimeout / 2);
2442 if oTxsSession is None:
2443 return False;
2444
2445 # Wait for the connect task to time out.
2446 self.oTstDrv.addTask(oTxsSession);
2447 self.oTstDrv.processPendingEvents();
2448 oRc = self.oTstDrv.waitForTasks(cMsTimeout);
2449 self.oTstDrv.removeTask(oTxsSession);
2450 if oRc != oTxsSession:
2451 if oRc is not None:
2452 reporter.log('oRc=%s, expected %s' % (oRc, oTxsSession));
2453 self.oTstDrv.processPendingEvents();
2454 oTxsSession.cancelTask(); # this is synchronous
2455 return False;
2456
2457 # Check the status.
2458 reporter.log2('TxsSession is ready, isSuccess() -> %s.' % (oTxsSession.isSuccess(),));
2459 if not oTxsSession.isSuccess():
2460 return False;
2461
2462 reporter.log2('Disconnecting from TXS...');
2463 return oTxsSession.syncDisconnect();
2464
2465
2466
2467class TxsConnectTask(TdTaskBase):
2468 """
2469 Class that takes care of connecting to a VM.
2470 """
2471
2472 class TxsConnectTaskVBoxCallback(vbox.VirtualBoxEventHandlerBase):
2473 """ Class for looking for IPv4 address changes on interface 0."""
2474 def __init__(self, dArgs):
2475 vbox.VirtualBoxEventHandlerBase.__init__(self, dArgs); # pylint: disable=W0233
2476 self.oParentTask = dArgs['oParentTask'];
2477 self.sMachineId = dArgs['sMachineId'];
2478
2479 def onGuestPropertyChange(self, sMachineId, sName, sValue, sFlags):
2480 """Look for IP address."""
2481 reporter.log2('onGuestPropertyChange(,%s,%s,%s,%s)' % (sMachineId, sName, sValue, sFlags));
2482 if sMachineId == self.sMachineId \
2483 and sName == '/VirtualBox/GuestInfo/Net/0/V4/IP':
2484 self.oParentTask._setIp(sValue); # pylint: disable=W0212
2485
2486
2487 def __init__(self, oSession, cMsTimeout, sIpAddr, sMacAddr, fReversedSetup):
2488 TdTaskBase.__init__(self, utils.getCallerName());
2489 self.cMsTimeout = cMsTimeout;
2490 self.sIpAddr = None;
2491 self.sNextIpAddr = None;
2492 self.sMacAddr = sMacAddr;
2493 self.fReversedSetup = fReversedSetup;
2494 self.oVBox = oSession.oVBox;
2495 self.oVBoxEventHandler = None;
2496 self.oTxsSession = None;
2497 self.fpApiVer = oSession.fpApiVer;
2498
2499 # Skip things we don't implement.
2500 if sMacAddr is not None:
2501 reporter.error('TxsConnectTask does not implement sMacAddr yet');
2502 raise base.GenError();
2503
2504 reporter.log2('TxsConnectTask: sIpAddr=%s fReversedSetup=%s' % (sIpAddr, fReversedSetup))
2505 if fReversedSetup is True:
2506 self._openTcpSession(sIpAddr, fReversedSetup = True);
2507 elif sIpAddr is not None and sIpAddr.strip() != '':
2508 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2509 else:
2510 #
2511 # If we've got no IP address, register callbacks that listens for
2512 # the primary network adaptor of the VM to set a IPv4 guest prop.
2513 # Note! The order in which things are done here is kind of important.
2514 #
2515
2516 # 0. The caller zaps the property before starting the VM.
2517 #try:
2518 # oSession.delGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2519 #except:
2520 # reporter.logXcpt();
2521
2522 # 1. Register the callback / event listener object.
2523 dArgs = {'oParentTask':self, 'sMachineId':oSession.o.machine.id};
2524 self.oVBoxEventHandler = self.oVBox.registerDerivedEventHandler(self.TxsConnectTaskVBoxCallback, dArgs);
2525
2526 # 2. Query the guest properties.
2527 try:
2528 sIpAddr = oSession.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
2529 except:
2530 reporter.errorXcpt('IMachine::getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP") failed');
2531 self._deregisterEventHandler();
2532 raise;
2533 else:
2534 if sIpAddr is not None:
2535 self._setIp(sIpAddr);
2536 # end __init__
2537
2538 def __del__(self):
2539 """ Make sure we deregister the callback. """
2540 self._deregisterEventHandler();
2541 return TdTaskBase.__del__(self);
2542
2543 def toString(self):
2544 return '<%s cMsTimeout=%s, sIpAddr=%s, sNextIpAddr=%s, sMacAddr=%s, fReversedSetup=%s,' \
2545 ' oTxsSession=%s oVBoxEventHandler=%s, oVBox=%s>' \
2546 % (TdTaskBase.toString(self), self.cMsTimeout, self.sIpAddr, self.sNextIpAddr, self.sMacAddr, self.fReversedSetup,
2547 self.oTxsSession, self.oVBoxEventHandler, self.oVBox);
2548
2549 def _deregisterEventHandler(self):
2550 """Deregisters the event handler."""
2551 fRc = True;
2552 if self.oVBoxEventHandler is not None:
2553 fRc = self.oVBoxEventHandler.unregister();
2554 self.oVBoxEventHandler = None;
2555 return fRc;
2556
2557 def _setIp(self, sIpAddr, fInitCall = False):
2558 """Called when we get an IP. Will create a TXS session and signal the task."""
2559 sIpAddr = sIpAddr.strip();
2560
2561 if sIpAddr is not None \
2562 and sIpAddr != '':
2563 if vbox.isIpAddrValid(sIpAddr) or fInitCall:
2564 try:
2565 for s in sIpAddr.split('.'):
2566 i = int(s);
2567 if str(i) != s:
2568 raise Exception();
2569 except:
2570 reporter.fatalXcpt();
2571 else:
2572 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2573 return None;
2574
2575 reporter.log('TxsConnectTask: Ignoring Bad ip "%s"' % (sIpAddr));
2576 else:
2577 reporter.log2('TxsConnectTask: Ignoring empty ip "%s"' % (sIpAddr));
2578 return None;
2579
2580 def _openTcpSession(self, sIpAddr, uPort = None, fReversedSetup = False, cMsIdleFudge = 0):
2581 """
2582 Calls txsclient.openTcpSession and switches our task to reflect the
2583 state of the subtask.
2584 """
2585 self.oCv.acquire();
2586 if self.oTxsSession is None:
2587 reporter.log2('_openTcpSession: sIpAddr=%s, uPort=%d, fReversedSetup=%s' % \
2588 (sIpAddr, uPort if uPort is not None else 0, fReversedSetup));
2589 self.sIpAddr = sIpAddr;
2590 self.oTxsSession = txsclient.openTcpSession(self.cMsTimeout, sIpAddr, uPort, \
2591 fReversedSetup, cMsIdleFudge);
2592 self.oTxsSession.setTaskOwner(self);
2593 else:
2594 self.sNextIpAddr = sIpAddr;
2595 reporter.log2('_openTcpSession: sNextIpAddr=%s' % (sIpAddr,));
2596 self.oCv.release();
2597 return None;
2598
2599 def notifyAboutReadyTask(self, oTxsSession):
2600 """
2601 Called by the TXS session task when it's done.
2602
2603 We'll signal the task completed or retry depending on the result.
2604 """
2605
2606 self.oCv.acquire();
2607
2608 # Disassociate ourselves with the session (avoid cyclic ref)
2609 oTxsSession.setTaskOwner(None);
2610 fSuccess = oTxsSession.isSuccess();
2611 if self.oTxsSession is not None:
2612 if not fSuccess:
2613 self.oTxsSession = None;
2614 if fSuccess and self.fReversedSetup:
2615 self.sIpAddr = oTxsSession.oTransport.sHostname;
2616 else:
2617 fSuccess = False;
2618
2619 # Signal done, or retry?
2620 if fSuccess \
2621 or self.fReversedSetup \
2622 or self.getAgeAsMs() >= self.cMsTimeout:
2623 self.signalTaskLocked();
2624 else:
2625 sIpAddr = self.sNextIpAddr if self.sNextIpAddr is not None else self.sIpAddr;
2626 self._openTcpSession(sIpAddr, cMsIdleFudge = 5000);
2627
2628 self.oCv.release();
2629 return True;
2630
2631 #
2632 # Public methods
2633 #
2634
2635 def getResult(self):
2636 """
2637 Returns the connected TXS session object on success.
2638 Returns None on failure or if the task has not yet completed.
2639 """
2640 self.oCv.acquire();
2641 oTxsSession = self.oTxsSession;
2642 self.oCv.release();
2643
2644 if oTxsSession is not None and not oTxsSession.isSuccess():
2645 oTxsSession = None;
2646 return oTxsSession;
2647
2648 def cancelTask(self):
2649 """ Cancels the task. """
2650 self.oCv.acquire();
2651 if not self.fSignalled:
2652 oTxsSession = self.oTxsSession;
2653 if oTxsSession is not None:
2654 self.oCv.release();
2655 oTxsSession.setTaskOwner(None);
2656 oTxsSession.cancelTask();
2657 oTxsSession.waitForTask(1000);
2658 self.oCv.acquire();
2659 self.signalTaskLocked();
2660 self.oCv.release();
2661 return True;
2662
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