VirtualBox

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

Last change on this file since 54977 was 54938, checked in by vboxsync, 10 years ago

tdTreeDepth1.py: new test for checking if VBoxSVC can deal with the maximum medium and snapshot tree depths
vboxwrappers.py: add functionality for creating base and diff disk images, and allow attaching them to a VM

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