2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>
4 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 selftest - Samba test runner
29 selftest [--srcdir=DIR] [--builddir=DIR] [--exeext=EXT][--target=samba4|samba3|win|kvm] [--socket-wrapper] [--quick] [--exclude=FILE] [--include=FILE] [--one] [--prefix=prefix] [--testlist=FILE] [TESTS]
33 A simple test runner. TESTS is a regular expression with tests to run.
41 Show list of available options.
47 =item I<--builddir=DIR>
57 Change directory to run tests in. Default is 'st'.
59 =item I<--target samba4|samba3|win|kvm>
61 Specify test target against which to run. Default is 'samba4'.
65 Run only a limited number of tests. Intended to run in about 30 seconds on
66 moderately recent systems.
68 =item I<--socket-wrapper>
70 Use socket wrapper library for communication with server. Only works
71 when the server is running locally.
73 Will prevent TCP and UDP ports being opened on the local host but
74 (transparently) redirects these calls to use unix domain sockets.
78 Specify a file containing a list of tests that should be skipped. Possible
79 candidates are tests that segfault the server, flip or don't end.
83 Specify a file containing a list of tests that should be run. Same format
84 as the --exclude flag.
86 Not includes specified means all tests will be run.
90 Abort as soon as one test fails.
94 Load a list of tests from the specified location.
102 =item I<SMBD_VALGRIND>
104 =item I<TORTURE_MAXTIME>
116 selftest is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
126 use FindBin qw($RealBin $Script);
128 use File::Temp qw(tempfile);
131 use Cwd qw(abs_path);
140 use Time::HiRes qw(time);
144 my $opt_target = "samba4";
146 my $opt_socket_wrapper = 0;
147 my $opt_socket_wrapper_pcap = undef;
148 my $opt_socket_wrapper_keep_pcap = undef;
150 my @opt_exclude = ();
151 my @opt_include = ();
153 my $opt_image = undef;
156 my $opt_analyse_cmd = undef;
157 my $opt_resetup_env = undef;
158 my $opt_bindir = undef;
159 my $opt_load_list = undef;
172 my ($list, $fullname) = @_;
175 if ($fullname =~ /$$_[0]/) {
176 return ($$_[1]) if ($$_[1]);
188 return find_in_list(\@excludes, $name);
197 return unless ($opt_socket_wrapper_pcap);
198 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
201 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
203 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
205 SocketWrapper::setup_pcap($pcap_file);
212 my ($pcap_file, $exitcode) = @_;
214 return unless ($opt_socket_wrapper_pcap);
215 return if ($opt_socket_wrapper_keep_pcap);
216 return unless ($exitcode == 0);
217 return unless defined($pcap_file);
222 # expand strings from %ENV
223 sub expand_environment_strings($)
226 # we use a reverse sort so we do the longer ones first
227 foreach my $k (sort { $b cmp $a } keys %ENV) {
228 $s =~ s/\$$k/$ENV{$k}/g;
233 sub run_testsuite($$$$$)
235 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
236 my $pcap_file = setup_pcap($name);
238 Subunit::start_testsuite($name);
239 Subunit::progress_push();
240 Subunit::report_time(time());
242 Subunit::report_time(time());
243 Subunit::progress_pop();
246 Subunit::progress_pop();
247 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
250 Subunit::end_testsuite($name, "error",
251 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
255 my $exitcode = $? >> 8;
257 my $envlog = getlog_env($envname);
259 print "envlog: $envlog\n";
262 print "command: $cmd\n";
263 printf "expanded command: %s\n", expand_environment_strings($cmd);
265 if ($exitcode == 0) {
266 Subunit::end_testsuite($name, "success");
268 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
271 cleanup_pcap($pcap_file, $exitcode);
273 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
274 print "PCAP FILE: $pcap_file\n";
277 if ($exitcode != 0) {
278 exit(1) if ($opt_one);
286 print "Samba test runner
287 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
288 Copyright (C) Stefan Metzmacher <metze\@samba.org>
290 Usage: $Script [OPTIONS] TESTNAME-REGEX
293 --help this help page
294 --target=samba[34]|win|kvm Samba version to target
295 --testlist=FILE file to read available tests from
298 --prefix=DIR prefix to run tests in [st]
299 --srcdir=DIR source directory [.]
300 --builddir=DIR output directory [.]
301 --exeext=EXT executable extention []
304 --socket-wrapper-pcap save traffic to pcap directories
305 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
307 --socket-wrapper enable socket wrapper
308 --bindir=PATH path to target binaries
311 --ldap=openldap|fedora-ds back samba onto specified ldap server
314 --image=PATH path to KVM image
317 --quick run quick overall test
318 --one abort when the first test fails
320 --analyse-cmd CMD command to run after each test
325 my $result = GetOptions (
326 'help|h|?' => \$opt_help,
327 'target=s' => \$opt_target,
328 'prefix=s' => \$prefix,
329 'socket-wrapper' => \$opt_socket_wrapper,
330 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
331 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
332 'quick' => \$opt_quick,
334 'exclude=s' => \@opt_exclude,
335 'include=s' => \@opt_include,
336 'srcdir=s' => \$srcdir,
337 'builddir=s' => \$builddir,
338 'exeext=s' => \$exeext,
339 'verbose' => \$opt_verbose,
340 'testenv' => \$opt_testenv,
342 'analyse-cmd=s' => \$opt_analyse_cmd,
343 'resetup-environment' => \$opt_resetup_env,
344 'bindir:s' => \$opt_bindir,
345 'image=s' => \$opt_image,
346 'testlist=s' => \@testlists,
347 'load-list=s' => \$opt_load_list,
350 exit(1) if (not $result);
352 ShowHelp() if ($opt_help);
354 # we want unbuffered output
359 # quick hack to disable rpc validation when using valgrind - its way too slow
360 unless (defined($ENV{VALGRIND})) {
361 $ENV{VALIDATE} = "validate";
362 $ENV{MALLOC_CHECK_} = 2;
365 # make all our python scripts unbuffered
366 $ENV{PYTHONUNBUFFERED} = 1;
368 my $bindir = ($opt_bindir or "$builddir/bin");
369 my $bindir_abs = abs_path($bindir);
371 # Backwards compatibility:
372 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
373 if (defined($ENV{FEDORA_DS_ROOT})) {
380 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
383 $torture_maxtime *= 2;
390 die("using an empty prefix isn't allowed") unless $prefix ne "";
392 #Ensure we have the test prefix around
393 mkdir($prefix, 0777) unless -d $prefix;
395 my $prefix_abs = abs_path($prefix);
396 my $srcdir_abs = abs_path($srcdir);
397 my $builddir_abs = abs_path($builddir);
399 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
400 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
402 $ENV{PREFIX} = $prefix;
403 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
404 $ENV{PREFIX_ABS} = $prefix_abs;
405 $ENV{SRCDIR} = $srcdir;
406 $ENV{SRCDIR_ABS} = $srcdir_abs;
407 $ENV{BUILDDIR} = $builddir;
408 $ENV{BUILDDIR_ABS} = $builddir_abs;
409 $ENV{EXEEXT} = $exeext;
411 my $tls_enabled = not $opt_quick;
412 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
413 $ENV{LDB_MODULES_PATH} = "$bindir_abs/modules/ldb";
414 $ENV{LD_SAMBA_MODULE_PATH} = "$bindir_abs/modules";
415 sub prefix_pathvar($$)
417 my ($name, $newpath) = @_;
418 if (defined($ENV{$name})) {
419 $ENV{$name} = "$newpath:$ENV{$name}";
421 $ENV{$name} = $newpath;
424 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
425 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
427 if ($opt_socket_wrapper_keep_pcap) {
428 # Socket wrapper keep pcap implies socket wrapper pcap
429 $opt_socket_wrapper_pcap = 1;
432 if ($opt_socket_wrapper_pcap) {
433 # Socket wrapper pcap implies socket wrapper
434 $opt_socket_wrapper = 1;
437 my $socket_wrapper_dir;
438 if ($opt_socket_wrapper) {
439 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
440 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
443 print "WARNING: Not using socket wrapper, but also not running as root. Will not be able to listen on proper ports\n";
448 my $testenv_default = "none";
450 if ($opt_target eq "samba4") {
451 $testenv_default = "all";
452 require target::Samba4;
453 $target = new Samba4($bindir, $ldap, "$srcdir/setup", $exeext);
454 } elsif ($opt_target eq "samba3") {
455 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
456 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
458 $testenv_default = "member";
459 require target::Samba3;
460 $target = new Samba3($bindir);
461 } elsif ($opt_target eq "win") {
462 die("Windows tests will not run with socket wrapper enabled.")
463 if ($opt_socket_wrapper);
464 $testenv_default = "dc";
465 require target::Windows;
466 $target = new Windows();
467 } elsif ($opt_target eq "kvm") {
468 die("Kvm tests will not run with socket wrapper enabled.")
469 if ($opt_socket_wrapper);
471 die("No image specified") unless ($opt_image);
472 $target = new Kvm($opt_image, undef);
476 # Start a Virtual Distributed Ethernet Switch
477 # Returns the pid of the switch.
479 sub start_vde_switch($)
483 system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
485 open(PID, "$path/vde.pid");
493 # Stop a Virtual Distributed Ethernet Switch
494 sub stop_vde_switch($)
500 sub read_test_regexes($)
504 open(LF, "<$name") or die("unable to read $name: $!");
508 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
509 push (@ret, [$1, $4]);
511 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
512 push (@ret, [$_, undef]);
519 foreach (@opt_exclude) {
520 push (@excludes, read_test_regexes($_));
523 foreach (@opt_include) {
524 push (@includes, read_test_regexes($_));
527 my $interfaces = join(',', ("127.0.0.11/8",
534 my $conffile = "$prefix_abs/client/client.conf";
535 $ENV{SMB_CONF_PATH} = $conffile;
537 sub write_clientconf($$)
539 my ($conffile, $vars) = @_;
541 mkdir("$prefix/client", 0777) unless -d "$prefix/client";
543 if ( -d "$prefix/client/private" ) {
544 unlink <$prefix/client/private/*>;
546 mkdir("$prefix/client/private", 0777);
549 if ( -d "$prefix/client/lockdir" ) {
550 unlink <$prefix/client/lockdir/*>;
552 mkdir("$prefix/client/lockdir", 0777);
555 if ( -d "$prefix_abs/client/ncalrpcdir" ) {
556 unlink <$prefix/client/ncalrpcdir/*>;
558 mkdir("$prefix/client/ncalrpcdir", 0777);
561 open(CF, ">$conffile");
562 print CF "[global]\n";
563 if (defined($ENV{VALGRIND})) {
564 print CF "\ticonv:native = true\n";
566 print CF "\ticonv:native = false\n";
568 print CF "\tnetbios name = client\n";
569 if (defined($vars->{DOMAIN})) {
570 print CF "\tworkgroup = $vars->{DOMAIN}\n";
572 if (defined($vars->{REALM})) {
573 print CF "\trealm = $vars->{REALM}\n";
575 if ($opt_socket_wrapper) {
576 print CF "\tinterfaces = $interfaces\n";
579 private dir = $prefix_abs/client/private
580 lock dir = $prefix_abs/client/lockdir
581 ncalrpc dir = $prefix_abs/client/ncalrpcdir
582 name resolve order = bcast file
583 panic action = $RealBin/gdb_backtrace \%PID\% \%PROG\%
585 notify:inotify = false
587 system:anonymous = true
588 client lanman auth = Yes
590 torture:basedir = $prefix_abs/client
591 #We don't want to pass our self-tests if the PAC code is wrong
592 gensec:require_pac = true
593 modules dir = $ENV{LD_SAMBA_MODULE_PATH}
594 setup directory = ./setup
595 resolv:host file = $prefix_abs/dns_host_file
602 my $testsdir = "$srcdir/selftest";
604 sub should_run_test($)
610 for (my $i=0; $i <= $#tests; $i++) {
611 if ($name =~ /$tests[$i]/i) {
623 open(IN, $filename) or die("Unable to open $filename: $!");
626 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
627 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
628 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
635 if (should_run_test($name) == 1) {
636 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
642 close(IN) or die("Error creating recipe");
646 if ($#testlists == -1) {
647 die("No testlists specified");
650 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
651 if ($opt_socket_wrapper) {
652 $ENV{SELFTEST_INTERFACES} = $interfaces;
654 $ENV{SELFTEST_INTERFACES} = "";
657 $ENV{SELFTEST_VERBOSE} = "1";
659 $ENV{SELFTEST_VERBOSE} = "";
662 $ENV{SELFTEST_QUICK} = "1";
664 $ENV{SELFTEST_QUICK} = "";
666 $ENV{SELFTEST_TARGET} = $opt_target;
667 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
670 foreach my $fn (@testlists) {
671 foreach (read_testlist($fn)) {
673 next if (@includes and not defined(find_in_list(\@includes, $name)));
674 push (@available, $_);
678 my $restricted = undef;
679 my $restricted_used = {};
681 if ($opt_load_list) {
683 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
684 while (<LOAD_LIST>) {
686 push (@$restricted, $_);
691 Subunit::progress($#available+1);
692 Subunit::report_time(time());
694 my $individual_tests = undef;
695 $individual_tests = {};
697 foreach my $testsuite (@available) {
698 my $name = $$testsuite[0];
699 my $skipreason = skip($name);
700 if (defined($skipreason)) {
701 Subunit::skip_testsuite($name, $skipreason);
702 } elsif (defined($restricted)) {
703 # Find the testsuite for this test
705 foreach my $r (@$restricted) {
707 $individual_tests->{$name} = [];
709 $restricted_used->{$r} = 1;
710 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
711 push(@{$individual_tests->{$name}}, substr($r, length($name)+1));
713 $restricted_used->{$r} = 1;
716 push(@todo, $testsuite) if ($match);
718 push(@todo, $testsuite);
722 if (defined($restricted)) {
723 foreach (@$restricted) {
724 unless (defined($restricted_used->{$_})) {
725 print "No test or testsuite found matching $_\n";
728 } elsif ($#todo == -1) {
729 print STDERR "No tests to run\n";
733 my $suitestotal = $#todo + 1;
737 my %running_envs = ();
739 sub get_running_env($)
747 return $running_envs{$envname};
750 my @exported_envvars = (
755 # domain controller stuff
764 "MEMBER_NETBIOSNAME",
765 "MEMBER_NETBIOSALIAS",
767 # rpc proxy controller stuff
769 "RPC_PROXY_SERVER_IP",
770 "RPC_PROXY_NETBIOSNAME",
771 "RPC_PROXY_NETBIOSALIAS",
773 # domain controller stuff for Vampired DC
775 "VAMPIRE_DC_SERVER_IP",
776 "VAMPIRE_DC_NETBIOSNAME",
777 "VAMPIRE_DC_NETBIOSALIAS",
794 "WINBINDD_SOCKET_DIR",
795 "WINBINDD_PRIV_PIPE_DIR",
799 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
801 teardown_env($_) foreach(keys %running_envs);
802 die("Received signal $signame");
809 my $testenv_vars = undef;
815 $option =~ s/^[^:]*//;
818 $option = "client" if $option eq "";
820 if ($envname eq "none") {
822 } elsif (defined(get_running_env($envname))) {
823 $testenv_vars = get_running_env($envname);
824 if (not $target->check_env($testenv_vars)) {
825 print $target->getlog_env($testenv_vars);
826 $testenv_vars = undef;
829 $testenv_vars = $target->setup_env($envname, $prefix);
832 return undef unless defined($testenv_vars);
834 $running_envs{$envname} = $testenv_vars;
836 if ($option eq "local") {
837 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
838 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
839 } elsif ($option eq "client") {
840 SocketWrapper::set_default_iface(11);
841 write_clientconf($conffile, $testenv_vars);
842 $ENV{SMB_CONF_PATH} = $conffile;
844 die("Unknown option[$option] for envname[$envname]");
847 foreach (@exported_envvars) {
848 if (defined($testenv_vars->{$_})) {
849 $ENV{$_} = $testenv_vars->{$_};
855 return $testenv_vars;
858 sub exported_envvars_str($)
860 my ($testenv_vars) = @_;
863 foreach (@exported_envvars) {
864 next unless defined($testenv_vars->{$_});
865 $out .= $_."=".$testenv_vars->{$_}."\n";
874 return "" if ($envname eq "none");
875 return $target->getlog_env(get_running_env($envname));
881 return 1 if ($envname eq "none");
882 return $target->check_env(get_running_env($envname));
888 return if ($envname eq "none");
889 $target->teardown_env(get_running_env($envname));
890 delete $running_envs{$envname};
893 # This 'global' file needs to be empty when we start
894 unlink("$prefix_abs/dns_host_file");
897 my $testenv_name = $ENV{SELFTEST_TESTENV};
898 $testenv_name = $testenv_default unless defined($testenv_name);
900 my $testenv_vars = setup_env($testenv_name);
902 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
904 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
906 my $envvarstr = exported_envvars_str($testenv_vars);
908 my $term = ($ENV{TERMINAL} or "xterm");
909 system("$term -e 'echo -e \"
910 Welcome to the Samba4 Test environment '$testenv_name'
912 This matches the client environment used in make test
913 server is pid `cat \$PIDDIR/samba.pid`
915 Some useful environment variables:
916 TORTURE_OPTIONS=\$TORTURE_OPTIONS
917 SMB_CONF_PATH=\$SMB_CONF_PATH
920 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
921 teardown_env($testenv_name);
927 my $envname = $$_[1];
929 my $envvars = setup_env($envname);
930 if (not defined($envvars)) {
931 Subunit::start_testsuite($name);
932 Subunit::end_testsuite($name, "error",
933 "unable to set up environment $envname");
937 # Generate a file with the individual tests to run, if the
938 # test runner for this test suite supports it.
939 if ($individual_tests and $individual_tests->{$name}) {
941 my ($fh, $listid_file) = tempfile(UNLINK => 0);
942 foreach my $test (@{$individual_tests->{$name}}) {
945 $cmd .= " --load-list=$listid_file";
947 $cmd =~ s/\s+[^\s]+\s*$//;
948 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
952 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
954 if (defined($opt_analyse_cmd)) {
955 system("$opt_analyse_cmd \"$name\"");
958 teardown_env($envname) if ($opt_resetup_env);
964 teardown_env($_) foreach (keys %running_envs);
968 # if there were any valgrind failures, show them
969 foreach (<$prefix/valgrind.log*>) {
971 print "VALGRIND FAILURE\n";