3 # adt-run <options>... --- <virt-server> [<virt-server-arg>...]
5 # invoke in toplevel of package (not necessarily built)
6 # with package installed
10 # 4 at least one test failed
11 # 8 no tests in this package
12 # 12 erroneous package
14 # 20 other unexpected failures including bad usage
27 from optparse import OptionParser
32 signal.signal(signal.SIGINT, signal.SIG_DFL) # undo stupid Python SIGINT thing
35 def __init__(q,ec,m): q.ec = ec; q.m = m
37 def bomb(m): raise Quit(20, "unexpected error: %s" % m)
38 def badpkg(m): raise Quit(12, "erroneous package: %s" % m)
39 def report(tname, result): print '%-20s %s' % (tname, result)
42 def __init__(u, lno, m):
43 if lno >= 0: u.m = '%s (control line %d)' % (m, lno)
46 report(tname, 'SKIP %s' % u.m)
50 if not opts.debug: return
51 print >>sys.stderr, 'atd-run: debug:', m
54 return reduce((lambda a,b: a + b), l, [])
57 def __init__(p, tb, path, what, dir=False):
64 bomb("path %s specified as being in testbed but"
65 " not absolute: `%s'" % (what, p.p))
71 if p.dir: p.dirsfx = '/'
75 def append(p, suffix, what, dir=False):
76 return Path(p.tb, p.path() + suffix, what=what, dir=dir)
78 if p.tb: pfx = '/VIRT'
79 elif p.p[:1] == '/': pfx = '/HOST'
83 if p.local is not None: return p.local
85 p.local = tmpdir + '/tb-' + p.what
86 testbed.command('copyup', (p.path(), p.local + p.dirsfx))
89 if p.down is not None: return p.down
90 p.down = testbed.scratch.p + '/host-' + p.what
91 testbed.command('copydown', (p.path(), p.down + p.dirsfx))
96 usage = "%prog <options> -- <virt-server>..."
97 parser = OptionParser(usage=usage)
98 pa = parser.add_option
99 pe = parser.add_option
101 def cb_vserv(op,optstr,value,parser):
102 parser.values.vserver = list(parser.rargs)
105 def cb_path(op,optstr,value,parser, long,tb,dir):
106 name = long.replace('-','_')
107 setattr(parser.values, name, Path(tb, value, long, dir))
109 def pa_path(long, dir, help):
110 def papa_tb(long, ca, pahelp):
111 pa('', long, action='callback', callback=cb_path,
112 nargs=1, type='string', callback_args=ca,
113 help=(help % pahelp), metavar='PATH')
114 papa_tb('--'+long, (long, False, dir), 'host')
115 papa_tb('--'+long+'-tb',(long, True, dir), 'testbed')
117 pa_path('build-tree', True, 'use build tree from PATH on %s')
118 pa_path('control', False, 'read control file PATH on %s')
120 pa('-d', '--debug', action='store_true', dest='debug');
121 pa('','--user', type='string',
122 help='run tests as USER (needs root on testbed)')
124 class SpecialOption(optparse.Option): pass
125 vs_op = SpecialOption('','--VSERVER-DUMMY')
126 vs_op.action = 'callback'
130 vs_op.callback = cb_vserv
131 vs_op.callback_args = ( )
132 vs_op.callback_kwargs = { }
133 vs_op.help = 'introduces virtualisation server and args'
134 vs_op._short_opts = []
135 #vs_op._long_opts = ['--DUMMY']
136 vs_op._long_opts = ['---']
140 (opts,args) = parser.parse_args()
141 if not hasattr(opts,'vserver'):
142 parser.error('you must specifiy --- <virt-server>...')
144 if opts.build_tree is None:
145 opts.build_tree = Path(False, '.', 'build-tree', dir=True)
146 if opts.control is None:
147 opts.control = opts.build_tree.append(
148 'debian/tests/control', 'control')
157 tb.sp = subprocess.Popen(opts.vserver,
158 stdin=p, stdout=p, stderr=None)
162 if tb.sp is None: return
163 ec = tb.sp.returncode
170 tb.bomb('testbed gave exit status %d after quit' % ec)
172 if tb.scratch is not None: return
173 p = tb.commandr1('open')
174 tb.scratch = Path(True, p, 'tb-scratch', dir=True)
176 if tb.scratch is None: return
180 if tb.sp is not None:
184 if ec: print >>sys.stderr, ('adt-run: testbed failing,'
185 ' exit status %d' % ec)
187 raise Quit(16, 'testbed failed: %s' % m)
188 def send(tb, string):
191 print >>tb.sp.stdin, string
195 tb.bomb('cannot send to testbed: %s' %
196 formatexception_only(sys.last_type, sys.last_value))
197 def expect(tb, keyword, nresults=-1):
198 l = tb.sp.stdout.readline()
199 if not l: tb.bomb('unexpected eof from the testbed')
200 if not l.endswith('\n'): tb.bomb('unterminated line from the testbed')
204 if not ll: tb.bomb('unexpected whitespace-only line from the testbed')
206 if tb.lastsend is None:
207 tb.bomb("got banner `%s', expected `%s...'" %
210 tb.bomb("sent `%s', got `%s', expected `%s...'" %
211 (tb.lastsend, l, keyword))
213 if nresults >= 0 and len(ll) != nresults:
214 tb.bomb("sent `%s', got `%s' (%d result parameters),"
215 " expected %d result parameters" %
216 (string, l, len(ll), nresults))
218 def commandr(tb, cmd, nresults, args=()):
219 al = [cmd] + map(urllib.quote, args)
220 tb.send(string.join(al))
222 rl = map(urllib.unquote, ll)
224 def command(tb, cmd, args=()):
225 tb.commandr(cmd, 0, args)
226 def commandr1(tb, cmd, args=()):
227 rl = tb.commandr(cmd, 1, args)
231 def __init__(f, fname, stz, base, tnames, vl):
241 r = map((lambda w: (lno, w)), r)
243 return flatten(map(distribute, f.vl))
248 raise Unsupported(f.vl[1][0],
249 'only one %s field allowed' % fn)
252 class FieldIgnore(FieldBase):
256 def __init__(r,rname,base): pass
258 class Restriction_rw_build_tree(Restriction): pass
260 class Field_Restrictions(FieldBase):
262 for wle in f.words():
264 rname = rname.replace('-','_')
265 try: rclass = globals()['Restriction_'+rname]
266 except KeyError: raise Unsupported(lno,
267 'unknown restriction %s' % rname)
268 r = rclass(rname, f.base)
269 f.base['restrictions'].append(r)
271 class Field_Tests(FieldIgnore): pass
273 class Field_Tests_directory(FieldBase):
276 if td.startswith('/'): raise Unspported(f.lno,
277 'Tests-Directory may not be absolute')
278 base['testsdir'] = td
286 def __init__(t, tname, base):
287 if '/' in tname: raise Unsupported(base[' lno'],
288 'test name may not contain / character')
289 for k in base: setattr(t,k,base[k])
291 t.p = opts.build_tree.append(tname, 'test-'+tname)
297 so = testbed.scratch.append('stdout-' + t.tname, 'stdout-' + t.tname)
298 se = testbed.scratch.append('stdout-' + t.tname, 'stdout-' + t.tname)
299 rc = testbed.commandr1('execute',(t.p.ontb(),
300 '/dev/null', so.ontb(), se.ontb()))
306 if stab.st_size != 0:
307 l = file(seh).readline()
308 l = l.rstrip('\n \t\r')
309 if len(l) > 40: l = l[:40] + '...'
310 t.report('FAIL stderr: %s' % l)
312 t.report('FAIL non-zero exit status %d' % rc)
318 control = file(opts.control.onhost(), 'r')
320 def badctrl(m): testbed.badpkg('tests/control line %d: %s' % (lno, m))
321 stz = None # stz[field_name][index] = (lno, value)
327 if stz is None: return
333 initre = regexp.compile('([A-Z][-0-9a-z]*)\s*\:\s*(.*)$')
335 l = control.readline()
338 if not l.endswith('\n'): badctrl('unterminated line')
339 if regexp.compile('\s*\#').match(l): continue
340 if not regexp.compile('\S').match(l): end_stanza(stz); continue
341 initmat = initre.match(l)
343 (fname, l) = initmat.groups()
344 fname = string.capwords(fname)
346 stz = { ' lno': lno }
347 if not stz.has_key(fname): stz[fname] = [ ]
348 hcurrent = stz[fname]
349 elif regexp.compile('\s').match(l):
350 if not hcurrent: badctrl('unexpected continuation')
352 badctrl('syntax error')
353 hcurrent.append((lno, l))
356 def testbadctrl(stz, lno, m):
357 report_badctrl(lno, m)
362 try: tnames = stz['Tests']
365 raise Unsupported(stz[' lno'],
367 tnames = map((lambda lt: lt[1]), tnames)
368 tnames = string.join(tnames).split()
371 'testsdir': 'debian/tests'
373 for fname in stz.keys():
374 if fname.startswith(' '): continue
376 try: fclass = globals()['Field_'+
377 fname.replace('-','_')]
378 except KeyError: raise Unsupported(vl[0][0],
379 'unknown metadata field %s' % fname)
380 f = fclass(stz, fname, base, tnames, vl)
384 t = Test(tname, base)
386 except Unsupported, u:
387 for tname in tnames: u.report(tname)
390 def print_exception(ei, msgprefix=''):
391 if msgprefix: print >>sys.stderr, msgprefix
394 print >>sys.stderr, 'adt-run:', q.m
397 print >>sys.stderr, "adt-run: unexpected, exceptional, error:"
398 traceback.print_exc()
404 if tmpdir is not None:
405 rm_ec = subprocess.call(['rm','-rf','--',tmpdir])
406 if testbed is not None:
408 if rm_ec: bomb('rm -rf -- %s failed, code %d' % (tmpdir, ec))
410 print_exception(sys.exc_info(),
411 '\nadt-run: error cleaning up:\n')
419 tmpdir = tempfile.mkdtemp()
425 ec = print_exception(sys.exc_info(), '')