2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2010 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/>.
21 use FindBin
qw($RealBin $Script);
23 use File::Temp qw(tempfile);
33 Time
::HiRes
->import("time");
36 print "You don't have Time::Hires installed !\n";
40 my $opt_target = "samba";
42 my $opt_socket_wrapper = 0;
43 my $opt_socket_wrapper_pcap = undef;
44 my $opt_socket_wrapper_keep_pcap = undef;
51 my $opt_resetup_env = undef;
52 my $opt_binary_mapping = "";
53 my $opt_load_list = undef;
65 print STDERR
"Exiting early because of SIGPIPE.\n";
69 $SIG{PIPE
} = \
&pipe_handler
;
73 my ($list, $fullname) = @_;
76 if ($fullname =~ /$$_[0]/) {
77 return ($$_[1]) if ($$_[1]);
89 return find_in_list
(\
@excludes, $name);
98 return unless ($opt_socket_wrapper_pcap);
99 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR
});
102 $fname =~ s
%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\
-]%_%g;
104 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
106 SocketWrapper
::setup_pcap
($pcap_file);
113 my ($pcap_file, $exitcode) = @_;
115 return unless ($opt_socket_wrapper_pcap);
116 return if ($opt_socket_wrapper_keep_pcap);
117 return unless ($exitcode == 0);
118 return unless defined($pcap_file);
123 # expand strings from %ENV
124 sub expand_environment_strings
($)
127 # we use a reverse sort so we do the longer ones first
128 foreach my $k (sort { $b cmp $a } keys %ENV) {
129 $s =~ s/\$$k/$ENV{$k}/g;
134 sub run_testsuite
($$$$$)
136 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
137 my $pcap_file = setup_pcap
($name);
139 Subunit
::start_testsuite
($name);
140 Subunit
::progress_push
();
141 Subunit
::report_time
(time());
143 Subunit
::report_time
(time());
144 Subunit
::progress_pop
();
147 Subunit
::progress_pop
();
148 Subunit
::end_testsuite
($name, "error", "Unable to run $cmd: $!");
151 Subunit
::end_testsuite
($name, "error",
152 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($?
& 127), ($?
& 128) ?
'with' : 'without'));
156 my $exitcode = $?
>> 8;
158 my $envlog = getlog_env
($envname);
160 print "envlog: $envlog\n";
163 print "command: $cmd\n";
164 printf "expanded command: %s\n", expand_environment_strings
($cmd);
166 if ($exitcode == 0) {
167 Subunit
::end_testsuite
($name, "success");
169 Subunit
::end_testsuite
($name, "failure", "Exit code was $exitcode");
172 cleanup_pcap
($pcap_file, $exitcode);
174 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
175 print "PCAP FILE: $pcap_file\n";
178 if ($exitcode != 0) {
179 exit(1) if ($opt_one);
187 print "Samba test runner
188 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
189 Copyright (C) Stefan Metzmacher <metze\@samba.org>
191 Usage: $Script [OPTIONS] TESTNAME-REGEX
194 --help this help page
195 --target=samba[3]|win Samba version to target
196 --testlist=FILE file to read available tests from
199 --prefix=DIR prefix to run tests in [st]
200 --srcdir=DIR source directory [.]
201 --bindir=DIR binaries directory [./bin]
204 --socket-wrapper-pcap save traffic to pcap directories
205 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
207 --socket-wrapper enable socket wrapper
210 --ldap=openldap|fedora-ds back samba onto specified ldap server
213 --quick run quick overall test
214 --one abort when the first test fails
215 --testenv run a shell in the requested test environment
216 --list list available tests
221 my $result = GetOptions
(
222 'help|h|?' => \
$opt_help,
223 'target=s' => \
$opt_target,
224 'prefix=s' => \
$prefix,
225 'socket-wrapper' => \
$opt_socket_wrapper,
226 'socket-wrapper-pcap' => \
$opt_socket_wrapper_pcap,
227 'socket-wrapper-keep-pcap' => \
$opt_socket_wrapper_keep_pcap,
228 'quick' => \
$opt_quick,
230 'exclude=s' => \
@opt_exclude,
231 'include=s' => \
@opt_include,
232 'srcdir=s' => \
$srcdir,
233 'bindir=s' => \
$bindir,
234 'testenv' => \
$opt_testenv,
235 'list' => \
$opt_list,
237 'resetup-environment' => \
$opt_resetup_env,
238 'testlist=s' => \
@testlists,
239 'load-list=s' => \
$opt_load_list,
240 'binary-mapping=s' => \
$opt_binary_mapping
243 exit(1) if (not $result);
245 ShowHelp
() if ($opt_help);
247 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
249 # we want unbuffered output
254 # quick hack to disable rpc validation when using valgrind - its way too slow
255 unless (defined($ENV{VALGRIND
})) {
256 $ENV{VALIDATE
} = "validate";
257 $ENV{MALLOC_CHECK_
} = 2;
260 # make all our python scripts unbuffered
261 $ENV{PYTHONUNBUFFERED
} = 1;
263 my $bindir_abs = abs_path
($bindir);
265 # Backwards compatibility:
266 if (defined($ENV{TEST_LDAP
}) and $ENV{TEST_LDAP
} eq "yes") {
267 if (defined($ENV{FEDORA_DS_ROOT
})) {
274 my $torture_maxtime = ($ENV{TORTURE_MAXTIME
} or 1200);
277 $torture_maxtime *= 2;
284 die("using an empty prefix isn't allowed") unless $prefix ne "";
286 # Ensure we have the test prefix around.
288 # We need restrictive
289 # permissions on this as some subdirectories in this tree will have
290 # wider permissions (ie 0777) and this would allow other users on the
291 # host to subvert the test process.
292 mkdir($prefix, 0700) unless -d
$prefix;
295 my $prefix_abs = abs_path
($prefix);
296 my $tmpdir_abs = abs_path
("$prefix/tmp");
297 mkdir($tmpdir_abs, 0777) unless -d
$tmpdir_abs;
299 my $srcdir_abs = abs_path
($srcdir);
301 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
302 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
304 $ENV{PREFIX
} = $prefix;
305 $ENV{KRB5CCNAME
} = "$prefix/krb5ticket";
306 $ENV{PREFIX_ABS
} = $prefix_abs;
307 $ENV{SRCDIR
} = $srcdir;
308 $ENV{SRCDIR_ABS
} = $srcdir_abs;
309 $ENV{BINDIR
} = $bindir_abs;
311 my $tls_enabled = not $opt_quick;
312 $ENV{TLS_ENABLED
} = ($tls_enabled?
"yes":"no");
314 sub prefix_pathvar
($$)
316 my ($name, $newpath) = @_;
317 if (defined($ENV{$name})) {
318 $ENV{$name} = "$newpath:$ENV{$name}";
320 $ENV{$name} = $newpath;
323 prefix_pathvar
("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
324 prefix_pathvar
("PYTHONPATH", "$bindir_abs/python");
326 if ($opt_socket_wrapper_keep_pcap) {
327 # Socket wrapper keep pcap implies socket wrapper pcap
328 $opt_socket_wrapper_pcap = 1;
331 if ($opt_socket_wrapper_pcap) {
332 # Socket wrapper pcap implies socket wrapper
333 $opt_socket_wrapper = 1;
336 my $socket_wrapper_dir;
337 if ($opt_socket_wrapper) {
338 $socket_wrapper_dir = SocketWrapper
::setup_dir
("$prefix_abs/w", $opt_socket_wrapper_pcap);
339 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
340 } elsif (not $opt_list) {
342 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
347 my $testenv_default = "none";
349 my %binary_mapping = ();
350 if ($opt_binary_mapping) {
351 my @binmapping_list = split(/,/, $opt_binary_mapping);
352 foreach my $mapping (@binmapping_list) {
353 my ($bin, $map) = split(/\:/, $mapping);
354 $binary_mapping{$bin} = $map;
358 $ENV{BINARY_MAPPING
} = $opt_binary_mapping;
360 # After this many seconds, the server will self-terminate. All tests
361 # must terminate in this time, and testenv will only stay alive this
364 my $server_maxtime = 7500;
365 if (defined($ENV{SMBD_MAXTIME
}) and $ENV{SMBD_MAXTIME
} ne "") {
366 $server_maxtime = $ENV{SMBD_MAXTIME
};
370 if ($opt_target eq "samba") {
371 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
372 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
374 $testenv_default = "dc";
375 require target
::Samba
;
376 $target = new Samba
($bindir, \
%binary_mapping, $ldap, $srcdir, $server_maxtime);
377 } elsif ($opt_target eq "samba3") {
378 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
379 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
381 $testenv_default = "member";
382 require target
::Samba3
;
383 $target = new Samba3
($bindir, \
%binary_mapping, $srcdir_abs, $server_maxtime);
387 sub read_test_regexes
($)
391 open(LF
, "<$name") or die("unable to read $name: $!");
395 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
396 push (@ret, [$1, $4]);
398 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
399 push (@ret, [$_, undef]);
406 foreach (@opt_exclude) {
407 push (@excludes, read_test_regexes
($_));
410 foreach (@opt_include) {
411 push (@includes, read_test_regexes
($_));
414 my $interfaces = join(',', ("127.0.0.11/8",
421 my $clientdir = "$prefix_abs/client";
423 my $conffile = "$clientdir/client.conf";
424 $ENV{SMB_CONF_PATH
} = $conffile;
426 sub write_clientconf
($$$)
428 my ($conffile, $clientdir, $vars) = @_;
430 mkdir("$clientdir", 0777) unless -d
"$clientdir";
432 if ( -d
"$clientdir/private" ) {
433 unlink <$clientdir/private/*>;
435 mkdir("$clientdir/private", 0777);
438 if ( -d
"$clientdir/lockdir" ) {
439 unlink <$clientdir/lockdir/*>;
441 mkdir("$clientdir/lockdir", 0777);
444 if ( -d
"$clientdir/statedir" ) {
445 unlink <$clientdir/statedir/*>;
447 mkdir("$clientdir/statedir", 0777);
450 if ( -d
"$clientdir/cachedir" ) {
451 unlink <$clientdir/cachedir/*>;
453 mkdir("$clientdir/cachedir", 0777);
456 # this is ugly, but the ncalrpcdir needs exactly 0755
457 # otherwise tests fail.
460 if ( -d
"$clientdir/ncalrpcdir/np" ) {
461 unlink <$clientdir/ncalrpcdir/np
/*>;
462 rmdir "$clientdir/ncalrpcdir/np";
464 if ( -d
"$clientdir/ncalrpcdir" ) {
465 unlink <$clientdir/ncalrpcdir/*>;
466 rmdir "$clientdir/ncalrpcdir";
468 mkdir("$clientdir/ncalrpcdir", 0755);
471 open(CF
, ">$conffile");
472 print CF
"[global]\n";
473 print CF
"\tnetbios name = client\n";
474 if (defined($vars->{DOMAIN
})) {
475 print CF
"\tworkgroup = $vars->{DOMAIN}\n";
477 if (defined($vars->{REALM
})) {
478 print CF
"\trealm = $vars->{REALM}\n";
480 if ($opt_socket_wrapper) {
481 print CF
"\tinterfaces = $interfaces\n";
484 private dir = $clientdir/private
485 lock dir = $clientdir/lockdir
486 state directory = $clientdir/statedir
487 cache directory = $clientdir/cachedir
488 ncalrpc dir = $clientdir/ncalrpcdir
489 name resolve order = file bcast
490 panic action = $RealBin/gdb_backtrace \%d
492 notify:inotify = false
494 system:anonymous = true
495 client lanman auth = Yes
497 torture:basedir = $clientdir
498 #We don't want to pass our self-tests if the PAC code is wrong
499 gensec:require_pac = true
500 resolv:host file = $prefix_abs/dns_host_file
501 #We don't want to run 'speed' tests for very long
502 torture:timelimit = 1
509 sub should_run_test
($)
515 for (my $i=0; $i <= $#tests; $i++) {
516 if ($name =~ /$tests[$i]/i) {
528 open(IN
, $filename) or die("Unable to open $filename: $!");
531 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
532 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
533 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
540 if (should_run_test
($name) == 1) {
541 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
547 close(IN
) or die("Error creating recipe");
551 if ($#testlists == -1) {
552 die("No testlists specified");
555 $ENV{SELFTEST_PREFIX
} = "$prefix_abs";
556 $ENV{SELFTEST_TMPDIR
} = "$tmpdir_abs";
557 $ENV{TEST_DATA_PREFIX
} = "$tmpdir_abs";
558 if ($opt_socket_wrapper) {
559 $ENV{SELFTEST_INTERFACES
} = $interfaces;
561 $ENV{SELFTEST_INTERFACES
} = "";
564 $ENV{SELFTEST_QUICK
} = "1";
566 $ENV{SELFTEST_QUICK
} = "";
568 $ENV{SELFTEST_MAXTIME
} = $torture_maxtime;
571 foreach my $fn (@testlists) {
572 foreach (read_testlist
($fn)) {
574 next if (@includes and not defined(find_in_list
(\
@includes, $name)));
575 push (@available, $_);
579 my $restricted = undef;
580 my $restricted_used = {};
582 if ($opt_load_list) {
584 open(LOAD_LIST
, "<$opt_load_list") or die("Unable to open $opt_load_list");
585 while (<LOAD_LIST
>) {
587 push (@
$restricted, $_);
592 my $individual_tests = undef;
593 $individual_tests = {};
595 foreach my $testsuite (@available) {
596 my $name = $$testsuite[0];
597 my $skipreason = skip
($name);
598 if (defined($restricted)) {
599 # Find the testsuite for this test
601 foreach my $r (@
$restricted) {
603 $individual_tests->{$name} = [];
605 $restricted_used->{$r} = 1;
606 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
607 push(@
{$individual_tests->{$name}}, $r);
609 $restricted_used->{$r} = 1;
613 if (defined($skipreason)) {
615 Subunit
::skip_testsuite
($name, $skipreason);
618 push(@todo, $testsuite);
621 } elsif (defined($skipreason)) {
623 Subunit
::skip_testsuite
($name, $skipreason);
626 push(@todo, $testsuite);
630 if (defined($restricted)) {
631 foreach (@
$restricted) {
632 unless (defined($restricted_used->{$_})) {
633 print "No test or testsuite found matching $_\n";
636 } elsif ($#todo == -1) {
637 print STDERR
"No tests to run\n";
641 my $suitestotal = $#todo + 1;
644 Subunit
::progress
($suitestotal);
645 Subunit
::report_time
(time());
651 my %running_envs = ();
653 sub get_running_env
($)
661 return $running_envs{$envname};
664 my @exported_envvars = (
669 # domain controller stuff
678 "MEMBER_NETBIOSNAME",
679 "MEMBER_NETBIOSALIAS",
681 # rpc proxy controller stuff
683 "RPC_PROXY_SERVER_IP",
684 "RPC_PROXY_NETBIOSNAME",
685 "RPC_PROXY_NETBIOSALIAS",
687 # domain controller stuff for Vampired DC
689 "VAMPIRE_DC_SERVER_IP",
690 "VAMPIRE_DC_NETBIOSNAME",
691 "VAMPIRE_DC_NETBIOSALIAS",
708 "WINBINDD_SOCKET_DIR",
709 "WINBINDD_PRIV_PIPE_DIR",
714 "NSS_WRAPPER_PASSWD",
719 $SIG{INT
} = $SIG{QUIT
} = $SIG{TERM
} = sub {
721 teardown_env
($_) foreach(keys %running_envs);
722 die("Received signal $signame");
727 my ($name, $prefix) = @_;
729 my $testenv_vars = undef;
735 $option =~ s/^[^:]*//;
738 $option = "client" if $option eq "";
740 if ($envname eq "none") {
742 } elsif (defined(get_running_env
($envname))) {
743 $testenv_vars = get_running_env
($envname);
744 if (not $testenv_vars->{target
}->check_env($testenv_vars)) {
745 print $testenv_vars->{target
}->getlog_env($testenv_vars);
746 $testenv_vars = undef;
749 $testenv_vars = $target->setup_env($envname, $prefix);
750 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
751 return $testenv_vars;
752 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target
})) {
753 $testenv_vars->{target
} = $target;
755 if (not defined($testenv_vars)) {
756 warn("$opt_target can't start up known environment '$envname'");
761 return undef unless defined($testenv_vars);
763 $running_envs{$envname} = $testenv_vars;
765 if ($option eq "local") {
766 SocketWrapper
::set_default_iface
($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE
});
767 $ENV{SMB_CONF_PATH
} = $testenv_vars->{SERVERCONFFILE
};
768 } elsif ($option eq "client") {
769 SocketWrapper
::set_default_iface
(11);
770 write_clientconf
($conffile, $clientdir, $testenv_vars);
771 $ENV{SMB_CONF_PATH
} = $conffile;
773 die("Unknown option[$option] for envname[$envname]");
776 foreach (@exported_envvars) {
777 if (defined($testenv_vars->{$_})) {
778 $ENV{$_} = $testenv_vars->{$_};
784 return $testenv_vars;
787 sub exported_envvars_str
($)
789 my ($testenv_vars) = @_;
792 foreach (@exported_envvars) {
793 next unless defined($testenv_vars->{$_});
794 $out .= $_."=".$testenv_vars->{$_}."\n";
803 return "" if ($envname eq "none");
804 my $env = get_running_env
($envname);
805 return $env->{target
}->getlog_env($env);
811 return 1 if ($envname eq "none");
812 my $env = get_running_env
($envname);
813 return $env->{target
}->check_env($env);
819 return if ($envname eq "none");
820 my $env = get_running_env
($envname);
821 $env->{target
}->teardown_env($env);
822 delete $running_envs{$envname};
825 # This 'global' file needs to be empty when we start
826 unlink("$prefix_abs/dns_host_file");
829 my $testenv_name = $ENV{SELFTEST_TESTENV
};
830 $testenv_name = $testenv_default unless defined($testenv_name);
832 my $testenv_vars = setup_env
($testenv_name, $prefix);
834 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
836 $ENV{PIDDIR
} = $testenv_vars->{PIDDIR
};
837 $ENV{ENVNAME
} = $testenv_name;
839 my $envvarstr = exported_envvars_str
($testenv_vars);
842 if ($ENV{TERMINAL
}) {
843 @term = ($ENV{TERMINAL
});
845 @term = ("xterm", "-e");
847 my @term_args = ("bash", "-c", "echo -e \"
848 Welcome to the Samba4 Test environment '$testenv_name'
850 This matches the client environment used in make test
851 server is pid `cat \$PIDDIR/samba.pid`
853 Some useful environment variables:
854 TORTURE_OPTIONS=\$TORTURE_OPTIONS
855 SMB_CONF_PATH=\$SMB_CONF_PATH
858 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
860 system(@term, @term_args);
862 teardown_env
($testenv_name);
863 } elsif ($opt_list) {
867 my $envname = $$_[1];
869 unless($cmd =~ /\$LISTOPT/) {
870 warn("Unable to list tests in $name");
874 $cmd =~ s/\$LISTOPT/--list/g;
879 die("Unable to run $cmd: $!");
881 die(snprintf
("%s died with signal %d, %s coredump\n", $cmd, ($?
& 127), ($?
& 128) ?
'with' : 'without'));
884 my $exitcode = $?
>> 8;
885 if ($exitcode != 0) {
886 die("$cmd exited with exit code $exitcode");
894 my $envname = $$_[1];
896 my $envvars = setup_env
($envname, $prefix);
897 if (not defined($envvars)) {
898 Subunit
::start_testsuite
($name);
899 Subunit
::end_testsuite
($name, "error",
900 "unable to set up environment $envname - exiting");
902 } elsif ($envvars eq "UNKNOWN") {
903 Subunit
::start_testsuite
($name);
904 Subunit
::end_testsuite
($name, "skip",
905 "environment $envname is unknown in this test backend - skipping");
909 # Generate a file with the individual tests to run, if the
910 # test runner for this test suite supports it.
911 if ($individual_tests and $individual_tests->{$name}) {
913 my ($fh, $listid_file) = tempfile
(UNLINK
=> 0);
914 foreach my $test (@
{$individual_tests->{$name}}) {
915 print $fh substr($test, length($name)+1) . "\n";
917 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
919 $cmd =~ s/\s+[^\s]+\s*$//;
920 $cmd .= " " . join(' ', @
{$individual_tests->{$name}});
924 run_testsuite
($envname, $name, $cmd, $i, $suitestotal);
926 teardown_env
($envname) if ($opt_resetup_env);
932 teardown_env
($_) foreach (keys %running_envs);
936 # if there were any valgrind failures, show them
937 foreach (<$prefix/valgrind
.log*>) {
939 print "VALGRIND FAILURE\n";