ctdb-daemon: Use ctdb_connection_to_buf() to simplify
[Samba.git] / selftest / selftest.pl
blobcff150c9eb7d34eca99be45bb6e2c37c6e647258
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 # Enable pipefail so that we catch failing testsuites that are part of a
133 # pipeline (typically, piped through filter-subunit). This won't catch
134 # any testsuite failures that are turned into testsuite-xfails by
135 # filter-subunit.
136 system("bash", "-o", "pipefail", "-c", $cmd);
137 Subunit::report_time();
138 Subunit::progress_pop();
140 if ($? == -1) {
141 print "command: $cmd\n";
142 printf "expanded command: %s\n", expand_environment_strings($cmd);
143 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
144 exit(1);
145 } elsif ($? & 127) {
146 print "command: $cmd\n";
147 printf "expanded command: %s\n", expand_environment_strings($cmd);
148 Subunit::end_testsuite($name, "error",
149 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
150 exit(1);
153 my $exitcode = $? >> 8;
155 my $envlog = getlog_env($envname);
156 if ($envlog ne "") {
157 print "envlog: $envlog\n";
160 print "command: $cmd\n";
161 printf "expanded command: %s\n", expand_environment_strings($cmd);
163 if ($exitcode == 0) {
164 Subunit::end_testsuite($name, "success");
165 } else {
166 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
169 $target->cleanup_pcap($pcap_file, $exitcode);
171 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
172 print "PCAP FILE: $pcap_file\n";
175 if ($exitcode != 0) {
176 exit(1) if ($opt_one);
179 return $exitcode;
182 sub ShowHelp()
184 print "Samba test runner
185 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
186 Copyright (C) Stefan Metzmacher <metze\@samba.org>
188 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
190 Generic options:
191 --help this help page
192 --target=samba[3]|win Samba version to target
193 --testlist=FILE file to read available tests from
194 --exclude=FILE Exclude tests listed in the file
195 --include=FILE Include tests listed in the file
196 --exclude-env=ENV Exclude tests for the specified environment
197 --include-env=ENV Include tests for the specified environment
199 Paths:
200 --prefix=DIR prefix to run tests in [st]
201 --srcdir=DIR source directory [.]
202 --bindir=DIR binaries directory [./bin]
204 Preload cwrap:
205 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
206 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
207 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
208 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
209 --asan_so_path=FILE the asan library to preload
211 DNS:
212 --use-dns-faking Fake DNS entries rather than talking to our
213 DNS implementation.
215 Target Specific:
216 --socket-wrapper-pcap save traffic to pcap directories
217 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
218 failed
219 --socket-wrapper enable socket wrapper
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 'exclude-env=s' => \@opt_exclude_env,
242 'include-env=s' => \@opt_include_env,
243 'srcdir=s' => \$srcdir,
244 'bindir=s' => \$bindir,
245 'testenv' => \$opt_testenv,
246 'list' => \$opt_list,
247 'mitkrb5' => \$opt_mitkrb5,
248 'default-ldb-backend=s' => \$opt_default_ldb_backend,
249 'resetup-environment' => \$opt_resetup_env,
250 'testlist=s' => \@testlists,
251 'random-order' => \$opt_random_order,
252 'load-list=s' => \$opt_load_list,
253 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
254 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
255 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
256 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
257 'asan_so_path=s' => \$opt_libasan_so_path,
258 'crypt_so_path=s' => \$opt_libcrypt_so_path,
259 'use-dns-faking' => \$opt_use_dns_faking
262 exit(1) if (not $result);
264 ShowHelp() if ($opt_help);
266 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
268 # we want unbuffered output
269 $| = 1;
271 my @tests = @ARGV;
273 # quick hack to disable rpc validation when using valgrind - its way too slow
274 unless (defined($ENV{VALGRIND})) {
275 $ENV{VALIDATE} = "validate";
276 $ENV{MALLOC_CHECK_} = 3;
279 # make all our python scripts unbuffered
280 $ENV{PYTHONUNBUFFERED} = 1;
282 $ENV{SAMBA_DEPRECATED_SUPPRESS} = 1;
284 # do not depend on the users setup
285 # see also bootstrap/config.py
286 $ENV{TZ} = "UTC";
287 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
288 $ENV{LANGUAGE} = "en_US";
290 my $bindir_abs = abs_path($bindir);
292 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
294 $prefix =~ s+//+/+;
295 $prefix =~ s+/\./+/+;
296 $prefix =~ s+/$++;
298 die("using an empty prefix isn't allowed") unless $prefix ne "";
300 # Ensure we have the test prefix around.
302 # We need restrictive
303 # permissions on this as some subdirectories in this tree will have
304 # wider permissions (ie 0777) and this would allow other users on the
305 # host to subvert the test process.
306 umask 0077;
307 mkdir($prefix, 0700) unless -d $prefix;
308 chmod 0700, $prefix;
309 # We need to have no umask limitations for the tests.
310 umask 0000;
312 my $prefix_abs = abs_path($prefix);
313 my $tmpdir_abs = abs_path("$prefix/tmp");
314 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
316 my $srcdir_abs = abs_path($srcdir);
318 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
319 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
321 $ENV{SAMBA_SELFTEST} = "1";
323 $ENV{PREFIX} = $prefix;
324 $ENV{PREFIX_ABS} = $prefix_abs;
325 $ENV{SRCDIR} = $srcdir;
326 $ENV{SRCDIR_ABS} = $srcdir_abs;
327 $ENV{BINDIR} = $bindir_abs;
329 my $tls_enabled = not $opt_quick;
330 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
332 sub prefix_pathvar($$)
334 my ($name, $newpath) = @_;
335 if (defined($ENV{$name})) {
336 $ENV{$name} = "$newpath:$ENV{$name}";
337 } else {
338 $ENV{$name} = $newpath;
341 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
342 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
344 if ($opt_socket_wrapper_keep_pcap) {
345 # Socket wrapper keep pcap implies socket wrapper pcap
346 $opt_socket_wrapper_pcap = 1;
349 if ($opt_socket_wrapper_pcap) {
350 # Socket wrapper pcap implies socket wrapper
351 $opt_socket_wrapper = 1;
354 my $ld_preload = $ENV{LD_PRELOAD};
356 if ($opt_libasan_so_path) {
357 if ($ld_preload) {
358 if ($opt_libcrypt_so_path) {
359 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path:$ld_preload";
360 } else {
361 $ld_preload = "$opt_libasan_so_path:$ld_preload";
363 } else {
364 if ($opt_libcrypt_so_path) {
365 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path";
366 } else {
367 $ld_preload = "$opt_libasan_so_path";
372 if ($opt_libnss_wrapper_so_path) {
373 if ($ld_preload) {
374 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
375 } else {
376 $ld_preload = "$opt_libnss_wrapper_so_path";
380 if ($opt_libresolv_wrapper_so_path) {
381 if ($ld_preload) {
382 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
383 } else {
384 $ld_preload = "$opt_libresolv_wrapper_so_path";
388 if ($opt_libsocket_wrapper_so_path) {
389 if ($ld_preload) {
390 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
391 } else {
392 $ld_preload = "$opt_libsocket_wrapper_so_path";
396 if ($opt_libuid_wrapper_so_path) {
397 if ($ld_preload) {
398 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
399 } else {
400 $ld_preload = "$opt_libuid_wrapper_so_path";
404 if (defined($ENV{USE_NAMESPACES})) {
405 print "Using linux containerization for selftest testenv(s)...\n";
407 # Create a common bridge to connect up the testenv namespaces. We give
408 # it the client's IP address, as this is where the tests will run from
409 my $ipv4_addr = Samba::get_ipv4_addr("client");
410 my $ipv6_addr = Samba::get_ipv6_addr("client");
411 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
414 $ENV{LD_PRELOAD} = $ld_preload;
415 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
417 # Enable uid_wrapper globally
418 $ENV{UID_WRAPPER} = 1;
420 # We are already hitting the limit, so double it.
421 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
423 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
425 # This is needed in order to allow the ldb_*ldap module
426 # to work with a preloaded socket wrapper.
427 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
429 my $socket_wrapper_dir;
430 if ($opt_socket_wrapper) {
431 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
432 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
433 } elsif (not $opt_list) {
434 unless ($< == 0) {
435 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
439 if ($opt_use_dns_faking) {
440 print "DNS: Faking nameserver\n";
441 $ENV{SAMBA_DNS_FAKING} = 1;
444 my $testenv_default = "none";
446 if ($opt_mitkrb5 == 1) {
447 $ENV{MITKRB5} = $opt_mitkrb5;
448 $ENV{KRB5RCACHETYPE} = "none";
451 # After this many seconds, the server will self-terminate. All tests
452 # must terminate in this time, and testenv will only stay alive this
453 # long
455 my $server_maxtime;
456 if ($opt_testenv) {
457 # 1 year should be enough :-)
458 $server_maxtime = 365 * 24 * 60 * 60;
459 } else {
460 # make test should run under 5 hours
461 $server_maxtime = 5 * 60 * 60;
464 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
465 $server_maxtime = $ENV{SMBD_MAXTIME};
468 $target = new Samba($bindir, $srcdir, $server_maxtime,
469 $opt_socket_wrapper_pcap,
470 $opt_socket_wrapper_keep_pcap,
471 $opt_default_ldb_backend);
472 unless ($opt_list) {
473 if ($opt_target eq "samba") {
474 $testenv_default = "ad_dc";
475 } elsif ($opt_target eq "samba3") {
476 $testenv_default = "nt4_member";
480 sub read_test_regexes($)
482 my ($name) = @_;
483 my @ret = ();
484 open(LF, "<$name") or die("unable to read $name: $!");
485 while (<LF>) {
486 chomp;
487 next if (/^#/);
488 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
489 push (@ret, [$1, $4]);
490 } else {
491 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
492 push (@ret, [$_, undef]);
495 close(LF);
496 return @ret;
499 foreach (@opt_exclude) {
500 push (@excludes, read_test_regexes($_));
503 foreach (@opt_include) {
504 push (@includes, read_test_regexes($_));
507 # We give the selftest client 6 different IPv4 addresses to use. Most tests
508 # only use the first (.11) IP. Note that winsreplication.c is one test that
509 # uses the other IPs (search for iface_list_count()).
510 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
511 my $interfaces = Samba::get_interfaces_config("client", 6);
513 my $clientdir = "$prefix_abs/client";
515 my $conffile = "$clientdir/client.conf";
516 $ENV{SMB_CONF_PATH} = $conffile;
518 sub write_clientconf($$$)
520 my ($conffile, $clientdir, $vars) = @_;
522 mkdir("$clientdir", 0777) unless -d "$clientdir";
524 my @subdirs = (
525 { name => "private", mask => 0777 },
526 { name => "bind-dns", mask => 0777 },
527 { name => "lockdir", mask => 0777 },
528 { name => "statedir", mask => 0777 },
529 { name => "cachedir", mask => 0777 },
530 { name => "pkinit", mask => 0700 },
531 { name => "pid", mask => 0777 },
532 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
533 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
536 foreach my $sub (@subdirs) {
537 my $dir = "$clientdir/$sub->{name}";
538 remove_tree($dir);
539 my $mask = umask;
540 if (defined($sub->{umask})) {
541 umask $sub->{umask};
543 mkdir($dir, $sub->{mask});
544 umask $mask;
547 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
548 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
549 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
550 my $ca_users_dir = "$cadir/Users";
551 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
553 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
554 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
555 # We make a copy here and make the certificated easily
556 # accessible in the client environment.
557 my $mask = umask;
558 umask 0077;
559 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
560 for my $d (readdir USERS) {
561 my $user_dir = "${ca_users_dir}/${d}";
562 next if ${d} =~ /^\./;
563 next if (! -d "${user_dir}");
564 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
565 for my $l (readdir USER) {
566 my $user_link = "${user_dir}/${l}";
567 next if ${l} =~ /^\./;
568 next if (! -l "${user_link}");
570 my $dest = "${clientdir}/pkinit/${l}";
571 Samba::copy_file_content(${user_link}, ${dest});
573 closedir USER;
575 closedir USERS;
576 umask $mask;
578 open(CF, ">$conffile");
579 print CF "[global]\n";
580 print CF "\tnetbios name = client\n";
581 if (defined($vars->{DOMAIN})) {
582 print CF "\tworkgroup = $vars->{DOMAIN}\n";
584 if (defined($vars->{REALM})) {
585 print CF "\trealm = $vars->{REALM}\n";
587 if ($opt_socket_wrapper) {
588 print CF "\tinterfaces = $interfaces\n";
590 print CF "
591 private dir = $clientdir/private
592 binddns dir = $clientdir/bind-dns
593 lock dir = $clientdir/lockdir
594 state directory = $clientdir/statedir
595 cache directory = $clientdir/cachedir
596 ncalrpc dir = $clientdir/ncalrpcdir
597 pid directory = $clientdir/pid
598 panic action = $RealBin/gdb_backtrace \%d
599 max xmit = 32K
600 notify:inotify = false
601 ldb:nosync = true
602 system:anonymous = true
603 client lanman auth = Yes
604 client min protocol = CORE
605 log level = $client_loglevel
606 torture:basedir = $clientdir
607 #We don't want to run 'speed' tests for very long
608 torture:timelimit = 1
609 winbind separator = /
610 tls cafile = ${cacert}
611 tls crlfile = ${cacrl_pem}
612 tls verify peer = no_check
613 include system krb5 conf = no
614 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
616 close(CF);
619 my @todo = ();
621 sub should_run_test($)
623 my $name = shift;
624 if ($#tests == -1) {
625 return 1;
627 for (my $i=0; $i <= $#tests; $i++) {
628 if ($name =~ /$tests[$i]/i) {
629 return 1;
632 return 0;
635 sub read_testlist($)
637 my ($filename) = @_;
639 my @ret = ();
640 open(IN, $filename) or die("Unable to open $filename: $!");
642 while (<IN>) {
643 if (/-- TEST(-LOADLIST|) --\n/) {
644 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
645 my $name = <IN>;
646 $name =~ s/\n//g;
647 my $env = <IN>;
648 $env =~ s/\n//g;
649 my $loadlist;
650 if ($supports_loadlist) {
651 $loadlist = <IN>;
652 $loadlist =~ s/\n//g;
654 my $cmdline = <IN>;
655 $cmdline =~ s/\n//g;
656 if (should_run_test($name) == 1) {
657 push (@ret, [$name, $env, $cmdline, $loadlist]);
659 } else {
660 print;
663 close(IN) or die("Error creating recipe from $filename");
664 return @ret;
667 if ($#testlists == -1) {
668 die("No testlists specified");
671 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
672 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
673 $ENV{TMPDIR} = "$tmpdir_abs";
674 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
675 if ($opt_quick) {
676 $ENV{SELFTEST_QUICK} = "1";
677 } else {
678 $ENV{SELFTEST_QUICK} = "";
680 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
682 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
683 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
685 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
686 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
688 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
689 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
691 my @available = ();
692 foreach my $fn (@testlists) {
693 foreach (read_testlist($fn)) {
694 my $name = $$_[0];
695 next if (@includes and not defined(find_in_list(\@includes, $name)));
696 push (@available, $_);
700 my $restricted = undef;
701 my $restricted_used = {};
703 if ($opt_load_list) {
704 $restricted = [];
705 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
706 while (<LOAD_LIST>) {
707 chomp;
708 push (@$restricted, $_);
710 close(LOAD_LIST);
713 my $individual_tests = undef;
714 $individual_tests = {};
716 foreach my $testsuite (@available) {
717 my $name = $$testsuite[0];
718 my $skipreason = skip(@$testsuite);
719 if (defined($restricted)) {
720 # Find the testsuite for this test
721 my $match = undef;
722 foreach my $r (@$restricted) {
723 if ($r eq $name) {
724 $individual_tests->{$name} = [];
725 $match = $r;
726 $restricted_used->{$r} = 1;
727 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
728 push(@{$individual_tests->{$name}}, $r);
729 $match = $r;
730 $restricted_used->{$r} = 1;
733 if ($match) {
734 if (defined($skipreason)) {
735 if (not $opt_list) {
736 Subunit::skip_testsuite($name, $skipreason);
738 } else {
739 push(@todo, $testsuite);
742 } elsif (defined($skipreason)) {
743 if (not $opt_list) {
744 Subunit::skip_testsuite($name, $skipreason);
746 } else {
747 push(@todo, $testsuite);
751 if (defined($restricted)) {
752 foreach (@$restricted) {
753 unless (defined($restricted_used->{$_})) {
754 print "No test or testsuite found matching $_\n";
757 } elsif ($#todo == -1) {
758 print STDERR "No tests to run\n";
759 exit(1);
762 my $suitestotal = $#todo + 1;
764 unless ($opt_list) {
765 Subunit::progress($suitestotal);
766 Subunit::report_time();
769 my $i = 0;
770 $| = 1;
772 my %running_envs = ();
774 sub get_running_env($)
776 my ($name) = @_;
778 my $envname = $name;
780 $envname =~ s/:.*//;
782 return $running_envs{$envname};
785 sub sighandler($)
787 my $signame = shift;
789 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
790 $SIG{PIPE} = 'IGNORE';
792 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
794 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
795 teardown_env($_) foreach(keys %running_envs);
796 system("pstree -p $$");
797 print "$0: PID[$$]: Exiting...\n";
798 exit(1);
801 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
803 sub setup_env($$)
805 my ($name, $prefix) = @_;
807 my $testenv_vars = undef;
809 my $envname = $name;
810 my $option = $name;
812 $envname =~ s/:.*//;
813 $option =~ s/^[^:]*//;
814 $option =~ s/^://;
816 $option = "client" if $option eq "";
818 # Initially clear out the environment for the provision, so previous envs'
819 # variables don't leak in. Provisioning steps must explicitly set their
820 # necessary variables when calling out to other executables
821 Samba::clear_exported_envvars();
822 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
823 delete $ENV{SMB_CONF_PATH};
825 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
826 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
827 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
829 if (defined(get_running_env($envname))) {
830 $testenv_vars = get_running_env($envname);
831 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
832 print $testenv_vars->{target}->getlog_env($testenv_vars);
833 $testenv_vars = undef;
835 } else {
836 $testenv_vars = $target->setup_env($envname, $prefix);
837 if (not defined($testenv_vars)) {
838 my $msg = "$opt_target can't start up known environment '$envname'";
839 if ($opt_one) {
840 die($msg);
842 warn $msg;
843 return;
845 if (ref $testenv_vars ne "HASH") {
846 return $testenv_vars;
848 if (defined($testenv_vars->{target})) {
849 $testenv_vars->{target} = $target;
853 return undef unless defined($testenv_vars);
855 $running_envs{$envname} = $testenv_vars;
857 if ($option eq "local") {
858 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
859 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
860 } elsif ($option eq "client") {
861 SocketWrapper::set_default_iface(11);
862 write_clientconf($conffile, $clientdir, $testenv_vars);
863 $ENV{SMB_CONF_PATH} = $conffile;
864 } else {
865 die("Unknown option[$option] for envname[$envname]");
868 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
869 Samba::export_envvars($testenv_vars);
871 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
872 unlink($krb5_ccache_path);
873 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
874 return $testenv_vars;
877 sub getlog_env($)
879 my ($envname) = @_;
880 return "" if ($envname eq "none");
881 my $env = get_running_env($envname);
882 return $env->{target}->getlog_env($env);
885 sub check_env($)
887 my ($envname) = @_;
888 my $env = get_running_env($envname);
889 return $env->{target}->check_env($env);
892 sub teardown_env($)
894 my ($envname) = @_;
895 return if ($envname eq "none");
896 print STDERR "teardown_env($envname)\n";
897 my $env = get_running_env($envname);
898 $env->{target}->teardown_env($env);
899 delete $running_envs{$envname};
902 # This 'global' file needs to be empty when we start
903 unlink("$prefix_abs/dns_host_file");
904 unlink("$prefix_abs/hosts");
906 if ($opt_random_order) {
907 require List::Util;
908 my @newtodo = List::Util::shuffle(@todo);
909 @todo = @newtodo;
912 if ($opt_testenv) {
913 my $testenv_name = $ENV{SELFTEST_TESTENV};
914 $testenv_name = $testenv_default unless defined($testenv_name);
916 my $testenv_vars = setup_env($testenv_name, $prefix);
918 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
919 die("Unable to setup environment $testenv_name");
922 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
923 $ENV{ENVNAME} = $testenv_name;
925 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
927 my @term_args = ("echo -e \"
928 Welcome to the Samba4 Test environment '$testenv_name'
930 This matches the client environment used in make test
931 server is pid `cat \$PIDDIR/samba.pid`
933 Some useful environment variables:
934 TORTURE_OPTIONS=\$TORTURE_OPTIONS
935 SMB_CONF_PATH=\$SMB_CONF_PATH
937 $envvarstr
938 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
939 my @term = ();
940 if ($ENV{TERMINAL}) {
941 @term = ($ENV{TERMINAL});
942 # override the default terminal args (if specified)
943 if (defined($ENV{TERMINAL_ARGS})) {
944 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
946 } else {
947 @term = ("xterm", "-e");
948 unshift(@term_args, ("bash", "-c"));
951 system(@term, @term_args);
953 teardown_env($testenv_name);
954 } elsif ($opt_list) {
955 foreach (@todo) {
956 my $name = $$_[0];
957 my $envname = $$_[1];
958 my $cmd = $$_[2];
959 my $listcmd = $$_[3];
961 unless (defined($listcmd)) {
962 warn("Unable to list tests in $name");
963 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
964 # a single "test".
965 print "$name\n";
966 next;
969 system($listcmd);
971 if ($? == -1) {
972 die("Unable to run $listcmd: $!");
973 } elsif ($? & 127) {
974 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
977 my $exitcode = $? >> 8;
978 if ($exitcode != 0) {
979 die("$cmd exited with exit code $exitcode");
982 } else {
983 foreach (@todo) {
984 $i++;
985 my $cmd = $$_[2];
986 my $name = $$_[0];
987 my $envname = $$_[1];
988 my $envvars = setup_env($envname, $prefix);
990 if (not defined($envvars)) {
991 Subunit::start_testsuite($name);
992 Subunit::end_testsuite($name, "error",
993 "unable to set up environment $envname - exiting");
994 next;
995 } elsif ($envvars eq "UNKNOWN") {
996 Subunit::start_testsuite($name);
997 Subunit::end_testsuite($name, "error",
998 "environment $envname is unknown - exiting");
999 next;
1002 # Generate a file with the individual tests to run, if the
1003 # test runner for this test suite supports it.
1004 if ($individual_tests and $individual_tests->{$name}) {
1005 if ($$_[3]) {
1006 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1007 foreach my $test (@{$individual_tests->{$name}}) {
1008 print $fh substr($test, length($name)+1) . "\n";
1010 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1011 } else {
1012 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1016 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1018 teardown_env($envname) if ($opt_resetup_env);
1022 print "\n";
1024 teardown_env($_) foreach (keys %running_envs);
1026 my $failed = 0;
1028 # if there were any valgrind failures, show them
1029 foreach (<$prefix/valgrind.log*>) {
1030 next unless (-s $_);
1031 print "VALGRIND FAILURE\n";
1032 $failed++;
1033 system("cat $_");
1035 exit 0;