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