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
11 samba_master = os.getenv('SAMBA_MASTER', 'git://git.samba.org/samba.git')
12 samba_master_ssh = os.getenv('SAMBA_MASTER_SSH', 'git+ssh://git.samba.org/data/git/samba.git')
16 os.putenv('CC', "ccache gcc")
19 "source3" : [ "./autogen.sh",
20 "./configure.developer ${PREFIX}",
22 "make -j 4 everything", # don't use too many processes
24 "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
26 "source4" : [ "./autogen.sh",
27 "./configure.developer ${PREFIX}",
30 "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
32 "source4/lib/ldb" : [ "./autogen-waf.sh",
33 "./configure --enable-developer -C ${PREFIX}",
38 "lib/tdb" : [ "./autogen-waf.sh",
39 "./configure --enable-developer -C ${PREFIX}",
44 "lib/talloc" : [ "./autogen-waf.sh",
45 "./configure --enable-developer -C ${PREFIX}",
50 "lib/replace" : [ "./autogen-waf.sh",
51 "./configure --enable-developer -C ${PREFIX}",
56 "lib/tevent" : [ "./autogen-waf.sh",
57 "./configure --enable-developer -C ${PREFIX}",
63 retry_task = [ '''set -e
64 git remote add -t master master %s
68 git describe master/master > old_master.desc
70 git describe master/master > master.desc
71 diff old_master.desc master.desc
75 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
79 show = options.verbose
81 print("Running: '%s' in '%s'" % (cmd, dir))
83 ret = Popen([cmd], shell=True, stdout=PIPE).communicate()[0]
88 if checkfail and ret != 0:
89 raise Exception("FAILED %s: %d" % (cmd, ret))
93 '''handle build of one directory'''
94 def __init__(self, name, sequence):
97 if name in ['pass', 'fail', 'retry']:
102 self.tag = self.name.replace('/', '_')
103 self.sequence = sequence
105 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
106 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
107 cleanup_list.append(self.stdout_path)
108 cleanup_list.append(self.stderr_path)
109 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
110 self.stdout = open(self.stdout_path, 'w')
111 self.stderr = open(self.stderr_path, 'w')
112 self.stdin = open("/dev/null", 'r')
113 self.sdir = "%s/%s" % (testbase, self.tag)
114 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
115 run_cmd("rm -rf %s" % self.sdir)
116 cleanup_list.append(self.sdir)
117 cleanup_list.append(self.prefix)
118 os.makedirs(self.sdir)
119 run_cmd("rm -rf %s" % self.sdir)
120 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
123 def start_next(self):
124 if self.next == len(self.sequence):
125 print '%s: Completed OK' % self.name
128 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
129 print '%s: Running %s' % (self.name, self.cmd)
131 os.chdir("%s/%s" % (self.sdir, self.dir))
132 self.proc = Popen(self.cmd, shell=True,
133 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
139 '''handle build of multiple directories'''
140 def __init__(self, tasklist, tasknames):
143 self.tail_proc = None
145 if tasknames == ['pass']:
146 tasks = { 'pass' : [ '/bin/true' ]}
147 if tasknames == ['fail']:
148 tasks = { 'fail' : [ '/bin/false' ]}
152 b = builder(n, tasks[n])
155 self.retry = builder('retry', retry_task)
156 self.need_retry = False
159 if self.tail_proc is not None:
160 self.tail_proc.terminate()
161 self.tail_proc.wait()
162 self.tail_proc = None
163 if self.retry is not None:
164 self.retry.proc.terminate()
165 self.retry.proc.wait()
168 if b.proc is not None:
169 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
181 b.status = b.proc.poll()
187 ret = self.retry.proc.poll()
189 self.need_retry = True
199 if options.retry and self.need_retry:
201 print("retry needed")
205 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
207 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
212 def tarlogs(self, fname):
213 tar = tarfile.open(fname, "w:gz")
215 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
216 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
219 def remove_logs(self):
221 os.unlink(b.stdout_path)
222 os.unlink(b.stderr_path)
224 def start_tail(self):
226 cmd = "tail -f *.stdout *.stderr"
228 self.tail_proc = Popen(cmd, shell=True)
233 if options.nocleanup:
235 print("Cleaning up ....")
236 for d in cleanup_list:
237 run_cmd("rm -rf %s" % d)
241 '''get to the top of the git repo'''
243 while os.getcwd() != '/':
255 def rebase_tree(url):
256 print("Rebasing on %s" % url)
257 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
258 run_cmd("git fetch master", show=True, dir=test_master)
259 if options.fix_whitespace:
260 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
262 run_cmd("git rebase master/master", show=True, dir=test_master)
263 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
265 print("No differences between HEAD and master/master - exiting")
269 print("Pushing to %s" % url)
271 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
272 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
273 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
275 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
277 parser = OptionParser()
278 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
279 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
280 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
281 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
282 default=def_testbase)
283 parser.add_option("", "--passcmd", help="command to run on success", default=None)
284 parser.add_option("", "--verbose", help="show all commands as they are run",
285 default=False, action="store_true")
286 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
287 default=None, type='str')
288 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
289 default=False, action='store_true')
290 parser.add_option("", "--pushto", help="push to a git url on success",
291 default=None, type='str')
292 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
293 default=False, action='store_true')
294 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
295 default=False, action="store_true")
296 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
297 default=False, action="store_true")
298 parser.add_option("", "--retry", help="automatically retry if master changes",
299 default=False, action="store_true")
302 (options, args) = parser.parse_args()
305 if not options.rebase_master and options.rebase is None:
306 raise Exception('You can only use --retry if you also rebase')
308 testbase = "%s/build.%u" % (options.testbase, os.getpid())
309 test_master = "%s/master" % testbase
311 gitroot = find_git_root()
313 raise Exception("Failed to find git root")
316 os.makedirs(testbase)
317 except Exception, reason:
318 raise Exception("Unable to create %s : %s" % (testbase, reason))
319 cleanup_list.append(testbase)
323 run_cmd("rm -rf %s" % test_master)
324 cleanup_list.append(test_master)
325 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
331 if options.rebase is not None:
332 rebase_tree(options.rebase)
333 elif options.rebase_master:
334 rebase_tree(samba_master)
335 blist = buildlist(tasks, args)
338 (status, errstr) = blist.run()
339 if status != 0 or errstr != "retry":
348 print("waiting for tail to flush")
353 if options.passcmd is not None:
354 print("Running passcmd: %s" % options.passcmd)
355 run_cmd(options.passcmd, dir=test_master)
356 if options.pushto is not None:
357 push_to(options.pushto)
358 elif options.push_master:
359 push_to(samba_master_ssh)
361 blist.tarlogs("logs.tar.gz")
362 print("Logs in logs.tar.gz")
368 # something failed, gather a tar of the logs
369 blist.tarlogs("logs.tar.gz")
373 print("Logs in logs.tar.gz")
374 sys.exit(os.WEXITSTATUS(status))