chiark / gitweb /
packaging fixes; include a-t-o manpage; etc.
[autopkgtest.git] / runner / adt-run
index b6a37fd5edc138a6a918dffd1046387046eb6a7b..8873757bb00bbf3336b50896b11bb2761dd9a384 100755 (executable)
@@ -46,6 +46,7 @@ tmpdir = None         # pathstring on host
 testbed = None         # Testbed
 errorcode = 0          # exit status that we are going to use
 binaries = None                # Binaries (.debs we have registered)
+build_essential = ["build-essential"]
 
 #---------- output handling
 #
@@ -73,30 +74,40 @@ binaries = None             # Binaries (.debs we have registered)
 # is done by forking off a copy of ourselves to do plumbing,
 # which copy we wait for at the appropriate point.
 
+class DummyOpts:
+ def __init__(do): do.debuglevel = 0
+
+opts = DummyOpts()
 trace_stream = None
+summary_stream = None
 
 def pstderr(m):
        print >>sys.stderr, m
        if trace_stream is not None: print >>trace_stream, m
 
-def debug(m):
-       if not opts.debug and trace_stream is None: return
+def debug(m, minlevel=0):
+       if opts.debuglevel < minlevel: return
+       if opts.quiet and trace_stream is None: return
+       p = 'adt-run: debug'
+       if minlevel: p += `minlevel`
+       p += ': '
        for l in m.rstrip('\n').split('\n'):
-               s = 'atd-run: debug: ' + l
-               if opts.debug: print >>sys.stderr, s
+               s = p + l
+               if not opts.quiet: print >>sys.stderr, s
                if trace_stream is not None: print >>trace_stream, s
 
-def debug_file(hp):
+def debug_file(hp, minlevel=0):
+       if opts.debuglevel < minlevel: return
        def do_copy(stream, what):
                rc = subprocess.call(['cat',hp], stdout=stream)
                if rc: bomb('cat failed copying data from %s'
                            ' to %s, exit code %d' % (hp, what, rc))
-       if opts.debug: do_copy(sys.stderr, 'stderr')
+       if not opts.quiet: do_copy(sys.stderr, 'stderr')
        if trace_stream is not None: do_copy(trace_stream, 'trace log')
 
 class Errplumb:
  def __init__(ep, critical=False):
-       to_stderr = critical or opts.debug
+       to_stderr = critical or not opts.quiet
        count = to_stderr + (trace_stream is not None)
        if count == 0:
                ep.stream = open('/dev/null','w')
@@ -106,7 +117,7 @@ class Errplumb:
                else: ep.stream = trace_stream
                ep._sp = None
        else:
