summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/tests/shutdown/tdGuestOsShutdown1.py
blob: 2407e92e46c0b3557e9c9f82aafc09d546b20ddc (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
VMM Guest OS boot tests.
"""

__copyright__ = \
"""
Copyright (C) 2010-2023 Oracle and/or its affiliates.

This file is part of VirtualBox base platform packages, as
available from https://www.virtualbox.org.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, in version 3 of the
License.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses>.

The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL), a copy of it is provided in the "COPYING.CDDL" file included
in the VirtualBox 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.

SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
"""
__version__ = "$Revision: 155244 $"


# Standard Python imports.
import os
import sys
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.dirname(os.path.abspath(__file__))))
sys.path.append(g_ksValidationKitDir)

# Validation Kit imports.
from testdriver import vbox
from testdriver import base
from testdriver import reporter
from testdriver import vboxcon


class tdGuestOsBootTest1(vbox.TestDriver):
    """
    VMM Unit Tests Set.

    Scenario:
        - Create VM that corresponds to Guest OS pre-installed on selected HDD
        - Start VM and wait for TXS server connection (which is started after Guest successfully booted)
    """

    ksSataController = 'SATA Controller'
    ksIdeController  = 'IDE Controller'

    # VM parameters required to run HDD image.
    # Format: { HDD image filename: (sKind, HDD controller type) }
    kaoVmParams = {
        't-win80.vdi':        ( 'Windows 8 (64 bit)', ksSataController ),
    }

    # List of platforms which are able to suspend and resume host automatically.
    # In order to add new platform, self._SuspendResume() should be adapted.
    kasSuspendAllowedPlatforms = ( 'darwin' )

    kcMsVmStartLimit    = 5 * 60000
    kcMsVmShutdownLimit = 1 * 60000

    def __init__(self):
        """
        Reinitialize child class instance.
        """
        vbox.TestDriver.__init__(self)

        self.sVmName             = 'TestVM'
        self.sHddName            = None
        self.sHddPathBase        = os.path.join(self.sResourcePath, '4.2', 'nat', 'win80')
        self.oVM                 = None

        # TODO: that should be moved to some common place
        self.fEnableIOAPIC       = True
        self.cCpus               = 1
        self.fEnableNestedPaging = True
        self.fEnablePAE          = False
        self.fSuspendHost        = False
        self.cSecSuspendTime     = 60
        self.cShutdownIters      = 1
        self.fExtraVm            = False
        self.sExtraVmName        = "TestVM-Extra"
        self.oExtraVM            = None
        self.fLocalCatch         = False

    #
    # Overridden methods.
    #

    def showUsage(self):
        """
        Extend usage info
        """
        rc = vbox.TestDriver.showUsage(self)
        reporter.log('  --boot-hdd <HDD image file name>')

        reporter.log('  --cpus <# CPUs>')
        reporter.log('  --no-ioapic')
        reporter.log('  --no-nested-paging')
        reporter.log('  --pae')
        reporter.log('  --suspend-host')
        reporter.log('  --suspend-time <sec>')
        reporter.log('  --shutdown-iters <# iters>')
        reporter.log('  --extra-vm')
        reporter.log('  --local-catch')
        return rc

    def parseOption(self, asArgs, iArg):
        """
        Extend standard options set
        """
        if asArgs[iArg] == '--boot-hdd':
            iArg += 1
            if iArg >= len(asArgs): raise base.InvalidOption('The "--boot-hdd" option requires an argument')
            self.sHddName = asArgs[iArg]

        elif asArgs[iArg] == '--cpus':
            iArg += 1
            if iArg >= len(asArgs): raise base.InvalidOption('The "--cpus" option requires an argument')
            self.cCpus = int(asArgs[iArg])
        elif asArgs[iArg] == '--no-ioapic':
            self.fEnableIOAPIC = False
        elif asArgs[iArg] == '--no-nested-paging':
            self.fEnableNestedPaging = False
        elif asArgs[iArg] == '--pae':
            self.fEnablePAE = True
        elif asArgs[iArg] == '--suspend-host':
            self.fSuspendHost = True
        elif asArgs[iArg] == '--suspend-time':
            iArg += 1
            if iArg >= len(asArgs): raise base.InvalidOption('The "--suspend-time" option requires an argument')
            self.cSecSuspendTime = int(asArgs[iArg])
        elif asArgs[iArg] == '--shutdown-iters':
            iArg += 1
            if iArg >= len(asArgs): raise base.InvalidOption('The "--shutdown-iters" option requires an argument')
            self.cShutdownIters = int(asArgs[iArg])
        elif asArgs[iArg] == '--extra-vm':
            self.fExtraVm = True
        elif asArgs[iArg] == '--local-catch':
            self.fLocalCatch = True
        else:
            return vbox.TestDriver.parseOption(self, asArgs, iArg)

        return iArg + 1

    def getResourceSet(self):
        """
        Returns a set of file and/or directory names relative to
        TESTBOX_PATH_RESOURCES.
        """
        return [os.path.join(self.sHddPathBase, sRsrc) for sRsrc in self.kaoVmParams];

    def _addVM(self, sVmName, sNicTraceFile=None):
        """
        Create VM
        """
        # Get VM params specific to HDD image
        sKind, sController = self.kaoVmParams[self.sHddName]

        # Create VM itself
        eNic0AttachType = vboxcon.NetworkAttachmentType_NAT
        sHddPath = os.path.join(self.sHddPathBase, self.sHddName)
        assert os.path.isfile(sHddPath)

        oVM = \
            self.createTestVM(sVmName, 1, sKind=sKind, cCpus=self.cCpus,
                              eNic0AttachType=eNic0AttachType, sDvdImage = self.sVBoxValidationKitIso)
        assert oVM is not None

        oSession = self.openSession(oVM)

        # Attach an HDD
        fRc =         oSession.attachHd(sHddPath, sController, fImmutable=True)

        # Enable HW virt
        fRc = fRc and oSession.enableVirtEx(True)

        # Enable I/O APIC
        fRc = fRc and oSession.enableIoApic(self.fEnableIOAPIC)

        # Enable Nested Paging
        fRc = fRc and oSession.enableNestedPaging(self.fEnableNestedPaging)

        # Enable PAE
        fRc = fRc and oSession.enablePae(self.fEnablePAE)

        if (sNicTraceFile is not None):
            fRc = fRc and oSession.setNicTraceEnabled(True, sNicTraceFile)

        # Remote desktop
        oSession.setupVrdp(True)

        fRc = fRc and oSession.saveSettings()
        fRc = fRc and oSession.close()
        assert fRc is True

        return oVM

    def actionConfig(self):
        """
        Configure pre-conditions.
        """

        if not self.importVBoxApi():
            return False

        # Save time: do not start VM if there is no way to suspend host
        if (self.fSuspendHost is True and sys.platform not in self.kasSuspendAllowedPlatforms):
            reporter.log('Platform [%s] is not in the list of supported platforms' % sys.platform)
            return False

        assert self.sHddName is not None
        if self.sHddName not in self.kaoVmParams:
            reporter.log('Error: unknown HDD image specified: %s' % self.sHddName)
            return False

        if (self.fExtraVm is True):
            self.oExtraVM = self._addVM(self.sExtraVmName)

        self.oVM = self._addVM(self.sVmName)

        return vbox.TestDriver.actionConfig(self)

    def _SuspendResume(self, cSecTimeout):
        """
        Put host into sleep and automatically resume it after specified timeout.
        """
        fRc = False

        if (sys.platform == 'darwin'):
            tsStart = time.time()
            fRc  = os.system("/usr/bin/pmset relative wake %d" % self.cSecSuspendTime)
            fRc |= os.system("/usr/bin/pmset sleepnow")
            # Wait for host to wake up
            while ((time.time() - tsStart) < self.cSecSuspendTime):
                self.sleep(0.1)

        return fRc == 0

    def _waitKeyboardInterrupt(self):
        """
        Idle loop until user press CTRL+C
        """
        reporter.log('[LOCAL CATCH]: waiting for keyboard interrupt')
        while (True):
            try:
                self.sleep(1)
            except KeyboardInterrupt:
                reporter.log('[LOCAL CATCH]: keyboard interrupt occurred')
                break

    def actionExecute(self):
        """
        Execute the testcase itself.
        """
        #self.logVmInfo(self.oVM)

        reporter.testStart('SHUTDOWN GUEST')

        cIter = 0
        fRc = True

        if (self.fExtraVm is True):
            oExtraSession, oExtraTxsSession = self.startVmAndConnectToTxsViaTcp(self.sExtraVmName,
                                                                                fCdWait=False,
                                                                                cMsTimeout=self.kcMsVmStartLimit)
            if oExtraSession is None or oExtraTxsSession is None:
                reporter.error('Unable to start extra VM.')
                if (self.fLocalCatch is True):
                    self._waitKeyboardInterrupt()
                reporter.testDone()
                return False

        while (cIter < self.cShutdownIters):

            cIter += 1

            reporter.log("Starting iteration #%d." % cIter)

            oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(self.sVmName,
                                                                      fCdWait=False,
                                                                      cMsTimeout=self.kcMsVmStartLimit)
            if oSession is not None and oTxsSession is not None:
                # Wait until guest reported success
                reporter.log('Guest started. Connection to TXS service established.')

                if (self.fSuspendHost is True):
                    reporter.log("Disconnect form TXS.")
                    fRc = fRc and self.txsDisconnect(oSession, oTxsSession)
                    if (fRc is not True):
                        reporter.log("Disconnect form TXS failed.")
                    else:
                        reporter.log('Put host to sleep and resume it automatically after %d seconds.' % self.cSecSuspendTime)
                        fRc = fRc and self._SuspendResume(self.cSecSuspendTime)
                        if (fRc is True):
                            reporter.log("Sleep/resume success.")
                        else:
                            reporter.log("Sleep/resume failed.")
                        reporter.log("Re-connect to TXS in 10 seconds.")
                        self.sleep(10)
                        (fRc, oTxsSession) = self.txsDoConnectViaTcp(oSession, 2 * 60 * 10000)
                        if (fRc is not True):
                            reporter.log("Re-connect to TXS failed.")

                if (fRc is True):
                    reporter.log('Attempt to shutdown guest.')
                    fRc = fRc and oTxsSession.syncShutdown(cMsTimeout=(4 * 60 * 1000))
                    if (fRc is True):
                        reporter.log('Shutdown request issued successfully.')
                        self.waitOnDirectSessionClose(self.oVM, self.kcMsVmShutdownLimit)
                        reporter.log('Shutdown %s.' % ('success' if fRc is True else 'failed'))
                    else:
                        reporter.error('Shutdown request failed.')

                    # Do not terminate failing VM in order to catch it.
                    if (fRc is not True and self.fLocalCatch is True):
                        self._waitKeyboardInterrupt()
                        break

                    fRc = fRc and self.terminateVmBySession(oSession)
                    reporter.log('VM terminated.')

            else:
                reporter.error('Guest did not start (iteration %d of %d)' % (cIter, self.cShutdownIters))
                fRc = False

            # Stop if fail
            if (fRc is not True):
                break

        # Local catch at the end.
        if (self.fLocalCatch is True):
            reporter.log("Test completed. Waiting for user to press CTRL+C.")
            self._waitKeyboardInterrupt()

        if (self.fExtraVm is True):
            fRc = fRc and  self.terminateVmBySession(oExtraSession)

        reporter.testDone()
        return fRc is True

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