summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/testdriver/vboxinstaller.py
blob: 5ddca0f49b7b5535d0b15e5bfc9aa21cdf657115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
VirtualBox Installer Wrapper Driver.

This installs VirtualBox, starts a sub driver which does the real testing,
and then uninstall VirtualBox afterwards.  This reduces the complexity of the
other VBox test drivers.
"""

__copyright__ = \
"""
Copyright (C) 2010-2019 Oracle Corporation

This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
you can redistribute it and/or modify it under the terms of the GNU
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.

The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.

You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
__version__ = "$Revision: 128281 $"


# Standard Python imports.
import os
import sys
import re
#import socket
import tempfile
import time

# Only the main script needs to modify the path.
try:    __file__
except: __file__ = sys.argv[0];
g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)));
sys.path.append(g_ksValidationKitDir);

# Validation Kit imports.
from common             import utils, webutils;
from common.constants   import rtexitcode;
from testdriver         import reporter;
from testdriver.base    import TestDriverBase;



class VBoxInstallerTestDriver(TestDriverBase):
    """
    Implementation of a top level test driver.
    """


    ## State file indicating that we've skipped installation.
    ksVar_Skipped = 'vboxinstaller-skipped';


    def __init__(self):
        TestDriverBase.__init__(self);
        self._asSubDriver   = [];   # The sub driver and it's arguments.
        self._asBuildUrls   = [];   # The URLs passed us on the command line.
        self._asBuildFiles  = [];   # The downloaded file names.
        self._fAutoInstallPuelExtPack = True;

    #
    # Base method we override
    #

    def showUsage(self):
        rc = TestDriverBase.showUsage(self);
        #             0         1         2         3         4         5         6         7         8
        #             012345678901234567890123456789012345678901234567890123456789012345678901234567890
        reporter.log('');
        reporter.log('vboxinstaller Options:');
        reporter.log('  --vbox-build    <url[,url2[,..]]>');
        reporter.log('      Comma separated list of URL to file to download and install or/and');
        reporter.log('      unpack.  URLs without a schema are assumed to be files on the');
        reporter.log('      build share and will be copied off it.');
        reporter.log('  --no-puel-extpack');
        reporter.log('      Indicates that the PUEL extension pack should not be installed if found.');
        reporter.log('      The default is to install it if found in the vbox-build.');
        reporter.log('  --');
        reporter.log('      Indicates the end of our parameters and the start of the sub');
        reporter.log('      testdriver and its arguments.');
        return rc;

    def parseOption(self, asArgs, iArg):
        """
        Parse our arguments.
        """
        if asArgs[iArg] == '--':
            # End of our parameters and start of the sub driver invocation.
            iArg = self.requireMoreArgs(1, asArgs, iArg);
            assert not self._asSubDriver;
            self._asSubDriver = asArgs[iArg:];
            self._asSubDriver[0] = self._asSubDriver[0].replace('/', os.path.sep);
            iArg = len(asArgs) - 1;
        elif asArgs[iArg] == '--vbox-build':
            # List of files to copy/download and install.
            iArg = self.requireMoreArgs(1, asArgs, iArg);
            self._asBuildUrls = asArgs[iArg].split(',');
        elif asArgs[iArg] == '--no-puel-extpack':
            self._fAutoInstallPuelExtPack = False;
        elif asArgs[iArg] == '--puel-extpack':
            self._fAutoInstallPuelExtPack = True;
        else:
            return TestDriverBase.parseOption(self, asArgs, iArg);
        return iArg + 1;

    def completeOptions(self):
        #
        # Check that we've got what we need.
        #
        if not self._asBuildUrls:
            reporter.error('No build files specfiied ("--vbox-build file1[,file2[...]]")');
            return False;
        if not self._asSubDriver:
            reporter.error('No sub testdriver specified. (" -- test/stuff/tdStuff1.py args")');
            return False;

        #
        # Construct _asBuildFiles as an array parallel to _asBuildUrls.
        #
        for sUrl in self._asBuildUrls:
            sDstFile = os.path.join(self.sScratchPath, webutils.getFilename(sUrl));
            self._asBuildFiles.append(sDstFile);

        return TestDriverBase.completeOptions(self);

    def actionExtract(self):
        reporter.error('vboxinstall does not support extracting resources, you have to do that using the sub testdriver.');
        return False;

    def actionCleanupBefore(self):
        """
        Kills all VBox process we see.

        This is only supposed to execute on a testbox so we don't need to go
        all complicated wrt other users.
        """
        return self._killAllVBoxProcesses();

    def actionConfig(self):
        """
        Install VBox and pass on the configure request to the sub testdriver.
        """
        fRc = self._installVBox();
        if fRc is None: self._persistentVarSet(self.ksVar_Skipped, 'true');
        else:           self._persistentVarUnset(self.ksVar_Skipped);

        ## @todo vbox.py still has bugs preventing us from invoking it seperately with each action.
        if fRc is True and 'execute' not in self.asActions and 'all' not in self.asActions:
            fRc = self._executeSubDriver([ 'verify', ]);
        if fRc is True and 'execute' not in self.asActions and 'all' not in self.asActions:
            fRc = self._executeSubDriver([ 'config', ]);
        return fRc;

    def actionExecute(self):
        """
        Execute the sub testdriver.
        """
        return self._executeSubDriver(self.asActions);

    def actionCleanupAfter(self):
        """
        Forward this to the sub testdriver, then uninstall VBox.
        """
        fRc = True;
        if 'execute' not in self.asActions and 'all' not in self.asActions:
            fRc = self._executeSubDriver([ 'cleanup-after', ], fMaySkip = False);

        if not self._killAllVBoxProcesses():
            fRc = False;

        if not self._uninstallVBox(self._persistentVarExists(self.ksVar_Skipped)):
            fRc = False;

        if utils.getHostOs() == 'darwin':
            self._darwinUnmountDmg(fIgnoreError = True); # paranoia

        if not TestDriverBase.actionCleanupAfter(self):
            fRc = False;

        return fRc;


    def actionAbort(self):
        """
        Forward this to the sub testdriver first, then wipe all VBox like
        processes, and finally do the pid file processing (again).
        """
        fRc1 = self._executeSubDriver([ 'abort', ], fMaySkip = False);
        fRc2 = self._killAllVBoxProcesses();
        fRc3 = TestDriverBase.actionAbort(self);
        return fRc1 and fRc2 and fRc3;


    #
    # Persistent variables.
    #
    ## @todo integrate into the base driver. Persisten accross scratch wipes?

    def __persistentVarCalcName(self, sVar):
        """Returns the (full) filename for the given persistent variable."""
        assert re.match(r'^[a-zA-Z0-9_-]*$', sVar) is not None;
        return os.path.join(self.sScratchPath, 'persistent-%s.var' % (sVar,));

    def _persistentVarSet(self, sVar, sValue = ''):
        """
        Sets a persistent variable.

        Returns True on success, False + reporter.error on failure.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        try:
            oFile = open(sFull, 'w');
            if sValue:
                oFile.write(sValue.encode('utf-8'));
            oFile.close();
        except:
            reporter.errorXcpt('Error creating "%s"' % (sFull,));
            return False;
        return True;

    def _persistentVarUnset(self, sVar):
        """
        Unsets a persistent variable.

        Returns True on success, False + reporter.error on failure.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        if os.path.exists(sFull):
            try:
                os.unlink(sFull);
            except:
                reporter.errorXcpt('Error unlinking "%s"' % (sFull,));
                return False;
        return True;

    def _persistentVarExists(self, sVar):
        """
        Checks if a persistent variable exists.

        Returns true/false.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        return os.path.exists(self.__persistentVarCalcName(sVar));

    def _persistentVarGet(self, sVar):
        """
        Gets the value of a persistent variable.

        Returns variable value on success.
        Returns None if the variable doesn't exist or if an
        error (reported) occured.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        if not os.path.exists(sFull):
            return None;
        try:
            oFile = open(sFull, 'r');
            sValue = oFile.read().decode('utf-8');
            oFile.close();
        except:
            reporter.errorXcpt('Error creating "%s"' % (sFull,));
            return None;
        return sValue;


    #
    # Helpers.
    #

    def _killAllVBoxProcesses(self):
        """
        Kills all virtual box related processes we find in the system.
        """

        for iIteration in range(22):
            # Gather processes to kill.
            aoTodo = [];
            for oProcess in utils.processListAll():
                sBase = oProcess.getBaseImageNameNoExeSuff();
                if sBase is None:
                    continue;
                sBase = sBase.lower();
                if sBase in [ 'vboxsvc', 'virtualbox', 'virtualboxvm', 'vboxheadless', 'vboxmanage', 'vboxsdl', 'vboxwebsrv',
                              'vboxautostart', 'vboxballoonctrl', 'vboxbfe', 'vboxextpackhelperapp', 'vboxnetdhcp',
                              'vboxnetadpctl', 'vboxtestogl', 'vboxtunctl', 'vboxvmmpreload', 'vboxxpcomipcd', 'vmCreator', ]:
                    aoTodo.append(oProcess);
                if sBase.startswith('virtualbox-') and sBase.endswith('-multiarch.exe'):
                    aoTodo.append(oProcess);
                if iIteration in [0, 21]  and  sBase in [ 'windbg', 'gdb', 'gdb-i386-apple-darwin', ]:
                    reporter.log('Warning: debugger running: %s (%s)' % (oProcess.iPid, sBase,));
            if not aoTodo:
                return True;

            # Kill.
            for oProcess in aoTodo:
                reporter.log('Loop #%d - Killing %s (%s, uid=%s)'
                             % ( iIteration, oProcess.iPid, oProcess.sImage if oProcess.sName is None else oProcess.sName,
                                 oProcess.iUid, ));
                utils.processKill(oProcess.iPid); # No mercy.

            # Check if they're all dead like they should be.
            time.sleep(0.1);
            for oProcess in aoTodo:
                if utils.processExists(oProcess.iPid):
                    time.sleep(2);
                    break;

        return False;

    def _executeSync(self, asArgs, fMaySkip = False):
        """
        Executes a child process synchronously.

        Returns True if the process executed successfully and returned 0.
        Returns None if fMaySkip is true and the child exits with RTEXITCODE_SKIPPED.
        Returns False for all other cases.
        """
        reporter.log('Executing: %s' % (asArgs, ));
        reporter.flushall();
        try:
            iRc = utils.processCall(asArgs, shell = False, close_fds = False);
        except:
            reporter.errorXcpt();
            return False;
        reporter.log('Exit code: %s (%s)' % (iRc, asArgs));
        if fMaySkip and iRc == rtexitcode.RTEXITCODE_SKIPPED:
            return None;
        return iRc is 0;

    def _sudoExecuteSync(self, asArgs):
        """
        Executes a sudo child process synchronously.
        Returns a tuple [True, 0] if the process executed successfully
        and returned 0, otherwise [False, rc] is returned.
        """
        reporter.log('Executing [sudo]: %s' % (asArgs, ));
        reporter.flushall();
        iRc = 0;
        try:
            iRc = utils.sudoProcessCall(asArgs, shell = False, close_fds = False);
        except:
            reporter.errorXcpt();
            return (False, 0);
        reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs));
        return (iRc is 0, iRc);

    def _executeSubDriver(self, asActions, fMaySkip = True):
        """
        Execute the sub testdriver with the specified action.
        """
        asArgs = list(self._asSubDriver)
        asArgs.append('--no-wipe-clean');
        asArgs.extend(asActions);
        return self._executeSync(asArgs, fMaySkip = fMaySkip);

    def _maybeUnpackArchive(self, sMaybeArchive, fNonFatal = False):
        """
        Attempts to unpack the given build file.
        Updates _asBuildFiles.
        Returns True/False. No exceptions.
        """
        def unpackFilter(sMember):
            # type: (string) -> bool
            """ Skips debug info. """
            sLower = sMember.lower();
            if sLower.endswith('.pdb'):
                return False;
            return True;

        asMembers = utils.unpackFile(sMaybeArchive, self.sScratchPath, reporter.log,
                                     reporter.log if fNonFatal else reporter.error,
                                     fnFilter = unpackFilter);
        if asMembers is None:
            return False;
        self._asBuildFiles.extend(asMembers);
        return True;


    def _installVBox(self):
        """
        Download / copy the build files into the scratch area and install them.
        """
        reporter.testStart('Installing VirtualBox');
        reporter.log('CWD=%s' % (os.getcwd(),)); # curious

        #
        # Download the build files.
        #
        for i in range(len(self._asBuildUrls)):
            if webutils.downloadFile(self._asBuildUrls[i], self._asBuildFiles[i],
                                     self.sBuildPath, reporter.log, reporter.log) is not True:
                reporter.testDone(fSkipped = True);
                return None; # Failed to get binaries, probably deleted. Skip the test run.

        #
        # Unpack anything we know what is and append it to the build files
        # list.  This allows us to use VBoxAll*.tar.gz files.
        #
        for sFile in list(self._asBuildFiles):
            if self._maybeUnpackArchive(sFile, fNonFatal = True) is not True:
                reporter.testDone(fSkipped = True);
                return None; # Failed to unpack. Probably local error, like busy
                             # DLLs on windows, no reason for failing the build.

        #
        # Go to system specific installation code.
        #
        sHost = utils.getHostOs()
        if   sHost == 'darwin':     fRc = self._installVBoxOnDarwin();
        elif sHost == 'linux':      fRc = self._installVBoxOnLinux();
        elif sHost == 'solaris':    fRc = self._installVBoxOnSolaris();
        elif sHost == 'win':        fRc = self._installVBoxOnWindows();
        else:
            reporter.error('Unsupported host "%s".' % (sHost,));
        if fRc is False:
            reporter.testFailure('Installation error.');

        #
        # Install the extension pack.
        #
        if fRc is True  and  self._fAutoInstallPuelExtPack:
            fRc = self._installExtPack();
            if fRc is False:
                reporter.testFailure('Extension pack installation error.');

        # Some debugging...
        try:
            cMbFreeSpace = utils.getDiskUsage(self.sScratchPath);
            reporter.log('Disk usage after VBox install: %d MB available at %s' % (cMbFreeSpace, self.sScratchPath,));
        except:
            reporter.logXcpt('Unable to get disk free space. Ignored. Continuing.');

        reporter.testDone();
        return fRc;

    def _uninstallVBox(self, fIgnoreError = False):
        """
        Uninstall VirtualBox.
        """
        reporter.testStart('Uninstalling VirtualBox');

        sHost = utils.getHostOs()
        if   sHost == 'darwin':     fRc = self._uninstallVBoxOnDarwin();
        elif sHost == 'linux':      fRc = self._uninstallVBoxOnLinux();
        elif sHost == 'solaris':    fRc = self._uninstallVBoxOnSolaris(True);
        elif sHost == 'win':        fRc = self._uninstallVBoxOnWindows(True);
        else:
            reporter.error('Unsupported host "%s".' % (sHost,));
        if fRc is False and not fIgnoreError:
            reporter.testFailure('Uninstallation failed.');

        fRc2 = self._uninstallAllExtPacks();
        if not fRc2 and fRc:
            fRc = fRc2;

        reporter.testDone(fSkipped = (fRc is None));
        return fRc;

    def _findFile(self, sRegExp, fMandatory = False):
        """
        Returns the first build file that matches the given regular expression
        (basename only).

        Returns None if no match was found, logging it as an error if
        fMandatory is set.
        """
        oRegExp = re.compile(sRegExp);

        for sFile in self._asBuildFiles:
            if oRegExp.match(os.path.basename(sFile)) and os.path.exists(sFile):
                return sFile;

        if fMandatory:
            reporter.error('Failed to find a file matching "%s" in %s.' % (sRegExp, self._asBuildFiles,));
        return None;

    def _waitForTestManagerConnectivity(self, cSecTimeout):
        """
        Check and wait for network connectivity to the test manager.

        This is used with the windows installation and uninstallation since
        these usually disrupts network connectivity when installing the filter
        driver.  If we proceed to quickly, we might finish the test at a time
        when we cannot report to the test manager and thus end up with an
        abandonded test error.
        """
        cSecElapsed = 0;
        secStart    = utils.timestampSecond();
        while reporter.checkTestManagerConnection() is False:
            cSecElapsed = utils.timestampSecond() - secStart;
            if cSecElapsed >= cSecTimeout:
                reporter.log('_waitForTestManagerConnectivity: Giving up after %u secs.' % (cSecTimeout,));
                return False;
            time.sleep(2);

        if cSecElapsed > 0:
            reporter.log('_waitForTestManagerConnectivity: Waited %s secs.' % (cSecTimeout,));
        return True;


    #
    # Darwin (Mac OS X).
    #

    def _darwinDmgPath(self):
        """ Returns the path to the DMG mount."""
        return os.path.join(self.sScratchPath, 'DmgMountPoint');

    def _darwinUnmountDmg(self, fIgnoreError):
        """
        Umount any DMG on at the default mount point.
        """
        sMountPath = self._darwinDmgPath();
        if not os.path.exists(sMountPath):
            return True;

        # Unmount.
        fRc = self._executeSync(['hdiutil', 'detach', sMountPath ]);
        if not fRc and not fIgnoreError:
            reporter.error('Failed to unmount DMG at %s' % sMountPath);

        # Remove dir.
        try:
            os.rmdir(sMountPath);
        except:
            if not fIgnoreError:
                reporter.errorXcpt('Failed to remove directory %s' % sMountPath);
        return fRc;

    def _darwinMountDmg(self, sDmg):
        """
        Mount the DMG at the default mount point.
        """
        self._darwinUnmountDmg(fIgnoreError = True)

        sMountPath = self._darwinDmgPath();
        if not os.path.exists(sMountPath):
            try:
                os.mkdir(sMountPath, 0o755);
            except:
                reporter.logXcpt();
                return False;

        return self._executeSync(['hdiutil', 'attach', '-readonly', '-mount', 'required', '-mountpoint', sMountPath, sDmg, ]);

    def _installVBoxOnDarwin(self):
        """ Installs VBox on Mac OS X."""
        sDmg = self._findFile('^VirtualBox-.*\\.dmg$');
        if sDmg is None:
            return False;

        # Mount the DMG.
        fRc = self._darwinMountDmg(sDmg);
        if fRc is not True:
            return False;

        # Uninstall any previous vbox version first.
        sUninstaller = os.path.join(self._darwinDmgPath(), 'VirtualBox_Uninstall.tool');
        fRc, _ = self._sudoExecuteSync([sUninstaller, '--unattended',]);
        if fRc is True:

            # Install the package.
            sPkg = os.path.join(self._darwinDmgPath(), 'VirtualBox.pkg');
            fRc, _ = self._sudoExecuteSync(['installer', '-verbose', '-dumplog', '-pkg', sPkg, '-target', '/']);

        # Unmount the DMG and we're done.
        if not self._darwinUnmountDmg(fIgnoreError = False):
            fRc = False;
        return fRc;

    def _uninstallVBoxOnDarwin(self):
        """ Uninstalls VBox on Mac OS X."""

        # Is VirtualBox installed? If not, don't try uninstall it.
        sVBox = self._getVBoxInstallPath(fFailIfNotFound = False);
        if sVBox is None:
            return True;

        # Find the dmg.
        sDmg = self._findFile('^VirtualBox-.*\\.dmg$');
        if sDmg is None:
            return False;
        if not os.path.exists(sDmg):
            return True;

        # Mount the DMG.
        fRc = self._darwinMountDmg(sDmg);
        if fRc is not True:
            return False;

        # Execute the uninstaller.
        sUninstaller = os.path.join(self._darwinDmgPath(), 'VirtualBox_Uninstall.tool');
        fRc, _ = self._sudoExecuteSync([sUninstaller, '--unattended',]);

        # Unmount the DMG and we're done.
        if not self._darwinUnmountDmg(fIgnoreError = False):
            fRc = False;
        return fRc;

    #
    # GNU/Linux
    #

    def _installVBoxOnLinux(self):
        """ Installs VBox on Linux."""
        sRun = self._findFile('^VirtualBox-.*\\.run$');
        if sRun is None:
            return False;
        utils.chmodPlusX(sRun);

        # Install the new one.
        fRc, _ = self._sudoExecuteSync([sRun,]);
        return fRc;

    def _uninstallVBoxOnLinux(self):
        """ Uninstalls VBox on Linux."""

        # Is VirtualBox installed? If not, don't try uninstall it.
        sVBox = self._getVBoxInstallPath(fFailIfNotFound = False);
        if sVBox is None:
            return True;

        # Find the .run file and use it.
        sRun = self._findFile('^VirtualBox-.*\\.run$', fMandatory = False);
        if sRun is not None:
            utils.chmodPlusX(sRun);
            fRc, _ = self._sudoExecuteSync([sRun, 'uninstall']);
            return fRc;

        # Try the installed uninstaller.
        for sUninstaller in [os.path.join(sVBox, 'uninstall.sh'), '/opt/VirtualBox/uninstall.sh', ]:
            if os.path.isfile(sUninstaller):
                reporter.log('Invoking "%s"...' % (sUninstaller,));
                fRc, _ = self._sudoExecuteSync([sUninstaller, 'uninstall']);
                return fRc;

        reporter.log('Did not find any VirtualBox install to uninstall.');
        return True;


    #
    # Solaris
    #

    def _generateAutoResponseOnSolaris(self):
        """
        Generates an autoresponse file on solaris, returning the name.
        None is return on failure.
        """
        sPath = os.path.join(self.sScratchPath, 'SolarisAutoResponse');
        oFile = utils.openNoInherit(sPath, 'wt');
        oFile.write('basedir=default\n'
                    'runlevel=nocheck\n'
                    'conflict=quit\n'
                    'setuid=nocheck\n'
                    'action=nocheck\n'
                    'partial=quit\n'
                    'instance=unique\n'
                    'idepend=quit\n'
                    'rdepend=quit\n'
                    'space=quit\n'
                    'mail=\n');
        oFile.close();
        return sPath;

    def _installVBoxOnSolaris(self):
        """ Installs VBox on Solaris."""
        sPkg = self._findFile('^VirtualBox-.*\\.pkg$', fMandatory = False);
        if sPkg is None:
            sTar = self._findFile('^VirtualBox-.*-SunOS-.*\\.tar.gz$', fMandatory = False);
            if sTar is not None:
                if self._maybeUnpackArchive(sTar) is not True:
                    return False;
        sPkg = self._findFile('^VirtualBox-.*\\.pkg$', fMandatory = True);
        sRsp = self._findFile('^autoresponse$', fMandatory = True);
        if sPkg is None or sRsp is None:
            return False;

        # Uninstall first (ignore result).
        self._uninstallVBoxOnSolaris(False);

        # Install the new one.
        fRc, _ = self._sudoExecuteSync(['pkgadd', '-d', sPkg, '-n', '-a', sRsp, 'SUNWvbox']);
        return fRc;

    def _uninstallVBoxOnSolaris(self, fRestartSvcConfigD):
        """ Uninstalls VBox on Solaris."""
        reporter.flushall();
        if utils.processCall(['pkginfo', '-q', 'SUNWvbox']) != 0:
            return True;
        sRsp = self._generateAutoResponseOnSolaris();
        fRc, _ = self._sudoExecuteSync(['pkgrm', '-n', '-a', sRsp, 'SUNWvbox']);

        #
        # Restart the svc.configd as it has a tendency to clog up with time and
        # become  unresponsive.  It will handle SIGHUP by exiting the sigwait()
        # look in the main function and shut down the service nicely (backend_fini).
        # The restarter will then start a new instance of it.
        #
        if fRestartSvcConfigD:
            time.sleep(1); # Give it a chance to flush pkgrm stuff.
            self._sudoExecuteSync(['pkill', '-HUP', 'svc.configd']);
            time.sleep(5); # Spare a few cpu cycles it to shutdown and restart.

        return fRc;

    #
    # Windows
    #

    ## VBox windows services we can query the status of.
    kasWindowsServices = [ 'vboxdrv', 'vboxusbmon', 'vboxnetadp', 'vboxnetflt', 'vboxnetlwf' ];

    def _installVBoxOnWindows(self):
        """ Installs VBox on Windows."""
        sExe = self._findFile('^VirtualBox-.*-(MultiArch|Win).exe$');
        if sExe is None:
            return False;

        # TEMPORARY HACK - START
        # It seems that running the NDIS cleanup script upon uninstallation is not
        # a good idea, so let's run it before installing VirtualBox.
        #sHostName = socket.getfqdn();
        #if    not sHostName.startswith('testboxwin3') \
        #  and not sHostName.startswith('testboxharp2') \
        #  and not sHostName.startswith('wei01-b6ka-3') \
        #  and utils.getHostOsVersion() in ['8', '8.1', '9', '2008Server', '2008ServerR2', '2012Server']:
        #    reporter.log('Peforming extra NDIS cleanup...');
        #    sMagicScript = os.path.abspath(os.path.join(g_ksValidationKitDir, 'testdriver', 'win-vbox-net-uninstall.ps1'));
        #    fRc2, _ = self._sudoExecuteSync(['powershell.exe', '-Command', 'set-executionpolicy unrestricted']);
        #    if not fRc2:
        #        reporter.log('set-executionpolicy failed.');
        #    self._sudoExecuteSync(['powershell.exe', '-Command', 'get-executionpolicy']);
        #    fRc2, _ = self._sudoExecuteSync(['powershell.exe', '-File', sMagicScript]);
        #    if not fRc2:
        #        reporter.log('NDIS cleanup failed.');
        # TEMPORARY HACK - END

        # Uninstall any previous vbox version first.
        fRc = self._uninstallVBoxOnWindows(True);
        if fRc is not True:
            return None; # There shouldn't be anything to uninstall, and if there is, it's not our fault.

        # Install the new one.
        asArgs = [sExe, '-vvvv', '--silent', '--logging'];
        asArgs.extend(['--msiparams', 'REBOOT=ReallySuppress']);
        sVBoxInstallPath = os.environ.get('VBOX_INSTALL_PATH', None);
        if sVBoxInstallPath is not None:
            asArgs.extend(['INSTALLDIR="%s"' % (sVBoxInstallPath,)]);
        fRc2, iRc = self._sudoExecuteSync(asArgs);
        if fRc2 is False:
            if iRc == 3010: # ERROR_SUCCESS_REBOOT_REQUIRED
                reporter.log('Note: Installer required a reboot to complete installation');
                # Optional, don't fail.
            else:
                fRc = False;
        sLogFile = os.path.join(tempfile.gettempdir(), 'VirtualBox', 'VBoxInstallLog.txt');
        if      sLogFile is not None \
            and os.path.isfile(sLogFile):
            reporter.addLogFile(sLogFile, 'log/installer', "Verbose MSI installation log file");
        self._waitForTestManagerConnectivity(30);
        return fRc;

    def _isProcessPresent(self, sName):
        """ Checks whether the named process is present or not. """
        for oProcess in utils.processListAll():
            sBase = oProcess.getBaseImageNameNoExeSuff();
            if sBase is not None and sBase.lower() == sName:
                return True;
        return False;

    def _killProcessesByName(self, sName, sDesc, fChildren = False):
        """ Kills the named process, optionally including children. """
        cKilled = 0;
        aoProcesses = utils.processListAll();
        for oProcess in aoProcesses:
            sBase = oProcess.getBaseImageNameNoExeSuff();
            if sBase is not None and sBase.lower() == sName:
                reporter.log('Killing %s process: %s (%s)' % (sDesc, oProcess.iPid, sBase));
                utils.processKill(oProcess.iPid);
                cKilled += 1;

                if fChildren:
                    for oChild in aoProcesses:
                        if oChild.iParentPid == oProcess.iPid and oChild.iParentPid is not None:
                            reporter.log('Killing %s child process: %s (%s)' % (sDesc, oChild.iPid, sBase));
                            utils.processKill(oChild.iPid);
                            cKilled += 1;
        return cKilled;

    def _terminateProcessesByNameAndArgSubstr(self, sName, sArg, sDesc):
        """
        Terminates the named process using taskkill.exe, if any of its args
        contains the passed string.
        """
        cKilled = 0;
        aoProcesses = utils.processListAll();
        for oProcess in aoProcesses:
            sBase = oProcess.getBaseImageNameNoExeSuff();
            if sBase is not None and sBase.lower() == sName and any(sArg in s for s in oProcess.asArgs):

                reporter.log('Killing %s process: %s (%s)' % (sDesc, oProcess.iPid, sBase));
                self._executeSync(['taskkill.exe', '/pid', '%u' % (oProcess.iPid,)]);
                cKilled += 1;
        return cKilled;

    def _uninstallVBoxOnWindows(self, fIgnoreServices = False):
        """
        Uninstalls VBox on Windows, all installations we find to be on the safe side...
        """

        import win32com.client; # pylint: disable=F0401
        win32com.client.gencache.EnsureModule('{000C1092-0000-0000-C000-000000000046}', 1033, 1, 0);
        oInstaller = win32com.client.Dispatch('WindowsInstaller.Installer',
                                              resultCLSID = '{000C1090-0000-0000-C000-000000000046}')

        # Search installed products for VirtualBox.
        asProdCodes = [];
        for sProdCode in oInstaller.Products:
            try:
                sProdName = oInstaller.ProductInfo(sProdCode, "ProductName");
            except:
                reporter.logXcpt();
                continue;
            #reporter.log('Info: %s=%s' % (sProdCode, sProdName));
            if  sProdName.startswith('Oracle VM VirtualBox') \
             or sProdName.startswith('Sun VirtualBox'):
                asProdCodes.append([sProdCode, sProdName]);

        # Before we start uninstalling anything, just ruthlessly kill any cdb,
        # msiexec, drvinst and some rundll process we might find hanging around.
        if self._isProcessPresent('rundll32'):
            cTimes = 0;
            while cTimes < 3:
                cTimes += 1;
                cKilled = self._terminateProcessesByNameAndArgSubstr('rundll32', 'InstallSecurityPromptRunDllW',
                                                                     'MSI driver installation');
                if cKilled <= 0:
                    break;
                time.sleep(10); # Give related drvinst process a chance to clean up after we killed the verification dialog.

        if self._isProcessPresent('drvinst'):
            time.sleep(15);     # In the hope that it goes away.
            cTimes = 0;
            while cTimes < 4:
                cTimes += 1;
                cKilled = self._killProcessesByName('drvinst', 'MSI driver installation', True);
                if cKilled <= 0:
                    break;
                time.sleep(10); # Give related MSI process a chance to clean up after we killed the driver installer.

        if self._isProcessPresent('msiexec'):
            cTimes = 0;
            while cTimes < 3:
                reporter.log('found running msiexec process, waiting a bit...');
                time.sleep(20)  # In the hope that it goes away.
                if not self._isProcessPresent('msiexec'):
                    break;
                cTimes += 1;
            ## @todo this could also be the msiexec system service, try to detect this case!
            if cTimes >= 6:
                cKilled = self._killProcessesByName('msiexec', 'MSI driver installation');
                if cKilled > 0:
                    time.sleep(16); # fudge.

        # cdb.exe sometimes stays running (from utils.getProcessInfo), blocking
        # the scratch directory. No idea why.
        if self._isProcessPresent('cdb'):
            cTimes = 0;
            while cTimes < 3:
                cKilled = self._killProcessesByName('cdb', 'cdb.exe from getProcessInfo');
                if cKilled <= 0:
                    break;
                time.sleep(2); # fudge.

        # Do the uninstalling.
        fRc = True;
        sLogFile = os.path.join(self.sScratchPath, 'VBoxUninstallLog.txt');
        for sProdCode, sProdName in asProdCodes:
            reporter.log('Uninstalling %s (%s)...' % (sProdName, sProdCode));
            fRc2, iRc = self._sudoExecuteSync(['msiexec', '/uninstall', sProdCode, '/quiet', '/passive', '/norestart',
                                               '/L*v', '%s' % (sLogFile), ]);
            if fRc2 is False:
                if iRc == 3010: # ERROR_SUCCESS_REBOOT_REQUIRED
                    reporter.log('Note: Uninstaller required a reboot to complete uninstallation');
                    reporter.addLogFile(sLogFile, 'log/uninstaller_reboot', "Verbose MSI uninstallation log file (reboot required)");
                    # Optional, don't fail.
                else:
                    fRc = False;

        self._waitForTestManagerConnectivity(30);
        if fRc is False and os.path.isfile(sLogFile):
            reporter.addLogFile(sLogFile, 'log/uninstaller', "Verbose MSI uninstallation log file");

        # Log driver service states (should ls \Driver\VBox* and \Device\VBox*).
        for sService in self.kasWindowsServices:
            fRc2, _ = self._sudoExecuteSync(['sc.exe', 'query', sService]);
            if fIgnoreServices is False and fRc2 is True:
                fRc = False

        return fRc;


    #
    # Extension pack.
    #

    def _getVBoxInstallPath(self, fFailIfNotFound):
        """ Returns the default VBox installation path. """
        sHost = utils.getHostOs();
        if sHost == 'win':
            sProgFiles = os.environ.get('ProgramFiles', 'C:\\Program Files');
            asLocs = [
                os.path.join(sProgFiles, 'Oracle', 'VirtualBox'),
                os.path.join(sProgFiles, 'OracleVM', 'VirtualBox'),
                os.path.join(sProgFiles, 'Sun', 'VirtualBox'),
            ];
        elif sHost == 'linux' or sHost == 'solaris':
            asLocs = [ '/opt/VirtualBox', '/opt/VirtualBox-3.2', '/opt/VirtualBox-3.1', '/opt/VirtualBox-3.0'];
        elif sHost == 'darwin':
            asLocs = [ '/Applications/VirtualBox.app/Contents/MacOS' ];
        else:
            asLocs = [ '/opt/VirtualBox' ];
        if 'VBOX_INSTALL_PATH' in os.environ:
            asLocs.insert(0, os.environ.get('VBOX_INSTALL_PATH', None));

        for sLoc in asLocs:
            if os.path.isdir(sLoc):
                return sLoc;
        if fFailIfNotFound:
            reporter.error('Failed to locate VirtualBox installation: %s' % (asLocs,));
        else:
            reporter.log2('Failed to locate VirtualBox installation: %s' % (asLocs,));
        return None;

    def _installExtPack(self):
        """ Installs the extension pack. """
        sVBox = self._getVBoxInstallPath(fFailIfNotFound = True);
        if sVBox is None:
            return False;
        sExtPackDir = os.path.join(sVBox, 'ExtensionPacks');

        if self._uninstallAllExtPacks() is not True:
            return False;

        sExtPack = self._findFile('Oracle_VM_VirtualBox_Extension_Pack.vbox-extpack');
        if sExtPack is None:
            sExtPack = self._findFile('Oracle_VM_VirtualBox_Extension_Pack.*.vbox-extpack');
        if sExtPack is None:
            return True;

        sDstDir = os.path.join(sExtPackDir, 'Oracle_VM_VirtualBox_Extension_Pack');
        reporter.log('Installing extension pack "%s" to "%s"...' % (sExtPack, sExtPackDir));
        fRc, _ = self._sudoExecuteSync([ self.getBinTool('vts_tar'),
                                         '--extract',
                                         '--verbose',
                                         '--gzip',
                                         '--file',                sExtPack,
                                         '--directory',           sDstDir,
                                         '--file-mode-and-mask',  '0644',
                                         '--file-mode-or-mask',   '0644',
                                         '--dir-mode-and-mask',   '0755',
                                         '--dir-mode-or-mask',    '0755',
                                         '--owner',               '0',
                                         '--group',               '0',
                                       ]);
        return fRc;

    def _uninstallAllExtPacks(self):
        """ Uninstalls all extension packs. """
        sVBox = self._getVBoxInstallPath(fFailIfNotFound = False);
        if sVBox is None:
            return True;

        sExtPackDir = os.path.join(sVBox, 'ExtensionPacks');
        if not os.path.exists(sExtPackDir):
            return True;

        fRc, _ = self._sudoExecuteSync([self.getBinTool('vts_rm'), '-Rfv', '--', sExtPackDir]);
        return fRc;



if __name__ == '__main__':
    sys.exit(VBoxInstallerTestDriver().main(sys.argv));