chiark / gitweb /
debian/rules: remove runner/tmp
[autopkgtest.git] / virt-subproc / adt-virt-xenlvm
1 #!/usr/bin/python2.6
2 #
3 # adt-virt-xenlvm is part of autopkgtest
4 # autopkgtest is a tool for testing Debian binary packages
5 #
6 # autopkgtest is Copyright (C) 2006-2007 Canonical Ltd.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #
22 # See the file CREDITS for a full list of credits information (often
23 # installed as /usr/share/doc/autopkgtest/CREDITS).
24
25 import sys
26 import os
27 import re as regexp
28 import string
29 import subprocess
30 import fnmatch
31 import signal
32 from optparse import OptionParser
33
34 try: our_base = os.environ['AUTOPKGTEST_BASE']+'/lib'
35 except KeyError: our_base = '/usr/share/autopkgtest/python';
36 sys.path.insert(1, our_base)
37
38 import VirtSubproc as vsp
39
40 withholder = None
41 downtmp = '/root/adt-downtmp'
42 pauses = None
43 event_counters = { }
44
45 def check_pause(kind):
46         if not event_counters.has_key(kind): event_counters[kind] = 0
47         event = '%s#%d' % (kind, event_counters[kind])
48         event_counters[kind] += 1
49         for pattern in pauses:
50                 if not '#' in pattern: matched = kind == pattern
51                 else: matched = fnmatch.fnmatchcase(event,pattern)
52                 if matched: break
53         if not matched:
54                 return
55         print >>sys.stderr, ("----- adt-virt-xenlvm pause (event=%s),"
56                                 " stopping -----" % event)
57         os.kill(0, signal.SIGSTOP)
58
59 def parse_args():
60         global debuglevel, with_testbed, console, pauses
61
62         usage = "%prog <options> [-- <adt-xenlvm options>]"
63         parser = OptionParser(usage=usage)
64         pa = parser.add_option
65         pe = parser.error
66
67         pa('-r', '--gain-root', type='string', dest='gain_root')
68         pa('-d', '--debug', action='store_true', dest='debug')
69         pa('', '--userv', action='store_true', dest='userv')
70         pa('', '--distro', type='string', dest='distro')
71         pa('', '--nominum', type='string', dest='nominum')
72         pa('','--pause', type='string', dest='pause', default='')
73
74         (opts,xlargs) = parser.parse_args()
75         vsp.debuglevel = opts.debug
76         xargs_userv = []
77         xargs_direct = []
78
79         for k in ['distro','nominum']:
80                 v = getattr(opts,k)
81                 if v is None: continue
82                 xargs_direct.append('--%s=%s' % (k, v))
83                 xargs_userv.append('-D%s=%s' % (k, v))
84
85         if not opts.userv:
86                 if opts.gain_root is None: gain_root = []
87                 else: gain_root = opts.gain_root.split()
88                 with_testbed = (gain_root + ['adt-xenlvm-with-testbed'] +
89                         xargs_direct + xlargs +
90                         ['--','sh','-ec','echo y; exec cat'])
91                 vsp.down = (gain_root + ['adt-xenlvm-on-testbed'] +
92                         xargs_direct + xlargs + ['--'])
93                 vsp.downkind = 'shstring'
94         else:
95                 if opts.gain_root:
96                         pe('--userv and --gain-root are not compatible')
97                 basis = (['userv'] + xargs_userv + xlargs +
98                         ['root','adt-xenlvm-testbed'])
99                 with_testbed = basis + ['with']
100                 get_down = subprocess.Popen(basis + ['pon0'],
101                         stdin=file('/dev/null'), stdout=subprocess.PIPE,
102                         stderr=None)
103                 (pon0, _) = get_down.communicate()
104                 if get_down.returncode:
105                         vsp.bomb('failed to check userv service provision'
106                                 ' and subcommand details (code=%d)' %
107                                 get_down.returncode)
108                 vsp.down = pon0.split('\0')
109                 vsp.downkind = 'auxverb'
110         pauses = opts.pause.split(',')
111
112 def do_open():
113         global withholder
114         assert(withholder is None)
115         withholder = subprocess.Popen(
116                 with_testbed,
117                 stdin=subprocess.PIPE, stdout=subprocess.PIPE )
118         l = withholder.stdout.readline(2)
119         rc = withholder.poll()
120         if rc is not None:
121                 withholder.stdin.close()
122                 withholder.stdout.close()
123                 withholder = None
124                 vsp.bomb("with-testbed failed, code %d" % rc)
125         if l != "y\n":
126                 vsp.bomb("with-testbed sh gave wrong output `%s', not `l'"
127                         % l.rstrip("\n"))
128
129 def do_close():
130         global withholder
131         check_pause('close')
132         withholder.stdin.close()
133         withholder.stdout.close()
134         rc = withholder.wait()
135         withholder = None
136         if rc: vsp.bomb("with-testbed failed when closing/reverting,"
137                         " code %d" % rc)
138
139 def hook_forked_inchild():
140         if withholder is not None:
141                 withholder.stdin.close()
142                 withholder.stdout.close()
143
144 def hook_open():
145         hook_cleanup()
146         return do_open()
147
148 def hook_downtmp():
149         vsp.execute('mkdir %s' % downtmp, downp=True)
150         return downtmp
151
152 def hook_revert():
153         check_pause('revert')
154         do_close()
155         return do_open()
156
157 def hook_cleanup():
158         check_pause('cleanup')
159         global withholder
160         if withholder is not None:
161                 do_close()
162
163 def hook_capabilities():
164         return ['revert','root-on-testbed','suggested-normal-user=adtxenu']
165
166 parse_args()
167 vsp.main()