s3-shadow-copy2: Fix dir/@GMT-2012.10.15-13.48.43 form of paths
[Samba.git] / selftest / selftest.pl
blob831ebd90f8d6134efb739a5c1ab76f7358ff2195
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
548 winbind separator = /
550 close(CF);
553 my @todo = ();
555 sub should_run_test($)
557 my $name = shift;
558 if ($#tests == -1) {
559 return 1;
561 for (my $i=0; $i <= $#tests; $i++) {
562 if ($name =~ /$tests[$i]/i) {
563 return 1;
566 return 0;
569 sub read_testlist($)
571 my ($filename) = @_;
573 my @ret = ();
574 open(IN, $filename) or die("Unable to open $filename: $!");
576 while (<IN>) {
577 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
578 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
579 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
580 my $name = <IN>;
581 $name =~ s/\n//g;
582 my $env = <IN>;
583 $env =~ s/\n//g;
584 my $cmdline = <IN>;
585 $cmdline =~ s/\n//g;
586 if (should_run_test($name) == 1) {
587 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
589 } else {
590 print;
593 close(IN) or die("Error creating recipe");
594 return @ret;
597 if ($#testlists == -1) {
598 die("No testlists specified");
601 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
602 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
603 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
604 if ($opt_socket_wrapper) {
605 $ENV{SELFTEST_INTERFACES} = $interfaces;
606 } else {
607 $ENV{SELFTEST_INTERFACES} = "";
609 if ($opt_quick) {
610 $ENV{SELFTEST_QUICK} = "1";
611 } else {
612 $ENV{SELFTEST_QUICK} = "";
614 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
616 my @available = ();
617 foreach my $fn (@testlists) {
618 foreach (read_testlist($fn)) {
619 my $name = $$_[0];
620 next if (@includes and not defined(find_in_list(\@includes, $name)));
621 push (@available, $_);
625 my $restricted = undef;
626 my $restricted_used = {};
628 if ($opt_load_list) {
629 $restricted = [];
630 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
631 while (<LOAD_LIST>) {
632 chomp;
633 push (@$restricted, $_);
635 close(LOAD_LIST);
638 my $individual_tests = undef;
639 $individual_tests = {};
641 foreach my $testsuite (@available) {
642 my $name = $$testsuite[0];
643 my $skipreason = skip($name);
644 if (defined($restricted)) {
645 # Find the testsuite for this test
646 my $match = undef;
647 foreach my $r (@$restricted) {
648 if ($r eq $name) {
649 $individual_tests->{$name} = [];
650 $match = $r;
651 $restricted_used->{$r} = 1;
652 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
653 push(@{$individual_tests->{$name}}, $r);
654 $match = $r;
655 $restricted_used->{$r} = 1;
658 if ($match) {
659 if (defined($skipreason)) {
660 if (not $opt_list) {
661 Subunit::skip_testsuite($name, $skipreason);
663 } else {
664 push(@todo, $testsuite);
667 } elsif (defined($skipreason)) {
668 if (not $opt_list) {
669 Subunit::skip_testsuite($name, $skipreason);
671 } else {
672 push(@todo, $testsuite);
676 if (defined($restricted)) {
677 foreach (@$restricted) {
678 unless (defined($restricted_used->{$_})) {
679 print "No test or testsuite found matching $_\n";
682 } elsif ($#todo == -1) {
683 print STDERR "No tests to run\n";
684 exit(1);
687 my $suitestotal = $#todo + 1;
689 unless ($opt_list) {
690 Subunit::progress($suitestotal);
691 Subunit::report_time(time());
694 my $i = 0;
695 $| = 1;
697 my %running_envs = ();
699 sub get_running_env($)
701 my ($name) = @_;
703 my $envname = $name;
705 $envname =~ s/:.*//;
707 return $running_envs{$envname};
710 my @exported_envvars = (
711 # domain stuff
712 "DOMAIN",
713 "REALM",
715 # domain controller stuff
716 "DC_SERVER",
717 "DC_SERVER_IP",
718 "DC_NETBIOSNAME",
719 "DC_NETBIOSALIAS",
721 # domain member
722 "MEMBER_SERVER",
723 "MEMBER_SERVER_IP",
724 "MEMBER_NETBIOSNAME",
725 "MEMBER_NETBIOSALIAS",
727 # rpc proxy controller stuff
728 "RPC_PROXY_SERVER",
729 "RPC_PROXY_SERVER_IP",
730 "RPC_PROXY_NETBIOSNAME",
731 "RPC_PROXY_NETBIOSALIAS",
733 # domain controller stuff for Vampired DC
734 "VAMPIRE_DC_SERVER",
735 "VAMPIRE_DC_SERVER_IP",
736 "VAMPIRE_DC_NETBIOSNAME",
737 "VAMPIRE_DC_NETBIOSALIAS",
739 "PROMOTED_DC_SERVER",
740 "PROMOTED_DC_SERVER_IP",
741 "PROMOTED_DC_NETBIOSNAME",
742 "PROMOTED_DC_NETBIOSALIAS",
744 # server stuff
745 "SERVER",
746 "SERVER_IP",
747 "NETBIOSNAME",
748 "NETBIOSALIAS",
750 # user stuff
751 "USERNAME",
752 "USERID",
753 "PASSWORD",
754 "DC_USERNAME",
755 "DC_PASSWORD",
757 # misc stuff
758 "KRB5_CONFIG",
759 "SELFTEST_WINBINDD_SOCKET_DIR",
760 "WINBINDD_PRIV_PIPE_DIR",
761 "NMBD_SOCKET_DIR",
762 "LOCAL_PATH",
764 # nss_wrapper
765 "NSS_WRAPPER_PASSWD",
766 "NSS_WRAPPER_GROUP",
768 # UID/GID for rfc2307 mapping tests
769 "UID_RFC2307TEST",
770 "GID_RFC2307TEST"
773 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
774 my $signame = shift;
775 teardown_env($_) foreach(keys %running_envs);
776 die("Received signal $signame");
779 sub setup_env($$)
781 my ($name, $prefix) = @_;
783 my $testenv_vars = undef;
785 my $envname = $name;
786 my $option = $name;
788 $envname =~ s/:.*//;
789 $option =~ s/^[^:]*//;
790 $option =~ s/^://;
792 $option = "client" if $option eq "";
794 if ($envname eq "none") {
795 $testenv_vars = {};
796 } elsif (defined(get_running_env($envname))) {
797 $testenv_vars = get_running_env($envname);
798 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
799 print $testenv_vars->{target}->getlog_env($testenv_vars);
800 $testenv_vars = undef;
802 } else {
803 $testenv_vars = $target->setup_env($envname, $prefix);
804 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
805 return $testenv_vars;
806 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
807 $testenv_vars->{target} = $target;
809 if (not defined($testenv_vars)) {
810 warn("$opt_target can't start up known environment '$envname'");
815 return undef unless defined($testenv_vars);
817 $running_envs{$envname} = $testenv_vars;
819 if ($option eq "local") {
820 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
821 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
822 } elsif ($option eq "client") {
823 SocketWrapper::set_default_iface(11);
824 write_clientconf($conffile, $clientdir, $testenv_vars);
825 $ENV{SMB_CONF_PATH} = $conffile;
826 } else {
827 die("Unknown option[$option] for envname[$envname]");
830 foreach (@exported_envvars) {
831 if (defined($testenv_vars->{$_})) {
832 $ENV{$_} = $testenv_vars->{$_};
833 } else {
834 delete $ENV{$_};
838 return $testenv_vars;
841 sub exported_envvars_str($)
843 my ($testenv_vars) = @_;
844 my $out = "";
846 foreach (@exported_envvars) {
847 next unless defined($testenv_vars->{$_});
848 $out .= $_."=".$testenv_vars->{$_}."\n";
851 return $out;
854 sub getlog_env($)
856 my ($envname) = @_;
857 return "" if ($envname eq "none");
858 my $env = get_running_env($envname);
859 return $env->{target}->getlog_env($env);
862 sub check_env($)
864 my ($envname) = @_;
865 return 1 if ($envname eq "none");
866 my $env = get_running_env($envname);
867 return $env->{target}->check_env($env);
870 sub teardown_env($)
872 my ($envname) = @_;
873 return if ($envname eq "none");
874 my $env = get_running_env($envname);
875 $env->{target}->teardown_env($env);
876 delete $running_envs{$envname};
879 # This 'global' file needs to be empty when we start
880 unlink("$prefix_abs/dns_host_file");
881 unlink("$prefix_abs/hosts");
883 if ($opt_random_order) {
884 require List::Util;
885 my @newtodo = List::Util::shuffle(@todo);
886 @todo = @newtodo;
889 if ($opt_testenv) {
890 my $testenv_name = $ENV{SELFTEST_TESTENV};
891 $testenv_name = $testenv_default unless defined($testenv_name);
893 my $testenv_vars = setup_env($testenv_name, $prefix);
895 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
896 die("Unable to setup environment $testenv_name");
899 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
900 $ENV{ENVNAME} = $testenv_name;
902 my $envvarstr = exported_envvars_str($testenv_vars);
904 my @term_args = ("echo -e \"
905 Welcome to the Samba4 Test environment '$testenv_name'
907 This matches the client environment used in make test
908 server is pid `cat \$PIDDIR/samba.pid`
910 Some useful environment variables:
911 TORTURE_OPTIONS=\$TORTURE_OPTIONS
912 SMB_CONF_PATH=\$SMB_CONF_PATH
914 $envvarstr
915 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
916 my @term = ();
917 if ($ENV{TERMINAL}) {
918 @term = ($ENV{TERMINAL});
919 } else {
920 @term = ("xterm", "-e");
921 unshift(@term_args, ("bash", "-c"));
924 system(@term, @term_args);
926 teardown_env($testenv_name);
927 } elsif ($opt_list) {
928 foreach (@todo) {
929 my $cmd = $$_[2];
930 my $name = $$_[0];
931 my $envname = $$_[1];
933 unless($cmd =~ /\$LISTOPT/) {
934 warn("Unable to list tests in $name");
935 next;
938 $cmd =~ s/\$LISTOPT/--list/g;
940 system($cmd);
942 if ($? == -1) {
943 die("Unable to run $cmd: $!");
944 } elsif ($? & 127) {
945 die(sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
948 my $exitcode = $? >> 8;
949 if ($exitcode != 0) {
950 die("$cmd exited with exit code $exitcode");
953 } else {
954 foreach (@todo) {
955 $i++;
956 my $cmd = $$_[2];
957 my $name = $$_[0];
958 my $envname = $$_[1];
960 my $envvars = setup_env($envname, $prefix);
961 if (not defined($envvars)) {
962 Subunit::start_testsuite($name);
963 Subunit::end_testsuite($name, "error",
964 "unable to set up environment $envname - exiting");
965 next;
966 } elsif ($envvars eq "UNKNOWN") {
967 Subunit::start_testsuite($name);
968 Subunit::end_testsuite($name, "skip",
969 "environment $envname is unknown in this test backend - skipping");
970 next;
973 # Generate a file with the individual tests to run, if the
974 # test runner for this test suite supports it.
975 if ($individual_tests and $individual_tests->{$name}) {
976 if ($$_[3]) {
977 my ($fh, $listid_file) = tempfile(UNLINK => 0);
978 foreach my $test (@{$individual_tests->{$name}}) {
979 print $fh substr($test, length($name)+1) . "\n";
981 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
982 } elsif ($$_[4]) {
983 $cmd =~ s/\s+[^\s]+\s*$//;
984 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
988 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
990 teardown_env($envname) if ($opt_resetup_env);
994 print "\n";
996 teardown_env($_) foreach (keys %running_envs);
998 my $failed = 0;
1000 # if there were any valgrind failures, show them
1001 foreach (<$prefix/valgrind.log*>) {
1002 next unless (-s $_);
1003 print "VALGRIND FAILURE\n";
1004 $failed++;
1005 system("cat $_");
1007 exit 0;