VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/tests/storage/remoteexecutor.py@ 65747

Last change on this file since 65747 was 65747, checked in by vboxsync, 8 years ago

ValidationKit/tdStorageBenchmark1: Fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: remoteexecutor.py 65747 2017-02-10 21:07:52Z vboxsync $
3
4"""
5VirtualBox Validation Kit - Storage benchmark, test execution helpers.
6"""
7
8__copyright__ = \
9"""
10Copyright (C) 2016 Oracle Corporation
11
12This file is part of VirtualBox Open Source Edition (OSE), as
13available from http://www.virtualbox.org. This file is free software;
14you can redistribute it and/or modify it under the terms of the GNU
15General Public License (GPL) as published by the Free Software
16Foundation, in version 2 as it comes in the "COPYING" file of the
17VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
20The contents of this file may alternatively be used under the terms
21of the Common Development and Distribution License Version 1.0
22(CDDL) only, as it comes in the "COPYING.CDDL" file of the
23VirtualBox OSE distribution, in which case the provisions of the
24CDDL are applicable instead of those of the GPL.
25
26You may elect to license modified versions of this file under the
27terms and conditions of either the GPL or the CDDL or both.
28"""
29__version__ = "$Revision: 65747 $"
30
31
32# Standard Python imports.
33import array;
34import os;
35import shutil;
36import StringIO
37import subprocess;
38
39# Validation Kit imports.
40from common import utils;
41from testdriver import reporter;
42
43class StdInOutBuffer(object):
44 """ Standard input output buffer """
45
46 def __init__(self, sInput = None):
47 self.sInput = StringIO.StringIO();
48 if sInput is not None:
49 self.sInput.write(self._toString(sInput));
50 self.sInput.seek(0);
51 self.sOutput = '';
52
53 def _toString(self, sText):
54 """
55 Converts any possible array to
56 a string.
57 """
58 if isinstance(sText, array.array):
59 try:
60 return sText.tostring();
61 except:
62 pass;
63 else:
64 return sText;
65
66 def read(self, cb):
67 """file.read"""
68 return self.sInput.read(cb);
69
70 def write(self, sText):
71 """file.write"""
72 self.sOutput += self._toString(sText);
73 return None;
74
75 def getOutput(self):
76 """
77 Returns the output of the buffer.
78 """
79 return self.sOutput;
80
81 def close(self):
82 """ file.close """
83 return;
84
85class RemoteExecutor(object):
86 """
87 Helper for executing tests remotely through TXS or locally
88 """
89
90 def __init__(self, oTxsSession = None, asBinaryPaths = None, sScratchPath = None):
91 self.oTxsSession = oTxsSession;
92 self.asPaths = asBinaryPaths;
93 self.sScratchPath = sScratchPath;
94 if self.asPaths is None:
95 self.asPaths = [ ];
96
97 def _isFile(self, sFile):
98 """
99 Checks whether a file exists.
100 """
101 if self.oTxsSession is not None:
102 return self.oTxsSession.syncIsFile(sFile);
103 else:
104 return os.path.isfile(sFile);
105
106 def _getBinaryPath(self, sBinary):
107 """
108 Returns the complete path of the given binary if found
109 from the configured search path or None if not found.
110 """
111 for sPath in self.asPaths:
112 sFile = sPath + '/' + sBinary;
113 if self._isFile(sFile):
114 return sFile;
115 return None;
116
117 def _sudoExecuteSync(self, asArgs, sInput):
118 """
119 Executes a sudo child process synchronously.
120 Returns a tuple [True, 0] if the process executed successfully
121 and returned 0, otherwise [False, rc] is returned.
122 """
123 reporter.log('Executing [sudo]: %s' % (asArgs, ));
124 reporter.flushall();
125 fRc = True;
126 sOutput = '';
127 sError = '';
128 try:
129 oProcess = utils.sudoProcessPopen(asArgs, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
130 stderr=subprocess.PIPE, shell = False, close_fds = False);
131
132 sOutput, sError = oProcess.communicate(sInput);
133 iExitCode = oProcess.poll();
134
135 if iExitCode is not 0:
136 fRc = False;
137 except:
138 reporter.errorXcpt();
139 fRc = False;
140 reporter.log('Exit code [sudo]: %s (%s)' % (fRc, asArgs));
141 return (fRc, str(sOutput), str(sError));
142
143 def _execLocallyOrThroughTxs(self, sExec, asArgs, sInput, cMsTimeout):
144 """
145 Executes the given program locally or through TXS based on the
146 current config.
147 """
148 fRc = False;
149 sOutput = None;
150 if self.oTxsSession is not None:
151 oStdOut = StdInOutBuffer();
152 oStdErr = StdInOutBuffer();
153 oStdIn = None;
154 if sInput is not None:
155 oStdIn = StdInOutBuffer(sInput);
156 else:
157 oStdIn = '/dev/null'; # pylint: disable=R0204
158 fRc = self.oTxsSession.syncExecEx(sExec, (sExec,) + asArgs,
159 oStdIn = oStdIn, oStdOut = oStdOut,
160 oStdErr = oStdErr, cMsTimeout = cMsTimeout);
161 sOutput = oStdOut.getOutput();
162 sError = oStdErr.getOutput();
163 else:
164 fRc, sOutput, sError = self._sudoExecuteSync([sExec, ] + list(asArgs), sInput);
165 return (fRc, sOutput, sError);
166
167 def execBinary(self, sExec, asArgs, sInput = None, cMsTimeout = 3600000):
168 """
169 Executes the given binary with the given arguments
170 providing some optional input through stdin and
171 returning whether the process exited successfully and the output
172 in a string.
173 """
174
175 fRc = True;
176 sOutput = None;
177 sError = None;
178 sBinary = self._getBinaryPath(sExec);
179 if sBinary is not None:
180 fRc, sOutput, sError = self._execLocallyOrThroughTxs(sBinary, asArgs, sInput, cMsTimeout);
181 else:
182 fRc = False;
183 return (fRc, sOutput, sError);
184
185 def execBinaryNoStdOut(self, sExec, asArgs, sInput = None):
186 """
187 Executes the given binary with the given arguments
188 providing some optional input through stdin and
189 returning whether the process exited successfully.
190 """
191 fRc, _, _ = self.execBinary(sExec, asArgs, sInput);
192 return fRc;
193
194 def copyFile(self, sLocalFile, sFilename, cMsTimeout = 30000):
195 """
196 Copies the local file to the remote destination
197 if configured
198
199 Returns a file ID which can be used as an input parameter
200 to execBinary() resolving to the real filepath on the remote side
201 or locally.
202 """
203 sFileId = None;
204 if self.oTxsSession is not None:
205 sFileId = '${SCRATCH}/' + sFilename;
206 fRc = self.oTxsSession.syncUploadFile(sLocalFile, sFileId, cMsTimeout);
207 if not fRc:
208 sFileId = None;
209 else:
210 sFileId = self.sScratchPath + '/' + sFilename;
211 try:
212 shutil.copy(sLocalFile, sFileId);
213 except:
214 sFileId = None;
215
216 return sFileId;
217
218 def copyString(self, sContent, sFilename, cMsTimeout = 30000):
219 """
220 Creates a file remotely or locally with the given content.
221
222 Returns a file ID which can be used as an input parameter
223 to execBinary() resolving to the real filepath on the remote side
224 or locally.
225 """
226 sFileId = None;
227 if self.oTxsSession is not None:
228 sFileId = '${SCRATCH}/' + sFilename;
229 fRc = self.oTxsSession.syncUploadString(sContent, sFileId, cMsTimeout);
230 if not fRc:
231 sFileId = None;
232 else:
233 sFileId = self.sScratchPath + '/' + sFilename;
234 try:
235 oFile = open(sFileId, 'wb');
236 oFile.write(sContent);
237 oFile.close();
238 except:
239 sFileId = None;
240
241 return sFileId;
242
243 def mkDir(self, sDir, fMode = 0700, cMsTimeout = 30000):
244 """
245 Creates a new directory at the given location.
246 """
247 fRc = True;
248 if self.oTxsSession is not None:
249 fRc = self.oTxsSession.syncMkDir(sDir, fMode, cMsTimeout);
250 else:
251 fRc = self.execBinaryNoStdOut('mkdir', ('-m', format(fMode, 'o'), sDir));
252
253 return fRc;
254
255 def rmDir(self, sDir, cMsTimeout = 30000):
256 """
257 Removes the given directory.
258 """
259 fRc = True;
260 if self.oTxsSession is not None:
261 fRc = self.oTxsSession.syncRmDir(sDir, cMsTimeout);
262 else:
263 fRc = self.execBinaryNoStdOut('rmdir', (sDir,));
264
265 return fRc;
266
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