1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: tst-txsclient.py 70611 2018-01-17 15:47:10Z 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: 70611 $"
|
---|
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 | fStdTests = True;
|
---|
78 |
|
---|
79 | i = 1;
|
---|
80 | while i < len(asArgs):
|
---|
81 | if asArgs[i] == '--hostname':
|
---|
82 | sAddress = asArgs[i + 1];
|
---|
83 | i = i + 2;
|
---|
84 | elif asArgs[i] == '--port':
|
---|
85 | uPort = int(asArgs[i + 1]);
|
---|
86 | i = i + 2;
|
---|
87 | elif asArgs[i] == '--reversed-setup':
|
---|
88 | fReversedSetup = True;
|
---|
89 | i = i + 1;
|
---|
90 | elif asArgs[i] == '--timeout':
|
---|
91 | cMsTimeout = long(asArgs[i + 1]);
|
---|
92 | i = i + 2;
|
---|
93 | elif asArgs[i] == '--reboot':
|
---|
94 | fReboot = True;
|
---|
95 | fStdTests = False;
|
---|
96 | i = i + 1;
|
---|
97 | elif asArgs[i] == '--help':
|
---|
98 | print('tst-txsclient.py [--hostname <addr|name>] [--port <num>] [--timeout <cMS>] [--reboot] [--reversed-setup]');
|
---|
99 | return 0;
|
---|
100 | else:
|
---|
101 | print('Unknown argument: %s' % (asArgs[i]));
|
---|
102 | return 2;
|
---|
103 |
|
---|
104 | if uPort is None:
|
---|
105 | oSession = txsclient.openTcpSession(cMsTimeout, sAddress, fReversedSetup = fReversedSetup);
|
---|
106 | else:
|
---|
107 | oSession = txsclient.openTcpSession(cMsTimeout, sAddress, uPort = uPort, fReversedSetup = fReversedSetup);
|
---|
108 | if oSession is None:
|
---|
109 | print('openTcpSession failed');
|
---|
110 | return 1;
|
---|
111 |
|
---|
112 | fDone = oSession.waitForTask(30*1000);
|
---|
113 | print('connect: waitForTask -> %s, result %s' % (fDone, oSession.getResult()));
|
---|
114 | if fDone is True and oSession.isSuccess():
|
---|
115 | if fStdTests:
|
---|
116 | # Get the UUID of the remote instance.
|
---|
117 | sUuid = oSession.syncUuid();
|
---|
118 | if sUuid is not False:
|
---|
119 | print('%s: UUID = %s' % (boolRes(True), sUuid));
|
---|
120 | else:
|
---|
121 | print('%s: UUID' % (boolRes(False),));
|
---|
122 |
|
---|
123 | # Create and remove a directory on the scratch area.
|
---|
124 | rc = oSession.syncMkDir('${SCRATCH}/testdir1');
|
---|
125 | print('%s: MKDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
126 |
|
---|
127 | rc = oSession.syncIsDir('${SCRATCH}/testdir1');
|
---|
128 | print('%s: ISDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
129 |
|
---|
130 | rc = oSession.syncRmDir('${SCRATCH}/testdir1');
|
---|
131 | print('%s: RMDIR(${SCRATCH}/testdir1) -> %s' % (boolRes(rc), rc));
|
---|
132 |
|
---|
133 | # Create a two-level subdir.
|
---|
134 | rc = oSession.syncMkDirPath('${SCRATCH}/testdir2/subdir1');
|
---|
135 | print('%s: MKDRPATH(${SCRATCH}/testdir2/subdir1) -> %s' % (boolRes(rc), rc));
|
---|
136 |
|
---|
137 | rc = oSession.syncIsDir('${SCRATCH}/testdir2');
|
---|
138 | print('%s: ISDIR(${SCRATCH}/testdir2) -> %s' % (boolRes(rc), rc));
|
---|
139 | rc = oSession.syncIsDir('${SCRATCH}/testdir2/');
|
---|
140 | print('%s: ISDIR(${SCRATCH}/testdir2/) -> %s' % (boolRes(rc), rc));
|
---|
141 | rc = oSession.syncIsDir('${SCRATCH}/testdir2/subdir1');
|
---|
142 | print('%s: ISDIR(${SCRATCH}/testdir2/subdir1) -> %s' % (boolRes(rc), rc));
|
---|
143 |
|
---|
144 | rc = oSession.syncRmTree('${SCRATCH}/testdir2');
|
---|
145 | print('%s: RMTREE(${SCRATCH}/testdir2) -> %s' % (boolRes(rc), rc));
|
---|
146 |
|
---|
147 | # Check out a simple file.
|
---|
148 | rc = oSession.syncUploadString('howdy', '${SCRATCH}/howdyfile');
|
---|
149 | print('%s: PUT FILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
150 |
|
---|
151 | rc = oSession.syncUploadString('howdy-replaced', '${SCRATCH}/howdyfile');
|
---|
152 | print('%s: PUT FILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
153 |
|
---|
154 | rc = oSession.syncDownloadString('${SCRATCH}/howdyfile');
|
---|
155 | print('%s: GET FILE(${SCRATCH}/howdyfile) -> "%s" expected "howdy-replaced"' % (stringRes(rc, 'howdy-replaced'), rc));
|
---|
156 |
|
---|
157 | rc = oSession.syncIsFile('${SCRATCH}/howdyfile');
|
---|
158 | print('%s: ISFILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
159 | rc = oSession.syncIsDir('${SCRATCH}/howdyfile');
|
---|
160 | print('%s: ISDIR(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc, False), rc));
|
---|
161 | rc = oSession.syncIsSymlink('${SCRATCH}/howdyfile');
|
---|
162 | print('%s: ISSYMLNK(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc, False), rc));
|
---|
163 |
|
---|
164 | rc = oSession.syncRmFile('${SCRATCH}/howdyfile');
|
---|
165 | print('%s: RMFILE(${SCRATCH}/howdyfile) -> %s' % (boolRes(rc), rc));
|
---|
166 |
|
---|
167 | # Unicode filename (may or may not work, LANG/LC_TYPE dependent on some hosts).
|
---|
168 | rc = oSession.syncUploadString('howdy', u'${SCRATCH}/Schröder');
|
---|
169 | print((u'%s: PUT FILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
170 |
|
---|
171 | rc = oSession.syncIsFile(u'${SCRATCH}/Schröder');
|
---|
172 | print((u'%s: ISFILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
173 |
|
---|
174 | rc = oSession.syncRmFile(u'${SCRATCH}/Schröder');
|
---|
175 | print((u'%s: RMFILE(${SCRATCH}/Schröder) -> %s' % (boolRes(rc), rc)).encode('ascii', 'replace'));
|
---|
176 |
|
---|
177 | # Finally, some file uploading and downloading with unicode filenames.
|
---|
178 | strUpFile = 'tst-txsclient-upload.bin';
|
---|
179 | strDwnFile = 'tst-txsclient-download.bin';
|
---|
180 | try:
|
---|
181 | abRandFile = os.urandom(257897);
|
---|
182 | except:
|
---|
183 | print('INFO: no urandom... falling back on a simple string.');
|
---|
184 | abRandFile = 'asdflkjasdlfkjasdlfkjq023942relwjgkna9epr865u2nm345;hndafgoukhasre5kb2453km';
|
---|
185 | for i in range(1, 64):
|
---|
186 | abRandFile += abRandFile;
|
---|
187 | try:
|
---|
188 | oLocalFile = utils.openNoInherit(strUpFile, 'w+b');
|
---|
189 | oLocalFile.write(abRandFile);
|
---|
190 | oLocalFile.close();
|
---|
191 | rc = True;
|
---|
192 | except:
|
---|
193 | rc = False;
|
---|
194 | print('%s: creating file (%s) to upload failed....' % (boolRes(rc), strUpFile));
|
---|
195 |
|
---|
196 | if rc is True:
|
---|
197 | rc = oSession.syncUploadFile(strUpFile, '${SCRATCH}/tst-txsclient-uploaded.bin')
|
---|
198 | print('%s: PUT FILE(%s, ${SCRATCH}/tst-txsclient-uploaded.bin) -> %s' % (boolRes(rc), strUpFile, rc));
|
---|
199 |
|
---|
200 | rc = oSession.syncDownloadFile('${SCRATCH}/tst-txsclient-uploaded.bin', strDwnFile)
|
---|
201 | print('%s: GET FILE(${SCRATCH}/tst-txsclient-uploaded.bin, tst-txsclient-downloaded.txt) -> %s'
|
---|
202 | % (boolRes(rc), rc));
|
---|
203 |
|
---|
204 | try:
|
---|
205 | oLocalFile = utils.openNoInherit(strDwnFile, "rb");
|
---|
206 | abDwnFile = oLocalFile.read();
|
---|
207 | oLocalFile.close();
|
---|
208 | if abRandFile == abDwnFile:
|
---|
209 | print('%s: downloaded file matches the uploaded file' % (boolRes(True),));
|
---|
210 | else:
|
---|
211 | print('%s: downloaded file does not match the uploaded file' % (boolRes(False),));
|
---|
212 | print('abRandFile=%s' % (abRandFile,));
|
---|
213 | print('abDwnFile =%s' % (abRandFile,));
|
---|
214 | except:
|
---|
215 | print('%s: reading downloaded file (%s) failed....' % (boolRes(False), strDwnFile));
|
---|
216 |
|
---|
217 | rc = oSession.syncRmFile(u'${SCRATCH}/tst-txsclient-uploaded.bin');
|
---|
218 | print('%s: RMFILE(${SCRATCH}/tst-txsclient-uploaded.bin) -> %s' % (boolRes(rc), rc));
|
---|
219 |
|
---|
220 | try: os.remove(strUpFile);
|
---|
221 | except: pass;
|
---|
222 | try: os.remove(strDwnFile);
|
---|
223 | except: pass;
|
---|
224 |
|
---|
225 | # Execute some simple thing, if available.
|
---|
226 | # Intentionally skip this test if file is not available due to
|
---|
227 | # another inserted CD-ROM (e.g. not TestSuite.iso).
|
---|
228 | sProg = '${CDROM}/${OS/ARCH}/NetPerf${EXESUFF}';
|
---|
229 | rc = oSession.syncIsFile(sProg, 30 * 1000, True);
|
---|
230 | if rc is True:
|
---|
231 | rc = oSession.syncExecEx(sProg, (sProg, '--help'));
|
---|
232 | print('%s: EXEC(%s ${SCRATCH}) -> %s' % (boolRes(rc), sProg, rc));
|
---|
233 |
|
---|
234 | rc = oSession.syncExecEx(sProg, (sProg, 'there', 'is no such', 'parameter'), \
|
---|
235 | oStdOut='${SCRATCH}/stdout', \
|
---|
236 | oStdErr='${SCRATCH}/stderr');
|
---|
237 | print('%s: EXEC(%s there is not such parameter > ${SCRATCH}/stdout 2> ${SCRATCH}/stderr) -> %s'
|
---|
238 | % (boolRes(rc, False), sProg, rc));
|
---|
239 |
|
---|
240 | rc = oSession.syncDownloadString('${SCRATCH}/stdout');
|
---|
241 | print('INFO: GET FILE(${SCRATCH}/stdout) -> "%s"' % (rc));
|
---|
242 | rc = oSession.syncDownloadString('${SCRATCH}/stderr');
|
---|
243 | print('INFO: GET FILE(${SCRATCH}/stderr) -> "%s"' % (rc));
|
---|
244 |
|
---|
245 | print('TESTING: syncExec...');
|
---|
246 | rc = oSession.syncExec(sProg, (sProg, '--version'));
|
---|
247 | print('%s: EXEC(%s --version) -> %s' % (boolRes(rc), sProg, rc));
|
---|
248 |
|
---|
249 | print('TESTING: syncExec...');
|
---|
250 | rc = oSession.syncExec(sProg, (sProg, '--help'));
|
---|
251 | print('%s: EXEC(%s --help) -> %s' % (boolRes(rc), sProg, rc));
|
---|
252 |
|
---|
253 | #print('TESTING: syncExec sleep 30...'
|
---|
254 | #rc = oSession.syncExec('/usr/bin/sleep', ('/usr/bin/sleep', '30')));
|
---|
255 | #print('%s: EXEC(/bin/sleep 30) -> %s' % (boolRes(rc), rc));
|
---|
256 | else:
|
---|
257 | print('SKIP: Execution of %s skipped, does not exist on CD-ROM' % (sProg,));
|
---|
258 |
|
---|
259 | # Execute a non-existing file on CD-ROM.
|
---|
260 | sProg = '${CDROM}/${OS/ARCH}/NonExisting${EXESUFF}';
|
---|
261 | rc = oSession.syncExecEx(sProg, (sProg,), oStdIn = '/dev/null', oStdOut = '/dev/null', \
|
---|
262 | oStdErr = '/dev/null', oTestPipe = '/dev/null', \
|
---|
263 | sAsUser = '', cMsTimeout = 3600000, fIgnoreErrors = True);
|
---|
264 | if rc is None:
|
---|
265 | rc = True;
|
---|
266 | else:
|
---|
267 | reporter.error('Unexpected value \"%s\" while executing non-existent file "%s"' % (rc, sProg));
|
---|
268 | print('%s: EXEC(%s ${SCRATCH}) -> %s' % (boolRes(rc), sProg, rc));
|
---|
269 |
|
---|
270 | # Done
|
---|
271 | rc = oSession.syncDisconnect();
|
---|
272 | print('%s: disconnect() -> %s' % (boolRes(rc), rc));
|
---|
273 |
|
---|
274 | elif fReboot:
|
---|
275 | print('TESTING: syncReboot...');
|
---|
276 | rc = oSession.syncReboot();
|
---|
277 | print('%s: REBOOT() -> %s' % (boolRes(rc), rc));
|
---|
278 |
|
---|
279 | if g_cFailures != 0:
|
---|
280 | print('tst-txsclient.py: %u out of %u test failed' % (g_cFailures, g_cTests));
|
---|
281 | return 1;
|
---|
282 | print('tst-txsclient.py: all %u tests passed!' % (g_cTests));
|
---|
283 | return 0;
|
---|
284 |
|
---|
285 |
|
---|
286 | if __name__ == '__main__':
|
---|
287 | reporter.incVerbosity();
|
---|
288 | reporter.incVerbosity();
|
---|
289 | reporter.incVerbosity();
|
---|
290 | reporter.incVerbosity();
|
---|
291 | sys.exit(main(sys.argv));
|
---|
292 |
|
---|