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

"""
VirtualBox Validation Kit - Shared Folders #1.
"""

__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 shutil
import sys

# 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 reporter;
from testdriver import base;
from common     import utils;


class SubTstDrvAddSharedFolders1(base.SubTestDriverBase):
    """
    Sub-test driver for executing shared folders tests.
    """

    def __init__(self, oTstDrv):
        base.SubTestDriverBase.__init__(self, oTstDrv, 'add-shared-folders', 'Shared Folders');

        self.asTestsDef         = [ 'fsperf', ];
        self.asTests            = self.asTestsDef;
        self.asExtraArgs        = [];
        self.asGstFsPerfPaths   = [
            '${CDROM}/vboxvalidationkit/${OS/ARCH}/FsPerf${EXESUFF}',
            '${CDROM}/${OS/ARCH}/FsPerf${EXESUFF}',
            '${TXSDIR}/${OS/ARCH}/FsPerf${EXESUFF}',
            '${TXSDIR}/FsPerf${EXESUFF}',
            'E:/vboxvalidationkit/${OS/ARCH}/FsPerf${EXESUFF}',
        ];
        self.sGuestSlash = '';

    def parseOption(self, asArgs, iArg):
        if asArgs[iArg] == '--add-shared-folders-tests': # 'add' as in 'additions', not the verb.
            iArg += 1;
            iNext = self.oTstDrv.requireMoreArgs(1, asArgs, iArg);
            if asArgs[iArg] == 'all':
                self.asTests = self.asTestsDef;
            else:
                self.asTests = asArgs[iArg].split(':');
                for s in self.asTests:
                    if s not in self.asTestsDef:
                        raise base.InvalidOption('The "--add-shared-folders-tests" value "%s" is not valid; valid values are: %s'
                                                 % (s, ' '.join(self.asTestsDef)));
            return iNext;
        if asArgs[iArg] == '--add-shared-folders-extra-arg':
            iArg += 1;
            iNext = self.oTstDrv.requireMoreArgs(1, asArgs, iArg);
            self.asExtraArgs.append(asArgs[iArg]);
            return iNext;
        return iArg;

    def showUsage(self):
        base.SubTestDriverBase.showUsage(self);
        reporter.log('  --add-shared-folders-tests <t1[:t2[:]]>');
        reporter.log('      Default: all  (%s)' % (':'.join(self.asTestsDef)));
        reporter.log('  --add-shared-folders-extra-arg <fsperf-arg>');
        reporter.log('      Adds an extra FsPerf argument.  Can be repeated.');

        return True;

    def mountShareEx(self, oSession, oTxsSession, sShareName, sHostPath, sGuestMountPoint, fMustSucceed):
        """
        Automount a shared folder in the guest, extended version.

        Returns success status, based on fMustSucceed.
        """
        reporter.testStart('Automounting "%s"' % (sShareName,));

        reporter.log2('Creating shared folder "%s" at "%s" ...' % (sShareName, sGuestMountPoint));
        try:
            oConsole = oSession.o.console;
            oConsole.createSharedFolder(sShareName, sHostPath, True, True, sGuestMountPoint);
        except:
            if fMustSucceed:
                reporter.errorXcpt('createSharedFolder(%s,%s,True,True,%s)' % (sShareName, sHostPath, sGuestMountPoint));
            else:
                reporter.log('createSharedFolder(%s,%s,True,True,%s) failed, good' % (sShareName, sHostPath, sGuestMountPoint));
            reporter.testDone();
            return False is fMustSucceed;

        # Check whether we can see the shared folder now.  Retry for 30 seconds.
        msStart = base.timestampMilli();
        while True:
            fRc = oTxsSession.syncIsDir(sGuestMountPoint + self.sGuestSlash + 'candle.dir');
            reporter.log2('candle.dir check -> %s' % (fRc,));
            if fRc is fMustSucceed:
                break;
            if base.timestampMilli() - msStart > 30000:
                reporter.error('Shared folder mounting timed out!');
                break;
            self.oTstDrv.sleep(1);

        reporter.testDone();

        return fRc == fMustSucceed;

    def mountShare(self, oSession, oTxsSession, sShareName, sHostPath, sGuestMountPoint):
        """
        Automount a shared folder in the guest.

        Returns success status.
        """
        return self.mountShareEx(oSession, oTxsSession, sShareName, sHostPath, sGuestMountPoint, fMustSucceed = True);

    def unmountShareEx(self, oSession, oTxsSession, sShareName, sGuestMountPoint, fMustSucceed):
        """
        Unmounts a shared folder in the guest.

        Returns success status, based on fMustSucceed.
        """
        reporter.log2('Autounmount');
        try:
            oConsole = oSession.o.console;
            oConsole.removeSharedFolder(sShareName);
        except:
            if fMustSucceed:
                reporter.errorXcpt('removeSharedFolder(%s)' % (sShareName,));
            else:
                reporter.log('removeSharedFolder(%s)' % (sShareName,));
            reporter.testDone();
            return False is fMustSucceed;

        # Check whether the shared folder is gone on the guest now.  Retry for 30 seconds.
        msStart = base.timestampMilli();
        while True:
            fRc = oTxsSession.syncIsDir(sGuestMountPoint + self.sGuestSlash + 'candle.dir');
            reporter.log2('candle.dir check -> %s' % (fRc,));
            if fRc is not fMustSucceed:
                break;
            if base.timestampMilli() - msStart > 30000:
                reporter.error('Shared folder unmounting timed out!');
                fRc = False;
                break;
            self.oTstDrv.sleep(1);

        reporter.testDone();

        return fRc is not fMustSucceed;

    def unmountShare(self, oSession, oTxsSession, sShareName, sGuestMountPoint):
        """
        Unmounts a shared folder in the guest, extended version.

        Returns success status, based on fMustSucceed.
        """
        return self.unmountShareEx(oSession, oTxsSession, sShareName, sGuestMountPoint, fMustSucceed = True);

    def testIt(self, oTestVm, oSession, oTxsSession):
        """
        Executes the test.

        Returns fRc, oTxsSession.  The latter may have changed.
        """
        reporter.log("Active tests: %s" % (self.asTests,));

        #
        # Skip the test if before 6.0
        #
        if self.oTstDrv.fpApiVer < 6.0:
            reporter.log('Requires 6.0 or later (for now)');
            return (None, oTxsSession);

        # Guess a free mount point inside the guest.
        if oTestVm.isWindows() or oTestVm.isOS2():
            self.sGuestSlash  = '\\';
        else:
            self.sGuestSlash  = '/';

        #
        # Create the host directory to share. Empty except for a 'candle.dir' subdir
        # that we use to check that it mounted correctly.
        #
        sShareName1     = 'shfl1';
        sShareHostPath1 = os.path.join(self.oTstDrv.sScratchPath, sShareName1);
        reporter.log2('Creating shared host folder "%s"...' % (sShareHostPath1,));
        if os.path.exists(sShareHostPath1):
            try:    shutil.rmtree(sShareHostPath1);
            except: return (reporter.errorXcpt('shutil.rmtree(%s)' % (sShareHostPath1,)), oTxsSession);
        try:    os.mkdir(sShareHostPath1);
        except: return (reporter.errorXcpt('os.mkdir(%s)' % (sShareHostPath1,)), oTxsSession);
        try:    os.mkdir(os.path.join(sShareHostPath1, 'candle.dir'));
        except: return (reporter.errorXcpt('os.mkdir(%s)' % (sShareHostPath1,)), oTxsSession);

        # Guess a free mount point inside the guest.
        if oTestVm.isWindows() or oTestVm.isOS2():
            sMountPoint1 = 'V:';
        else:
            sMountPoint1 = '/mnt/' + sShareName1;

        fRc = self.mountShare(oSession, oTxsSession, sShareName1, sShareHostPath1, sMountPoint1);
        if fRc is not True:
            return (False, oTxsSession); # skip the remainder if we cannot auto mount the folder.

        #
        # Run FsPerf inside the guest.
        #
        fSkip = 'fsperf' not in self.asTests;
        if fSkip is False:
            cMbFree = utils.getDiskUsage(sShareHostPath1);
            if cMbFree >= 16:
                reporter.log2('Free space: %u MBs' % (cMbFree,));
            else:
                reporter.log('Skipping FsPerf because only %u MB free on %s' % (cMbFree, sShareHostPath1,));
                fSkip = True;
        if fSkip is False:
            # Common arguments:
            asArgs = ['FsPerf', '-d', sMountPoint1 + self.sGuestSlash + 'fstestdir-1', '-s8'];

            # Skip part of mmap on older windows systems without CcCoherencyFlushAndPurgeCache (>= w7).
            reporter.log2('oTestVm.sGuestOsType=%s' % (oTestVm.sGuestOsType,));
            if   oTestVm.getNonCanonicalGuestOsType() \
              in [ 'WindowsNT3x', 'WindowsNT4', 'Windows2000', 'WindowsXP', 'WindowsXP_64', 'Windows2003',
                   'Windows2003_64', 'WindowsVista', 'WindowsVista_64', 'Windows2008', 'Windows2008_64']:
                asArgs.append('--no-mmap-coherency');

            # Configure I/O block sizes according to guest memory size:
            cbMbRam = 128;
            try:    cbMbRam = oSession.o.machine.memorySize;
            except: reporter.errorXcpt();
            reporter.log2('cbMbRam=%s' % (cbMbRam,));
            asArgs.append('--set-block-size=1');
            asArgs.append('--add-block-size=512');
            asArgs.append('--add-block-size=4096');
            asArgs.append('--add-block-size=16384');
            asArgs.append('--add-block-size=65536');
            asArgs.append('--add-block-size=1048576');       #   1 MiB
            if cbMbRam >= 512:
                asArgs.append('--add-block-size=33554432');  #  32 MiB
            if cbMbRam >= 768:
                asArgs.append('--add-block-size=134217728'); # 128 MiB

            # Putting lots (10000) of files in a single directory causes issues on OS X
            # (HFS+ presumably, though could be slow disks) and some linuxes (slow disks,
            # maybe ext2/3?).  So, generally reduce the file count to 4096 everywhere
            # since we're not here to test the host file systems, and 3072 on macs.
            if utils.getHostOs() in [ 'darwin', ]:
                asArgs.append('--many-files=3072');
            elif utils.getHostOs() in [ 'linux', ]:
                asArgs.append('--many-files=4096');

            # Add the extra arguments from the command line and kick it off:
            asArgs.extend(self.asExtraArgs);

            # Run FsPerf:
            reporter.log2('Starting guest FsPerf (%s)...' % (asArgs,));
            sFsPerfPath = self._locateGstFsPerf(oTxsSession);

            ## @todo For some odd reason the combined GA/VaKit .ISO (by IPRT/fs/isomakercmd)
            #        sometimes (?) contains FsPerf as non-executable (-r--r--r-- 1 root root) on Linux.
            #
            #        So work around this for now by copying the desired FsPerf binary to the temp directory,
            #        make it executable and execute it from there.
            fISOMakerCmdIsBuggy = oTestVm.isLinux();
            if fISOMakerCmdIsBuggy:
                sFsPerfPathTemp = oTestVm.pathJoin(self.oTstDrv.getGuestTempDir(oTestVm), 'FsPerf${EXESUFF}');
                if oTestVm.isWindows() \
                or oTestVm.isOS2():
                    sCopy           = self.oTstDrv.getGuestSystemShell();
                    sCopyArgs       = ( sCopy, "/C", "copy", "/Y",  sFsPerfPath, sFsPerfPathTemp );
                else:
                    sCopy           = oTestVm.pathJoin(self.oTstDrv.getGuestSystemDir(oTestVm), 'cp');
                    sCopyArgs       = ( sCopy, "-a", "-v", sFsPerfPath, sFsPerfPathTemp );
                fRc = self.oTstDrv.txsRunTest(oTxsSession, 'Copying FsPerf', 60 * 1000,
                                              sCopy, sCopyArgs, fCheckSessionStatus = True);
                fRc = fRc and oTxsSession.syncChMod(sFsPerfPathTemp, 0o755);
                if fRc:
                    sFsPerfPath = sFsPerfPathTemp;

            fRc = self.oTstDrv.txsRunTest(oTxsSession, 'Running FsPerf', 90 * 60 * 1000, sFsPerfPath, asArgs,
                                          fCheckSessionStatus = True);
            reporter.log2('FsPerf -> %s' % (fRc,));
            if fRc:
                # Do a bit of diagnosis to find out why this failed.
                if     not oTestVm.isWindows() \
                   and not oTestVm.isOS2():
                    sCmdLs = oTestVm.pathJoin(self.oTstDrv.getGuestSystemDir(oTestVm), 'ls');
                    oTxsSession.syncExec(sCmdLs, (sCmdLs, "-al", sFsPerfPath), fIgnoreErrors = True);
                    oTxsSession.syncExec(sCmdLs, (sCmdLs, "-al", "-R", "/opt"), fIgnoreErrors = True);
                    oTxsSession.syncExec(sCmdLs, (sCmdLs, "-al", "-R", "/media/cdrom"), fIgnoreErrors = True);

            sTestDir = os.path.join(sShareHostPath1, 'fstestdir-1');
            if os.path.exists(sTestDir):
                fRc = reporter.errorXcpt('test directory lingers: %s' % (sTestDir,));
                try:    shutil.rmtree(sTestDir);
                except: fRc = reporter.errorXcpt('shutil.rmtree(%s)' % (sTestDir,));
        else:
            reporter.testStart('FsPerf');
            reporter.testDone(fSkip or fRc is None);

        #
        # Check if auto-unmounting works.
        #
        if fRc is True:
            fRc = self.unmountShare(oSession, oTxsSession, sShareName1, sMountPoint1);

        ## @todo Add tests for multiple automount shares, random unmounting, reboot test.

        return (fRc, oTxsSession);

    def _locateGstFsPerf(self, oTxsSession):
        """
        Returns guest side path to FsPerf.
        """
        for sFsPerfPath in self.asGstFsPerfPaths:
            if oTxsSession.syncIsFile(sFsPerfPath):
                reporter.log('Using FsPerf at "%s"' % (sFsPerfPath,));
                return sFsPerfPath;
        reporter.log('Unable to find guest FsPerf in any of these places: %s' % ('\n    '.join(self.asGstFsPerfPaths),));
        return self.asGstFsPerfPaths[0];



if __name__ == '__main__':
    reporter.error('Cannot run standalone, use tdAddBasic1.py');
    sys.exit(1);