VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/serial/tdSerial1.py@ 73494

Last change on this file since 73494 was 72231, checked in by vboxsync, 7 years ago

vkit/tests: pylint

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: tdSerial1.py 72231 2018-05-17 10:55:16Z vboxsync $
4
5"""
6VirtualBox Validation Kit - Serial port testing #1.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2018 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 72231 $"
31
32
33# Standard Python imports.
34import os;
35import random;
36import string;
37import struct;
38import sys;
39
40# Only the main script needs to modify the path.
41try: __file__
42except: __file__ = sys.argv[0];
43g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
44sys.path.append(g_ksValidationKitDir);
45
46# Validation Kit imports.
47from testdriver import base;
48from testdriver import reporter;
49from testdriver import vbox;
50from testdriver import vboxcon;
51
52import loopback;
53
54# Python 3 hacks:
55if sys.version_info[0] >= 3:
56 xrange = range; # pylint: disable=redefined-builtin,invalid-name
57
58
59class tdSerial1(vbox.TestDriver):
60 """
61 VBox serial port testing #1.
62 """
63
64 def __init__(self):
65 vbox.TestDriver.__init__(self);
66 self.asRsrcs = None;
67 self.oTestVmSet = self.oTestVmManager.selectSet(self.oTestVmManager.kfGrpStdSmoke);
68 self.asSerialModesDef = ['RawFile', 'Tcp', 'TcpServ', 'NamedPipe', 'NamedPipeServ', 'HostDev'];
69 self.asSerialModes = self.asSerialModesDef;
70 self.asSerialTestsDef = ['Write', 'ReadWrite'];
71 self.asSerialTests = self.asSerialTestsDef;
72 self.oLoopback = None;
73 self.sLocation = None;
74 self.fVerboseTest = False;
75
76 #
77 # Overridden methods.
78 #
79 def showUsage(self):
80 rc = vbox.TestDriver.showUsage(self);
81 reporter.log('');
82 reporter.log('tdSerial1 Options:');
83 reporter.log(' --serial-modes <m1[:m2[:]]');
84 reporter.log(' Default: %s' % (':'.join(self.asSerialModesDef)));
85 reporter.log(' --serial-tests <t1[:t2[:]]');
86 reporter.log(' Default: %s' % (':'.join(self.asSerialTestsDef)));
87 reporter.log(' --verbose-test');
88 reporter.log(' Whether to enable verbose output when running the');
89 reporter.log(' test utility inside the VM');
90 return rc;
91
92 def parseOption(self, asArgs, iArg):
93 if asArgs[iArg] == '--serial-modes':
94 iArg += 1;
95 if iArg >= len(asArgs):
96 raise base.InvalidOption('The "--serial-modes" takes a colon separated list of serial port modes to test');
97 self.asSerialModes = asArgs[iArg].split(':');
98 for s in self.asSerialModes:
99 if s not in self.asSerialModesDef:
100 reporter.log('warning: The "--serial-modes" value "%s" is not a valid serial port mode.' % (s));
101 elif asArgs[iArg] == '--serial-tests':
102 iArg += 1;
103 if iArg >= len(asArgs):
104 raise base.InvalidOption('The "--serial-tests" takes a colon separated list of serial port tests');
105 self.asSerialTests = asArgs[iArg].split(':');
106 for s in self.asSerialTests:
107 if s not in self.asSerialTestsDef:
108 reporter.log('warning: The "--serial-tests" value "%s" is not a valid serial port test.' % (s));
109 elif asArgs[iArg] == '--verbose-test':
110 iArg += 1;
111 self.fVerboseTest = True;
112 else:
113 return vbox.TestDriver.parseOption(self, asArgs, iArg);
114
115 return iArg + 1;
116
117 def actionVerify(self):
118 if self.sVBoxValidationKitIso is None or not os.path.isfile(self.sVBoxValidationKitIso):
119 reporter.error('Cannot find the VBoxValidationKit.iso! (%s)'
120 'Please unzip a Validation Kit build in the current directory or in some parent one.'
121 % (self.sVBoxValidationKitIso,) );
122 return False;
123 return vbox.TestDriver.actionVerify(self);
124
125 def actionConfig(self):
126 # Make sure vboxapi has been imported so we can use the constants.
127 if not self.importVBoxApi():
128 return False;
129
130 assert self.sVBoxValidationKitIso is not None;
131 return self.oTestVmSet.actionConfig(self, sDvdImage = self.sVBoxValidationKitIso);
132
133 def actionExecute(self):
134 """
135 Execute the testcase.
136 """
137 return self.oTestVmSet.actionExecute(self, self.testOneVmConfig)
138
139
140 #
141 # Test execution helpers.
142 #
143
144 def _generateRawPortFilename(self, oTestDrv, oTestVm, sInfix, sSuffix):
145 """ Generates a raw port filename. """
146 random.seed();
147 sRandom = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10));
148 return os.path.join(oTestDrv.sScratchPath, oTestVm.sVmName + sInfix + sRandom + sSuffix);
149
150 def setupSerialMode(self, oSession, oTestVm, sMode):
151 """
152 Sets up the serial mode.
153 """
154 fRc = True;
155 fServer = False;
156 sLocation = None;
157 ePortMode = vboxcon.PortMode_Disconnected;
158 if sMode == 'RawFile':
159 sLocation = self._generateRawPortFilename(self, oTestVm, '-com1-', '.out');
160 ePortMode = vboxcon.PortMode_RawFile;
161 elif sMode == 'Tcp':
162 sLocation = '127.0.0.1:1234';
163 self.oLoopback = loopback.SerialLoopback(loopback.g_ksLoopbackTcpServ, sLocation);
164 ePortMode = vboxcon.PortMode_TCP;
165 elif sMode == 'TcpServ':
166 fServer = True;
167 sLocation = '1234';
168 ePortMode = vboxcon.PortMode_TCP;
169 self.oLoopback = loopback.SerialLoopback(loopback.g_ksLoopbackTcpClient, '127.0.0.1:1234');
170 elif sMode == 'NamedPipe':
171 sLocation = self._generateRawPortFilename(self, oTestVm, '-com1-', '.out');
172 ePortMode = vboxcon.PortMode_HostPipe;
173 self.oLoopback = loopback.SerialLoopback(loopback.g_ksLoopbackNamedPipeServ, sLocation);
174 elif sMode == 'NamedPipeServ':
175 fServer = True;
176 sLocation = self._generateRawPortFilename(self, oTestVm, '-com1-', '.out');
177 ePortMode = vboxcon.PortMode_HostPipe;
178 self.oLoopback = loopback.SerialLoopback(loopback.g_ksLoopbackNamedPipeClient, sLocation);
179 elif sMode == 'HostDev':
180 sLocation = '/dev/ttyUSB0';
181 ePortMode = vboxcon.PortMode_HostDevice;
182 else:
183 reporter.log('warning, invalid mode %s given' % (sMode, ));
184 fRc = False;
185
186 if fRc:
187 fRc = oSession.changeSerialPortAttachment(0, ePortMode, sLocation, fServer);
188 if fRc and (sMode == 'TcpServ' or sMode == 'NamedPipeServ'):
189 self.sleep(2); # Fudge to allow the TCP server to get started.
190 fRc = self.oLoopback.connect();
191 if not fRc:
192 reporter.log('Failed to connect to %s' % (sLocation, ));
193 self.sLocation = sLocation;
194
195 return fRc;
196
197 def testWrite(self, oSession, oTxsSession, oTestVm, sMode):
198 """
199 Does a simple write test verifying the output.
200 """
201 _ = oSession;
202
203 reporter.testStart('Write');
204 tupCmdLine = ('SerialTest', '--tests', 'write', '--txbytes', '1048576',);
205 if self.fVerboseTest:
206 tupCmdLine += ('--verbose');
207 if oTestVm.isWindows():
208 tupCmdLine += ('--device', r'\\.\COM1',);
209 elif oTestVm.isLinux():
210 tupCmdLine += ('--device', r'/dev/ttyS0',);
211
212 fRc = self.txsRunTest(oTxsSession, 'SerialTest', 3600 * 1000, \
213 '${CDROM}/${OS/ARCH}/SerialTest${EXESUFF}', tupCmdLine);
214 if not fRc:
215 reporter.testFailure('Running serial test utility failed');
216 elif sMode == 'RawFile':
217 # Open serial port and verify
218 cLast = 0;
219 try:
220 oFile = open(self.sLocation, 'rb');
221 sFmt = '=I';
222 cBytes = 4;
223 for i in xrange(1048576 / 4):
224 _ = i;
225 sData = oFile.read(cBytes);
226 tupUnpacked = struct.unpack(sFmt, sData);
227 cLast = cLast + 1;
228 if tupUnpacked[0] != cLast:
229 reporter.testFailure('Corruption detected, expected counter value %s, got %s'
230 % (cLast + 1, tupUnpacked[0]));
231 break;
232 oFile.close();
233 except:
234 reporter.logXcpt();
235 reporter.testFailure('Verifying the written data failed');
236 reporter.testDone();
237 return fRc;
238
239 def testReadWrite(self, oSession, oTxsSession, oTestVm):
240 """
241 Does a simple write test verifying the output.
242 """
243 _ = oSession;
244
245 reporter.testStart('ReadWrite');
246 tupCmdLine = ('SerialTest', '--tests', 'readwrite', '--txbytes', '1048576');
247 if self.fVerboseTest:
248 tupCmdLine += ('--verbose');
249 if oTestVm.isWindows():
250 tupCmdLine += ('--device', r'\\.\COM1',);
251 elif oTestVm.isLinux():
252 tupCmdLine += ('--device', r'/dev/ttyS0',);
253
254 fRc = self.txsRunTest(oTxsSession, 'SerialTest', 600 * 1000, \
255 '${CDROM}/${OS/ARCH}/SerialTest${EXESUFF}', tupCmdLine);
256 if not fRc:
257 reporter.testFailure('Running serial test utility failed');
258
259 reporter.testDone();
260 return fRc;
261
262 def isModeCompatibleWithTest(self, sMode, sTest):
263 """
264 Returns whether the given port mode and test combination is
265 supported for testing.
266 """
267 if sMode == 'RawFile' and sTest == 'ReadWrite':
268 return False;
269 elif sMode != 'RawFile' and sTest == 'Write':
270 return False;
271
272 return True;
273
274 def testOneVmConfig(self, oVM, oTestVm):
275 """
276 Runs the specified VM thru test #1.
277 """
278
279 # Reconfigure the VM
280 fRc = True;
281 oSession = self.openSession(oVM);
282 if oSession is not None:
283 fRc = oSession.enableSerialPort(0);
284
285 fRc = fRc and oSession.saveSettings();
286 fRc = oSession.close() and fRc;
287 oSession = None;
288 else:
289 fRc = False;
290
291 if fRc is True:
292 self.logVmInfo(oVM);
293 oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(oTestVm.sVmName, fCdWait = True);
294 if oSession is not None:
295 self.addTask(oTxsSession);
296
297 for sMode in self.asSerialModes:
298 reporter.testStart(sMode);
299 fRc = self.setupSerialMode(oSession, oTestVm, sMode);
300 if fRc:
301 for sTest in self.asSerialTests:
302 # Skip tests which don't work with the current mode.
303 if self.isModeCompatibleWithTest(sMode, sTest):
304 if sTest == 'Write':
305 fRc = self.testWrite(oSession, oTxsSession, oTestVm, sMode);
306 if sTest == 'ReadWrite':
307 fRc = self.testReadWrite(oSession, oTxsSession, oTestVm);
308 if self.oLoopback is not None:
309 self.oLoopback.shutdown();
310 self.oLoopback = None;
311
312 reporter.testDone();
313
314 self.removeTask(oTxsSession);
315 self.terminateVmBySession(oSession);
316 return fRc;
317
318if __name__ == '__main__':
319 sys.exit(tdSerial1().main(sys.argv));
320
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