2 # run tests on all Samba subprojects and push to a git tree on success
3 # Copyright Andrew Tridgell 2010
4 # released under GNU GPL v3 or later
6 from subprocess import Popen, PIPE
7 import os, signal, tarfile, sys, time
8 from optparse import OptionParser
14 "source3" : [ "./autogen.sh",
15 "./configure.developer ${PREFIX}",
20 "source4" : [ "./autogen.sh",
21 "./configure.developer ${PREFIX}",
25 "source4/lib/ldb" : [ "./autogen-waf.sh",
26 "./configure --enable-developer -C ${PREFIX}",
30 "lib/tdb" : [ "./autogen-waf.sh",
31 "./configure --enable-developer -C ${PREFIX}",
35 "lib/talloc" : [ "./autogen-waf.sh",
36 "./configure --enable-developer -C ${PREFIX}",
40 "lib/replace" : [ "./autogen-waf.sh",
41 "./configure --enable-developer -C ${PREFIX}",
45 "lib/tevent" : [ "./autogen-waf.sh",
46 "./configure --enable-developer -C ${PREFIX}",
51 def run_cmd(cmd, dir=".", show=None):
55 show = options.verbose
57 print("Running: '%s' in '%s'" % (cmd, dir))
61 raise Exception("FAILED %s: %d" % (cmd, ret))
64 '''handle build of one directory'''
65 def __init__(self, name, sequence):
68 if name == 'pass' or name == 'fail':
70 self.tag = self.name.replace('/', '_')
71 self.sequence = sequence
73 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
74 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
75 cleanup_list.append(self.stdout_path)
76 cleanup_list.append(self.stderr_path)
77 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
78 self.stdout = open(self.stdout_path, 'w')
79 self.stderr = open(self.stderr_path, 'w')
80 self.stdin = open("/dev/null", 'r')
81 self.sdir = "%s/%s" % (testbase, self.tag)
82 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
83 run_cmd("rm -rf %s" % self.sdir)
84 cleanup_list.append(self.sdir)
85 cleanup_list.append(self.prefix)
86 os.makedirs(self.sdir)
87 run_cmd("rm -rf %s" % self.sdir)
88 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
92 if self.next == len(self.sequence):
93 print '%s: Completed OK' % self.name
96 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
97 print '%s: Running %s' % (self.name, self.cmd)
99 os.chdir("%s/%s" % (self.sdir, self.dir))
100 self.proc = Popen(self.cmd, shell=True,
101 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
107 '''handle build of multiple directories'''
108 def __init__(self, tasklist, tasknames):
110 self.tail_proc = None
111 if tasknames == ['pass']:
112 tasks = { 'pass' : [ '/bin/true' ]}
113 if tasknames == ['fail']:
114 tasks = { 'fail' : [ '/bin/false' ]}
118 b = builder(n, tasks[n])
123 if b.proc is not None:
127 if self.tail_proc is not None:
128 self.tail_proc.terminate()
129 self.tail_proc.wait()
130 self.tail_proc = None
139 b.status = b.proc.poll()
153 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
155 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
160 def tarlogs(self, fname):
161 tar = tarfile.open(fname, "w:gz")
163 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
164 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
167 def remove_logs(self):
169 os.unlink(b.stdout_path)
170 os.unlink(b.stderr_path)
172 def start_tail(self):
174 cmd = "tail -f *.stdout *.stderr"
176 self.tail_proc = Popen(cmd, shell=True)
181 if options.nocleanup:
183 print("Cleaning up ....")
184 for d in cleanup_list:
185 run_cmd("rm -rf %s" % d)
189 '''get to the top of the git repo'''
191 while os.getcwd() != '/':
203 def rebase_tree(url):
204 print("Rebasing on %s" % url)
205 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
206 run_cmd("git fetch master", show=True, dir=test_master)
207 run_cmd("git rebase master/master", show=True, dir=test_master)
210 print("Pushing to %s" % url)
212 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
213 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
214 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
216 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
218 parser = OptionParser()
219 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
220 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
221 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
222 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
223 default=def_testbase)
224 parser.add_option("", "--passcmd", help="command to run on success", default=None)
225 parser.add_option("", "--verbose", help="show all commands as they are run",
226 default=False, action="store_true")
227 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
228 default=None, type='str')
229 parser.add_option("", "--pushto", help="push to a git url on success",
230 default=None, type='str')
231 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
232 default=False, action="store_true")
235 (options, args) = parser.parse_args()
237 testbase = "%s/build.%u" % (options.testbase, os.getpid())
238 test_master = "%s/master" % testbase
240 gitroot = find_git_root()
242 raise Exception("Failed to find git root")
245 os.makedirs(testbase)
246 except Exception, reason:
247 raise Exception("Unable to create %s : %s" % (testbase, reason))
248 cleanup_list.append(testbase)
251 run_cmd("rm -rf %s" % test_master)
252 cleanup_list.append(test_master)
253 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
259 if options.rebase is not None:
260 rebase_tree(options.rebase)
261 blist = buildlist(tasks, args)
264 (status, errstr) = blist.run()
271 print("waiting for tail to flush")
276 if options.passcmd is not None:
277 print("Running passcmd: %s" % options.passcmd)
278 run_cmd(options.passcmd, dir=test_master)
279 if options.pushto is not None:
280 push_to(options.pushto)
282 blist.tarlogs("logs.tar.gz")
283 print("Logs in logs.tar.gz")
289 # something failed, gather a tar of the logs
290 blist.tarlogs("logs.tar.gz")
294 print("Logs in logs.tar.gz")
295 sys.exit(os.WEXITSTATUS(status))