VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testdriver/tst-txsclient.py

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette