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 | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 55 | super(Autotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 56 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 57 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 58 | def install(self, host = None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 59 | """ |
| 60 | Install autotest. If get() was not called previously, an |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 61 | attempt will be made to install from the autotest svn |
| 62 | repository. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 63 | |
| 64 | Args: |
| 65 | host: a Host instance on which autotest will be |
| 66 | installed |
| 67 | |
| 68 | Raises: |
| 69 | AutoservError: if a tarball was not specified and |
| 70 | the target host does not have svn installed in its path |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 71 | |
| 72 | TODO(poirier): check dependencies |
| 73 | autotest needs: |
| 74 | bzcat |
| 75 | liboptdev (oprofile) |
| 76 | binutils-dev (oprofile) |
| 77 | make |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 78 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 79 | if not host: |
| 80 | host = self.host |
mbligh | b84a1cf | 2007-08-09 23:08:13 +0000 | [diff] [blame] | 81 | print "Installing autotest on %s" % host.hostname |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 82 | # try to install from file or directory |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 83 | if self.source_material: |
| 84 | if os.path.isdir(self.source_material): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 85 | # Copy autotest recursively |
| 86 | autodir = _get_autodir(host) |
mbligh | f238b45 | 2007-07-25 21:11:14 +0000 | [diff] [blame] | 87 | host.run('mkdir -p "%s"' % |
| 88 | utils.sh_escape(autodir)) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 89 | host.send_file(self.source_material, |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 90 | autodir) |
| 91 | else: |
| 92 | # Copy autotest via tarball |
| 93 | raise "Not yet implemented!" |
| 94 | return |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 95 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 96 | # if that fails try to install using svn |
| 97 | if utils.run('which svn').exit_status: |
| 98 | raise AutoservError('svn not found in path on \ |
| 99 | target machine: %s' % host.name) |
| 100 | try: |
| 101 | host.run('svn checkout %s %s' % |
| 102 | (AUTOTEST_SVN, _get_autodir(host))) |
| 103 | except errors.AutoservRunError, e: |
| 104 | host.run('svn checkout %s %s' % |
| 105 | (AUTOTEST_HTTP, _get_autodir(host))) |
| 106 | |
| 107 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 108 | def run(self, control_file, results_dir, host = None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 109 | """ |
| 110 | Run an autotest job on the remote machine. |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 111 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 112 | Args: |
| 113 | control_file: an open file-like-obj of the control file |
| 114 | results_dir: a str path where the results should be stored |
| 115 | on the local filesystem |
| 116 | host: a Host instance on which the control file should |
| 117 | be run |
| 118 | |
| 119 | Raises: |
| 120 | AutotestRunError: if there is a problem executing |
| 121 | the control file |
| 122 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 123 | if not host: |
| 124 | host = self.host |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 125 | host.ensure_up() |
| 126 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 127 | atrun = _Run(host, results_dir) |
| 128 | atrun.verify_machine() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 129 | if os.path.isdir(results_dir): |
| 130 | shutil.rmtree(results_dir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 131 | debug = os.path.join(results_dir, 'debug') |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 132 | os.makedirs(debug) |
| 133 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 134 | # Ready .... Aim .... |
| 135 | host.run('rm -f ' + atrun.remote_control_file) |
| 136 | host.run('rm -f ' + atrun.remote_control_file + '.state') |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 137 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 138 | # Copy control_file to remote_control_file on the host |
| 139 | tmppath = utils.get(control_file) |
| 140 | host.send_file(tmppath, atrun.remote_control_file) |
| 141 | os.remove(tmppath) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 142 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 143 | atrun.execute_control() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 144 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 145 | # retrive results |
| 146 | results = os.path.join(atrun.autodir, 'results', 'default') |
| 147 | # Copy all dirs in default to results_dir |
| 148 | host.get_file(results + '/', results_dir) |
| 149 | |
| 150 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 151 | def run_test(self, test_name, results_dir, host = None, **options): |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 152 | """ |
| 153 | Assemble a tiny little control file to just run one test, |
| 154 | and run it as an autotest client-side test |
| 155 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 156 | if not host: |
| 157 | host = self.host |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 158 | args = ["%s=%s" % (o[0], repr(o[1])) for o in options.items()] |
| 159 | args = ", ".join([repr(test_name)] + args) |
| 160 | control = "job.run_test(%s)" % args |
| 161 | self.run(control, results_dir, host) |
| 162 | |
| 163 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 164 | class _Run(object): |
| 165 | """ |
| 166 | Represents a run of autotest control file. This class maintains |
| 167 | all the state necessary as an autotest control file is executed. |
| 168 | |
| 169 | It is not intended to be used directly, rather control files |
| 170 | should be run using the run method in Autotest. |
| 171 | """ |
| 172 | def __init__(self, host, results_dir): |
| 173 | self.host = host |
| 174 | self.results_dir = results_dir |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 175 | self.env = '' |
mbligh | 928c036 | 2007-07-31 17:59:49 +0000 | [diff] [blame] | 176 | if hasattr(host, 'env'): |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 177 | self.env = host.env |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 178 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 179 | self.autodir = _get_autodir(self.host) |
| 180 | self.remote_control_file = os.path.join(self.autodir, 'control') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 181 | |
| 182 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 183 | def verify_machine(self): |
| 184 | binary = os.path.join(self.autodir, 'bin/autotest') |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame^] | 185 | try: |
| 186 | self.host.run('ls ' + binary) |
| 187 | except: |
| 188 | self. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 189 | |
| 190 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 191 | def __execute_section(self, section): |
| 192 | print "Executing %s/bin/autotest %s/control phase %d" % \ |
| 193 | (self.autodir, self.autodir, |
| 194 | section) |
| 195 | logfile = "%s/debug/client.log.%d" % (self.results_dir, |
| 196 | section) |
| 197 | client_log = open(logfile, 'w') |
| 198 | if section > 0: |
| 199 | cont = '-c' |
| 200 | else: |
| 201 | cont = '' |
| 202 | client = os.path.join(self.autodir, 'bin/autotest_client') |
| 203 | ssh = "ssh -q %s@%s" % (self.host.user, self.host.hostname) |
| 204 | cmd = "%s %s %s" % (client, cont, self.remote_control_file) |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 205 | print "%s '%s %s'" % (ssh, self.env, cmd) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 206 | # Use Popen here, not m.ssh, as we want it in the background |
mbligh | 3ceb720 | 2007-07-27 21:10:26 +0000 | [diff] [blame] | 207 | p = subprocess.Popen("%s '%s %s'" % (ssh, self.env, cmd), |
| 208 | shell=True, |
| 209 | stdout=client_log, |
| 210 | stderr=subprocess.PIPE) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 211 | line = None |
| 212 | for line in iter(p.stderr.readline, ''): |
| 213 | print line, |
| 214 | sys.stdout.flush() |
| 215 | if not line: |
| 216 | raise AutotestRunError("execute_section: %s '%s' \ |
| 217 | failed to return anything" % (ssh, cmd)) |
| 218 | return line |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 219 | |
| 220 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 221 | def execute_control(self): |
| 222 | section = 0 |
| 223 | while True: |
| 224 | last = self.__execute_section(section) |
| 225 | section += 1 |
| 226 | if re.match('DONE', last): |
| 227 | print "Client complete" |
| 228 | return |
| 229 | elif re.match('REBOOT', last): |
| 230 | print "Client is rebooting" |
| 231 | print "Waiting for client to halt" |
| 232 | if not self.host.wait_down(HALT_TIME): |
| 233 | raise AutotestRunError("%s \ |
| 234 | failed to shutdown after %ds" % |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 235 | (self.host.hostname, |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 236 | HALT_TIME)) |
| 237 | print "Client down, waiting for restart" |
| 238 | if not self.host.wait_up(BOOT_TIME): |
| 239 | # since reboot failed |
| 240 | # hardreset the machine once if possible |
| 241 | # before failing this control file |
| 242 | if hasattr(self.host, 'hardreset'): |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 243 | print "Hardresetting %s" % ( |
mbligh | 928c036 | 2007-07-31 17:59:49 +0000 | [diff] [blame] | 244 | self.host.hostname,) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 245 | self.host.hardreset() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 246 | raise AutotestRunError("%s failed to " |
| 247 | "boot after %ds" % ( |
| 248 | self.host.hostname, |
| 249 | BOOT_TIME,)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 250 | continue |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 251 | raise AutotestRunError("Aborting - unknown " |
| 252 | "return code: %s\n" % last) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | def _get_autodir(host): |
| 256 | try: |
| 257 | atdir = host.run( |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 258 | 'grep "autodir *=" /etc/autotest.conf').stdout.strip() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 259 | if atdir: |
| 260 | m = re.search(r'autodir *= *[\'"]?([^\'"]*)[\'"]?', |
| 261 | atdir) |
| 262 | return m.group(1) |
| 263 | except errors.AutoservRunError: |
| 264 | pass |
| 265 | for path in ['/usr/local/autotest', '/home/autotest']: |
| 266 | try: |
| 267 | host.run('ls ' + path) |
| 268 | return path |
| 269 | except errors.AutoservRunError: |
| 270 | pass |
| 271 | raise AutotestRunError("Cannot figure out autotest directory") |