1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: tst-txsclient.py 70615 2018-01-17 20:28:16Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Simple testcase for txsclient.py.
|
---|
6 | """
|
---|
7 |
|
---|
8 | from __future__ import print_function;
|
---|
9 |
|
---|
10 | __copyright__ = \
|
---|
11 | """
|
---|
12 | Copyright (C) 2010-2017 Oracle Corporation
|
---|
13 |
|
---|
14 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
15 | available from http://www.virtualbox.org. This file is free software;
|
---|
16 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
17 | General Public License (GPL) as published by the Free Software
|
---|
18 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
19 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
20 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
21 |
|
---|
22 | The contents of this file may alternatively be used under the terms
|
---|
23 | of the Common Development and Distribution License Version 1.0
|
---|
24 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
25 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
26 | CDDL are applicable instead of those of the GPL.
|
---|
27 |
|
---|
28 | You may elect to license modified versions of this file under the
|
---|
29 | terms and conditions of either the GPL or the CDDL or both.
|
---|
30 | """
|
---|
31 | __version__ = "$Revision: 70615 $"
|
---|
32 |
|
---|
33 | # Standard python imports.
|
---|
34 | import os
|
---|
35 | import sys
|
---|
36 |
|
---|
37 | # Validation Kit imports.
|
---|
38 | sys.path.insert(0, '.');
|
---|
39 | sys.path.insert(0, '..');
|
---|
40 | import testdriver.txsclient as txsclient
|
---|
41 | import testdriver.reporter as reporter
|
---|
42 | from common import utils;
|
---|
43 |
|
---|
44 | # Python 3 hacks:
|
---|
45 | if sys.version_info[0] >= 3:
|
---|
46 | long = int; # pylint: disable=redefined-builtin,invalid-name
|
---|
47 |
|
---|
48 | g_cTests = 0;
|
---|
49 | g_cFailures = 0
|
---|
50 |
|
---|
51 | def boolRes(rc, fExpect = True):
|
---|
52 | """Checks a boolean result."""
|
---|
53 | global g_cTests, g_cFailures;
|
---|
54 | g_cTests = g_cTests + 1;
|
---|
55 | if isinstance(rc, bool):
|
---|
56 | if rc == fExpect:
|
---|
57 | return 'PASSED';
|
---|
58 | g_cFailures = g_cFailures + 1;
|
---|
59 | return 'FAILED';
|
---|
60 |
|
---|
61 | def stringRes(rc, sExpect):
|
---|
62 | """Checks a string result."""
|
---|
63 | global g_cTests, g_cFailures;
|
---|
64 | g_cTests = g_cTests + 1;
|
---|
65 | if utils.isString(rc):
|
---|
66 | if rc == sExpect:
|
---|
67 | return 'PASSED';
|
---|
68 | g_cFailures = g_cFailures + 1;
|
---|
69 | return 'FAILED';
|
---|
70 |
|
---|
71 | def main(asArgs): # pylint: disable=C0111,R0914,R0915
|
---|
72 | cMsTimeout = long(30*1000);
|
---|
73 | sAddress = 'localhost';
|
---|
74 | uPort = None;
|
---|
75 | fReversedSetup = False;
|
---|
76 | fReboot = False;
|
---|
77 | fShutdown = False;
|
---|
78 | fStdTests = True;
|
---|
79 |
|
---|
80 | i = 1;
|
---|
81 | while i < len(asArgs):
|
---|
82 | if asArgs[i] == '--hostname':
|
---|
83 | sAddress = asArgs[i + 1];
|
---|
84 | i = i + 2;
|
---|
85 | elif asArgs[i] == '--port':
|
---|
86 | uPort = int(asArgs[i + 1]);
|
---|
87 | i = i + 2;
|
---|
88 | elif asArgs[i] == '--reversed-setup':
|
---|
89 | fReversedSetup = True;
|
---|
90 | i = i + 1;
|
---|
91 | elif asArgs[i] == '--timeout':
|
---|
92 | cMsTimeout = long(asArgs[i + 1]);
|
---|
93 | i = i + 2;
|
---|
94 | elif asArgs[i] == '--reboot':
|
---|
95 | fReboot = True;
|
---|
96 | fShutdown = False;
|
---|
97 | fStdTests = False;
|
---|
98 | i = i + 1;
|
---|
99 | elif asArgs[i] == '--shutdown':
|
---|
100 | fShutdown = True;
|
---|
101 | fReboot = False;
|
---|
102 | fStdTests = False;
|
---|
103 | i = i + 1;
|
---|
104 | elif asArgs[i] == '--help':
|
---|
105 | print('tst-txsclient.py [--hostname <addr|name>] [--port <num>] [--timeout <cMS>] '
|
---|
106 | '[--reboot|--shutdown] [--reversed-setup]');
|
---|
107 | return 0;
|
---|
108 | else:
|
---|
109 | print('Unknown argument: %s' % (asArgs[i]));
|
---|
110 | return 2;
|
---|
111 |
|
---|
112 | if uPort is None:
|
---|
113 | oSession = txsclient.openTcpSession(cMsTimeout, sAddress, fReversedSetup = fReversedSetup);
|
---|
114 | else:
|
---|
115 | oSession = txsclient.openTcpSession(cMsTimeout, sAddress, uPort = uPort, fReversedSetup = fReversedSetup);
|
---|
116 | if oSession is None:
|
---|
117 | print('openTcpSession failed');
|
---|
118 | return 1;
|
---|
119 |
|
---|
120 | fDone = oSession.waitForTask(30*1000);
|
---|
121 | print('connect: waitForTask -> %s, result %s' % (fDone, oSession.getResult()));
|
---|
122 | if fDone is True and oSession.isSuccess():
|
---|
123 | if fStdTests:
|
---|
124 | # Get the UUID of the remote instance.
|
---|
125 | sUuid = oSession.syncUuid();
|
---|
126 | if sUuid is not False:
|
---|
127 | print('%s: UUID = %s' % (boolRes(True), sUuid));
|
---|
128 | else:
|
---|
129 | print('%s: UUID' % (boolRes(False),));
|
---|
130 |
|
---|
131 | # Create and remove a directory on the scratch area.
|
---|
132 | rc = oSession.syncMkDir('${SCRATCH}/testdir1');
|
---|
133 | print('%s: MKDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
134 |
|
---|
135 | rc = oSession.syncIsDir('${SCRATCH}/testdir1');
|
---|
136 | print('%s: ISDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
137 |
|
---|
138 | rc = oSession.syncRmDir('${SCRATCH}/testdir1');
|
---|
139 | print('%s: RMDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
140 |
|
---|
141 | # Create a two-level subdir.
|
---|
142 | rc = oSession.syncMkDirPath('${SCRATCH}/testdir2/subdir1');
|
---|
143 | print('%s: MKDRPATH(${SCRATCH}/testdir2/subdir1) -> %s' % (boolRes(rc), rc));
|
---|
144 |
|
---|
145 | rc = oSession.syncIsDir('${SCRATCH}/testdir2');
|
---|
146 | print('%s: ISDIR(${SCRATCH}/testdir2) -> %s' % (boolRes(rc), rc));
|
---|
147 | rc = oSession.syncIsDir('${SCRATCH}/testdir2/');
|
---|
148 | print('%s: ISDIR(${SCRATCH}/testdir2/) -> %s' % (boolRes(rc), rc));
|
---|
149 | rc = oSession.syncIsDir('${SCRATCH}/testdir2/subdir1');
|
---|
150 | print('%s: ISDIR(${SCRATCH}/testdir2/subdir1) -> %s' % (boolRes(rc), rc));
|
---|
151 |
|
---|
152 | rc = oSession.syncRmTree('${SCRATCH}/testdir2');
|
---|
153 | print('%s: RMTREE(${SCRATCH}/testdir2) -> %s' % (boolRes(rc), rc));
|
---|
154 |
|
---|
155 | # Check out a simple file.
|
---|
156 | rc = oSession.syncUploadString('howdy', '${SCRATCH}/howdyfile');
|
---|
157 | print('%s: PUT FILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
158 |
|
---|
159 | rc = oSession.syncUploadString('howdy-replaced', '${SCRATCH}/howdyfile');
|
---|
160 | print('%s: PUT FILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
161 |
|
---|
162 | rc = oSession.syncDownloadString('${SCRATCH}/howdyfile');
|
---|
163 | print('%s: GET FILE(${SCRATCH}/howdyfile) -> "%s" expected "howdy-replaced"' % (stringRes(rc, 'howdy-replaced'), rc));
|
---|
164 |
|
---|
165 | rc = oSession.syncIsFile('${SCRATCH}/howdyfile');
|
---|
166 | print('%s: ISFILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
167 | rc = oSession.syncIsDir('${SCRATCH}/howdyfile');
|
---|
168 | print('%s: ISDIR(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc, False), rc));
|
---|
169 | rc = oSession.syncIsSymlink('${SCRATCH}/howdyfile');
|
---|
170 | print('%s: ISSYMLNK(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc, False), rc));
|
---|
171 |
|
---|
172 | rc = oSession.syncRmFile('${SCRATCH}/howdyfile');
|
---|
173 | print('%s: RMFILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
174 |
|
---|
175 | # Unicode filename (may or may not work, LANG/LC_TYPE dependent on some hosts).
|
---|
176 | rc = oSession.syncUploadString('howdy', u'${SCRATCH}/Schröder');
|
---|
177 | print((u'%s: PUT FILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
178 |
|
---|
179 | rc = oSession.syncIsFile(u'${SCRATCH}/Schröder');
|
---|
180 | print((u'%s: ISFILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
181 |
|
---|
182 | rc = oSession.syncRmFile(u'${SCRATCH}/Schröder');
|
---|
183 | print((u'%s: RMFILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
184 |
|
---|
185 | # Finally, some file uploading and downloading with unicode filenames.
|
---|
186 | strUpFile = 'tst-txsclient-upload.bin';
|
---|
187 | strDwnFile = 'tst-txsclient-download.bin';
|
---|
188 | try:
|
---|
189 | abRandFile = os.urandom(257897);
|
---|
190 | except:
|
---|
191 | print('INFO: no urandom... falling back on a simple string.');
|
---|
192 | abRandFile = 'asdflkjasdlfkjasdlfkjq023942relwjgkna9epr865u2nm345;hndafgoukhasre5kb2453km';
|
---|
193 | for i in range(1, 64):
|
---|
194 | abRandFile += abRandFile;
|
---|
195 | try:
|
---|
196 | oLocalFile = utils.openNoInherit(strUpFile, 'w+b');
|
---|
197 | oLocalFile.write(abRandFile);
|
---|
198 | oLocalFile.close();
|
---|
199 | rc = True;
|
---|
200 | except:
|
---|
201 | rc = False;
|
---|
202 | print('%s: creating file (%s) to upload failed....' % (boolRes(rc), strUpFile));
|
---|
203 |
|
---|
204 | if rc is True:
|
---|
205 | rc = oSession.syncUploadFile(strUpFile, '${SCRATCH}/tst-txsclient-uploaded.bin')
|
---|
206 | print('%s: PUT FILE(%s, ${SCRATCH}/tst-txsclient-uploaded.bin) -> %s' % (boolRes(rc), strUpFile, rc));
|
---|
207 |
|
---|
208 | rc = oSession.syncDownloadFile('${SCRATCH}/tst-txsclient-uploaded.bin', strDwnFile)
|
---|
209 | print('%s: GET FILE(${SCRATCH}/tst-txsclient-uploaded.bin, tst-txsclient-downloaded.txt) -> %s'
|
---|
210 | % (boolRes(rc), rc));
|
---|
211 |
|
---|
212 | try:
|
---|
213 | oLocalFile = utils.openNoInherit(strDwnFile, "rb");
|
---|
214 | abDwnFile = oLocalFile.read();
|
---|
215 | oLocalFile.close();
|
---|
216 | if abRandFile == abDwnFile:
|
---|
217 | print('%s: downloaded file matches the uploaded file' % (boolRes(True),));
|
---|
218 | else:
|
---|
219 | print('%s: downloaded file does not match the uploaded file' % (boolRes(False),));
|
---|
220 | print('abRandFile=%s' % (abRandFile,));
|
---|
221 | print('abDwnFile =%s' % (abRandFile,));
|
---|
222 | except:
|
---|
223 | print('%s: reading downloaded file (%s) failed....' % (boolRes(False), strDwnFile));
|
---|
224 |
|
---|
225 | rc = oSession.syncRmFile(u'${SCRATCH}/tst-txsclient-uploaded.bin');
|
---|
226 | print('%s: RMFILE(${SCRATCH}/tst-txsclient-uploaded.bin) -> %s' % (boolRes(rc), rc));
|
---|
227 |
|
---|
228 | try: os.remove(strUpFile);
|
---|
229 | except: pass;
|
---|
230 | try: os.remove(strDwnFile);
|
---|
231 | except: pass;
|
---|
232 |
|
---|
233 | # Execute some simple thing, if available.
|
---|
234 | # Intentionally skip this test if file is not available due to
|
---|
235 | # another inserted CD-ROM (e.g. not TestSuite.iso).
|
---|
236 | sProg = '${CDROM}/${OS/ARCH}/NetPerf${EXESUFF}';
|
---|
237 | rc = oSession.syncIsFile(sProg, 30 * 1000, True);
|
---|
238 | if rc is True:
|
---|
239 | rc = oSession.syncExecEx(sProg, (sProg, '--help'));
|
---|
240 | print('%s: EXEC(%s ${SCRATCH}) -> %s' % (boolRes(rc), sProg, rc));
|
---|
241 |
|
---|
242 | rc = oSession.syncExecEx(sProg, (sProg, 'there', 'is no such', 'parameter'), \
|
---|
243 | oStdOut='${SCRATCH}/stdout', \
|
---|
244 | oStdErr='${SCRATCH}/stderr');
|
---|
245 | print('%s: EXEC(%s there is not such parameter > ${SCRATCH}/stdout 2> ${SCRATCH}/stderr) -> %s'
|
---|
246 | % (boolRes(rc, False), sProg, rc));
|
---|
247 |
|
---|
248 | rc = oSession.syncDownloadString('${SCRATCH}/stdout');
|
---|
249 | print('INFO: GET FILE(${SCRATCH}/stdout) -> "%s"' % (rc));
|
---|
250 | rc = oSession.syncDownloadString('${SCRATCH}/stderr');
|
---|
251 | print('INFO: GET FILE(${SCRATCH}/stderr) -> "%s"' % (rc));
|
---|
252 |
|
---|
253 | print('TESTING: syncExec...');
|
---|
254 | rc = oSession.syncExec(sProg, (sProg, '--version'));
|
---|
255 | print('%s: EXEC(%s --version) -> %s' % (boolRes(rc), sProg, rc));
|
---|
256 |
|
---|
257 | print('TESTING: syncExec...');
|
---|
258 | rc = oSession.syncExec(sProg, (sProg, '--help'));
|
---|
259 | print('%s: EXEC(%s --help) -> %s' % (boolRes(rc), sProg, rc));
|
---|
260 |
|
---|
261 | #print('TESTING: syncExec sleep 30...'
|
---|
262 | #rc = oSession.syncExec('/usr/bin/sleep', ('/usr/bin/sleep', '30')));
|
---|
263 | #print('%s: EXEC(/bin/sleep 30) -> %s' % (boolRes(rc), rc));
|
---|
264 | else:
|
---|
265 | print('SKIP: Execution of %s skipped, does not exist on CD-ROM' % (sProg,));
|
---|
266 |
|
---|
267 | # Execute a non-existing file on CD-ROM.
|
---|
268 | sProg = '${CDROM}/${OS/ARCH}/NonExisting${EXESUFF}';
|
---|
269 | rc = oSession.syncExecEx(sProg, (sProg,), oStdIn = '/dev/null', oStdOut = '/dev/null', \
|
---|
270 | oStdErr = '/dev/null', oTestPipe = '/dev/null', \
|
---|
271 | sAsUser = '', cMsTimeout = 3600000, fIgnoreErrors = True);
|
---|
272 | if rc is None:
|
---|
273 | rc = True;
|
---|
274 | else:
|
---|
275 | reporter.error('Unexpected value \"%s\" while executing non-existent file "%s"' % (rc, sProg));
|
---|
276 | print('%s: EXEC(%s ${SCRATCH}) -> %s' % (boolRes(rc), sProg, rc));
|
---|
277 |
|
---|
278 | # Done
|
---|
279 | rc = oSession.syncDisconnect();
|
---|
280 | print('%s: disconnect() -> %s' % (boolRes(rc), rc));
|
---|
281 |
|
---|
282 | elif fReboot:
|
---|
283 | print('TESTING: syncReboot...');
|
---|
284 | rc = oSession.syncReboot();
|
---|
285 | print('%s: REBOOT() -> %s' % (boolRes(rc), rc));
|
---|
286 | elif fShutdown:
|
---|
287 | print('TESTING: syncShutdown...');
|
---|
288 | rc = oSession.syncShutdown();
|
---|
289 | print('%s: SHUTDOWN() -> %s' % (boolRes(rc), rc));
|
---|
290 |
|
---|
291 |
|
---|
292 | if g_cFailures != 0:
|
---|
293 | print('tst-txsclient.py: %u out of %u test failed' % (g_cFailures, g_cTests));
|
---|
294 | return 1;
|
---|
295 | print('tst-txsclient.py: all %u tests passed!' % (g_cTests));
|
---|
296 | return 0;
|
---|
297 |
|
---|
298 |
|
---|
299 | if __name__ == '__main__':
|
---|
300 | reporter.incVerbosity();
|
---|
301 | reporter.incVerbosity();
|
---|
302 | reporter.incVerbosity();
|
---|
303 | reporter.incVerbosity();
|
---|
304 | sys.exit(main(sys.argv));
|
---|
305 |
|
---|