mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | This module defines the Autotest class |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| 8 | Autotest: software to run tests automatically |
| 9 | """ |
| 10 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 11 | __author__ = """ |
| 12 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 13 | poirier@google.com (Benjamin Poirier), |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 14 | stutsman@google.com (Ryan Stutsman) |
| 15 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | |
| 17 | import re |
| 18 | import os |
| 19 | import sys |
| 20 | import subprocess |
| 21 | import urllib |
| 22 | import tempfile |
| 23 | import shutil |
| 24 | |
| 25 | import installable_object |
| 26 | import errors |
| 27 | import utils |
| 28 | |
| 29 | |
| 30 | AUTOTEST_SVN = 'svn://test.kernel.org/autotest/trunk/client' |
| 31 | AUTOTEST_HTTP = 'http://test.kernel.org/svn/autotest/trunk/client' |
| 32 | |
| 33 | # Timeouts for powering down and up respectively |
| 34 | HALT_TIME = 300 |
| 35 | BOOT_TIME = 300 |
| 36 | |
| 37 | |
| 38 | class AutotestRunError(errors.AutoservRunError): |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | class Autotest(installable_object.InstallableObject): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 43 | """ |
| 44 | This class represents the Autotest program. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 45 | |
| 46 | Autotest is used to run tests automatically and collect the results. |
| 47 | It also supports profilers. |
| 48 | |
| 49 | Implementation details: |
| 50 | This is a leaf class in an abstract class hierarchy, it must |
| 51 | implement the unimplemented methods in parent classes. |
| 52 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 53 | def __init__(self, host = None): |
| 54 | self.host = host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 55 | self.got = False |
| 56 | self.installed = False |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame^] | 57 | self.serverdir = utils.get_server_dir() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 58 | super(Autotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 59 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 60 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 61 | def install(self, host = None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 62 | """ |
| 63 | Install autotest. If get() was not called previously, an |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 64 | attempt will be made to install from the autotest svn |
| 65 | repository. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 66 | |
| 67 | Args: |
| 68 | host: a Host instance on which autotest will be |
| 69 | installed |
| 70 | |
| 71 | Raises: |
| 72 | AutoservError: if a tarball was not specified and |
| 73 | the target host does not have svn installed in its path |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 74 | |
| 75 | TODO(poirier): check dependencies |
| 76 | autotest needs: |
| 77 | bzcat |
| 78 | liboptdev (oprofile) |
| 79 | binutils-dev (oprofile) |
| 80 | make |
mbligh | 629e39e | 2007-08-10 19:32:00 +0000 | [diff] [blame] | 81 | psutils (netperf) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 82 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 83 | if not host: |
| 84 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 85 | if not self.got: |
| 86 | self.get() |
mbligh | 7386abc | 2007-08-31 08:57:56 +0000 | [diff] [blame] | 87 | host.ensure_up() |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 88 | host.setup() |
mbligh | b84a1cf | 2007-08-09 23:08:13 +0000 | [diff] [blame] | 89 | print "Installing autotest on %s" % host.hostname |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 90 | # try to install from file or directory |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 91 | if self.source_material: |
| 92 | if os.path.isdir(self.source_material): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 93 | # Copy autotest recursively |
| 94 | autodir = _get_autodir(host) |
mbligh | f238b45 | 2007-07-25 21:11:14 +0000 | [diff] [blame] | 95 | host.run('mkdir -p "%s"' % |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 96 | utils.sh_escape(autodir)) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 97 | host.send_file(self.source_material, |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 98 | autodir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 99 | else: |
| 100 | # Copy autotest via tarball |
| 101 | raise "Not yet implemented!" |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 102 | print "Installation of autotest completed" |
| 103 | self.installed = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 104 | return |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 105 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 106 | # if that fails try to install using svn |
| 107 | if utils.run('which svn').exit_status: |
| 108 | raise AutoservError('svn not found in path on \ |
| 109 | target machine: %s' % host.name) |
| 110 | try: |
| 111 | host.run('svn checkout %s %s' % |
| 112 | (AUTOTEST_SVN, _get_autodir(host))) |
| 113 | except errors.AutoservRunError, e: |
| 114 | host.run('svn checkout %s %s' % |
| 115 | (AUTOTEST_HTTP, _get_autodir(host))) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 116 | print "Installation of autotest completed" |
| 117 | self.installed = True |
| 118 | |
| 119 | |
| 120 | def get(self, location = None): |
| 121 | if not location: |
| 122 | location = os.path.join(self.serverdir, '../client') |
| 123 | location = os.path.abspath(location) |
mbligh | 8fc0e5a | 2007-10-11 18:39:03 +0000 | [diff] [blame] | 124 | # If there's stuff run on our client directory already, it |
| 125 | # can cause problems. Try giving it a quick clean first. |
| 126 | cwd = os.getcwd() |
| 127 | os.chdir(location) |
| 128 | os.system('tools/make_clean') |
| 129 | os.chdir(cwd) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 130 | super(Autotest, self).get(location) |
| 131 | self.got = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 132 | |
| 133 | |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 134 | def run(self, control_file, results_dir = '.', host = None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 135 | """ |
| 136 | Run an autotest job on the remote machine. |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 137 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 138 | Args: |
| 139 | control_file: an open file-like-obj of the control file |
| 140 | results_dir: a str path where the results should be stored |
| 141 | on the local filesystem |
| 142 | host: a Host instance on which the control file should |
| 143 | be run |
| 144 | |
| 145 | Raises: |
| 146 | AutotestRunError: if there is a problem executing |
| 147 | the control file |
| 148 | """ |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 149 | results_dir = os.path.abspath(results_dir) |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 150 | if not host: |
| 151 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 152 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 153 | self.install(host) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 154 | |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 155 | host.ensure_up() |
| 156 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 157 | atrun = _Run(host, results_dir) |
mbligh | e4f0151 | 2007-08-10 01:42:50 +0000 | [diff] [blame] | 158 | try: |
| 159 | atrun.verify_machine() |
| 160 | except: |
| 161 | print "Verify machine failed on %s. Reinstalling" % \ |
| 162 | host.hostname |
| 163 | self.install(host) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 164 | atrun.verify_machine() |
| 165 | debug = os.path.join(results_dir, 'debug') |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 166 | try: |
| 167 | os.makedirs(debug) |
| 168 | except: |
| 169 | pass |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 170 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 171 | # Ready .... Aim .... |
| 172 | host.run('rm -f ' + atrun.remote_control_file) |
| 173 | host.run('rm -f ' + atrun.remote_control_file + '.state') |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 174 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 175 | # Copy control_file to remote_control_file on the host |
| 176 | tmppath = utils.get(control_file) |
| 177 | host.send_file(tmppath, atrun.remote_control_file) |
| 178 | os.remove(tmppath) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 179 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 180 | atrun.execute_control() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 181 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 182 | # retrive results |
| 183 | results = os.path.join(atrun.autodir, 'results', 'default') |
| 184 | # Copy all dirs in default to results_dir |
| 185 | host.get_file(results + '/', results_dir) |
| 186 | |
| 187 | |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 188 | def run_test(self, test_name, results_dir = '.', host = None, \ |
| 189 | *args, **dargs): |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 190 | """ |
| 191 | Assemble a tiny little control file to just run one test, |
| 192 | and run it as an autotest client-side test |
| 193 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 194 | if not host: |
| 195 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 196 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 197 | self.install(host) |
mbligh | 271d5af | 2007-08-10 19:54:01 +0000 | [diff] [blame] | 198 | opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()] |
| 199 | cmd = ", ".join([repr(test_name)] + map(repr, args) + opts) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 200 | control = "job.run_test(%s)\n" % cmd |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 201 | self.run(control, results_dir, host) |
| 202 | |
| 203 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 204 | class _Run(object): |
| 205 | """ |
| 206 | Represents a run of autotest control file. This class maintains |
| 207 | all the state necessary as an autotest control file is executed. |
| 208 | |
| 209 | It is not intended to be used directly, rather control files |
| 210 | should be run using the run method in Autotest. |
| 211 | """ |
| 212 | def __init__(self, host, results_dir): |
| 213 | self.host = host |
| 214 | self.results_dir = results_dir |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 215 | self.env = '' |
mbligh | 928c036 | 2007-07-31 17:59:49 +0000 | [diff] [blame] | 216 | if hasattr(host, 'env'): |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 217 | self.env = host.env |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 218 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 219 | self.autodir = _get_autodir(self.host) |
| 220 | self.remote_control_file = os.path.join(self.autodir, 'control') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 221 | |
| 222 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 223 | def verify_machine(self): |
| 224 | binary = os.path.join(self.autodir, 'bin/autotest') |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 225 | try: |
| 226 | self.host.run('ls ' + binary) |
| 227 | except: |
mbligh | 8d7e347 | 2007-08-10 01:35:18 +0000 | [diff] [blame] | 228 | raise "Autotest does not appear to be installed" |
| 229 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 230 | self.host.run('umount %s' % tmpdir, ignore_status=True) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 231 | |
| 232 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 233 | def __execute_section(self, section): |
| 234 | print "Executing %s/bin/autotest %s/control phase %d" % \ |
| 235 | (self.autodir, self.autodir, |
| 236 | section) |
| 237 | logfile = "%s/debug/client.log.%d" % (self.results_dir, |
| 238 | section) |
| 239 | client_log = open(logfile, 'w') |
| 240 | if section > 0: |
| 241 | cont = '-c' |
| 242 | else: |
| 243 | cont = '' |
| 244 | client = os.path.join(self.autodir, 'bin/autotest_client') |
| 245 | ssh = "ssh -q %s@%s" % (self.host.user, self.host.hostname) |
| 246 | cmd = "%s %s %s" % (client, cont, self.remote_control_file) |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 247 | print "%s '%s %s'" % (ssh, self.env, cmd) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 248 | # Use Popen here, not m.ssh, as we want it in the background |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 249 | p = subprocess.Popen("%s '%s %s'" % (ssh, self.env, cmd), |
| 250 | shell=True, |
| 251 | stdout=client_log, |
| 252 | stderr=subprocess.PIPE) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 253 | line = None |
| 254 | for line in iter(p.stderr.readline, ''): |
| 255 | print line, |
| 256 | sys.stdout.flush() |
| 257 | if not line: |
| 258 | raise AutotestRunError("execute_section: %s '%s' \ |
| 259 | failed to return anything" % (ssh, cmd)) |
| 260 | return line |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 261 | |
| 262 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 263 | def execute_control(self): |
| 264 | section = 0 |
| 265 | while True: |
| 266 | last = self.__execute_section(section) |
| 267 | section += 1 |
| 268 | if re.match('DONE', last): |
| 269 | print "Client complete" |
| 270 | return |
| 271 | elif re.match('REBOOT', last): |
| 272 | print "Client is rebooting" |
| 273 | print "Waiting for client to halt" |
| 274 | if not self.host.wait_down(HALT_TIME): |
| 275 | raise AutotestRunError("%s \ |
| 276 | failed to shutdown after %ds" % |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 277 | (self.host.hostname, |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 278 | HALT_TIME)) |
| 279 | print "Client down, waiting for restart" |
| 280 | if not self.host.wait_up(BOOT_TIME): |
| 281 | # since reboot failed |
| 282 | # hardreset the machine once if possible |
| 283 | # before failing this control file |
| 284 | if hasattr(self.host, 'hardreset'): |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 285 | print "Hardresetting %s" % ( |
mbligh | 928c036 | 2007-07-31 17:59:49 +0000 | [diff] [blame] | 286 | self.host.hostname,) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 287 | self.host.hardreset() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 288 | raise AutotestRunError("%s failed to " |
| 289 | "boot after %ds" % ( |
| 290 | self.host.hostname, |
| 291 | BOOT_TIME,)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 292 | continue |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 293 | raise AutotestRunError("Aborting - unknown " |
| 294 | "return code: %s\n" % last) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 295 | |
| 296 | |
| 297 | def _get_autodir(host): |
| 298 | try: |
| 299 | atdir = host.run( |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 300 | 'grep "autodir *=" /etc/autotest.conf').stdout.strip() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 301 | if atdir: |
| 302 | m = re.search(r'autodir *= *[\'"]?([^\'"]*)[\'"]?', |
| 303 | atdir) |
| 304 | return m.group(1) |
| 305 | except errors.AutoservRunError: |
| 306 | pass |
| 307 | for path in ['/usr/local/autotest', '/home/autotest']: |
| 308 | try: |
| 309 | host.run('ls ' + path) |
| 310 | return path |
| 311 | except errors.AutoservRunError: |
| 312 | pass |
| 313 | raise AutotestRunError("Cannot figure out autotest directory") |