Add ROLE_IPA_DC into two more places
[Samba.git] / selftest / selftest.pl
blob99ec813af7c8746593eda115ecd43032811b3a0c
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";
289 my $bindir_abs = abs_path($bindir);
291 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
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 umask 0077;
306 mkdir($prefix, 0700) unless -d $prefix;
307 chmod 0700, $prefix;
308 # We need to have no umask limitations for the tests.
309 umask 0000;
311 my $prefix_abs = abs_path($prefix);
312 my $tmpdir_abs = abs_path("$prefix/tmp");
313 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
315 my $srcdir_abs = abs_path($srcdir);
317 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
318 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
320 $ENV{SAMBA_SELFTEST} = "1";
322 $ENV{PREFIX} = $prefix;
323 $ENV{PREFIX_ABS} = $prefix_abs;
324 $ENV{SRCDIR} = $srcdir;
325 $ENV{SRCDIR_ABS} = $srcdir_abs;
326 $ENV{BINDIR} = $bindir_abs;
328 my $tls_enabled = not $opt_quick;
329 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
331 sub prefix_pathvar($$)
333 my ($name, $newpath) = @_;
334 if (defined($ENV{$name})) {
335 $ENV{$name} = "$newpath:$ENV{$name}";
336 } else {
337 $ENV{$name} = $newpath;
340 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
341 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
343 if ($opt_socket_wrapper_keep_pcap) {
344 # Socket wrapper keep pcap implies socket wrapper pcap
345 $opt_socket_wrapper_pcap = 1;
348 if ($opt_socket_wrapper_pcap) {
349 # Socket wrapper pcap implies socket wrapper
350 $opt_socket_wrapper = 1;
353 my $ld_preload = $ENV{LD_PRELOAD};
355 if ($opt_libasan_so_path) {
356 if ($ld_preload) {
357 if ($opt_libcrypt_so_path) {
358 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path:$ld_preload";
359 } else {
360 $ld_preload = "$opt_libasan_so_path:$ld_preload";
362 } else {
363 if ($opt_libcrypt_so_path) {
364 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path";
365 } else {
366 $ld_preload = "$opt_libasan_so_path";
371 if ($opt_libnss_wrapper_so_path) {
372 if ($ld_preload) {
373 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
374 } else {
375 $ld_preload = "$opt_libnss_wrapper_so_path";
379 if ($opt_libresolv_wrapper_so_path) {
380 if ($ld_preload) {
381 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
382 } else {
383 $ld_preload = "$opt_libresolv_wrapper_so_path";
387 if ($opt_libsocket_wrapper_so_path) {
388 if ($ld_preload) {
389 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
390 } else {
391 $ld_preload = "$opt_libsocket_wrapper_so_path";
395 if ($opt_libuid_wrapper_so_path) {
396 if ($ld_preload) {
397 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
398 } else {
399 $ld_preload = "$opt_libuid_wrapper_so_path";
403 if (defined($ENV{USE_NAMESPACES})) {
404 print "Using linux containerization for selftest testenv(s)...\n";
406 # Create a common bridge to connect up the testenv namespaces. We give
407 # it the client's IP address, as this is where the tests will run from
408 my $ipv4_addr = Samba::get_ipv4_addr("client");
409 my $ipv6_addr = Samba::get_ipv6_addr("client");
410 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
413 $ENV{LD_PRELOAD} = $ld_preload;
414 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
416 # Enable uid_wrapper globally
417 $ENV{UID_WRAPPER} = 1;
419 # We are already hitting the limit, so double it.
420 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
422 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
424 # This is needed in order to allow the ldb_*ldap module
425 # to work with a preloaded socket wrapper.
426 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
428 my $socket_wrapper_dir;
429 if ($opt_socket_wrapper) {
430 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
431 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
432 } elsif (not $opt_list) {
433 unless ($< == 0) {
434 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
438 if ($opt_use_dns_faking) {
439 print "DNS: Faking nameserver\n";
440 $ENV{SAMBA_DNS_FAKING} = 1;
443 my $testenv_default = "none";
445 if ($opt_mitkrb5 == 1) {
446 $ENV{MITKRB5} = $opt_mitkrb5;
447 $ENV{KRB5RCACHETYPE} = "none";
450 # After this many seconds, the server will self-terminate. All tests
451 # must terminate in this time, and testenv will only stay alive this
452 # long
454 my $server_maxtime;
455 if ($opt_testenv) {
456 # 1 year should be enough :-)
457 $server_maxtime = 365 * 24 * 60 * 60;
458 } else {
459 # make test should run under 5 hours
460 $server_maxtime = 5 * 60 * 60;
463 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
464 $server_maxtime = $ENV{SMBD_MAXTIME};
467 $target = new Samba($bindir, $srcdir, $server_maxtime,
468 $opt_socket_wrapper_pcap,
469 $opt_socket_wrapper_keep_pcap,
470 $opt_default_ldb_backend);
471 unless ($opt_list) {
472 if ($opt_target eq "samba") {
473 $testenv_default = "ad_dc";
474 } elsif ($opt_target eq "samba3") {
475 $testenv_default = "nt4_member";
479 sub read_test_regexes($)
481 my ($name) = @_;
482 my @ret = ();
483 open(LF, "<$name") or die("unable to read $name: $!");
484 while (<LF>) {
485 chomp;
486 next if (/^#/);
487 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
488 push (@ret, [$1, $4]);
489 } else {
490 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
491 push (@ret, [$_, undef]);
494 close(LF);
495 return @ret;
498 foreach (@opt_exclude) {
499 push (@excludes, read_test_regexes($_));
502 foreach (@opt_include) {
503 push (@includes, read_test_regexes($_));
506 # We give the selftest client 6 different IPv4 addresses to use. Most tests
507 # only use the first (.11) IP. Note that winsreplication.c is one test that
508 # uses the other IPs (search for iface_list_count()).
509 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
510 my $interfaces = Samba::get_interfaces_config("client", 6);
512 my $clientdir = "$prefix_abs/client";
514 my $conffile = "$clientdir/client.conf";
515 $ENV{SMB_CONF_PATH} = $conffile;
517 sub write_clientconf($$$)
519 my ($conffile, $clientdir, $vars) = @_;
521 mkdir("$clientdir", 0777) unless -d "$clientdir";
523 my @subdirs = (
524 { name => "private", mask => 0777 },
525 { name => "bind-dns", mask => 0777 },
526 { name => "lockdir", mask => 0777 },
527 { name => "statedir", mask => 0777 },
528 { name => "cachedir", mask => 0777 },
529 { name => "pkinit", mask => 0700 },
530 { name => "pid", mask => 0777 },
531 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
532 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
535 foreach my $sub (@subdirs) {
536 my $dir = "$clientdir/$sub->{name}";
537 remove_tree($dir);
538 my $mask = umask;
539 if (defined($sub->{umask})) {
540 umask $sub->{umask};
542 mkdir($dir, $sub->{mask});
543 umask $mask;
546 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
547 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
548 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
549 my $ca_users_dir = "$cadir/Users";
550 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
552 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
553 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
554 # We make a copy here and make the certificated easily
555 # accessable in the client environment.
556 my $mask = umask;
557 umask 0077;
558 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
559 for my $d (readdir USERS) {
560 my $user_dir = "${ca_users_dir}/${d}";
561 next if ${d} =~ /^\./;
562 next if (! -d "${user_dir}");
563 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
564 for my $l (readdir USER) {
565 my $user_link = "${user_dir}/${l}";
566 next if ${l} =~ /^\./;
567 next if (! -l "${user_link}");
569 my $dest = "${clientdir}/pkinit/${l}";
570 Samba::copy_file_content(${user_link}, ${dest});
572 closedir USER;
574 closedir USERS;
575 umask $mask;
577 open(CF, ">$conffile");
578 print CF "[global]\n";
579 print CF "\tnetbios name = client\n";
580 if (defined($vars->{DOMAIN})) {
581 print CF "\tworkgroup = $vars->{DOMAIN}\n";
583 if (defined($vars->{REALM})) {
584 print CF "\trealm = $vars->{REALM}\n";
586 if ($opt_socket_wrapper) {
587 print CF "\tinterfaces = $interfaces\n";
589 print CF "
590 private dir = $clientdir/private
591 binddns dir = $clientdir/bind-dns
592 lock dir = $clientdir/lockdir
593 state directory = $clientdir/statedir
594 cache directory = $clientdir/cachedir
595 ncalrpc dir = $clientdir/ncalrpcdir
596 pid directory = $clientdir/pid
597 panic action = $RealBin/gdb_backtrace \%d
598 max xmit = 32K
599 notify:inotify = false
600 ldb:nosync = true
601 system:anonymous = true
602 client lanman auth = Yes
603 client min protocol = CORE
604 log level = $client_loglevel
605 torture:basedir = $clientdir
606 #We don't want to run 'speed' tests for very long
607 torture:timelimit = 1
608 winbind separator = /
609 tls cafile = ${cacert}
610 tls crlfile = ${cacrl_pem}
611 tls verify peer = no_check
612 include system krb5 conf = no
613 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
615 close(CF);
618 my @todo = ();
620 sub should_run_test($)
622 my $name = shift;
623 if ($#tests == -1) {
624 return 1;
626 for (my $i=0; $i <= $#tests; $i++) {
627 if ($name =~ /$tests[$i]/i) {
628 return 1;
631 return 0;
634 sub read_testlist($)
636 my ($filename) = @_;
638 my @ret = ();
639 open(IN, $filename) or die("Unable to open $filename: $!");
641 while (<IN>) {
642 if (/-- TEST(-LOADLIST|) --\n/) {
643 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
644 my $name = <IN>;
645 $name =~ s/\n//g;
646 my $env = <IN>;
647 $env =~ s/\n//g;
648 my $loadlist;
649 if ($supports_loadlist) {
650 $loadlist = <IN>;
651 $loadlist =~ s/\n//g;
653 my $cmdline = <IN>;
654 $cmdline =~ s/\n//g;
655 if (should_run_test($name) == 1) {
656 push (@ret, [$name, $env, $cmdline, $loadlist]);
658 } else {
659 print;
662 close(IN) or die("Error creating recipe from $filename");
663 return @ret;
666 if ($#testlists == -1) {
667 die("No testlists specified");
670 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
671 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
672 $ENV{TMPDIR} = "$tmpdir_abs";
673 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
674 if ($opt_quick) {
675 $ENV{SELFTEST_QUICK} = "1";
676 } else {
677 $ENV{SELFTEST_QUICK} = "";
679 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
681 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
682 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
684 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
685 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
687 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
688 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
690 my @available = ();
691 foreach my $fn (@testlists) {
692 foreach (read_testlist($fn)) {
693 my $name = $$_[0];
694 next if (@includes and not defined(find_in_list(\@includes, $name)));
695 push (@available, $_);
699 my $restricted = undef;
700 my $restricted_used = {};
702 if ($opt_load_list) {
703 $restricted = [];
704 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
705 while (<LOAD_LIST>) {
706 chomp;
707 push (@$restricted, $_);
709 close(LOAD_LIST);
712 my $individual_tests = undef;
713 $individual_tests = {};
715 foreach my $testsuite (@available) {
716 my $name = $$testsuite[0];
717 my $skipreason = skip(@$testsuite);
718 if (defined($restricted)) {
719 # Find the testsuite for this test
720 my $match = undef;
721 foreach my $r (@$restricted) {
722 if ($r eq $name) {
723 $individual_tests->{$name} = [];
724 $match = $r;
725 $restricted_used->{$r} = 1;
726 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
727 push(@{$individual_tests->{$name}}, $r);
728 $match = $r;
729 $restricted_used->{$r} = 1;
732 if ($match) {
733 if (defined($skipreason)) {
734 if (not $opt_list) {
735 Subunit::skip_testsuite($name, $skipreason);
737 } else {
738 push(@todo, $testsuite);
741 } elsif (defined($skipreason)) {
742 if (not $opt_list) {
743 Subunit::skip_testsuite($name, $skipreason);
745 } else {
746 push(@todo, $testsuite);
750 if (defined($restricted)) {
751 foreach (@$restricted) {
752 unless (defined($restricted_used->{$_})) {
753 print "No test or testsuite found matching $_\n";
756 } elsif ($#todo == -1) {
757 print STDERR "No tests to run\n";
758 exit(1);
761 my $suitestotal = $#todo + 1;
763 unless ($opt_list) {
764 Subunit::progress($suitestotal);
765 Subunit::report_time();
768 my $i = 0;
769 $| = 1;
771 my %running_envs = ();
773 sub get_running_env($)
775 my ($name) = @_;
777 my $envname = $name;
779 $envname =~ s/:.*//;
781 return $running_envs{$envname};
784 sub sighandler($)
786 my $signame = shift;
788 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
789 $SIG{PIPE} = 'IGNORE';
791 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
793 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
794 teardown_env($_) foreach(keys %running_envs);
795 system("pstree -p $$");
796 print "$0: PID[$$]: Exiting...\n";
797 exit(1);
800 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
802 sub setup_env($$)
804 my ($name, $prefix) = @_;
806 my $testenv_vars = undef;
808 my $envname = $name;
809 my $option = $name;
811 $envname =~ s/:.*//;
812 $option =~ s/^[^:]*//;
813 $option =~ s/^://;
815 $option = "client" if $option eq "";
817 # Initially clear out the environment for the provision, so previous envs'
818 # variables don't leak in. Provisioning steps must explicitly set their
819 # necessary variables when calling out to other executables
820 Samba::clear_exported_envvars();
821 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
822 delete $ENV{SMB_CONF_PATH};
824 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
825 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
826 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
828 if (defined(get_running_env($envname))) {
829 $testenv_vars = get_running_env($envname);
830 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
831 print $testenv_vars->{target}->getlog_env($testenv_vars);
832 $testenv_vars = undef;
834 } else {
835 $testenv_vars = $target->setup_env($envname, $prefix);
836 if (not defined($testenv_vars)) {
837 my $msg = "$opt_target can't start up known environment '$envname'";
838 if ($opt_one) {
839 die($msg);
841 warn $msg;
842 return;
844 if (ref $testenv_vars ne "HASH") {
845 return $testenv_vars;
847 if (defined($testenv_vars->{target})) {
848 $testenv_vars->{target} = $target;
852 return undef unless defined($testenv_vars);
854 $running_envs{$envname} = $testenv_vars;
856 if ($option eq "local") {
857 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
858 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
859 } elsif ($option eq "client") {
860 SocketWrapper::set_default_iface(11);
861 write_clientconf($conffile, $clientdir, $testenv_vars);
862 $ENV{SMB_CONF_PATH} = $conffile;
863 } else {
864 die("Unknown option[$option] for envname[$envname]");
867 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
868 Samba::export_envvars($testenv_vars);
870 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
871 unlink($krb5_ccache_path);
872 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
873 return $testenv_vars;
876 sub getlog_env($)
878 my ($envname) = @_;
879 return "" if ($envname eq "none");
880 my $env = get_running_env($envname);
881 return $env->{target}->getlog_env($env);
884 sub check_env($)
886 my ($envname) = @_;
887 my $env = get_running_env($envname);
888 return $env->{target}->check_env($env);
891 sub teardown_env($)
893 my ($envname) = @_;
894 return if ($envname eq "none");
895 print STDERR "teardown_env($envname)\n";
896 my $env = get_running_env($envname);
897 $env->{target}->teardown_env($env);
898 delete $running_envs{$envname};
901 # This 'global' file needs to be empty when we start
902 unlink("$prefix_abs/dns_host_file");
903 unlink("$prefix_abs/hosts");
905 if ($opt_random_order) {
906 require List::Util;
907 my @newtodo = List::Util::shuffle(@todo);
908 @todo = @newtodo;
911 if ($opt_testenv) {
912 my $testenv_name = $ENV{SELFTEST_TESTENV};
913 $testenv_name = $testenv_default unless defined($testenv_name);
915 my $testenv_vars = setup_env($testenv_name, $prefix);
917 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
918 die("Unable to setup environment $testenv_name");
921 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
922 $ENV{ENVNAME} = $testenv_name;
924 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
926 my @term_args = ("echo -e \"
927 Welcome to the Samba4 Test environment '$testenv_name'
929 This matches the client environment used in make test
930 server is pid `cat \$PIDDIR/samba.pid`
932 Some useful environment variables:
933 TORTURE_OPTIONS=\$TORTURE_OPTIONS
934 SMB_CONF_PATH=\$SMB_CONF_PATH
936 $envvarstr
937 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
938 my @term = ();
939 if ($ENV{TERMINAL}) {
940 @term = ($ENV{TERMINAL});
941 # override the default terminal args (if specified)
942 if (defined($ENV{TERMINAL_ARGS})) {
943 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
945 } else {
946 @term = ("xterm", "-e");
947 unshift(@term_args, ("bash", "-c"));
950 system(@term, @term_args);
952 teardown_env($testenv_name);
953 } elsif ($opt_list) {
954 foreach (@todo) {
955 my $name = $$_[0];
956 my $envname = $$_[1];
957 my $cmd = $$_[2];
958 my $listcmd = $$_[3];
960 unless (defined($listcmd)) {
961 warn("Unable to list tests in $name");
962 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
963 # a single "test".
964 print "$name\n";
965 next;
968 system($listcmd);
970 if ($? == -1) {
971 die("Unable to run $listcmd: $!");
972 } elsif ($? & 127) {
973 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
976 my $exitcode = $? >> 8;
977 if ($exitcode != 0) {
978 die("$cmd exited with exit code $exitcode");
981 } else {
982 foreach (@todo) {
983 $i++;
984 my $cmd = $$_[2];
985 my $name = $$_[0];
986 my $envname = $$_[1];
987 my $envvars = setup_env($envname, $prefix);
989 if (not defined($envvars)) {
990 Subunit::start_testsuite($name);
991 Subunit::end_testsuite($name, "error",
992 "unable to set up environment $envname - exiting");
993 next;
994 } elsif ($envvars eq "UNKNOWN") {
995 Subunit::start_testsuite($name);
996 Subunit::end_testsuite($name, "error",
997 "environment $envname is unknown - exiting");
998 next;
1001 # Generate a file with the individual tests to run, if the
1002 # test runner for this test suite supports it.
1003 if ($individual_tests and $individual_tests->{$name}) {
1004 if ($$_[3]) {
1005 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1006 foreach my $test (@{$individual_tests->{$name}}) {
1007 print $fh substr($test, length($name)+1) . "\n";
1009 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1010 } else {
1011 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1015 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1017 teardown_env($envname) if ($opt_resetup_env);
1021 print "\n";
1023 teardown_env($_) foreach (keys %running_envs);
1025 my $failed = 0;
1027 # if there were any valgrind failures, show them
1028 foreach (<$prefix/valgrind.log*>) {
1029 next unless (-s $_);
1030 print "VALGRIND FAILURE\n";
1031 $failed++;
1032 system("cat $_");
1034 exit 0;