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')
17 "source3" : [ "./autogen.sh",
18 "./configure.developer ${PREFIX}",
20 "make -j 4 everything", # don't use too many processes
22 "TDB_NO_FSYNC=1 make test" ],
24 "source4" : [ "./autogen.sh",
25 "./configure.developer ${PREFIX}",
28 "TDB_NO_FSYNC=1 make test" ],
30 "source4/lib/ldb" : [ "./autogen-waf.sh",
31 "./configure --enable-developer -C ${PREFIX}",
36 "lib/tdb" : [ "./autogen-waf.sh",
37 "./configure --enable-developer -C ${PREFIX}",
42 "lib/talloc" : [ "./autogen-waf.sh",
43 "./configure --enable-developer -C ${PREFIX}",
48 "lib/replace" : [ "./autogen-waf.sh",
49 "./configure --enable-developer -C ${PREFIX}",
54 "lib/tevent" : [ "./autogen-waf.sh",
55 "./configure --enable-developer -C ${PREFIX}",
61 def run_cmd(cmd, dir=".", show=None):
65 show = options.verbose
67 print("Running: '%s' in '%s'" % (cmd, dir))
71 raise Exception("FAILED %s: %d" % (cmd, ret))
74 '''handle build of one directory'''
75 def __init__(self, name, sequence):
78 if name == 'pass' or name == 'fail':
80 self.tag = self.name.replace('/', '_')
81 self.sequence = sequence
83 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
84 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
85 cleanup_list.append(self.stdout_path)
86 cleanup_list.append(self.stderr_path)
87 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
88 self.stdout = open(self.stdout_path, 'w')
89 self.stderr = open(self.stderr_path, 'w')
90 self.stdin = open("/dev/null", 'r')
91 self.sdir = "%s/%s" % (testbase, self.tag)
92 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
93 run_cmd("rm -rf %s" % self.sdir)
94 cleanup_list.append(self.sdir)
95 cleanup_list.append(self.prefix)
96 os.makedirs(self.sdir)
97 run_cmd("rm -rf %s" % self.sdir)
98 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
101 def start_next(self):
102 if self.next == len(self.sequence):
103 print '%s: Completed OK' % self.name
106 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
107 print '%s: Running %s' % (self.name, self.cmd)
109 os.chdir("%s/%s" % (self.sdir, self.dir))
110 self.proc = Popen(self.cmd, shell=True,
111 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
117 '''handle build of multiple directories'''
118 def __init__(self, tasklist, tasknames):
121 self.tail_proc = None
122 if tasknames == ['pass']:
123 tasks = { 'pass' : [ '/bin/true' ]}
124 if tasknames == ['fail']:
125 tasks = { 'fail' : [ '/bin/false' ]}
129 b = builder(n, tasks[n])
134 if b.proc is not None:
138 if self.tail_proc is not None:
139 self.tail_proc.terminate()
140 self.tail_proc.wait()
141 self.tail_proc = None
150 b.status = b.proc.poll()
164 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
166 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
171 def tarlogs(self, fname):
172 tar = tarfile.open(fname, "w:gz")
174 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
175 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
178 def remove_logs(self):
180 os.unlink(b.stdout_path)
181 os.unlink(b.stderr_path)
183 def start_tail(self):
185 cmd = "tail -f *.stdout *.stderr"
187 self.tail_proc = Popen(cmd, shell=True)
192 if options.nocleanup:
194 print("Cleaning up ....")
195 for d in cleanup_list:
196 run_cmd("rm -rf %s" % d)
200 '''get to the top of the git repo'''
202 while os.getcwd() != '/':
214 def rebase_tree(url):
215 print("Rebasing on %s" % url)
216 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
217 run_cmd("git fetch master", show=True, dir=test_master)
218 if options.fix_whitespace:
219 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
221 run_cmd("git rebase master/master", show=True, dir=test_master)
224 print("Pushing to %s" % url)
226 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
227 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
228 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
230 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
232 parser = OptionParser()
233 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
234 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
235 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
236 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
237 default=def_testbase)
238 parser.add_option("", "--passcmd", help="command to run on success", default=None)
239 parser.add_option("", "--verbose", help="show all commands as they are run",
240 default=False, action="store_true")
241 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
242 default=None, type='str')
243 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
244 default=False, action='store_true')
245 parser.add_option("", "--pushto", help="push to a git url on success",
246 default=None, type='str')
247 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
248 default=False, action='store_true')
249 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
250 default=False, action="store_true")
251 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
252 default=False, action="store_true")
255 (options, args) = parser.parse_args()
257 testbase = "%s/build.%u" % (options.testbase, os.getpid())
258 test_master = "%s/master" % testbase
260 gitroot = find_git_root()
262 raise Exception("Failed to find git root")
265 os.makedirs(testbase)
266 except Exception, reason:
267 raise Exception("Unable to create %s : %s" % (testbase, reason))
268 cleanup_list.append(testbase)
271 run_cmd("rm -rf %s" % test_master)
272 cleanup_list.append(test_master)
273 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
279 if options.rebase is not None:
280 rebase_tree(options.rebase)
281 elif options.rebase_master:
282 rebase_tree(samba_master)
283 blist = buildlist(tasks, args)
286 (status, errstr) = blist.run()
293 print("waiting for tail to flush")
298 if options.passcmd is not None:
299 print("Running passcmd: %s" % options.passcmd)
300 run_cmd(options.passcmd, dir=test_master)
301 if options.pushto is not None:
302 push_to(options.pushto)
303 elif options.push_master:
304 push_to(samba_master_ssh)
306 blist.tarlogs("logs.tar.gz")
307 print("Logs in logs.tar.gz")
313 # something failed, gather a tar of the logs
314 blist.tarlogs("logs.tar.gz")
318 print("Logs in logs.tar.gz")
319 sys.exit(os.WEXITSTATUS(status))