winbind: Allow winbindd to be run from inside "samba"
[Samba.git] / selftest / selftest.pl
blob97b24ed8658a19f3c2f07a41a66ef9e1ae6926e0
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;
31 eval {
32 require Time::HiRes;
33 Time::HiRes->import("time");
35 if ($@) {
36 print "You don't have Time::Hires installed !\n";
39 my $opt_help = 0;
40 my $opt_target = "samba";
41 my $opt_quick = 0;
42 my $opt_socket_wrapper = 0;
43 my $opt_socket_wrapper_pcap = undef;
44 my $opt_socket_wrapper_keep_pcap = undef;
45 my $opt_random_order = 0;
46 my $opt_one = 0;
47 my @opt_exclude = ();
48 my @opt_include = ();
49 my $opt_testenv = 0;
50 my $opt_list = 0;
51 my $ldap = undef;
52 my $opt_resetup_env = undef;
53 my $opt_binary_mapping = "";
54 my $opt_load_list = undef;
55 my $opt_libnss_wrapper_so_path = "";
56 my $opt_libsocket_wrapper_so_path = "";
57 my $opt_libuid_wrapper_so_path = "";
58 my @testlists = ();
60 my $srcdir = ".";
61 my $bindir = "./bin";
62 my $prefix = "./st";
64 my @includes = ();
65 my @excludes = ();
67 sub pipe_handler {
68 my $sig = shift @_;
69 print STDERR "Exiting early because of SIGPIPE.\n";
70 exit(1);
73 $SIG{PIPE} = \&pipe_handler;
75 sub find_in_list($$)
77 my ($list, $fullname) = @_;
79 foreach (@$list) {
80 if ($fullname =~ /$$_[0]/) {
81 return ($$_[1]) if ($$_[1]);
82 return "";
86 return undef;
89 sub skip($)
91 my ($name) = @_;
93 return find_in_list(\@excludes, $name);
96 sub getlog_env($);
98 sub setup_pcap($)
100 my ($name) = @_;
102 return unless ($opt_socket_wrapper_pcap);
103 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
105 my $fname = $name;
106 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
108 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
110 SocketWrapper::setup_pcap($pcap_file);
112 return $pcap_file;
115 sub cleanup_pcap($$)
117 my ($pcap_file, $exitcode) = @_;
119 return unless ($opt_socket_wrapper_pcap);
120 return if ($opt_socket_wrapper_keep_pcap);
121 return unless ($exitcode == 0);
122 return unless defined($pcap_file);
124 unlink($pcap_file);
127 # expand strings from %ENV
128 sub expand_environment_strings($)
130 my $s = shift;
131 # we use a reverse sort so we do the longer ones first
132 foreach my $k (sort { $b cmp $a } keys %ENV) {
133 $s =~ s/\$$k/$ENV{$k}/g;
135 return $s;
138 sub run_testsuite($$$$$)
140 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
141 my $pcap_file = setup_pcap($name);
143 Subunit::start_testsuite($name);
144 Subunit::progress_push();
145 Subunit::report_time(time());
146 system($cmd);
147 Subunit::report_time(time());
148 Subunit::progress_pop();
150 if ($? == -1) {
151 Subunit::progress_pop();
152 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
153 exit(1);
154 } elsif ($? & 127) {
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
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
202 Paths:
203 --prefix=DIR prefix to run tests in [st]
204 --srcdir=DIR source directory [.]
205 --bindir=DIR binaries directory [./bin]
207 Preload cwrap:
208 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
209 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
210 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
212 Target Specific:
213 --socket-wrapper-pcap save traffic to pcap directories
214 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
215 failed
216 --socket-wrapper enable socket wrapper
218 Samba4 Specific:
219 --ldap=openldap|fedora-ds back samba onto specified ldap server
221 Behaviour:
222 --quick run quick overall test
223 --one abort when the first test fails
224 --testenv run a shell in the requested test environment
225 --list list available tests
227 exit(0);
230 my $result = GetOptions (
231 'help|h|?' => \$opt_help,
232 'target=s' => \$opt_target,
233 'prefix=s' => \$prefix,
234 'socket-wrapper' => \$opt_socket_wrapper,
235 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
236 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
237 'quick' => \$opt_quick,
238 'one' => \$opt_one,
239 'exclude=s' => \@opt_exclude,
240 'include=s' => \@opt_include,
241 'srcdir=s' => \$srcdir,
242 'bindir=s' => \$bindir,
243 'testenv' => \$opt_testenv,
244 'list' => \$opt_list,
245 'ldap:s' => \$ldap,
246 'resetup-environment' => \$opt_resetup_env,
247 'testlist=s' => \@testlists,
248 'random-order' => \$opt_random_order,
249 'load-list=s' => \$opt_load_list,
250 'binary-mapping=s' => \$opt_binary_mapping,
251 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
252 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
253 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path
256 exit(1) if (not $result);
258 ShowHelp() if ($opt_help);
260 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
262 # we want unbuffered output
263 $| = 1;
265 my @tests = @ARGV;
267 # quick hack to disable rpc validation when using valgrind - its way too slow
268 unless (defined($ENV{VALGRIND})) {
269 $ENV{VALIDATE} = "validate";
270 $ENV{MALLOC_CHECK_} = 3;
273 # make all our python scripts unbuffered
274 $ENV{PYTHONUNBUFFERED} = 1;
276 my $bindir_abs = abs_path($bindir);
278 # Backwards compatibility:
279 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
280 if (defined($ENV{FEDORA_DS_ROOT})) {
281 $ldap = "fedora-ds";
282 } else {
283 $ldap = "openldap";
287 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
288 if ($ldap) {
289 # LDAP is slow
290 $torture_maxtime *= 2;
293 $prefix =~ s+//+/+;
294 $prefix =~ s+/./+/+;
295 $prefix =~ s+/$++;
297 die("using an empty prefix isn't allowed") unless $prefix ne "";
299 # Ensure we have the test prefix around.
301 # We need restrictive
302 # permissions on this as some subdirectories in this tree will have
303 # wider permissions (ie 0777) and this would allow other users on the
304 # host to subvert the test process.
305 mkdir($prefix, 0700) unless -d $prefix;
306 chmod 0700, $prefix;
308 my $prefix_abs = abs_path($prefix);
309 my $tmpdir_abs = abs_path("$prefix/tmp");
310 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
312 my $srcdir_abs = abs_path($srcdir);
314 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
315 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
317 $ENV{PREFIX} = $prefix;
318 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
319 $ENV{PREFIX_ABS} = $prefix_abs;
320 $ENV{SRCDIR} = $srcdir;
321 $ENV{SRCDIR_ABS} = $srcdir_abs;
322 $ENV{BINDIR} = $bindir_abs;
324 my $tls_enabled = not $opt_quick;
325 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
327 sub prefix_pathvar($$)
329 my ($name, $newpath) = @_;
330 if (defined($ENV{$name})) {
331 $ENV{$name} = "$newpath:$ENV{$name}";
332 } else {
333 $ENV{$name} = $newpath;
336 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
337 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
339 if ($opt_socket_wrapper_keep_pcap) {
340 # Socket wrapper keep pcap implies socket wrapper pcap
341 $opt_socket_wrapper_pcap = 1;
344 if ($opt_socket_wrapper_pcap) {
345 # Socket wrapper pcap implies socket wrapper
346 $opt_socket_wrapper = 1;
349 my $ld_preload = $ENV{LD_PRELOAD};
351 if ($opt_libnss_wrapper_so_path) {
352 if ($ld_preload) {
353 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
354 } else {
355 $ld_preload = "$opt_libnss_wrapper_so_path";
359 if ($opt_libsocket_wrapper_so_path) {
360 if ($ld_preload) {
361 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
362 } else {
363 $ld_preload = "$opt_libsocket_wrapper_so_path";
367 if ($opt_libuid_wrapper_so_path) {
368 if ($ld_preload) {
369 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
370 } else {
371 $ld_preload = "$opt_libuid_wrapper_so_path";
375 $ENV{LD_PRELOAD} = $ld_preload;
376 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
378 # Enable uid_wrapper globally
379 $ENV{UID_WRAPPER} = 1;
381 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
383 # This is needed in order to allow the ldb_*ldap module
384 # to work with a preloaded socket wrapper.
385 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
387 my $socket_wrapper_dir;
388 if ($opt_socket_wrapper) {
389 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
390 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
391 } elsif (not $opt_list) {
392 unless ($< == 0) {
393 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
397 my $target;
398 my $testenv_default = "none";
400 my %binary_mapping = ();
401 if ($opt_binary_mapping) {
402 my @binmapping_list = split(/,/, $opt_binary_mapping);
403 foreach my $mapping (@binmapping_list) {
404 my ($bin, $map) = split(/\:/, $mapping);
405 $binary_mapping{$bin} = $map;
409 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
411 # After this many seconds, the server will self-terminate. All tests
412 # must terminate in this time, and testenv will only stay alive this
413 # long
415 my $server_maxtime = 7500;
416 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
417 $server_maxtime = $ENV{SMBD_MAXTIME};
420 unless ($opt_list) {
421 if ($opt_target eq "samba") {
422 $testenv_default = "dc";
423 require target::Samba;
424 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
425 } elsif ($opt_target eq "samba3") {
426 $testenv_default = "member";
427 require target::Samba3;
428 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
432 sub read_test_regexes($)
434 my ($name) = @_;
435 my @ret = ();
436 open(LF, "<$name") or die("unable to read $name: $!");
437 while (<LF>) {
438 chomp;
439 next if (/^#/);
440 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
441 push (@ret, [$1, $4]);
442 } else {
443 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
444 push (@ret, [$_, undef]);
447 close(LF);
448 return @ret;
451 foreach (@opt_exclude) {
452 push (@excludes, read_test_regexes($_));
455 foreach (@opt_include) {
456 push (@includes, read_test_regexes($_));
459 my $interfaces = join(',', ("127.0.0.11/8",
460 "127.0.0.12/8",
461 "127.0.0.13/8",
462 "127.0.0.14/8",
463 "127.0.0.15/8",
464 "127.0.0.16/8"));
466 my $clientdir = "$prefix_abs/client";
468 my $conffile = "$clientdir/client.conf";
469 $ENV{SMB_CONF_PATH} = $conffile;
471 sub write_clientconf($$$)
473 my ($conffile, $clientdir, $vars) = @_;
475 mkdir("$clientdir", 0777) unless -d "$clientdir";
477 if ( -d "$clientdir/private" ) {
478 unlink <$clientdir/private/*>;
479 } else {
480 mkdir("$clientdir/private", 0777);
483 if ( -d "$clientdir/lockdir" ) {
484 unlink <$clientdir/lockdir/*>;
485 } else {
486 mkdir("$clientdir/lockdir", 0777);
489 if ( -d "$clientdir/statedir" ) {
490 unlink <$clientdir/statedir/*>;
491 } else {
492 mkdir("$clientdir/statedir", 0777);
495 if ( -d "$clientdir/cachedir" ) {
496 unlink <$clientdir/cachedir/*>;
497 } else {
498 mkdir("$clientdir/cachedir", 0777);
501 # this is ugly, but the ncalrpcdir needs exactly 0755
502 # otherwise tests fail.
503 my $mask = umask;
504 umask 0022;
505 if ( -d "$clientdir/ncalrpcdir/np" ) {
506 unlink <$clientdir/ncalrpcdir/np/*>;
507 rmdir "$clientdir/ncalrpcdir/np";
509 if ( -d "$clientdir/ncalrpcdir" ) {
510 unlink <$clientdir/ncalrpcdir/*>;
511 rmdir "$clientdir/ncalrpcdir";
513 mkdir("$clientdir/ncalrpcdir", 0755);
514 umask $mask;
516 open(CF, ">$conffile");
517 print CF "[global]\n";
518 print CF "\tnetbios name = client\n";
519 if (defined($vars->{DOMAIN})) {
520 print CF "\tworkgroup = $vars->{DOMAIN}\n";
522 if (defined($vars->{REALM})) {
523 print CF "\trealm = $vars->{REALM}\n";
525 if ($opt_socket_wrapper) {
526 print CF "\tinterfaces = $interfaces\n";
528 print CF "
529 private dir = $clientdir/private
530 lock dir = $clientdir/lockdir
531 state directory = $clientdir/statedir
532 cache directory = $clientdir/cachedir
533 ncalrpc dir = $clientdir/ncalrpcdir
534 name resolve order = file bcast
535 panic action = $RealBin/gdb_backtrace \%d
536 max xmit = 32K
537 notify:inotify = false
538 ldb:nosync = true
539 system:anonymous = true
540 client lanman auth = Yes
541 log level = 1
542 torture:basedir = $clientdir
543 #We don't want to pass our self-tests if the PAC code is wrong
544 gensec:require_pac = true
545 resolv:host file = $prefix_abs/dns_host_file
546 #We don't want to run 'speed' tests for very long
547 torture:timelimit = 1
549 close(CF);
552 my @todo = ();
554 sub should_run_test($)
556 my $name = shift;
557 if ($#tests == -1) {
558 return 1;
560 for (my $i=0; $i <= $#tests; $i++) {
561 if ($name =~ /$tests[$i]/i) {
562 return 1;
565 return 0;
568 sub read_testlist($)
570 my ($filename) = @_;
572 my @ret = ();
573 open(IN, $filename) or die("Unable to open $filename: $!");
575 while (<IN>) {
576 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
577 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
578 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
579 my $name = <IN>;
580 $name =~ s/\n//g;
581 my $env = <IN>;
582 $env =~ s/\n//g;
583 my $cmdline = <IN>;
584 $cmdline =~ s/\n//g;
585 if (should_run_test($name) == 1) {
586 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
588 } else {
589 print;
592 close(IN) or die("Error creating recipe");
593 return @ret;
596 if ($#testlists == -1) {
597 die("No testlists specified");
600 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
601 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
602 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
603 if ($opt_socket_wrapper) {
604 $ENV{SELFTEST_INTERFACES} = $interfaces;
605 } else {
606 $ENV{SELFTEST_INTERFACES} = "";
608 if ($opt_quick) {
609 $ENV{SELFTEST_QUICK} = "1";
610 } else {
611 $ENV{SELFTEST_QUICK} = "";
613 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
615 my @available = ();
616 foreach my $fn (@testlists) {
617 foreach (read_testlist($fn)) {
618 my $name = $$_[0];
619 next if (@includes and not defined(find_in_list(\@includes, $name)));
620 push (@available, $_);
624 my $restricted = undef;
625 my $restricted_used = {};
627 if ($opt_load_list) {
628 $restricted = [];
629 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
630 while (<LOAD_LIST>) {
631 chomp;
632 push (@$restricted, $_);
634 close(LOAD_LIST);
637 my $individual_tests = undef;
638 $individual_tests = {};
640 foreach my $testsuite (@available) {
641 my $name = $$testsuite[0];
642 my $skipreason = skip($name);
643 if (defined($restricted)) {
644 # Find the testsuite for this test
645 my $match = undef;
646 foreach my $r (@$restricted) {
647 if ($r eq $name) {
648 $individual_tests->{$name} = [];
649 $match = $r;
650 $restricted_used->{$r} = 1;
651 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
652 push(@{$individual_tests->{$name}}, $r);
653 $match = $r;
654 $restricted_used->{$r} = 1;
657 if ($match) {
658 if (defined($skipreason)) {
659 if (not $opt_list) {
660 Subunit::skip_testsuite($name, $skipreason);
662 } else {
663 push(@todo, $testsuite);
666 } elsif (defined($skipreason)) {
667 if (not $opt_list) {
668 Subunit::skip_testsuite($name, $skipreason);
670 } else {
671 push(@todo, $testsuite);
675 if (defined($restricted)) {
676 foreach (@$restricted) {
677 unless (defined($restricted_used->{$_})) {
678 print "No test or testsuite found matching $_\n";
681 } elsif ($#todo == -1) {
682 print STDERR "No tests to run\n";
683 exit(1);
686 my $suitestotal = $#todo + 1;
688 unless ($opt_list) {
689 Subunit::progress($suitestotal);
690 Subunit::report_time(time());
693 my $i = 0;
694 $| = 1;
696 my %running_envs = ();
698 sub get_running_env($)
700 my ($name) = @_;
702 my $envname = $name;
704 $envname =~ s/:.*//;
706 return $running_envs{$envname};
709 my @exported_envvars = (
710 # domain stuff
711 "DOMAIN",
712 "REALM",
714 # domain controller stuff
715 "DC_SERVER",
716 "DC_SERVER_IP",
717 "DC_NETBIOSNAME",
718 "DC_NETBIOSALIAS",
720 # domain member
721 "MEMBER_SERVER",
722 "MEMBER_SERVER_IP",
723 "MEMBER_NETBIOSNAME",
724 "MEMBER_NETBIOSALIAS",
726 # rpc proxy controller stuff
727 "RPC_PROXY_SERVER",
728 "RPC_PROXY_SERVER_IP",
729 "RPC_PROXY_NETBIOSNAME",
730 "RPC_PROXY_NETBIOSALIAS",
732 # domain controller stuff for Vampired DC
733 "VAMPIRE_DC_SERVER",
734 "VAMPIRE_DC_SERVER_IP",
735 "VAMPIRE_DC_NETBIOSNAME",
736 "VAMPIRE_DC_NETBIOSALIAS",
738 "PROMOTED_DC_SERVER",
739 "PROMOTED_DC_SERVER_IP",
740 "PROMOTED_DC_NETBIOSNAME",
741 "PROMOTED_DC_NETBIOSALIAS",
743 # server stuff
744 "SERVER",
745 "SERVER_IP",
746 "NETBIOSNAME",
747 "NETBIOSALIAS",
749 # user stuff
750 "USERNAME",
751 "USERID",
752 "PASSWORD",
753 "DC_USERNAME",
754 "DC_PASSWORD",
756 # misc stuff
757 "KRB5_CONFIG",
758 "SELFTEST_WINBINDD_SOCKET_DIR",
759 "WINBINDD_PRIV_PIPE_DIR",
760 "NMBD_SOCKET_DIR",
761 "LOCAL_PATH",
763 # nss_wrapper
764 "NSS_WRAPPER_PASSWD",
765 "NSS_WRAPPER_GROUP",
767 # UID/GID for rfc2307 mapping tests
768 "UID_RFC2307TEST",
769 "GID_RFC2307TEST"
772 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
773 my $signame = shift;
774 teardown_env($_) foreach(keys %running_envs);
775 die("Received signal $signame");
778 sub setup_env($$)
780 my ($name, $prefix) = @_;
782 my $testenv_vars = undef;
784 my $envname = $name;
785 my $option = $name;
787 $envname =~ s/:.*//;
788 $option =~ s/^[^:]*//;
789 $option =~ s/^://;
791 $option = "client" if $option eq "";
793 if ($envname eq "none") {
794 $testenv_vars = {};
795 } elsif (defined(get_running_env($envname))) {
796 $testenv_vars = get_running_env($envname);
797 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
798 print $testenv_vars->{target}->getlog_env($testenv_vars);
799 $testenv_vars = undef;
801 } else {
802 $testenv_vars = $target->setup_env($envname, $prefix);
803 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
804 return $testenv_vars;
805 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
806 $testenv_vars->{target} = $target;
808 if (not defined($testenv_vars)) {
809 warn("$opt_target can't start up known environment '$envname'");
814 return undef unless defined($testenv_vars);
816 $running_envs{$envname} = $testenv_vars;
818 if ($option eq "local") {
819 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
820 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
821 } elsif ($option eq "client") {
822 SocketWrapper::set_default_iface(11);
823 write_clientconf($conffile, $clientdir, $testenv_vars);
824 $ENV{SMB_CONF_PATH} = $conffile;
825 } else {
826 die("Unknown option[$option] for envname[$envname]");
829 foreach (@exported_envvars) {
830 if (defined($testenv_vars->{$_})) {
831 $ENV{$_} = $testenv_vars->{$_};
832 } else {
833 delete $ENV{$_};
837 return $testenv_vars;
840 sub exported_envvars_str($)
842 my ($testenv_vars) = @_;
843 my $out = "";
845 foreach (@exported_envvars) {
846 next unless defined($testenv_vars->{$_});
847 $out .= $_."=".$testenv_vars->{$_}."\n";
850 return $out;
853 sub getlog_env($)
855 my ($envname) = @_;
856 return "" if ($envname eq "none");
857 my $env = get_running_env($envname);
858 return $env->{target}->getlog_env($env);
861 sub check_env($)
863 my ($envname) = @_;
864 return 1 if ($envname eq "none");
865 my $env = get_running_env($envname);
866 return $env->{target}->check_env($env);
869 sub teardown_env($)
871 my ($envname) = @_;
872 return if ($envname eq "none");
873 my $env = get_running_env($envname);
874 $env->{target}->teardown_env($env);
875 delete $running_envs{$envname};
878 # This 'global' file needs to be empty when we start
879 unlink("$prefix_abs/dns_host_file");
880 unlink("$prefix_abs/hosts");
882 if ($opt_random_order) {
883 require List::Util;
884 my @newtodo = List::Util::shuffle(@todo);
885 @todo = @newtodo;
888 if ($opt_testenv) {
889 my $testenv_name = $ENV{SELFTEST_TESTENV};
890 $testenv_name = $testenv_default unless defined($testenv_name);
892 my $testenv_vars = setup_env($testenv_name, $prefix);
894 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
895 die("Unable to setup environment $testenv_name");
898 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
899 $ENV{ENVNAME} = $testenv_name;
901 my $envvarstr = exported_envvars_str($testenv_vars);
903 my @term_args = ("echo -e \"
904 Welcome to the Samba4 Test environment '$testenv_name'
906 This matches the client environment used in make test
907 server is pid `cat \$PIDDIR/samba.pid`
909 Some useful environment variables:
910 TORTURE_OPTIONS=\$TORTURE_OPTIONS
911 SMB_CONF_PATH=\$SMB_CONF_PATH
913 $envvarstr
914 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
915 my @term = ();
916 if ($ENV{TERMINAL}) {
917 @term = ($ENV{TERMINAL});
918 } else {
919 @term = ("xterm", "-e");
920 unshift(@term_args, ("bash", "-c"));
923 system(@term, @term_args);
925 teardown_env($testenv_name);
926 } elsif ($opt_list) {
927 foreach (@todo) {
928 my $cmd = $$_[2];
929 my $name = $$_[0];
930 my $envname = $$_[1];
932 unless($cmd =~ /\$LISTOPT/) {
933 warn("Unable to list tests in $name");
934 next;
937 $cmd =~ s/\$LISTOPT/--list/g;
939 system($cmd);
941 if ($? == -1) {
942 die("Unable to run $cmd: $!");
943 } elsif ($? & 127) {
944 die(sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
947 my $exitcode = $? >> 8;
948 if ($exitcode != 0) {
949 die("$cmd exited with exit code $exitcode");
952 } else {
953 foreach (@todo) {
954 $i++;
955 my $cmd = $$_[2];
956 my $name = $$_[0];
957 my $envname = $$_[1];
959 my $envvars = setup_env($envname, $prefix);
960 if (not defined($envvars)) {
961 Subunit::start_testsuite($name);
962 Subunit::end_testsuite($name, "error",
963 "unable to set up environment $envname - exiting");
964 next;
965 } elsif ($envvars eq "UNKNOWN") {
966 Subunit::start_testsuite($name);
967 Subunit::end_testsuite($name, "skip",
968 "environment $envname is unknown in this test backend - skipping");
969 next;
972 # Generate a file with the individual tests to run, if the
973 # test runner for this test suite supports it.
974 if ($individual_tests and $individual_tests->{$name}) {
975 if ($$_[3]) {
976 my ($fh, $listid_file) = tempfile(UNLINK => 0);
977 foreach my $test (@{$individual_tests->{$name}}) {
978 print $fh substr($test, length($name)+1) . "\n";
980 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
981 } elsif ($$_[4]) {
982 $cmd =~ s/\s+[^\s]+\s*$//;
983 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
987 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
989 teardown_env($envname) if ($opt_resetup_env);
993 print "\n";
995 teardown_env($_) foreach (keys %running_envs);
997 my $failed = 0;
999 # if there were any valgrind failures, show them
1000 foreach (<$prefix/valgrind.log*>) {
1001 next unless (-s $_);
1002 print "VALGRIND FAILURE\n";
1003 $failed++;
1004 system("cat $_");
1006 exit 0;