s3-libnetapi: pass down ndr_interface_table to libnetapi_open_pipe().
[Samba.git] / selftest / selftest.pl
blobb60b76228d04199a9b2e8c9cc042175bf8aed37a
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 @testlists = ();
57 my $srcdir = ".";
58 my $bindir = "./bin";
59 my $prefix = "./st";
61 my @includes = ();
62 my @excludes = ();
64 sub pipe_handler {
65 my $sig = shift @_;
66 print STDERR "Exiting early because of SIGPIPE.\n";
67 exit(1);
70 $SIG{PIPE} = \&pipe_handler;
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 Subunit::progress_pop();
149 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
150 exit(1);
151 } elsif ($? & 127) {
152 Subunit::end_testsuite($name, "error",
153 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
154 exit(1);
157 my $exitcode = $? >> 8;
159 my $envlog = getlog_env($envname);
160 if ($envlog ne "") {
161 print "envlog: $envlog\n";
164 print "command: $cmd\n";
165 printf "expanded command: %s\n", expand_environment_strings($cmd);
167 if ($exitcode == 0) {
168 Subunit::end_testsuite($name, "success");
169 } else {
170 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
173 cleanup_pcap($pcap_file, $exitcode);
175 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
176 print "PCAP FILE: $pcap_file\n";
179 if ($exitcode != 0) {
180 exit(1) if ($opt_one);
183 return $exitcode;
186 sub ShowHelp()
188 print "Samba test runner
189 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
190 Copyright (C) Stefan Metzmacher <metze\@samba.org>
192 Usage: $Script [OPTIONS] TESTNAME-REGEX
194 Generic options:
195 --help this help page
196 --target=samba[3]|win Samba version to target
197 --testlist=FILE file to read available tests from
199 Paths:
200 --prefix=DIR prefix to run tests in [st]
201 --srcdir=DIR source directory [.]
202 --bindir=DIR binaries directory [./bin]
204 Target Specific:
205 --socket-wrapper-pcap save traffic to pcap directories
206 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
207 failed
208 --socket-wrapper enable socket wrapper
210 Samba4 Specific:
211 --ldap=openldap|fedora-ds back samba onto specified ldap server
213 Behaviour:
214 --quick run quick overall test
215 --one abort when the first test fails
216 --testenv run a shell in the requested test environment
217 --list list available tests
219 exit(0);
222 my $result = GetOptions (
223 'help|h|?' => \$opt_help,
224 'target=s' => \$opt_target,
225 'prefix=s' => \$prefix,
226 'socket-wrapper' => \$opt_socket_wrapper,
227 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
228 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
229 'quick' => \$opt_quick,
230 'one' => \$opt_one,
231 'exclude=s' => \@opt_exclude,
232 'include=s' => \@opt_include,
233 'srcdir=s' => \$srcdir,
234 'bindir=s' => \$bindir,
235 'testenv' => \$opt_testenv,
236 'list' => \$opt_list,
237 'ldap:s' => \$ldap,
238 'resetup-environment' => \$opt_resetup_env,
239 'testlist=s' => \@testlists,
240 'random-order' => \$opt_random_order,
241 'load-list=s' => \$opt_load_list,
242 'binary-mapping=s' => \$opt_binary_mapping
245 exit(1) if (not $result);
247 ShowHelp() if ($opt_help);
249 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
251 # we want unbuffered output
252 $| = 1;
254 my @tests = @ARGV;
256 # quick hack to disable rpc validation when using valgrind - its way too slow
257 unless (defined($ENV{VALGRIND})) {
258 $ENV{VALIDATE} = "validate";
259 $ENV{MALLOC_CHECK_} = 2;
262 # make all our python scripts unbuffered
263 $ENV{PYTHONUNBUFFERED} = 1;
265 my $bindir_abs = abs_path($bindir);
267 # Backwards compatibility:
268 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
269 if (defined($ENV{FEDORA_DS_ROOT})) {
270 $ldap = "fedora-ds";
271 } else {
272 $ldap = "openldap";
276 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
277 if ($ldap) {
278 # LDAP is slow
279 $torture_maxtime *= 2;
282 $prefix =~ s+//+/+;
283 $prefix =~ s+/./+/+;
284 $prefix =~ s+/$++;
286 die("using an empty prefix isn't allowed") unless $prefix ne "";
288 # Ensure we have the test prefix around.
290 # We need restrictive
291 # permissions on this as some subdirectories in this tree will have
292 # wider permissions (ie 0777) and this would allow other users on the
293 # host to subvert the test process.
294 mkdir($prefix, 0700) unless -d $prefix;
295 chmod 0700, $prefix;
297 my $prefix_abs = abs_path($prefix);
298 my $tmpdir_abs = abs_path("$prefix/tmp");
299 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
301 my $srcdir_abs = abs_path($srcdir);
303 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
304 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
306 $ENV{PREFIX} = $prefix;
307 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
308 $ENV{PREFIX_ABS} = $prefix_abs;
309 $ENV{SRCDIR} = $srcdir;
310 $ENV{SRCDIR_ABS} = $srcdir_abs;
311 $ENV{BINDIR} = $bindir_abs;
313 my $tls_enabled = not $opt_quick;
314 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
316 sub prefix_pathvar($$)
318 my ($name, $newpath) = @_;
319 if (defined($ENV{$name})) {
320 $ENV{$name} = "$newpath:$ENV{$name}";
321 } else {
322 $ENV{$name} = $newpath;
325 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
326 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
328 if ($opt_socket_wrapper_keep_pcap) {
329 # Socket wrapper keep pcap implies socket wrapper pcap
330 $opt_socket_wrapper_pcap = 1;
333 if ($opt_socket_wrapper_pcap) {
334 # Socket wrapper pcap implies socket wrapper
335 $opt_socket_wrapper = 1;
338 my $socket_wrapper_dir;
339 if ($opt_socket_wrapper) {
340 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
341 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
342 } elsif (not $opt_list) {
343 unless ($< == 0) {
344 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
348 my $target;
349 my $testenv_default = "none";
351 my %binary_mapping = ();
352 if ($opt_binary_mapping) {
353 my @binmapping_list = split(/,/, $opt_binary_mapping);
354 foreach my $mapping (@binmapping_list) {
355 my ($bin, $map) = split(/\:/, $mapping);
356 $binary_mapping{$bin} = $map;
360 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
362 # After this many seconds, the server will self-terminate. All tests
363 # must terminate in this time, and testenv will only stay alive this
364 # long
366 my $server_maxtime = 7500;
367 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
368 $server_maxtime = $ENV{SMBD_MAXTIME};
371 unless ($opt_list) {
372 if ($opt_target eq "samba") {
373 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
374 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
376 $testenv_default = "dc";
377 require target::Samba;
378 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
379 } elsif ($opt_target eq "samba3") {
380 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
381 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
383 $testenv_default = "member";
384 require target::Samba3;
385 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
389 sub read_test_regexes($)
391 my ($name) = @_;
392 my @ret = ();
393 open(LF, "<$name") or die("unable to read $name: $!");
394 while (<LF>) {
395 chomp;
396 next if (/^#/);
397 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
398 push (@ret, [$1, $4]);
399 } else {
400 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
401 push (@ret, [$_, undef]);
404 close(LF);
405 return @ret;
408 foreach (@opt_exclude) {
409 push (@excludes, read_test_regexes($_));
412 foreach (@opt_include) {
413 push (@includes, read_test_regexes($_));
416 my $interfaces = join(',', ("127.0.0.11/8",
417 "127.0.0.12/8",
418 "127.0.0.13/8",
419 "127.0.0.14/8",
420 "127.0.0.15/8",
421 "127.0.0.16/8"));
423 my $clientdir = "$prefix_abs/client";
425 my $conffile = "$clientdir/client.conf";
426 $ENV{SMB_CONF_PATH} = $conffile;
428 sub write_clientconf($$$)
430 my ($conffile, $clientdir, $vars) = @_;
432 mkdir("$clientdir", 0777) unless -d "$clientdir";
434 if ( -d "$clientdir/private" ) {
435 unlink <$clientdir/private/*>;
436 } else {
437 mkdir("$clientdir/private", 0777);
440 if ( -d "$clientdir/lockdir" ) {
441 unlink <$clientdir/lockdir/*>;
442 } else {
443 mkdir("$clientdir/lockdir", 0777);
446 if ( -d "$clientdir/statedir" ) {
447 unlink <$clientdir/statedir/*>;
448 } else {
449 mkdir("$clientdir/statedir", 0777);
452 if ( -d "$clientdir/cachedir" ) {
453 unlink <$clientdir/cachedir/*>;
454 } else {
455 mkdir("$clientdir/cachedir", 0777);
458 # this is ugly, but the ncalrpcdir needs exactly 0755
459 # otherwise tests fail.
460 my $mask = umask;
461 umask 0022;
462 if ( -d "$clientdir/ncalrpcdir/np" ) {
463 unlink <$clientdir/ncalrpcdir/np/*>;
464 rmdir "$clientdir/ncalrpcdir/np";
466 if ( -d "$clientdir/ncalrpcdir" ) {
467 unlink <$clientdir/ncalrpcdir/*>;
468 rmdir "$clientdir/ncalrpcdir";
470 mkdir("$clientdir/ncalrpcdir", 0755);
471 umask $mask;
473 open(CF, ">$conffile");
474 print CF "[global]\n";
475 print CF "\tnetbios name = client\n";
476 if (defined($vars->{DOMAIN})) {
477 print CF "\tworkgroup = $vars->{DOMAIN}\n";
479 if (defined($vars->{REALM})) {
480 print CF "\trealm = $vars->{REALM}\n";
482 if ($opt_socket_wrapper) {
483 print CF "\tinterfaces = $interfaces\n";
485 print CF "
486 private dir = $clientdir/private
487 lock dir = $clientdir/lockdir
488 state directory = $clientdir/statedir
489 cache directory = $clientdir/cachedir
490 ncalrpc dir = $clientdir/ncalrpcdir
491 name resolve order = file bcast
492 panic action = $RealBin/gdb_backtrace \%d
493 max xmit = 32K
494 notify:inotify = false
495 ldb:nosync = true
496 system:anonymous = true
497 client lanman auth = Yes
498 log level = 1
499 torture:basedir = $clientdir
500 #We don't want to pass our self-tests if the PAC code is wrong
501 gensec:require_pac = true
502 resolv:host file = $prefix_abs/dns_host_file
503 #We don't want to run 'speed' tests for very long
504 torture:timelimit = 1
506 close(CF);
509 my @todo = ();
511 sub should_run_test($)
513 my $name = shift;
514 if ($#tests == -1) {
515 return 1;
517 for (my $i=0; $i <= $#tests; $i++) {
518 if ($name =~ /$tests[$i]/i) {
519 return 1;
522 return 0;
525 sub read_testlist($)
527 my ($filename) = @_;
529 my @ret = ();
530 open(IN, $filename) or die("Unable to open $filename: $!");
532 while (<IN>) {
533 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
534 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
535 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
536 my $name = <IN>;
537 $name =~ s/\n//g;
538 my $env = <IN>;
539 $env =~ s/\n//g;
540 my $cmdline = <IN>;
541 $cmdline =~ s/\n//g;
542 if (should_run_test($name) == 1) {
543 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
545 } else {
546 print;
549 close(IN) or die("Error creating recipe");
550 return @ret;
553 if ($#testlists == -1) {
554 die("No testlists specified");
557 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
558 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
559 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
560 if ($opt_socket_wrapper) {
561 $ENV{SELFTEST_INTERFACES} = $interfaces;
562 } else {
563 $ENV{SELFTEST_INTERFACES} = "";
565 if ($opt_quick) {
566 $ENV{SELFTEST_QUICK} = "1";
567 } else {
568 $ENV{SELFTEST_QUICK} = "";
570 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
572 my @available = ();
573 foreach my $fn (@testlists) {
574 foreach (read_testlist($fn)) {
575 my $name = $$_[0];
576 next if (@includes and not defined(find_in_list(\@includes, $name)));
577 push (@available, $_);
581 my $restricted = undef;
582 my $restricted_used = {};
584 if ($opt_load_list) {
585 $restricted = [];
586 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
587 while (<LOAD_LIST>) {
588 chomp;
589 push (@$restricted, $_);
591 close(LOAD_LIST);
594 my $individual_tests = undef;
595 $individual_tests = {};
597 foreach my $testsuite (@available) {
598 my $name = $$testsuite[0];
599 my $skipreason = skip($name);
600 if (defined($restricted)) {
601 # Find the testsuite for this test
602 my $match = undef;
603 foreach my $r (@$restricted) {
604 if ($r eq $name) {
605 $individual_tests->{$name} = [];
606 $match = $r;
607 $restricted_used->{$r} = 1;
608 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
609 push(@{$individual_tests->{$name}}, $r);
610 $match = $r;
611 $restricted_used->{$r} = 1;
614 if ($match) {
615 if (defined($skipreason)) {
616 if (not $opt_list) {
617 Subunit::skip_testsuite($name, $skipreason);
619 } else {
620 push(@todo, $testsuite);
623 } elsif (defined($skipreason)) {
624 if (not $opt_list) {
625 Subunit::skip_testsuite($name, $skipreason);
627 } else {
628 push(@todo, $testsuite);
632 if (defined($restricted)) {
633 foreach (@$restricted) {
634 unless (defined($restricted_used->{$_})) {
635 print "No test or testsuite found matching $_\n";
638 } elsif ($#todo == -1) {
639 print STDERR "No tests to run\n";
640 exit(1);
643 my $suitestotal = $#todo + 1;
645 unless ($opt_list) {
646 Subunit::progress($suitestotal);
647 Subunit::report_time(time());
650 my $i = 0;
651 $| = 1;
653 my %running_envs = ();
655 sub get_running_env($)
657 my ($name) = @_;
659 my $envname = $name;
661 $envname =~ s/:.*//;
663 return $running_envs{$envname};
666 my @exported_envvars = (
667 # domain stuff
668 "DOMAIN",
669 "REALM",
671 # domain controller stuff
672 "DC_SERVER",
673 "DC_SERVER_IP",
674 "DC_NETBIOSNAME",
675 "DC_NETBIOSALIAS",
677 # domain member
678 "MEMBER_SERVER",
679 "MEMBER_SERVER_IP",
680 "MEMBER_NETBIOSNAME",
681 "MEMBER_NETBIOSALIAS",
683 # rpc proxy controller stuff
684 "RPC_PROXY_SERVER",
685 "RPC_PROXY_SERVER_IP",
686 "RPC_PROXY_NETBIOSNAME",
687 "RPC_PROXY_NETBIOSALIAS",
689 # domain controller stuff for Vampired DC
690 "VAMPIRE_DC_SERVER",
691 "VAMPIRE_DC_SERVER_IP",
692 "VAMPIRE_DC_NETBIOSNAME",
693 "VAMPIRE_DC_NETBIOSALIAS",
695 "PROMOTED_DC_SERVER",
696 "PROMOTED_DC_SERVER_IP",
697 "PROMOTED_DC_NETBIOSNAME",
698 "PROMOTED_DC_NETBIOSALIAS",
700 # server stuff
701 "SERVER",
702 "SERVER_IP",
703 "NETBIOSNAME",
704 "NETBIOSALIAS",
706 # user stuff
707 "USERNAME",
708 "USERID",
709 "PASSWORD",
710 "DC_USERNAME",
711 "DC_PASSWORD",
713 # misc stuff
714 "KRB5_CONFIG",
715 "WINBINDD_SOCKET_DIR",
716 "WINBINDD_PRIV_PIPE_DIR",
717 "NMBD_SOCKET_DIR",
718 "LOCAL_PATH",
720 # nss_wrapper
721 "NSS_WRAPPER_PASSWD",
722 "NSS_WRAPPER_GROUP",
724 # UID/GID for rfc2307 mapping tests
725 "UID_RFC2307TEST",
726 "GID_RFC2307TEST"
729 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
730 my $signame = shift;
731 teardown_env($_) foreach(keys %running_envs);
732 die("Received signal $signame");
735 sub setup_env($$)
737 my ($name, $prefix) = @_;
739 my $testenv_vars = undef;
741 my $envname = $name;
742 my $option = $name;
744 $envname =~ s/:.*//;
745 $option =~ s/^[^:]*//;
746 $option =~ s/^://;
748 $option = "client" if $option eq "";
750 if ($envname eq "none") {
751 $testenv_vars = {};
752 } elsif (defined(get_running_env($envname))) {
753 $testenv_vars = get_running_env($envname);
754 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
755 print $testenv_vars->{target}->getlog_env($testenv_vars);
756 $testenv_vars = undef;
758 } else {
759 $testenv_vars = $target->setup_env($envname, $prefix);
760 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
761 return $testenv_vars;
762 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
763 $testenv_vars->{target} = $target;
765 if (not defined($testenv_vars)) {
766 warn("$opt_target can't start up known environment '$envname'");
771 return undef unless defined($testenv_vars);
773 $running_envs{$envname} = $testenv_vars;
775 if ($option eq "local") {
776 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
777 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
778 } elsif ($option eq "client") {
779 SocketWrapper::set_default_iface(11);
780 write_clientconf($conffile, $clientdir, $testenv_vars);
781 $ENV{SMB_CONF_PATH} = $conffile;
782 } else {
783 die("Unknown option[$option] for envname[$envname]");
786 foreach (@exported_envvars) {
787 if (defined($testenv_vars->{$_})) {
788 $ENV{$_} = $testenv_vars->{$_};
789 } else {
790 delete $ENV{$_};
794 return $testenv_vars;
797 sub exported_envvars_str($)
799 my ($testenv_vars) = @_;
800 my $out = "";
802 foreach (@exported_envvars) {
803 next unless defined($testenv_vars->{$_});
804 $out .= $_."=".$testenv_vars->{$_}."\n";
807 return $out;
810 sub getlog_env($)
812 my ($envname) = @_;
813 return "" if ($envname eq "none");
814 my $env = get_running_env($envname);
815 return $env->{target}->getlog_env($env);
818 sub check_env($)
820 my ($envname) = @_;
821 return 1 if ($envname eq "none");
822 my $env = get_running_env($envname);
823 return $env->{target}->check_env($env);
826 sub teardown_env($)
828 my ($envname) = @_;
829 return if ($envname eq "none");
830 my $env = get_running_env($envname);
831 $env->{target}->teardown_env($env);
832 delete $running_envs{$envname};
835 # This 'global' file needs to be empty when we start
836 unlink("$prefix_abs/dns_host_file");
838 if ($opt_random_order) {
839 require List::Util;
840 my @newtodo = List::Util::shuffle(@todo);
841 @todo = @newtodo;
844 if ($opt_testenv) {
845 my $testenv_name = $ENV{SELFTEST_TESTENV};
846 $testenv_name = $testenv_default unless defined($testenv_name);
848 my $testenv_vars = setup_env($testenv_name, $prefix);
850 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
851 die("Unable to setup environment $testenv_name");
854 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
855 $ENV{ENVNAME} = $testenv_name;
857 my $envvarstr = exported_envvars_str($testenv_vars);
859 my @term_args = ("echo -e \"
860 Welcome to the Samba4 Test environment '$testenv_name'
862 This matches the client environment used in make test
863 server is pid `cat \$PIDDIR/samba.pid`
865 Some useful environment variables:
866 TORTURE_OPTIONS=\$TORTURE_OPTIONS
867 SMB_CONF_PATH=\$SMB_CONF_PATH
869 $envvarstr
870 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
871 my @term = ();
872 if ($ENV{TERMINAL}) {
873 @term = ($ENV{TERMINAL});
874 } else {
875 @term = ("xterm", "-e");
876 unshift(@term_args, ("bash", "-c"));
879 system(@term, @term_args);
881 teardown_env($testenv_name);
882 } elsif ($opt_list) {
883 foreach (@todo) {
884 my $cmd = $$_[2];
885 my $name = $$_[0];
886 my $envname = $$_[1];
888 unless($cmd =~ /\$LISTOPT/) {
889 warn("Unable to list tests in $name");
890 next;
893 $cmd =~ s/\$LISTOPT/--list/g;
895 system($cmd);
897 if ($? == -1) {
898 die("Unable to run $cmd: $!");
899 } elsif ($? & 127) {
900 die(sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
903 my $exitcode = $? >> 8;
904 if ($exitcode != 0) {
905 die("$cmd exited with exit code $exitcode");
908 } else {
909 foreach (@todo) {
910 $i++;
911 my $cmd = $$_[2];
912 my $name = $$_[0];
913 my $envname = $$_[1];
915 my $envvars = setup_env($envname, $prefix);
916 if (not defined($envvars)) {
917 Subunit::start_testsuite($name);
918 Subunit::end_testsuite($name, "error",
919 "unable to set up environment $envname - exiting");
920 next;
921 } elsif ($envvars eq "UNKNOWN") {
922 Subunit::start_testsuite($name);
923 Subunit::end_testsuite($name, "skip",
924 "environment $envname is unknown in this test backend - skipping");
925 next;
928 # Generate a file with the individual tests to run, if the
929 # test runner for this test suite supports it.
930 if ($individual_tests and $individual_tests->{$name}) {
931 if ($$_[3]) {
932 my ($fh, $listid_file) = tempfile(UNLINK => 0);
933 foreach my $test (@{$individual_tests->{$name}}) {
934 print $fh substr($test, length($name)+1) . "\n";
936 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
937 } elsif ($$_[4]) {
938 $cmd =~ s/\s+[^\s]+\s*$//;
939 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
943 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
945 teardown_env($envname) if ($opt_resetup_env);
949 print "\n";
951 teardown_env($_) foreach (keys %running_envs);
953 my $failed = 0;
955 # if there were any valgrind failures, show them
956 foreach (<$prefix/valgrind.log*>) {
957 next unless (-s $_);
958 print "VALGRIND FAILURE\n";
959 $failed++;
960 system("cat $_");
962 exit 0;