-               ep._sp = subprocess.Popen(['tee','/dev/stderr'],
+               ep._sp = subprocess.Popen(['tee','-a','/dev/stderr'],
                        stdin=subprocess.PIPE, stdout=trace_stream,
                        close_fds=True)
                ep.stream = ep._sp.stdin
@@ -129,9 +140,14 @@ def subprocess_cooked(cmdl, critical=False, dbg=None, **kwargs):
        ep.wait()
        return (rc, output)
 
+def psummary(m):
+       if summary_stream is not None: print >>summary_stream, m
+
 def preport(m):
        print m
+       sys.stdout.flush()
        if trace_stream is not None: print >>trace_stream, m
+       psummary(m)
 
 def report(tname, result):
        preport('%-20s %s' % (tname, result))
@@ -167,7 +183,7 @@ def mkdir_okexist(pathname, mode=02755):
                if oe.errno != errno.EEXIST: raise
 
 def rmtree(what, pathname):
-       debug('/ %s rmtree %s' % (what, pathname))
+       debug('/ %s rmtree %s' % (what, pathname), 2)
        shutil.rmtree(pathname)
 
 def debug_subprocess(what, cmdl=None, script=None):
@@ -179,11 +195,12 @@ def debug_subprocess(what, cmdl=None, script=None):
                        ol.append(x.    replace('\\','\\\\').
                                        replace(' ','\\ ')      )
                o += ' '+ ' '.join(ol)
-       if script is not None:
-               o += '\n'
+       debug(o)
+       if script is not None and opts.debuglevel >= 1:
+               o = ''
                for l in script.rstrip('\n').split('\n'):
                        o += '$     '+l+'\n'
-       debug(o)
+               debug(o,1)
 
 def flatten(l):
        return reduce((lambda a,b: a + b), l, []) 
@@ -214,7 +231,8 @@ class AutoFile:
                else: return p.path[tbp]+p.dir+'!'
        out = p.what
        if p.spec is not None:
-               if p.spec_tbp: out += '#'
+               if not hasattr(p,'spec_tb'): out += '~'
+               elif p.spec_tbp: out += '#'
                else: out += '='
                out += p.spec
        out += ':'
@@ -225,7 +243,8 @@ class AutoFile:
 
  def _wrong(p, how):
        xtra = ''
-       if p.spec is not None: xtra = ' spec[%s]=%s' % (p.spec, p.spec_tb)
+       if p.spec is not None:
+               xtra = ' spec[%s]=%s' % (p.spec, getattr(p,'spec_tb',None))
        raise ("internal error: %s (%s)" % (how, str(p)))
 
  def _ensure_path(p, tbp):
@@ -278,7 +297,7 @@ class AutoFile:
        p._debug('invalidated %s' % 'HT'[tbp])
 
  def _debug(p, m):
-       debug('/ %s#%x: %s' % (p.what, id(p), m))
+       debug('/ %s#%x: %s' % (p.what, id(p), m), 3)
 
  def _constructed(p):
        p._debug('constructed: '+str(p))
@@ -378,11 +397,13 @@ class Action:
        a.af = af
        a.ah = arghandling
        a.what = what
+       a.missing_tests_control = False
  def __repr__(a):
        return "<Action %s %s %s>" % (a.kind, a.what, `a.af`)
 
 def parse_args():
        global opts
+       global n_non_actions # argh, stupid python scoping rules
        usage = "%prog <options> --- <virt-server>..."
        parser = OptionParser(usage=usage)
        pa = parser.add_option
@@ -403,6 +424,7 @@ def parse_args():
        # actions (ie, test sets to run, sources to build, binaries to use):
 
        def cb_action(op,optstr,value,parser, long,kindpath,is_act):
+               global n_non_actions
                parser.largs.append((value,kindpath))
                n_non_actions += not(is_act)
 
@@ -494,7 +516,7 @@ def parse_args():
 
        def cb_path(op,optstr,value,parser, constructor,long,dir):
                name = long.replace('-','_')
-               af = constructor(arghandling['tb'], value, long, dir)
+               af = constructor(long, value,arghandling['tb'])
                setattr(parser.values, name, af)
 
        def pa_path(long, constructor, help, dir=False):
@@ -512,12 +534,16 @@ def parse_args():
        pa('','--log-file',             type='string', dest='logfile',
                help='write the log LOGFILE, emptying it beforehand,'
                     ' instead of using OUTPUT-DIR/log or TMPDIR/log')
+       pa('','--summary-file',         type='string', dest='summary',
+               help='write a summary report to SUMMARY,'
+                    ' emptying it beforehand')
 
        pa('','--user',                 type='string', dest='user',
                help='run tests as USER (needs root on testbed)')
        pa('','--gain-root',            type='string', dest='gainroot',
                help='prefix debian/rules binary with GAINROOT')
-       pa('-q', '--quiet', action='store_false', dest='debug', default=True);
+       pa('-q', '--quiet', action='store_false', dest='quiet', default=False);
+       pa('-d', '--debug', action='count', dest='debuglevel', default=0);
        pa('','--gnupg-home',           type='string', dest='gnupghome',
                default='~/.autopkgtest/gpg',
                help='use GNUPGHOME rather than ~/.autopkgtest (for'
@@ -586,7 +612,7 @@ def parse_args():
                opts.actions.append(Action(kind, af, arghandling, what))
 
 def setup_trace():
-       global trace_stream, tmpdir
+       global trace_stream, tmpdir, summary_stream
 
        if opts.tmpdir is not None:
                rmtree('tmpdir(specified)',opts.tmpdir)
@@ -603,11 +629,13 @@ def setup_trace():
                        opts.logfile = opts.tmpdir + '/log'
        if opts.logfile is not None:
                trace_stream = open(opts.logfile, 'w', 0)
+       if opts.summary is not None:
+               summary_stream = open(opts.summary, 'w', 0)
 
-       debug('#options: '+`opts`)
+       debug('options: '+`opts`, 1)
 
 def finalise_options():
-       global opts, tb
+       global opts, tb, build_essential
 
        if opts.user is None and 'root-on-testbed' not in testbed.caps:
                opts.user = ''
@@ -638,6 +666,7 @@ def finalise_options():
                if (opts.user or
                    'root-on-testbed' not in testbed.caps):
                        opts.gainroot = 'fakeroot'
+                       build_essential += ['fakeroot']
 
        if opts.gnupghome.startswith('~/'):
                try: home = os.environ['HOME']
@@ -661,8 +690,8 @@ class Testbed:
        tb._ephemeral = []
        tb._debug('init')
        tb._need_reset_apt = False
- def _debug(tb, m):
-       debug('** '+m)
+ def _debug(tb, m, minlevel=0):
+       debug('** '+m, minlevel)
  def start(tb):
        tb._debug('start')
        p = subprocess.PIPE
@@ -709,22 +738,28 @@ class Testbed:
        tb.scratch = None
        if tb.sp is None: return
        tb.command('close')
- def prepare(tb, deps_new):
-       tb._debug('prepare, modified=%s, deps_processed=%s, deps_new=%s' %
-               (tb.modified, tb.deps_processed, deps_new))
+ def prepare1(tb, deps_new):
+       tb._debug('prepare1, modified=%s, deps_processed=%s, deps_new=%s' %
+               (tb.modified, tb.deps_processed, deps_new), 1)
        if 'revert' in tb.caps and (tb.modified or
            [d for d in tb.deps_processed if d not in deps_new]):
+               for af in tb._ephemeral: af.read(False)
                tb._debug('reset **')
                tb.command('revert')
                tb.blamed = []
                for af in tb._ephemeral: af.invalidate(True)
-       binaries.publish()
        tb.modified = False
+ def prepare2(tb, deps_new):
+       tb._debug('prepare2, deps_new=%s' % deps_new, 1)
+       binaries.publish()
        tb._install_deps(deps_new)
+ def prepare(tb, deps_new):
+       tb.prepare1(deps_new)
+       tb.prepare2(deps_new)
  def register_ephemeral(tb, af):
-       if not getattr(af,'spec_tbp',False): tb._ephemeral.append(af)
+       tb._ephemeral.append(af)
  def _install_deps(tb, deps_new):
-       tb._debug(' installing dependencies '+`deps_new`)
+       tb._debug(' installing dependencies '+`deps_new`, 1)
        tb.deps_processed = deps_new
        if not deps_new: return
        dstr = ', '.join(deps_new)
@@ -736,13 +771,13 @@ class Testbed:
                ]])
        cmdl = ['python','-c',script]
        what = 'install-deps'
