VirtualBox

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

Last change on this file since 70576 was 70576, checked in by vboxsync, 7 years ago

oops

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 160.3 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: vbox.py 70576 2018-01-13 14:29:04Z vboxsync $
3# pylint: disable=C0302
4
5"""
6VirtualBox Specific base testdriver.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2010-2017 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: 70576 $"
31
32
33# Standard Python imports.
34import os
35import platform
36import sys
37import threading
38import time
39import traceback
40import datetime
41
42# Figure out where the validation kit lives and make sure it's in the path.
43try: __file__
44except: __file__ = sys.argv[0];
45g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)));
46if g_ksValidationKitDir not in sys.path:
47 sys.path.append(g_ksValidationKitDir);
48
49# Validation Kit imports.
50from common import utils;
51from testdriver import base;
52from testdriver import btresolver;
53from testdriver import reporter;
54from testdriver import vboxcon;
55from testdriver import vboxtestvms;
56
57# Python 3 hacks:
58if sys.version_info[0] >= 3:
59 xrange = range; # pylint: disable=redefined-builtin,invalid-name
60 long = int; # pylint: disable=redefined-builtin,invalid-name
61
62#
63# Exception and Error Unification Hacks.
64# Note! This is pretty gross stuff. Be warned!
65# TODO: Find better ways of doing these things, preferrably in vboxapi.
66#
67
68ComException = None; # pylint: disable=C0103
69__fnComExceptionGetAttr__ = None; # pylint: disable=C0103
70
71def __MyDefaultGetAttr(oSelf, sName):
72 """ __getattribute__/__getattr__ default fake."""
73 try:
74 oAttr = oSelf.__dict__[sName];
75 except:
76 oAttr = dir(oSelf)[sName];
77 return oAttr;
78
79def __MyComExceptionGetAttr(oSelf, sName):
80 """ ComException.__getattr__ wrapper - both XPCOM and COM. """
81 try:
82 oAttr = __fnComExceptionGetAttr__(oSelf, sName);
83 except AttributeError:
84 if platform.system() == 'Windows':
85 if sName == 'errno':
86 oAttr = __fnComExceptionGetAttr__(oSelf, 'hresult');
87 elif sName == 'msg':
88 oAttr = __fnComExceptionGetAttr__(oSelf, 'strerror');
89 else:
90 raise;
91 else:
92 if sName == 'hresult':
93 oAttr = __fnComExceptionGetAttr__(oSelf, 'errno');
94 elif sName == 'strerror':
95 oAttr = __fnComExceptionGetAttr__(oSelf, 'msg');
96 elif sName == 'excepinfo':
97 oAttr = None;
98 elif sName == 'argerror':
99 oAttr = None;
100 else:
101 raise;
102 #print '__MyComExceptionGetAttr(,%s) -> "%s"' % (sName, oAttr);
103 return oAttr;
104
105def __deployExceptionHacks__(oNativeComExceptionClass):
106 """
107 Deploys the exception and error hacks that helps unifying COM and XPCOM
108 exceptions and errors.
109 """
110 global ComException # pylint: disable=C0103
111 global __fnComExceptionGetAttr__ # pylint: disable=C0103
112
113 # Hook up our attribute getter for the exception class (ASSUMES new-style).
114 if __fnComExceptionGetAttr__ is None:
115 try:
116 __fnComExceptionGetAttr__ = getattr(oNativeComExceptionClass, '__getattr__');
117 except:
118 try:
119 __fnComExceptionGetAttr__ = getattr(oNativeComExceptionClass, '__getattribute__');
120 except:
121 __fnComExceptionGetAttr__ = __MyDefaultGetAttr;
122 setattr(oNativeComExceptionClass, '__getattr__', __MyComExceptionGetAttr)
123
124 # Make the modified classes accessible (are there better ways to do this?)
125 ComException = oNativeComExceptionClass
126 return None;
127
128
129
130#
131# Utility functions.
132#
133
134def isIpAddrValid(sIpAddr):
135 """
136 Checks if a IPv4 address looks valid. This will return false for
137 localhost and similar.
138 Returns True / False.
139 """
140 if sIpAddr is None: return False;
141 if len(sIpAddr.split('.')) != 4: return False;
142 if sIpAddr.endswith('.0'): return False;
143 if sIpAddr.endswith('.255'): return False;
144 if sIpAddr.startswith('127.'): return False;
145 if sIpAddr.startswith('169.254.'): return False;
146 if sIpAddr.startswith('192.0.2.'): return False;
147 if sIpAddr.startswith('224.0.0.'): return False;
148 return True;
149
150def stringifyErrorInfo(oErrInfo):
151 """
152 Stringifies the error information in a IVirtualBoxErrorInfo object.
153
154 Returns string with error info.
155 """
156 try:
157 rc = oErrInfo.resultCode;
158 sText = oErrInfo.text;
159 sIid = oErrInfo.interfaceID;
160 sComponent = oErrInfo.component;
161 except:
162 sRet = 'bad error object (%s)?' % (oErrInfo,);
163 traceback.print_exc();
164 else:
165 sRet = 'rc=%s text="%s" IID=%s component=%s' % (ComError.toString(rc), sText, sIid, sComponent);
166 return sRet;
167
168def reportError(oErr, sText):
169 """
170 Report a VirtualBox error on oErr. oErr can be IVirtualBoxErrorInfo
171 or IProgress. Anything else is ignored.
172
173 Returns the same a reporter.error().
174 """
175 try:
176 oErrObj = oErr.errorInfo; # IProgress.
177 except:
178 oErrObj = oErr;
179 reporter.error(sText);
180 return reporter.error(stringifyErrorInfo(oErrObj));
181
182
183#
184# Classes
185#
186
187class ComError(object):
188 """
189 Unified COM and XPCOM status code repository.
190 This works more like a module than a class since it's replacing a module.
191 """
192
193 # The VBOX_E_XXX bits:
194 __VBOX_E_BASE = -2135228416;
195 VBOX_E_OBJECT_NOT_FOUND = __VBOX_E_BASE + 1;
196 VBOX_E_INVALID_VM_STATE = __VBOX_E_BASE + 2;
197 VBOX_E_VM_ERROR = __VBOX_E_BASE + 3;
198 VBOX_E_FILE_ERROR = __VBOX_E_BASE + 4;
199 VBOX_E_IPRT_ERROR = __VBOX_E_BASE + 5;
200 VBOX_E_PDM_ERROR = __VBOX_E_BASE + 6;
201 VBOX_E_INVALID_OBJECT_STATE = __VBOX_E_BASE + 7;
202 VBOX_E_HOST_ERROR = __VBOX_E_BASE + 8;
203 VBOX_E_NOT_SUPPORTED = __VBOX_E_BASE + 9;
204 VBOX_E_XML_ERROR = __VBOX_E_BASE + 10;
205 VBOX_E_INVALID_SESSION_STATE = __VBOX_E_BASE + 11;
206 VBOX_E_OBJECT_IN_USE = __VBOX_E_BASE + 12;
207 VBOX_E_DONT_CALL_AGAIN = __VBOX_E_BASE + 13;
208
209 # Reverse lookup table.
210 dDecimalToConst = {}; # pylint: disable=C0103
211
212 def __init__(self):
213 raise base.GenError('No instances, please');
214
215 @staticmethod
216 def copyErrors(oNativeComErrorClass):
217 """
218 Copy all error codes from oNativeComErrorClass to this class and
219 install compatability mappings.
220 """
221
222 # First, add the VBOX_E_XXX constants to dDecimalToConst.
223 for sAttr in dir(ComError):
224 if sAttr.startswith('VBOX_E'):
225 oAttr = getattr(ComError, sAttr);
226 ComError.dDecimalToConst[oAttr] = sAttr;
227
228 # Copy all error codes from oNativeComErrorClass to this class.
229 for sAttr in dir(oNativeComErrorClass):
230 if sAttr[0].isupper():
231 oAttr = getattr(oNativeComErrorClass, sAttr);
232 setattr(ComError, sAttr, oAttr);
233 if isinstance(oAttr, int):
234 ComError.dDecimalToConst[oAttr] = sAttr;
235
236 # Install mappings to the other platform.
237 if platform.system() == 'Windows':
238 ComError.NS_OK = ComError.S_OK;
239 ComError.NS_ERROR_FAILURE = ComError.E_FAIL;
240 ComError.NS_ERROR_ABORT = ComError.E_ABORT;
241 ComError.NS_ERROR_NULL_POINTER = ComError.E_POINTER;
242 ComError.NS_ERROR_NO_INTERFACE = ComError.E_NOINTERFACE;
243 ComError.NS_ERROR_INVALID_ARG = ComError.E_INVALIDARG;
244 ComError.NS_ERROR_OUT_OF_MEMORY = ComError.E_OUTOFMEMORY;
245 ComError.NS_ERROR_NOT_IMPLEMENTED = ComError.E_NOTIMPL;
246 ComError.NS_ERROR_UNEXPECTED = ComError.E_UNEXPECTED;
247 else:
248 ComError.E_ACCESSDENIED = -2147024891; # see VBox/com/defs.h
249 ComError.S_OK = ComError.NS_OK;
250 ComError.E_FAIL = ComError.NS_ERROR_FAILURE;
251 ComError.E_ABORT = ComError.NS_ERROR_ABORT;
252 ComError.E_POINTER = ComError.NS_ERROR_NULL_POINTER;
253 ComError.E_NOINTERFACE = ComError.NS_ERROR_NO_INTERFACE;
254 ComError.E_INVALIDARG = ComError.NS_ERROR_INVALID_ARG;
255 ComError.E_OUTOFMEMORY = ComError.NS_ERROR_OUT_OF_MEMORY;
256 ComError.E_NOTIMPL = ComError.NS_ERROR_NOT_IMPLEMENTED;
257 ComError.E_UNEXPECTED = ComError.NS_ERROR_UNEXPECTED;
258 ComError.DISP_E_EXCEPTION = -2147352567; # For COM compatability only.
259 return True;
260
261 @staticmethod
262 def getXcptResult(oXcpt):
263 """
264 Gets the result code for an exception.
265 Returns COM status code (or E_UNEXPECTED).
266 """
267 if platform.system() == 'Windows':
268 # The DISP_E_EXCEPTION + excptinfo fun needs checking up, only
269 # empirical info on it so far.
270 try:
271 hrXcpt = oXcpt.hresult;
272 except AttributeError:
273 hrXcpt = ComError.E_UNEXPECTED;
274 if hrXcpt == ComError.DISP_E_EXCEPTION and oXcpt.excepinfo is not None:
275 hrXcpt = oXcpt.excepinfo[5];
276 else:
277 try:
278 hrXcpt = oXcpt.errno;
279 except AttributeError:
280 hrXcpt = ComError.E_UNEXPECTED;
281 return hrXcpt;
282
283 @staticmethod
284 def equal(oXcpt, hr):
285 """
286 Checks if the ComException e is not equal to the COM status code hr.
287 This takes DISP_E_EXCEPTION & excepinfo into account.
288
289 This method can be used with any Exception derivate, however it will
290 only return True for classes similar to the two ComException variants.
291 """
292 if platform.system() == 'Windows':
293 # The DISP_E_EXCEPTION + excptinfo fun needs checking up, only
294 # empirical info on it so far.
295 try:
296 hrXcpt = oXcpt.hresult;
297 except AttributeError:
298 return False;
299 if hrXcpt == ComError.DISP_E_EXCEPTION and oXcpt.excepinfo is not None:
300 hrXcpt = oXcpt.excepinfo[5];
301 else:
302 try:
303 hrXcpt = oXcpt.errno;
304 except AttributeError:
305 return False;
306 return hrXcpt == hr;
307
308 @staticmethod
309 def notEqual(oXcpt, hr):
310 """
311 Checks if the ComException e is not equal to the COM status code hr.
312 See equal() for more details.
313 """
314 return not ComError.equal(oXcpt, hr)
315
316 @staticmethod
317 def toString(hr):
318 """
319 Converts the specified COM status code to a string.
320 """
321 try:
322 sStr = ComError.dDecimalToConst[int(hr)];
323 except KeyError:
324 hrLong = long(hr);
325 sStr = '%#x (%d)' % (hrLong, hrLong);
326 return sStr;
327
328
329class Build(object): # pylint: disable=R0903
330 """
331 A VirtualBox build.
332
333 Note! After dropping the installation of VBox from this code and instead
334 realizing that with the vboxinstall.py wrapper driver, this class is
335 of much less importance and contains unnecessary bits and pieces.
336 """
337
338 def __init__(self, oDriver, strInstallPath):
339 """
340 Construct a build object from a build file name and/or install path.
341 """
342 # Initialize all members first.
343 self.oDriver = oDriver;
344 self.sInstallPath = strInstallPath;
345 self.sSdkPath = None;
346 self.sSrcRoot = None;
347 self.sKind = None;
348 self.sDesignation = None;
349 self.sType = None;
350 self.sOs = None;
351 self.sArch = None;
352 self.sGuestAdditionsIso = None;
353
354 # Figure out the values as best we can.
355 if strInstallPath is None:
356 #
357 # Both parameters are None, which means we're falling back on a
358 # build in the development tree.
359 #
360 self.sKind = "development";
361
362 if self.sType is None:
363 self.sType = os.environ.get("KBUILD_TYPE", os.environ.get("BUILD_TYPE", "release"));
364 if self.sOs is None:
365 self.sOs = os.environ.get("KBUILD_TARGET", os.environ.get("BUILD_TARGET", oDriver.sHost));
366 if self.sArch is None:
367 self.sArch = os.environ.get("KBUILD_TARGET_ARCH", os.environ.get("BUILD_TARGET_ARCH", oDriver.sHostArch));
368
369 sOut = os.path.join('out', self.sOs + '.' + self.sArch, self.sType);
370 sSearch = os.environ.get('VBOX_TD_DEV_TREE', os.path.dirname(__file__)); # Env.var. for older trees or testboxscript.
371 sCandidat = None;
372 for i in range(0, 10): # pylint: disable=W0612
373 sBldDir = os.path.join(sSearch, sOut);
374 if os.path.isdir(sBldDir):
375 sCandidat = os.path.join(sBldDir, 'bin', 'VBoxSVC' + base.exeSuff());
376 if os.path.isfile(sCandidat):
377 self.sSdkPath = os.path.join(sBldDir, 'bin/sdk');
378 break;
379 sCandidat = os.path.join(sBldDir, 'dist/VirtualBox.app/Contents/MacOS/VBoxSVC');
380 if os.path.isfile(sCandidat):
381 self.sSdkPath = os.path.join(sBldDir, 'dist/sdk');
382 break;
383 sSearch = os.path.abspath(os.path.join(sSearch, '..'));
384 if sCandidat is None or not os.path.isfile(sCandidat):
385 raise base.GenError();
386 self.sInstallPath = os.path.abspath(os.path.dirname(sCandidat));
387 self.sSrcRoot = os.path.abspath(sSearch);
388
389 self.sDesignation = os.environ.get('TEST_BUILD_DESIGNATION', None);
390 if self.sDesignation is None:
391 try:
392 oFile = utils.openNoInherit(os.path.join(self.sSrcRoot, sOut, 'revision.kmk'), 'r');
393 except:
394 pass;
395 else:
396 s = oFile.readline();
397 oFile.close();
398 import re;
399 oMatch = re.search("VBOX_SVN_REV=(\\d+)", s);
400 if oMatch is not None:
401 self.sDesignation = oMatch.group(1);
402
403 if self.sDesignation is None:
404 self.sDesignation = 'XXXXX'
405 else:
406 #
407 # We've been pointed to an existing installation, this could be
408 # in the out dir of a svn checkout, untarred VBoxAll or a real
409 # installation directory.
410 #
411 self.sKind = "preinstalled";
412 self.sType = "release";
413 self.sOs = oDriver.sHost;
414 self.sArch = oDriver.sHostArch;
415 self.sInstallPath = os.path.abspath(strInstallPath);
416 self.sSdkPath = os.path.join(self.sInstallPath, 'sdk');
417 self.sSrcRoot = None;
418 self.sDesignation = os.environ.get('TEST_BUILD_DESIGNATION', 'XXXXX');
419 ## @todo Much more work is required here.
420
421 # Do some checks.
422 sVMMR0 = os.path.join(self.sInstallPath, 'VMMR0.r0');
423 if not os.path.isfile(sVMMR0) and utils.getHostOs() == 'solaris': # solaris is special.
424 sVMMR0 = os.path.join(self.sInstallPath, 'amd64' if utils.getHostArch() == 'amd64' else 'i386', 'VMMR0.r0');
425 if not os.path.isfile(sVMMR0):
426 raise base.GenError('%s is missing' % (sVMMR0,));
427
428 # Guest additions location is different on windows for some _stupid_ reason.
429 if self.sOs == 'win' and self.sKind != 'development':
430 self.sGuestAdditionsIso = '%s/VBoxGuestAdditions.iso' % (self.sInstallPath,);
431 elif self.sOs == 'darwin':
432 self.sGuestAdditionsIso = '%s/VBoxGuestAdditions.iso' % (self.sInstallPath,);
433 elif self.sOs == 'solaris':
434 self.sGuestAdditionsIso = '%s/VBoxGuestAdditions.iso' % (self.sInstallPath,);
435 else:
436 self.sGuestAdditionsIso = '%s/additions/VBoxGuestAdditions.iso' % (self.sInstallPath,);
437
438 # __init__ end;
439
440 def isDevBuild(self):
441 """ Returns True if it's development build (kind), otherwise False. """
442 return self.sKind == 'development';
443
444
445class EventHandlerBase(object):
446 """
447 Base class for both Console and VirtualBox event handlers.
448 """
449
450 def __init__(self, dArgs, fpApiVer, sName = None):
451 self.oVBoxMgr = dArgs['oVBoxMgr'];
452 self.oEventSrc = dArgs['oEventSrc']; # Console/VirtualBox for < 3.3
453 self.oListener = dArgs['oListener'];
454 self.fPassive = self.oListener != None;
455 self.sName = sName
456 self.fShutdown = False;
457 self.oThread = None;
458 self.fpApiVer = fpApiVer;
459 self.dEventNo2Name = {};
460 for sKey, iValue in self.oVBoxMgr.constants.all_values('VBoxEventType').items():
461 self.dEventNo2Name[iValue] = sKey;
462
463 def threadForPassiveMode(self):
464 """
465 The thread procedure for the event processing thread.
466 """
467 assert self.fPassive is not None;
468 while not self.fShutdown:
469 try:
470 oEvt = self.oEventSrc.getEvent(self.oListener, 500);
471 except:
472 if not self.oVBoxMgr.xcptIsDeadInterface(): reporter.logXcpt();
473 else: reporter.log('threadForPassiveMode/%s: interface croaked (ignored)' % (self.sName,));
474 break;
475 if oEvt:
476 self.handleEvent(oEvt);
477 if not self.fShutdown:
478 try:
479 self.oEventSrc.eventProcessed(self.oListener, oEvt);
480 except:
481 reporter.logXcpt();
482 break;
483 self.unregister(fWaitForThread = False);
484 return None;
485
486 def startThreadForPassiveMode(self):
487 """
488 Called when working in passive mode.
489 """
490 self.oThread = threading.Thread(target = self.threadForPassiveMode, \
491 args=(), name=('PAS-%s' % (self.sName,)));
492 self.oThread.setDaemon(True)
493 self.oThread.start();
494 return None;
495
496 def unregister(self, fWaitForThread = True):
497 """
498 Unregister the event handler.
499 """
500 fRc = False;
501 if not self.fShutdown:
502 self.fShutdown = True;
503
504 if self.oEventSrc is not None:
505 if self.fpApiVer < 3.3:
506 try:
507 self.oEventSrc.unregisterCallback(self.oListener);
508 fRc = True;
509 except:
510 reporter.errorXcpt('unregisterCallback failed on %s' % (self.oListener,));
511 else:
512 try:
513 self.oEventSrc.unregisterListener(self.oListener);
514 fRc = True;
515 except:
516 if self.oVBoxMgr.xcptIsDeadInterface():
517 reporter.log('unregisterListener failed on %s because of dead interface (%s)'
518 % (self.oListener, self.oVBoxMgr.xcptToString(),));
519 else:
520 reporter.errorXcpt('unregisterListener failed on %s' % (self.oListener,));
521
522 if self.oThread is not None \
523 and self.oThread != threading.current_thread():
524 self.oThread.join();
525 self.oThread = None;
526
527 _ = fWaitForThread;
528 return fRc;
529
530 def handleEvent(self, oEvt):
531 """
532 Compatibility wrapper that child classes implement.
533 """
534 _ = oEvt;
535 return None;
536
537 @staticmethod
538 def registerDerivedEventHandler(oVBoxMgr, fpApiVer, oSubClass, dArgsCopy,
539 oSrcParent, sSrcParentNm, sICallbackNm,
540 fMustSucceed = True, sLogSuffix = ''):
541 """
542 Registers the callback / event listener.
543 """
544 dArgsCopy['oVBoxMgr'] = oVBoxMgr;
545 dArgsCopy['oListener'] = None;
546 if fpApiVer < 3.3:
547 dArgsCopy['oEventSrc'] = oSrcParent;
548 try:
549 oRet = oVBoxMgr.createCallback(sICallbackNm, oSubClass, dArgsCopy);
550 except:
551 reporter.errorXcpt('%s::registerCallback(%s) failed%s' % (sSrcParentNm, oRet, sLogSuffix));
552 else:
553 try:
554 oSrcParent.registerCallback(oRet);
555 return oRet;
556 except Exception as oXcpt:
557 if fMustSucceed or ComError.notEqual(oXcpt, ComError.E_UNEXPECTED):
558 reporter.errorXcpt('%s::registerCallback(%s)%s' % (sSrcParentNm, oRet, sLogSuffix));
559 else:
560 fPassive = sys.platform == 'win32'; # or webservices.
561 try:
562 oEventSrc = oSrcParent.eventSource;
563 dArgsCopy['oEventSrc'] = oEventSrc;
564 if not fPassive:
565 oListener = oRet = oVBoxMgr.createListener(oSubClass, dArgsCopy);
566 else:
567 oListener = oEventSrc.createListener();
568 dArgsCopy['oListener'] = oListener;
569 oRet = oSubClass(dArgsCopy);
570 except:
571 reporter.errorXcpt('%s::eventSource.createListener(%s) failed%s' % (sSrcParentNm, oListener, sLogSuffix));
572 else:
573 try:
574 oEventSrc.registerListener(oListener, [vboxcon.VBoxEventType_Any], not fPassive);
575 except Exception as oXcpt:
576 if fMustSucceed or ComError.notEqual(oXcpt, ComError.E_UNEXPECTED):
577 reporter.errorXcpt('%s::eventSource.registerListener(%s) failed%s' \
578 % (sSrcParentNm, oListener, sLogSuffix));
579 else:
580 if not fPassive:
581 if sys.platform == 'win32':
582 from win32com.server.util import unwrap # pylint: disable=import-error
583 oRet = unwrap(oRet);
584 oRet.oListener = oListener;
585 else:
586 oRet.startThreadForPassiveMode();
587 return oRet;
588 return None;
589
590
591
592
593class ConsoleEventHandlerBase(EventHandlerBase):
594 """
595 Base class for handling IConsole events.
596
597 The class has IConsoleCallback (<=3.2) compatible callback methods which
598 the user can override as needed.
599
600 Note! This class must not inherit from object or we'll get type errors in VBoxPython.
601 """
602 def __init__(self, dArgs, sName = None):
603 self.oSession = dArgs['oSession'];
604 self.oConsole = dArgs['oConsole'];
605 if sName is None:
606 sName = self.oSession.sName;
607 EventHandlerBase.__init__(self, dArgs, self.oSession.fpApiVer, sName);
608
609
610 # pylint: disable=C0111,R0913,W0613
611 def onMousePointerShapeChange(self, fVisible, fAlpha, xHot, yHot, cx, cy, abShape):
612 reporter.log2('onMousePointerShapeChange/%s' % (self.sName));
613 def onMouseCapabilityChange(self, fSupportsAbsolute, *aArgs): # Extra argument was added in 3.2.
614 reporter.log2('onMouseCapabilityChange/%s' % (self.sName));
615 def onKeyboardLedsChange(self, fNumLock, fCapsLock, fScrollLock):
616 reporter.log2('onKeyboardLedsChange/%s' % (self.sName));
617 def onStateChange(self, eState):
618 reporter.log2('onStateChange/%s' % (self.sName));
619 def onAdditionsStateChange(self):
620 reporter.log2('onAdditionsStateChange/%s' % (self.sName));
621 def onNetworkAdapterChange(self, oNic):
622 reporter.log2('onNetworkAdapterChange/%s' % (self.sName));
623 def onSerialPortChange(self, oPort):
624 reporter.log2('onSerialPortChange/%s' % (self.sName));
625 def onParallelPortChange(self, oPort):
626 reporter.log2('onParallelPortChange/%s' % (self.sName));
627 def onStorageControllerChange(self):
628 reporter.log2('onStorageControllerChange/%s' % (self.sName));
629 def onMediumChange(self, attachment):
630 reporter.log2('onMediumChange/%s' % (self.sName));
631 def onCPUChange(self, iCpu, fAdd):
632 reporter.log2('onCPUChange/%s' % (self.sName));
633 def onVRDPServerChange(self):
634 reporter.log2('onVRDPServerChange/%s' % (self.sName));
635 def onRemoteDisplayInfoChange(self):
636 reporter.log2('onRemoteDisplayInfoChange/%s' % (self.sName));
637 def onUSBControllerChange(self):
638 reporter.log2('onUSBControllerChange/%s' % (self.sName));
639 def onUSBDeviceStateChange(self, oDevice, fAttached, oError):
640 reporter.log2('onUSBDeviceStateChange/%s' % (self.sName));
641 def onSharedFolderChange(self, fGlobal):
642 reporter.log2('onSharedFolderChange/%s' % (self.sName));
643 def onRuntimeError(self, fFatal, sErrId, sMessage):
644 reporter.log2('onRuntimeError/%s' % (self.sName));
645 def onCanShowWindow(self):
646 reporter.log2('onCanShowWindow/%s' % (self.sName));
647 return True
648 def onShowWindow(self):
649 reporter.log2('onShowWindow/%s' % (self.sName));
650 return None;
651 # pylint: enable=C0111,R0913,W0613
652
653 def handleEvent(self, oEvt):
654 """
655 Compatibility wrapper.
656 """
657 try:
658 oEvtBase = self.oVBoxMgr.queryInterface(oEvt, 'IEvent');
659 eType = oEvtBase.type;
660 except:
661 reporter.logXcpt();
662 return None;
663 if eType == vboxcon.VBoxEventType_OnRuntimeError:
664 try:
665 oEvtIt = self.oVBoxMgr.queryInterface(oEvtBase, 'IRuntimeErrorEvent');
666 return self.onRuntimeError(oEvtIt.fatal, oEvtIt.id, oEvtIt.message)
667 except:
668 reporter.logXcpt();
669 ## @todo implement the other events.
670 if eType != vboxcon.VBoxEventType_OnMousePointerShapeChanged:
671 if eType in self.dEventNo2Name:
672 reporter.log2('%s(%s)/%s' % (self.dEventNo2Name[eType], str(eType), self.sName));
673 else:
674 reporter.log2('%s/%s' % (str(eType), self.sName));
675 return None;
676
677
678class VirtualBoxEventHandlerBase(EventHandlerBase):
679 """
680 Base class for handling IVirtualBox events.
681
682 The class has IConsoleCallback (<=3.2) compatible callback methods which
683 the user can override as needed.
684
685 Note! This class must not inherit from object or we'll get type errors in VBoxPython.
686 """
687 def __init__(self, dArgs, sName = "emanon"):
688 self.oVBoxMgr = dArgs['oVBoxMgr'];
689 self.oVBox = dArgs['oVBox'];
690 EventHandlerBase.__init__(self, dArgs, self.oVBox.fpApiVer, sName);
691
692 # pylint: disable=C0111,W0613
693 def onMachineStateChange(self, sMachineId, eState):
694 pass;
695 def onMachineDataChange(self, sMachineId):
696 pass;
697 def onExtraDataCanChange(self, sMachineId, sKey, sValue):
698 # The COM bridge does tuples differently. Not very funny if you ask me... ;-)
699 if self.oVBoxMgr.type == 'MSCOM':
700 return '', 0, True;
701 return True, ''
702 def onExtraDataChange(self, sMachineId, sKey, sValue):
703 pass;
704 def onMediumRegistered(self, sMediumId, eMediumType, fRegistered):
705 pass;
706 def onMachineRegistered(self, sMachineId, fRegistered):
707 pass;
708 def onSessionStateChange(self, sMachineId, eState):
709 pass;
710 def onSnapshotTaken(self, sMachineId, sSnapshotId):
711 pass;
712 def onSnapshotDiscarded(self, sMachineId, sSnapshotId):
713 pass;
714 def onSnapshotChange(self, sMachineId, sSnapshotId):
715 pass;
716 def onGuestPropertyChange(self, sMachineId, sName, sValue, sFlags):
717 pass;
718 # pylint: enable=C0111,W0613
719
720 def handleEvent(self, oEvt):
721 """
722 Compatibility wrapper.
723 """
724 try:
725 oEvtBase = self.oVBoxMgr.queryInterface(oEvt, 'IEvent');
726 eType = oEvtBase.type;
727 except:
728 reporter.logXcpt();
729 return None;
730 if eType == vboxcon.VBoxEventType_OnMachineStateChanged:
731 try:
732 oEvtIt = self.oVBoxMgr.queryInterface(oEvtBase, 'IMachineStateChangedEvent');
733 return self.onMachineStateChange(oEvtIt.machineId, oEvtIt.state)
734 except:
735 reporter.logXcpt();
736 elif eType == vboxcon.VBoxEventType_OnGuestPropertyChanged:
737 try:
738 oEvtIt = self.oVBoxMgr.queryInterface(oEvtBase, 'IGuestPropertyChangedEvent');
739 return self.onGuestPropertyChange(oEvtIt.machineId, oEvtIt.name, oEvtIt.value, oEvtIt.flags);
740 except:
741 reporter.logXcpt();
742 ## @todo implement the other events.
743 if eType in self.dEventNo2Name:
744 reporter.log2('%s(%s)/%s' % (self.dEventNo2Name[eType], str(eType), self.sName));
745 else:
746 reporter.log2('%s/%s' % (str(eType), self.sName));
747 return None;
748
749
750class SessionConsoleEventHandler(ConsoleEventHandlerBase):
751 """
752 For catching machine state changes and waking up the task machinery at that point.
753 """
754 def __init__(self, dArgs):
755 ConsoleEventHandlerBase.__init__(self, dArgs);
756
757 def onMachineStateChange(self, sMachineId, eState): # pylint: disable=W0613
758 """ Just interrupt the wait loop here so it can check again. """
759 _ = sMachineId; _ = eState;
760 self.oVBoxMgr.interruptWaitEvents();
761
762 def onRuntimeError(self, fFatal, sErrId, sMessage):
763 reporter.log('onRuntimeError/%s: fFatal=%d sErrId=%s sMessage=%s' % (self.sName, fFatal, sErrId, sMessage));
764 oSession = self.oSession;
765 if oSession is not None: # paranoia
766 if sErrId == 'HostMemoryLow':
767 oSession.signalHostMemoryLow();
768 if sys.platform == 'win32':
769 from testdriver import winbase;
770 winbase.logMemoryStats();
771 oSession.signalTask();
772 self.oVBoxMgr.interruptWaitEvents();
773
774
775
776class TestDriver(base.TestDriver): # pylint: disable=R0902
777 """
778 This is the VirtualBox test driver.
779 """
780
781 def __init__(self):
782 base.TestDriver.__init__(self);
783 self.fImportedVBoxApi = False;
784 self.fpApiVer = 3.2;
785 self.oBuild = None;
786 self.oVBoxMgr = None;
787 self.oVBox = None;
788 self.aoRemoteSessions = [];
789 self.aoVMs = []; ## @todo not sure if this list will be of any use.
790 self.oTestVmManager = vboxtestvms.TestVmManager(self.sResourcePath);
791 self.oTestVmSet = vboxtestvms.TestVmSet();
792 self.sSessionTypeDef = 'headless';
793 self.sSessionType = self.sSessionTypeDef;
794 self.fEnableVrdp = True;
795 self.uVrdpBasePortDef = 6000;
796 self.uVrdpBasePort = self.uVrdpBasePortDef;
797 self.sDefBridgedNic = None;
798 self.fUseDefaultSvc = False;
799 self.sLogSelfGroups = '';
800 self.sLogSelfFlags = 'time';
801 self.sLogSelfDest = '';
802 self.sLogSessionGroups = '';
803 self.sLogSessionFlags = 'time';
804 self.sLogSessionDest = '';
805 self.sLogSvcGroups = '';
806 self.sLogSvcFlags = 'time';
807 self.sLogSvcDest = '';
808 self.sSelfLogFile = None;
809 self.sVBoxSvcLogFile = None;
810 self.oVBoxSvcProcess = None;
811 self.sVBoxSvcPidFile = None;
812 self.fVBoxSvcInDebugger = False;
813 self.sVBoxValidationKit = None;
814 self.sVBoxValidationKitIso = None;
815 self.sVBoxBootSectors = None;
816 self.fAlwaysUploadLogs = False;
817 self.fAlwaysUploadScreenshots = False;
818 self.fEnableDebugger = True;
819
820 # Quietly detect build and validation kit.
821 self._detectBuild(False);
822 self._detectValidationKit(False);
823
824 # Make sure all debug logs goes to the scratch area unless
825 # specified otherwise (more of this later on).
826 if 'VBOX_LOG_DEST' not in os.environ:
827 os.environ['VBOX_LOG_DEST'] = 'nodeny dir=%s' % (self.sScratchPath);
828
829
830 def _detectBuild(self, fQuiet = False):
831 """
832 This is used internally to try figure a locally installed build when
833 running tests manually.
834 """
835 if self.oBuild is not None:
836 return True;
837
838 # Try dev build first since that's where I'll be using it first...
839 if True is True:
840 try:
841 self.oBuild = Build(self, None);
842 return True;
843 except base.GenError:
844 pass;
845
846 # Try default installation locations.
847 if self.sHost == 'win':
848 sProgFiles = os.environ.get('ProgramFiles', 'C:\\Program Files');
849 asLocs = [
850 os.path.join(sProgFiles, 'Oracle', 'VirtualBox'),
851 os.path.join(sProgFiles, 'OracleVM', 'VirtualBox'),
852 os.path.join(sProgFiles, 'Sun', 'VirtualBox'),
853 ];
854 elif self.sHost == 'solaris':
855 asLocs = [ '/opt/VirtualBox-3.2', '/opt/VirtualBox-3.1', '/opt/VirtualBox-3.0', '/opt/VirtualBox' ];
856 elif self.sHost == 'darwin':
857 asLocs = [ '/Applications/VirtualBox.app/Contents/MacOS' ];
858 elif self.sHost == 'linux':
859 asLocs = [ '/opt/VirtualBox-3.2', '/opt/VirtualBox-3.1', '/opt/VirtualBox-3.0', '/opt/VirtualBox' ];
860 else:
861 asLocs = [ '/opt/VirtualBox' ];
862 if 'VBOX_INSTALL_PATH' in os.environ:
863 asLocs.insert(0, os.environ['VBOX_INSTALL_PATH']);
864
865 for sLoc in asLocs:
866 try:
867 self.oBuild = Build(self, sLoc);
868 return True;
869 except base.GenError:
870 pass;
871
872 if not fQuiet:
873 reporter.error('failed to find VirtualBox installation');
874 return False;
875
876 def _detectValidationKit(self, fQuiet = False):
877 """
878 This is used internally by the constructor to try locate an unzipped
879 VBox Validation Kit somewhere in the immediate proximity.
880 """
881 if self.sVBoxValidationKit is not None:
882 return True;
883
884 #
885 # Normally it's found where we're running from, which is the same as
886 # the script directly on the testboxes.
887 #
888 asCandidates = [self.sScriptPath, ];
889 if g_ksValidationKitDir not in asCandidates:
890 asCandidates.append(g_ksValidationKitDir);
891 if os.getcwd() not in asCandidates:
892 asCandidates.append(os.getcwd());
893 if self.oBuild is not None and self.oBuild.sInstallPath not in asCandidates:
894 asCandidates.append(self.oBuild.sInstallPath);
895
896 #
897 # When working out of the tree, we'll search the current directory
898 # as well as parent dirs.
899 #
900 for sDir in list(asCandidates):
901 for i in range(10):
902 sDir = os.path.dirname(sDir);
903 if sDir not in asCandidates:
904 asCandidates.append(sDir);
905
906 #
907 # Do the searching.
908 #
909 sCandidate = None;
910 for i, _ in enumerate(asCandidates):
911 sCandidate = asCandidates[i];
912 if os.path.isfile(os.path.join(sCandidate, 'VBoxValidationKit.iso')):
913 break;
914 sCandidate = os.path.join(sCandidate, 'validationkit');
915 if os.path.isfile(os.path.join(sCandidate, 'VBoxValidationKit.iso')):
916 break;
917 sCandidate = None;
918
919 fRc = sCandidate is not None;
920 if fRc is False:
921 if not fQuiet:
922 reporter.error('failed to find VBox Validation Kit installation (candidates: %s)' % (asCandidates,));
923 sCandidate = os.path.join(self.sScriptPath, 'validationkit'); # Don't leave the values as None.
924
925 #
926 # Set the member values.
927 #
928 self.sVBoxValidationKit = sCandidate;
929 self.sVBoxValidationKitIso = os.path.join(sCandidate, 'VBoxValidationKit.iso');
930 self.sVBoxBootSectors = os.path.join(sCandidate, 'bootsectors');
931 return fRc;
932
933 def _makeEnvironmentChanges(self):
934 """
935 Make the necessary VBox related environment changes.
936 Children not importing the VBox API should call this.
937 """
938 # Make sure we've got our own VirtualBox config and VBoxSVC (on XPCOM at least).
939 if not self.fUseDefaultSvc:
940 os.environ['VBOX_USER_HOME'] = os.path.join(self.sScratchPath, 'VBoxUserHome');
941 sUser = os.environ.get('USERNAME', os.environ.get('USER', os.environ.get('LOGNAME', 'unknown')));
942 os.environ['VBOX_IPC_SOCKETID'] = sUser + '-VBoxTest';
943 return True;
944
945 def importVBoxApi(self):
946 """
947 Import the 'vboxapi' module from the VirtualBox build we're using and
948 instantiate the two basic objects.
949
950 This will try detect an development or installed build if no build has
951 been associated with the driver yet.
952 """
953 if self.fImportedVBoxApi:
954 return True;
955
956 self._makeEnvironmentChanges();
957
958 # Do the detecting.
959 self._detectBuild();
960 if self.oBuild is None:
961 return False;
962
963 # Avoid crashing when loading the 32-bit module (or whatever it is that goes bang).
964 if self.oBuild.sArch == 'x86' \
965 and self.sHost == 'darwin' \
966 and platform.architecture()[0] == '64bit' \
967 and self.oBuild.sKind == 'development' \
968 and os.getenv('VERSIONER_PYTHON_PREFER_32_BIT') != 'yes':
969 reporter.log("WARNING: 64-bit python on darwin, 32-bit VBox development build => crash");
970 reporter.log("WARNING: bash-3.2$ /usr/bin/python2.5 ./testdriver");
971 reporter.log("WARNING: or");
972 reporter.log("WARNING: bash-3.2$ VERSIONER_PYTHON_PREFER_32_BIT=yes ./testdriver");
973 return False;
974
975 # Start VBoxSVC and load the vboxapi bits.
976 if self._startVBoxSVC() is True:
977 assert(self.oVBoxSvcProcess is not None);
978
979 sSavedSysPath = sys.path;
980 self._setupVBoxApi();
981 sys.path = sSavedSysPath;
982
983 # Adjust the default machine folder.
984 if self.fImportedVBoxApi and not self.fUseDefaultSvc and self.fpApiVer >= 4.0:
985 sNewFolder = os.path.join(self.sScratchPath, 'VBoxUserHome', 'Machines');
986 try:
987 self.oVBox.systemProperties.defaultMachineFolder = sNewFolder;
988 except:
989 self.fImportedVBoxApi = False;
990 self.oVBoxMgr = None;
991 self.oVBox = None;
992 reporter.logXcpt("defaultMachineFolder exception (sNewFolder=%s)" % (sNewFolder,));
993
994 # Kill VBoxSVC on failure.
995 if self.oVBoxMgr is None:
996 self._stopVBoxSVC();
997 else:
998 assert(self.oVBoxSvcProcess is None);
999 return self.fImportedVBoxApi;
1000
1001 def _startVBoxSVC(self): # pylint: disable=R0915
1002 """ Starts VBoxSVC. """
1003 assert(self.oVBoxSvcProcess is None);
1004
1005 # Setup vbox logging for VBoxSVC now and start it manually. This way
1006 # we can control both logging and shutdown.
1007 self.sVBoxSvcLogFile = '%s/VBoxSVC-debug.log' % (self.sScratchPath,);
1008 try: os.remove(self.sVBoxSvcLogFile);
1009 except: pass;
1010 os.environ['VBOX_LOG'] = self.sLogSvcGroups;
1011 os.environ['VBOX_LOG_FLAGS'] = '%s append' % (self.sLogSvcFlags,); # Append becuse of VBoxXPCOMIPCD.
1012 if self.sLogSvcDest:
1013 os.environ['VBOX_LOG_DEST'] = 'nodeny ' + self.sLogSvcDest;
1014 else:
1015 os.environ['VBOX_LOG_DEST'] = 'nodeny file=%s' % (self.sVBoxSvcLogFile,);
1016 os.environ['VBOXSVC_RELEASE_LOG_FLAGS'] = 'time append';
1017
1018 # Always leave a pid file behind so we can kill it during cleanup-before.
1019 self.sVBoxSvcPidFile = '%s/VBoxSVC.pid' % (self.sScratchPath,);
1020 fWritePidFile = True;
1021
1022 cMsFudge = 1;
1023 sVBoxSVC = '%s/VBoxSVC' % (self.oBuild.sInstallPath,); ## @todo .exe and stuff.
1024 if self.fVBoxSvcInDebugger:
1025 if self.sHost in ('darwin', 'freebsd', 'linux', 'solaris', ):
1026 # Start VBoxSVC in gdb in a new terminal.
1027 #sTerm = '/usr/bin/gnome-terminal'; - doesn't work, some fork+exec stuff confusing us.
1028 sTerm = '/usr/bin/xterm';
1029 if not os.path.isfile(sTerm): sTerm = '/usr/X11/bin/xterm';
1030 if not os.path.isfile(sTerm): sTerm = '/usr/X11R6/bin/xterm';
1031 if not os.path.isfile(sTerm): sTerm = '/usr/bin/xterm';
1032 if not os.path.isfile(sTerm): sTerm = 'xterm';
1033 sGdb = '/usr/bin/gdb';
1034 if not os.path.isfile(sGdb): sGdb = '/usr/local/bin/gdb';
1035 if not os.path.isfile(sGdb): sGdb = '/usr/sfw/bin/gdb';
1036 if not os.path.isfile(sGdb): sGdb = 'gdb';
1037 sGdbCmdLine = '%s --args %s --pidfile %s' % (sGdb, sVBoxSVC, self.sVBoxSvcPidFile);
1038 reporter.log('term="%s" gdb="%s"' % (sTerm, sGdbCmdLine));
1039 os.environ['SHELL'] = self.sOrgShell; # Non-working shell may cause gdb and/or the term problems.
1040 self.oVBoxSvcProcess = base.Process.spawnp(sTerm, sTerm, '-e', sGdbCmdLine);
1041 os.environ['SHELL'] = self.sOurShell;
1042 if self.oVBoxSvcProcess is not None:
1043 reporter.log('Press enter or return after starting VBoxSVC in the debugger...');
1044 sys.stdin.read(1);
1045 fWritePidFile = False;
1046
1047 elif self.sHost == 'win':
1048 sWinDbg = 'c:\\Program Files\\Debugging Tools for Windows\\windbg.exe';
1049 if not os.path.isfile(sWinDbg): sWinDbg = 'c:\\Program Files\\Debugging Tools for Windows (x64)\\windbg.exe';
1050 if not os.path.isfile(sWinDbg): sWinDbg = 'c:\\Programme\\Debugging Tools for Windows\\windbg.exe'; # Localization rulez! pylint: disable=C0301
1051 if not os.path.isfile(sWinDbg): sWinDbg = 'c:\\Programme\\Debugging Tools for Windows (x64)\\windbg.exe';
1052 if not os.path.isfile(sWinDbg): sWinDbg = 'windbg'; # WinDbg must be in the path; better than nothing.
1053 # Assume that everything WinDbg needs is defined using the environment variables.
1054 # See WinDbg help for more information.
1055 reporter.log('windbg="%s"' % (sWinDbg));
1056 self.oVBoxSvcProcess = base.Process.spawn(sWinDbg, sWinDbg, sVBoxSVC + base.exeSuff());
1057 if self.oVBoxSvcProcess is not None:
1058 reporter.log('Press enter or return after starting VBoxSVC in the debugger...');
1059 sys.stdin.read(1);
1060 fWritePidFile = False;
1061 ## @todo add a pipe interface similar to xpcom if feasible, i.e. if
1062 # we can get actual handle values for pipes in python.
1063
1064 else:
1065 reporter.error('Port me!');
1066 else: # Run without a debugger attached.
1067 if self.sHost in ('darwin', 'freebsd', 'linux', 'solaris', ):
1068 #
1069 # XPCOM - We can use a pipe to let VBoxSVC notify us when it's ready.
1070 #
1071 iPipeR, iPipeW = os.pipe();
1072 if hasattr(os, 'set_inheritable'):
1073 os.set_inheritable(iPipeW, True); # pylint: disable=no-member
1074 os.environ['NSPR_INHERIT_FDS'] = 'vboxsvc:startup-pipe:5:0x%x' % (iPipeW,);
1075 reporter.log2("NSPR_INHERIT_FDS=%s" % (os.environ['NSPR_INHERIT_FDS']));
1076
1077 self.oVBoxSvcProcess = base.Process.spawn(sVBoxSVC, sVBoxSVC, '--auto-shutdown'); # SIGUSR1 requirement.
1078 try: # Try make sure we get the SIGINT and not VBoxSVC.
1079 os.setpgid(self.oVBoxSvcProcess.getPid(), 0); # pylint: disable=E1101
1080 os.setpgid(0, 0); # pylint: disable=E1101
1081 except:
1082 reporter.logXcpt();
1083
1084 os.close(iPipeW);
1085 try:
1086 sResponse = os.read(iPipeR, 32);
1087 except:
1088 reporter.logXcpt();
1089 sResponse = None;
1090 os.close(iPipeR);
1091
1092 if hasattr(sResponse, 'decode'):
1093 sResponse = sResponse.decode('utf-8', 'ignore');
1094
1095 if sResponse is None or sResponse.strip() != 'READY':
1096 reporter.error('VBoxSVC failed starting up... (sResponse=%s)' % (sResponse,));
1097 if not self.oVBoxSvcProcess.wait(5000):
1098 self.oVBoxSvcProcess.terminate(2500);
1099 self.oVBoxSvcProcess.wait(5000);
1100 self.oVBoxSvcProcess = None;
1101
1102 elif self.sHost == 'win':
1103 #
1104 # Windows - Just fudge it for now.
1105 #
1106 cMsFudge = 2000;
1107 self.oVBoxSvcProcess = base.Process.spawn(sVBoxSVC, sVBoxSVC);
1108
1109 else:
1110 reporter.error('Port me!');
1111
1112 #
1113 # Enable automatic crash reporting if we succeeded.
1114 #
1115 if self.oVBoxSvcProcess is not None:
1116 self.oVBoxSvcProcess.enableCrashReporting('crash/report/svc', 'crash/dump/svc');
1117
1118 #
1119 # Fudge and pid file.
1120 #
1121 if self.oVBoxSvcProcess != None and not self.oVBoxSvcProcess.wait(cMsFudge):
1122 if fWritePidFile:
1123 iPid = self.oVBoxSvcProcess.getPid();
1124 try:
1125 oFile = utils.openNoInherit(self.sVBoxSvcPidFile, "w+");
1126 oFile.write('%s' % (iPid,));
1127 oFile.close();
1128 except:
1129 reporter.logXcpt('sPidFile=%s' % (self.sVBoxSvcPidFile,));
1130 reporter.log('VBoxSVC PID=%u' % (iPid,));
1131
1132 #
1133 # Finally add the task so we'll notice when it dies in a relatively timely manner.
1134 #
1135 self.addTask(self.oVBoxSvcProcess);
1136 else:
1137 self.oVBoxSvcProcess = None;
1138 try: os.remove(self.sVBoxSvcPidFile);
1139 except: pass;
1140
1141 return self.oVBoxSvcProcess != None;
1142
1143
1144 def _killVBoxSVCByPidFile(self, sPidFile):
1145 """ Kill a VBoxSVC given the pid from it's pid file. """
1146
1147 # Read the pid file.
1148 if not os.path.isfile(sPidFile):
1149 return False;
1150 try:
1151 oFile = utils.openNoInherit(sPidFile, "r");
1152 sPid = oFile.readline().strip();
1153 oFile.close();
1154 except:
1155 reporter.logXcpt('sPidfile=%s' % (sPidFile,));
1156 return False;
1157
1158 # Convert the pid to an integer and validate the range a little bit.
1159 try:
1160 iPid = long(sPid);
1161 except:
1162 reporter.logXcpt('sPidfile=%s sPid="%s"' % (sPidFile, sPid));
1163 return False;
1164 if iPid <= 0:
1165 reporter.log('negative pid - sPidfile=%s sPid="%s" iPid=%d' % (sPidFile, sPid, iPid));
1166 return False;
1167
1168 # Take care checking that it's VBoxSVC we're about to inhume.
1169 if base.processCheckPidAndName(iPid, "VBoxSVC") is not True:
1170 reporter.log('Ignoring stale VBoxSVC pid file (pid=%s)' % (iPid,));
1171 return False;
1172
1173 # Loop thru our different ways of getting VBoxSVC to terminate.
1174 for aHow in [ [ base.sendUserSignal1, 5000, 'Dropping VBoxSVC a SIGUSR1 hint...'], \
1175 [ base.processInterrupt, 5000, 'Dropping VBoxSVC a SIGINT hint...'], \
1176 [ base.processTerminate, 7500, 'VBoxSVC is still around, killing it...'] ]:
1177 reporter.log(aHow[2]);
1178 if aHow[0](iPid) is True:
1179 msStart = base.timestampMilli();
1180 while base.timestampMilli() - msStart < 5000 \
1181 and base.processExists(iPid):
1182 time.sleep(0.2);
1183
1184 fRc = not base.processExists(iPid);
1185 if fRc is True:
1186 break;
1187 if fRc:
1188 reporter.log('Successfully killed VBoxSVC (pid=%s)' % (iPid,));
1189 else:
1190 reporter.log('Failed to kill VBoxSVC (pid=%s)' % (iPid,));
1191 return fRc;
1192
1193 def _stopVBoxSVC(self):
1194 """
1195 Stops VBoxSVC. Try the polite way first.
1196 """
1197
1198 if self.oVBoxSvcProcess:
1199 self.removeTask(self.oVBoxSvcProcess);
1200 self.oVBoxSvcProcess.enableCrashReporting(None, None); # Disables it.
1201
1202 fRc = False;
1203 if self.oVBoxSvcProcess is not None \
1204 and not self.fVBoxSvcInDebugger:
1205 # by process object.
1206 if self.oVBoxSvcProcess.isRunning():
1207 reporter.log('Dropping VBoxSVC a SIGUSR1 hint...');
1208 if not self.oVBoxSvcProcess.sendUserSignal1() \
1209 or not self.oVBoxSvcProcess.wait(5000):
1210 reporter.log('Dropping VBoxSVC a SIGINT hint...');
1211 if not self.oVBoxSvcProcess.interrupt() \
1212 or not self.oVBoxSvcProcess.wait(5000):
1213 reporter.log('VBoxSVC is still around, killing it...');
1214 self.oVBoxSvcProcess.terminate();
1215 self.oVBoxSvcProcess.wait(7500);
1216 else:
1217 reporter.log('VBoxSVC is no longer running...');
1218 if not self.oVBoxSvcProcess.isRunning():
1219 self.oVBoxSvcProcess = None;
1220 else:
1221 # by pid file.
1222 self._killVBoxSVCByPidFile('%s/VBoxSVC.pid' % (self.sScratchPath,));
1223 return fRc;
1224
1225 def _setupVBoxApi(self):
1226 """
1227 Import and set up the vboxapi.
1228 The caller saves and restores sys.path.
1229 """
1230
1231 # Setup vbox logging for self (the test driver).
1232 self.sSelfLogFile = '%s/VBoxTestDriver.log' % (self.sScratchPath,);
1233 try: os.remove(self.sSelfLogFile);
1234 except: pass;
1235 os.environ['VBOX_LOG'] = self.sLogSelfGroups;
1236 os.environ['VBOX_LOG_FLAGS'] = '%s append' % (self.sLogSelfFlags, );
1237 if self.sLogSelfDest:
1238 os.environ['VBOX_LOG_DEST'] = 'nodeny ' + self.sLogSelfDest;
1239 else:
1240 os.environ['VBOX_LOG_DEST'] = 'nodeny file=%s' % (self.sSelfLogFile,);
1241 os.environ['VBOX_RELEASE_LOG_FLAGS'] = 'time append';
1242
1243 # Hack the sys.path + environment so the vboxapi can be found.
1244 sys.path.insert(0, self.oBuild.sInstallPath);
1245 if self.oBuild.sSdkPath is not None:
1246 sys.path.insert(0, os.path.join(self.oBuild.sSdkPath, 'installer'))
1247 sys.path.insert(1, os.path.join(self.oBuild.sSdkPath, 'install')); # stupid stupid windows installer!
1248 sys.path.insert(2, os.path.join(self.oBuild.sSdkPath, 'bindings', 'xpcom', 'python'))
1249 os.environ['VBOX_PROGRAM_PATH'] = self.oBuild.sInstallPath;
1250 reporter.log("sys.path: %s" % (sys.path));
1251
1252 try:
1253 from vboxapi import VirtualBoxManager # pylint: disable=import-error
1254 except:
1255 reporter.logXcpt('Error importing vboxapi');
1256 return False;
1257
1258 # Exception and error hacks.
1259 try:
1260 # pylint: disable=import-error
1261 if self.sHost == 'win':
1262 from pythoncom import com_error as NativeComExceptionClass # pylint: disable=E0611
1263 import winerror as NativeComErrorClass
1264 else:
1265 from xpcom import Exception as NativeComExceptionClass
1266 from xpcom import nsError as NativeComErrorClass
1267 # pylint: enable=import-error
1268 except:
1269 reporter.logXcpt('Error importing (XP)COM related stuff for exception hacks and errors');
1270 return False;
1271 __deployExceptionHacks__(NativeComExceptionClass)
1272 ComError.copyErrors(NativeComErrorClass);
1273
1274 # Create the manager.
1275 try:
1276 self.oVBoxMgr = VirtualBoxManager(None, None)
1277 except:
1278 self.oVBoxMgr = None;
1279 reporter.logXcpt('VirtualBoxManager exception');
1280 return False;
1281
1282 # Figure the API version.
1283 try:
1284 oVBox = self.oVBoxMgr.getVirtualBox();
1285
1286 try:
1287 sVer = oVBox.version;
1288 except:
1289 reporter.logXcpt('Failed to get VirtualBox version, assuming 4.0.0');
1290 sVer = "4.0.0";
1291 reporter.log("IVirtualBox.version=%s" % (sVer,));
1292
1293 # Convert the string to three integer values and check ranges.
1294 asVerComponents = sVer.split('.');
1295 try:
1296 sLast = asVerComponents[2].split('_')[0].split('r')[0];
1297 aiVerComponents = (int(asVerComponents[0]), int(asVerComponents[1]), int(sLast));
1298 except:
1299 raise base.GenError('Malformed version "%s"' % (sVer,));
1300 if aiVerComponents[0] < 3 or aiVerComponents[0] > 19:
1301 raise base.GenError('Malformed version "%s" - 1st component is out of bounds 3..19: %u'
1302 % (sVer, aiVerComponents[0]));
1303 if aiVerComponents[1] < 0 or aiVerComponents[1] > 9:
1304 raise base.GenError('Malformed version "%s" - 2nd component is out of bounds 0..9: %u'
1305 % (sVer, aiVerComponents[1]));
1306 if aiVerComponents[2] < 0 or aiVerComponents[2] > 99:
1307 raise base.GenError('Malformed version "%s" - 3rd component is out of bounds 0..99: %u'
1308 % (sVer, aiVerComponents[2]));
1309
1310 # Convert the three integers into a floating point value. The API is table witin a
1311 # x.y release, so the third component only indicates whether it's a stable or
1312 # development build of the next release.
1313 self.fpApiVer = aiVerComponents[0] + 0.1 * aiVerComponents[1];
1314 if aiVerComponents[2] >= 51:
1315 if self.fpApiVer not in [4.3, 3.2,]:
1316 self.fpApiVer += 0.1;
1317 else:
1318 self.fpApiVer += 1.1;
1319
1320 # Patch VBox manage to gloss over portability issues (error constants, etc).
1321 self._patchVBoxMgr();
1322
1323 # Wrap oVBox.
1324 from testdriver.vboxwrappers import VirtualBoxWrapper;
1325 self.oVBox = VirtualBoxWrapper(oVBox, self.oVBoxMgr, self.fpApiVer, self);
1326
1327 # Install the constant wrapping hack.
1328 vboxcon.goHackModuleClass.oVBoxMgr = self.oVBoxMgr; # VBoxConstantWrappingHack.
1329 vboxcon.fpApiVer = self.fpApiVer;
1330
1331 except:
1332 self.oVBoxMgr = None;
1333 self.oVBox = None;
1334 reporter.logXcpt("getVirtualBox / API version exception");
1335 return False;
1336
1337 # Done
1338 self.fImportedVBoxApi = True;
1339 reporter.log('Found version %s (%s)' % (self.fpApiVer, sVer));
1340 return True;
1341
1342 def _patchVBoxMgr(self):
1343 """
1344 Glosses over missing self.oVBoxMgr methods on older VBox versions.
1345 """
1346
1347 def _xcptGetResult(oSelf, oXcpt = None):
1348 """ See vboxapi. """
1349 _ = oSelf;
1350 if oXcpt is None: oXcpt = sys.exc_info()[1];
1351 if sys.platform == 'win32':
1352 import winerror; # pylint: disable=import-error
1353 hrXcpt = oXcpt.hresult;
1354 if hrXcpt == winerror.DISP_E_EXCEPTION:
1355 hrXcpt = oXcpt.excepinfo[5];
1356 else:
1357 hrXcpt = oXcpt.error;
1358 return hrXcpt;
1359
1360 def _xcptIsDeadInterface(oSelf, oXcpt = None):
1361 """ See vboxapi. """
1362 return oSelf.xcptGetStatus(oXcpt) in [
1363 0x80004004, -2147467260, # NS_ERROR_ABORT
1364 0x800706be, -2147023170, # NS_ERROR_CALL_FAILED (RPC_S_CALL_FAILED)
1365 0x800706ba, -2147023174, # RPC_S_SERVER_UNAVAILABLE.
1366 0x800706be, -2147023170, # RPC_S_CALL_FAILED.
1367 0x800706bf, -2147023169, # RPC_S_CALL_FAILED_DNE.
1368 0x80010108, -2147417848, # RPC_E_DISCONNECTED.
1369 0x800706b5, -2147023179, # RPC_S_UNKNOWN_IF
1370 ];
1371
1372 def _xcptIsOurXcptKind(oSelf, oXcpt = None):
1373 """ See vboxapi. """
1374 _ = oSelf;
1375 if oXcpt is None: oXcpt = sys.exc_info()[1];
1376 if sys.platform == 'win32':
1377 from pythoncom import com_error as NativeComExceptionClass # pylint: disable=import-error,E0611
1378 else:
1379 from xpcom import Exception as NativeComExceptionClass # pylint: disable=import-error
1380 return isinstance(oXcpt, NativeComExceptionClass);
1381
1382 def _xcptIsEqual(oSelf, oXcpt, hrStatus):
1383 """ See vboxapi. """
1384 hrXcpt = oSelf.xcptGetResult(oXcpt);
1385 return hrXcpt == hrStatus or hrXcpt == hrStatus - 0x100000000;
1386
1387 def _xcptToString(oSelf, oXcpt):
1388 """ See vboxapi. """
1389 _ = oSelf;
1390 if oXcpt is None: oXcpt = sys.exc_info()[1];
1391 return str(oXcpt);
1392
1393 # Add utilities found in newer vboxapi revision.
1394 if not hasattr(self.oVBoxMgr, 'xcptIsDeadInterface'):
1395 import types;
1396 self.oVBoxMgr.xcptGetResult = types.MethodType(_xcptGetResult, self.oVBoxMgr);
1397 self.oVBoxMgr.xcptIsDeadInterface = types.MethodType(_xcptIsDeadInterface, self.oVBoxMgr);
1398 self.oVBoxMgr.xcptIsOurXcptKind = types.MethodType(_xcptIsOurXcptKind, self.oVBoxMgr);
1399 self.oVBoxMgr.xcptIsEqual = types.MethodType(_xcptIsEqual, self.oVBoxMgr);
1400 self.oVBoxMgr.xcptToString = types.MethodType(_xcptToString, self.oVBoxMgr);
1401
1402
1403 def _teardownVBoxApi(self): # pylint: disable=too-many-statements
1404 """
1405 Drop all VBox object references and shutdown com/xpcom.
1406 """
1407 if not self.fImportedVBoxApi:
1408 return True;
1409 import gc;
1410
1411 # Drop all references we've have to COM objects.
1412 self.aoRemoteSessions = [];
1413 self.aoVMs = [];
1414 self.oVBoxMgr = None;
1415 self.oVBox = None;
1416 vboxcon.goHackModuleClass.oVBoxMgr = None; # VBoxConstantWrappingHack.
1417
1418 # Do garbage collection to try get rid of those objects.
1419 try:
1420 gc.collect();
1421 except:
1422 reporter.logXcpt();
1423 self.fImportedVBoxApi = False;
1424
1425 # Check whether the python is still having any COM objects/interfaces around.
1426 cVBoxMgrs = 0;
1427 aoObjsLeftBehind = [];
1428 if self.sHost == 'win':
1429 import pythoncom; # pylint: disable=import-error
1430 try:
1431 cIfs = pythoncom._GetInterfaceCount(); # pylint: disable=no-member,protected-access
1432 cObjs = pythoncom._GetGatewayCount(); # pylint: disable=no-member,protected-access
1433 if cObjs == 0 and cIfs == 0:
1434 reporter.log('_teardownVBoxApi: no interfaces or objects left behind.');
1435 else:
1436 reporter.log('_teardownVBoxApi: Python COM still has %s objects and %s interfaces...' % ( cObjs, cIfs));
1437
1438 from win32com.client import DispatchBaseClass; # pylint: disable=import-error
1439 for oObj in gc.get_objects():
1440 if isinstance(oObj, DispatchBaseClass):
1441 reporter.log('_teardownVBoxApi: %s' % (oObj,));
1442 aoObjsLeftBehind.append(oObj);
1443 elif utils.getObjectTypeName(oObj) == 'VirtualBoxManager':
1444 reporter.log('_teardownVBoxApi: %s' % (oObj,));
1445 cVBoxMgrs += 1;
1446 aoObjsLeftBehind.append(oObj);
1447 oObj = None;
1448 except:
1449 reporter.logXcpt();
1450
1451 # If not being used, we can safely uninitialize COM.
1452 if cIfs == 0 and cObjs == 0 and cVBoxMgrs == 0 and len(aoObjsLeftBehind) == 0:
1453 reporter.log('_teardownVBoxApi: Calling CoUninitialize...');
1454 try: pythoncom.CoUninitialize(); # pylint: disable=no-member
1455 except: reporter.logXcpt();
1456 else:
1457 reporter.log('_teardownVBoxApi: Returned from CoUninitialize.');
1458 else:
1459 try:
1460 # XPCOM doesn't crash and burn like COM if you shut it down with interfaces and objects around.
1461 # Also, it keeps a number of internal objects and interfaces around to do its job, so shutting
1462 # it down before we go looking for dangling interfaces is more or less required.
1463 from xpcom import _xpcom as _xpcom; # pylint: disable=import-error
1464 hrc = _xpcom.DeinitCOM();
1465 cIfs = _xpcom._GetInterfaceCount(); # pylint: disable=W0212
1466 cObjs = _xpcom._GetGatewayCount(); # pylint: disable=W0212
1467
1468 if cObjs == 0 and cIfs == 0:
1469 reporter.log('_teardownVBoxApi: No XPCOM interfaces or objects active. (hrc=%#x)' % (hrc,));
1470 else:
1471 reporter.log('_teardownVBoxApi: %s XPCOM objects and %s interfaces still around! (hrc=%#x)'
1472 % (cObjs, cIfs, hrc));
1473 if hasattr(_xpcom, '_DumpInterfaces'):
1474 try: _xpcom._DumpInterfaces(); # pylint: disable=W0212
1475 except: reporter.logXcpt('_teardownVBoxApi: _DumpInterfaces failed');
1476
1477 from xpcom.client import Component; # pylint: disable=import-error
1478 for oObj in gc.get_objects():
1479 if isinstance(oObj, Component):
1480 reporter.log('_teardownVBoxApi: %s' % (oObj,));
1481 aoObjsLeftBehind.append(oObj);
1482 if utils.getObjectTypeName(oObj) == 'VirtualBoxManager':
1483 reporter.log('_teardownVBoxApi: %s' % (oObj,));
1484 cVBoxMgrs += 1;
1485 aoObjsLeftBehind.append(oObj);
1486 oObj = None;
1487 except:
1488 reporter.logXcpt();
1489
1490 # Try get the referrers to (XP)COM interfaces and objects that was left behind.
1491 for iObj in range(len(aoObjsLeftBehind)): # pylint: disable=consider-using-enumerate
1492 try:
1493 aoReferrers = gc.get_referrers(aoObjsLeftBehind[iObj]);
1494 reporter.log('_teardownVBoxApi: Found %u referrers to %s:' % (len(aoReferrers), aoObjsLeftBehind[iObj],));
1495 for oReferrer in aoReferrers:
1496 oMyFrame = sys._getframe(0); # pylint: disable=protected-access
1497 if oReferrer is oMyFrame:
1498 reporter.log('_teardownVBoxApi: - frame of this function');
1499 elif oReferrer is aoObjsLeftBehind:
1500 reporter.log('_teardownVBoxApi: - aoObjsLeftBehind');
1501 else:
1502 fPrinted = False;
1503 if isinstance(oReferrer, dict) or isinstance(oReferrer, list) or isinstance(oReferrer, tuple):
1504 try:
1505 aoSubReferreres = gc.get_referrers(oReferrer);
1506 for oSubRef in aoSubReferreres:
1507 if not isinstance(oSubRef, list) \
1508 and not isinstance(oSubRef, dict) \
1509 and oSubRef is not oMyFrame \
1510 and oSubRef is not aoSubReferreres:
1511 reporter.log('_teardownVBoxApi: - %s :: %s:'
1512 % (utils.getObjectTypeName(oSubRef), utils.getObjectTypeName(oReferrer)));
1513 fPrinted = True;
1514 break;
1515 del aoSubReferreres;
1516 except:
1517 reporter.logXcpt('subref');
1518 if not fPrinted:
1519 reporter.log('_teardownVBoxApi: - %s:' % (utils.getObjectTypeName(oReferrer),));
1520 try:
1521 import pprint;
1522 for sLine in pprint.pformat(oReferrer, width = 130).split('\n'):
1523 reporter.log('_teardownVBoxApi: %s' % (sLine,));
1524 except:
1525 reporter.log('_teardownVBoxApi: %s' % (oReferrer,));
1526 except:
1527 reporter.logXcpt();
1528 del aoObjsLeftBehind;
1529
1530 # Force garbage collection again, just for good measure.
1531 try:
1532 gc.collect();
1533 time.sleep(0.5); # fudge factor
1534 except:
1535 reporter.logXcpt();
1536 return True;
1537
1538 def _powerOffAllVms(self):
1539 """
1540 Tries to power off all running VMs.
1541 """
1542 for oSession in self.aoRemoteSessions:
1543 uPid = oSession.getPid();
1544 if uPid is not None:
1545 reporter.log('_powerOffAllVms: PID is %s for %s, trying to kill it.' % (uPid, oSession.sName,));
1546 base.processKill(uPid);
1547 else:
1548 reporter.log('_powerOffAllVms: No PID for %s' % (oSession.sName,));
1549 oSession.close();
1550 return None;
1551
1552
1553
1554 #
1555 # Build type, OS and arch getters.
1556 #
1557
1558 def getBuildType(self):
1559 """
1560 Get the build type.
1561 """
1562 if not self._detectBuild():
1563 return 'release';
1564 return self.oBuild.sType;
1565
1566 def getBuildOs(self):
1567 """
1568 Get the build OS.
1569 """
1570 if not self._detectBuild():
1571 return self.sHost;
1572 return self.oBuild.sOs;
1573
1574 def getBuildArch(self):
1575 """
1576 Get the build arch.
1577 """
1578 if not self._detectBuild():
1579 return self.sHostArch;
1580 return self.oBuild.sArch;
1581
1582 def getGuestAdditionsIso(self):
1583 """
1584 Get the path to the guest addition iso.
1585 """
1586 if not self._detectBuild():
1587 return None;
1588 return self.oBuild.sGuestAdditionsIso;
1589
1590 #
1591 # Override everything from the base class so the testdrivers don't have to
1592 # check whether we have overridden a method or not.
1593 #
1594
1595 def showUsage(self):
1596 rc = base.TestDriver.showUsage(self);
1597 reporter.log('');
1598 reporter.log('Generic VirtualBox Options:');
1599 reporter.log(' --vbox-session-type <type>');
1600 reporter.log(' Sets the session type. Typical values are: gui, headless, sdl');
1601 reporter.log(' Default: %s' % (self.sSessionTypeDef));
1602 reporter.log(' --vrdp, --no-vrdp');
1603 reporter.log(' Enables VRDP, ports starting at 6000');
1604 reporter.log(' Default: --vrdp');
1605 reporter.log(' --vrdp-base-port <port>');
1606 reporter.log(' Sets the base for VRDP port assignments.');
1607 reporter.log(' Default: %s' % (self.uVrdpBasePortDef));
1608 reporter.log(' --vbox-default-bridged-nic <interface>');
1609 reporter.log(' Sets the default interface for bridged networking.');
1610 reporter.log(' Default: autodetect');
1611 reporter.log(' --vbox-use-svc-defaults');
1612 reporter.log(' Use default locations and files for VBoxSVC. This is useful');
1613 reporter.log(' for automatically configuring the test VMs for debugging.');
1614 reporter.log(' --vbox-self-log');
1615 reporter.log(' The VBox logger group settings for the testdriver.');
1616 reporter.log(' --vbox-self-log-flags');
1617 reporter.log(' The VBox logger flags settings for the testdriver.');
1618 reporter.log(' --vbox-self-log-dest');
1619 reporter.log(' The VBox logger destination settings for the testdriver.');
1620 reporter.log(' --vbox-session-log');
1621 reporter.log(' The VM session logger group settings.');
1622 reporter.log(' --vbox-session-log-flags');
1623 reporter.log(' The VM session logger flags.');
1624 reporter.log(' --vbox-session-log-dest');
1625 reporter.log(' The VM session logger destination settings.');
1626 reporter.log(' --vbox-svc-log');
1627 reporter.log(' The VBoxSVC logger group settings.');
1628 reporter.log(' --vbox-svc-log-flags');
1629 reporter.log(' The VBoxSVC logger flag settings.');
1630 reporter.log(' --vbox-svc-log-dest');
1631 reporter.log(' The VBoxSVC logger destination settings.');
1632 reporter.log(' --vbox-log');
1633 reporter.log(' The VBox logger group settings for everyone.');
1634 reporter.log(' --vbox-log-flags');
1635 reporter.log(' The VBox logger flags settings for everyone.');
1636 reporter.log(' --vbox-log-dest');
1637 reporter.log(' The VBox logger destination settings for everyone.');
1638 reporter.log(' --vbox-svc-debug');
1639 reporter.log(' Start VBoxSVC in a debugger');
1640 reporter.log(' --vbox-always-upload-logs');
1641 reporter.log(' Whether to always upload log files, or only do so on failure.');
1642 reporter.log(' --vbox-always-upload-screenshots');
1643 reporter.log(' Whether to always upload final screen shots, or only do so on failure.');
1644 reporter.log(' --vbox-debugger, --no-vbox-debugger');
1645 reporter.log(' Enables the VBox debugger, port at 5000');
1646 reporter.log(' Default: --vbox-debugger');
1647 if self.oTestVmSet is not None:
1648 self.oTestVmSet.showUsage();
1649 return rc;
1650
1651 def parseOption(self, asArgs, iArg): # pylint: disable=R0915
1652 if asArgs[iArg] == '--vbox-session-type':
1653 iArg += 1;
1654 if iArg >= len(asArgs):
1655 raise base.InvalidOption('The "--vbox-session-type" takes an argument');
1656 self.sSessionType = asArgs[iArg];
1657 elif asArgs[iArg] == '--vrdp':
1658 self.fEnableVrdp = True;
1659 elif asArgs[iArg] == '--no-vrdp':
1660 self.fEnableVrdp = False;
1661 elif asArgs[iArg] == '--vrdp-base-port':
1662 iArg += 1;
1663 if iArg >= len(asArgs):
1664 raise base.InvalidOption('The "--vrdp-base-port" takes an argument');
1665 try: self.uVrdpBasePort = int(asArgs[iArg]);
1666 except: raise base.InvalidOption('The "--vrdp-base-port" value "%s" is not a valid integer' % (asArgs[iArg],));
1667 if self.uVrdpBasePort <= 0 or self.uVrdpBasePort >= 65530:
1668 raise base.InvalidOption('The "--vrdp-base-port" value "%s" is not in the valid range (1..65530)'
1669 % (asArgs[iArg],));
1670 elif asArgs[iArg] == '--vbox-default-bridged-nic':
1671 iArg += 1;
1672 if iArg >= len(asArgs):
1673 raise base.InvalidOption('The "--vbox-default-bridged-nic" takes an argument');
1674 self.sDefBridgedNic = asArgs[iArg];
1675 elif asArgs[iArg] == '--vbox-use-svc-defaults':
1676 self.fUseDefaultSvc = True;
1677 elif asArgs[iArg] == '--vbox-self-log':
1678 iArg += 1;
1679 if iArg >= len(asArgs):
1680 raise base.InvalidOption('The "--vbox-self-log" takes an argument');
1681 self.sLogSelfGroups = asArgs[iArg];
1682 elif asArgs[iArg] == '--vbox-self-log-flags':
1683 iArg += 1;
1684 if iArg >= len(asArgs):
1685 raise base.InvalidOption('The "--vbox-self-log-flags" takes an argument');
1686 self.sLogSelfFlags = asArgs[iArg];
1687 elif asArgs[iArg] == '--vbox-self-log-dest':
1688 iArg += 1;
1689 if iArg >= len(asArgs):
1690 raise base.InvalidOption('The "--vbox-self-log-dest" takes an argument');
1691 self.sLogSelfDest = asArgs[iArg];
1692 elif asArgs[iArg] == '--vbox-session-log':
1693 iArg += 1;
1694 if iArg >= len(asArgs):
1695 raise base.InvalidOption('The "--vbox-session-log" takes an argument');
1696 self.sLogSessionGroups = asArgs[iArg];
1697 elif asArgs[iArg] == '--vbox-session-log-flags':
1698 iArg += 1;
1699 if iArg >= len(asArgs):
1700 raise base.InvalidOption('The "--vbox-session-log-flags" takes an argument');
1701 self.sLogSessionFlags = asArgs[iArg];
1702 elif asArgs[iArg] == '--vbox-session-log-dest':
1703 iArg += 1;
1704 if iArg >= len(asArgs):
1705 raise base.InvalidOption('The "--vbox-session-log-dest" takes an argument');
1706 self.sLogSessionDest = asArgs[iArg];
1707 elif asArgs[iArg] == '--vbox-svc-log':
1708 iArg += 1;
1709 if iArg >= len(asArgs):
1710 raise base.InvalidOption('The "--vbox-svc-log" takes an argument');
1711 self.sLogSvcGroups = asArgs[iArg];
1712 elif asArgs[iArg] == '--vbox-svc-log-flags':
1713 iArg += 1;
1714 if iArg >= len(asArgs):
1715 raise base.InvalidOption('The "--vbox-svc-log-flags" takes an argument');
1716 self.sLogSvcFlags = asArgs[iArg];
1717 elif asArgs[iArg] == '--vbox-svc-log-dest':
1718 iArg += 1;
1719 if iArg >= len(asArgs):
1720 raise base.InvalidOption('The "--vbox-svc-log-dest" takes an argument');
1721 self.sLogSvcDest = asArgs[iArg];
1722 elif asArgs[iArg] == '--vbox-log':
1723 iArg += 1;
1724 if iArg >= len(asArgs):
1725 raise base.InvalidOption('The "--vbox-log" takes an argument');
1726 self.sLogSelfGroups = asArgs[iArg];
1727 self.sLogSessionGroups = asArgs[iArg];
1728 self.sLogSvcGroups = asArgs[iArg];
1729 elif asArgs[iArg] == '--vbox-log-flags':
1730 iArg += 1;
1731 if iArg >= len(asArgs):
1732 raise base.InvalidOption('The "--vbox-svc-flags" takes an argument');
1733 self.sLogSelfFlags = asArgs[iArg];
1734 self.sLogSessionFlags = asArgs[iArg];
1735 self.sLogSvcFlags = asArgs[iArg];
1736 elif asArgs[iArg] == '--vbox-log-dest':
1737 iArg += 1;
1738 if iArg >= len(asArgs):
1739 raise base.InvalidOption('The "--vbox-log-dest" takes an argument');
1740 self.sLogSelfDest = asArgs[iArg];
1741 self.sLogSessionDest = asArgs[iArg];
1742 self.sLogSvcDest = asArgs[iArg];
1743 elif asArgs[iArg] == '--vbox-svc-debug':
1744 self.fVBoxSvcInDebugger = True;
1745 elif asArgs[iArg] == '--vbox-always-upload-logs':
1746 self.fAlwaysUploadLogs = True;
1747 elif asArgs[iArg] == '--vbox-always-upload-screenshots':
1748 self.fAlwaysUploadScreenshots = True;
1749 elif asArgs[iArg] == '--vbox-debugger':
1750 self.fEnableDebugger = True;
1751 elif asArgs[iArg] == '--no-vbox-debugger':
1752 self.fEnableDebugger = False;
1753 else:
1754 # Relevant for selecting VMs to test?
1755 if self.oTestVmSet is not None:
1756 iRc = self.oTestVmSet.parseOption(asArgs, iArg);
1757 if iRc != iArg:
1758 return iRc;
1759
1760 # Hand it to the base class.
1761 return base.TestDriver.parseOption(self, asArgs, iArg);
1762 return iArg + 1;
1763
1764 def completeOptions(self):
1765 return base.TestDriver.completeOptions(self);
1766
1767 def getResourceSet(self):
1768 if self.oTestVmSet is not None:
1769 return self.oTestVmSet.getResourceSet();
1770 return base.TestDriver.getResourceSet(self);
1771
1772 def actionExtract(self):
1773 return base.TestDriver.actionExtract(self);
1774
1775 def actionVerify(self):
1776 return base.TestDriver.actionVerify(self);
1777
1778 def actionConfig(self):
1779 return base.TestDriver.actionConfig(self);
1780
1781 def actionExecute(self):
1782 return base.TestDriver.actionExecute(self);
1783
1784 def actionCleanupBefore(self):
1785 """
1786 Kill any VBoxSVC left behind by a previous test run.
1787 """
1788 self._killVBoxSVCByPidFile('%s/VBoxSVC.pid' % (self.sScratchPath,));
1789 return base.TestDriver.actionCleanupBefore(self);
1790
1791 def actionCleanupAfter(self):
1792 """
1793 Clean up the VBox bits and then call the base driver.
1794
1795 If your test driver overrides this, it should normally call us at the
1796 end of the job.
1797 """
1798
1799 # Kill any left over VM processes.
1800 self._powerOffAllVms();
1801
1802 # Drop all VBox object references and shutdown xpcom then
1803 # terminating VBoxSVC, with extreme prejudice if need be.
1804 self._teardownVBoxApi();
1805 self._stopVBoxSVC();
1806
1807 # Add the VBoxSVC and testdriver debug+release log files.
1808 if self.fAlwaysUploadLogs or reporter.getErrorCount() > 0:
1809 if self.sVBoxSvcLogFile is not None and os.path.isfile(self.sVBoxSvcLogFile):
1810 reporter.addLogFile(self.sVBoxSvcLogFile, 'log/debug/svc', 'Debug log file for VBoxSVC');
1811 self.sVBoxSvcLogFile = None;
1812
1813 if self.sSelfLogFile is not None and os.path.isfile(self.sSelfLogFile):
1814 reporter.addLogFile(self.sSelfLogFile, 'log/debug/client', 'Debug log file for the test driver');
1815 self.sSelfLogFile = None;
1816
1817 sVBoxSvcRelLog = os.path.join(self.sScratchPath, 'VBoxUserHome', 'VBoxSVC.log');
1818 if os.path.isfile(sVBoxSvcRelLog):
1819 reporter.addLogFile(sVBoxSvcRelLog, 'log/release/svc', 'Release log file for VBoxSVC');
1820 for sSuff in [ '.1', '.2', '.3', '.4', '.5', '.6', '.7', '.8' ]:
1821 if os.path.isfile(sVBoxSvcRelLog + sSuff):
1822 reporter.addLogFile(sVBoxSvcRelLog + sSuff, 'log/release/svc', 'Release log file for VBoxSVC');
1823 # Testbox debugging - START - TEMPORARY, REMOVE ASAP.
1824 if self.sHost in ('darwin', 'freebsd', 'linux', 'solaris', ):
1825 try:
1826 reporter.log('> ls -la %s' % (os.path.join(self.sScratchPath, 'VBoxUserHome'),));
1827 utils.processCall(['ls', '-la', os.path.join(self.sScratchPath, 'VBoxUserHome')]);
1828 reporter.log('> ls -la %s' % (self.sScratchPath,));
1829 utils.processCall(['ls', '-la', self.sScratchPath]);
1830 except: pass;
1831 # Testbox debugging - END - TEMPORARY, REMOVE ASAP.
1832
1833 # Finally, call the base driver to wipe the scratch space.
1834 return base.TestDriver.actionCleanupAfter(self);
1835
1836 def actionAbort(self):
1837 """
1838 Terminate VBoxSVC if we've got a pid file.
1839 """
1840 #
1841 # Take default action first, then kill VBoxSVC. The other way around
1842 # is problematic since the testscript would continue running and possibly
1843 # trigger a new VBoxSVC to start.
1844 #
1845 fRc1 = base.TestDriver.actionAbort(self);
1846 fRc2 = self._killVBoxSVCByPidFile('%s/VBoxSVC.pid' % (self.sScratchPath,));
1847 return fRc1 is True and fRc2 is True;
1848
1849 def onExit(self, iRc):
1850 """
1851 Stop VBoxSVC if we've started it.
1852 """
1853 if self.oVBoxSvcProcess is not None:
1854 reporter.log('*** Shutting down the VBox API... (iRc=%s)' % (iRc,));
1855 self._powerOffAllVms();
1856 self._teardownVBoxApi();
1857 self._stopVBoxSVC();
1858 reporter.log('*** VBox API shutdown done.');
1859 return base.TestDriver.onExit(self, iRc);
1860
1861
1862 #
1863 # Task wait method override.
1864 #
1865
1866 def notifyAboutReadyTask(self, oTask):
1867 """
1868 Overriding base.TestDriver.notifyAboutReadyTask.
1869 """
1870 try:
1871 self.oVBoxMgr.interruptWaitEvents();
1872 reporter.log2('vbox.notifyAboutReadyTask: called interruptWaitEvents');
1873 except:
1874 reporter.logXcpt('vbox.notifyAboutReadyTask');
1875 return base.TestDriver.notifyAboutReadyTask(self, oTask);
1876
1877 def waitForTasksSleepWorker(self, cMsTimeout):
1878 """
1879 Overriding base.TestDriver.waitForTasksSleepWorker.
1880 """
1881 try:
1882 rc = self.oVBoxMgr.waitForEvents(int(cMsTimeout));
1883 _ = rc; #reporter.log2('vbox.waitForTasksSleepWorker(%u): true (waitForEvents -> %s)' % (cMsTimeout, rc));
1884 reporter.doPollWork('vbox.TestDriver.waitForTasksSleepWorker');
1885 return True;
1886 except KeyboardInterrupt:
1887 raise;
1888 except:
1889 reporter.logXcpt('vbox.waitForTasksSleepWorker');
1890 return False;
1891
1892 #
1893 # Utility methods.
1894 #
1895
1896 def processEvents(self, cMsTimeout = 0):
1897 """
1898 Processes events, returning after the first batch has been processed
1899 or the time limit has been reached.
1900
1901 Only Ctrl-C exception, no return.
1902 """
1903 try:
1904 self.oVBoxMgr.waitForEvents(cMsTimeout);
1905 except KeyboardInterrupt:
1906 raise;
1907 except:
1908 pass;
1909 return None;
1910
1911 def processPendingEvents(self):
1912 """ processEvents(0) - no waiting. """
1913 return self.processEvents(0);
1914
1915 def sleep(self, cSecs):
1916 """
1917 Sleep for a specified amount of time, processing XPCOM events all the while.
1918 """
1919 cMsTimeout = long(cSecs * 1000);
1920 msStart = base.timestampMilli();
1921 self.processEvents(0);
1922 while True:
1923 cMsElapsed = base.timestampMilli() - msStart;
1924 if cMsElapsed > cMsTimeout:
1925 break;
1926 #reporter.log2('cMsTimeout=%s - cMsElapsed=%d => %s' % (cMsTimeout, cMsElapsed, cMsTimeout - cMsElapsed));
1927 self.processEvents(cMsTimeout - cMsElapsed);
1928 return None;
1929
1930 def _logVmInfoUnsafe(self, oVM): # pylint: disable=R0915,R0912
1931 """
1932 Internal worker for logVmInfo that is wrapped in try/except.
1933
1934 This is copy, paste, search, replace and edit of infoCmd from vboxshell.py.
1935 """
1936 oOsType = self.oVBox.getGuestOSType(oVM.OSTypeId)
1937 reporter.log(" Name: %s" % (oVM.name));
1938 reporter.log(" ID: %s" % (oVM.id));
1939 reporter.log(" OS Type: %s - %s" % (oVM.OSTypeId, oOsType.description));
1940 reporter.log(" Machine state: %s" % (oVM.state));
1941 reporter.log(" Session state: %s" % (oVM.sessionState));
1942 if self.fpApiVer >= 4.2:
1943 reporter.log(" Session PID: %u (%#x)" % (oVM.sessionPID, oVM.sessionPID));
1944 else:
1945 reporter.log(" Session PID: %u (%#x)" % (oVM.sessionPid, oVM.sessionPid));
1946 if self.fpApiVer >= 5.0:
1947 reporter.log(" Session Name: %s" % (oVM.sessionName));
1948 else:
1949 reporter.log(" Session Name: %s" % (oVM.sessionType));
1950 reporter.log(" CPUs: %s" % (oVM.CPUCount));
1951 reporter.log(" RAM: %sMB" % (oVM.memorySize));
1952 reporter.log(" VRAM: %sMB" % (oVM.VRAMSize));
1953 reporter.log(" Monitors: %s" % (oVM.monitorCount));
1954 if oVM.chipsetType == vboxcon.ChipsetType_PIIX3: sType = "PIIX3";
1955 elif oVM.chipsetType == vboxcon.ChipsetType_ICH9: sType = "ICH9";
1956 else: sType = "unknown %s" % (oVM.chipsetType);
1957 reporter.log(" Chipset: %s" % (sType));
1958 if oVM.firmwareType == vboxcon.FirmwareType_BIOS: sType = "BIOS";
1959 elif oVM.firmwareType == vboxcon.FirmwareType_EFI: sType = "EFI";
1960 elif oVM.firmwareType == vboxcon.FirmwareType_EFI32: sType = "EFI32";
1961 elif oVM.firmwareType == vboxcon.FirmwareType_EFI64: sType = "EFI64";
1962 elif oVM.firmwareType == vboxcon.FirmwareType_EFIDUAL: sType = "EFIDUAL";
1963 else: sType = "unknown %s" % (oVM.firmwareType);
1964 reporter.log(" Firmware: %s" % (sType));
1965 reporter.log(" HwVirtEx: %s" % (oVM.getHWVirtExProperty(vboxcon.HWVirtExPropertyType_Enabled)));
1966 reporter.log(" VPID support: %s" % (oVM.getHWVirtExProperty(vboxcon.HWVirtExPropertyType_VPID)));
1967 reporter.log(" Nested paging: %s" % (oVM.getHWVirtExProperty(vboxcon.HWVirtExPropertyType_NestedPaging)));
1968 if self.fpApiVer >= 4.2 and hasattr(vboxcon, 'CPUPropertyType_LongMode'):
1969 reporter.log(" Long-mode: %s" % (oVM.getCPUProperty(vboxcon.CPUPropertyType_LongMode)));
1970 if self.fpApiVer >= 3.2:
1971 reporter.log(" PAE: %s" % (oVM.getCPUProperty(vboxcon.CPUPropertyType_PAE)));
1972 if self.fpApiVer < 5.0:
1973 reporter.log(" Synthetic CPU: %s" % (oVM.getCPUProperty(vboxcon.CPUPropertyType_Synthetic)));
1974 else:
1975 reporter.log(" PAE: %s" % (oVM.getCpuProperty(vboxcon.CpuPropertyType_PAE)));
1976 reporter.log(" Synthetic CPU: %s" % (oVM.getCpuProperty(vboxcon.CpuPropertyType_Synthetic)));
1977 reporter.log(" ACPI: %s" % (oVM.BIOSSettings.ACPIEnabled));
1978 reporter.log(" IO-APIC: %s" % (oVM.BIOSSettings.IOAPICEnabled));
1979 if self.fpApiVer >= 3.2:
1980 if self.fpApiVer >= 4.2:
1981 reporter.log(" HPET: %s" % (oVM.HPETEnabled));
1982 else:
1983 reporter.log(" HPET: %s" % (oVM.hpetEnabled));
1984 reporter.log(" 3D acceleration: %s" % (oVM.accelerate3DEnabled));
1985 reporter.log(" 2D acceleration: %s" % (oVM.accelerate2DVideoEnabled));
1986 reporter.log(" TeleporterEnabled: %s" % (oVM.teleporterEnabled));
1987 reporter.log(" TeleporterPort: %s" % (oVM.teleporterPort));
1988 reporter.log(" TeleporterAddress: %s" % (oVM.teleporterAddress));
1989 reporter.log(" TeleporterPassword: %s" % (oVM.teleporterPassword));
1990 reporter.log(" Clipboard mode: %s" % (oVM.clipboardMode));
1991 if self.fpApiVer >= 5.0:
1992 reporter.log(" Drag and drop mode: %s" % (oVM.dnDMode));
1993 elif self.fpApiVer >= 4.3:
1994 reporter.log(" Drag and drop mode: %s" % (oVM.dragAndDropMode));
1995 if self.fpApiVer >= 4.0:
1996 reporter.log(" VRDP server: %s" % (oVM.VRDEServer.enabled));
1997 try: sPorts = oVM.VRDEServer.getVRDEProperty("TCP/Ports");
1998 except: sPorts = "";
1999 reporter.log(" VRDP server ports: %s" % (sPorts));
2000 reporter.log(" VRDP auth: %s (%s)" % (oVM.VRDEServer.authType, oVM.VRDEServer.authLibrary));
2001 else:
2002 reporter.log(" VRDP server: %s" % (oVM.VRDPServer.enabled));
2003 reporter.log(" VRDP server ports: %s" % (oVM.VRDPServer.ports));
2004 reporter.log(" Last changed: %s" % (oVM.lastStateChange));
2005
2006 aoControllers = self.oVBoxMgr.getArray(oVM, 'storageControllers')
2007 if aoControllers:
2008 reporter.log(" Controllers:");
2009 for oCtrl in aoControllers:
2010 reporter.log(" %s %s bus: %s type: %s" % (oCtrl.name, oCtrl.controllerType, oCtrl.bus, oCtrl.controllerType));
2011 oAudioAdapter = oVM.audioAdapter;
2012 if oAudioAdapter.audioController == vboxcon.AudioControllerType_AC97: sType = "AC97";
2013 elif oAudioAdapter.audioController == vboxcon.AudioControllerType_SB16: sType = "SB16";
2014 elif oAudioAdapter.audioController == vboxcon.AudioControllerType_HDA: sType = "HDA";
2015 else: sType = "unknown %s" % (oAudioAdapter.audioController);
2016 reporter.log(" AudioController: %s" % (sType));
2017 reporter.log(" AudioEnabled: %s" % (oAudioAdapter.enabled));
2018 if oAudioAdapter.audioDriver == vboxcon.AudioDriverType_CoreAudio: sType = "CoreAudio";
2019 elif oAudioAdapter.audioDriver == vboxcon.AudioDriverType_DirectSound: sType = "DirectSound";
2020 elif oAudioAdapter.audioDriver == vboxcon.AudioDriverType_Pulse: sType = "PulseAudio";
2021 elif oAudioAdapter.audioDriver == vboxcon.AudioDriverType_OSS: sType = "OSS";
2022 elif oAudioAdapter.audioDriver == vboxcon.AudioDriverType_Null: sType = "NULL";
2023 else: sType = "unknown %s" % (oAudioAdapter.audioDriver);
2024 reporter.log(" Host AudioDriver: %s" % (sType));
2025
2026 self.processPendingEvents();
2027 aoAttachments = self.oVBoxMgr.getArray(oVM, 'mediumAttachments')
2028 if aoAttachments:
2029 reporter.log(" Attachments:");
2030 for oAtt in aoAttachments:
2031 sCtrl = "Controller: %s port: %s device: %s type: %s" % (oAtt.controller, oAtt.port, oAtt.device, oAtt.type);
2032 oMedium = oAtt.medium
2033 if oAtt.type == vboxcon.DeviceType_HardDisk:
2034 reporter.log(" %s: HDD" % sCtrl);
2035 reporter.log(" Id: %s" % (oMedium.id));
2036 reporter.log(" Name: %s" % (oMedium.name));
2037 reporter.log(" Format: %s" % (oMedium.format));
2038 reporter.log(" Location: %s" % (oMedium.location));
2039
2040 if oAtt.type == vboxcon.DeviceType_DVD:
2041 reporter.log(" %s: DVD" % sCtrl);
2042 if oMedium:
2043 reporter.log(" Id: %s" % (oMedium.id));
2044 reporter.log(" Name: %s" % (oMedium.name));
2045 if oMedium.hostDrive:
2046 reporter.log(" Host DVD %s" % (oMedium.location));
2047 if oAtt.passthrough:
2048 reporter.log(" [passthrough mode]");
2049 else:
2050 reporter.log(" Virtual image: %s" % (oMedium.location));
2051 reporter.log(" Size: %s" % (oMedium.size));
2052 else:
2053 reporter.log(" empty");
2054
2055 if oAtt.type == vboxcon.DeviceType_Floppy:
2056 reporter.log(" %s: Floppy" % sCtrl);
2057 if oMedium:
2058 reporter.log(" Id: %s" % (oMedium.id));
2059 reporter.log(" Name: %s" % (oMedium.name));
2060 if oMedium.hostDrive:
2061 reporter.log(" Host floppy: %s" % (oMedium.location));
2062 else:
2063 reporter.log(" Virtual image: %s" % (oMedium.location));
2064 reporter.log(" Size: %s" % (oMedium.size));
2065 else:
2066 reporter.log(" empty");
2067 self.processPendingEvents();
2068
2069 reporter.log(" Network Adapter:");
2070 for iSlot in range(0, 32):
2071 try: oNic = oVM.getNetworkAdapter(iSlot)
2072 except: break;
2073 if not oNic.enabled:
2074 reporter.log2(" slot #%d found but not enabled, skipping" % (iSlot,));
2075 continue;
2076 if oNic.adapterType == vboxcon.NetworkAdapterType_Am79C973: sType = "PCNet";
2077 elif oNic.adapterType == vboxcon.NetworkAdapterType_Am79C970A: sType = "PCNetOld";
2078 elif oNic.adapterType == vboxcon.NetworkAdapterType_I82545EM: sType = "E1000";
2079 elif oNic.adapterType == vboxcon.NetworkAdapterType_I82540EM: sType = "E1000Desk";
2080 elif oNic.adapterType == vboxcon.NetworkAdapterType_I82543GC: sType = "E1000Srv2";
2081 elif oNic.adapterType == vboxcon.NetworkAdapterType_Virtio: sType = "Virtio";
2082 else: sType = "unknown %s" % (oNic.adapterType);
2083 reporter.log(" slot #%d: type: %s (%s) MAC Address: %s lineSpeed: %s" % \
2084 (iSlot, sType, oNic.adapterType, oNic.MACAddress, oNic.lineSpeed) );
2085
2086 if oNic.attachmentType == vboxcon.NetworkAttachmentType_NAT:
2087 reporter.log(" attachmentType: NAT (%s)" % (oNic.attachmentType));
2088 if self.fpApiVer >= 4.1:
2089 reporter.log(" nat-network: %s" % (oNic.NATNetwork,));
2090 elif oNic.attachmentType == vboxcon.NetworkAttachmentType_Bridged:
2091 reporter.log(" attachmentType: Bridged (%s)" % (oNic.attachmentType));
2092 if self.fpApiVer >= 4.1:
2093 reporter.log(" hostInterface: %s" % (oNic.bridgedInterface));
2094 else:
2095 reporter.log(" hostInterface: %s" % (oNic.hostInterface));
2096 elif oNic.attachmentType == vboxcon.NetworkAttachmentType_Internal:
2097 reporter.log(" attachmentType: Internal (%s)" % (oNic.attachmentType));
2098 reporter.log(" intnet-name: %s" % (oNic.internalNetwork,));
2099 elif oNic.attachmentType == vboxcon.NetworkAttachmentType_HostOnly:
2100 reporter.log(" attachmentType: HostOnly (%s)" % (oNic.attachmentType));
2101 if self.fpApiVer >= 4.1:
2102 reporter.log(" hostInterface: %s" % (oNic.hostOnlyInterface));
2103 else:
2104 reporter.log(" hostInterface: %s" % (oNic.hostInterface));
2105 else:
2106 if self.fpApiVer >= 4.1:
2107 if oNic.attachmentType == vboxcon.NetworkAttachmentType_Generic:
2108 reporter.log(" attachmentType: Generic (%s)" % (oNic.attachmentType));
2109 reporter.log(" generic-driver: %s" % (oNic.GenericDriver));
2110 else:
2111 reporter.log(" attachmentType: unknown-%s" % (oNic.attachmentType));
2112 else:
2113 reporter.log(" attachmentType: unknown-%s" % (oNic.attachmentType));
2114 if oNic.traceEnabled:
2115 reporter.log(" traceFile: %s" % (oNic.traceFile));
2116 self.processPendingEvents();
2117 return True;
2118
2119 def logVmInfo(self, oVM): # pylint: disable=R0915,R0912
2120 """
2121 Logs VM configuration details.
2122
2123 This is copy, past, search, replace and edit of infoCmd from vboxshell.py.
2124 """
2125 try:
2126 fRc = self._logVmInfoUnsafe(oVM);
2127 except:
2128 reporter.logXcpt();
2129 fRc = False;
2130 return fRc;
2131
2132 def logVmInfoByName(self, sName):
2133 """
2134 logVmInfo + getVmByName.
2135 """
2136 return self.logVmInfo(self.getVmByName(sName));
2137
2138 def tryFindGuestOsId(self, sIdOrDesc):
2139 """
2140 Takes a guest OS ID or Description and returns the ID.
2141 If nothing matching it is found, the input is returned unmodified.
2142 """
2143
2144 if self.fpApiVer >= 4.0:
2145 if sIdOrDesc == 'Solaris (64 bit)':
2146 sIdOrDesc = 'Oracle Solaris 10 5/09 and earlier (64 bit)';
2147
2148 try:
2149 aoGuestTypes = self.oVBoxMgr.getArray(self.oVBox, 'GuestOSTypes');
2150 except:
2151 reporter.logXcpt();
2152 else:
2153 for oGuestOS in aoGuestTypes:
2154 try:
2155 sId = oGuestOS.id;
2156 sDesc = oGuestOS.description;
2157 except:
2158 reporter.logXcpt();
2159 else:
2160 if sIdOrDesc == sId or sIdOrDesc == sDesc:
2161 sIdOrDesc = sId;
2162 break;
2163 self.processPendingEvents();
2164 return sIdOrDesc
2165
2166 def resourceFindVmHd(self, sVmName, sFlavor):
2167 """
2168 Search the test resources for the most recent VM HD.
2169
2170 Returns path relative to the test resource root.
2171 """
2172 ## @todo implement a proper search algo here.
2173 return '4.2/' + sFlavor + '/' + sVmName + '/t-' + sVmName + '.vdi';
2174
2175
2176 #
2177 # VM Api wrappers that logs errors, hides exceptions and other details.
2178 #
2179
2180 # pylint: disable=R0913,R0914,R0915
2181 def createTestVM(self, sName, iGroup, sHd = None, cMbRam = None, cCpus = 1, fVirtEx = None, fNestedPaging = None, \
2182 sDvdImage = None, sKind = "Other", fIoApic = None, fPae = None, fFastBootLogo = True, \
2183 eNic0Type = None, eNic0AttachType = None, sNic0NetName = 'default', sNic0MacAddr = 'grouped', \
2184 sFloppy = None, fNatForwardingForTxs = None, sHddControllerType = 'IDE Controller', \
2185 fVmmDevTestingPart = None, fVmmDevTestingMmio = False, sFirmwareType = 'bios', sChipsetType = 'piix3', \
2186 sDvdControllerType = 'IDE Controller',):
2187 """
2188 Creates a test VM with a immutable HD from the test resources.
2189 """
2190 if not self.importVBoxApi():
2191 return None;
2192
2193 # create + register the VM
2194 try:
2195 if self.fpApiVer >= 4.2: # Introduces grouping (third parameter, empty for now).
2196 oVM = self.oVBox.createMachine("", sName, [], self.tryFindGuestOsId(sKind), "");
2197 elif self.fpApiVer >= 4.0:
2198 oVM = self.oVBox.createMachine("", sName, self.tryFindGuestOsId(sKind), "", False);
2199 elif self.fpApiVer >= 3.2:
2200 oVM = self.oVBox.createMachine(sName, self.tryFindGuestOsId(sKind), "", "", False);
2201 else:
2202 oVM = self.oVBox.createMachine(sName, self.tryFindGuestOsId(sKind), "", "");
2203 try:
2204 oVM.saveSettings();
2205 try:
2206 self.oVBox.registerMachine(oVM);
2207 except:
2208 raise;
2209 except:
2210 reporter.logXcpt();
2211 if self.fpApiVer >= 4.0:
2212 try:
2213 if self.fpApiVer >= 4.3:
2214 oProgress = oVM.deleteConfig([]);
2215 else:
2216 oProgress = oVM.delete(None);
2217 self.waitOnProgress(oProgress);
2218 except:
2219 reporter.logXcpt();
2220 else:
2221 try: oVM.deleteSettings();
2222 except: reporter.logXcpt();
2223 raise;
2224 except:
2225 reporter.errorXcpt('failed to create vm "%s"' % (sName));
2226 return None;
2227
2228 # Configure the VM.
2229 fRc = True;
2230 oSession = self.openSession(oVM);
2231 if oSession is not None:
2232 fRc = oSession.setupPreferredConfig();
2233
2234 if fRc and cMbRam is not None :
2235 fRc = oSession.setRamSize(cMbRam);
2236 if fRc and cCpus is not None:
2237 fRc = oSession.setCpuCount(cCpus);
2238 if fRc and fVirtEx is not None:
2239 fRc = oSession.enableVirtEx(fVirtEx);
2240 if fRc and fNestedPaging is not None:
2241 fRc = oSession.enableNestedPaging(fNestedPaging);
2242 if fRc and fIoApic is not None:
2243 fRc = oSession.enableIoApic(fIoApic);
2244 if fRc and fPae is not None:
2245 fRc = oSession.enablePae(fPae);
2246 if fRc and sDvdImage is not None:
2247 fRc = oSession.attachDvd(sDvdImage, sDvdControllerType);
2248 if fRc and sHd is not None:
2249 fRc = oSession.attachHd(sHd, sHddControllerType);
2250 if fRc and sFloppy is not None:
2251 fRc = oSession.attachFloppy(sFloppy);
2252 if fRc and eNic0Type is not None:
2253 fRc = oSession.setNicType(eNic0Type, 0);
2254 if fRc and (eNic0AttachType is not None or (sNic0NetName is not None and sNic0NetName != 'default')):
2255 fRc = oSession.setNicAttachment(eNic0AttachType, sNic0NetName, 0);
2256 if fRc and sNic0MacAddr is not None:
2257 if sNic0MacAddr == 'grouped':
2258 sNic0MacAddr = '%02u' % (iGroup);
2259 fRc = oSession.setNicMacAddress(sNic0MacAddr, 0);
2260 if fRc and fNatForwardingForTxs is True:
2261 fRc = oSession.setupNatForwardingForTxs();
2262 if fRc and fFastBootLogo is not None:
2263 fRc = oSession.setupBootLogo(fFastBootLogo);
2264 if fRc and self.fEnableVrdp:
2265 fRc = oSession.setupVrdp(True, self.uVrdpBasePort + iGroup);
2266 if fRc and fVmmDevTestingPart is not None:
2267 fRc = oSession.enableVmmDevTestingPart(fVmmDevTestingPart, fVmmDevTestingMmio);
2268 if fRc and sFirmwareType == 'bios':
2269 fRc = oSession.setFirmwareType(vboxcon.FirmwareType_BIOS);
2270 elif sFirmwareType == 'efi':
2271 fRc = oSession.setFirmwareType(vboxcon.FirmwareType_EFI);
2272 if fRc and self.fEnableDebugger:
2273 fRc = oSession.setExtraData('VBoxInternal/DBGC/Enabled', '1');
2274 if fRc and sChipsetType == 'piix3':
2275 fRc = oSession.setChipsetType(vboxcon.ChipsetType_PIIX3);
2276 elif sChipsetType == 'ich9':
2277 fRc = oSession.setChipsetType(vboxcon.ChipsetType_ICH9);
2278
2279 if fRc: fRc = oSession.saveSettings();
2280 if not fRc: oSession.discardSettings(True);
2281 oSession.close();
2282 if not fRc:
2283 try: self.oVBox.unregisterMachine(oVM.id);
2284 except: pass;
2285 if self.fpApiVer >= 4.0:
2286 try:
2287 if self.fpApiVer >= 4.3:
2288 oProgress = oVM.deleteConfig([]);
2289 else:
2290 oProgress = oVM.delete(None);
2291 self.waitOnProgress(oProgress);
2292 except:
2293 reporter.logXcpt();
2294 else:
2295 try: oVM.deleteSettings();
2296 except: reporter.logXcpt();
2297 return None;
2298
2299 # success.
2300 reporter.log('created "%s" with name "%s"' % (oVM.id, sName));
2301 self.aoVMs.append(oVM);
2302 self.logVmInfo(oVM); # testing...
2303 return oVM;
2304 # pylint: enable=R0913,R0914,R0915
2305
2306 def addTestMachine(self, sNameOrId, fQuiet = False):
2307 """
2308 Adds an already existing (that is, configured) test VM to the
2309 test VM list.
2310 """
2311 # find + add the VM to the list.
2312 try:
2313 if self.fpApiVer >= 4.0:
2314 oVM = self.oVBox.findMachine(sNameOrId);
2315 else:
2316 reporter.error('fpApiVer=%s - did you remember to initialize the API' % (self.fpApiVer,));
2317 except:
2318 reporter.errorXcpt('could not find vm "%s"' % (sNameOrId,));
2319 return None;
2320
2321 self.aoVMs.append(oVM);
2322 if not fQuiet:
2323 reporter.log('Added "%s" with name "%s"' % (oVM.id, sNameOrId));
2324 self.logVmInfo(oVM);
2325 return oVM;
2326
2327 def openSession(self, oVM):
2328 """
2329 Opens a session for the VM. Returns the a Session wrapper object that
2330 will automatically close the session when the wrapper goes out of scope.
2331
2332 On failure None is returned and an error is logged.
2333 """
2334 try:
2335 sUuid = oVM.id;
2336 except:
2337 reporter.errorXcpt('failed to get the UUID for VM "%s"' % (oVM,));
2338 return None;
2339
2340 # This loop is a kludge to deal with us racing the closing of the
2341 # direct session of a previous VM run. See waitOnDirectSessionClose.
2342 for i in range(10):
2343 try:
2344 if self.fpApiVer <= 3.2:
2345 oSession = self.oVBoxMgr.openMachineSession(sUuid);
2346 else:
2347 oSession = self.oVBoxMgr.openMachineSession(oVM);
2348 break;
2349 except:
2350 if i == 9:
2351 reporter.errorXcpt('failed to open session for "%s" ("%s")' % (sUuid, oVM));
2352 return None;
2353 if i > 0:
2354 reporter.logXcpt('warning: failed to open session for "%s" ("%s") - retrying in %u secs' % (sUuid, oVM, i));
2355 self.waitOnDirectSessionClose(oVM, 5000 + i * 1000);
2356 from testdriver.vboxwrappers import SessionWrapper;
2357 return SessionWrapper(oSession, oVM, self.oVBox, self.oVBoxMgr, self, False);
2358
2359 def getVmByName(self, sName):
2360 """
2361 Get a test VM by name. Returns None if not found, logged.
2362 """
2363 # Look it up in our 'cache'.
2364 for oVM in self.aoVMs:
2365 try:
2366 #reporter.log2('cur: %s / %s (oVM=%s)' % (oVM.name, oVM.id, oVM));
2367 if oVM.name == sName:
2368 return oVM;
2369 except:
2370 reporter.errorXcpt('failed to get the name from the VM "%s"' % (oVM));
2371
2372 # Look it up the standard way.
2373 return self.addTestMachine(sName, fQuiet = True);
2374
2375 def getVmByUuid(self, sUuid):
2376 """
2377 Get a test VM by uuid. Returns None if not found, logged.
2378 """
2379 # Look it up in our 'cache'.
2380 for oVM in self.aoVMs:
2381 try:
2382 if oVM.id == sUuid:
2383 return oVM;
2384 except:
2385 reporter.errorXcpt('failed to get the UUID from the VM "%s"' % (oVM));
2386
2387 # Look it up the standard way.
2388 return self.addTestMachine(sUuid, fQuiet = True);
2389
2390 def waitOnProgress(self, oProgress, cMsTimeout = 1000000, fErrorOnTimeout = True, cMsInterval = 1000):
2391 """
2392 Waits for a progress object to complete. Returns the status code.
2393 """
2394 # Wait for progress no longer than cMsTimeout time period.
2395 tsStart = datetime.datetime.now()
2396 while True:
2397 self.processPendingEvents();
2398 try:
2399 if oProgress.completed:
2400 break;
2401 except:
2402 return -1;
2403 self.processPendingEvents();
2404
2405 tsNow = datetime.datetime.now()
2406 tsDelta = tsNow - tsStart
2407 if ((tsDelta.microseconds + tsDelta.seconds * 1000000) // 1000) > cMsTimeout:
2408 if fErrorOnTimeout:
2409 reporter.errorTimeout('Timeout while waiting for progress.')
2410 return -1
2411
2412 reporter.doPollWork('vbox.TestDriver.waitOnProgress');
2413 try: oProgress.waitForCompletion(cMsInterval);
2414 except: return -2;
2415
2416 try: rc = oProgress.resultCode;
2417 except: rc = -2;
2418 self.processPendingEvents();
2419 return rc;
2420
2421 def waitOnDirectSessionClose(self, oVM, cMsTimeout):
2422 """
2423 Waits for the VM process to close it's current direct session.
2424
2425 Returns None.
2426 """
2427 # Get the original values so we're not subject to
2428 try:
2429 eCurState = oVM.sessionState;
2430 if self.fpApiVer >= 5.0:
2431 sCurName = sOrgName = oVM.sessionName;
2432 else:
2433 sCurName = sOrgName = oVM.sessionType;
2434 if self.fpApiVer >= 4.2:
2435 iCurPid = iOrgPid = oVM.sessionPID;
2436 else:
2437 iCurPid = iOrgPid = oVM.sessionPid;
2438 except Exception as oXcpt:
2439 if ComError.notEqual(oXcpt, ComError.E_ACCESSDENIED):
2440 reporter.logXcpt();
2441 self.processPendingEvents();
2442 return None;
2443 self.processPendingEvents();
2444
2445 msStart = base.timestampMilli();
2446 while iCurPid == iOrgPid \
2447 and sCurName == sOrgName \
2448 and sCurName != '' \
2449 and base.timestampMilli() - msStart < cMsTimeout \
2450 and ( eCurState == vboxcon.SessionState_Unlocking \
2451 or eCurState == vboxcon.SessionState_Spawning \
2452 or eCurState == vboxcon.SessionState_Locked):
2453 self.processEvents(1000);
2454 try:
2455 eCurState = oVM.sessionState;
2456 sCurName = oVM.sessionName if self.fpApiVer >= 5.0 else oVM.sessionType;
2457 iCurPid = oVM.sessionPID if self.fpApiVer >= 4.2 else oVM.sessionPid;
2458 except Exception as oXcpt:
2459 if ComError.notEqual(oXcpt, ComError.E_ACCESSDENIED):
2460 reporter.logXcpt();
2461 break;
2462 self.processPendingEvents();
2463 self.processPendingEvents();
2464 return None;
2465
2466 def uploadStartupLogFile(self, oVM, sVmName):
2467 """
2468 Uploads the VBoxStartup.log when present.
2469 """
2470 fRc = True;
2471 try:
2472 sLogFile = os.path.join(oVM.logFolder, 'VBoxHardening.log');
2473 except:
2474 reporter.logXcpt();
2475 fRc = False;
2476 else:
2477 if os.path.isfile(sLogFile):
2478 reporter.addLogFile(sLogFile, 'log/release/vm', '%s hardening log' % (sVmName, ),
2479 sAltName = '%s-%s' % (sVmName, os.path.basename(sLogFile),));
2480 return fRc;
2481
2482 def annotateAndUploadProcessReport(self, sProcessReport, sFilename, sKind, sDesc):
2483 """
2484 Annotates the given VM process report and uploads it if successfull.
2485 """
2486 fRc = False;
2487 if self.oBuild is not None and self.oBuild.sInstallPath is not None:
2488 oResolver = btresolver.BacktraceResolver(self.sScratchPath, self.oBuild.sInstallPath,
2489 self.getBuildOs(), self.getBuildArch(),
2490 fnLog = reporter.log);
2491 fRcTmp = oResolver.prepareEnv();
2492 if fRcTmp:
2493 reporter.log('Successfully prepared environment');
2494 sReportDbgSym = oResolver.annotateReport(sProcessReport);
2495 if sReportDbgSym is not None:
2496 reporter.addLogString(sReportDbgSym, sFilename, sKind, sDesc);
2497 fRc = True;
2498 else:
2499 reporter.log('Annotating report failed');
2500 oResolver.cleanupEnv();
2501 return fRc;
2502
2503 def startVmEx(self, oVM, fWait = True, sType = None, sName = None, asEnv = None): # pylint: disable=R0914,R0915
2504 """
2505 Start the VM, returning the VM session and progress object on success.
2506 The session is also added to the task list and to the aoRemoteSessions set.
2507
2508 asEnv is a list of string on the putenv() form.
2509
2510 On failure (None, None) is returned and an error is logged.
2511 """
2512 # Massage and check the input.
2513 if sType is None:
2514 sType = self.sSessionType;
2515 if sName is None:
2516 try: sName = oVM.name;
2517 except: sName = 'bad-vm-handle';
2518 reporter.log('startVmEx: sName=%s fWait=%s sType=%s' % (sName, fWait, sType));
2519 if oVM is None:
2520 return (None, None);
2521
2522 ## @todo Do this elsewhere.
2523 # Hack alert. Disables all annoying GUI popups.
2524 if sType == 'gui' and not self.aoRemoteSessions:
2525 try:
2526 self.oVBox.setExtraData('GUI/Input/AutoCapture', 'false');
2527 if self.fpApiVer >= 3.2:
2528 self.oVBox.setExtraData('GUI/LicenseAgreed', '8');
2529 else:
2530 self.oVBox.setExtraData('GUI/LicenseAgreed', '7');
2531 self.oVBox.setExtraData('GUI/RegistrationData', 'triesLeft=0');
2532 self.oVBox.setExtraData('GUI/SUNOnlineData', 'triesLeft=0');
2533 self.oVBox.setExtraData('GUI/SuppressMessages', 'confirmVMReset,remindAboutMouseIntegrationOn,'
2534 'remindAboutMouseIntegrationOff,remindAboutPausedVMInput,confirmInputCapture,'
2535 'confirmGoingFullscreen,remindAboutInaccessibleMedia,remindAboutWrongColorDepth,'
2536 'confirmRemoveMedium,allPopupPanes,allMessageBoxes,all');
2537 self.oVBox.setExtraData('GUI/UpdateDate', 'never');
2538 self.oVBox.setExtraData('GUI/PreventBetaWarning', self.oVBox.version);
2539 except:
2540 reporter.logXcpt();
2541
2542 # The UUID for the name.
2543 try:
2544 sUuid = oVM.id;
2545 except:
2546 reporter.errorXcpt('failed to get the UUID for VM "%s"' % (oVM));
2547 return (None, None);
2548 self.processPendingEvents();
2549
2550 # Construct the environment.
2551 sLogFile = '%s/VM-%s.log' % (self.sScratchPath, sUuid);
2552 try: os.remove(sLogFile);
2553 except: pass;
2554 if self.sLogSessionDest:
2555 sLogDest = self.sLogSessionDest;
2556 else:
2557 sLogDest = 'file=%s' % sLogFile;
2558 sEnv = 'VBOX_LOG=%s\nVBOX_LOG_FLAGS=%s\nVBOX_LOG_DEST=nodeny %s\nVBOX_RELEASE_LOG_FLAGS=append time' \
2559 % (self.sLogSessionGroups, self.sLogSessionFlags, sLogDest,);
2560 # Extra audio logging
2561 sEnv += '\nVBOX_RELEASE_LOG=drv_audio.e.l.l2+drv_host_audio.e.l.l2'
2562 if sType == 'gui':
2563 sEnv += '\nVBOX_GUI_DBG_ENABLED=1'
2564 if asEnv is not None and asEnv:
2565 sEnv += '\n' + ('\n'.join(asEnv));
2566
2567 # Shortcuts for local testing.
2568 oProgress = oWrapped = None;
2569 oTestVM = self.oTestVmSet.findTestVmByName(sName) if self.oTestVmSet is not None else None;
2570 try:
2571 if oTestVM is not None \
2572 and oTestVM.fSnapshotRestoreCurrent is True:
2573 if oVM.state is vboxcon.MachineState_Running:
2574 reporter.log2('Machine "%s" already running.' % (sName,));
2575 oProgress = None;
2576 oWrapped = self.openSession(oVM);
2577 else:
2578 reporter.log2('Checking if snapshot for machine "%s" exists.' % (sName,));
2579 oSessionWrapperRestore = self.openSession(oVM);
2580 if oSessionWrapperRestore is not None:
2581 oSnapshotCur = oVM.currentSnapshot;
2582 if oSnapshotCur is not None:
2583 reporter.log2('Restoring snapshot for machine "%s".' % (sName,));
2584 oSessionWrapperRestore.restoreSnapshot(oSnapshotCur);
2585 reporter.log2('Current snapshot for machine "%s" restored.' % (sName,));
2586 else:
2587 reporter.log('warning: no current snapshot for machine "%s" found.' % (sName,));
2588 oSessionWrapperRestore.close();
2589 except:
2590 reporter.errorXcpt();
2591 return (None, None);
2592
2593 # Open a remote session, wait for this operation to complete.
2594 # (The loop is a kludge to deal with us racing the closing of the
2595 # direct session of a previous VM run. See waitOnDirectSessionClose.)
2596 if oWrapped is None:
2597 for i in range(10):
2598 try:
2599 if self.fpApiVer < 4.3 \
2600 or (self.fpApiVer == 4.3 and not hasattr(self.oVBoxMgr, 'getSessionObject')):
2601 oSession = self.oVBoxMgr.mgr.getSessionObject(self.oVBox); # pylint: disable=E1101
2602 elif self.fpApiVer < 5.2 \
2603 or (self.fpApiVer == 5.2 and hasattr(self.oVBoxMgr, 'vbox')):
2604 oSession = self.oVBoxMgr.getSessionObject(self.oVBox); # pylint: disable=E1101
2605 else:
2606 oSession = self.oVBoxMgr.getSessionObject(); # pylint: disable=E1101
2607 if self.fpApiVer < 3.3:
2608 oProgress = self.oVBox.openRemoteSession(oSession, sUuid, sType, sEnv);
2609 else:
2610 oProgress = oVM.launchVMProcess(oSession, sType, sEnv);
2611 break;
2612 except:
2613 if i == 9:
2614 reporter.errorXcpt('failed to start VM "%s" ("%s"), aborting.' % (sUuid, sName));
2615 return (None, None);
2616 oSession = None;
2617 if i >= 0:
2618 reporter.logXcpt('warning: failed to start VM "%s" ("%s") - retrying in %u secs.' % (sUuid, oVM, i)); # pylint: disable=C0301
2619 self.waitOnDirectSessionClose(oVM, 5000 + i * 1000);
2620 if fWait and oProgress is not None:
2621 rc = self.waitOnProgress(oProgress);
2622 if rc < 0:
2623 self.waitOnDirectSessionClose(oVM, 5000);
2624
2625 # VM failed to power up, still collect VBox.log, need to wrap the session object
2626 # in order to use the helper for adding the log files to the report.
2627 from testdriver.vboxwrappers import SessionWrapper;
2628 oTmp = SessionWrapper(oSession, oVM, self.oVBox, self.oVBoxMgr, self, True, sName, sLogFile);
2629 oTmp.addLogsToReport();
2630 try:
2631 if oSession is not None:
2632 oSession.close();
2633 except: pass;
2634 reportError(oProgress, 'failed to open session for "%s"' % (sName));
2635 self.uploadStartupLogFile(oVM, sName);
2636 return (None, None);
2637 reporter.log2('waitOnProgress -> %s' % (rc,));
2638
2639 # Wrap up the session object and push on to the list before returning it.
2640 if oWrapped is None:
2641 from testdriver.vboxwrappers import SessionWrapper;
2642 oWrapped = SessionWrapper(oSession, oVM, self.oVBox, self.oVBoxMgr, self, True, sName, sLogFile);
2643
2644 oWrapped.registerEventHandlerForTask();
2645 self.aoRemoteSessions.append(oWrapped);
2646 if oWrapped is not self.aoRemoteSessions[len(self.aoRemoteSessions) - 1]:
2647 reporter.error('not by reference: oWrapped=%s aoRemoteSessions[%s]=%s'
2648 % (oWrapped, len(self.aoRemoteSessions) - 1,
2649 self.aoRemoteSessions[len(self.aoRemoteSessions) - 1]));
2650 self.addTask(oWrapped);
2651
2652 reporter.log2('startVmEx: oSession=%s, oSessionWrapper=%s, oProgress=%s' % (oSession, oWrapped, oProgress));
2653
2654 from testdriver.vboxwrappers import ProgressWrapper;
2655 return (oWrapped, ProgressWrapper(oProgress, self.oVBoxMgr, self,
2656 'starting %s' % (sName,)) if oProgress else None);
2657
2658 def startVm(self, oVM, sType=None, sName = None, asEnv = None):
2659 """ Simplified version of startVmEx. """
2660 oSession, _ = self.startVmEx(oVM, True, sType, sName, asEnv = asEnv);
2661 return oSession;
2662
2663 def startVmByNameEx(self, sName, fWait=True, sType=None, asEnv = None):
2664 """
2665 Start the VM, returning the VM session and progress object on success.
2666 The session is also added to the task list and to the aoRemoteSessions set.
2667
2668 On failure (None, None) is returned and an error is logged.
2669 """
2670 oVM = self.getVmByName(sName);
2671 if oVM is None:
2672 return (None, None);
2673 return self.startVmEx(oVM, fWait, sType, sName, asEnv = asEnv);
2674
2675 def startVmByName(self, sName, sType=None, asEnv = None):
2676 """
2677 Start the VM, returning the VM session on success. The session is
2678 also added to the task list and to the aoRemoteSessions set.
2679
2680 On failure None is returned and an error is logged.
2681 """
2682 oSession, _ = self.startVmByNameEx(sName, True, sType, asEnv = asEnv);
2683 return oSession;
2684
2685 def terminateVmBySession(self, oSession, oProgress = None, fTakeScreenshot = None): # pylint: disable=R0915
2686 """
2687 Terminates the VM specified by oSession and adds the release logs to
2688 the test report.
2689
2690 This will try achieve this by using powerOff, but will resort to
2691 tougher methods if that fails.
2692
2693 The session will always be removed from the task list.
2694 The session will be closed unless we fail to kill the process.
2695 The session will be removed from the remote session list if closed.
2696
2697 The progress object (a wrapper!) is for teleportation and similar VM
2698 operations, it will be attempted canceled before powering off the VM.
2699 Failures are logged but ignored.
2700 The progress object will always be removed from the task list.
2701
2702 Returns True if powerOff and session close both succeed.
2703 Returns False if on failure (logged), including when we successfully
2704 kill the VM process.
2705 """
2706 reporter.log2('terminateVmBySession: oSession=%s (pid=%s) oProgress=%s' % (oSession.sName, oSession.getPid(), oProgress));
2707
2708 # Call getPid first to make sure the PID is cached in the wrapper.
2709 oSession.getPid();
2710
2711 #
2712 # If the host is out of memory, just skip all the info collection as it
2713 # requires memory too and seems to wedge.
2714 #
2715 sHostProcessInfo = None;
2716 sHostProcessInfoHung = None;
2717 sLastScreenshotPath = None;
2718 sOsKernelLog = None;
2719 sVgaText = None;
2720 asMiscInfos = [];
2721
2722 if not oSession.fHostMemoryLow:
2723 # Try to fetch the VM process info before meddling with its state.
2724 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2725 sHostProcessInfo = utils.processGetInfo(oSession.getPid(), fSudo = True);
2726
2727 #
2728 # Pause the VM if we're going to take any screenshots or dig into the
2729 # guest. Failures are quitely ignored.
2730 #
2731 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2732 try:
2733 if oSession.oVM.state in [ vboxcon.MachineState_Running,
2734 vboxcon.MachineState_LiveSnapshotting,
2735 vboxcon.MachineState_Teleporting ]:
2736 oSession.o.console.pause();
2737 except:
2738 reporter.logXcpt();
2739
2740 #
2741 # Take Screenshot and upload it (see below) to Test Manager if appropriate/requested.
2742 #
2743 if fTakeScreenshot is True or self.fAlwaysUploadScreenshots or reporter.testErrorCount() > 0:
2744 sLastScreenshotPath = os.path.join(self.sScratchPath, "LastScreenshot-%s.png" % oSession.sName);
2745 fRc = oSession.takeScreenshot(sLastScreenshotPath);
2746 if fRc is not True:
2747 sLastScreenshotPath = None;
2748
2749 # Query the OS kernel log from the debugger if appropriate/requested.
2750 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2751 sOsKernelLog = oSession.queryOsKernelLog();
2752
2753 # Do "info vgatext all" separately.
2754 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2755 sVgaText = oSession.queryDbgInfoVgaText();
2756
2757 # Various infos (do after kernel because of symbols).
2758 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2759 # Dump the guest stack for all CPUs.
2760 cCpus = oSession.getCpuCount();
2761 if cCpus > 0:
2762 for iCpu in xrange(0, cCpus):
2763 sThis = oSession.queryDbgGuestStack(iCpu);
2764 if sThis:
2765 asMiscInfos += [
2766 '================ start guest stack VCPU %s ================\n' % (iCpu,),
2767 sThis,
2768 '================ end guest stack VCPU %s ==================\n' % (iCpu,),
2769 ];
2770
2771 for sInfo, sArg in [ ('mode', 'all'),
2772 ('fflags', ''),
2773 ('cpumguest', 'verbose all'),
2774 ('cpumguestinstr', 'symbol all'),
2775 ('pic', ''),
2776 ('apic', ''),
2777 ('apiclvt', ''),
2778 ('apictimer', ''),
2779 ('ioapic', ''),
2780 ('pit', ''),
2781 ('phys', ''),
2782 ('clocks', ''),
2783 ('timers', ''),
2784 ('gdtguest', ''),
2785 ('ldtguest', ''),
2786 ]:
2787 if sInfo in ['apic',] and self.fpApiVer < 5.1: # asserts and burns
2788 continue;
2789 sThis = oSession.queryDbgInfo(sInfo, sArg);
2790 if sThis:
2791 if sThis[-1] != '\n':
2792 sThis += '\n';
2793 asMiscInfos += [
2794 '================ start %s %s ================\n' % (sInfo, sArg),
2795 sThis,
2796 '================ end %s %s ==================\n' % (sInfo, sArg),
2797 ];
2798
2799 #
2800 # Terminate the VM
2801 #
2802
2803 # Cancel the progress object if specified.
2804 if oProgress is not None:
2805 if not oProgress.isCompleted() and oProgress.isCancelable():
2806 reporter.log2('terminateVmBySession: canceling "%s"...' % (oProgress.sName));
2807 try:
2808 oProgress.o.cancel();
2809 except:
2810 reporter.logXcpt();
2811 else:
2812 oProgress.wait();
2813 self.removeTask(oProgress);
2814
2815 # Check if the VM has terminated by itself before powering it off.
2816 fClose = True;
2817 fRc = True;
2818 if oSession.needsPoweringOff():
2819 reporter.log('terminateVmBySession: powering off "%s"...' % (oSession.sName,));
2820 fRc = oSession.powerOff(fFudgeOnFailure = False);
2821 if fRc is not True:
2822 # power off failed, try terminate it in a nice manner.
2823 fRc = False;
2824 uPid = oSession.getPid();
2825 if uPid is not None:
2826 #
2827 # Collect some information about the VM process first to have
2828 # some state information for further investigation why powering off failed.
2829 #
2830 sHostProcessInfoHung = utils.processGetInfo(uPid, fSudo = True);
2831
2832 # Exterminate...
2833 reporter.error('terminateVmBySession: Terminating PID %u (VM %s)' % (uPid, oSession.sName));
2834 fClose = base.processTerminate(uPid);
2835 if fClose is True:
2836 self.waitOnDirectSessionClose(oSession.oVM, 5000);
2837 fClose = oSession.waitForTask(1000);
2838
2839 if fClose is not True:
2840 # Being nice failed...
2841 reporter.error('terminateVmBySession: Termination failed, trying to kill PID %u (VM %s) instead' \
2842 % (uPid, oSession.sName));
2843 fClose = base.processKill(uPid);
2844 if fClose is True:
2845 self.waitOnDirectSessionClose(oSession.oVM, 5000);
2846 fClose = oSession.waitForTask(1000);
2847 if fClose is not True:
2848 reporter.error('terminateVmBySession: Failed to kill PID %u (VM %s)' % (uPid, oSession.sName));
2849
2850 # The final steps.
2851 if fClose is True:
2852 reporter.log('terminateVmBySession: closing session "%s"...' % (oSession.sName,));
2853 oSession.close();
2854 self.waitOnDirectSessionClose(oSession.oVM, 10000);
2855 try:
2856 eState = oSession.oVM.state;
2857 except:
2858 reporter.logXcpt();
2859 else:
2860 if eState == vboxcon.MachineState_Aborted:
2861 reporter.error('terminateVmBySession: The VM "%s" aborted!' % (oSession.sName,));
2862 self.removeTask(oSession);
2863
2864 #
2865 # Add the release log, debug log and a screenshot of the VM to the test report.
2866 #
2867 if self.fAlwaysUploadLogs or reporter.testErrorCount() > 0:
2868 oSession.addLogsToReport();
2869
2870 # Add a screenshot if it has been requested and taken successfully.
2871 if sLastScreenshotPath is not None:
2872 if reporter.testErrorCount() > 0:
2873 reporter.addLogFile(sLastScreenshotPath, 'screenshot/failure', 'Last VM screenshot');
2874 else:
2875 reporter.addLogFile(sLastScreenshotPath, 'screenshot/success', 'Last VM screenshot');
2876
2877 # Add the guest OS log if it has been requested and taken successfully.
2878 if sOsKernelLog is not None:
2879 reporter.addLogString(sOsKernelLog, 'kernel.log', 'log/guest/kernel', 'Guest OS kernel log');
2880
2881 # Add "info vgatext all" if we've got it.
2882 if sVgaText is not None:
2883 reporter.addLogString(sVgaText, 'vgatext.txt', 'info/vgatext', 'info vgatext all');
2884
2885 # Add the "info xxxx" items if we've got any.
2886 if asMiscInfos:
2887 reporter.addLogString(u''.join(asMiscInfos), 'info.txt', 'info/collection', 'A bunch of info items.');
2888
2889 # Add the host process info if we were able to retrieve it.
2890 if sHostProcessInfo is not None:
2891 reporter.log('Trying to annotate the VM process report, please stand by...');
2892 fRcTmp = self.annotateAndUploadProcessReport(sHostProcessInfo, 'vmprocess.log',
2893 'process/report/vm', 'Annotated VM process state');
2894 # Upload the raw log for manual annotation in case resolving failed.
2895 if not fRcTmp:
2896 reporter.log('Failed to annotate VM process report, uploading raw report');
2897 reporter.addLogString(sHostProcessInfo, 'vmprocess.log', 'process/report/vm', 'VM process state');
2898
2899 # Add the host process info for failed power off attempts if we were able to retrieve it.
2900 if sHostProcessInfoHung is not None:
2901 reporter.log('Trying to annotate the hung VM process report, please stand by...');
2902 fRcTmp = self.annotateAndUploadProcessReport(sHostProcessInfoHung, 'vmprocess-hung.log',
2903 'process/report/vm', 'Annotated hung VM process state');
2904 # Upload the raw log for manual annotation in case resolving failed.
2905 if not fRcTmp:
2906 reporter.log('Failed to annotate hung VM process report, uploading raw report');
2907 reporter.addLogString(sHostProcessInfoHung, 'vmprocess-hung.log', 'process/report/vm',
2908 'Hung VM process state');
2909
2910 return fRc;
2911
2912
2913 #
2914 # Some information query functions (mix).
2915 #
2916 # Methods require the VBox API. If the information is provided by both
2917 # the testboxscript as well as VBox API, we'll check if it matches.
2918 #
2919
2920 def _hasHostCpuFeature(self, sEnvVar, sEnum, fpApiMinVer, fQuiet):
2921 """
2922 Common Worker for hasHostNestedPaging() and hasHostHwVirt().
2923
2924 Returns True / False.
2925 Raises exception on environment / host mismatch.
2926 """
2927 fEnv = os.environ.get(sEnvVar, None);
2928 if fEnv is not None:
2929 fEnv = fEnv.lower() not in [ 'false', 'f', 'not', 'no', 'n', '0', ];
2930
2931 fVBox = None;
2932 self.importVBoxApi();
2933 if self.fpApiVer >= fpApiMinVer and hasattr(vboxcon, sEnum):
2934 try:
2935 fVBox = self.oVBox.host.getProcessorFeature(getattr(vboxcon, sEnum));
2936 except:
2937 if not fQuiet:
2938 reporter.logXcpt();
2939
2940 if fVBox is not None:
2941 if fEnv is not None:
2942 if fEnv != fVBox and not fQuiet:
2943 reporter.log('TestBox configuration overwritten: fVBox=%s (%s) vs. fEnv=%s (%s)'
2944 % (fVBox, sEnum, fEnv, sEnvVar));
2945 return fEnv;
2946 return fVBox;
2947 if fEnv is not None:
2948 return fEnv;
2949 return False;
2950
2951 def hasHostHwVirt(self, fQuiet = False):
2952 """
2953 Checks if hardware assisted virtualization is supported by the host.
2954
2955 Returns True / False.
2956 Raises exception on environment / host mismatch.
2957 """
2958 return self._hasHostCpuFeature('TESTBOX_HAS_HW_VIRT', 'ProcessorFeature_HWVirtEx', 3.1, fQuiet);
2959
2960 def hasHostNestedPaging(self, fQuiet = False):
2961 """
2962 Checks if nested paging is supported by the host.
2963
2964 Returns True / False.
2965 Raises exception on environment / host mismatch.
2966 """
2967 return self._hasHostCpuFeature('TESTBOX_HAS_NESTED_PAGING', 'ProcessorFeature_NestedPaging', 4.2, fQuiet) \
2968 and self.hasHostHwVirt(fQuiet);
2969
2970 def hasHostLongMode(self, fQuiet = False):
2971 """
2972 Checks if the host supports 64-bit guests.
2973
2974 Returns True / False.
2975 Raises exception on environment / host mismatch.
2976 """
2977 # Note that the testboxscript doesn't export this variable atm.
2978 return self._hasHostCpuFeature('TESTBOX_HAS_LONG_MODE', 'ProcessorFeature_LongMode', 3.1, fQuiet);
2979
2980 def getHostCpuCount(self, fQuiet = False):
2981 """
2982 Returns the number of CPUs on the host.
2983
2984 Returns True / False.
2985 Raises exception on environment / host mismatch.
2986 """
2987 cEnv = os.environ.get('TESTBOX_CPU_COUNT', None);
2988 if cEnv is not None:
2989 cEnv = int(cEnv);
2990
2991 try:
2992 cVBox = self.oVBox.host.processorOnlineCount;
2993 except:
2994 if not fQuiet:
2995 reporter.logXcpt();
2996 cVBox = None;
2997
2998 if cVBox is not None:
2999 if cEnv is not None:
3000 assert cVBox == cEnv, 'Misconfigured TestBox: VBox: %u CPUs, testboxscript: %u CPUs' % (cVBox, cEnv);
3001 return cVBox;
3002 if cEnv is not None:
3003 return cEnv;
3004 return 1;
3005
3006 def _getHostCpuDesc(self, fQuiet = False):
3007 """
3008 Internal method used for getting the host CPU description from VBoxSVC.
3009 Returns description string, on failure an empty string is returned.
3010 """
3011 try:
3012 return self.oVBox.host.getProcessorDescription(0);
3013 except:
3014 if not fQuiet:
3015 reporter.logXcpt();
3016 return '';
3017
3018 def isHostCpuAmd(self, fQuiet = False):
3019 """
3020 Checks if the host CPU vendor is AMD.
3021
3022 Returns True / False.
3023 """
3024 sCpuDesc = self._getHostCpuDesc(fQuiet);
3025 return sCpuDesc.startswith("AMD") or sCpuDesc == 'AuthenticAMD';
3026
3027 def isHostCpuIntel(self, fQuiet = False):
3028 """
3029 Checks if the host CPU vendor is Intel.
3030
3031 Returns True / False.
3032 """
3033 sCpuDesc = self._getHostCpuDesc(fQuiet);
3034 return sCpuDesc.startswith("Intel") or sCpuDesc == 'GenuineIntel';
3035
3036 def isHostCpuVia(self, fQuiet = False):
3037 """
3038 Checks if the host CPU vendor is VIA (or Centaur).
3039
3040 Returns True / False.
3041 """
3042 sCpuDesc = self._getHostCpuDesc(fQuiet);
3043 return sCpuDesc.startswith("VIA") or sCpuDesc == 'CentaurHauls';
3044
3045 def isHostCpuP4(self, fQuiet = False):
3046 """
3047 Checks if the host CPU is a Pentium 4 / Pentium D.
3048
3049 Returns True / False.
3050 """
3051 if not self.isHostCpuIntel(fQuiet):
3052 return False;
3053
3054 (uFamilyModel, _, _, _) = self.oVBox.host.getProcessorCPUIDLeaf(0, 0x1, 0);
3055 return ((uFamilyModel >> 8) & 0xf) == 0xf;
3056
3057 def hasRawModeSupport(self, fQuiet = False):
3058 """
3059 Checks if raw-mode is supported by VirtualBox that the testbox is
3060 configured for it.
3061
3062 Returns True / False.
3063 Raises no exceptions.
3064
3065 Note! Differs from the rest in that we don't require the
3066 TESTBOX_WITH_RAW_MODE value to match the API. It is
3067 sometimes helpful to disable raw-mode on individual
3068 test boxes. (This probably goes for
3069 """
3070 # The environment variable can be used to disable raw-mode.
3071 fEnv = os.environ.get('TESTBOX_WITH_RAW_MODE', None);
3072 if fEnv is not None:
3073 fEnv = fEnv.lower() not in [ 'false', 'f', 'not', 'no', 'n', '0', ];
3074 if fEnv is False:
3075 return False;
3076
3077 # Starting with 5.0 GA / RC2 the API can tell us whether VBox was built
3078 # with raw-mode support or not.
3079 self.importVBoxApi();
3080 if self.fpApiVer >= 5.0:
3081 try:
3082 fVBox = self.oVBox.systemProperties.rawModeSupported;
3083 except:
3084 if not fQuiet:
3085 reporter.logXcpt();
3086 fVBox = True;
3087 if fVBox is False:
3088 return False;
3089
3090 return True;
3091
3092 #
3093 # Testdriver execution methods.
3094 #
3095
3096 def handleTask(self, oTask, sMethod):
3097 """
3098 Callback method for handling unknown tasks in the various run loops.
3099
3100 The testdriver should override this if it already tasks running when
3101 calling startVmAndConnectToTxsViaTcp, txsRunTest or similar methods.
3102 Call super to handle unknown tasks.
3103
3104 Returns True if handled, False if not.
3105 """
3106 reporter.error('%s: unknown task %s' % (sMethod, oTask));
3107 return False;
3108
3109 def txsDoTask(self, oSession, oTxsSession, fnAsync, aArgs):
3110 """
3111 Generic TXS task wrapper which waits both on the TXS and the session tasks.
3112
3113 Returns False on error, logged.
3114
3115 Returns task result on success.
3116 """
3117 # All async methods ends with the following to args.
3118 cMsTimeout = aArgs[-2];
3119 fIgnoreErrors = aArgs[-1];
3120
3121 fRemoveVm = self.addTask(oSession);
3122 fRemoveTxs = self.addTask(oTxsSession);
3123
3124 rc = fnAsync(*aArgs); # pylint: disable=W0142
3125 if rc is True:
3126 rc = False;
3127 oTask = self.waitForTasks(cMsTimeout + 1);
3128 if oTask is oTxsSession:
3129 if oTxsSession.isSuccess():
3130 rc = oTxsSession.getResult();
3131 elif fIgnoreErrors is True:
3132 reporter.log( 'txsDoTask: task failed (%s)' % (oTxsSession.getLastReply()[1],));
3133 else:
3134 reporter.error('txsDoTask: task failed (%s)' % (oTxsSession.getLastReply()[1],));
3135 else:
3136 oTxsSession.cancelTask();
3137 if oTask is None:
3138 if fIgnoreErrors is True:
3139 reporter.log( 'txsDoTask: The task timed out.');
3140 else:
3141 reporter.errorTimeout('txsDoTask: The task timed out.');
3142 elif oTask is oSession:
3143 reporter.error('txsDoTask: The VM terminated unexpectedly');
3144 else:
3145 if fIgnoreErrors is True:
3146 reporter.log( 'txsDoTask: An unknown task %s was returned' % (oTask,));
3147 else:
3148 reporter.error('txsDoTask: An unknown task %s was returned' % (oTask,));
3149 else:
3150 reporter.error('txsDoTask: fnAsync returned %s' % (rc,));
3151
3152 if fRemoveTxs:
3153 self.removeTask(oTxsSession);
3154 if fRemoveVm:
3155 self.removeTask(oSession);
3156 return rc;
3157
3158 # pylint: disable=C0111
3159
3160 def txsDisconnect(self, oSession, oTxsSession, cMsTimeout = 30000, fIgnoreErrors = False):
3161 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncDisconnect,
3162 (self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3163
3164 def txsUuid(self, oSession, oTxsSession, cMsTimeout = 30000, fIgnoreErrors = False):
3165 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncUuid,
3166 (self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3167
3168 def txsMkDir(self, oSession, oTxsSession, sRemoteDir, fMode = 0o700, cMsTimeout = 30000, fIgnoreErrors = False):
3169 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncMkDir,
3170 (sRemoteDir, fMode, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3171
3172 def txsMkDirPath(self, oSession, oTxsSession, sRemoteDir, fMode = 0o700, cMsTimeout = 30000, fIgnoreErrors = False):
3173 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncMkDirPath,
3174 (sRemoteDir, fMode, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3175
3176 def txsMkSymlink(self, oSession, oTxsSession, sLinkTarget, sLink, cMsTimeout = 30000, fIgnoreErrors = False):
3177 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncMkSymlink,
3178 (sLinkTarget, sLink, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3179
3180 def txsRmDir(self, oSession, oTxsSession, sRemoteDir, cMsTimeout = 30000, fIgnoreErrors = False):
3181 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncRmDir,
3182 (sRemoteDir, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3183
3184 def txsRmFile(self, oSession, oTxsSession, sRemoteFile, cMsTimeout = 30000, fIgnoreErrors = False):
3185 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncRmFile,
3186 (sRemoteFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3187
3188 def txsRmSymlink(self, oSession, oTxsSession, sRemoteSymlink, cMsTimeout = 30000, fIgnoreErrors = False):
3189 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncRmSymlink,
3190 (sRemoteSymlink, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3191
3192 def txsRmTree(self, oSession, oTxsSession, sRemoteTree, cMsTimeout = 30000, fIgnoreErrors = False):
3193 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncRmTree,
3194 (sRemoteTree, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3195
3196 def txsIsDir(self, oSession, oTxsSession, sRemoteDir, cMsTimeout = 30000, fIgnoreErrors = False):
3197 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncIsDir,
3198 (sRemoteDir, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3199
3200 def txsIsFile(self, oSession, oTxsSession, sRemoteFile, cMsTimeout = 30000, fIgnoreErrors = False):
3201 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncIsFile,
3202 (sRemoteFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3203
3204 def txsIsSymlink(self, oSession, oTxsSession, sRemoteSymlink, cMsTimeout = 30000, fIgnoreErrors = False):
3205 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncIsSymlink,
3206 (sRemoteSymlink, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3207
3208 def txsUploadFile(self, oSession, oTxsSession, sLocalFile, sRemoteFile, cMsTimeout = 30000, fIgnoreErrors = False):
3209 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncUploadFile, \
3210 (sLocalFile, sRemoteFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3211
3212 def txsUploadString(self, oSession, oTxsSession, sContent, sRemoteFile, cMsTimeout = 30000, fIgnoreErrors = False):
3213 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncUploadString, \
3214 (sContent, sRemoteFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3215
3216 def txsDownloadFile(self, oSession, oTxsSession, sRemoteFile, sLocalFile, cMsTimeout = 30000, fIgnoreErrors = False):
3217 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncDownloadFile, \
3218 (sRemoteFile, sLocalFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3219
3220 def txsDownloadFiles(self, oSession, oTxsSession, asFiles, fIgnoreErrors = False):
3221 """
3222 Convenience function to get files from the guest and stores it
3223 into the scratch directory for later (manual) review.
3224
3225 Returns True on success.
3226
3227 Returns False on failure, logged.
3228 """
3229 fRc = True;
3230 for sGstFile in asFiles:
3231 ## @todo Check for already existing files on the host and create a new
3232 # name for the current file to download.
3233 sTmpFile = os.path.join(self.sScratchPath, 'tmp-' + os.path.basename(sGstFile));
3234 reporter.log2('Downloading file "%s" to "%s" ...' % (sGstFile, sTmpFile));
3235 fRc = self.txsDownloadFile(oSession, oTxsSession, sGstFile, sTmpFile, 30 * 1000, fIgnoreErrors);
3236 try: os.unlink(sTmpFile);
3237 except: pass;
3238 if fRc:
3239 reporter.addLogFile(sTmpFile, 'misc/other', 'guest - ' + sGstFile);
3240 else:
3241 if fIgnoreErrors is not True:
3242 reporter.error('error downloading file "%s" to "%s"' % (sGstFile, sTmpFile));
3243 return fRc;
3244 reporter.log('warning: file "%s" was not downloaded, ignoring.' % (sGstFile,));
3245 return True;
3246
3247 def txsDownloadString(self, oSession, oTxsSession, sRemoteFile, cMsTimeout = 30000, fIgnoreErrors = False):
3248 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncDownloadString,
3249 (sRemoteFile, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3250
3251 def txsUnpackFile(self, oSession, oTxsSession, sRemoteFile, sRemoteDir, cMsTimeout = 30000, fIgnoreErrors = False):
3252 return self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncUnpackFile, \
3253 (sRemoteFile, sRemoteDir, self.adjustTimeoutMs(cMsTimeout), fIgnoreErrors));
3254
3255 # pylint: enable=C0111
3256
3257 def txsCdWait(self,
3258 oSession, # type: vboxwrappers.SessionWrapper
3259 oTxsSession, # type: txsclient.Session
3260 cMsTimeout = 30000, # type: int
3261 sFile = None # type: String
3262 ): # -> bool
3263 """
3264 Mostly an internal helper for txsRebootAndReconnectViaTcp and
3265 startVmAndConnectToTxsViaTcp that waits for the CDROM drive to become
3266 ready. It does this by polling for a file it knows to exist on the CD.
3267
3268 Returns True on success.
3269
3270 Returns False on failure, logged.
3271 """
3272
3273 if sFile is None:
3274 sFile = 'valkit.txt';
3275 fRemoveVm = self.addTask(oSession);
3276 fRemoveTxs = self.addTask(oTxsSession);
3277 cMsTimeout = self.adjustTimeoutMs(cMsTimeout);
3278 msStart = base.timestampMilli();
3279 cMsTimeout2 = cMsTimeout;
3280 fRc = oTxsSession.asyncIsFile('${CDROM}/%s' % (sFile,), cMsTimeout2);
3281 if fRc is True:
3282 while True:
3283 # wait for it to complete.
3284 oTask = self.waitForTasks(cMsTimeout2 + 1);
3285 if oTask is not oTxsSession:
3286 oTxsSession.cancelTask();
3287 if oTask is None:
3288 reporter.errorTimeout('txsCdWait: The task timed out (after %s ms).'
3289 % (base.timestampMilli() - msStart,));
3290 elif oTask is oSession:
3291 reporter.error('txsCdWait: The VM terminated unexpectedly');
3292 else:
3293 reporter.error('txsCdWait: An unknown task %s was returned' % (oTask,));
3294 fRc = False;
3295 break;
3296 if oTxsSession.isSuccess():
3297 break;
3298
3299 # Check for timeout.
3300 cMsElapsed = base.timestampMilli() - msStart;
3301 if cMsElapsed >= cMsTimeout:
3302 reporter.error('txsCdWait: timed out');
3303 fRc = False;
3304 break;
3305
3306 # delay.
3307 self.sleep(1);
3308
3309 # resubmitt the task.
3310 cMsTimeout2 = msStart + cMsTimeout - base.timestampMilli();
3311 if cMsTimeout2 < 500:
3312 cMsTimeout2 = 500;
3313 fRc = oTxsSession.asyncIsFile('${CDROM}/%s' % (sFile,), cMsTimeout2);
3314 if fRc is not True:
3315 reporter.error('txsCdWait: asyncIsFile failed');
3316 break;
3317 else:
3318 reporter.error('txsCdWait: asyncIsFile failed');
3319
3320 if fRemoveTxs:
3321 self.removeTask(oTxsSession);
3322 if fRemoveVm:
3323 self.removeTask(oSession);
3324 return fRc;
3325
3326 def txsDoConnectViaTcp(self, oSession, cMsTimeout, fNatForwardingForTxs = False):
3327 """
3328 Mostly an internal worker for connecting to TXS via TCP used by the
3329 *ViaTcp methods.
3330
3331 Returns a tuplet with True/False and TxsSession/None depending on the
3332 result. Errors are logged.
3333 """
3334
3335 reporter.log2('txsDoConnectViaTcp: oSession=%s, cMsTimeout=%s, fNatForwardingForTxs=%s'
3336 % (oSession, cMsTimeout, fNatForwardingForTxs));
3337
3338 cMsTimeout = self.adjustTimeoutMs(cMsTimeout);
3339 oTxsConnect = oSession.txsConnectViaTcp(cMsTimeout, fNatForwardingForTxs = fNatForwardingForTxs);
3340 if oTxsConnect is not None:
3341 self.addTask(oTxsConnect);
3342 fRemoveVm = self.addTask(oSession);
3343 oTask = self.waitForTasks(cMsTimeout + 1);
3344 reporter.log2('txsDoConnectViaTcp: waitForTasks returned %s' % (oTask,));
3345 self.removeTask(oTxsConnect);
3346 if oTask is oTxsConnect:
3347 oTxsSession = oTxsConnect.getResult();
3348 if oTxsSession is not None:
3349 reporter.log('txsDoConnectViaTcp: Connected to TXS on %s.' % (oTxsSession.oTransport.sHostname,));
3350 return (True, oTxsSession);
3351
3352 reporter.error('txsDoConnectViaTcp: failed to connect to TXS.');
3353 else:
3354 oTxsConnect.cancelTask();
3355 if oTask is None:
3356 reporter.errorTimeout('txsDoConnectViaTcp: connect stage 1 timed out');
3357 elif oTask is oSession:
3358 oSession.reportPrematureTermination('txsDoConnectViaTcp: ');
3359 else:
3360 reporter.error('txsDoConnectViaTcp: unknown/wrong task %s' % (oTask,));
3361 if fRemoveVm:
3362 self.removeTask(oSession);
3363 else:
3364 reporter.error('txsDoConnectViaTcp: txsConnectViaTcp failed');
3365 return (False, None);
3366
3367 def startVmAndConnectToTxsViaTcp(self, sVmName, fCdWait = False, cMsTimeout = 15*60000, \
3368 cMsCdWait = 30000, sFileCdWait = None, \
3369 fNatForwardingForTxs = False):
3370 """
3371 Starts the specified VM and tries to connect to its TXS via TCP.
3372 The VM will be powered off if TXS doesn't respond before the specified
3373 time has elapsed.
3374
3375 Returns a the VM and TXS sessions (a two tuple) on success. The VM
3376 session is in the task list, the TXS session is not.
3377 Returns (None, None) on failure, fully logged.
3378 """
3379
3380 # Zap the guest IP to make sure we're not getting a stale entry
3381 # (unless we're restoring the VM of course).
3382 oTestVM = self.oTestVmSet.findTestVmByName(sVmName) if self.oTestVmSet is not None else None;
3383 if oTestVM is None \
3384 or oTestVM.fSnapshotRestoreCurrent is False:
3385 try:
3386 oSession1 = self.openSession(self.getVmByName(sVmName));
3387 oSession1.delGuestPropertyValue('/VirtualBox/GuestInfo/Net/0/V4/IP');
3388 oSession1.saveSettings(True);
3389 del oSession1;
3390 except:
3391 reporter.logXcpt();
3392
3393 # Start the VM.
3394 reporter.log('startVmAndConnectToTxsViaTcp: Starting(/preparing) "%s" (timeout %s s)...' % (sVmName, cMsTimeout / 1000));
3395 reporter.flushall();
3396 oSession = self.startVmByName(sVmName);
3397 if oSession is not None:
3398 # Connect to TXS.
3399 reporter.log2('startVmAndConnectToTxsViaTcp: Started(/prepared) "%s", connecting to TXS ...' % (sVmName,));
3400 (fRc, oTxsSession) = self.txsDoConnectViaTcp(oSession, cMsTimeout, fNatForwardingForTxs);
3401 if fRc is True:
3402 if fCdWait:
3403 # Wait for CD?
3404 fRc = self.txsCdWait(oSession, oTxsSession, cMsCdWait, sFileCdWait);
3405 if fRc is not True:
3406 reporter.error('startVmAndConnectToTxsViaTcp: txsCdWait failed');
3407 if fRc is True:
3408 # Success!
3409 return (oSession, oTxsSession);
3410 else:
3411 reporter.error('startVmAndConnectToTxsViaTcp: txsDoConnectViaTcp failed');
3412 # If something went wrong while waiting for TXS to be started - take VM screenshot before terminate it
3413 self.terminateVmBySession(oSession);
3414 return (None, None);
3415
3416 def txsRebootAndReconnectViaTcp(self, oSession, oTxsSession, fCdWait = False, cMsTimeout = 15*60000, \
3417 cMsCdWait = 30000, sFileCdWait = None, fNatForwardingForTxs = False):
3418 """
3419 Executes the TXS reboot command
3420
3421 Returns A tuple of True and the new TXS session on success.
3422
3423 Returns A tuple of False and either the old TXS session or None on failure.
3424 """
3425 reporter.log2('txsRebootAndReconnect: cMsTimeout=%u' % (cMsTimeout,));
3426
3427 #
3428 # This stuff is a bit complicated because of rebooting being kind of
3429 # disruptive to the TXS and such... The protocol is that TXS will:
3430 # - ACK the reboot command.
3431 # - Shutdown the transport layer, implicitly disconnecting us.
3432 # - Execute the reboot operation.
3433 # - On failure, it will be re-init the transport layer and be
3434 # available pretty much immediately. UUID unchanged.
3435 # - On success, it will be respawed after the reboot (hopefully),
3436 # with a different UUID.
3437 #
3438 fRc = False;
3439 iStart = base.timestampMilli();
3440
3441 # Get UUID.
3442 cMsTimeout2 = min(60000, cMsTimeout);
3443 sUuidBefore = self.txsUuid(oSession, oTxsSession, self.adjustTimeoutMs(cMsTimeout2, 60000));
3444 if sUuidBefore is not False:
3445 # Reboot.
3446 cMsElapsed = base.timestampMilli() - iStart;
3447 cMsTimeout2 = cMsTimeout - cMsElapsed;
3448 fRc = self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncReboot,
3449 (self.adjustTimeoutMs(cMsTimeout2, 60000), False));
3450 if fRc is True:
3451 # Reconnect.
3452 if fNatForwardingForTxs is True:
3453 self.sleep(22); # NAT fudge - Two fixes are wanted: 1. TXS connect retries. 2. Main API reboot/reset hint.
3454 cMsElapsed = base.timestampMilli() - iStart;
3455 (fRc, oTxsSession) = self.txsDoConnectViaTcp(oSession, cMsTimeout - cMsElapsed, fNatForwardingForTxs);
3456 if fRc is True:
3457 # Check the UUID.
3458 cMsElapsed = base.timestampMilli() - iStart;
3459 cMsTimeout2 = min(60000, cMsTimeout - cMsElapsed);
3460 sUuidAfter = self.txsDoTask(oSession, oTxsSession, oTxsSession.asyncUuid,
3461 (self.adjustTimeoutMs(cMsTimeout2, 60000), False));
3462 if sUuidBefore is not False:
3463 if sUuidAfter != sUuidBefore:
3464 reporter.log('The guest rebooted (UUID %s -> %s)' % (sUuidBefore, sUuidAfter))
3465
3466 # Do CD wait if specified.
3467 if fCdWait:
3468 fRc = self.txsCdWait(oSession, oTxsSession, cMsCdWait, sFileCdWait);
3469 if fRc is not True:
3470 reporter.error('txsRebootAndReconnectViaTcp: txsCdWait failed');
3471 else:
3472 reporter.error('txsRebootAndReconnectViaTcp: failed to get UUID (after)');
3473 else:
3474 reporter.error('txsRebootAndReconnectViaTcp: did not reboot (UUID %s)' % (sUuidBefore,));
3475 else:
3476 reporter.error('txsRebootAndReconnectViaTcp: txsDoConnectViaTcp failed');
3477 else:
3478 reporter.error('txsRebootAndReconnectViaTcp: reboot failed');
3479 else:
3480 reporter.error('txsRebootAndReconnectViaTcp: failed to get UUID (before)');
3481 return (fRc, oTxsSession);
3482
3483 # pylint: disable=R0914,R0913
3484
3485 def txsRunTest(self, oTxsSession, sTestName, cMsTimeout, sExecName, asArgs = (), asAddEnv = (), sAsUser = ""):
3486 """
3487 Executes the specified test task, waiting till it completes or times out.
3488
3489 The VM session (if any) must be in the task list.
3490
3491 Returns True if we executed the task and nothing abnormal happend.
3492 Query the process status from the TXS session.
3493
3494 Returns False if some unexpected task was signalled or we failed to
3495 submit the job.
3496 """
3497 reporter.testStart(sTestName);
3498 reporter.log2('txsRunTest: cMsTimeout=%u sExecName=%s asArgs=%s' % (cMsTimeout, sExecName, asArgs));
3499
3500 # Submit the job.
3501 fRc = False;
3502 if oTxsSession.asyncExec(sExecName, asArgs, asAddEnv, sAsUser, cMsTimeout = self.adjustTimeoutMs(cMsTimeout)):
3503 self.addTask(oTxsSession);
3504
3505 # Wait for the job to complete.
3506 while True:
3507 oTask = self.waitForTasks(cMsTimeout + 1);
3508 if oTask is None:
3509 reporter.log('txsRunTest: waitForTasks timed out');
3510 break;
3511 if oTask is oTxsSession:
3512 fRc = True;
3513 reporter.log('txsRunTest: isSuccess=%s getResult=%s' % (oTxsSession.isSuccess(), oTxsSession.getResult()));
3514 break;
3515 if not self.handleTask(oTask, 'txsRunTest'):
3516 break;
3517
3518 self.removeTask(oTxsSession);
3519 if not oTxsSession.pollTask():
3520 oTxsSession.cancelTask();
3521 else:
3522 reporter.error('txsRunTest: asyncExec failed');
3523
3524 reporter.testDone();
3525 return fRc;
3526
3527 def txsRunTestRedirectStd(self, oTxsSession, sTestName, cMsTimeout, sExecName, asArgs = (), asAddEnv = (), sAsUser = "",
3528 oStdIn = '/dev/null', oStdOut = '/dev/null', oStdErr = '/dev/null', oTestPipe = '/dev/null'):
3529 """
3530 Executes the specified test task, waiting till it completes or times out,
3531 redirecting stdin, stdout and stderr to the given objects.
3532
3533 The VM session (if any) must be in the task list.
3534
3535 Returns True if we executed the task and nothing abnormal happend.
3536 Query the process status from the TXS session.
3537
3538 Returns False if some unexpected task was signalled or we failed to
3539 submit the job.
3540 """
3541 reporter.testStart(sTestName);
3542 reporter.log2('txsRunTestRedirectStd: cMsTimeout=%u sExecName=%s asArgs=%s' % (cMsTimeout, sExecName, asArgs));
3543
3544 # Submit the job.
3545 fRc = False;
3546 if oTxsSession.asyncExecEx(sExecName, asArgs, asAddEnv, oStdIn, oStdOut, oStdErr,
3547 oTestPipe, sAsUser, cMsTimeout = self.adjustTimeoutMs(cMsTimeout)):
3548 self.addTask(oTxsSession);
3549
3550 # Wait for the job to complete.
3551 while True:
3552 oTask = self.waitForTasks(cMsTimeout + 1);
3553 if oTask is None:
3554 reporter.log('txsRunTestRedirectStd: waitForTasks timed out');
3555 break;
3556 if oTask is oTxsSession:
3557 fRc = True;
3558 reporter.log('txsRunTestRedirectStd: isSuccess=%s getResult=%s'
3559 % (oTxsSession.isSuccess(), oTxsSession.getResult()));
3560 break;
3561 if not self.handleTask(oTask, 'txsRunTestRedirectStd'):
3562 break;
3563
3564 self.removeTask(oTxsSession);
3565 if not oTxsSession.pollTask():
3566 oTxsSession.cancelTask();
3567 else:
3568 reporter.error('txsRunTestRedirectStd: asyncExec failed');
3569
3570 reporter.testDone();
3571 return fRc;
3572
3573 def txsRunTest2(self, oTxsSession1, oTxsSession2, sTestName, cMsTimeout,
3574 sExecName1, asArgs1,
3575 sExecName2, asArgs2,
3576 asAddEnv1 = (), sAsUser1 = '', fWithTestPipe1 = True,
3577 asAddEnv2 = (), sAsUser2 = '', fWithTestPipe2 = True):
3578 """
3579 Executes the specified test tasks, waiting till they complete or
3580 times out. The 1st task is started after the 2nd one.
3581
3582 The VM session (if any) must be in the task list.
3583
3584 Returns True if we executed the task and nothing abnormal happend.
3585 Query the process status from the TXS sessions.
3586
3587 Returns False if some unexpected task was signalled or we failed to
3588 submit the job.
3589 """
3590 reporter.testStart(sTestName);
3591
3592 # Submit the jobs.
3593 fRc = False;
3594 if oTxsSession1.asyncExec(sExecName1, asArgs1, asAddEnv1, sAsUser1, fWithTestPipe1, '1-',
3595 self.adjustTimeoutMs(cMsTimeout)):
3596 self.addTask(oTxsSession1);
3597
3598 self.sleep(2); # fudge! grr
3599
3600 if oTxsSession2.asyncExec(sExecName2, asArgs2, asAddEnv2, sAsUser2, fWithTestPipe2, '2-',
3601 self.adjustTimeoutMs(cMsTimeout)):
3602 self.addTask(oTxsSession2);
3603
3604 # Wait for the jobs to complete.
3605 cPendingJobs = 2;
3606 while True:
3607 oTask = self.waitForTasks(cMsTimeout + 1);
3608 if oTask is None:
3609 reporter.log('txsRunTest2: waitForTasks timed out');
3610 break;
3611
3612 if oTask is oTxsSession1 or oTask is oTxsSession2:
3613 if oTask is oTxsSession1: iTask = 1;
3614 else: iTask = 2;
3615 reporter.log('txsRunTest2: #%u - isSuccess=%s getResult=%s' \
3616 % (iTask, oTask.isSuccess(), oTask.getResult()));
3617 self.removeTask(oTask);
3618 cPendingJobs -= 1;
3619 if cPendingJobs <= 0:
3620 fRc = True;
3621 break;
3622
3623 elif not self.handleTask(oTask, 'txsRunTest'):
3624 break;
3625
3626 self.removeTask(oTxsSession2);
3627 if not oTxsSession2.pollTask():
3628 oTxsSession2.cancelTask();
3629 else:
3630 reporter.error('txsRunTest2: asyncExec #2 failed');
3631
3632 self.removeTask(oTxsSession1);
3633 if not oTxsSession1.pollTask():
3634 oTxsSession1.cancelTask();
3635 else:
3636 reporter.error('txsRunTest2: asyncExec #1 failed');
3637
3638 reporter.testDone();
3639 return fRc;
3640
3641 # pylint: enable=R0914,R0913
3642
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