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
25 from optparse import OptionParser
30 signal.signal(signal.SIGINT, signal.SIG_DFL) # undo stupid Python SIGINT thing
33 def __init__(q,ec,m): q.ec = ec; q.m = m
35 def bomb(m): raise Quit(20, "unexpected error: %s" % m)
36 def badpkg(m): raise Quit(12, "erroneous package: %s" % m)
40 if not opts.debug: return
41 print >>sys.stderr, 'atd-run: debug:', m
44 def __init__(p, tb, path, what, dir=False):
51 bomb("path %s specified as being in testbed but"
52 " not absolute: `%s'" % (what, p.p))
56 if p.dir: p.dirsfx = '/'
60 def append(p, suffix, what, dir=False):
61 return Path(p.tb, p.path() + suffix, what=what, dir=dir)
63 if p.tb: pfx = '/VIRT'
64 elif p.p[:1] == '/': pfx = '/HOST'
68 if not p.tb: return p.p
69 if p.local is not None: return p.local
71 p.local = tmpdir + '/tb.' + p.what
72 testbed.command('copyup', (p.path(), p.local + p.dirsfx))
77 usage = "%prog <options> -- <virt-server>..."
78 parser = OptionParser(usage=usage)
79 pa = parser.add_option
80 pe = parser.add_option
82 def cb_vserv(op,optstr,value,parser):
83 parser.values.vserver = list(parser.rargs)
86 def cb_path(op,optstr,value,parser, long,tb,dir):
87 name = long.replace('-','_')
88 parser.values.__dict__[name] = Path(tb, value, long, dir)
90 def pa_path(long, dir, help):
91 def papa_tb(long, ca, pahelp):
92 pa('', long, action='callback', callback=cb_path,
93 nargs=1, type='string', callback_args=ca,
94 help=(help % pahelp), metavar='PATH')
95 papa_tb('--'+long, (long, False, dir), 'host')
96 papa_tb('--'+long+'-tb',(long, True, dir), 'testbed')
98 pa_path('build-tree', True, 'use build tree from PATH on %s')
99 pa_path('control', False, 'read control file PATH on %s')
101 pa('-d', '--debug', action='store_true', dest='debug');
102 pa('','--user', type='string',
103 help='run tests as USER (needs root on testbed)')
105 class SpecialOption(optparse.Option): pass
106 vs_op = SpecialOption('','--VSERVER-DUMMY')
107 vs_op.action = 'callback'
111 vs_op.callback = cb_vserv
112 vs_op.callback_args = ( )
113 vs_op.callback_kwargs = { }
114 vs_op.help = 'introduces virtualisation server and args'
115 vs_op._short_opts = []
116 #vs_op._long_opts = ['--DUMMY']
117 vs_op._long_opts = ['---']
121 (opts,args) = parser.parse_args()
122 if not hasattr(opts,'vserver'):
123 parser.error('you must specifiy --- <virt-server>...')
125 if opts.build_tree is None:
126 opts.build_tree = Path(False, '.', 'build-tree', dir=True)
127 if opts.control is None:
128 opts.control = opts.build_tree.append(
129 'debian/tests/control', 'control')
138 tb.sp = subprocess.Popen(opts.vserver,
139 stdin=p, stdout=p, stderr=None)
143 if tb.sp is None: return
144 ec = tb.sp.returncode
151 tb.bomb('testbed gave exit status %d after quit' % ec)
153 if tb.scratch is not None: return
154 p = tb.commandr1('open')
155 tb.scratch = Path(True, p, 'tb-scratch', dir=True)
157 if tb.scratch is None: return
161 if tb.sp is not None:
165 if ec: print >>sys.stderr, ('adt-run: testbed failing,'
166 ' exit status %d' % ec)
168 raise Quit(16, 'testbed failed: %s' % m)
169 def send(tb, string):
172 print >>tb.sp.stdin, string
176 tb.bomb('cannot send to testbed: %s' %
177 formatexception_only(sys.last_type, sys.last_value))
178 def expect(tb, keyword, nresults=-1):
179 l = tb.sp.stdout.readline()
180 if not l: tb.bomb('unexpected eof from the testbed')
181 if not l.endswith('\n'): tb.bomb('unterminated line from the testbed')
185 if not ll: tb.bomb('unexpected whitespace-only line from the testbed')
187 if tb.lastsend is None:
188 tb.bomb("got banner `%s', expected `%s...'" %
191 tb.bomb("sent `%s', got `%s', expected `%s...'" %
192 (tb.lastsend, l, keyword))
194 if nresults >= 0 and len(ll) != nresults:
195 tb.bomb("sent `%s', got `%s' (%d result parameters),"
196 " expected %d result parameters" %
197 (string, l, len(ll), nresults))
199 def commandr(tb, cmd, nresults, args=()):
200 al = [cmd] + map(urllib.quote, args)
201 tb.send(string.join(al))
203 rl = map(urllib.unquote, ll)
205 def command(tb, cmd, args=()):
206 tb.commandr(cmd, 0, args)
207 def commandr1(tb, cmd, args=()):
208 rl = tb.commandr(cmd, 1, args)
212 control = file(opts.control.onhost(), 'r')
215 def print_exception(ei, msgprefix=''):
216 if msgprefix: print >>sys.stderr, msgprefix
219 print >>sys.stderr, 'adt-run:', q.m
222 print >>sys.stderr, "adt-run: unexpected, exceptional, error:"
223 traceback.print_exc()
229 if tmpdir is not None:
230 rm_ec = subprocess.call(['rm','-rf','--',tmpdir])
231 if testbed is not None:
233 if rm_ec: bomb('rm -rf -- %s failed, code %d' % (tmpdir, ec))
235 print_exception(sys.exc_info(),
236 '\nadt-run: error cleaning up:\n')
244 tmpdir = tempfile.mkdtemp()
249 ec = print_exception(sys.exc_info(), '')