s3: smbd: Duplicate smb_file_link_information() hardlink handling as smb2_file_link_i...
[Samba.git] / selftest / selftest.pl
blob230c49124b41fbc98f77dcc63fd4fcefca152dd3
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;
20 use warnings;
22 use FindBin qw($RealBin $Script);
23 use File::Spec;
24 use File::Temp qw(tempfile);
25 use File::Path qw(remove_tree);
26 use Getopt::Long;
27 use POSIX;
28 use Cwd qw(abs_path);
29 use lib "$RealBin";
30 use Subunit;
31 use SocketWrapper;
32 use target::Samba;
33 use Time::HiRes qw(time);
35 eval {
36 require Time::HiRes;
37 Time::HiRes->import("time");
39 if ($@) {
40 print "You don't have Time::Hires installed !\n";
43 my $opt_help = 0;
44 my $opt_target = "samba";
45 my $opt_quick = 0;
46 my $opt_socket_wrapper = 0;
47 my $opt_socket_wrapper_pcap = undef;
48 my $opt_socket_wrapper_keep_pcap = undef;
49 my $opt_random_order = 0;
50 my $opt_one = 0;
51 my @opt_exclude = ();
52 my @opt_include = ();
53 my @opt_exclude_env = ();
54 my @opt_include_env = ();
55 my $opt_testenv = 0;
56 my $opt_list = 0;
57 my $opt_mitkrb5 = 0;
58 my $opt_default_ldb_backend = "mdb";
59 my $opt_resetup_env = undef;
60 my $opt_load_list = undef;
61 my $opt_libnss_wrapper_so_path = "";
62 my $opt_libresolv_wrapper_so_path = "";
63 my $opt_libsocket_wrapper_so_path = "";
64 my $opt_libuid_wrapper_so_path = "";
65 my $opt_libasan_so_path = "";
66 my $opt_libcrypt_so_path = "";
67 my $opt_use_dns_faking = 0;
68 my @testlists = ();
70 my $srcdir = ".";
71 my $bindir = "./bin";
72 my $prefix = "./st";
74 my @includes = ();
75 my @excludes = ();
77 sub find_in_list($$)
79 my ($list, $fullname) = @_;
81 foreach (@$list) {
82 if ($fullname =~ /$$_[0]/) {
83 return ($$_[1]) if ($$_[1]);
84 return "";
88 return undef;
91 sub skip
93 my ($name, $envname) = @_;
94 my ($env_basename, $env_localpart) = split(/:/, $envname);
96 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
97 return "environment $envname is disabled as this build does not include an AD DC";
100 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
101 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
102 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
103 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
106 return find_in_list(\@excludes, $name);
109 sub getlog_env($);
111 # expand strings from %ENV
112 sub expand_environment_strings($)
114 my $s = shift;
115 # we use a reverse sort so we do the longer ones first
116 foreach my $k (sort { $b cmp $a } keys %ENV) {
117 $s =~ s/\$$k/$ENV{$k}/g;
119 return $s;
122 my $target;
124 sub run_testsuite($$$$$)
126 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
127 my $pcap_file = $target->setup_pcap($name);
129 Subunit::start_testsuite($name);
130 Subunit::progress_push();
131 Subunit::report_time();
132 system($cmd);
133 Subunit::report_time();
134 Subunit::progress_pop();
136 if ($? == -1) {
137 print "command: $cmd\n";
138 printf "expanded command: %s\n", expand_environment_strings($cmd);
139 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
140 exit(1);
141 } elsif ($? & 127) {
142 print "command: $cmd\n";
143 printf "expanded command: %s\n", expand_environment_strings($cmd);
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 $target->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 [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
190 --exclude=FILE Exclude tests listed in the file
191 --include=FILE Include tests listed in the file
192 --exclude-env=ENV Exclude tests for the specified environment
193 --include-env=ENV Include tests for the specified environment
195 Paths:
196 --prefix=DIR prefix to run tests in [st]
197 --srcdir=DIR source directory [.]
198 --bindir=DIR binaries directory [./bin]
200 Preload cwrap:
201 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
202 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
203 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
204 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
205 --asan_so_path=FILE the asan library to preload
207 DNS:
208 --use-dns-faking Fake DNS entries rather than talking to our
209 DNS implementation.
211 Target Specific:
212 --socket-wrapper-pcap save traffic to pcap directories
213 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
214 failed
215 --socket-wrapper enable socket wrapper
217 Behaviour:
218 --quick run quick overall test
219 --one abort when the first test fails
220 --testenv run a shell in the requested test environment
221 --list list available tests
223 exit(0);
226 my $result = GetOptions (
227 'help|h|?' => \$opt_help,
228 'target=s' => \$opt_target,
229 'prefix=s' => \$prefix,
230 'socket-wrapper' => \$opt_socket_wrapper,
231 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
232 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
233 'quick' => \$opt_quick,
234 'one' => \$opt_one,
235 'exclude=s' => \@opt_exclude,
236 'include=s' => \@opt_include,
237 'exclude-env=s' => \@opt_exclude_env,
238 'include-env=s' => \@opt_include_env,
239 'srcdir=s' => \$srcdir,
240 'bindir=s' => \$bindir,
241 'testenv' => \$opt_testenv,
242 'list' => \$opt_list,
243 'mitkrb5' => \$opt_mitkrb5,
244 'default-ldb-backend=s' => \$opt_default_ldb_backend,
245 'resetup-environment' => \$opt_resetup_env,
246 'testlist=s' => \@testlists,
247 'random-order' => \$opt_random_order,
248 'load-list=s' => \$opt_load_list,
249 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
250 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
251 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
252 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
253 'asan_so_path=s' => \$opt_libasan_so_path,
254 'crypt_so_path=s' => \$opt_libcrypt_so_path,
255 'use-dns-faking' => \$opt_use_dns_faking
258 exit(1) if (not $result);
260 ShowHelp() if ($opt_help);
262 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
264 # we want unbuffered output
265 $| = 1;
267 my @tests = @ARGV;
269 # quick hack to disable rpc validation when using valgrind - its way too slow
270 unless (defined($ENV{VALGRIND})) {
271 $ENV{VALIDATE} = "validate";
272 $ENV{MALLOC_CHECK_} = 3;
275 # make all our python scripts unbuffered
276 $ENV{PYTHONUNBUFFERED} = 1;
278 $ENV{SAMBA_DEPRECATED_SUPPRESS} = 1;
280 # do not depend on the users setup
281 # see also bootstrap/config.py
282 $ENV{TZ} = "UTC";
283 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
285 my $bindir_abs = abs_path($bindir);
287 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
289 $prefix =~ s+//+/+;
290 $prefix =~ s+/\./+/+;
291 $prefix =~ s+/$++;
293 die("using an empty prefix isn't allowed") unless $prefix ne "";
295 # Ensure we have the test prefix around.
297 # We need restrictive
298 # permissions on this as some subdirectories in this tree will have
299 # wider permissions (ie 0777) and this would allow other users on the
300 # host to subvert the test process.
301 umask 0077;
302 mkdir($prefix, 0700) unless -d $prefix;
303 chmod 0700, $prefix;
304 # We need to have no umask limitations for the tests.
305 umask 0000;
307 my $prefix_abs = abs_path($prefix);
308 my $tmpdir_abs = abs_path("$prefix/tmp");
309 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
311 my $srcdir_abs = abs_path($srcdir);
313 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
314 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
316 $ENV{SAMBA_SELFTEST} = "1";
318 $ENV{PREFIX} = $prefix;
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_libasan_so_path) {
352 if ($ld_preload) {
353 if ($opt_libcrypt_so_path) {
354 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path:$ld_preload";
355 } else {
356 $ld_preload = "$opt_libasan_so_path:$ld_preload";
358 } else {
359 if ($opt_libcrypt_so_path) {
360 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path";
361 } else {
362 $ld_preload = "$opt_libasan_so_path";
367 if ($opt_libnss_wrapper_so_path) {
368 if ($ld_preload) {
369 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
370 } else {
371 $ld_preload = "$opt_libnss_wrapper_so_path";
375 if ($opt_libresolv_wrapper_so_path) {
376 if ($ld_preload) {
377 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
378 } else {
379 $ld_preload = "$opt_libresolv_wrapper_so_path";
383 if ($opt_libsocket_wrapper_so_path) {
384 if ($ld_preload) {
385 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
386 } else {
387 $ld_preload = "$opt_libsocket_wrapper_so_path";
391 if ($opt_libuid_wrapper_so_path) {
392 if ($ld_preload) {
393 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
394 } else {
395 $ld_preload = "$opt_libuid_wrapper_so_path";
399 if (defined($ENV{USE_NAMESPACES})) {
400 print "Using linux containerization for selftest testenv(s)...\n";
402 # Create a common bridge to connect up the testenv namespaces. We give
403 # it the client's IP address, as this is where the tests will run from
404 my $ipv4_addr = Samba::get_ipv4_addr("client");
405 my $ipv6_addr = Samba::get_ipv6_addr("client");
406 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
409 $ENV{LD_PRELOAD} = $ld_preload;
410 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
412 # Enable uid_wrapper globally
413 $ENV{UID_WRAPPER} = 1;
415 # We are already hitting the limit, so double it.
416 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
418 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
420 # This is needed in order to allow the ldb_*ldap module
421 # to work with a preloaded socket wrapper.
422 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
424 my $socket_wrapper_dir;
425 if ($opt_socket_wrapper) {
426 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
427 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
428 } elsif (not $opt_list) {
429 unless ($< == 0) {
430 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
434 if ($opt_use_dns_faking) {
435 print "DNS: Faking nameserver\n";
436 $ENV{SAMBA_DNS_FAKING} = 1;
439 my $testenv_default = "none";
441 if ($opt_mitkrb5 == 1) {
442 $ENV{MITKRB5} = $opt_mitkrb5;
443 $ENV{KRB5RCACHETYPE} = "none";
446 # After this many seconds, the server will self-terminate. All tests
447 # must terminate in this time, and testenv will only stay alive this
448 # long
450 my $server_maxtime;
451 if ($opt_testenv) {
452 # 1 year should be enough :-)
453 $server_maxtime = 365 * 24 * 60 * 60;
454 } else {
455 # make test should run under 5 hours
456 $server_maxtime = 5 * 60 * 60;
459 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
460 $server_maxtime = $ENV{SMBD_MAXTIME};
463 $target = new Samba($bindir, $srcdir, $server_maxtime,
464 $opt_socket_wrapper_pcap,
465 $opt_socket_wrapper_keep_pcap,
466 $opt_default_ldb_backend);
467 unless ($opt_list) {
468 if ($opt_target eq "samba") {
469 $testenv_default = "ad_dc";
470 } elsif ($opt_target eq "samba3") {
471 $testenv_default = "nt4_member";
475 sub read_test_regexes($)
477 my ($name) = @_;
478 my @ret = ();
479 open(LF, "<$name") or die("unable to read $name: $!");
480 while (<LF>) {
481 chomp;
482 next if (/^#/);
483 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
484 push (@ret, [$1, $4]);
485 } else {
486 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
487 push (@ret, [$_, undef]);
490 close(LF);
491 return @ret;
494 foreach (@opt_exclude) {
495 push (@excludes, read_test_regexes($_));
498 foreach (@opt_include) {
499 push (@includes, read_test_regexes($_));
502 # We give the selftest client 6 different IPv4 addresses to use. Most tests
503 # only use the first (.11) IP. Note that winsreplication.c is one test that
504 # uses the other IPs (search for iface_list_count()).
505 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
506 my $interfaces = Samba::get_interfaces_config("client", 6);
508 my $clientdir = "$prefix_abs/client";
510 my $conffile = "$clientdir/client.conf";
511 $ENV{SMB_CONF_PATH} = $conffile;
513 sub write_clientconf($$$)
515 my ($conffile, $clientdir, $vars) = @_;
517 mkdir("$clientdir", 0777) unless -d "$clientdir";
519 my @subdirs = (
520 { name => "private", mask => 0777 },
521 { name => "bind-dns", mask => 0777 },
522 { name => "lockdir", mask => 0777 },
523 { name => "statedir", mask => 0777 },
524 { name => "cachedir", mask => 0777 },
525 { name => "pkinit", mask => 0700 },
526 { name => "pid", mask => 0777 },
527 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
528 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
531 foreach my $sub (@subdirs) {
532 my $dir = "$clientdir/$sub->{name}";
533 remove_tree($dir);
534 my $mask = umask;
535 if (defined($sub->{umask})) {
536 umask $sub->{umask};
538 mkdir($dir, $sub->{mask});
539 umask $mask;
542 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
543 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
544 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
545 my $ca_users_dir = "$cadir/Users";
546 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
548 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
549 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
550 # We make a copy here and make the certificated easily
551 # accessable in the client environment.
552 my $mask = umask;
553 umask 0077;
554 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
555 for my $d (readdir USERS) {
556 my $user_dir = "${ca_users_dir}/${d}";
557 next if ${d} =~ /^\./;
558 next if (! -d "${user_dir}");
559 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
560 for my $l (readdir USER) {
561 my $user_link = "${user_dir}/${l}";
562 next if ${l} =~ /^\./;
563 next if (! -l "${user_link}");
565 my $dest = "${clientdir}/pkinit/${l}";
566 Samba::copy_file_content(${user_link}, ${dest});
568 closedir USER;
570 closedir USERS;
571 umask $mask;
573 open(CF, ">$conffile");
574 print CF "[global]\n";
575 print CF "\tnetbios name = client\n";
576 if (defined($vars->{DOMAIN})) {
577 print CF "\tworkgroup = $vars->{DOMAIN}\n";
579 if (defined($vars->{REALM})) {
580 print CF "\trealm = $vars->{REALM}\n";
582 if ($opt_socket_wrapper) {
583 print CF "\tinterfaces = $interfaces\n";
585 print CF "
586 private dir = $clientdir/private
587 binddns dir = $clientdir/bind-dns
588 lock dir = $clientdir/lockdir
589 state directory = $clientdir/statedir
590 cache directory = $clientdir/cachedir
591 ncalrpc dir = $clientdir/ncalrpcdir
592 pid directory = $clientdir/pid
593 panic action = $RealBin/gdb_backtrace \%d
594 max xmit = 32K
595 notify:inotify = false
596 ldb:nosync = true
597 system:anonymous = true
598 client lanman auth = Yes
599 client min protocol = CORE
600 log level = $client_loglevel
601 torture:basedir = $clientdir
602 #We don't want to run 'speed' tests for very long
603 torture:timelimit = 1
604 winbind separator = /
605 tls cafile = ${cacert}
606 tls crlfile = ${cacrl_pem}
607 tls verify peer = no_check
608 include system krb5 conf = no
609 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
611 close(CF);
614 my @todo = ();
616 sub should_run_test($)
618 my $name = shift;
619 if ($#tests == -1) {
620 return 1;
622 for (my $i=0; $i <= $#tests; $i++) {
623 if ($name =~ /$tests[$i]/i) {
624 return 1;
627 return 0;
630 sub read_testlist($)
632 my ($filename) = @_;
634 my @ret = ();
635 open(IN, $filename) or die("Unable to open $filename: $!");
637 while (<IN>) {
638 if (/-- TEST(-LOADLIST|) --\n/) {
639 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
640 my $name = <IN>;
641 $name =~ s/\n//g;
642 my $env = <IN>;
643 $env =~ s/\n//g;
644 my $loadlist;
645 if ($supports_loadlist) {
646 $loadlist = <IN>;
647 $loadlist =~ s/\n//g;
649 my $cmdline = <IN>;
650 $cmdline =~ s/\n//g;
651 if (should_run_test($name) == 1) {
652 push (@ret, [$name, $env, $cmdline, $loadlist]);
654 } else {
655 print;
658 close(IN) or die("Error creating recipe from $filename");
659 return @ret;
662 if ($#testlists == -1) {
663 die("No testlists specified");
666 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
667 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
668 $ENV{TMPDIR} = "$tmpdir_abs";
669 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
670 if ($opt_quick) {
671 $ENV{SELFTEST_QUICK} = "1";
672 } else {
673 $ENV{SELFTEST_QUICK} = "";
675 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
677 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
678 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
680 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
681 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
683 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
684 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
686 my @available = ();
687 foreach my $fn (@testlists) {
688 foreach (read_testlist($fn)) {
689 my $name = $$_[0];
690 next if (@includes and not defined(find_in_list(\@includes, $name)));
691 push (@available, $_);
695 my $restricted = undef;
696 my $restricted_used = {};
698 if ($opt_load_list) {
699 $restricted = [];
700 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
701 while (<LOAD_LIST>) {
702 chomp;
703 push (@$restricted, $_);
705 close(LOAD_LIST);
708 my $individual_tests = undef;
709 $individual_tests = {};
711 foreach my $testsuite (@available) {
712 my $name = $$testsuite[0];
713 my $skipreason = skip(@$testsuite);
714 if (defined($restricted)) {
715 # Find the testsuite for this test
716 my $match = undef;
717 foreach my $r (@$restricted) {
718 if ($r eq $name) {
719 $individual_tests->{$name} = [];
720 $match = $r;
721 $restricted_used->{$r} = 1;
722 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
723 push(@{$individual_tests->{$name}}, $r);
724 $match = $r;
725 $restricted_used->{$r} = 1;
728 if ($match) {
729 if (defined($skipreason)) {
730 if (not $opt_list) {
731 Subunit::skip_testsuite($name, $skipreason);
733 } else {
734 push(@todo, $testsuite);
737 } elsif (defined($skipreason)) {
738 if (not $opt_list) {
739 Subunit::skip_testsuite($name, $skipreason);
741 } else {
742 push(@todo, $testsuite);
746 if (defined($restricted)) {
747 foreach (@$restricted) {
748 unless (defined($restricted_used->{$_})) {
749 print "No test or testsuite found matching $_\n";
752 } elsif ($#todo == -1) {
753 print STDERR "No tests to run\n";
754 exit(1);
757 my $suitestotal = $#todo + 1;
759 unless ($opt_list) {
760 Subunit::progress($suitestotal);
761 Subunit::report_time();
764 my $i = 0;
765 $| = 1;
767 my %running_envs = ();
769 sub get_running_env($)
771 my ($name) = @_;
773 my $envname = $name;
775 $envname =~ s/:.*//;
777 return $running_envs{$envname};
780 sub sighandler($)
782 my $signame = shift;
784 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
785 $SIG{PIPE} = 'IGNORE';
787 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
789 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
790 teardown_env($_) foreach(keys %running_envs);
791 system("pstree -p $$");
792 print "$0: PID[$$]: Exiting...\n";
793 exit(1);
796 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
798 sub setup_env($$)
800 my ($name, $prefix) = @_;
802 my $testenv_vars = undef;
804 my $envname = $name;
805 my $option = $name;
807 $envname =~ s/:.*//;
808 $option =~ s/^[^:]*//;
809 $option =~ s/^://;
811 $option = "client" if $option eq "";
813 # Initially clear out the environment for the provision, so previous envs'
814 # variables don't leak in. Provisioning steps must explicitly set their
815 # necessary variables when calling out to other executables
816 Samba::clear_exported_envvars();
817 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
818 delete $ENV{SMB_CONF_PATH};
820 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
821 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
822 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
824 if (defined(get_running_env($envname))) {
825 $testenv_vars = get_running_env($envname);
826 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
827 print $testenv_vars->{target}->getlog_env($testenv_vars);
828 $testenv_vars = undef;
830 } else {
831 $testenv_vars = $target->setup_env($envname, $prefix);
832 if (not defined($testenv_vars)) {
833 my $msg = "$opt_target can't start up known environment '$envname'";
834 if ($opt_one) {
835 die($msg);
837 warn $msg;
838 return;
840 if (ref $testenv_vars ne "HASH") {
841 return $testenv_vars;
843 if (defined($testenv_vars->{target})) {
844 $testenv_vars->{target} = $target;
848 return undef unless defined($testenv_vars);
850 $running_envs{$envname} = $testenv_vars;
852 if ($option eq "local") {
853 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
854 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
855 } elsif ($option eq "client") {
856 SocketWrapper::set_default_iface(11);
857 write_clientconf($conffile, $clientdir, $testenv_vars);
858 $ENV{SMB_CONF_PATH} = $conffile;
859 } else {
860 die("Unknown option[$option] for envname[$envname]");
863 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
864 Samba::export_envvars($testenv_vars);
866 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
867 unlink($krb5_ccache_path);
868 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
869 return $testenv_vars;
872 sub getlog_env($)
874 my ($envname) = @_;
875 return "" if ($envname eq "none");
876 my $env = get_running_env($envname);
877 return $env->{target}->getlog_env($env);
880 sub check_env($)
882 my ($envname) = @_;
883 my $env = get_running_env($envname);
884 return $env->{target}->check_env($env);
887 sub teardown_env($)
889 my ($envname) = @_;
890 return if ($envname eq "none");
891 print STDERR "teardown_env($envname)\n";
892 my $env = get_running_env($envname);
893 $env->{target}->teardown_env($env);
894 delete $running_envs{$envname};
897 # This 'global' file needs to be empty when we start
898 unlink("$prefix_abs/dns_host_file");
899 unlink("$prefix_abs/hosts");
901 if ($opt_random_order) {
902 require List::Util;
903 my @newtodo = List::Util::shuffle(@todo);
904 @todo = @newtodo;
907 if ($opt_testenv) {
908 my $testenv_name = $ENV{SELFTEST_TESTENV};
909 $testenv_name = $testenv_default unless defined($testenv_name);
911 my $testenv_vars = setup_env($testenv_name, $prefix);
913 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
914 die("Unable to setup environment $testenv_name");
917 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
918 $ENV{ENVNAME} = $testenv_name;
920 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
922 my @term_args = ("echo -e \"
923 Welcome to the Samba4 Test environment '$testenv_name'
925 This matches the client environment used in make test
926 server is pid `cat \$PIDDIR/samba.pid`
928 Some useful environment variables:
929 TORTURE_OPTIONS=\$TORTURE_OPTIONS
930 SMB_CONF_PATH=\$SMB_CONF_PATH
932 $envvarstr
933 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
934 my @term = ();
935 if ($ENV{TERMINAL}) {
936 @term = ($ENV{TERMINAL});
937 # override the default terminal args (if specified)
938 if (defined($ENV{TERMINAL_ARGS})) {
939 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
941 } else {
942 @term = ("xterm", "-e");
943 unshift(@term_args, ("bash", "-c"));
946 system(@term, @term_args);
948 teardown_env($testenv_name);
949 } elsif ($opt_list) {
950 foreach (@todo) {
951 my $name = $$_[0];
952 my $envname = $$_[1];
953 my $cmd = $$_[2];
954 my $listcmd = $$_[3];
956 unless (defined($listcmd)) {
957 warn("Unable to list tests in $name");
958 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
959 # a single "test".
960 print "$name\n";
961 next;
964 system($listcmd);
966 if ($? == -1) {
967 die("Unable to run $listcmd: $!");
968 } elsif ($? & 127) {
969 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
972 my $exitcode = $? >> 8;
973 if ($exitcode != 0) {
974 die("$cmd exited with exit code $exitcode");
977 } else {
978 foreach (@todo) {
979 $i++;
980 my $cmd = $$_[2];
981 my $name = $$_[0];
982 my $envname = $$_[1];
983 my $envvars = setup_env($envname, $prefix);
985 if (not defined($envvars)) {
986 Subunit::start_testsuite($name);
987 Subunit::end_testsuite($name, "error",
988 "unable to set up environment $envname - exiting");
989 next;
990 } elsif ($envvars eq "UNKNOWN") {
991 Subunit::start_testsuite($name);
992 Subunit::end_testsuite($name, "error",
993 "environment $envname is unknown - exiting");
994 next;
997 # Generate a file with the individual tests to run, if the
998 # test runner for this test suite supports it.
999 if ($individual_tests and $individual_tests->{$name}) {
1000 if ($$_[3]) {
1001 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1002 foreach my $test (@{$individual_tests->{$name}}) {
1003 print $fh substr($test, length($name)+1) . "\n";
1005 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1006 } else {
1007 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1011 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1013 teardown_env($envname) if ($opt_resetup_env);
1017 print "\n";
1019 teardown_env($_) foreach (keys %running_envs);
1021 my $failed = 0;
1023 # if there were any valgrind failures, show them
1024 foreach (<$prefix/valgrind.log*>) {
1025 next unless (-s $_);
1026 print "VALGRIND FAILURE\n";
1027 $failed++;
1028 system("cat $_");
1030 exit 0;