tevent: Fix typos
[Samba.git] / selftest / selftest.pl
blob9bfd4e50015265abec1a22f95cb7f03ccb1f8951
1 #!/usr/bin/perl
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/>.
19 use strict;
21 use FindBin qw($RealBin $Script);
22 use File::Spec;
23 use File::Temp qw(tempfile);
24 use Getopt::Long;
25 use POSIX;
26 use Cwd qw(abs_path);
27 use lib "$RealBin";
28 use Subunit;
29 use SocketWrapper;
30 use target::Samba;
32 eval {
33 require Time::HiRes;
34 Time::HiRes->import("time");
36 if ($@) {
37 print "You don't have Time::Hires installed !\n";
40 my $opt_help = 0;
41 my $opt_target = "samba";
42 my $opt_quick = 0;
43 my $opt_socket_wrapper = 0;
44 my $opt_socket_wrapper_pcap = undef;
45 my $opt_socket_wrapper_keep_pcap = undef;
46 my $opt_random_order = 0;
47 my $opt_one = 0;
48 my @opt_exclude = ();
49 my @opt_include = ();
50 my @opt_exclude_env = ();
51 my @opt_include_env = ();
52 my $opt_testenv = 0;
53 my $opt_list = 0;
54 my $opt_mitkrb5 = 0;
55 my $ldap = undef;
56 my $opt_resetup_env = undef;
57 my $opt_load_list = undef;
58 my $opt_libnss_wrapper_so_path = "";
59 my $opt_libresolv_wrapper_so_path = "";
60 my $opt_libsocket_wrapper_so_path = "";
61 my $opt_libuid_wrapper_so_path = "";
62 my $opt_use_dns_faking = 0;
63 my @testlists = ();
65 my $srcdir = ".";
66 my $bindir = "./bin";
67 my $prefix = "./st";
69 my @includes = ();
70 my @excludes = ();
72 sub find_in_list($$)
74 my ($list, $fullname) = @_;
76 foreach (@$list) {
77 if ($fullname =~ /$$_[0]/) {
78 return ($$_[1]) if ($$_[1]);
79 return "";
83 return undef;
86 sub skip($)
88 my ($name) = @_;
90 return find_in_list(\@excludes, $name);
93 sub getlog_env($);
95 sub setup_pcap($)
97 my ($name) = @_;
99 return unless ($opt_socket_wrapper_pcap);
100 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
102 my $fname = $name;
103 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
105 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
107 SocketWrapper::setup_pcap($pcap_file);
109 return $pcap_file;
112 sub cleanup_pcap($$)
114 my ($pcap_file, $exitcode) = @_;
116 return unless ($opt_socket_wrapper_pcap);
117 return if ($opt_socket_wrapper_keep_pcap);
118 return unless ($exitcode == 0);
119 return unless defined($pcap_file);
121 unlink($pcap_file);
124 # expand strings from %ENV
125 sub expand_environment_strings($)
127 my $s = shift;
128 # we use a reverse sort so we do the longer ones first
129 foreach my $k (sort { $b cmp $a } keys %ENV) {
130 $s =~ s/\$$k/$ENV{$k}/g;
132 return $s;
135 sub run_testsuite($$$$$)
137 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
138 my $pcap_file = setup_pcap($name);
140 Subunit::start_testsuite($name);
141 Subunit::progress_push();
142 Subunit::report_time(time());
143 system($cmd);
144 Subunit::report_time(time());
145 Subunit::progress_pop();
147 if ($? == -1) {
148 print "command: $cmd\n";
149 printf "expanded command: %s\n", expand_environment_strings($cmd);
150 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
151 exit(1);
152 } elsif ($? & 127) {
153 print "command: $cmd\n";
154 printf "expanded command: %s\n", expand_environment_strings($cmd);
155 Subunit::end_testsuite($name, "error",
156 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
157 exit(1);
160 my $exitcode = $? >> 8;
162 my $envlog = getlog_env($envname);
163 if ($envlog ne "") {
164 print "envlog: $envlog\n";
167 print "command: $cmd\n";
168 printf "expanded command: %s\n", expand_environment_strings($cmd);
170 if ($exitcode == 0) {
171 Subunit::end_testsuite($name, "success");
172 } else {
173 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
176 cleanup_pcap($pcap_file, $exitcode);
178 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
179 print "PCAP FILE: $pcap_file\n";
182 if ($exitcode != 0) {
183 exit(1) if ($opt_one);
186 return $exitcode;
189 sub ShowHelp()
191 print "Samba test runner
192 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
193 Copyright (C) Stefan Metzmacher <metze\@samba.org>
195 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
197 Generic options:
198 --help this help page
199 --target=samba[3]|win Samba version to target
200 --testlist=FILE file to read available tests from
201 --exclude=FILE Exclude tests listed in the file
202 --include=FILE Include tests listed in the file
203 --exclude-env=ENV Exclude tests for the specified environment
204 --include-env=ENV Include tests for the specified environment
206 Paths:
207 --prefix=DIR prefix to run tests in [st]
208 --srcdir=DIR source directory [.]
209 --bindir=DIR binaries directory [./bin]
211 Preload cwrap:
212 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
213 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
214 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
215 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
217 DNS:
218 --use-dns-faking Fake DNS entries rather than talking to our
219 DNS implementation.
221 Target Specific:
222 --socket-wrapper-pcap save traffic to pcap directories
223 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
224 failed
225 --socket-wrapper enable socket wrapper
227 Samba4 Specific:
228 --ldap=openldap|fedora-ds back samba onto specified ldap server
230 Behaviour:
231 --quick run quick overall test
232 --one abort when the first test fails
233 --testenv run a shell in the requested test environment
234 --list list available tests
236 exit(0);
239 my $result = GetOptions (
240 'help|h|?' => \$opt_help,
241 'target=s' => \$opt_target,
242 'prefix=s' => \$prefix,
243 'socket-wrapper' => \$opt_socket_wrapper,
244 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
245 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
246 'quick' => \$opt_quick,
247 'one' => \$opt_one,
248 'exclude=s' => \@opt_exclude,
249 'include=s' => \@opt_include,
250 'exclude-env=s' => \@opt_exclude_env,
251 'include-env=s' => \@opt_include_env,
252 'srcdir=s' => \$srcdir,
253 'bindir=s' => \$bindir,
254 'testenv' => \$opt_testenv,
255 'list' => \$opt_list,
256 'mitkrb5' => \$opt_mitkrb5,
257 'ldap:s' => \$ldap,
258 'resetup-environment' => \$opt_resetup_env,
259 'testlist=s' => \@testlists,
260 'random-order' => \$opt_random_order,
261 'load-list=s' => \$opt_load_list,
262 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
263 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
264 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
265 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
266 'use-dns-faking' => \$opt_use_dns_faking
269 exit(1) if (not $result);
271 ShowHelp() if ($opt_help);
273 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
275 # we want unbuffered output
276 $| = 1;
278 my @tests = @ARGV;
280 # quick hack to disable rpc validation when using valgrind - its way too slow
281 unless (defined($ENV{VALGRIND})) {
282 $ENV{VALIDATE} = "validate";
283 $ENV{MALLOC_CHECK_} = 3;
286 # make all our python scripts unbuffered
287 $ENV{PYTHONUNBUFFERED} = 1;
289 my $bindir_abs = abs_path($bindir);
291 # Backwards compatibility:
292 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
293 if (defined($ENV{FEDORA_DS_ROOT})) {
294 $ldap = "fedora-ds";
295 } else {
296 $ldap = "openldap";
300 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
301 if ($ldap) {
302 # LDAP is slow
303 $torture_maxtime *= 2;
306 $prefix =~ s+//+/+;
307 $prefix =~ s+/./+/+;
308 $prefix =~ s+/$++;
310 die("using an empty prefix isn't allowed") unless $prefix ne "";
312 # Ensure we have the test prefix around.
314 # We need restrictive
315 # permissions on this as some subdirectories in this tree will have
316 # wider permissions (ie 0777) and this would allow other users on the
317 # host to subvert the test process.
318 umask 0077;
319 mkdir($prefix, 0700) unless -d $prefix;
320 chmod 0700, $prefix;
321 # We need to have no umask limitations for the tests.
322 umask 0000;
324 my $prefix_abs = abs_path($prefix);
325 my $tmpdir_abs = abs_path("$prefix/tmp");
326 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
328 my $srcdir_abs = abs_path($srcdir);
330 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
331 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
333 $ENV{SAMBA_SELFTEST} = "1";
335 $ENV{PREFIX} = $prefix;
336 $ENV{PREFIX_ABS} = $prefix_abs;
337 $ENV{SRCDIR} = $srcdir;
338 $ENV{SRCDIR_ABS} = $srcdir_abs;
339 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
340 $ENV{BINDIR} = $bindir_abs;
342 my $tls_enabled = not $opt_quick;
343 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
345 sub prefix_pathvar($$)
347 my ($name, $newpath) = @_;
348 if (defined($ENV{$name})) {
349 $ENV{$name} = "$newpath:$ENV{$name}";
350 } else {
351 $ENV{$name} = $newpath;
354 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
355 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
357 if ($opt_socket_wrapper_keep_pcap) {
358 # Socket wrapper keep pcap implies socket wrapper pcap
359 $opt_socket_wrapper_pcap = 1;
362 if ($opt_socket_wrapper_pcap) {
363 # Socket wrapper pcap implies socket wrapper
364 $opt_socket_wrapper = 1;
367 my $ld_preload = $ENV{LD_PRELOAD};
369 if ($opt_libnss_wrapper_so_path) {
370 if ($ld_preload) {
371 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
372 } else {
373 $ld_preload = "$opt_libnss_wrapper_so_path";
377 if ($opt_libresolv_wrapper_so_path) {
378 if ($ld_preload) {
379 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
380 } else {
381 $ld_preload = "$opt_libresolv_wrapper_so_path";
385 if ($opt_libsocket_wrapper_so_path) {
386 if ($ld_preload) {
387 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
388 } else {
389 $ld_preload = "$opt_libsocket_wrapper_so_path";
393 if ($opt_libuid_wrapper_so_path) {
394 if ($ld_preload) {
395 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
396 } else {
397 $ld_preload = "$opt_libuid_wrapper_so_path";
401 $ENV{LD_PRELOAD} = $ld_preload;
402 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
404 # Enable uid_wrapper globally
405 $ENV{UID_WRAPPER} = 1;
407 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
409 # This is needed in order to allow the ldb_*ldap module
410 # to work with a preloaded socket wrapper.
411 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
413 my $socket_wrapper_dir;
414 if ($opt_socket_wrapper) {
415 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
416 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
417 } elsif (not $opt_list) {
418 unless ($< == 0) {
419 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
423 if ($opt_use_dns_faking) {
424 print "DNS: Faking nameserver\n";
425 $ENV{SAMBA_DNS_FAKING} = 1;
428 my $target;
429 my $testenv_default = "none";
431 if ($opt_mitkrb5 == 1) {
432 $ENV{MITKRB5} = $opt_mitkrb5;
435 # After this many seconds, the server will self-terminate. All tests
436 # must terminate in this time, and testenv will only stay alive this
437 # long
439 my $server_maxtime;
440 if ($opt_testenv) {
441 # 1 year should be enough :-)
442 $server_maxtime = 365 * 24 * 60 * 60;
443 } else {
444 # make test should run under 5 hours
445 $server_maxtime = 5 * 60 * 60;
448 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
449 $server_maxtime = $ENV{SMBD_MAXTIME};
452 unless ($opt_list) {
453 if ($opt_target eq "samba") {
454 $testenv_default = "ad_dc";
455 require target::Samba;
456 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
457 } elsif ($opt_target eq "samba3") {
458 $testenv_default = "nt4_member";
459 require target::Samba3;
460 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
464 sub read_test_regexes($)
466 my ($name) = @_;
467 my @ret = ();
468 open(LF, "<$name") or die("unable to read $name: $!");
469 while (<LF>) {
470 chomp;
471 next if (/^#/);
472 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
473 push (@ret, [$1, $4]);
474 } else {
475 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
476 push (@ret, [$_, undef]);
479 close(LF);
480 return @ret;
483 foreach (@opt_exclude) {
484 push (@excludes, read_test_regexes($_));
487 foreach (@opt_include) {
488 push (@includes, read_test_regexes($_));
491 my $interfaces = join(',', ("127.0.0.11/8",
492 "127.0.0.12/8",
493 "127.0.0.13/8",
494 "127.0.0.14/8",
495 "127.0.0.15/8",
496 "127.0.0.16/8"));
498 my $clientdir = "$prefix_abs/client";
500 my $conffile = "$clientdir/client.conf";
501 $ENV{SMB_CONF_PATH} = $conffile;
503 sub write_clientconf($$$)
505 my ($conffile, $clientdir, $vars) = @_;
507 mkdir("$clientdir", 0777) unless -d "$clientdir";
509 if ( -d "$clientdir/private" ) {
510 unlink <$clientdir/private/*>;
511 } else {
512 mkdir("$clientdir/private", 0777);
515 if ( -d "$clientdir/lockdir" ) {
516 unlink <$clientdir/lockdir/*>;
517 } else {
518 mkdir("$clientdir/lockdir", 0777);
521 if ( -d "$clientdir/statedir" ) {
522 unlink <$clientdir/statedir/*>;
523 } else {
524 mkdir("$clientdir/statedir", 0777);
527 if ( -d "$clientdir/cachedir" ) {
528 unlink <$clientdir/cachedir/*>;
529 } else {
530 mkdir("$clientdir/cachedir", 0777);
533 # this is ugly, but the ncalrpcdir needs exactly 0755
534 # otherwise tests fail.
535 my $mask = umask;
536 umask 0022;
537 if ( -d "$clientdir/ncalrpcdir/np" ) {
538 unlink <$clientdir/ncalrpcdir/np/*>;
539 rmdir "$clientdir/ncalrpcdir/np";
541 if ( -d "$clientdir/ncalrpcdir" ) {
542 unlink <$clientdir/ncalrpcdir/*>;
543 rmdir "$clientdir/ncalrpcdir";
545 mkdir("$clientdir/ncalrpcdir", 0755);
546 umask $mask;
548 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
549 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
550 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
551 my $ca_users_dir = "$cadir/Users";
553 if ( -d "$clientdir/pkinit" ) {
554 unlink <$clientdir/pkinit/*>;
555 } else {
556 mkdir("$clientdir/pkinit", 0700);
559 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
560 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
561 # We make a copy here and make the certificated easily
562 # accessable in the client environment.
563 my $mask = umask;
564 umask 0077;
565 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
566 for my $d (readdir USERS) {
567 my $user_dir = "${ca_users_dir}/${d}";
568 next if ${d} =~ /^\./;
569 next if (! -d "${user_dir}");
570 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
571 for my $l (readdir USER) {
572 my $user_link = "${user_dir}/${l}";
573 next if ${l} =~ /^\./;
574 next if (! -l "${user_link}");
576 my $dest = "${clientdir}/pkinit/${l}";
577 Samba::copy_file_content(${user_link}, ${dest});
579 closedir USER;
581 closedir USERS;
582 umask $mask;
584 open(CF, ">$conffile");
585 print CF "[global]\n";
586 print CF "\tnetbios name = client\n";
587 if (defined($vars->{DOMAIN})) {
588 print CF "\tworkgroup = $vars->{DOMAIN}\n";
590 if (defined($vars->{REALM})) {
591 print CF "\trealm = $vars->{REALM}\n";
593 if ($opt_socket_wrapper) {
594 print CF "\tinterfaces = $interfaces\n";
596 print CF "
597 private dir = $clientdir/private
598 lock dir = $clientdir/lockdir
599 state directory = $clientdir/statedir
600 cache directory = $clientdir/cachedir
601 ncalrpc dir = $clientdir/ncalrpcdir
602 panic action = $RealBin/gdb_backtrace \%d
603 max xmit = 32K
604 notify:inotify = false
605 ldb:nosync = true
606 system:anonymous = true
607 client lanman auth = Yes
608 log level = 1
609 torture:basedir = $clientdir
610 #We don't want to pass our self-tests if the PAC code is wrong
611 gensec:require_pac = true
612 #We don't want to run 'speed' tests for very long
613 torture:timelimit = 1
614 winbind separator = /
615 tls cafile = ${cacert}
616 tls crlfile = ${cacrl_pem}
617 tls verify peer = no_check
618 include system krb5 conf = no
620 close(CF);
623 my @todo = ();
625 sub should_run_test($)
627 my $name = shift;
628 if ($#tests == -1) {
629 return 1;
631 for (my $i=0; $i <= $#tests; $i++) {
632 if ($name =~ /$tests[$i]/i) {
633 return 1;
636 return 0;
639 sub read_testlist($)
641 my ($filename) = @_;
643 my @ret = ();
644 open(IN, $filename) or die("Unable to open $filename: $!");
646 while (<IN>) {
647 if (/-- TEST(-LOADLIST|) --\n/) {
648 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
649 my $name = <IN>;
650 $name =~ s/\n//g;
651 my $env = <IN>;
652 $env =~ s/\n//g;
653 my $loadlist;
654 if ($supports_loadlist) {
655 $loadlist = <IN>;
656 $loadlist =~ s/\n//g;
658 my $cmdline = <IN>;
659 $cmdline =~ s/\n//g;
660 if (should_run_test($name) == 1) {
661 push (@ret, [$name, $env, $cmdline, $loadlist]);
663 } else {
664 print;
667 close(IN) or die("Error creating recipe from $filename");
668 return @ret;
671 if ($#testlists == -1) {
672 die("No testlists specified");
675 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
676 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
677 $ENV{TMPDIR} = "$tmpdir_abs";
678 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
679 if ($opt_socket_wrapper) {
680 $ENV{SELFTEST_INTERFACES} = $interfaces;
681 } else {
682 $ENV{SELFTEST_INTERFACES} = "";
684 if ($opt_quick) {
685 $ENV{SELFTEST_QUICK} = "1";
686 } else {
687 $ENV{SELFTEST_QUICK} = "";
689 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
691 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
692 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
694 my @available = ();
695 foreach my $fn (@testlists) {
696 foreach (read_testlist($fn)) {
697 my $name = $$_[0];
698 next if (@includes and not defined(find_in_list(\@includes, $name)));
699 push (@available, $_);
703 my $restricted = undef;
704 my $restricted_used = {};
706 if ($opt_load_list) {
707 $restricted = [];
708 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
709 while (<LOAD_LIST>) {
710 chomp;
711 push (@$restricted, $_);
713 close(LOAD_LIST);
716 my $individual_tests = undef;
717 $individual_tests = {};
719 foreach my $testsuite (@available) {
720 my $name = $$testsuite[0];
721 my $skipreason = skip($name);
722 if (defined($restricted)) {
723 # Find the testsuite for this test
724 my $match = undef;
725 foreach my $r (@$restricted) {
726 if ($r eq $name) {
727 $individual_tests->{$name} = [];
728 $match = $r;
729 $restricted_used->{$r} = 1;
730 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
731 push(@{$individual_tests->{$name}}, $r);
732 $match = $r;
733 $restricted_used->{$r} = 1;
736 if ($match) {
737 if (defined($skipreason)) {
738 if (not $opt_list) {
739 Subunit::skip_testsuite($name, $skipreason);
741 } else {
742 push(@todo, $testsuite);
745 } elsif (defined($skipreason)) {
746 if (not $opt_list) {
747 Subunit::skip_testsuite($name, $skipreason);
749 } else {
750 push(@todo, $testsuite);
754 if (defined($restricted)) {
755 foreach (@$restricted) {
756 unless (defined($restricted_used->{$_})) {
757 print "No test or testsuite found matching $_\n";
760 } elsif ($#todo == -1) {
761 print STDERR "No tests to run\n";
762 exit(1);
765 my $suitestotal = $#todo + 1;
767 unless ($opt_list) {
768 Subunit::progress($suitestotal);
769 Subunit::report_time(time());
772 my $i = 0;
773 $| = 1;
775 my %running_envs = ();
777 sub get_running_env($)
779 my ($name) = @_;
781 my $envname = $name;
783 $envname =~ s/:.*//;
785 return $running_envs{$envname};
788 my @exported_envvars = (
789 # domain stuff
790 "DOMAIN",
791 "REALM",
793 # stuff related to a trusted domain
794 "TRUST_SERVER",
795 "TRUST_SERVER_IP",
796 "TRUST_SERVER_IPV6",
797 "TRUST_NETBIOSNAME",
798 "TRUST_USERNAME",
799 "TRUST_PASSWORD",
800 "TRUST_DOMAIN",
801 "TRUST_REALM",
803 # domain controller stuff
804 "DC_SERVER",
805 "DC_SERVER_IP",
806 "DC_SERVER_IPV6",
807 "DC_NETBIOSNAME",
808 "DC_NETBIOSALIAS",
810 # domain member
811 "MEMBER_SERVER",
812 "MEMBER_SERVER_IP",
813 "MEMBER_SERVER_IPV6",
814 "MEMBER_NETBIOSNAME",
815 "MEMBER_NETBIOSALIAS",
817 # rpc proxy controller stuff
818 "RPC_PROXY_SERVER",
819 "RPC_PROXY_SERVER_IP",
820 "RPC_PROXY_SERVER_IPV6",
821 "RPC_PROXY_NETBIOSNAME",
822 "RPC_PROXY_NETBIOSALIAS",
824 # domain controller stuff for Vampired DC
825 "VAMPIRE_DC_SERVER",
826 "VAMPIRE_DC_SERVER_IP",
827 "VAMPIRE_DC_SERVER_IPV6",
828 "VAMPIRE_DC_NETBIOSNAME",
829 "VAMPIRE_DC_NETBIOSALIAS",
831 # domain controller stuff for FL 2000 Vampired DC
832 "VAMPIRE_2000_DC_SERVER",
833 "VAMPIRE_2000_DC_SERVER_IP",
834 "VAMPIRE_2000_DC_SERVER_IPV6",
835 "VAMPIRE_2000_DC_NETBIOSNAME",
836 "VAMPIRE_2000_DC_NETBIOSALIAS",
838 "PROMOTED_DC_SERVER",
839 "PROMOTED_DC_SERVER_IP",
840 "PROMOTED_DC_SERVER_IPV6",
841 "PROMOTED_DC_NETBIOSNAME",
842 "PROMOTED_DC_NETBIOSALIAS",
844 # server stuff
845 "SERVER",
846 "SERVER_IP",
847 "SERVER_IPV6",
848 "NETBIOSNAME",
849 "NETBIOSALIAS",
851 # user stuff
852 "USERNAME",
853 "USERID",
854 "PASSWORD",
855 "DC_USERNAME",
856 "DC_PASSWORD",
858 # UID/GID for rfc2307 mapping tests
859 "UID_RFC2307TEST",
860 "GID_RFC2307TEST",
862 # misc stuff
863 "KRB5_CONFIG",
864 "KRB5CCNAME",
865 "SELFTEST_WINBINDD_SOCKET_DIR",
866 "NMBD_SOCKET_DIR",
867 "LOCAL_PATH",
868 "DNS_FORWARDER1",
869 "DNS_FORWARDER2",
870 "RESOLV_CONF",
871 "UNACCEPTABLE_PASSWORD",
872 "LOCK_DIR",
873 "SMBD_TEST_LOG",
875 # nss_wrapper
876 "NSS_WRAPPER_PASSWD",
877 "NSS_WRAPPER_GROUP",
878 "NSS_WRAPPER_HOSTS",
879 "NSS_WRAPPER_MODULE_SO_PATH",
880 "NSS_WRAPPER_MODULE_FN_PREFIX",
882 # resolv_wrapper
883 "RESOLV_WRAPPER_CONF",
884 "RESOLV_WRAPPER_HOSTS",
887 sub sighandler($)
889 my $signame = shift;
891 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
892 $SIG{PIPE} = 'IGNORE';
894 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
896 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
897 teardown_env($_) foreach(keys %running_envs);
898 system("pstree -p $$");
899 print "$0: PID[$$]: Exiting...\n";
900 exit(1);
903 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
905 sub setup_env($$)
907 my ($name, $prefix) = @_;
909 my $testenv_vars = undef;
911 my $envname = $name;
912 my $option = $name;
914 $envname =~ s/:.*//;
915 $option =~ s/^[^:]*//;
916 $option =~ s/^://;
918 $option = "client" if $option eq "";
920 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
922 if (defined(get_running_env($envname))) {
923 $testenv_vars = get_running_env($envname);
924 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
925 print $testenv_vars->{target}->getlog_env($testenv_vars);
926 $testenv_vars = undef;
928 } else {
929 $testenv_vars = $target->setup_env($envname, $prefix);
930 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
931 return $testenv_vars;
932 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
933 $testenv_vars->{target} = $target;
935 if (not defined($testenv_vars)) {
936 warn("$opt_target can't start up known environment '$envname'");
940 return undef unless defined($testenv_vars);
942 $running_envs{$envname} = $testenv_vars;
944 if ($option eq "local") {
945 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
946 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
947 } elsif ($option eq "client") {
948 SocketWrapper::set_default_iface(11);
949 write_clientconf($conffile, $clientdir, $testenv_vars);
950 $ENV{SMB_CONF_PATH} = $conffile;
951 } else {
952 die("Unknown option[$option] for envname[$envname]");
955 foreach (@exported_envvars) {
956 if (defined($testenv_vars->{$_})) {
957 $ENV{$_} = $testenv_vars->{$_};
958 } else {
959 delete $ENV{$_};
963 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
964 unlink($krb5_ccache_path);
965 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
966 return $testenv_vars;
969 sub exported_envvars_str($)
971 my ($testenv_vars) = @_;
972 my $out = "";
974 foreach (@exported_envvars) {
975 next unless defined($testenv_vars->{$_});
976 $out .= $_."=".$testenv_vars->{$_}."\n";
979 return $out;
982 sub getlog_env($)
984 my ($envname) = @_;
985 return "" if ($envname eq "none");
986 my $env = get_running_env($envname);
987 return $env->{target}->getlog_env($env);
990 sub check_env($)
992 my ($envname) = @_;
993 my $env = get_running_env($envname);
994 return $env->{target}->check_env($env);
997 sub teardown_env($)
999 my ($envname) = @_;
1000 return if ($envname eq "none");
1001 print STDERR "teardown_env($envname)\n";
1002 my $env = get_running_env($envname);
1003 $env->{target}->teardown_env($env);
1004 delete $running_envs{$envname};
1007 # This 'global' file needs to be empty when we start
1008 unlink("$prefix_abs/dns_host_file");
1009 unlink("$prefix_abs/hosts");
1011 if ($opt_random_order) {
1012 require List::Util;
1013 my @newtodo = List::Util::shuffle(@todo);
1014 @todo = @newtodo;
1017 if ($opt_testenv) {
1018 my $testenv_name = $ENV{SELFTEST_TESTENV};
1019 $testenv_name = $testenv_default unless defined($testenv_name);
1021 my $testenv_vars = setup_env($testenv_name, $prefix);
1023 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1024 die("Unable to setup environment $testenv_name");
1027 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1028 $ENV{ENVNAME} = $testenv_name;
1030 my $envvarstr = exported_envvars_str($testenv_vars);
1032 my @term_args = ("echo -e \"
1033 Welcome to the Samba4 Test environment '$testenv_name'
1035 This matches the client environment used in make test
1036 server is pid `cat \$PIDDIR/samba.pid`
1038 Some useful environment variables:
1039 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1040 SMB_CONF_PATH=\$SMB_CONF_PATH
1042 $envvarstr
1043 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1044 my @term = ();
1045 if ($ENV{TERMINAL}) {
1046 @term = ($ENV{TERMINAL});
1047 } else {
1048 @term = ("xterm", "-e");
1049 unshift(@term_args, ("bash", "-c"));
1052 system(@term, @term_args);
1054 teardown_env($testenv_name);
1055 } elsif ($opt_list) {
1056 foreach (@todo) {
1057 my $name = $$_[0];
1058 my $envname = $$_[1];
1059 my $cmd = $$_[2];
1060 my $listcmd = $$_[3];
1062 unless (defined($listcmd)) {
1063 warn("Unable to list tests in $name");
1064 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1065 # a single "test".
1066 print "$name\n";
1067 next;
1070 system($listcmd);
1072 if ($? == -1) {
1073 die("Unable to run $listcmd: $!");
1074 } elsif ($? & 127) {
1075 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1078 my $exitcode = $? >> 8;
1079 if ($exitcode != 0) {
1080 die("$cmd exited with exit code $exitcode");
1083 } else {
1084 foreach (@todo) {
1085 $i++;
1086 my $cmd = $$_[2];
1087 my $name = $$_[0];
1088 my $envname = $$_[1];
1089 my ($env_basename, $env_localpart) = split(/:/, $envname);
1090 my $envvars = "SKIP";
1092 if (@opt_include_env) {
1093 foreach my $env (@opt_include_env) {
1094 if ($env_basename eq $env) {
1095 $envvars = setup_env($envname, $prefix);
1098 } elsif (@opt_exclude_env) {
1099 my $excluded = 0;
1100 foreach my $env (@opt_exclude_env) {
1101 if ($env_basename eq $env) {
1102 $excluded = 1;
1105 if ($excluded == 0) {
1106 $envvars = setup_env($envname, $prefix);
1108 } else {
1109 $envvars = setup_env($envname, $prefix);
1112 if (not defined($envvars)) {
1113 Subunit::start_testsuite($name);
1114 Subunit::end_testsuite($name, "error",
1115 "unable to set up environment $envname - exiting");
1116 next;
1117 } elsif ($envvars eq "SKIP") {
1118 Subunit::start_testsuite($name);
1119 Subunit::end_testsuite($name, "skip",
1120 "environment $envname is disabled (via --exclude-env / --include-env command line options) in this test run - skipping");
1121 next;
1122 } elsif ($envvars eq "UNKNOWN") {
1123 Subunit::start_testsuite($name);
1124 Subunit::end_testsuite($name, "skip",
1125 "environment $envname is unknown in this test backend - skipping");
1126 next;
1129 # Generate a file with the individual tests to run, if the
1130 # test runner for this test suite supports it.
1131 if ($individual_tests and $individual_tests->{$name}) {
1132 if ($$_[3]) {
1133 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1134 foreach my $test (@{$individual_tests->{$name}}) {
1135 print $fh substr($test, length($name)+1) . "\n";
1137 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1138 } else {
1139 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1143 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1145 teardown_env($envname) if ($opt_resetup_env);
1149 print "\n";
1151 teardown_env($_) foreach (keys %running_envs);
1153 my $failed = 0;
1155 # if there were any valgrind failures, show them
1156 foreach (<$prefix/valgrind.log*>) {
1157 next unless (-s $_);
1158 print "VALGRIND FAILURE\n";
1159 $failed++;
1160 system("cat $_");
1162 exit 0;