-       rc = testbed.execute(what, cmdl, script=script)
+       rc = testbed.execute(what+'-debinstall', cmdl, script=script)
        if rc: badpkg('dependency install failed, exit code %d' % rc)
  def needs_reset(tb):
-       tb._debug('needs_reset, previously=%s' % tb.modified)
+       tb._debug('needs_reset, previously=%s' % tb.modified, 1)
        tb.modified = True
  def blame(tb, m):
-       tb._debug('blame += %s' % m)
+       tb._debug('blame += %s' % m, 1)
        tb.blamed.append(m)
  def bomb(tb, m):
        tb._debug('bomb %s' % m)
@@ -757,7 +792,7 @@ class Testbed:
  def send(tb, string):
        tb.sp.stdin
        try:
-               debug('>> '+string)
+               debug('>> '+string, 2)
                print >>tb.sp.stdin, string
                tb.sp.stdin.flush()
                tb.lastsend = string
@@ -770,7 +805,7 @@ class Testbed:
        if not l: tb.bomb('unexpected eof from the testbed')
        if not l.endswith('\n'): tb.bomb('unterminated line from the testbed')
        l = l.rstrip('\n')
-       debug('<< '+l)
+       debug('<< '+l, 2)
        ll = l.split()
        if not ll: tb.bomb('unexpected whitespace-only line from the testbed')
        if ll[0] != keyword:
