mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2008 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 5 | import os, pickle, random, re, resource, select, shutil, signal, StringIO |
| 6 | import socket, struct, subprocess, sys, time, textwrap, urllib, urlparse |
| 7 | import warnings |
mbligh | 76a4293 | 2008-10-09 20:35:38 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import error, barrier, debug |
mbligh | 81edd79 | 2008-08-26 16:54:02 +0000 | [diff] [blame] | 9 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 10 | def deprecated(func): |
| 11 | """This is a decorator which can be used to mark functions as deprecated. |
| 12 | It will result in a warning being emmitted when the function is used.""" |
| 13 | def new_func(*args, **dargs): |
| 14 | warnings.warn("Call to deprecated function %s." % func.__name__, |
| 15 | category=DeprecationWarning) |
| 16 | return func(*args, **dargs) |
| 17 | new_func.__name__ = func.__name__ |
| 18 | new_func.__doc__ = func.__doc__ |
| 19 | new_func.__dict__.update(func.__dict__) |
| 20 | return new_func |
| 21 | |
| 22 | |
| 23 | class BgJob(object): |
mbligh | bd96b45 | 2008-09-03 23:14:27 +0000 | [diff] [blame] | 24 | def __init__(self, command, stdout_tee=None, stderr_tee=None, verbose=True): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 25 | self.command = command |
| 26 | self.stdout_tee = stdout_tee |
| 27 | self.stderr_tee = stderr_tee |
| 28 | self.result = CmdResult(command) |
mbligh | 76a4293 | 2008-10-09 20:35:38 +0000 | [diff] [blame] | 29 | self.log = debug.get_logger() |
mbligh | bd96b45 | 2008-09-03 23:14:27 +0000 | [diff] [blame] | 30 | if verbose: |
mbligh | 76a4293 | 2008-10-09 20:35:38 +0000 | [diff] [blame] | 31 | self.log.debug("running: %s" % command) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 32 | self.sp = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 33 | stderr=subprocess.PIPE, |
| 34 | preexec_fn=self._reset_sigpipe, shell=True, |
| 35 | executable="/bin/bash") |
| 36 | |
| 37 | |
| 38 | def output_prepare(self, stdout_file=None, stderr_file=None): |
| 39 | self.stdout_file = stdout_file |
| 40 | self.stderr_file = stderr_file |
| 41 | |
| 42 | def process_output(self, stdout=True, final_read=False): |
| 43 | """output_prepare must be called prior to calling this""" |
| 44 | if stdout: |
| 45 | pipe, buf, tee = self.sp.stdout, self.stdout_file, self.stdout_tee |
| 46 | else: |
| 47 | pipe, buf, tee = self.sp.stderr, self.stderr_file, self.stderr_tee |
| 48 | |
| 49 | if final_read: |
| 50 | # read in all the data we can from pipe and then stop |
| 51 | data = [] |
| 52 | while select.select([pipe], [], [], 0)[0]: |
| 53 | data.append(os.read(pipe.fileno(), 1024)) |
| 54 | if len(data[-1]) == 0: |
| 55 | break |
| 56 | data = "".join(data) |
| 57 | else: |
| 58 | # perform a single read |
| 59 | data = os.read(pipe.fileno(), 1024) |
| 60 | buf.write(data) |
| 61 | if tee: |
| 62 | tee.write(data) |
| 63 | tee.flush() |
| 64 | |
| 65 | |
| 66 | def cleanup(self): |
| 67 | self.sp.stdout.close() |
| 68 | self.sp.stderr.close() |
| 69 | self.result.stdout = self.stdout_file.getvalue() |
| 70 | self.result.stderr = self.stderr_file.getvalue() |
| 71 | |
| 72 | |
| 73 | def _reset_sigpipe(self): |
| 74 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 75 | |
mbligh | 81edd79 | 2008-08-26 16:54:02 +0000 | [diff] [blame] | 76 | |
| 77 | def ip_to_long(ip): |
| 78 | # !L is a long in network byte order |
| 79 | return struct.unpack('!L', socket.inet_aton(ip))[0] |
| 80 | |
| 81 | |
| 82 | def long_to_ip(number): |
| 83 | # See above comment. |
| 84 | return socket.inet_ntoa(struct.pack('!L', number)) |
| 85 | |
| 86 | |
| 87 | def create_subnet_mask(bits): |
| 88 | # ~ does weird things in python...but this does work |
| 89 | return (1 << 32) - (1 << 32-bits) |
| 90 | |
| 91 | |
| 92 | def format_ip_with_mask(ip, mask_bits): |
| 93 | masked_ip = ip_to_long(ip) & create_subnet_mask(mask_bits) |
| 94 | return "%s/%s" % (long_to_ip(masked_ip), mask_bits) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 95 | |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 96 | |
jadmanski | e80d471 | 2008-10-03 16:15:59 +0000 | [diff] [blame] | 97 | def normalize_hostname(alias): |
| 98 | ip = socket.gethostbyname(alias) |
| 99 | return socket.gethostbyaddr(ip)[0] |
| 100 | |
| 101 | |
mbligh | d6d043c | 2008-09-27 21:00:45 +0000 | [diff] [blame] | 102 | def get_ip_local_port_range(): |
| 103 | match = re.match(r'\s*(\d+)\s*(\d+)\s*$', |
| 104 | read_one_line('/proc/sys/net/ipv4/ip_local_port_range')) |
| 105 | return (int(match.group(1)), int(match.group(2))) |
| 106 | |
| 107 | |
| 108 | def set_ip_local_port_range(lower, upper): |
| 109 | write_one_line('/proc/sys/net/ipv4/ip_local_port_range', |
| 110 | '%d %d\n' % (lower, upper)) |
| 111 | |
mbligh | 315b941 | 2008-10-01 03:34:11 +0000 | [diff] [blame] | 112 | |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 113 | def read_one_line(filename): |
mbligh | 6e8840c | 2008-07-11 18:05:49 +0000 | [diff] [blame] | 114 | return open(filename, 'r').readline().rstrip('\n') |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 115 | |
| 116 | |
mbligh | b9d0551 | 2008-10-18 13:53:27 +0000 | [diff] [blame] | 117 | def write_one_line(filename, line): |
| 118 | open_write_close(filename, line.rstrip('\n') + '\n') |
| 119 | |
| 120 | |
| 121 | def open_write_close(filename, data): |
mbligh | 618ac9e | 2008-10-06 17:14:32 +0000 | [diff] [blame] | 122 | f = open(filename, 'w') |
mbligh | b9d0551 | 2008-10-18 13:53:27 +0000 | [diff] [blame] | 123 | try: |
| 124 | f.write(data) |
| 125 | finally: |
| 126 | f.close() |
jadmanski | 5182e16 | 2008-05-13 21:48:16 +0000 | [diff] [blame] | 127 | |
| 128 | |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 129 | def read_keyval(path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 130 | """ |
| 131 | Read a key-value pair format file into a dictionary, and return it. |
| 132 | Takes either a filename or directory name as input. If it's a |
| 133 | directory name, we assume you want the file to be called keyval. |
| 134 | """ |
| 135 | if os.path.isdir(path): |
| 136 | path = os.path.join(path, 'keyval') |
| 137 | keyval = {} |
| 138 | for line in open(path): |
jadmanski | a6014a0 | 2008-07-14 19:41:54 +0000 | [diff] [blame] | 139 | line = re.sub('#.*', '', line).rstrip() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 140 | if not re.search(r'^[-\w]+=', line): |
| 141 | raise ValueError('Invalid format line: %s' % line) |
| 142 | key, value = line.split('=', 1) |
| 143 | if re.search('^\d+$', value): |
| 144 | value = int(value) |
| 145 | elif re.search('^(\d+\.)?\d+$', value): |
| 146 | value = float(value) |
| 147 | keyval[key] = value |
| 148 | return keyval |
mbligh | de0d47e | 2008-03-28 14:37:18 +0000 | [diff] [blame] | 149 | |
| 150 | |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 151 | def write_keyval(path, dictionary, type_tag=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 152 | """ |
| 153 | Write a key-value pair format file out to a file. This uses append |
| 154 | mode to open the file, so existing text will not be overwritten or |
| 155 | reparsed. |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 156 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 157 | If type_tag is None, then the key must be composed of alphanumeric |
| 158 | characters (or dashes+underscores). However, if type-tag is not |
| 159 | null then the keys must also have "{type_tag}" as a suffix. At |
| 160 | the moment the only valid values of type_tag are "attr" and "perf". |
| 161 | """ |
| 162 | if os.path.isdir(path): |
| 163 | path = os.path.join(path, 'keyval') |
| 164 | keyval = open(path, 'a') |
jadmanski | cc54917 | 2008-05-21 18:11:51 +0000 | [diff] [blame] | 165 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 166 | if type_tag is None: |
| 167 | key_regex = re.compile(r'^[-\w]+$') |
| 168 | else: |
| 169 | if type_tag not in ('attr', 'perf'): |
| 170 | raise ValueError('Invalid type tag: %s' % type_tag) |
| 171 | escaped_tag = re.escape(type_tag) |
| 172 | key_regex = re.compile(r'^[-\w]+\{%s\}$' % escaped_tag) |
| 173 | try: |
| 174 | for key, value in dictionary.iteritems(): |
| 175 | if not key_regex.search(key): |
| 176 | raise ValueError('Invalid key: %s' % key) |
| 177 | keyval.write('%s=%s\n' % (key, value)) |
| 178 | finally: |
| 179 | keyval.close() |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | def is_url(path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 183 | """Return true if path looks like a URL""" |
| 184 | # for now, just handle http and ftp |
| 185 | url_parts = urlparse.urlparse(path) |
| 186 | return (url_parts[0] in ('http', 'ftp')) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 187 | |
| 188 | |
jadmanski | ed91ba9 | 2008-09-30 17:19:27 +0000 | [diff] [blame] | 189 | def urlopen(url, data=None, proxies=None, timeout=5): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 190 | """Wrapper to urllib.urlopen with timeout addition.""" |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 191 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 192 | # Save old timeout |
| 193 | old_timeout = socket.getdefaulttimeout() |
| 194 | socket.setdefaulttimeout(timeout) |
| 195 | try: |
| 196 | return urllib.urlopen(url, data=data, proxies=proxies) |
| 197 | finally: |
| 198 | socket.setdefaulttimeout(old_timeout) |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def urlretrieve(url, filename=None, reporthook=None, data=None, timeout=300): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | """Wrapper to urllib.urlretrieve with timeout addition.""" |
| 203 | old_timeout = socket.getdefaulttimeout() |
| 204 | socket.setdefaulttimeout(timeout) |
| 205 | try: |
| 206 | return urllib.urlretrieve(url, filename=filename, |
| 207 | reporthook=reporthook, data=data) |
| 208 | finally: |
| 209 | socket.setdefaulttimeout(old_timeout) |
| 210 | |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 211 | |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 212 | def get_file(src, dest, permissions=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 213 | """Get a file from src, which can be local or a remote URL""" |
| 214 | if (src == dest): |
| 215 | return |
| 216 | if (is_url(src)): |
| 217 | print 'PWD: ' + os.getcwd() |
| 218 | print 'Fetching \n\t', src, '\n\t->', dest |
| 219 | try: |
| 220 | urllib.urlretrieve(src, dest) |
| 221 | except IOError, e: |
| 222 | raise error.AutotestError('Unable to retrieve %s (to %s)' |
| 223 | % (src, dest), e) |
| 224 | else: |
| 225 | shutil.copyfile(src, dest) |
| 226 | if permissions: |
| 227 | os.chmod(dest, permissions) |
| 228 | return dest |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | def unmap_url(srcdir, src, destdir='.'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 232 | """ |
| 233 | Receives either a path to a local file or a URL. |
| 234 | returns either the path to the local file, or the fetched URL |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 235 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 236 | unmap_url('/usr/src', 'foo.tar', '/tmp') |
| 237 | = '/usr/src/foo.tar' |
| 238 | unmap_url('/usr/src', 'http://site/file', '/tmp') |
| 239 | = '/tmp/file' |
| 240 | (after retrieving it) |
| 241 | """ |
| 242 | if is_url(src): |
| 243 | url_parts = urlparse.urlparse(src) |
| 244 | filename = os.path.basename(url_parts[2]) |
| 245 | dest = os.path.join(destdir, filename) |
| 246 | return get_file(src, dest) |
| 247 | else: |
| 248 | return os.path.join(srcdir, src) |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | def update_version(srcdir, preserve_srcdir, new_version, install, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 252 | *args, **dargs): |
| 253 | """ |
| 254 | Make sure srcdir is version new_version |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 255 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 256 | If not, delete it and install() the new version. |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 257 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 258 | In the preserve_srcdir case, we just check it's up to date, |
| 259 | and if not, we rerun install, without removing srcdir |
| 260 | """ |
| 261 | versionfile = os.path.join(srcdir, '.version') |
| 262 | install_needed = True |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 263 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 264 | if os.path.exists(versionfile): |
| 265 | old_version = pickle.load(open(versionfile)) |
| 266 | if old_version == new_version: |
| 267 | install_needed = False |
mbligh | 6231cd6 | 2008-02-02 19:18:33 +0000 | [diff] [blame] | 268 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 269 | if install_needed: |
| 270 | if not preserve_srcdir and os.path.exists(srcdir): |
| 271 | shutil.rmtree(srcdir) |
| 272 | install(*args, **dargs) |
| 273 | if os.path.exists(srcdir): |
| 274 | pickle.dump(new_version, open(versionfile, 'w')) |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 275 | |
| 276 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 277 | def run(command, timeout=None, ignore_status=False, |
mbligh | bd96b45 | 2008-09-03 23:14:27 +0000 | [diff] [blame] | 278 | stdout_tee=None, stderr_tee=None, verbose=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 279 | """ |
| 280 | Run a command on the host. |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 281 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 282 | Args: |
| 283 | command: the command line string |
| 284 | timeout: time limit in seconds before attempting to |
| 285 | kill the running process. The run() function |
| 286 | will take a few seconds longer than 'timeout' |
| 287 | to complete if it has to kill the process. |
| 288 | ignore_status: do not raise an exception, no matter what |
| 289 | the exit code of the command is. |
| 290 | stdout_tee: optional file-like object to which stdout data |
| 291 | will be written as it is generated (data will still |
| 292 | be stored in result.stdout) |
| 293 | stderr_tee: likewise for stderr |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 294 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 295 | Returns: |
| 296 | a CmdResult object |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 297 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 298 | Raises: |
| 299 | CmdError: the exit code of the command |
| 300 | execution was not 0 |
| 301 | """ |
mbligh | bd96b45 | 2008-09-03 23:14:27 +0000 | [diff] [blame] | 302 | bg_job = join_bg_jobs((BgJob(command, stdout_tee, stderr_tee, verbose),), |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 303 | timeout)[0] |
| 304 | if not ignore_status and bg_job.result.exit_status: |
jadmanski | 9c1098b | 2008-09-02 14:18:48 +0000 | [diff] [blame] | 305 | raise error.CmdError(command, bg_job.result, |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 306 | "Command returned non-zero exit status") |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 307 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 308 | return bg_job.result |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 309 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 310 | def run_parallel(commands, timeout=None, ignore_status=False, |
| 311 | stdout_tee=None, stderr_tee=None): |
| 312 | """Beahves the same as run with the following exceptions: |
| 313 | |
| 314 | - commands is a list of commands to run in parallel. |
| 315 | - ignore_status toggles whether or not an exception should be raised |
| 316 | on any error. |
| 317 | |
| 318 | returns a list of CmdResult objects |
| 319 | """ |
| 320 | bg_jobs = [] |
| 321 | for command in commands: |
| 322 | bg_jobs.append(BgJob(command, stdout_tee, stderr_tee)) |
| 323 | |
| 324 | # Updates objects in bg_jobs list with their process information |
| 325 | join_bg_jobs(bg_jobs, timeout) |
| 326 | |
| 327 | for bg_job in bg_jobs: |
| 328 | if not ignore_status and bg_job.result.exit_status: |
| 329 | raise error.CmdError(command, bg_job.result, |
| 330 | "Command returned non-zero exit status") |
| 331 | |
| 332 | return [bg_job.result for bg_job in bg_jobs] |
| 333 | |
| 334 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 335 | @deprecated |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 336 | def run_bg(command): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 337 | """Function deprecated. Please use BgJob class instead.""" |
| 338 | bg_job = BgJob(command) |
| 339 | return bg_job.sp, bg_job.result |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 340 | |
| 341 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 342 | def join_bg_jobs(bg_jobs, timeout=None): |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 343 | """Joins the bg_jobs with the current thread. |
| 344 | |
| 345 | Returns the same list of bg_jobs objects that was passed in. |
| 346 | """ |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 347 | ret, timeouterr = 0, False |
| 348 | for bg_job in bg_jobs: |
| 349 | bg_job.output_prepare(StringIO.StringIO(), StringIO.StringIO()) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 350 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 351 | try: |
| 352 | # We are holding ends to stdin, stdout pipes |
| 353 | # hence we need to be sure to close those fds no mater what |
| 354 | start_time = time.time() |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 355 | timeout_error = _wait_for_commands(bg_jobs, start_time, timeout) |
| 356 | |
| 357 | for bg_job in bg_jobs: |
| 358 | # Process stdout and stderr |
| 359 | bg_job.process_output(stdout=True,final_read=True) |
| 360 | bg_job.process_output(stdout=False,final_read=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 361 | finally: |
| 362 | # close our ends of the pipes to the sp no matter what |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 363 | for bg_job in bg_jobs: |
| 364 | bg_job.cleanup() |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 365 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 366 | if timeout_error: |
| 367 | # TODO: This needs to be fixed to better represent what happens when |
| 368 | # running in parallel. However this is backwards compatable, so it will |
| 369 | # do for the time being. |
| 370 | raise error.CmdError(bg_jobs[0].command, bg_jobs[0].result, |
| 371 | "Command(s) did not complete within %d seconds" |
| 372 | % timeout) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 373 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 374 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 375 | return bg_jobs |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 376 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 377 | |
| 378 | def _wait_for_commands(bg_jobs, start_time, timeout): |
| 379 | # This returns True if it must return due to a timeout, otherwise False. |
| 380 | |
mbligh | f0b4a0a | 2008-09-03 20:46:16 +0000 | [diff] [blame] | 381 | # To check for processes which terminate without producing any output |
| 382 | # a 1 second timeout is used in select. |
| 383 | SELECT_TIMEOUT = 1 |
| 384 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 385 | select_list = [] |
| 386 | reverse_dict = {} |
| 387 | for bg_job in bg_jobs: |
| 388 | select_list.append(bg_job.sp.stdout) |
| 389 | select_list.append(bg_job.sp.stderr) |
| 390 | reverse_dict[bg_job.sp.stdout] = (bg_job,True) |
| 391 | reverse_dict[bg_job.sp.stderr] = (bg_job,False) |
| 392 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 393 | if timeout: |
| 394 | stop_time = start_time + timeout |
| 395 | time_left = stop_time - time.time() |
| 396 | else: |
| 397 | time_left = None # so that select never times out |
| 398 | while not timeout or time_left > 0: |
| 399 | # select will return when stdout is ready (including when it is |
| 400 | # EOF, that is the process has terminated). |
mbligh | f0b4a0a | 2008-09-03 20:46:16 +0000 | [diff] [blame] | 401 | ready, _, _ = select.select(select_list, [], [], SELECT_TIMEOUT) |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 402 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 403 | # os.read() has to be used instead of |
| 404 | # subproc.stdout.read() which will otherwise block |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 405 | for fileno in ready: |
| 406 | bg_job,stdout = reverse_dict[fileno] |
| 407 | bg_job.process_output(stdout) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 408 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 409 | remaining_jobs = [x for x in bg_jobs if x.result.exit_status is None] |
| 410 | if len(remaining_jobs) == 0: |
| 411 | return False |
| 412 | for bg_job in remaining_jobs: |
| 413 | bg_job.result.exit_status = bg_job.sp.poll() |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 414 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 415 | if timeout: |
| 416 | time_left = stop_time - time.time() |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 417 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 418 | # Kill all processes which did not complete prior to timeout |
| 419 | for bg_job in [x for x in bg_jobs if x.result.exit_status is None]: |
| 420 | nuke_subprocess(bg_job.sp) |
mbligh | 095dc64 | 2008-10-01 03:41:35 +0000 | [diff] [blame] | 421 | bg_job.result.exit_status = bg_job.sp.poll() |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 422 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 423 | return True |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 424 | |
| 425 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 426 | def nuke_subprocess(subproc): |
jadmanski | 09f9203 | 2008-09-17 14:05:27 +0000 | [diff] [blame] | 427 | # check if the subprocess is still alive, first |
| 428 | if subproc.poll() is not None: |
| 429 | return subproc.poll() |
| 430 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 431 | # the process has not terminated within timeout, |
| 432 | # kill it via an escalating series of signals. |
| 433 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 434 | for sig in signal_queue: |
| 435 | try: |
| 436 | os.kill(subproc.pid, sig) |
| 437 | # The process may have died before we could kill it. |
| 438 | except OSError: |
| 439 | pass |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 440 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 441 | for i in range(5): |
| 442 | rc = subproc.poll() |
| 443 | if rc != None: |
| 444 | return rc |
| 445 | time.sleep(1) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 446 | |
| 447 | |
| 448 | def nuke_pid(pid): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 449 | # the process has not terminated within timeout, |
| 450 | # kill it via an escalating series of signals. |
| 451 | signal_queue = [signal.SIGTERM, signal.SIGKILL] |
| 452 | for sig in signal_queue: |
| 453 | try: |
| 454 | os.kill(pid, sig) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 455 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 456 | # The process may have died before we could kill it. |
| 457 | except OSError: |
| 458 | pass |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 459 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 460 | try: |
| 461 | for i in range(5): |
| 462 | status = os.waitpid(pid, os.WNOHANG)[0] |
| 463 | if status == pid: |
| 464 | return |
| 465 | time.sleep(1) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 466 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 467 | if status != pid: |
| 468 | raise error.AutoservRunError('Could not kill %d' |
| 469 | % pid, None) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 470 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 471 | # the process died before we join it. |
| 472 | except OSError: |
| 473 | pass |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 474 | |
| 475 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 476 | |
| 477 | def system(command, timeout=None, ignore_status=False): |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 478 | """This function returns the exit status of command.""" |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 479 | return run(command, timeout=timeout, ignore_status=ignore_status, |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 480 | stdout_tee=sys.stdout, stderr_tee=sys.stderr).exit_status |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 481 | |
| 482 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 483 | def system_parallel(commands, timeout=None, ignore_status=False): |
| 484 | """This function returns a list of exit statuses for the respective |
| 485 | list of commands.""" |
| 486 | return [bg_jobs.exit_status for bg_jobs in |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 487 | run_parallel(commands, timeout=timeout, ignore_status=ignore_status, |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 488 | stdout_tee=sys.stdout, stderr_tee=sys.stderr)] |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 489 | |
| 490 | |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 491 | def system_output(command, timeout=None, ignore_status=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 492 | retain_output=False): |
| 493 | if retain_output: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 494 | out = run(command, timeout=timeout, ignore_status=ignore_status, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 495 | stdout_tee=sys.stdout, stderr_tee=sys.stderr).stdout |
| 496 | else: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 497 | out = run(command, timeout=timeout, ignore_status=ignore_status).stdout |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 498 | if out[-1:] == '\n': out = out[:-1] |
| 499 | return out |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 500 | |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 501 | |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 502 | def system_output_parallel(commands, timeout=None, ignore_status=False, |
| 503 | retain_output=False): |
| 504 | if retain_output: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 505 | out = [bg_job.stdout for bg_job in run_parallel(commands, |
| 506 | timeout=timeout, ignore_status=ignore_status, |
| 507 | stdout_tee=sys.stdout, stderr_tee=sys.stderr)] |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 508 | else: |
mbligh | f8dffb1 | 2008-10-29 16:45:26 +0000 | [diff] [blame] | 509 | out = [bg_job.stdout for bg_job in run_parallel(commands, |
| 510 | timeout=timeout, ignore_status=ignore_status)] |
mbligh | a5630a5 | 2008-09-03 22:09:50 +0000 | [diff] [blame] | 511 | for x in out: |
| 512 | if out[-1:] == '\n': out = out[:-1] |
| 513 | return out |
| 514 | |
| 515 | |
| 516 | def get_cpu_percentage(function, *args, **dargs): |
| 517 | """Returns a tuple containing the CPU% and return value from function call. |
| 518 | |
| 519 | This function calculates the usage time by taking the difference of |
| 520 | the user and system times both before and after the function call. |
| 521 | """ |
| 522 | child_pre = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 523 | self_pre = resource.getrusage(resource.RUSAGE_SELF) |
| 524 | start = time.time() |
| 525 | to_return = function(*args, **dargs) |
| 526 | elapsed = time.time() - start |
| 527 | self_post = resource.getrusage(resource.RUSAGE_SELF) |
| 528 | child_post = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 529 | |
| 530 | # Calculate CPU Percentage |
| 531 | s_user, s_system = [a - b for a, b in zip(self_post, self_pre)[:2]] |
| 532 | c_user, c_system = [a - b for a, b in zip(child_post, child_pre)[:2]] |
| 533 | cpu_percent = (s_user + c_user + s_system + c_system) / elapsed |
| 534 | |
| 535 | return cpu_percent, to_return |
| 536 | |
| 537 | |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 538 | """ |
| 539 | This function is used when there is a need to run more than one |
| 540 | job simultaneously starting exactly at the same time. It basically returns |
| 541 | a modified control file (containing the synchronization code prepended) |
| 542 | whenever it is ready to run the control file. The synchronization |
| 543 | is done using barriers to make sure that the jobs start at the same time. |
| 544 | |
| 545 | Here is how the synchronization is done to make sure that the tests |
| 546 | start at exactly the same time on the client. |
| 547 | sc_bar is a server barrier and s_bar, c_bar are the normal barriers |
| 548 | |
| 549 | Job1 Job2 ...... JobN |
| 550 | Server: | sc_bar |
| 551 | Server: | s_bar ...... s_bar |
| 552 | Server: | at.run() at.run() ...... at.run() |
| 553 | ----------|------------------------------------------------------ |
| 554 | Client | sc_bar |
| 555 | Client | c_bar c_bar ...... c_bar |
| 556 | Client | <run test> <run test> ...... <run test> |
| 557 | |
| 558 | |
| 559 | PARAMS: |
| 560 | control_file : The control file which to which the above synchronization |
| 561 | code would be prepended to |
| 562 | host_name : The host name on which the job is going to run |
| 563 | host_num (non negative) : A number to identify the machine so that we have |
| 564 | different sets of s_bar_ports for each of the machines. |
| 565 | instance : The number of the job |
| 566 | num_jobs : Total number of jobs that are going to run in parallel with |
| 567 | this job starting at the same time |
| 568 | port_base : Port number that is used to derive the actual barrier ports. |
| 569 | |
| 570 | RETURN VALUE: |
| 571 | The modified control file. |
| 572 | |
| 573 | """ |
| 574 | def get_sync_control_file(control, host_name, host_num, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 575 | instance, num_jobs, port_base=63100): |
| 576 | sc_bar_port = port_base |
| 577 | c_bar_port = port_base |
| 578 | if host_num < 0: |
| 579 | print "Please provide a non negative number for the host" |
| 580 | return None |
| 581 | s_bar_port = port_base + 1 + host_num # The set of s_bar_ports are |
| 582 | # the same for a given machine |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 583 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 584 | sc_bar_timeout = 180 |
| 585 | s_bar_timeout = c_bar_timeout = 120 |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 586 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 587 | # The barrier code snippet is prepended into the conrol file |
| 588 | # dynamically before at.run() is called finally. |
| 589 | control_new = [] |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 590 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 591 | # jobid is the unique name used to identify the processes |
| 592 | # trying to reach the barriers |
| 593 | jobid = "%s#%d" % (host_name, instance) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 594 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 595 | rendv = [] |
| 596 | # rendvstr is a temp holder for the rendezvous list of the processes |
| 597 | for n in range(num_jobs): |
| 598 | rendv.append("'%s#%d'" % (host_name, n)) |
| 599 | rendvstr = ",".join(rendv) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 600 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 601 | if instance == 0: |
| 602 | # Do the setup and wait at the server barrier |
| 603 | # Clean up the tmp and the control dirs for the first instance |
| 604 | control_new.append('if os.path.exists(job.tmpdir):') |
| 605 | control_new.append("\t system('umount -f %s > /dev/null" |
| 606 | "2> /dev/null' % job.tmpdir," |
| 607 | "ignore_status=True)") |
| 608 | control_new.append("\t system('rm -rf ' + job.tmpdir)") |
| 609 | control_new.append( |
| 610 | 'b0 = job.barrier("%s", "sc_bar", %d, port=%d)' |
| 611 | % (jobid, sc_bar_timeout, sc_bar_port)) |
| 612 | control_new.append( |
| 613 | 'b0.rendevous_servers("PARALLEL_MASTER", "%s")' |
| 614 | % jobid) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 615 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 616 | elif instance == 1: |
| 617 | # Wait at the server barrier to wait for instance=0 |
| 618 | # process to complete setup |
| 619 | b0 = barrier.barrier("PARALLEL_MASTER", "sc_bar", sc_bar_timeout, |
| 620 | port=sc_bar_port) |
| 621 | b0.rendevous_servers("PARALLEL_MASTER", jobid) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 622 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 623 | if(num_jobs > 2): |
| 624 | b1 = barrier.barrier(jobid, "s_bar", s_bar_timeout, |
| 625 | port=s_bar_port) |
| 626 | b1.rendevous(rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 627 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 628 | else: |
| 629 | # For the rest of the clients |
| 630 | b2 = barrier.barrier(jobid, "s_bar", s_bar_timeout, port=s_bar_port) |
| 631 | b2.rendevous(rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 632 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 633 | # Client side barrier for all the tests to start at the same time |
| 634 | control_new.append('b1 = job.barrier("%s", "c_bar", %d, port=%d)' |
| 635 | % (jobid, c_bar_timeout, c_bar_port)) |
| 636 | control_new.append("b1.rendevous(%s)" % rendvstr) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 637 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 638 | # Stick in the rest of the control file |
| 639 | control_new.append(control) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 640 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 641 | return "\n".join(control_new) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 642 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 643 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 644 | def get_arch(run_function=run): |
| 645 | """ |
| 646 | Get the hardware architecture of the machine. |
| 647 | run_function is used to execute the commands. It defaults to |
| 648 | utils.run() but a custom method (if provided) should be of the |
| 649 | same schema as utils.run. It should return a CmdResult object and |
| 650 | throw a CmdError exception. |
| 651 | """ |
| 652 | arch = run_function('/bin/uname -m').stdout.rstrip() |
| 653 | if re.match(r'i\d86$', arch): |
| 654 | arch = 'i386' |
| 655 | return arch |
| 656 | |
| 657 | |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 658 | class CmdResult(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 659 | """ |
| 660 | Command execution result. |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 661 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 662 | command: String containing the command line itself |
| 663 | exit_status: Integer exit code of the process |
| 664 | stdout: String containing stdout of the process |
| 665 | stderr: String containing stderr of the process |
| 666 | duration: Elapsed wall clock time running the process |
| 667 | """ |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 668 | |
| 669 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 670 | def __init__(self, command=None, stdout="", stderr="", |
| 671 | exit_status=None, duration=0): |
| 672 | self.command = command |
| 673 | self.exit_status = exit_status |
| 674 | self.stdout = stdout |
| 675 | self.stderr = stderr |
| 676 | self.duration = duration |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 677 | |
| 678 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 679 | def __repr__(self): |
| 680 | wrapper = textwrap.TextWrapper(width = 78, |
| 681 | initial_indent="\n ", |
| 682 | subsequent_indent=" ") |
| 683 | |
| 684 | stdout = self.stdout.rstrip() |
| 685 | if stdout: |
| 686 | stdout = "\nstdout:\n%s" % stdout |
| 687 | |
| 688 | stderr = self.stderr.rstrip() |
| 689 | if stderr: |
| 690 | stderr = "\nstderr:\n%s" % stderr |
| 691 | |
| 692 | return ("* Command: %s\n" |
| 693 | "Exit status: %s\n" |
| 694 | "Duration: %s\n" |
| 695 | "%s" |
| 696 | "%s" |
| 697 | % (wrapper.fill(self.command), self.exit_status, |
| 698 | self.duration, stdout, stderr)) |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 699 | |
| 700 | |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 701 | class run_randomly: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 702 | def __init__(self, run_sequentially=False): |
| 703 | # Run sequentially is for debugging control files |
| 704 | self.test_list = [] |
| 705 | self.run_sequentially = run_sequentially |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 706 | |
| 707 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 708 | def add(self, *args, **dargs): |
| 709 | test = (args, dargs) |
| 710 | self.test_list.append(test) |
mbligh | 462c015 | 2008-03-13 15:37:10 +0000 | [diff] [blame] | 711 | |
| 712 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 713 | def run(self, fn): |
| 714 | while self.test_list: |
| 715 | test_index = random.randint(0, len(self.test_list)-1) |
| 716 | if self.run_sequentially: |
| 717 | test_index = 0 |
| 718 | (args, dargs) = self.test_list.pop(test_index) |
| 719 | fn(*args, **dargs) |