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 call
, check_call
,Popen
, PIPE
7 import os
, tarfile
, sys
, time
8 from optparse
import OptionParser
10 from email
.mime
.text
import MIMEText
12 samba_master
= os
.getenv('SAMBA_MASTER', 'git://git.samba.org/samba.git')
13 samba_master_ssh
= os
.getenv('SAMBA_MASTER_SSH', 'git+ssh://git.samba.org/data/git/samba.git')
17 os
.putenv('CC', "ccache gcc")
20 "source3" : [ ("autogen", "./autogen.sh", "text/plain"),
21 ("configure", "./configure.developer ${PREFIX}", "text/plain"),
22 ("make basics", "make basics", "text/plain"),
23 ("make", "make -j 4 everything", "text/plain"), # don't use too many processes
24 ("install", "make install", "text/plain"),
25 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain") ],
27 # We have 'test' before 'install' because, 'test' should work without 'install'
28 "source4" : [ ("configure", "./configure.developer ${PREFIX}", "text/plain"),
29 ("make", "make -j", "text/plain"),
30 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain"),
31 ("install", "make install", "text/plain") ],
33 "source4/lib/ldb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
34 ("make", "make -j", "text/plain"),
35 ("install", "make install", "text/plain"),
36 ("test", "TDB_NO_FSYNC=1 make test", "text/plain") ],
38 # We don't use TDB_NO_FSYNC=1 here, because we want to test the transaction code
39 "lib/tdb" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
40 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
41 ("make", "make -j", "text/plain"),
42 ("install", "make install", "text/plain"),
43 ("test", "make test", "text/plain") ],
45 "lib/talloc" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
46 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
47 ("make", "make -j", "text/plain"),
48 ("install", "make install", "text/plain"),
49 ("test", "make test", "text/plain"), ],
51 "lib/replace" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
52 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
53 ("make", "make -j", "text/plain"),
54 ("install", "make install", "text/plain"),
55 ("test", "make test", "text/plain"), ],
57 "lib/tevent" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
58 ("make", "make -j", "text/plain"),
59 ("install", "make install", "text/plain"),
60 ("test", "make test", "text/plain"), ],
63 retry_task
= [ ( "retry",
65 git remote add -t master master %s
69 git describe master/master > old_master.desc
71 git describe master/master > master.desc
72 diff old_master.desc master.desc
74 ''' % samba_master
, "test/plain" ) ]
76 def run_cmd(cmd
, dir=".", show
=None, output
=False, checkfail
=True):
78 show
= options
.verbose
80 print("Running: '%s' in '%s'" % (cmd
, dir))
82 return Popen([cmd
], shell
=True, stdout
=PIPE
, cwd
=dir).communicate()[0]
84 return check_call(cmd
, shell
=True, cwd
=dir)
86 return call(cmd
, shell
=True, cwd
=dir)
89 class builder(object):
90 '''handle build of one directory'''
92 def __init__(self
, name
, sequence
):
95 if name
in ['pass', 'fail', 'retry']:
100 self
.tag
= self
.name
.replace('/', '_')
101 self
.sequence
= sequence
103 self
.stdout_path
= "%s/%s.stdout" % (gitroot
, self
.tag
)
104 self
.stderr_path
= "%s/%s.stderr" % (gitroot
, self
.tag
)
106 print("stdout for %s in %s" % (self
.name
, self
.stdout_path
))
107 print("stderr for %s in %s" % (self
.name
, self
.stderr_path
))
108 run_cmd("rm -f %s %s" % (self
.stdout_path
, self
.stderr_path
))
109 self
.stdout
= open(self
.stdout_path
, 'w')
110 self
.stderr
= open(self
.stderr_path
, 'w')
111 self
.stdin
= open("/dev/null", 'r')
112 self
.sdir
= "%s/%s" % (testbase
, self
.tag
)
113 self
.prefix
= "%s/prefix/%s" % (testbase
, self
.tag
)
114 run_cmd("rm -rf %s" % self
.sdir
)
115 cleanup_list
.append(self
.sdir
)
116 cleanup_list
.append(self
.prefix
)
117 os
.makedirs(self
.sdir
)
118 run_cmd("rm -rf %s" % self
.sdir
)
119 run_cmd("git clone --shared %s %s" % (gitroot
, self
.sdir
))
122 def start_next(self
):
123 if self
.next
== len(self
.sequence
):
124 print '%s: Completed OK' % self
.name
127 (self
.stage
, self
.cmd
, self
.output_mime_type
) = self
.sequence
[self
.next
]
128 self
.cmd
= self
.cmd
.replace("${PREFIX}", "--prefix=%s" % self
.prefix
)
129 # if self.output_mime_type == "text/x-subunit":
130 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
131 print '%s: [%s] Running %s' % (self
.name
, self
.stage
, self
.cmd
)
133 os
.chdir("%s/%s" % (self
.sdir
, self
.dir))
134 self
.proc
= Popen(self
.cmd
, shell
=True,
135 stdout
=self
.stdout
, stderr
=self
.stderr
, stdin
=self
.stdin
)
140 class buildlist(object):
141 '''handle build of multiple directories'''
143 def __init__(self
, tasklist
, tasknames
):
146 self
.tail_proc
= None
148 if tasknames
== ['pass']:
149 tasks
= { 'pass' : [ ("pass", '/bin/true', "text/plain") ]}
150 if tasknames
== ['fail']:
151 tasks
= { 'fail' : [ ("fail", '/bin/false', "text/plain") ]}
155 b
= builder(n
, tasks
[n
])
158 self
.retry
= builder('retry', retry_task
)
159 self
.need_retry
= False
162 if self
.tail_proc
is not None:
163 self
.tail_proc
.terminate()
164 self
.tail_proc
.wait()
165 self
.tail_proc
= None
166 if self
.retry
is not None:
167 self
.retry
.proc
.terminate()
168 self
.retry
.proc
.wait()
171 if b
.proc
is not None:
172 run_cmd("killbysubdir %s > /dev/null 2>&1" % b
.sdir
, checkfail
=False)
184 b
.status
= b
.proc
.poll()
190 ret
= self
.retry
.proc
.poll()
192 self
.need_retry
= True
202 if options
.retry
and self
.need_retry
:
204 print("retry needed")
205 return (0, None, None, None, "retry")
208 if os
.WIFSIGNALED(b
.status
) or os
.WEXITSTATUS(b
.status
) != 0:
210 return (b
.status
, b
.name
, b
.stage
, b
.tag
, "%s: [%s] failed '%s' with status %d" % (b
.name
, b
.stage
, b
.cmd
, b
.status
))
213 return (0, None, None, None, "All OK")
215 def tarlogs(self
, fname
):
216 tar
= tarfile
.open(fname
, "w:gz")
218 tar
.add(b
.stdout_path
, arcname
="%s.stdout" % b
.tag
)
219 tar
.add(b
.stderr_path
, arcname
="%s.stderr" % b
.tag
)
220 if os
.path
.exists("autobuild.log"):
221 tar
.add("autobuild.log")
224 def remove_logs(self
):
226 os
.unlink(b
.stdout_path
)
227 os
.unlink(b
.stderr_path
)
229 def start_tail(self
):
231 cmd
= "tail -f *.stdout *.stderr"
233 self
.tail_proc
= Popen(cmd
, shell
=True)
238 if options
.nocleanup
:
240 print("Cleaning up ....")
241 for d
in cleanup_list
:
242 run_cmd("rm -rf %s" % d
)
246 '''get to the top of the git repo'''
249 if os
.path
.isdir(os
.path
.join(p
, ".git")):
251 p
= os
.path
.abspath(os
.path
.join(p
, '..'))
255 def daemonize(logfile
):
257 if pid
== 0: # Parent
260 if pid
!= 0: # Actual daemon
265 import resource
# Resource usage information.
266 maxfd
= resource
.getrlimit(resource
.RLIMIT_NOFILE
)[1]
267 if maxfd
== resource
.RLIM_INFINITY
:
268 maxfd
= 1024 # Rough guess at maximum number of open file descriptors.
269 for fd
in range(0, maxfd
):
274 os
.open(logfile
, os
.O_RDWR | os
.O_CREAT
)
278 def write_pidfile(fname
):
279 '''write a pid file, cleanup on exit'''
280 f
= open(fname
, mode
='w')
281 f
.write("%u\n" % os
.getpid())
285 def rebase_tree(url
):
286 print("Rebasing on %s" % url
)
287 run_cmd("git remote add -t master master %s" % url
, show
=True, dir=test_master
)
288 run_cmd("git fetch master", show
=True, dir=test_master
)
289 if options
.fix_whitespace
:
290 run_cmd("git rebase --whitespace=fix master/master", show
=True, dir=test_master
)
292 run_cmd("git rebase master/master", show
=True, dir=test_master
)
293 diff
= run_cmd("git --no-pager diff HEAD master/master", dir=test_master
, output
=True)
295 print("No differences between HEAD and master/master - exiting")
299 print("Pushing to %s" % url
)
301 run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master
)
302 run_cmd("git commit --amend -c HEAD", dir=test_master
)
303 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
304 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
305 run_cmd("git remote add -t master pushto %s" % url
, show
=True, dir=test_master
)
306 run_cmd("git push pushto +HEAD:master", show
=True, dir=test_master
)
308 def_testbase
= os
.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os
.getenv('USER'))
310 parser
= OptionParser()
311 parser
.add_option("", "--tail", help="show output while running", default
=False, action
="store_true")
312 parser
.add_option("", "--keeplogs", help="keep logs", default
=False, action
="store_true")
313 parser
.add_option("", "--nocleanup", help="don't remove test tree", default
=False, action
="store_true")
314 parser
.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase
,
315 default
=def_testbase
)
316 parser
.add_option("", "--passcmd", help="command to run on success", default
=None)
317 parser
.add_option("", "--verbose", help="show all commands as they are run",
318 default
=False, action
="store_true")
319 parser
.add_option("", "--rebase", help="rebase on the given tree before testing",
320 default
=None, type='str')
321 parser
.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master
,
322 default
=False, action
='store_true')
323 parser
.add_option("", "--pushto", help="push to a git url on success",
324 default
=None, type='str')
325 parser
.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh
,
326 default
=False, action
='store_true')
327 parser
.add_option("", "--mark", help="add a Tested-By signoff before pushing",
328 default
=False, action
="store_true")
329 parser
.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
330 default
=False, action
="store_true")
331 parser
.add_option("", "--retry", help="automatically retry if master changes",
332 default
=False, action
="store_true")
333 parser
.add_option("", "--email", help="send email to the given address on failure",
334 type='str', default
=None)
335 parser
.add_option("", "--always-email", help="always send email, even on success",
337 parser
.add_option("", "--daemon", help="daemonize after initial setup",
341 def email_failure(status
, failed_task
, failed_stage
, failed_tag
, errstr
):
342 '''send an email to options.email about the failure'''
343 user
= os
.getenv("USER")
347 Your autobuild failed when trying to test %s with the following error:
350 the autobuild has been abandoned. Please fix the error and resubmit.
352 A summary of the autobuild process is here:
354 http://git.samba.org/%s/samba-autobuild/autobuild.log
355 ''' % (failed_task
, errstr
, user
)
357 if failed_task
!= 'rebase':
359 You can see logs of the failed task here:
361 http://git.samba.org/%s/samba-autobuild/%s.stdout
362 http://git.samba.org/%s/samba-autobuild/%s.stderr
364 or you can get full logs of all tasks in this job here:
366 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
368 The top commit for the tree that was built was:
372 ''' % (user
, failed_tag
, user
, failed_tag
, user
, top_commit_msg
)
374 msg
['Subject'] = 'autobuild failure for task %s during %s' % (failed_task
, failed_stage
)
375 msg
['From'] = 'autobuild@samba.org'
376 msg
['To'] = options
.email
380 s
.sendmail(msg
['From'], [msg
['To']], msg
.as_string())
384 '''send an email to options.email about a successful build'''
385 user
= os
.getenv("USER")
389 Your autobuild has succeeded.
396 you can get full logs of all tasks in this job here:
398 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
403 The top commit for the tree that was built was:
409 msg
['Subject'] = 'autobuild success'
410 msg
['From'] = 'autobuild@samba.org'
411 msg
['To'] = options
.email
415 s
.sendmail(msg
['From'], [msg
['To']], msg
.as_string())
419 (options
, args
) = parser
.parse_args()
422 if not options
.rebase_master
and options
.rebase
is None:
423 raise Exception('You can only use --retry if you also rebase')
425 testbase
= "%s/b%u" % (options
.testbase
, os
.getpid())
426 test_master
= "%s/master" % testbase
428 gitroot
= find_git_root()
430 raise Exception("Failed to find git root")
432 # get the top commit message, for emails
433 top_commit_msg
= run_cmd("git log -1", dir=gitroot
, output
=True)
436 os
.makedirs(testbase
)
437 except Exception, reason
:
438 raise Exception("Unable to create %s : %s" % (testbase
, reason
))
439 cleanup_list
.append(testbase
)
442 logfile
= os
.path
.join(testbase
, "log")
443 print "Forking into the background, writing progress to %s" % logfile
446 write_pidfile(gitroot
+ "/autobuild.pid")
450 run_cmd("rm -rf %s" % test_master
)
451 cleanup_list
.append(test_master
)
452 run_cmd("git clone --shared %s %s" % (gitroot
, test_master
))
459 if options
.rebase
is not None:
460 rebase_tree(options
.rebase
)
461 elif options
.rebase_master
:
462 rebase_tree(samba_master
)
464 email_failure(-1, 'rebase', 'rebase', 'rebase', 'rebase on master failed')
466 blist
= buildlist(tasks
, args
)
469 (status
, failed_task
, failed_stage
, failed_tag
, errstr
) = blist
.run()
470 if status
!= 0 or errstr
!= "retry":
477 cleanup_list
.append(gitroot
+ "/autobuild.pid")
481 print("waiting for tail to flush")
486 if options
.passcmd
is not None:
487 print("Running passcmd: %s" % options
.passcmd
)
488 run_cmd(options
.passcmd
, dir=test_master
)
489 if options
.pushto
is not None:
490 push_to(options
.pushto
)
491 elif options
.push_master
:
492 push_to(samba_master_ssh
)
494 blist
.tarlogs("logs.tar.gz")
495 print("Logs in logs.tar.gz")
496 if options
.always_email
:
503 # something failed, gather a tar of the logs
504 blist
.tarlogs("logs.tar.gz")
506 if options
.email
is not None:
507 email_failure(status
, failed_task
, failed_stage
, failed_tag
, errstr
)
511 print("Logs in logs.tar.gz")