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):
120 self.tail_proc = None
121 if tasknames == ['pass']:
122 tasks = { 'pass' : [ '/bin/true' ]}
123 if tasknames == ['fail']:
124 tasks = { 'fail' : [ '/bin/false' ]}
128 b = builder(n, tasks[n])
133 if b.proc is not None:
137 if self.tail_proc is not None:
138 self.tail_proc.terminate()
139 self.tail_proc.wait()
140 self.tail_proc = None
149 b.status = b.proc.poll()
163 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
165 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
170 def tarlogs(self, fname):
171 tar = tarfile.open(fname, "w:gz")
173 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
174 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
177 def remove_logs(self):
179 os.unlink(b.stdout_path)
180 os.unlink(b.stderr_path)
182 def start_tail(self):
184 cmd = "tail -f *.stdout *.stderr"
186 self.tail_proc = Popen(cmd, shell=True)
191 if options.nocleanup:
193 print("Cleaning up ....")
194 for d in cleanup_list:
195 run_cmd("rm -rf %s" % d)
199 '''get to the top of the git repo'''
201 while os.getcwd() != '/':
213 def rebase_tree(url):
214 print("Rebasing on %s" % url)
215 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
216 run_cmd("git fetch master", show=True, dir=test_master)
217 run_cmd("git rebase master/master", show=True, dir=test_master)
220 print("Pushing to %s" % url)
222 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
223 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
224 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
226 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
228 parser = OptionParser()
229 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
230 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
231 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
232 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
233 default=def_testbase)
234 parser.add_option("", "--passcmd", help="command to run on success", default=None)
235 parser.add_option("", "--verbose", help="show all commands as they are run",
236 default=False, action="store_true")
237 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
238 default=None, type='str')
239 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
240 default=False, action='store_true')
241 parser.add_option("", "--pushto", help="push to a git url on success",
242 default=None, type='str')
243 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
244 default=False, action='store_true')
245 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
246 default=False, action="store_true")
249 (options, args) = parser.parse_args()
251 testbase = "%s/build.%u" % (options.testbase, os.getpid())
252 test_master = "%s/master" % testbase
254 gitroot = find_git_root()
256 raise Exception("Failed to find git root")
259 os.makedirs(testbase)
260 except Exception, reason:
261 raise Exception("Unable to create %s : %s" % (testbase, reason))
262 cleanup_list.append(testbase)
265 run_cmd("rm -rf %s" % test_master)
266 cleanup_list.append(test_master)
267 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
273 if options.rebase is not None:
274 rebase_tree(options.rebase)
275 elif options.rebase_master:
276 rebase_tree(samba_master)
277 blist = buildlist(tasks, args)
280 (status, errstr) = blist.run()
287 print("waiting for tail to flush")
292 if options.passcmd is not None:
293 print("Running passcmd: %s" % options.passcmd)
294 run_cmd(options.passcmd, dir=test_master)
295 if options.pushto is not None:
296 push_to(options.pushto)
297 elif options.push_master:
298 push_to(samba_master_ssh)
300 blist.tarlogs("logs.tar.gz")
301 print("Logs in logs.tar.gz")
307 # something failed, gather a tar of the logs
308 blist.tarlogs("logs.tar.gz")
312 print("Logs in logs.tar.gz")
313 sys.exit(os.WEXITSTATUS(status))