VirtualBox

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

Last change on this file since 94155 was 94126, checked in by vboxsync, 3 years ago

ValKit/testdriver: pylint 2.9.6 adjustments (mostly about using 'with' statements).

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