@@ -803,7 +838,7 @@ class Testbed:
        return rl[0]
  def execute(tb, what, cmdl,
                si='/dev/null', so='/dev/null', se=None, cwd=None,
-               script=False):
+               script=False, tmpdir=None):
        # Options for script:
        #   False - do not call debug_subprocess, no synch. reporting required
        #   None or string - call debug_subprocess with that value,
@@ -820,7 +855,7 @@ class Testbed:
                ep = Errplumb()
                se_catch = TemporaryFile(what+'-xerr')
                se_use = se_catch.write(True)
-               if opts.debug: xdump = 'debug=2-2'
+               if not opts.quiet: xdump = 'debug=2-2'
                elif trace_stream is not None:
                        xdump = 'debug=2-%d' % trace_stream.fileno()
        else:
@@ -831,7 +866,10 @@ class Testbed:
        cmdl = [None,
                ','.join(map(urllib.quote, cmdl)),
                si, so, se_use, cwd]
+
        if xdump is not None and 'execute-debug' in tb.caps: cmdl += [xdump]
+       if tmpdir is not None: cmdl.append('env=TMPDIR=%s' % tmpdir)
+
        rc = tb.commandr1('execute', cmdl)
        try: rc = int(rc)
        except ValueError: bomb("execute for %s gave invalid response `%s'"
@@ -875,7 +913,6 @@ class Restriction:
  def __init__(r,rname,base): pass
 
 class Restriction_rw_build_tree(Restriction): pass
-class Restriction_rw_tests_tree(Restriction): pass
 class Restriction_breaks_testbed(Restriction):
  def __init__(r, rname, base):
        if 'revert' not in testbed.caps:
@@ -896,8 +933,20 @@ class Field_Restrictions(FieldBase):
                except KeyError: raise Unsupported(lno,
                        'unknown restriction %s' % rname)
                r = rclass(nrname, f.base)
+               f.base['restriction_names'].append(rname)
                f.base['restrictions'].append(r)
 
+class Field_Features(FieldIgnore):
+ def parse(f):
+       for wle in f.words():
+               (lno, fname) = wle
+               f.base['feature_names'].append(fname)
+               nfname = fname.replace('-','_')
+               try: fclass = globals()['Feature_'+nfname]
+               except KeyError: continue
+               ft = fclass(nfname, f.base)
+               f.base['features'].append(ft)
+
 class Field_Tests(FieldIgnore): pass
 
 class Field_Depends(FieldBase):
@@ -905,7 +954,7 @@ class Field_Depends(FieldBase):
        print >>sys.stderr, "Field_Depends:", `f.stz`, `f.base`, `f.tnames`, `f.vl`
        dl = map(lambda x: x.strip(),
                flatten(map(lambda (lno, v): v.split(','), f.vl)))
-       re = regexp.compile('[^-.+:~0-9a-z()<>=*]')
+       re = regexp.compile('[^-.+:~0-9a-z()<>=*@]')
        for d in dl:
                if re.search(d):
                        badpkg("Test Depends field contains dependency"
@@ -921,15 +970,18 @@ class Field_Tests_directory(FieldBase):
 
 def run_tests(stanzas, tree):
        global errorcode, testbed
+       if stanzas == ():
+               report('*', 'SKIP no tests in this package')
+               errorcode |= 8
        for stanza in stanzas:
                tests = stanza[' tests']
                if not tests:
-                       report('*', 'SKIP no tests in this package')
+                       report('*', 'SKIP package has metadata but no tests')
                        errorcode |= 8
                for t in tests:
                        t.prepare()
                        t.run(tree)
-                       if 'breaks-testbed' in t.restrictions:
+                       if 'breaks-testbed' in t.restriction_names:
                                testbed.needs_reset()
                testbed.needs_reset()
 
@@ -958,12 +1010,12 @@ class Test:
        dn = []
        for d in t.depends:
                t._debug(' processing dependency '+d)
-               if not '_' in d:
+               if not '@' in d:
                        t._debug('  literal dependency '+d)
                        dn.append(d)
                else:
                        for (pkg,bin) in t.act.binaries:
-                               d = d.replace('_',pkg)
+                               d = d.replace('@',pkg)
                                t._debug('  synthesised dependency '+d)
                                dn.append(d)
        testbed.prepare(dn)
@@ -971,7 +1023,7 @@ class Test:
        t._debug('[----------------------------------------')
        def stdouterr(oe):
                idstr = t.what + '-' + oe
-               if opts.output_dir is not None and opts.output_dir.tb:
+               if opts.output_dir is not None and opts.output_dir.spec_tbp:
                        use_dir = opts.output_dir
                else:
                        use_dir = testbed.scratch
@@ -982,11 +1034,31 @@ class Test:
        af = RelativeInputFile(t.what, tree, t.path)
        so = stdouterr('stdout')
        se = stdouterr('stderr')
+
        tf = af.read(True)
-       if 'needs-root' not in t.restrictions:
-               tf = opts.user_wrap(tf)
-       rc = testbed.execute('test-'+t.what, [tf],
-               so=so.write(True), se=se.write(True), cwd=tree.read(True))
+       tmpdir = None
+       tree.read(True)
+
+       rc = testbed.execute('testchmod-'+t.what, ['chmod','+x','--',tf])
+       if rc: bomb('failed to chmod +x %s' % tf)
+
+       if 'needs-root' not in t.restriction_names and opts.user is not None:
+               tfl = ['su',opts.user,'-c',tf]
+               tmpdir = '%s%s-tmpdir' % (testbed.scratch.read(True), t.what)
+               script = 'rm -rf -- "$1"; mkdir -- "$1"'
+               if opts.user: script += '; chown %s "$1"' % opts.user
+               if 'rw-build-tree' in t.restriction_names:
+                       script += '; chown -R %s "$2"' % opts.user
+               rc = testbed.execute('mktmpdir-'+t.what,
+                       ['sh','-xec',script,'x',tmpdir,tree.read(True)])
+               if rc: bomb("could not create test tmpdir `%s', exit code %d"
+                               % (tmpdir, rc))
+       else:
+               tfl = [tf]
+
+       rc = testbed.execute('test-'+t.what, tfl,
+               so=so.write(True), se=se.write(True), cwd=tree.read(True),
+               tmpdir=tmpdir)
 
        so_read = so.read()
        se_read = se.read()
@@ -996,7 +1068,7 @@ class Test:
        if stab.st_size != 0:
                l = open(se_read).readline()
                l = l.rstrip('\n \t\r')
-               if len(l) > 40: l = l[:40] + '...'
+               if len(l) > 35: l = l[:35] + '...'
                t.reportfail('status: %d, stderr: %s' % (rc, l))
                t._debug(' - - - - - - - - - - stderr - - - - - - - - - -')
                debug_file(se_read)
@@ -1019,6 +1091,8 @@ def read_control(act, tree, control_override):
                control_af = control_override
                testbed.blame('arg:'+control_override.spec)
        else:
+               if act.missing_tests_control:
+                       return ()
                control_af = RelativeInputFile(act.what+'-testcontrol',
                        tree, 'debian/tests/control')
        try:
@@ -1077,9 +1151,12 @@ def read_control(act, tree, control_override):
                        tnames = map((lambda lt: lt[1]), tnames)
                        tnames = string.join(tnames).split()
                        base = {
+                               'restriction_names': [],
                                'restrictions': [],
+                               'feature_names': [],
+                               'features': [],
                                'testsdir': 'debian/tests',
-                               'depends' : '_'
+                               'depends' : '@'
                        }
                        for fname in stz.keys():
                                if fname.startswith(' '): continue
@@ -1104,9 +1181,11 @@ def print_exception(ei, msgprefix=''):
        (et, q, tb) = ei
        if et is Quit:
                pstderr('adt-run: ' + q.m)
+               psummary('quitting: '+q.m)
                return q.ec
        else:
                pstderr("adt-run: unexpected, exceptional, error:")
+               psummary('quitting: unexpected error, consult transcript')
                traceback.print_exc(None, sys.stderr)
                if trace_stream is not None:
                        traceback.print_exc(None, trace_stream)
@@ -1222,7 +1301,9 @@ END
                'print res',
                'print d.missingDeps',
                'print d.requiredChanges',
-               'assert(res)',
+               'if not res: raise "gdebi failed (%s, %s, %s): %s" % '+
+                       ' (`res`, `d.missingDeps`, `d.requiredChanges`, '+
+                         'd._failureString)',
                'cache.commit()',
                ''
                ]
@@ -1349,21 +1430,28 @@ def source_rules_command(act,script,what,which,work,cwd,
        rc = testbed.execute('%s-%s' % (what,which),
                        ['sh','-ec',script]+xargs, script=script,
                        so=so.write(True), cwd=cwd)
-       results = open(so.read()).read().rstrip('\n').split("\n")
-       if rc: badpkg("%s failed with exit code %d" % (which,rc))
+       results = open(so.read()).read().rstrip('\n')
+       if len(results): results = results.split("\n")
+       else: results = []
+       if rc: badpkg("rules %s failed with exit code %d" % (which,rc))
        if results_lines is not None and len(results) != results_lines:
                badpkg("got %d lines of results from %s where %d expected"
                        % (len(results), which, results_lines))
        if results_lines==1: return results[0]
        return results
 
-def build_source(act):
+def build_source(act, control_override):
        act.blame = 'arg:'+act.af.spec
        testbed.blame(act.blame)
+       testbed.prepare1([])
        testbed.needs_reset()
 
        what = act.what
        basename = act.af.spec
+       debiancontrol = None
+       act.binaries = []
+
+       def debug_b(m): debug('* <%s:%s> %s' % (act.kind, act.what, m))
 
        if act.kind == 'dsc':
                dsc = act.af
@@ -1402,29 +1490,28 @@ def build_source(act):
                print >>dsc_w, 'Binary: none-so-this-is-not-a-package-name'
                dsc_w.close()
 
-       script = binaries.apt_pkg_gdebi_script(
-               dsc.read(True), [[
-               'from GDebi.DscSrcPackage import DscSrcPackage',
-               'd = DscSrcPackage(cache, arg)',
-               'res = d.checkDeb()',
-                ],[
-               'from GDebi.DebPackage import DebPackage',
-               'd = DebPackage(cache)',
-               'res = d.satisfyDependsStr("build-essential")',
-               ]])
-
-       cmdl = ['python','-c',script]
-       whatp = what+'-builddeps'
-       rc = testbed.execute(what, cmdl, script=script)
-       if rc: badpkg('build-depends install failed, exit code %d' % rc)
+       if act.kind == 'dsc':
+               testbed.prepare2([])
+               script = binaries.apt_pkg_gdebi_script('', [[
+                               'from GDebi.DebPackage import DebPackage',
+                               'd = DebPackage(cache)',
+                               'res = d.satisfyDependsStr("dpkg-dev")',
+                       ]])
+               cmdl = ['python','-c',script]
+               whatp = what+'-dpkgsource'
+               rc = testbed.execute(what, cmdl, script=script)
+               if rc: badpkg('dpkg-source install failed, exit code %d' % rc)
 
        work = TemporaryDir(what+'-build')
+       act.work = work
 
-       script = [
-               'arg="$1"',
-               'origpwd=`pwd`',
-               'cd '+work.write(True)
-       ]
+       tmpdir = work.write(True)+'/tmpdir'
+       tmpdir_script = [
+                       'TMPDIR="$1"',
+                       'rm -rf -- "$TMPDIR"',
+                       'export TMPDIR',
+                       opts.user_wrap('mkdir -- "$TMPDIR"'),
+               ]
 
        if act.kind == 'ubtree':
                spec = '%s/real-tree' % work.write(True)
@@ -1442,52 +1529,129 @@ def build_source(act):
                        '''
                initcwd = work.write(True)
 
-       if opts.user: script += [
-                       'chown '+opts.user+' .',
-                       'spec="$arg" '+opts.user_wrap(create_command)
-                       ]
-       else: script += [
-                       'spec="$arg"',
-                       create_command
-               ]
+       script = [
+               'spec="$2"',
+               'origpwd=`pwd`',
+               'cd '+work.write(True)
+       ]
+
+       if opts.user:
+               script += ([ 'chown '+opts.user+' .' ] +
+                       tmpdir_script +
+                       [ 'spec="$spec" origpwd="$origpwd" '
+                               +opts.user_wrap(create_command) ])
+       else:
+               script += (tmpdir_script +
+                       [ create_command ])
 
        script += [
                        'cd */.',
-                       'dpkg-checkbuilddeps',
                        'pwd >&3',
-                       opts.user_wrap('debian/rules build'),
+                       'set +e; test -f debian/tests/control; echo $? >&3'
                ]
-       result_pwd = source_rules_command(act,script,what,'build',work,
-                       cwd=initcwd, results_lines=1, xargs=['x',spec])
+       (result_pwd, control_test_rc) = source_rules_command(
+                       act,script,what,'extract',work,
+                       cwd=initcwd, results_lines=2, xargs=['x',tmpdir,spec])
+
+       filter = act.ah['dsc_filter']
 
-       if os.path.dirname(result_pwd)+'/' != work.read(True):
-               badpkg("results dir `%s' is not in expected parent dir `%s'"
-                       % (result_pwd, work.read(True)))
+       if control_test_rc == '1': act.missing_tests_control = True
+
+       # For optional builds:
+       #
+       # We might need to build the package because:
+       #   - we want its binaries (filter isn't _ and at least one of the
+       #       deb_... isn't ignore)
+       #   - the test control file says so
+       #       (assuming we have any tests)
+
+       class NeedBuildException: pass
+       def build_needed(m):
+               debug_b('build needed for %s' % m)
+               raise NeedBuildException()
+
+       try:
+               if filter != '_' and (act.ah['deb_forbuilds'] != 'ignore' or
+                                     act.ah['deb_fortests'] != 'ignore'):
+                       build_needed('binaries')
+
+               result_pwd_af = InputDir(what+'-treeforcontrol',
+                       result_pwd, True)
+               stanzas = read_control(act, result_pwd_af, control_override)
+               for stanza in stanzas:
+                       for t in stanza[' tests']:
+                               if 'no-build-needed' not in t.feature_names:
+                                       build_needed('test %s' % t.tname)
+                               for d in t.depends:
+                                       if '@' in d:
+                                               build_needed('test %s '
+                                                'dependency %s' % (t.tname,d))
+
+               debug_b('build not needed')
+               built = False
+
+       except NeedBuildException:
+
+               if act.kind != 'dsc':
+                       testbed.prepare2([])
+
+               script = binaries.apt_pkg_gdebi_script(
+                       dsc.read(True), [[
+                       'from GDebi.DscSrcPackage import DscSrcPackage',
+                       'd = DscSrcPackage(cache, arg)',
+                       'res = d.checkDeb()',
+                        ],[
+                       'from GDebi.DebPackage import DebPackage',
+                       'd = DebPackage(cache)',
+                       'res = d.satisfyDependsStr("'+
+                                       ','.join(build_essential)+
+                               '")',
+                       ]])
+
+               cmdl = ['python','-c',script]
+               whatp = what+'-builddeps'
+               rc = testbed.execute(what, cmdl, script=script)
+               if rc: badpkg('build-depends install failed,'
+                             ' exit code %d' % rc)
+
+               script = tmpdir_script + [
+                       'cd "$2"',
+                       'dpkg-checkbuilddeps',
+                       opts.user_wrap('debian/rules build'),
+                       ]
+               source_rules_command(act,script,what,'build',work,
+                               cwd=initcwd, xargs=['x',tmpdir,result_pwd])
+
+               if os.path.dirname(result_pwd)+'/' != work.read(True):
+                       badpkg("results dir `%s' is not in expected parent"
+                              " dir `%s'" % (result_pwd, work.read(True)))
+
+               built = True
 
-       act.work = work
        act.tests_tree = InputDir(what+'-tests-tree',
                                work.read(True)+os.path.basename(result_pwd),
                                True)
        if act.ah['dsc_tests']:
-               act.tests_tree.read()
                testbed.register_ephemeral(act.work)
                testbed.register_ephemeral(act.tests_tree)
 
+       if not built:
+               act.blamed = []
+               return
+
        act.blamed = copy.copy(testbed.blamed)
 
-       def debug_b(m): debug('* <%s:%s> %s' % (act.kind, act.what, m))
-       act.binaries = []
-       filter = act.ah['dsc_filter']
        debug_b('filter=%s' % filter)
        if filter != '_':
-               script = [
+               script = tmpdir_script + [
                        'cd '+work.write(True)+'/*/.',
                        opts.user_wrap(opts.gainroot+' debian/rules binary'),
                        'cd ..',
                        'echo *.deb >&3',
                        ]
                result_debs = source_rules_command(act,script,what,
-                               'binary',work,work.write(True),results_lines=1)
+                               'binary',work,work.write(True),
+                               results_lines=1, xargs=['x',tmpdir])
                if result_debs == '*': debs = []
                else: debs = result_debs.split(' ')
                debug_b('debs='+`debs`)
@@ -1526,16 +1690,19 @@ def process_actions():
        binaries = Binaries()
 
        for act in opts.actions:
-               testbed.register_ephemeral(act.af)
+               if not act.af.spec_tbp:
+                       testbed.register_ephemeral(act.af)
 
        binaries.reset()
+       control_override = None
 
        debug_a1('builds ...')
        for act in opts.actions:
                debug_a2('%s %s' %
                        (act.kind, act.what))
 
-               testbed.prepare([])
+               if act.kind == 'control':
+                       control_override = act.af
                if act.kind == 'deb':
                        testbed.blame('arg:'+act.af.spec)
                        determine_package(act)
@@ -1543,9 +1710,11 @@ def process_actions():
                        binaries.register(act,act.pkg,act.af,
                                'forbuilds',testbed.blamed)
                if act.kind == 'dsc' or act.kind == 'ubtree':
-                       build_source(act)
+                       build_source(act, control_override)
                if act.kind == 'tree':
                        act.binaries = []
+               if act.kind.endswith('tree') or act.kind == 'dsc':
+                       control_override = None
 
        debug_a1('builds done.')