s3-libnet: avoid using lp_dns_hostname() in join code
[samba4-gss.git] / selftest / selftest.pl
blob2455b75a36ee9e0932f4744cba24bc5c4cee1480
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_libpam_wrapper_so_path = "";
66 my $opt_libpam_matrix_so_path = "";
67 my $opt_libasan_so_path = "";
68 my $opt_libcrypt_so_path = "";
69 my $opt_use_dns_faking = 0;
70 my @testlists = ();
72 my $srcdir = ".";
73 my $bindir = "./bin";
74 my $prefix = "./st";
76 my @includes = ();
77 my @excludes = ();
79 sub find_in_list($$)
81 my ($list, $fullname) = @_;
83 foreach (@$list) {
84 if ($fullname =~ /$$_[0]/) {
85 return ($$_[1]) if ($$_[1]);
86 return "";
90 return undef;
93 sub skip
95 my ($name, $envname) = @_;
96 my ($env_basename, $env_localpart) = split(/:/, $envname);
98 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
99 return "environment $envname is disabled as this build does not include an AD DC";
102 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
103 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
104 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
105 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
108 return find_in_list(\@excludes, $name);
111 sub getlog_env($);
113 # expand strings from %ENV
114 sub expand_environment_strings($)
116 my $s = shift;
117 # we use a reverse sort so we do the longer ones first
118 foreach my $k (sort { $b cmp $a } keys %ENV) {
119 $s =~ s/\$$k/$ENV{$k}/g;
121 return $s;
124 my $target;
126 sub run_testsuite($$$$$)
128 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
129 my $pcap_file = $target->setup_pcap($name);
131 Subunit::report_time();
132 Subunit::start_testsuite($name);
133 Subunit::progress_push();
134 Subunit::report_time();
135 # Enable pipefail so that we catch failing testsuites that are part of a
136 # pipeline (typically, piped through filter-subunit). This won't catch
137 # any testsuite failures that are turned into testsuite-xfails by
138 # filter-subunit.
139 system("bash", "-o", "pipefail", "-c", $cmd);
140 Subunit::report_time();
141 Subunit::progress_pop();
143 if ($? == -1) {
144 print "command: $cmd\n";
145 printf "expanded command: %s\n", expand_environment_strings($cmd);
146 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
147 exit(1);
148 } elsif ($? & 127) {
149 print "command: $cmd\n";
150 printf "expanded command: %s\n", expand_environment_strings($cmd);
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 $target->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 [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
197 --exclude=FILE Exclude tests listed in the file
198 --include=FILE Include tests listed in the file
199 --exclude-env=ENV Exclude tests for the specified environment
200 --include-env=ENV Include tests for the specified environment
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 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
210 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
211 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
212 --asan_so_path=FILE the asan library to preload
214 DNS:
215 --use-dns-faking Fake DNS entries rather than talking to our
216 DNS implementation.
218 Target Specific:
219 --socket-wrapper-pcap save traffic to pcap directories
220 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
221 failed
222 --socket-wrapper enable socket wrapper
224 Behaviour:
225 --quick run quick overall test
226 --one abort when the first test fails
227 --testenv run a shell in the requested test environment
228 --list list available tests
230 exit(0);
233 my $result = GetOptions (
234 'help|h|?' => \$opt_help,
235 'target=s' => \$opt_target,
236 'prefix=s' => \$prefix,
237 'socket-wrapper' => \$opt_socket_wrapper,
238 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
239 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
240 'quick' => \$opt_quick,
241 'one' => \$opt_one,
242 'exclude=s' => \@opt_exclude,
243 'include=s' => \@opt_include,
244 'exclude-env=s' => \@opt_exclude_env,
245 'include-env=s' => \@opt_include_env,
246 'srcdir=s' => \$srcdir,
247 'bindir=s' => \$bindir,
248 'testenv' => \$opt_testenv,
249 'list' => \$opt_list,
250 'mitkrb5' => \$opt_mitkrb5,
251 'default-ldb-backend=s' => \$opt_default_ldb_backend,
252 'resetup-environment' => \$opt_resetup_env,
253 'testlist=s' => \@testlists,
254 'random-order' => \$opt_random_order,
255 'load-list=s' => \$opt_load_list,
256 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
257 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
258 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
259 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
260 'pam_wrapper_so_path=s' => \$opt_libpam_wrapper_so_path,
261 'pam_matrix_so_path=s' => \$opt_libpam_matrix_so_path,
262 'asan_so_path=s' => \$opt_libasan_so_path,
263 'crypt_so_path=s' => \$opt_libcrypt_so_path,
264 'use-dns-faking' => \$opt_use_dns_faking
267 exit(1) if (not $result);
269 ShowHelp() if ($opt_help);
271 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
273 # we want unbuffered output
274 $| = 1;
276 my @tests = @ARGV;
278 # quick hack to disable rpc validation when using valgrind - its way too slow
279 unless (defined($ENV{VALGRIND})) {
280 $ENV{VALIDATE} = "validate";
281 $ENV{MALLOC_CHECK_} = 3;
284 # make all our python scripts unbuffered
285 $ENV{PYTHONUNBUFFERED} = 1;
287 $ENV{SAMBA_DEPRECATED_SUPPRESS} = 1;
289 # do not depend on the users setup
290 # see also bootstrap/config.py
291 $ENV{TZ} = "UTC";
292 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
293 $ENV{LANGUAGE} = "en_US";
295 my $bindir_abs = abs_path($bindir);
297 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
299 $prefix =~ s+//+/+;
300 $prefix =~ s+/\./+/+;
301 $prefix =~ s+/$++;
303 die("using an empty prefix isn't allowed") unless $prefix ne "";
305 # Ensure we have the test prefix around.
307 # We need restrictive
308 # permissions on this as some subdirectories in this tree will have
309 # wider permissions (ie 0777) and this would allow other users on the
310 # host to subvert the test process.
311 umask 0077;
312 mkdir($prefix, 0700) unless -d $prefix;
313 chmod 0700, $prefix;
314 # We need to have no umask limitations for the tests.
315 umask 0000;
317 my $prefix_abs = abs_path($prefix);
318 my $tmpdir_abs = abs_path("$prefix/tmp");
319 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
321 my $srcdir_abs = abs_path($srcdir);
323 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
324 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
326 $ENV{SAMBA_SELFTEST} = "1";
328 $ENV{PREFIX} = $prefix;
329 $ENV{PREFIX_ABS} = $prefix_abs;
330 $ENV{SRCDIR} = $srcdir;
331 $ENV{SRCDIR_ABS} = $srcdir_abs;
332 $ENV{BINDIR} = $bindir_abs;
334 my $tls_enabled = not $opt_quick;
335 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
337 sub prefix_pathvar($$)
339 my ($name, $newpath) = @_;
340 if (defined($ENV{$name})) {
341 $ENV{$name} = "$newpath:$ENV{$name}";
342 } else {
343 $ENV{$name} = $newpath;
346 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
347 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
349 if ($opt_socket_wrapper_keep_pcap) {
350 # Socket wrapper keep pcap implies socket wrapper pcap
351 $opt_socket_wrapper_pcap = 1;
354 if ($opt_socket_wrapper_pcap) {
355 # Socket wrapper pcap implies socket wrapper
356 $opt_socket_wrapper = 1;
359 my $ld_preload = $ENV{LD_PRELOAD};
361 if ($opt_libasan_so_path) {
362 if ($ld_preload) {
363 if ($opt_libcrypt_so_path) {
364 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path:$ld_preload";
365 } else {
366 $ld_preload = "$opt_libasan_so_path:$ld_preload";
368 } else {
369 if ($opt_libcrypt_so_path) {
370 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path";
371 } else {
372 $ld_preload = "$opt_libasan_so_path";
377 if ($opt_libnss_wrapper_so_path) {
378 if ($ld_preload) {
379 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
380 } else {
381 $ld_preload = "$opt_libnss_wrapper_so_path";
385 if ($opt_libresolv_wrapper_so_path) {
386 if ($ld_preload) {
387 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
388 } else {
389 $ld_preload = "$opt_libresolv_wrapper_so_path";
393 if ($opt_libsocket_wrapper_so_path) {
394 if ($ld_preload) {
395 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
396 } else {
397 $ld_preload = "$opt_libsocket_wrapper_so_path";
401 if ($opt_libuid_wrapper_so_path) {
402 if ($ld_preload) {
403 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
404 } else {
405 $ld_preload = "$opt_libuid_wrapper_so_path";
409 if ($opt_libpam_wrapper_so_path) {
410 if ($ld_preload) {
411 $ld_preload = "$ld_preload:$opt_libpam_wrapper_so_path";
412 } else {
413 $ld_preload = "$opt_libpam_wrapper_so_path";
417 if (defined($ENV{USE_NAMESPACES})) {
418 print "Using linux containerization for selftest testenv(s)...\n";
420 # Create a common bridge to connect up the testenv namespaces. We give
421 # it the client's IP address, as this is where the tests will run from
422 my $ipv4_addr = Samba::get_ipv4_addr("client");
423 my $ipv6_addr = Samba::get_ipv6_addr("client");
424 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
427 $ENV{LD_PRELOAD} = $ld_preload;
428 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
430 # Enable uid_wrapper globally
431 $ENV{UID_WRAPPER} = 1;
433 # We are already hitting the limit, so double it.
434 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
436 my $socket_wrapper_dir;
437 if ($opt_socket_wrapper) {
438 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
439 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
440 } elsif (not $opt_list) {
441 unless ($< == 0) {
442 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
446 if ($opt_use_dns_faking) {
447 print "DNS: Faking nameserver\n";
448 $ENV{SAMBA_DNS_FAKING} = 1;
451 my $testenv_default = "none";
453 if ($opt_mitkrb5 == 1) {
454 $ENV{MITKRB5} = $opt_mitkrb5;
455 $ENV{KRB5RCACHETYPE} = "none";
458 # Enable support for SHA1 in OpenSSL
459 # This is required e.g. for pkinit sha1 tests
460 $ENV{OPENSSL_ENABLE_SHA1_SIGNATURES} = 1;
462 # After this many seconds, the server will self-terminate. All tests
463 # must terminate in this time, and testenv will only stay alive this
464 # long
466 my $server_maxtime;
467 if ($opt_testenv) {
468 # 1 year should be enough :-)
469 $server_maxtime = 365 * 24 * 60 * 60;
470 } else {
471 # make test should run under 5 hours
472 $server_maxtime = 5 * 60 * 60;
475 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
476 $server_maxtime = $ENV{SMBD_MAXTIME};
479 $target = new Samba($bindir, $srcdir, $server_maxtime,
480 $opt_socket_wrapper_pcap,
481 $opt_socket_wrapper_keep_pcap,
482 $opt_libpam_matrix_so_path,
483 $opt_default_ldb_backend);
484 unless ($opt_list) {
485 if ($opt_target eq "samba") {
486 $testenv_default = "ad_dc";
487 } elsif ($opt_target eq "samba3") {
488 $testenv_default = "nt4_member";
492 sub read_test_regexes($)
494 my ($name) = @_;
495 my @ret = ();
496 open(LF, "<$name") or die("unable to read $name: $!");
497 while (<LF>) {
498 chomp;
499 next if (/^#/);
500 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
501 push (@ret, [$1, $4]);
502 } else {
503 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
504 push (@ret, [$_, undef]);
507 close(LF);
508 return @ret;
511 foreach (@opt_exclude) {
512 push (@excludes, read_test_regexes($_));
515 foreach (@opt_include) {
516 push (@includes, read_test_regexes($_));
519 # We give the selftest client 6 different IPv4 addresses to use. Most tests
520 # only use the first (.11) IP. Note that winsreplication.c is one test that
521 # uses the other IPs (search for iface_list_count()).
522 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
523 my $interfaces = Samba::get_interfaces_config("client", 6);
525 my $clientdir = "$prefix_abs/client";
527 my $conffile = "$clientdir/client.conf";
528 $ENV{SMB_CONF_PATH} = $conffile;
530 sub write_clientconf($$$)
532 my ($conffile, $clientdir, $vars) = @_;
534 mkdir("$clientdir", 0777) unless -d "$clientdir";
536 my @subdirs = (
537 { name => "private", mask => 0777 },
538 { name => "bind-dns", mask => 0777 },
539 { name => "lockdir", mask => 0777 },
540 { name => "statedir", mask => 0777 },
541 { name => "cachedir", mask => 0777 },
542 { name => "pkinit", mask => 0700 },
543 { name => "pid", mask => 0777 },
544 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
545 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
548 foreach my $sub (@subdirs) {
549 my $dir = "$clientdir/$sub->{name}";
550 remove_tree($dir);
551 my $mask = umask;
552 if (defined($sub->{umask})) {
553 umask $sub->{umask};
555 mkdir($dir, $sub->{mask});
556 umask $mask;
559 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
560 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
561 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
562 my $ca_users_dir = "$cadir/Users";
563 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
565 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
566 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
567 # We make a copy here and make the certificated easily
568 # accessible in the client environment.
569 my $mask = umask;
570 umask 0077;
571 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
572 for my $d (readdir USERS) {
573 my $user_dir = "${ca_users_dir}/${d}";
574 next if ${d} =~ /^\./;
575 next if (! -d "${user_dir}");
576 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
577 for my $l (readdir USER) {
578 my $user_link = "${user_dir}/${l}";
579 next if ${l} =~ /^\./;
580 next if (! -l "${user_link}");
582 my $dest = "${clientdir}/pkinit/${l}";
583 Samba::copy_file_content(${user_link}, ${dest});
585 closedir USER;
587 closedir USERS;
588 umask $mask;
590 open(CF, ">$conffile");
591 print CF "[global]\n";
592 print CF "\tnetbios name = client\n";
593 if (defined($vars->{DOMAIN})) {
594 print CF "\tworkgroup = $vars->{DOMAIN}\n";
596 if (defined($vars->{REALM})) {
597 print CF "\trealm = $vars->{REALM}\n";
599 if ($opt_socket_wrapper) {
600 print CF "\tinterfaces = $interfaces\n";
602 print CF "
603 private dir = $clientdir/private
604 binddns dir = $clientdir/bind-dns
605 lock dir = $clientdir/lockdir
606 state directory = $clientdir/statedir
607 cache directory = $clientdir/cachedir
608 ncalrpc dir = $clientdir/ncalrpcdir
609 pid directory = $clientdir/pid
610 panic action = $RealBin/gdb_backtrace \%d
611 max xmit = 32K
612 notify:inotify = false
613 ldb:nosync = true
614 system:anonymous = true
615 client lanman auth = Yes
616 client min protocol = CORE
617 log level = $client_loglevel
618 torture:basedir = $clientdir
619 #We don't want to run 'speed' tests for very long
620 torture:timelimit = 1
621 winbind separator = /
622 tls cafile = ${cacert}
623 tls crlfile = ${cacrl_pem}
624 tls verify peer = no_check
625 include system krb5 conf = no
626 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
628 close(CF);
631 my @todo = ();
633 sub should_run_test($)
635 my $name = shift;
636 if ($#tests == -1) {
637 return 1;
639 for (my $i=0; $i <= $#tests; $i++) {
640 if ($name =~ /$tests[$i]/i) {
641 return 1;
644 return 0;
647 sub read_testlist($)
649 my ($filename) = @_;
651 my @ret = ();
652 open(IN, $filename) or die("Unable to open $filename: $!");
654 while (<IN>) {
655 if (/-- TEST(-LOADLIST|) --\n/) {
656 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
657 my $name = <IN>;
658 $name =~ s/\n//g;
659 my $env = <IN>;
660 $env =~ s/\n//g;
661 my $loadlist;
662 if ($supports_loadlist) {
663 $loadlist = <IN>;
664 $loadlist =~ s/\n//g;
666 my $cmdline = <IN>;
667 $cmdline =~ s/\n//g;
668 if (should_run_test($name) == 1) {
669 push (@ret, [$name, $env, $cmdline, $loadlist]);
671 } else {
672 print;
675 close(IN) or die("Error creating recipe from $filename");
676 return @ret;
679 if ($#testlists == -1) {
680 die("No testlists specified");
683 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
684 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
685 $ENV{TMPDIR} = "$tmpdir_abs";
686 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
687 if ($opt_quick) {
688 $ENV{SELFTEST_QUICK} = "1";
689 } else {
690 $ENV{SELFTEST_QUICK} = "";
692 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
694 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
695 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
697 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
698 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
700 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
701 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
703 my @available = ();
704 foreach my $fn (@testlists) {
705 foreach (read_testlist($fn)) {
706 my $name = $$_[0];
707 next if (@includes and not defined(find_in_list(\@includes, $name)));
708 push (@available, $_);
712 my $restricted = undef;
713 my $restricted_used = {};
715 if ($opt_load_list) {
716 $restricted = [];
717 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
718 while (<LOAD_LIST>) {
719 chomp;
720 push (@$restricted, $_);
722 close(LOAD_LIST);
725 my $individual_tests = undef;
726 $individual_tests = {};
728 foreach my $testsuite (@available) {
729 my $name = $$testsuite[0];
730 my $skipreason = skip(@$testsuite);
731 if (defined($restricted)) {
732 # Find the testsuite for this test
733 my $match = undef;
734 foreach my $r (@$restricted) {
735 if ($r eq $name) {
736 $individual_tests->{$name} = [];
737 $match = $r;
738 $restricted_used->{$r} = 1;
739 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
740 push(@{$individual_tests->{$name}}, $r);
741 $match = $r;
742 $restricted_used->{$r} = 1;
745 if ($match) {
746 if (defined($skipreason)) {
747 if (not $opt_list) {
748 Subunit::skip_testsuite($name, $skipreason);
750 } else {
751 push(@todo, $testsuite);
754 } elsif (defined($skipreason)) {
755 if (not $opt_list) {
756 Subunit::skip_testsuite($name, $skipreason);
758 } else {
759 push(@todo, $testsuite);
763 if (defined($restricted)) {
764 foreach (@$restricted) {
765 unless (defined($restricted_used->{$_})) {
766 print "No test or testsuite found matching $_\n";
769 } elsif ($#todo == -1) {
770 print STDERR "No tests to run\n";
771 exit(1);
774 my $suitestotal = $#todo + 1;
776 unless ($opt_list) {
777 Subunit::progress($suitestotal);
778 Subunit::report_time();
781 my $i = 0;
782 $| = 1;
784 my %running_envs = ();
786 sub get_running_env($)
788 my ($name) = @_;
790 my $envname = $name;
792 $envname =~ s/:.*//;
794 return $running_envs{$envname};
797 sub sighandler($)
799 my $signame = shift;
801 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
802 $SIG{PIPE} = 'IGNORE';
804 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
806 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
807 teardown_env($_) foreach(keys %running_envs);
808 system("pstree -p $$");
809 print "$0: PID[$$]: Exiting...\n";
810 exit(1);
813 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
815 sub setup_env($$)
817 my ($name, $prefix) = @_;
819 my $testenv_vars = undef;
821 my $envname = $name;
822 my $option = $name;
824 $envname =~ s/:.*//;
825 $option =~ s/^[^:]*//;
826 $option =~ s/^://;
828 $option = "client" if $option eq "";
830 # Initially clear out the environment for the provision, so previous envs'
831 # variables don't leak in. Provisioning steps must explicitly set their
832 # necessary variables when calling out to other executables
833 Samba::clear_exported_envvars();
834 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
835 delete $ENV{SMB_CONF_PATH};
837 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
838 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
839 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
841 if (defined(get_running_env($envname))) {
842 $testenv_vars = get_running_env($envname);
843 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
844 print $testenv_vars->{target}->getlog_env($testenv_vars);
845 $testenv_vars = undef;
847 } else {
848 $testenv_vars = $target->setup_env($envname, $prefix);
849 if (not defined($testenv_vars)) {
850 my $msg = "$opt_target can't start up known environment '$envname'";
851 if ($opt_one) {
852 die($msg);
854 warn $msg;
855 return;
857 if (ref $testenv_vars ne "HASH") {
858 return $testenv_vars;
860 if (defined($testenv_vars->{target})) {
861 $testenv_vars->{target} = $target;
865 return undef unless defined($testenv_vars);
867 $running_envs{$envname} = $testenv_vars;
869 if ($option eq "local") {
870 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
871 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
872 } elsif ($option eq "client") {
873 SocketWrapper::set_default_iface(11);
874 write_clientconf($conffile, $clientdir, $testenv_vars);
875 $ENV{SMB_CONF_PATH} = $conffile;
876 } else {
877 die("Unknown option[$option] for envname[$envname]");
880 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
881 Samba::export_envvars($testenv_vars);
883 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
884 unlink($krb5_ccache_path);
885 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
886 return $testenv_vars;
889 sub getlog_env($)
891 my ($envname) = @_;
892 return "" if ($envname eq "none");
893 my $env = get_running_env($envname);
894 return $env->{target}->getlog_env($env);
897 sub check_env($)
899 my ($envname) = @_;
900 my $env = get_running_env($envname);
901 return $env->{target}->check_env($env);
904 sub teardown_env($)
906 my ($envname) = @_;
907 return if ($envname eq "none");
908 print STDERR "teardown_env($envname)\n";
909 my $env = get_running_env($envname);
910 $env->{target}->teardown_env($env);
911 delete $running_envs{$envname};
914 # This 'global' file needs to be empty when we start
915 unlink("$prefix_abs/dns_host_file");
916 unlink("$prefix_abs/hosts");
918 if ($opt_random_order) {
919 require List::Util;
920 my @newtodo = List::Util::shuffle(@todo);
921 @todo = @newtodo;
924 if ($opt_testenv) {
925 my $testenv_name = $ENV{SELFTEST_TESTENV};
926 $testenv_name = $testenv_default unless defined($testenv_name);
928 my $testenv_vars = setup_env($testenv_name, $prefix);
930 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
931 die("Unable to setup environment $testenv_name");
934 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
935 $ENV{ENVNAME} = $testenv_name;
937 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
939 my @term_args = ("echo -e \"
940 Welcome to the Samba4 Test environment '$testenv_name'
942 This matches the client environment used in make test
943 server is pid `cat \$PIDDIR/samba.pid`
945 Some useful environment variables:
946 TORTURE_OPTIONS=\$TORTURE_OPTIONS
947 SMB_CONF_PATH=\$SMB_CONF_PATH
949 $envvarstr
950 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
951 my @term = ();
952 if ($ENV{TERMINAL}) {
953 @term = ($ENV{TERMINAL});
954 # override the default terminal args (if specified)
955 if (defined($ENV{TERMINAL_ARGS})) {
956 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
958 } else {
959 @term = ("xterm", "-e");
960 unshift(@term_args, ("bash", "-c"));
963 system(@term, @term_args);
965 teardown_env($testenv_name);
966 } elsif ($opt_list) {
967 foreach (@todo) {
968 my $name = $$_[0];
969 my $envname = $$_[1];
970 my $cmd = $$_[2];
971 my $listcmd = $$_[3];
973 unless (defined($listcmd)) {
974 warn("Unable to list tests in $name");
975 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
976 # a single "test".
977 print "$name\n";
978 next;
981 system($listcmd);
983 if ($? == -1) {
984 die("Unable to run $listcmd: $!");
985 } elsif ($? & 127) {
986 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
989 my $exitcode = $? >> 8;
990 if ($exitcode != 0) {
991 die("$cmd exited with exit code $exitcode");
994 } else {
995 foreach (@todo) {
996 $i++;
997 my $cmd = $$_[2];
998 my $name = $$_[0];
999 my $envname = $$_[1];
1000 my $envvars = setup_env($envname, $prefix);
1002 if (not defined($envvars)) {
1003 Subunit::start_testsuite($name);
1004 Subunit::end_testsuite($name, "error",
1005 "unable to set up environment $envname - exiting");
1006 next;
1007 } elsif ($envvars eq "UNKNOWN") {
1008 Subunit::start_testsuite($name);
1009 Subunit::end_testsuite($name, "error",
1010 "environment $envname is unknown - exiting");
1011 next;
1014 # Generate a file with the individual tests to run, if the
1015 # test runner for this test suite supports it.
1016 if ($individual_tests and $individual_tests->{$name}) {
1017 if ($$_[3]) {
1018 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1019 foreach my $test (@{$individual_tests->{$name}}) {
1020 print $fh substr($test, length($name)+1) . "\n";
1022 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1023 } else {
1024 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1028 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1030 teardown_env($envname) if ($opt_resetup_env);
1034 print "\n";
1036 teardown_env($_) foreach (keys %running_envs);
1038 my $failed = 0;
1040 # if there were any valgrind failures, show them
1041 foreach (<$prefix/valgrind.log*>) {
1042 next unless (-s $_);
1043 print "VALGRIND FAILURE\n";
1044 $failed++;
1045 system("cat $_");
1047 exit 0;