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" ],
26 "source4" : [ "./autogen.sh",
27 "./configure.developer ${PREFIX}",
30 "TDB_NO_FSYNC=1 make test" ],
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 def run_cmd(cmd, dir=".", show=None):
67 show = options.verbose
69 print("Running: '%s' in '%s'" % (cmd, dir))
73 raise Exception("FAILED %s: %d" % (cmd, ret))
76 '''handle build of one directory'''
77 def __init__(self, name, sequence):
80 if name == 'pass' or name == 'fail':
82 self.tag = self.name.replace('/', '_')
83 self.sequence = sequence
85 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
86 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
87 cleanup_list.append(self.stdout_path)
88 cleanup_list.append(self.stderr_path)
89 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
90 self.stdout = open(self.stdout_path, 'w')
91 self.stderr = open(self.stderr_path, 'w')
92 self.stdin = open("/dev/null", 'r')
93 self.sdir = "%s/%s" % (testbase, self.tag)
94 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
95 run_cmd("rm -rf %s" % self.sdir)
96 cleanup_list.append(self.sdir)
97 cleanup_list.append(self.prefix)
98 os.makedirs(self.sdir)
99 run_cmd("rm -rf %s" % self.sdir)
100 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
103 def start_next(self):
104 if self.next == len(self.sequence):
105 print '%s: Completed OK' % self.name
108 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
109 print '%s: Running %s' % (self.name, self.cmd)
111 os.chdir("%s/%s" % (self.sdir, self.dir))
112 self.proc = Popen(self.cmd, shell=True,
113 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
119 '''handle build of multiple directories'''
120 def __init__(self, tasklist, tasknames):
123 self.tail_proc = None
124 if tasknames == ['pass']:
125 tasks = { 'pass' : [ '/bin/true' ]}
126 if tasknames == ['fail']:
127 tasks = { 'fail' : [ '/bin/false' ]}
131 b = builder(n, tasks[n])
136 if b.proc is not None:
140 if self.tail_proc is not None:
141 self.tail_proc.terminate()
142 self.tail_proc.wait()
143 self.tail_proc = None
152 b.status = b.proc.poll()
166 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
168 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
173 def tarlogs(self, fname):
174 tar = tarfile.open(fname, "w:gz")
176 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
177 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
180 def remove_logs(self):
182 os.unlink(b.stdout_path)
183 os.unlink(b.stderr_path)
185 def start_tail(self):
187 cmd = "tail -f *.stdout *.stderr"
189 self.tail_proc = Popen(cmd, shell=True)
194 if options.nocleanup:
196 print("Cleaning up ....")
197 for d in cleanup_list:
198 run_cmd("rm -rf %s" % d)
202 '''get to the top of the git repo'''
204 while os.getcwd() != '/':
216 def rebase_tree(url):
217 print("Rebasing on %s" % url)
218 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
219 run_cmd("git fetch master", show=True, dir=test_master)
220 if options.fix_whitespace:
221 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
223 run_cmd("git rebase master/master", show=True, dir=test_master)
226 print("Pushing to %s" % url)
228 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
229 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
230 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
232 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
234 parser = OptionParser()
235 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
236 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
237 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
238 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
239 default=def_testbase)
240 parser.add_option("", "--passcmd", help="command to run on success", default=None)
241 parser.add_option("", "--verbose", help="show all commands as they are run",
242 default=False, action="store_true")
243 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
244 default=None, type='str')
245 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
246 default=False, action='store_true')
247 parser.add_option("", "--pushto", help="push to a git url on success",
248 default=None, type='str')
249 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
250 default=False, action='store_true')
251 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
252 default=False, action="store_true")
253 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
254 default=False, action="store_true")
257 (options, args) = parser.parse_args()
259 testbase = "%s/build.%u" % (options.testbase, os.getpid())
260 test_master = "%s/master" % testbase
262 gitroot = find_git_root()
264 raise Exception("Failed to find git root")
267 os.makedirs(testbase)
268 except Exception, reason:
269 raise Exception("Unable to create %s : %s" % (testbase, reason))
270 cleanup_list.append(testbase)
273 run_cmd("rm -rf %s" % test_master)
274 cleanup_list.append(test_master)
275 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
281 if options.rebase is not None:
282 rebase_tree(options.rebase)
283 elif options.rebase_master:
284 rebase_tree(samba_master)
285 blist = buildlist(tasks, args)
288 (status, errstr) = blist.run()
295 print("waiting for tail to flush")
300 if options.passcmd is not None:
301 print("Running passcmd: %s" % options.passcmd)
302 run_cmd(options.passcmd, dir=test_master)
303 if options.pushto is not None:
304 push_to(options.pushto)
305 elif options.push_master:
306 push_to(samba_master_ssh)
308 blist.tarlogs("logs.tar.gz")
309 print("Logs in logs.tar.gz")
315 # something failed, gather a tar of the logs
316 blist.tarlogs("logs.tar.gz")
320 print("Logs in logs.tar.gz")
321 sys.exit(os.WEXITSTATUS(status))