s4:selftest: change the blackbox.kinit test to use a binary mapping for smbclient
[Samba/gebeck_regimport.git] / selftest / selftest.pl
blobcdcd2e4048fc92ce03228350ebf17113752c22e6
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_one = 0;
46 my @opt_exclude = ();
47 my @opt_include = ();
48 my $opt_testenv = 0;
49 my $opt_list = 0;
50 my $ldap = undef;
51 my $opt_resetup_env = undef;
52 my $opt_binary_mapping = "";
53 my $opt_load_list = undef;
54 my @testlists = ();
56 my $srcdir = ".";
57 my $bindir = "./bin";
58 my $prefix = "./st";
60 my @includes = ();
61 my @excludes = ();
63 sub pipe_handler {
64 my $sig = shift @_;
65 print STDERR "Exiting early because of SIGPIPE.\n";
66 exit(1);
69 $SIG{PIPE} = \&pipe_handler;
71 sub find_in_list($$)
73 my ($list, $fullname) = @_;
75 foreach (@$list) {
76 if ($fullname =~ /$$_[0]/) {
77 return ($$_[1]) if ($$_[1]);
78 return "";
82 return undef;
85 sub skip($)
87 my ($name) = @_;
89 return find_in_list(\@excludes, $name);
92 sub getlog_env($);
94 sub setup_pcap($)
96 my ($name) = @_;
98 return unless ($opt_socket_wrapper_pcap);
99 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
101 my $fname = $name;
102 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
104 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
106 SocketWrapper::setup_pcap($pcap_file);
108 return $pcap_file;
111 sub cleanup_pcap($$)
113 my ($pcap_file, $exitcode) = @_;
115 return unless ($opt_socket_wrapper_pcap);
116 return if ($opt_socket_wrapper_keep_pcap);
117 return unless ($exitcode == 0);
118 return unless defined($pcap_file);
120 unlink($pcap_file);
123 # expand strings from %ENV
124 sub expand_environment_strings($)
126 my $s = shift;
127 # we use a reverse sort so we do the longer ones first
128 foreach my $k (sort { $b cmp $a } keys %ENV) {
129 $s =~ s/\$$k/$ENV{$k}/g;
131 return $s;
134 sub run_testsuite($$$$$)
136 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
137 my $pcap_file = setup_pcap($name);
139 Subunit::start_testsuite($name);
140 Subunit::progress_push();
141 Subunit::report_time(time());
142 system($cmd);
143 Subunit::report_time(time());
144 Subunit::progress_pop();
146 if ($? == -1) {
147 Subunit::progress_pop();
148 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
149 exit(1);
150 } elsif ($? & 127) {
151 Subunit::end_testsuite($name, "error",
152 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
153 exit(1);
156 my $exitcode = $? >> 8;
158 my $envlog = getlog_env($envname);
159 if ($envlog ne "") {
160 print "envlog: $envlog\n";
163 print "command: $cmd\n";
164 printf "expanded command: %s\n", expand_environment_strings($cmd);
166 if ($exitcode == 0) {
167 Subunit::end_testsuite($name, "success");
168 } else {
169 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
172 cleanup_pcap($pcap_file, $exitcode);
174 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
175 print "PCAP FILE: $pcap_file\n";
178 if ($exitcode != 0) {
179 exit(1) if ($opt_one);
182 return $exitcode;
185 sub ShowHelp()
187 print "Samba test runner
188 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
189 Copyright (C) Stefan Metzmacher <metze\@samba.org>
191 Usage: $Script [OPTIONS] TESTNAME-REGEX
193 Generic options:
194 --help this help page
195 --target=samba[3]|win Samba version to target
196 --testlist=FILE file to read available tests from
198 Paths:
199 --prefix=DIR prefix to run tests in [st]
200 --srcdir=DIR source directory [.]
201 --bindir=DIR binaries directory [./bin]
203 Target Specific:
204 --socket-wrapper-pcap save traffic to pcap directories
205 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
206 failed
207 --socket-wrapper enable socket wrapper
209 Samba4 Specific:
210 --ldap=openldap|fedora-ds back samba onto specified ldap server
212 Behaviour:
213 --quick run quick overall test
214 --one abort when the first test fails
215 --testenv run a shell in the requested test environment
216 --list list available tests
218 exit(0);
221 my $result = GetOptions (
222 'help|h|?' => \$opt_help,
223 'target=s' => \$opt_target,
224 'prefix=s' => \$prefix,
225 'socket-wrapper' => \$opt_socket_wrapper,
226 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
227 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
228 'quick' => \$opt_quick,
229 'one' => \$opt_one,
230 'exclude=s' => \@opt_exclude,
231 'include=s' => \@opt_include,
232 'srcdir=s' => \$srcdir,
233 'bindir=s' => \$bindir,
234 'testenv' => \$opt_testenv,
235 'list' => \$opt_list,
236 'ldap:s' => \$ldap,
237 'resetup-environment' => \$opt_resetup_env,
238 'testlist=s' => \@testlists,
239 'load-list=s' => \$opt_load_list,
240 'binary-mapping=s' => \$opt_binary_mapping
243 exit(1) if (not $result);
245 ShowHelp() if ($opt_help);
247 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
249 # we want unbuffered output
250 $| = 1;
252 my @tests = @ARGV;
254 # quick hack to disable rpc validation when using valgrind - its way too slow
255 unless (defined($ENV{VALGRIND})) {
256 $ENV{VALIDATE} = "validate";
257 $ENV{MALLOC_CHECK_} = 2;
260 # make all our python scripts unbuffered
261 $ENV{PYTHONUNBUFFERED} = 1;
263 my $bindir_abs = abs_path($bindir);
265 # Backwards compatibility:
266 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
267 if (defined($ENV{FEDORA_DS_ROOT})) {
268 $ldap = "fedora-ds";
269 } else {
270 $ldap = "openldap";
274 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
275 if ($ldap) {
276 # LDAP is slow
277 $torture_maxtime *= 2;
280 $prefix =~ s+//+/+;
281 $prefix =~ s+/./+/+;
282 $prefix =~ s+/$++;
284 die("using an empty prefix isn't allowed") unless $prefix ne "";
286 # Ensure we have the test prefix around.
288 # We need restrictive
289 # permissions on this as some subdirectories in this tree will have
290 # wider permissions (ie 0777) and this would allow other users on the
291 # host to subvert the test process.
292 mkdir($prefix, 0700) unless -d $prefix;
293 chmod 0700, $prefix;
295 my $prefix_abs = abs_path($prefix);
296 my $tmpdir_abs = abs_path("$prefix/tmp");
297 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
299 my $srcdir_abs = abs_path($srcdir);
301 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
302 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
304 $ENV{PREFIX} = $prefix;
305 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
306 $ENV{PREFIX_ABS} = $prefix_abs;
307 $ENV{SRCDIR} = $srcdir;
308 $ENV{SRCDIR_ABS} = $srcdir_abs;
309 $ENV{BINDIR} = $bindir_abs;
311 my $tls_enabled = not $opt_quick;
312 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
314 sub prefix_pathvar($$)
316 my ($name, $newpath) = @_;
317 if (defined($ENV{$name})) {
318 $ENV{$name} = "$newpath:$ENV{$name}";
319 } else {
320 $ENV{$name} = $newpath;
323 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
324 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
326 if ($opt_socket_wrapper_keep_pcap) {
327 # Socket wrapper keep pcap implies socket wrapper pcap
328 $opt_socket_wrapper_pcap = 1;
331 if ($opt_socket_wrapper_pcap) {
332 # Socket wrapper pcap implies socket wrapper
333 $opt_socket_wrapper = 1;
336 my $socket_wrapper_dir;
337 if ($opt_socket_wrapper) {
338 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
339 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
340 } elsif (not $opt_list) {
341 unless ($< == 0) {
342 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
346 my $target;
347 my $testenv_default = "none";
349 my %binary_mapping = ();
350 if ($opt_binary_mapping) {
351 my @binmapping_list = split(/,/, $opt_binary_mapping);
352 foreach my $mapping (@binmapping_list) {
353 my ($bin, $map) = split(/\:/, $mapping);
354 $binary_mapping{$bin} = $map;
358 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
360 # After this many seconds, the server will self-terminate. All tests
361 # must terminate in this time, and testenv will only stay alive this
362 # long
364 my $server_maxtime = 7500;
365 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
366 $server_maxtime = $ENV{SMBD_MAXTIME};
369 unless ($opt_list) {
370 if ($opt_target eq "samba") {
371 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
372 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
374 $testenv_default = "dc";
375 require target::Samba;
376 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
377 } elsif ($opt_target eq "samba3") {
378 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
379 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
381 $testenv_default = "member";
382 require target::Samba3;
383 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
387 sub read_test_regexes($)
389 my ($name) = @_;
390 my @ret = ();
391 open(LF, "<$name") or die("unable to read $name: $!");
392 while (<LF>) {
393 chomp;
394 next if (/^#/);
395 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
396 push (@ret, [$1, $4]);
397 } else {
398 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
399 push (@ret, [$_, undef]);
402 close(LF);
403 return @ret;
406 foreach (@opt_exclude) {
407 push (@excludes, read_test_regexes($_));
410 foreach (@opt_include) {
411 push (@includes, read_test_regexes($_));
414 my $interfaces = join(',', ("127.0.0.11/8",
415 "127.0.0.12/8",
416 "127.0.0.13/8",
417 "127.0.0.14/8",
418 "127.0.0.15/8",
419 "127.0.0.16/8"));
421 my $clientdir = "$prefix_abs/client";
423 my $conffile = "$clientdir/client.conf";
424 $ENV{SMB_CONF_PATH} = $conffile;
426 sub write_clientconf($$$)
428 my ($conffile, $clientdir, $vars) = @_;
430 mkdir("$clientdir", 0777) unless -d "$clientdir";
432 if ( -d "$clientdir/private" ) {
433 unlink <$clientdir/private/*>;
434 } else {
435 mkdir("$clientdir/private", 0777);
438 if ( -d "$clientdir/lockdir" ) {
439 unlink <$clientdir/lockdir/*>;
440 } else {
441 mkdir("$clientdir/lockdir", 0777);
444 if ( -d "$clientdir/statedir" ) {
445 unlink <$clientdir/statedir/*>;
446 } else {
447 mkdir("$clientdir/statedir", 0777);
450 if ( -d "$clientdir/cachedir" ) {
451 unlink <$clientdir/cachedir/*>;
452 } else {
453 mkdir("$clientdir/cachedir", 0777);
456 # this is ugly, but the ncalrpcdir needs exactly 0755
457 # otherwise tests fail.
458 my $mask = umask;
459 umask 0022;
460 if ( -d "$clientdir/ncalrpcdir/np" ) {
461 unlink <$clientdir/ncalrpcdir/np/*>;
462 rmdir "$clientdir/ncalrpcdir/np";
464 if ( -d "$clientdir/ncalrpcdir" ) {
465 unlink <$clientdir/ncalrpcdir/*>;
466 rmdir "$clientdir/ncalrpcdir";
468 mkdir("$clientdir/ncalrpcdir", 0755);
469 umask $mask;
471 open(CF, ">$conffile");
472 print CF "[global]\n";
473 print CF "\tnetbios name = client\n";
474 if (defined($vars->{DOMAIN})) {
475 print CF "\tworkgroup = $vars->{DOMAIN}\n";
477 if (defined($vars->{REALM})) {
478 print CF "\trealm = $vars->{REALM}\n";
480 if ($opt_socket_wrapper) {
481 print CF "\tinterfaces = $interfaces\n";
483 print CF "
484 private dir = $clientdir/private
485 lock dir = $clientdir/lockdir
486 state directory = $clientdir/statedir
487 cache directory = $clientdir/cachedir
488 ncalrpc dir = $clientdir/ncalrpcdir
489 name resolve order = file bcast
490 panic action = $RealBin/gdb_backtrace \%d
491 max xmit = 32K
492 notify:inotify = false
493 ldb:nosync = true
494 system:anonymous = true
495 client lanman auth = Yes
496 log level = 1
497 torture:basedir = $clientdir
498 #We don't want to pass our self-tests if the PAC code is wrong
499 gensec:require_pac = true
500 resolv:host file = $prefix_abs/dns_host_file
501 #We don't want to run 'speed' tests for very long
502 torture:timelimit = 1
504 close(CF);
507 my @todo = ();
509 sub should_run_test($)
511 my $name = shift;
512 if ($#tests == -1) {
513 return 1;
515 for (my $i=0; $i <= $#tests; $i++) {
516 if ($name =~ /$tests[$i]/i) {
517 return 1;
520 return 0;
523 sub read_testlist($)
525 my ($filename) = @_;
527 my @ret = ();
528 open(IN, $filename) or die("Unable to open $filename: $!");
530 while (<IN>) {
531 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
532 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
533 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
534 my $name = <IN>;
535 $name =~ s/\n//g;
536 my $env = <IN>;
537 $env =~ s/\n//g;
538 my $cmdline = <IN>;
539 $cmdline =~ s/\n//g;
540 if (should_run_test($name) == 1) {
541 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
543 } else {
544 print;
547 close(IN) or die("Error creating recipe");
548 return @ret;
551 if ($#testlists == -1) {
552 die("No testlists specified");
555 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
556 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
557 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
558 if ($opt_socket_wrapper) {
559 $ENV{SELFTEST_INTERFACES} = $interfaces;
560 } else {
561 $ENV{SELFTEST_INTERFACES} = "";
563 if ($opt_quick) {
564 $ENV{SELFTEST_QUICK} = "1";
565 } else {
566 $ENV{SELFTEST_QUICK} = "";
568 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
570 my @available = ();
571 foreach my $fn (@testlists) {
572 foreach (read_testlist($fn)) {
573 my $name = $$_[0];
574 next if (@includes and not defined(find_in_list(\@includes, $name)));
575 push (@available, $_);
579 my $restricted = undef;
580 my $restricted_used = {};
582 if ($opt_load_list) {
583 $restricted = [];
584 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
585 while (<LOAD_LIST>) {
586 chomp;
587 push (@$restricted, $_);
589 close(LOAD_LIST);
592 my $individual_tests = undef;
593 $individual_tests = {};
595 foreach my $testsuite (@available) {
596 my $name = $$testsuite[0];
597 my $skipreason = skip($name);
598 if (defined($restricted)) {
599 # Find the testsuite for this test
600 my $match = undef;
601 foreach my $r (@$restricted) {
602 if ($r eq $name) {
603 $individual_tests->{$name} = [];
604 $match = $r;
605 $restricted_used->{$r} = 1;
606 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
607 push(@{$individual_tests->{$name}}, $r);
608 $match = $r;
609 $restricted_used->{$r} = 1;
612 if ($match) {
613 if (defined($skipreason)) {
614 if (not $opt_list) {
615 Subunit::skip_testsuite($name, $skipreason);
617 } else {
618 push(@todo, $testsuite);
621 } elsif (defined($skipreason)) {
622 if (not $opt_list) {
623 Subunit::skip_testsuite($name, $skipreason);
625 } else {
626 push(@todo, $testsuite);
630 if (defined($restricted)) {
631 foreach (@$restricted) {
632 unless (defined($restricted_used->{$_})) {
633 print "No test or testsuite found matching $_\n";
636 } elsif ($#todo == -1) {
637 print STDERR "No tests to run\n";
638 exit(1);
641 my $suitestotal = $#todo + 1;
643 unless ($opt_list) {
644 Subunit::progress($suitestotal);
645 Subunit::report_time(time());
648 my $i = 0;
649 $| = 1;
651 my %running_envs = ();
653 sub get_running_env($)
655 my ($name) = @_;
657 my $envname = $name;
659 $envname =~ s/:.*//;
661 return $running_envs{$envname};
664 my @exported_envvars = (
665 # domain stuff
666 "DOMAIN",
667 "REALM",
669 # domain controller stuff
670 "DC_SERVER",
671 "DC_SERVER_IP",
672 "DC_NETBIOSNAME",
673 "DC_NETBIOSALIAS",
675 # domain member
676 "MEMBER_SERVER",
677 "MEMBER_SERVER_IP",
678 "MEMBER_NETBIOSNAME",
679 "MEMBER_NETBIOSALIAS",
681 # rpc proxy controller stuff
682 "RPC_PROXY_SERVER",
683 "RPC_PROXY_SERVER_IP",
684 "RPC_PROXY_NETBIOSNAME",
685 "RPC_PROXY_NETBIOSALIAS",
687 # domain controller stuff for Vampired DC
688 "VAMPIRE_DC_SERVER",
689 "VAMPIRE_DC_SERVER_IP",
690 "VAMPIRE_DC_NETBIOSNAME",
691 "VAMPIRE_DC_NETBIOSALIAS",
693 # server stuff
694 "SERVER",
695 "SERVER_IP",
696 "NETBIOSNAME",
697 "NETBIOSALIAS",
699 # user stuff
700 "USERNAME",
701 "USERID",
702 "PASSWORD",
703 "DC_USERNAME",
704 "DC_PASSWORD",
706 # misc stuff
707 "KRB5_CONFIG",
708 "WINBINDD_SOCKET_DIR",
709 "WINBINDD_PRIV_PIPE_DIR",
710 "NMBD_SOCKET_DIR",
711 "LOCAL_PATH"
714 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
715 my $signame = shift;
716 teardown_env($_) foreach(keys %running_envs);
717 die("Received signal $signame");
720 sub setup_env($$)
722 my ($name, $prefix) = @_;
724 my $testenv_vars = undef;
726 my $envname = $name;
727 my $option = $name;
729 $envname =~ s/:.*//;
730 $option =~ s/^[^:]*//;
731 $option =~ s/^://;
733 $option = "client" if $option eq "";
735 if ($envname eq "none") {
736 $testenv_vars = {};
737 } elsif (defined(get_running_env($envname))) {
738 $testenv_vars = get_running_env($envname);
739 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
740 print $testenv_vars->{target}->getlog_env($testenv_vars);
741 $testenv_vars = undef;
743 } else {
744 $testenv_vars = $target->setup_env($envname, $prefix);
745 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
746 return $testenv_vars;
747 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
748 $testenv_vars->{target} = $target;
750 if (not defined($testenv_vars)) {
751 warn("$opt_target can't start up known environment '$envname'");
756 return undef unless defined($testenv_vars);
758 $running_envs{$envname} = $testenv_vars;
760 if ($option eq "local") {
761 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
762 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
763 } elsif ($option eq "client") {
764 SocketWrapper::set_default_iface(11);
765 write_clientconf($conffile, $clientdir, $testenv_vars);
766 $ENV{SMB_CONF_PATH} = $conffile;
767 } else {
768 die("Unknown option[$option] for envname[$envname]");
771 foreach (@exported_envvars) {
772 if (defined($testenv_vars->{$_})) {
773 $ENV{$_} = $testenv_vars->{$_};
774 } else {
775 delete $ENV{$_};
779 return $testenv_vars;
782 sub exported_envvars_str($)
784 my ($testenv_vars) = @_;
785 my $out = "";
787 foreach (@exported_envvars) {
788 next unless defined($testenv_vars->{$_});
789 $out .= $_."=".$testenv_vars->{$_}."\n";
792 return $out;
795 sub getlog_env($)
797 my ($envname) = @_;
798 return "" if ($envname eq "none");
799 my $env = get_running_env($envname);
800 return $env->{target}->getlog_env($env);
803 sub check_env($)
805 my ($envname) = @_;
806 return 1 if ($envname eq "none");
807 my $env = get_running_env($envname);
808 return $env->{target}->check_env($env);
811 sub teardown_env($)
813 my ($envname) = @_;
814 return if ($envname eq "none");
815 my $env = get_running_env($envname);
816 $env->{target}->teardown_env($env);
817 delete $running_envs{$envname};
820 # This 'global' file needs to be empty when we start
821 unlink("$prefix_abs/dns_host_file");
823 if ($opt_testenv) {
824 my $testenv_name = $ENV{SELFTEST_TESTENV};
825 $testenv_name = $testenv_default unless defined($testenv_name);
827 my $testenv_vars = setup_env($testenv_name, $prefix);
829 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
831 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
832 $ENV{ENVNAME} = $testenv_name;
834 my $envvarstr = exported_envvars_str($testenv_vars);
836 my $term = ($ENV{TERMINAL} or "xterm -e");
837 system("$term 'echo -e \"
838 Welcome to the Samba4 Test environment '$testenv_name'
840 This matches the client environment used in make test
841 server is pid `cat \$PIDDIR/samba.pid`
843 Some useful environment variables:
844 TORTURE_OPTIONS=\$TORTURE_OPTIONS
845 SMB_CONF_PATH=\$SMB_CONF_PATH
847 $envvarstr
848 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
849 teardown_env($testenv_name);
850 } elsif ($opt_list) {
851 foreach (@todo) {
852 my $cmd = $$_[2];
853 my $name = $$_[0];
854 my $envname = $$_[1];
856 unless($cmd =~ /\$LISTOPT/) {
857 warn("Unable to list tests in $name");
858 next;
861 $cmd =~ s/\$LISTOPT/--list/g;
863 system($cmd);
865 if ($? == -1) {
866 die("Unable to run $cmd: $!");
867 } elsif ($? & 127) {
868 die(snprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
871 my $exitcode = $? >> 8;
872 if ($exitcode != 0) {
873 die("$cmd exited with exit code $exitcode");
876 } else {
877 foreach (@todo) {
878 $i++;
879 my $cmd = $$_[2];
880 my $name = $$_[0];
881 my $envname = $$_[1];
883 my $envvars = setup_env($envname, $prefix);
884 if (not defined($envvars)) {
885 Subunit::start_testsuite($name);
886 Subunit::end_testsuite($name, "error",
887 "unable to set up environment $envname - exiting");
888 next;
889 } elsif ($envvars eq "UNKNOWN") {
890 Subunit::start_testsuite($name);
891 Subunit::end_testsuite($name, "skip",
892 "environment $envname is unknown in this test backend - skipping");
893 next;
896 # Generate a file with the individual tests to run, if the
897 # test runner for this test suite supports it.
898 if ($individual_tests and $individual_tests->{$name}) {
899 if ($$_[3]) {
900 my ($fh, $listid_file) = tempfile(UNLINK => 0);
901 foreach my $test (@{$individual_tests->{$name}}) {
902 print $fh substr($test, length($name)+1) . "\n";
904 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
905 } elsif ($$_[4]) {
906 $cmd =~ s/\s+[^\s]+\s*$//;
907 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
911 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
913 teardown_env($envname) if ($opt_resetup_env);
917 print "\n";
919 teardown_env($_) foreach (keys %running_envs);
921 my $failed = 0;
923 # if there were any valgrind failures, show them
924 foreach (<$prefix/valgrind.log*>) {
925 next unless (-s $_);
926 print "VALGRIND FAILURE\n";
927 $failed++;
928 system("cat $_");
930 exit 0;