s3: smbd: VFS: Add vfs_stat_smb_basename() - to be called when we *know* stream name...
[Samba.git] / selftest / selftest.pl
blob25e0f936e04529fbb4b84b5eabc34ed02f1c5bbf
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 find_in_list($$)
66 my ($list, $fullname) = @_;
68 foreach (@$list) {
69 if ($fullname =~ /$$_[0]/) {
70 return ($$_[1]) if ($$_[1]);
71 return "";
75 return undef;
78 sub skip($)
80 my ($name) = @_;
82 return find_in_list(\@excludes, $name);
85 sub getlog_env($);
87 sub setup_pcap($)
89 my ($name) = @_;
91 return unless ($opt_socket_wrapper_pcap);
92 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
94 my $fname = $name;
95 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
97 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
99 SocketWrapper::setup_pcap($pcap_file);
101 return $pcap_file;
104 sub cleanup_pcap($$)
106 my ($pcap_file, $exitcode) = @_;
108 return unless ($opt_socket_wrapper_pcap);
109 return if ($opt_socket_wrapper_keep_pcap);
110 return unless ($exitcode == 0);
111 return unless defined($pcap_file);
113 unlink($pcap_file);
116 # expand strings from %ENV
117 sub expand_environment_strings($)
119 my $s = shift;
120 # we use a reverse sort so we do the longer ones first
121 foreach my $k (sort { $b cmp $a } keys %ENV) {
122 $s =~ s/\$$k/$ENV{$k}/g;
124 return $s;
127 sub run_testsuite($$$$$)
129 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
130 my $pcap_file = setup_pcap($name);
132 Subunit::start_testsuite($name);
133 Subunit::progress_push();
134 Subunit::report_time(time());
135 system($cmd);
136 Subunit::report_time(time());
137 Subunit::progress_pop();
139 if ($? == -1) {
140 Subunit::progress_pop();
141 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
142 exit(1);
143 } elsif ($? & 127) {
144 Subunit::end_testsuite($name, "error",
145 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
146 exit(1);
149 my $exitcode = $? >> 8;
151 my $envlog = getlog_env($envname);
152 if ($envlog ne "") {
153 print "envlog: $envlog\n";
156 print "command: $cmd\n";
157 printf "expanded command: %s\n", expand_environment_strings($cmd);
159 if ($exitcode == 0) {
160 Subunit::end_testsuite($name, "success");
161 } else {
162 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
165 cleanup_pcap($pcap_file, $exitcode);
167 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
168 print "PCAP FILE: $pcap_file\n";
171 if ($exitcode != 0) {
172 exit(1) if ($opt_one);
175 return $exitcode;
178 sub ShowHelp()
180 print "Samba test runner
181 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
182 Copyright (C) Stefan Metzmacher <metze\@samba.org>
184 Usage: $Script [OPTIONS] TESTNAME-REGEX
186 Generic options:
187 --help this help page
188 --target=samba[3]|win Samba version to target
189 --testlist=FILE file to read available tests from
191 Paths:
192 --prefix=DIR prefix to run tests in [st]
193 --srcdir=DIR source directory [.]
194 --bindir=DIR binaries directory [./bin]
196 Target Specific:
197 --socket-wrapper-pcap save traffic to pcap directories
198 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
199 failed
200 --socket-wrapper enable socket wrapper
202 Samba4 Specific:
203 --ldap=openldap|fedora-ds back samba onto specified ldap server
205 Behaviour:
206 --quick run quick overall test
207 --one abort when the first test fails
208 --testenv run a shell in the requested test environment
209 --list list available tests
211 exit(0);
214 my $result = GetOptions (
215 'help|h|?' => \$opt_help,
216 'target=s' => \$opt_target,
217 'prefix=s' => \$prefix,
218 'socket-wrapper' => \$opt_socket_wrapper,
219 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
220 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
221 'quick' => \$opt_quick,
222 'one' => \$opt_one,
223 'exclude=s' => \@opt_exclude,
224 'include=s' => \@opt_include,
225 'srcdir=s' => \$srcdir,
226 'bindir=s' => \$bindir,
227 'testenv' => \$opt_testenv,
228 'list' => \$opt_list,
229 'ldap:s' => \$ldap,
230 'resetup-environment' => \$opt_resetup_env,
231 'testlist=s' => \@testlists,
232 'random-order' => \$opt_random_order,
233 'load-list=s' => \$opt_load_list,
234 'binary-mapping=s' => \$opt_binary_mapping
237 exit(1) if (not $result);
239 ShowHelp() if ($opt_help);
241 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
243 # we want unbuffered output
244 $| = 1;
246 my @tests = @ARGV;
248 # quick hack to disable rpc validation when using valgrind - its way too slow
249 unless (defined($ENV{VALGRIND})) {
250 $ENV{VALIDATE} = "validate";
251 $ENV{MALLOC_CHECK_} = 2;
254 # make all our python scripts unbuffered
255 $ENV{PYTHONUNBUFFERED} = 1;
257 my $bindir_abs = abs_path($bindir);
259 # Backwards compatibility:
260 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
261 if (defined($ENV{FEDORA_DS_ROOT})) {
262 $ldap = "fedora-ds";
263 } else {
264 $ldap = "openldap";
268 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
269 if ($ldap) {
270 # LDAP is slow
271 $torture_maxtime *= 2;
274 $prefix =~ s+//+/+;
275 $prefix =~ s+/./+/+;
276 $prefix =~ s+/$++;
278 die("using an empty prefix isn't allowed") unless $prefix ne "";
280 # Ensure we have the test prefix around.
282 # We need restrictive
283 # permissions on this as some subdirectories in this tree will have
284 # wider permissions (ie 0777) and this would allow other users on the
285 # host to subvert the test process.
286 mkdir($prefix, 0700) unless -d $prefix;
287 chmod 0700, $prefix;
289 my $prefix_abs = abs_path($prefix);
290 my $tmpdir_abs = abs_path("$prefix/tmp");
291 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
293 my $srcdir_abs = abs_path($srcdir);
295 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
296 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
298 $ENV{PREFIX} = $prefix;
299 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
300 $ENV{PREFIX_ABS} = $prefix_abs;
301 $ENV{SRCDIR} = $srcdir;
302 $ENV{SRCDIR_ABS} = $srcdir_abs;
303 $ENV{BINDIR} = $bindir_abs;
305 my $tls_enabled = not $opt_quick;
306 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
308 sub prefix_pathvar($$)
310 my ($name, $newpath) = @_;
311 if (defined($ENV{$name})) {
312 $ENV{$name} = "$newpath:$ENV{$name}";
313 } else {
314 $ENV{$name} = $newpath;
317 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
318 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
320 if ($opt_socket_wrapper_keep_pcap) {
321 # Socket wrapper keep pcap implies socket wrapper pcap
322 $opt_socket_wrapper_pcap = 1;
325 if ($opt_socket_wrapper_pcap) {
326 # Socket wrapper pcap implies socket wrapper
327 $opt_socket_wrapper = 1;
330 my $socket_wrapper_dir;
331 if ($opt_socket_wrapper) {
332 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
333 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
334 } elsif (not $opt_list) {
335 unless ($< == 0) {
336 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
340 my $target;
341 my $testenv_default = "none";
343 my %binary_mapping = ();
344 if ($opt_binary_mapping) {
345 my @binmapping_list = split(/,/, $opt_binary_mapping);
346 foreach my $mapping (@binmapping_list) {
347 my ($bin, $map) = split(/\:/, $mapping);
348 $binary_mapping{$bin} = $map;
352 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
354 # After this many seconds, the server will self-terminate. All tests
355 # must terminate in this time, and testenv will only stay alive this
356 # long
358 my $server_maxtime = 7500;
359 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
360 $server_maxtime = $ENV{SMBD_MAXTIME};
363 unless ($opt_list) {
364 if ($opt_target eq "samba") {
365 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
366 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
368 $testenv_default = "dc";
369 require target::Samba;
370 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
371 } elsif ($opt_target eq "samba3") {
372 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
373 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
375 $testenv_default = "member";
376 require target::Samba3;
377 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
381 sub read_test_regexes($)
383 my ($name) = @_;
384 my @ret = ();
385 open(LF, "<$name") or die("unable to read $name: $!");
386 while (<LF>) {
387 chomp;
388 next if (/^#/);
389 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
390 push (@ret, [$1, $4]);
391 } else {
392 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
393 push (@ret, [$_, undef]);
396 close(LF);
397 return @ret;
400 foreach (@opt_exclude) {
401 push (@excludes, read_test_regexes($_));
404 foreach (@opt_include) {
405 push (@includes, read_test_regexes($_));
408 my $interfaces = join(',', ("127.0.0.11/8",
409 "127.0.0.12/8",
410 "127.0.0.13/8",
411 "127.0.0.14/8",
412 "127.0.0.15/8",
413 "127.0.0.16/8"));
415 my $clientdir = "$prefix_abs/client";
417 my $conffile = "$clientdir/client.conf";
418 $ENV{SMB_CONF_PATH} = $conffile;
420 sub write_clientconf($$$)
422 my ($conffile, $clientdir, $vars) = @_;
424 mkdir("$clientdir", 0777) unless -d "$clientdir";
426 if ( -d "$clientdir/private" ) {
427 unlink <$clientdir/private/*>;
428 } else {
429 mkdir("$clientdir/private", 0777);
432 if ( -d "$clientdir/lockdir" ) {
433 unlink <$clientdir/lockdir/*>;
434 } else {
435 mkdir("$clientdir/lockdir", 0777);
438 if ( -d "$clientdir/statedir" ) {
439 unlink <$clientdir/statedir/*>;
440 } else {
441 mkdir("$clientdir/statedir", 0777);
444 if ( -d "$clientdir/cachedir" ) {
445 unlink <$clientdir/cachedir/*>;
446 } else {
447 mkdir("$clientdir/cachedir", 0777);
450 # this is ugly, but the ncalrpcdir needs exactly 0755
451 # otherwise tests fail.
452 my $mask = umask;
453 umask 0022;
454 if ( -d "$clientdir/ncalrpcdir/np" ) {
455 unlink <$clientdir/ncalrpcdir/np/*>;
456 rmdir "$clientdir/ncalrpcdir/np";
458 if ( -d "$clientdir/ncalrpcdir" ) {
459 unlink <$clientdir/ncalrpcdir/*>;
460 rmdir "$clientdir/ncalrpcdir";
462 mkdir("$clientdir/ncalrpcdir", 0755);
463 umask $mask;
465 open(CF, ">$conffile");
466 print CF "[global]\n";
467 print CF "\tnetbios name = client\n";
468 if (defined($vars->{DOMAIN})) {
469 print CF "\tworkgroup = $vars->{DOMAIN}\n";
471 if (defined($vars->{REALM})) {
472 print CF "\trealm = $vars->{REALM}\n";
474 if ($opt_socket_wrapper) {
475 print CF "\tinterfaces = $interfaces\n";
477 print CF "
478 private dir = $clientdir/private
479 lock dir = $clientdir/lockdir
480 state directory = $clientdir/statedir
481 cache directory = $clientdir/cachedir
482 ncalrpc dir = $clientdir/ncalrpcdir
483 name resolve order = file bcast
484 panic action = $RealBin/gdb_backtrace \%d
485 max xmit = 32K
486 notify:inotify = false
487 ldb:nosync = true
488 system:anonymous = true
489 client lanman auth = Yes
490 log level = 1
491 torture:basedir = $clientdir
492 #We don't want to pass our self-tests if the PAC code is wrong
493 gensec:require_pac = true
494 resolv:host file = $prefix_abs/dns_host_file
495 #We don't want to run 'speed' tests for very long
496 torture:timelimit = 1
498 close(CF);
501 my @todo = ();
503 sub should_run_test($)
505 my $name = shift;
506 if ($#tests == -1) {
507 return 1;
509 for (my $i=0; $i <= $#tests; $i++) {
510 if ($name =~ /$tests[$i]/i) {
511 return 1;
514 return 0;
517 sub read_testlist($)
519 my ($filename) = @_;
521 my @ret = ();
522 open(IN, $filename) or die("Unable to open $filename: $!");
524 while (<IN>) {
525 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
526 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
527 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
528 my $name = <IN>;
529 $name =~ s/\n//g;
530 my $env = <IN>;
531 $env =~ s/\n//g;
532 my $cmdline = <IN>;
533 $cmdline =~ s/\n//g;
534 if (should_run_test($name) == 1) {
535 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
537 } else {
538 print;
541 close(IN) or die("Error creating recipe");
542 return @ret;
545 if ($#testlists == -1) {
546 die("No testlists specified");
549 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
550 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
551 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
552 if ($opt_socket_wrapper) {
553 $ENV{SELFTEST_INTERFACES} = $interfaces;
554 } else {
555 $ENV{SELFTEST_INTERFACES} = "";
557 if ($opt_quick) {
558 $ENV{SELFTEST_QUICK} = "1";
559 } else {
560 $ENV{SELFTEST_QUICK} = "";
562 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
564 my @available = ();
565 foreach my $fn (@testlists) {
566 foreach (read_testlist($fn)) {
567 my $name = $$_[0];
568 next if (@includes and not defined(find_in_list(\@includes, $name)));
569 push (@available, $_);
573 my $restricted = undef;
574 my $restricted_used = {};
576 if ($opt_load_list) {
577 $restricted = [];
578 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
579 while (<LOAD_LIST>) {
580 chomp;
581 push (@$restricted, $_);
583 close(LOAD_LIST);
586 my $individual_tests = undef;
587 $individual_tests = {};
589 foreach my $testsuite (@available) {
590 my $name = $$testsuite[0];
591 my $skipreason = skip($name);
592 if (defined($restricted)) {
593 # Find the testsuite for this test
594 my $match = undef;
595 foreach my $r (@$restricted) {
596 if ($r eq $name) {
597 $individual_tests->{$name} = [];
598 $match = $r;
599 $restricted_used->{$r} = 1;
600 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
601 push(@{$individual_tests->{$name}}, $r);
602 $match = $r;
603 $restricted_used->{$r} = 1;
606 if ($match) {
607 if (defined($skipreason)) {
608 if (not $opt_list) {
609 Subunit::skip_testsuite($name, $skipreason);
611 } else {
612 push(@todo, $testsuite);
615 } elsif (defined($skipreason)) {
616 if (not $opt_list) {
617 Subunit::skip_testsuite($name, $skipreason);
619 } else {
620 push(@todo, $testsuite);
624 if (defined($restricted)) {
625 foreach (@$restricted) {
626 unless (defined($restricted_used->{$_})) {
627 print "No test or testsuite found matching $_\n";
630 } elsif ($#todo == -1) {
631 print STDERR "No tests to run\n";
632 exit(1);
635 my $suitestotal = $#todo + 1;
637 unless ($opt_list) {
638 Subunit::progress($suitestotal);
639 Subunit::report_time(time());
642 my $i = 0;
643 $| = 1;
645 my %running_envs = ();
647 sub get_running_env($)
649 my ($name) = @_;
651 my $envname = $name;
653 $envname =~ s/:.*//;
655 return $running_envs{$envname};
658 my @exported_envvars = (
659 # domain stuff
660 "DOMAIN",
661 "REALM",
663 # domain controller stuff
664 "DC_SERVER",
665 "DC_SERVER_IP",
666 "DC_SERVER_IPV6",
667 "DC_NETBIOSNAME",
668 "DC_NETBIOSALIAS",
670 # domain member
671 "MEMBER_SERVER",
672 "MEMBER_SERVER_IP",
673 "MEMBER_SERVER_IPV6",
674 "MEMBER_NETBIOSNAME",
675 "MEMBER_NETBIOSALIAS",
677 # rpc proxy controller stuff
678 "RPC_PROXY_SERVER",
679 "RPC_PROXY_SERVER_IP",
680 "RPC_PROXY_SERVER_IPV6",
681 "RPC_PROXY_NETBIOSNAME",
682 "RPC_PROXY_NETBIOSALIAS",
684 # domain controller stuff for Vampired DC
685 "VAMPIRE_DC_SERVER",
686 "VAMPIRE_DC_SERVER_IP",
687 "VAMPIRE_DC_SERVER_IPV6",
688 "VAMPIRE_DC_NETBIOSNAME",
689 "VAMPIRE_DC_NETBIOSALIAS",
691 "PROMOTED_DC_SERVER",
692 "PROMOTED_DC_SERVER_IP",
693 "PROMOTED_DC_SERVER_IPV6",
694 "PROMOTED_DC_NETBIOSNAME",
695 "PROMOTED_DC_NETBIOSALIAS",
697 # server stuff
698 "SERVER",
699 "SERVER_IP",
700 "SERVER_IPV6",
701 "NETBIOSNAME",
702 "NETBIOSALIAS",
704 # user stuff
705 "USERNAME",
706 "USERID",
707 "PASSWORD",
708 "DC_USERNAME",
709 "DC_PASSWORD",
711 # misc stuff
712 "KRB5_CONFIG",
713 "WINBINDD_SOCKET_DIR",
714 "WINBINDD_PRIV_PIPE_DIR",
715 "NMBD_SOCKET_DIR",
716 "LOCAL_PATH",
718 # nss_wrapper
719 "NSS_WRAPPER_PASSWD",
720 "NSS_WRAPPER_GROUP",
722 # UID/GID for rfc2307 mapping tests
723 "UID_RFC2307TEST",
724 "GID_RFC2307TEST"
727 sub sighandler($)
729 my $signame = shift;
731 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
732 $SIG{PIPE} = 'IGNORE';
734 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
736 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
737 teardown_env($_) foreach(keys %running_envs);
738 system("pstree -p $$");
739 print "$0: PID[$$]: Exiting...\n";
740 exit(1);
743 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
745 sub setup_env($$)
747 my ($name, $prefix) = @_;
749 my $testenv_vars = undef;
751 my $envname = $name;
752 my $option = $name;
754 $envname =~ s/:.*//;
755 $option =~ s/^[^:]*//;
756 $option =~ s/^://;
758 $option = "client" if $option eq "";
760 if ($envname eq "none") {
761 $testenv_vars = {};
762 } elsif (defined(get_running_env($envname))) {
763 $testenv_vars = get_running_env($envname);
764 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
765 print $testenv_vars->{target}->getlog_env($testenv_vars);
766 $testenv_vars = undef;
768 } else {
769 $testenv_vars = $target->setup_env($envname, $prefix);
770 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
771 return $testenv_vars;
772 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
773 $testenv_vars->{target} = $target;
775 if (not defined($testenv_vars)) {
776 warn("$opt_target can't start up known environment '$envname'");
781 return undef unless defined($testenv_vars);
783 $running_envs{$envname} = $testenv_vars;
785 if ($option eq "local") {
786 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
787 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
788 } elsif ($option eq "client") {
789 SocketWrapper::set_default_iface(11);
790 write_clientconf($conffile, $clientdir, $testenv_vars);
791 $ENV{SMB_CONF_PATH} = $conffile;
792 } else {
793 die("Unknown option[$option] for envname[$envname]");
796 foreach (@exported_envvars) {
797 if (defined($testenv_vars->{$_})) {
798 $ENV{$_} = $testenv_vars->{$_};
799 } else {
800 delete $ENV{$_};
804 return $testenv_vars;
807 sub exported_envvars_str($)
809 my ($testenv_vars) = @_;
810 my $out = "";
812 foreach (@exported_envvars) {
813 next unless defined($testenv_vars->{$_});
814 $out .= $_."=".$testenv_vars->{$_}."\n";
817 return $out;
820 sub getlog_env($)
822 my ($envname) = @_;
823 return "" if ($envname eq "none");
824 my $env = get_running_env($envname);
825 return $env->{target}->getlog_env($env);
828 sub check_env($)
830 my ($envname) = @_;
831 return 1 if ($envname eq "none");
832 my $env = get_running_env($envname);
833 return $env->{target}->check_env($env);
836 sub teardown_env($)
838 my ($envname) = @_;
839 return if ($envname eq "none");
840 print STDERR "teardown_env($envname)\n";
841 my $env = get_running_env($envname);
842 $env->{target}->teardown_env($env);
843 delete $running_envs{$envname};
846 # This 'global' file needs to be empty when we start
847 unlink("$prefix_abs/dns_host_file");
849 if ($opt_random_order) {
850 require List::Util;
851 my @newtodo = List::Util::shuffle(@todo);
852 @todo = @newtodo;
855 if ($opt_testenv) {
856 my $testenv_name = $ENV{SELFTEST_TESTENV};
857 $testenv_name = $testenv_default unless defined($testenv_name);
859 my $testenv_vars = setup_env($testenv_name, $prefix);
861 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
863 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
864 $ENV{ENVNAME} = $testenv_name;
866 my $envvarstr = exported_envvars_str($testenv_vars);
868 my @term_args = ("echo -e \"
869 Welcome to the Samba4 Test environment '$testenv_name'
871 This matches the client environment used in make test
872 server is pid `cat \$PIDDIR/samba.pid`
874 Some useful environment variables:
875 TORTURE_OPTIONS=\$TORTURE_OPTIONS
876 SMB_CONF_PATH=\$SMB_CONF_PATH
878 $envvarstr
879 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
880 my @term = ();
881 if ($ENV{TERMINAL}) {
882 @term = ($ENV{TERMINAL});
883 } else {
884 @term = ("xterm", "-e");
885 unshift(@term_args, ("bash", "-c"));
888 system(@term, @term_args);
890 teardown_env($testenv_name);
891 } elsif ($opt_list) {
892 foreach (@todo) {
893 my $cmd = $$_[2];
894 my $name = $$_[0];
895 my $envname = $$_[1];
897 unless($cmd =~ /\$LISTOPT/) {
898 warn("Unable to list tests in $name");
899 next;
902 $cmd =~ s/\$LISTOPT/--list/g;
904 system($cmd);
906 if ($? == -1) {
907 die("Unable to run $cmd: $!");
908 } elsif ($? & 127) {
909 die(sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
912 my $exitcode = $? >> 8;
913 if ($exitcode != 0) {
914 die("$cmd exited with exit code $exitcode");
917 } else {
918 foreach (@todo) {
919 $i++;
920 my $cmd = $$_[2];
921 my $name = $$_[0];
922 my $envname = $$_[1];
924 my $envvars = setup_env($envname, $prefix);
925 if (not defined($envvars)) {
926 Subunit::start_testsuite($name);
927 Subunit::end_testsuite($name, "error",
928 "unable to set up environment $envname - exiting");
929 next;
930 } elsif ($envvars eq "UNKNOWN") {
931 Subunit::start_testsuite($name);
932 Subunit::end_testsuite($name, "skip",
933 "environment $envname is unknown in this test backend - skipping");
934 next;
937 # Generate a file with the individual tests to run, if the
938 # test runner for this test suite supports it.
939 if ($individual_tests and $individual_tests->{$name}) {
940 if ($$_[3]) {
941 my ($fh, $listid_file) = tempfile(UNLINK => 0);
942 foreach my $test (@{$individual_tests->{$name}}) {
943 print $fh substr($test, length($name)+1) . "\n";
945 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
946 } elsif ($$_[4]) {
947 $cmd =~ s/\s+[^\s]+\s*$//;
948 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
952 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
954 teardown_env($envname) if ($opt_resetup_env);
958 print "\n";
960 teardown_env($_) foreach (keys %running_envs);
962 my $failed = 0;
964 # if there were any valgrind failures, show them
965 foreach (<$prefix/valgrind.log*>) {
966 next unless (-s $_);
967 print "VALGRIND FAILURE\n";
968 $failed++;
969 system("cat $_");
971 exit 0;