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
11 from distutils
.sysconfig
import get_python_lib
13 # This speeds up testing remarkably.
14 os
.environ
['TDB_NO_FSYNC'] = '1'
25 "talloc" : "lib/talloc",
26 "replace" : "lib/replace",
27 "tevent" : "lib/tevent",
34 defaulttasks
= [ "ctdb", "samba", "samba-ctdb", "samba-libs", "ldb", "tdb", "talloc", "replace", "tevent", "pidl" ]
37 "ctdb" : [ ("random-sleep", "../script/random-sleep.sh 60 600", "text/plain"),
38 ("configure", "./configure ${PREFIX}", "text/plain"),
39 ("make", "make all", "text/plain"),
40 ("install", "make install", "text/plain"),
41 ("test", "make autotest", "text/plain"),
42 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
43 ("clean", "make clean", "text/plain") ],
45 # We have 'test' before 'install' because, 'test' should work without 'install'
46 "samba" : [ ("configure", "./configure.developer --picky-developer ${PREFIX} --with-selftest-prefix=./bin/ab --with-profiling-data", "text/plain"),
47 ("make", "make -j", "text/plain"),
48 ("test", "make test FAIL_IMMEDIATELY=1", "text/plain"),
49 ("install", "make install", "text/plain"),
50 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
51 ("clean", "make clean", "text/plain") ],
53 "samba-ctdb" : [ ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
55 # make sure we have tdb around:
56 ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
57 ("tdb-make", "cd lib/tdb && make", "text/plain"),
58 ("tdb-install", "cd lib/tdb && make install", "text/plain"),
61 # build samba with cluster support (also building ctdb):
62 ("samba-configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=${PREFIX_DIR}/lib/pkgconfig:${PKG_CONFIG_PATH} ./configure.developer --picky-developer ${PREFIX} --with-selftest-prefix=./bin/ab --with-cluster-support --bundled-libraries=!tdb", "text/plain"),
63 ("samba-make", "make", "text/plain"),
64 ("samba-check", "./bin/smbd -b | grep CLUSTER_SUPPORT", "text/plain"),
65 ("samba-install", "make install", "text/plain"),
66 ("ctdb-check", "test -e ${PREFIX_DIR}/sbin/ctdbd", "text/plain"),
69 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
70 ("clean", "make clean", "text/plain"),
71 ("ctdb-clean", "cd ./ctdb && make clean", "text/plain") ],
74 ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
75 ("talloc-configure", "cd lib/talloc && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
76 ("talloc-make", "cd lib/talloc && make", "text/plain"),
77 ("talloc-install", "cd lib/talloc && make install", "text/plain"),
79 ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
80 ("tdb-make", "cd lib/tdb && make", "text/plain"),
81 ("tdb-install", "cd lib/tdb && make install", "text/plain"),
83 ("tevent-configure", "cd lib/tevent && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
84 ("tevent-make", "cd lib/tevent && make", "text/plain"),
85 ("tevent-install", "cd lib/tevent && make install", "text/plain"),
87 ("ldb-configure", "cd lib/ldb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
88 ("ldb-make", "cd lib/ldb && make", "text/plain"),
89 ("ldb-install", "cd lib/ldb && make install", "text/plain"),
91 ("configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=!talloc,!tdb,!pytdb,!ldb,!pyldb,!tevent,!pytevent --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
92 ("make", "make", "text/plain"),
93 ("install", "make install", "text/plain"),
94 ("dist", "make dist", "text/plain")],
97 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
98 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
99 ("make", "make", "text/plain"),
100 ("install", "make install", "text/plain"),
101 ("test", "make test", "text/plain"),
102 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
103 ("distcheck", "make distcheck", "text/plain"),
104 ("clean", "make clean", "text/plain") ],
107 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
108 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
109 ("make", "make", "text/plain"),
110 ("install", "make install", "text/plain"),
111 ("test", "make test", "text/plain"),
112 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
113 ("distcheck", "make distcheck", "text/plain"),
114 ("clean", "make clean", "text/plain") ],
117 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
118 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
119 ("make", "make", "text/plain"),
120 ("install", "make install", "text/plain"),
121 ("test", "make test", "text/plain"),
122 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
123 ("distcheck", "make distcheck", "text/plain"),
124 ("clean", "make clean", "text/plain") ],
127 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
128 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
129 ("make", "make", "text/plain"),
130 ("install", "make install", "text/plain"),
131 ("test", "make test", "text/plain"),
132 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
133 ("distcheck", "make distcheck", "text/plain"),
134 ("clean", "make clean", "text/plain") ],
137 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
138 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
139 ("make", "make", "text/plain"),
140 ("install", "make install", "text/plain"),
141 ("test", "make test", "text/plain"),
142 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
143 ("distcheck", "make distcheck", "text/plain"),
144 ("clean", "make clean", "text/plain") ],
147 ("random-sleep", "../script/random-sleep.sh 60 600", "text/plain"),
148 ("configure", "perl Makefile.PL PREFIX=${PREFIX_DIR}", "text/plain"),
149 ("touch", "touch *.yp", "text/plain"),
150 ("make", "make", "text/plain"),
151 ("test", "make test", "text/plain"),
152 ("install", "make install", "text/plain"),
153 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
154 ("clean", "make clean", "text/plain") ],
156 # these are useful for debugging autobuild
157 'pass' : [ ("pass", 'echo passing && /bin/true', "text/plain") ],
158 'fail' : [ ("fail", 'echo failing && /bin/false', "text/plain") ]
161 def run_cmd(cmd
, dir=".", show
=None, output
=False, checkfail
=True):
163 show
= options
.verbose
165 print("Running: '%s' in '%s'" % (cmd
, dir))
167 return Popen([cmd
], shell
=True, stdout
=PIPE
, cwd
=dir).communicate()[0]
169 return check_call(cmd
, shell
=True, cwd
=dir)
171 return call(cmd
, shell
=True, cwd
=dir)
174 class builder(object):
175 '''handle build of one directory'''
177 def __init__(self
, name
, sequence
):
179 self
.dir = builddirs
[name
]
181 self
.tag
= self
.name
.replace('/', '_')
182 self
.sequence
= sequence
184 self
.stdout_path
= "%s/%s.stdout" % (gitroot
, self
.tag
)
185 self
.stderr_path
= "%s/%s.stderr" % (gitroot
, self
.tag
)
187 print("stdout for %s in %s" % (self
.name
, self
.stdout_path
))
188 print("stderr for %s in %s" % (self
.name
, self
.stderr_path
))
189 run_cmd("rm -f %s %s" % (self
.stdout_path
, self
.stderr_path
))
190 self
.stdout
= open(self
.stdout_path
, 'w')
191 self
.stderr
= open(self
.stderr_path
, 'w')
192 self
.stdin
= open("/dev/null", 'r')
193 self
.sdir
= "%s/%s" % (testbase
, self
.tag
)
194 self
.prefix
= "%s/prefix/%s" % (testbase
, self
.tag
)
195 run_cmd("rm -rf %s" % self
.sdir
)
196 cleanup_list
.append(self
.sdir
)
197 cleanup_list
.append(self
.prefix
)
198 os
.makedirs(self
.sdir
)
199 run_cmd("rm -rf %s" % self
.sdir
)
200 run_cmd("git clone --shared %s %s" % (test_master
, self
.sdir
), dir=test_master
, show
=True)
203 def start_next(self
):
204 if self
.next
== len(self
.sequence
):
205 print '%s: Completed OK' % self
.name
208 (self
.stage
, self
.cmd
, self
.output_mime_type
) = self
.sequence
[self
.next
]
209 self
.cmd
= self
.cmd
.replace("${PYTHON_PREFIX}", get_python_lib(standard_lib
=1, prefix
=self
.prefix
))
210 self
.cmd
= self
.cmd
.replace("${PREFIX}", "--prefix=%s" % self
.prefix
)
211 self
.cmd
= self
.cmd
.replace("${PREFIX_DIR}", "%s" % self
.prefix
)
212 # if self.output_mime_type == "text/x-subunit":
213 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
214 print '%s: [%s] Running %s' % (self
.name
, self
.stage
, self
.cmd
)
216 os
.chdir("%s/%s" % (self
.sdir
, self
.dir))
217 self
.proc
= Popen(self
.cmd
, shell
=True,
218 stdout
=self
.stdout
, stderr
=self
.stderr
, stdin
=self
.stdin
)
223 class buildlist(object):
224 '''handle build of multiple directories'''
226 def __init__(self
, tasklist
, tasknames
, rebase_url
, rebase_branch
="master"):
229 self
.tail_proc
= None
232 tasknames
= defaulttasks
234 b
= builder(n
, tasks
[n
])
237 rebase_remote
= "rebaseon"
238 retry_task
= [ ("retry",
240 git remote add -t %s %s %s
244 git describe %s/%s > old_remote_branch.desc
246 git describe %s/%s > remote_branch.desc
247 diff old_remote_branch.desc remote_branch.desc
250 rebase_branch
, rebase_remote
, rebase_url
,
252 rebase_remote
, rebase_branch
,
254 rebase_remote
, rebase_branch
258 self
.retry
= builder('retry', retry_task
)
259 self
.need_retry
= False
262 if self
.tail_proc
is not None:
263 self
.tail_proc
.terminate()
264 self
.tail_proc
.wait()
265 self
.tail_proc
= None
266 if self
.retry
is not None:
267 self
.retry
.proc
.terminate()
268 self
.retry
.proc
.wait()
271 if b
.proc
is not None:
272 run_cmd("killbysubdir %s > /dev/null 2>&1" % b
.sdir
, checkfail
=False)
284 b
.status
= b
.proc
.poll()
290 ret
= self
.retry
.proc
.poll()
292 self
.need_retry
= True
302 if options
.retry
and self
.need_retry
:
304 print("retry needed")
305 return (0, None, None, None, "retry")
308 if os
.WIFSIGNALED(b
.status
) or os
.WEXITSTATUS(b
.status
) != 0:
310 return (b
.status
, b
.name
, b
.stage
, b
.tag
, "%s: [%s] failed '%s' with status %d" % (b
.name
, b
.stage
, b
.cmd
, b
.status
))
313 return (0, None, None, None, "All OK")
315 def tarlogs(self
, fname
):
316 tar
= tarfile
.open(fname
, "w:gz")
318 tar
.add(b
.stdout_path
, arcname
="%s.stdout" % b
.tag
)
319 tar
.add(b
.stderr_path
, arcname
="%s.stderr" % b
.tag
)
320 if os
.path
.exists("autobuild.log"):
321 tar
.add("autobuild.log")
324 def remove_logs(self
):
326 os
.unlink(b
.stdout_path
)
327 os
.unlink(b
.stderr_path
)
329 def start_tail(self
):
331 cmd
= "tail -f *.stdout *.stderr"
333 self
.tail_proc
= Popen(cmd
, shell
=True)
338 if options
.nocleanup
:
340 print("Cleaning up ....")
341 for d
in cleanup_list
:
342 run_cmd("rm -rf %s" % d
)
346 '''get to the top of the git repo'''
349 if os
.path
.isdir(os
.path
.join(p
, ".git")):
351 p
= os
.path
.abspath(os
.path
.join(p
, '..'))
355 def daemonize(logfile
):
357 if pid
== 0: # Parent
360 if pid
!= 0: # Actual daemon
365 import resource
# Resource usage information.
366 maxfd
= resource
.getrlimit(resource
.RLIMIT_NOFILE
)[1]
367 if maxfd
== resource
.RLIM_INFINITY
:
368 maxfd
= 1024 # Rough guess at maximum number of open file descriptors.
369 for fd
in range(0, maxfd
):
374 os
.open(logfile
, os
.O_RDWR | os
.O_CREAT
)
378 def write_pidfile(fname
):
379 '''write a pid file, cleanup on exit'''
380 f
= open(fname
, mode
='w')
381 f
.write("%u\n" % os
.getpid())
385 def rebase_tree(rebase_url
, rebase_branch
= "master"):
386 rebase_remote
= "rebaseon"
387 print("Rebasing on %s" % rebase_url
)
388 run_cmd("git describe HEAD", show
=True, dir=test_master
)
389 run_cmd("git remote add -t %s %s %s" %
390 (rebase_branch
, rebase_remote
, rebase_url
),
391 show
=True, dir=test_master
)
392 run_cmd("git fetch %s" % rebase_remote
, show
=True, dir=test_master
)
393 if options
.fix_whitespace
:
394 run_cmd("git rebase --force-rebase --whitespace=fix %s/%s" %
395 (rebase_remote
, rebase_branch
),
396 show
=True, dir=test_master
)
398 run_cmd("git rebase --force-rebase %s/%s" %
399 (rebase_remote
, rebase_branch
),
400 show
=True, dir=test_master
)
401 diff
= run_cmd("git --no-pager diff HEAD %s/%s" %
402 (rebase_remote
, rebase_branch
),
403 dir=test_master
, output
=True)
405 print("No differences between HEAD and %s/%s - exiting" %
406 (rebase_remote
, rebase_branch
))
408 run_cmd("git describe %s/%s" %
409 (rebase_remote
, rebase_branch
),
410 show
=True, dir=test_master
)
411 run_cmd("git describe HEAD", show
=True, dir=test_master
)
412 run_cmd("git --no-pager diff --stat HEAD %s/%s" %
413 (rebase_remote
, rebase_branch
),
414 show
=True, dir=test_master
)
416 def push_to(push_url
, push_branch
= "master"):
417 push_remote
= "pushto"
418 print("Pushing to %s" % push_url
)
420 run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master
)
421 run_cmd("git commit --amend -c HEAD", dir=test_master
)
422 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
423 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
424 run_cmd("git remote add -t %s %s %s" %
425 (push_branch
, push_remote
, push_url
),
426 show
=True, dir=test_master
)
427 run_cmd("git push %s +HEAD:%s" %
428 (push_remote
, push_branch
),
429 show
=True, dir=test_master
)
431 def_testbase
= os
.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os
.getenv('USER'))
433 gitroot
= find_git_root()
435 raise Exception("Failed to find git root")
437 parser
= OptionParser()
438 parser
.add_option("", "--tail", help="show output while running", default
=False, action
="store_true")
439 parser
.add_option("", "--keeplogs", help="keep logs", default
=False, action
="store_true")
440 parser
.add_option("", "--nocleanup", help="don't remove test tree", default
=False, action
="store_true")
441 parser
.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase
,
442 default
=def_testbase
)
443 parser
.add_option("", "--passcmd", help="command to run on success", default
=None)
444 parser
.add_option("", "--verbose", help="show all commands as they are run",
445 default
=False, action
="store_true")
446 parser
.add_option("", "--rebase", help="rebase on the given tree before testing",
447 default
=None, type='str')
448 parser
.add_option("", "--pushto", help="push to a git url on success",
449 default
=None, type='str')
450 parser
.add_option("", "--mark", help="add a Tested-By signoff before pushing",
451 default
=False, action
="store_true")
452 parser
.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
453 default
=False, action
="store_true")
454 parser
.add_option("", "--retry", help="automatically retry if master changes",
455 default
=False, action
="store_true")
456 parser
.add_option("", "--email", help="send email to the given address on failure",
457 type='str', default
=None)
458 parser
.add_option("", "--always-email", help="always send email, even on success",
460 parser
.add_option("", "--daemon", help="daemonize after initial setup",
462 parser
.add_option("", "--branch", help="the branch to work on (default=master)",
463 default
="master", type='str')
464 parser
.add_option("", "--log-base", help="location where the logs can be found (default=cwd)",
465 default
=gitroot
, type='str')
467 def email_failure(status
, failed_task
, failed_stage
, failed_tag
, errstr
, log_base
=None):
468 '''send an email to options.email about the failure'''
469 user
= os
.getenv("USER")
475 Your autobuild failed when trying to test %s with the following error:
478 the autobuild has been abandoned. Please fix the error and resubmit.
480 A summary of the autobuild process is here:
483 ''' % (failed_task
, errstr
, log_base
)
485 if failed_task
!= 'rebase':
487 You can see logs of the failed task here:
492 or you can get full logs of all tasks in this job here:
496 The top commit for the tree that was built was:
500 ''' % (log_base
, failed_tag
, log_base
, failed_tag
, log_base
, top_commit_msg
)
502 msg
['Subject'] = 'autobuild failure for task %s during %s' % (failed_task
, failed_stage
)
503 msg
['From'] = 'autobuild@samba.org'
504 msg
['To'] = options
.email
508 s
.sendmail(msg
['From'], [msg
['To']], msg
.as_string())
511 def email_success(log_base
=None):
512 '''send an email to options.email about a successful build'''
513 user
= os
.getenv("USER")
519 Your autobuild has succeeded.
526 you can get full logs of all tasks in this job here:
533 The top commit for the tree that was built was:
539 msg
['Subject'] = 'autobuild success'
540 msg
['From'] = 'autobuild@samba.org'
541 msg
['To'] = options
.email
545 s
.sendmail(msg
['From'], [msg
['To']], msg
.as_string())
549 (options
, args
) = parser
.parse_args()
552 if options
.rebase
is None:
553 raise Exception('You can only use --retry if you also rebase')
555 testbase
= "%s/b%u" % (options
.testbase
, os
.getpid())
556 test_master
= "%s/master" % testbase
558 # get the top commit message, for emails
559 top_commit_msg
= run_cmd("git log -1", dir=gitroot
, output
=True)
562 os
.makedirs(testbase
)
563 except Exception, reason
:
564 raise Exception("Unable to create %s : %s" % (testbase
, reason
))
565 cleanup_list
.append(testbase
)
568 logfile
= os
.path
.join(testbase
, "log")
569 print "Forking into the background, writing progress to %s" % logfile
572 write_pidfile(gitroot
+ "/autobuild.pid")
576 run_cmd("rm -rf %s" % test_master
)
577 cleanup_list
.append(test_master
)
578 run_cmd("git clone --shared %s %s" % (gitroot
, test_master
), show
=True, dir=gitroot
)
585 if options
.rebase
is not None:
586 rebase_tree(options
.rebase
, rebase_branch
=options
.branch
)
588 cleanup_list
.append(gitroot
+ "/autobuild.pid")
590 email_failure(-1, 'rebase', 'rebase', 'rebase',
591 'rebase on %s failed' % options
.branch
,
592 log_base
=options
.log_base
)
594 blist
= buildlist(tasks
, args
, options
.rebase
, rebase_branch
=options
.branch
)
597 (status
, failed_task
, failed_stage
, failed_tag
, errstr
) = blist
.run()
598 if status
!= 0 or errstr
!= "retry":
605 cleanup_list
.append(gitroot
+ "/autobuild.pid")
609 print("waiting for tail to flush")
614 if options
.passcmd
is not None:
615 print("Running passcmd: %s" % options
.passcmd
)
616 run_cmd(options
.passcmd
, dir=test_master
)
617 if options
.pushto
is not None:
618 push_to(options
.pushto
, push_branch
=options
.branch
)
620 blist
.tarlogs("logs.tar.gz")
621 print("Logs in logs.tar.gz")
622 if options
.always_email
:
623 email_success(log_base
=options
.log_base
)
629 # something failed, gather a tar of the logs
630 blist
.tarlogs("logs.tar.gz")
632 if options
.email
is not None:
633 email_failure(status
, failed_task
, failed_stage
, failed_tag
, errstr
, log_base
=options
.log_base
)
637 print("Logs in logs.tar.gz")