selftest: add "smb2.dirlease" test suite
[samba4-gss.git] / selftest / selftest.pl
bloba0c4ec80f59939753b11ff95de8583926f31913a
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 # After this many seconds, the server will self-terminate. All tests
459 # must terminate in this time, and testenv will only stay alive this
460 # long
462 my $server_maxtime;
463 if ($opt_testenv) {
464 # 1 year should be enough :-)
465 $server_maxtime = 365 * 24 * 60 * 60;
466 } else {
467 # make test should run under 5 hours
468 $server_maxtime = 5 * 60 * 60;
471 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
472 $server_maxtime = $ENV{SMBD_MAXTIME};
475 $target = new Samba($bindir, $srcdir, $server_maxtime,
476 $opt_socket_wrapper_pcap,
477 $opt_socket_wrapper_keep_pcap,
478 $opt_libpam_matrix_so_path,
479 $opt_default_ldb_backend);
480 unless ($opt_list) {
481 if ($opt_target eq "samba") {
482 $testenv_default = "ad_dc";
483 } elsif ($opt_target eq "samba3") {
484 $testenv_default = "nt4_member";
488 sub read_test_regexes($)
490 my ($name) = @_;
491 my @ret = ();
492 open(LF, "<$name") or die("unable to read $name: $!");
493 while (<LF>) {
494 chomp;
495 next if (/^#/);
496 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
497 push (@ret, [$1, $4]);
498 } else {
499 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
500 push (@ret, [$_, undef]);
503 close(LF);
504 return @ret;
507 foreach (@opt_exclude) {
508 push (@excludes, read_test_regexes($_));
511 foreach (@opt_include) {
512 push (@includes, read_test_regexes($_));
515 # We give the selftest client 6 different IPv4 addresses to use. Most tests
516 # only use the first (.11) IP. Note that winsreplication.c is one test that
517 # uses the other IPs (search for iface_list_count()).
518 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
519 my $interfaces = Samba::get_interfaces_config("client", 6);
521 my $clientdir = "$prefix_abs/client";
523 my $conffile = "$clientdir/client.conf";
524 $ENV{SMB_CONF_PATH} = $conffile;
526 sub write_clientconf($$$)
528 my ($conffile, $clientdir, $vars) = @_;
530 mkdir("$clientdir", 0777) unless -d "$clientdir";
532 my @subdirs = (
533 { name => "private", mask => 0777 },
534 { name => "bind-dns", mask => 0777 },
535 { name => "lockdir", mask => 0777 },
536 { name => "statedir", mask => 0777 },
537 { name => "cachedir", mask => 0777 },
538 { name => "pkinit", mask => 0700 },
539 { name => "pid", mask => 0777 },
540 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
541 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
544 foreach my $sub (@subdirs) {
545 my $dir = "$clientdir/$sub->{name}";
546 remove_tree($dir);
547 my $mask = umask;
548 if (defined($sub->{umask})) {
549 umask $sub->{umask};
551 mkdir($dir, $sub->{mask});
552 umask $mask;
555 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
556 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
557 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
558 my $ca_users_dir = "$cadir/Users";
559 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
561 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
562 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
563 # We make a copy here and make the certificated easily
564 # accessible in the client environment.
565 my $mask = umask;
566 umask 0077;
567 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
568 for my $d (readdir USERS) {
569 my $user_dir = "${ca_users_dir}/${d}";
570 next if ${d} =~ /^\./;
571 next if (! -d "${user_dir}");
572 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
573 for my $l (readdir USER) {
574 my $user_link = "${user_dir}/${l}";
575 next if ${l} =~ /^\./;
576 next if (! -l "${user_link}");
578 my $dest = "${clientdir}/pkinit/${l}";
579 Samba::copy_file_content(${user_link}, ${dest});
581 closedir USER;
583 closedir USERS;
584 umask $mask;
586 open(CF, ">$conffile");
587 print CF "[global]\n";
588 print CF "\tnetbios name = client\n";
589 if (defined($vars->{DOMAIN})) {
590 print CF "\tworkgroup = $vars->{DOMAIN}\n";
592 if (defined($vars->{REALM})) {
593 print CF "\trealm = $vars->{REALM}\n";
595 if ($opt_socket_wrapper) {
596 print CF "\tinterfaces = $interfaces\n";
598 print CF "
599 private dir = $clientdir/private
600 binddns dir = $clientdir/bind-dns
601 lock dir = $clientdir/lockdir
602 state directory = $clientdir/statedir
603 cache directory = $clientdir/cachedir
604 ncalrpc dir = $clientdir/ncalrpcdir
605 pid directory = $clientdir/pid
606 panic action = $RealBin/gdb_backtrace \%d
607 max xmit = 32K
608 notify:inotify = false
609 ldb:nosync = true
610 system:anonymous = true
611 client lanman auth = Yes
612 client min protocol = CORE
613 log level = $client_loglevel
614 torture:basedir = $clientdir
615 #We don't want to run 'speed' tests for very long
616 torture:timelimit = 1
617 winbind separator = /
618 tls cafile = ${cacert}
619 tls crlfile = ${cacrl_pem}
620 tls verify peer = no_check
621 include system krb5 conf = no
622 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
624 close(CF);
627 my @todo = ();
629 sub should_run_test($)
631 my $name = shift;
632 if ($#tests == -1) {
633 return 1;
635 for (my $i=0; $i <= $#tests; $i++) {
636 if ($name =~ /$tests[$i]/i) {
637 return 1;
640 return 0;
643 sub read_testlist($)
645 my ($filename) = @_;
647 my @ret = ();
648 open(IN, $filename) or die("Unable to open $filename: $!");
650 while (<IN>) {
651 if (/-- TEST(-LOADLIST|) --\n/) {
652 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
653 my $name = <IN>;
654 $name =~ s/\n//g;
655 my $env = <IN>;
656 $env =~ s/\n//g;
657 my $loadlist;
658 if ($supports_loadlist) {
659 $loadlist = <IN>;
660 $loadlist =~ s/\n//g;
662 my $cmdline = <IN>;
663 $cmdline =~ s/\n//g;
664 if (should_run_test($name) == 1) {
665 push (@ret, [$name, $env, $cmdline, $loadlist]);
667 } else {
668 print;
671 close(IN) or die("Error creating recipe from $filename");
672 return @ret;
675 if ($#testlists == -1) {
676 die("No testlists specified");
679 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
680 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
681 $ENV{TMPDIR} = "$tmpdir_abs";
682 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
683 if ($opt_quick) {
684 $ENV{SELFTEST_QUICK} = "1";
685 } else {
686 $ENV{SELFTEST_QUICK} = "";
688 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
690 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
691 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
693 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
694 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
696 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
697 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
699 my @available = ();
700 foreach my $fn (@testlists) {
701 foreach (read_testlist($fn)) {
702 my $name = $$_[0];
703 next if (@includes and not defined(find_in_list(\@includes, $name)));
704 push (@available, $_);
708 my $restricted = undef;
709 my $restricted_used = {};
711 if ($opt_load_list) {
712 $restricted = [];
713 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
714 while (<LOAD_LIST>) {
715 chomp;
716 push (@$restricted, $_);
718 close(LOAD_LIST);
721 my $individual_tests = undef;
722 $individual_tests = {};
724 foreach my $testsuite (@available) {
725 my $name = $$testsuite[0];
726 my $skipreason = skip(@$testsuite);
727 if (defined($restricted)) {
728 # Find the testsuite for this test
729 my $match = undef;
730 foreach my $r (@$restricted) {
731 if ($r eq $name) {
732 $individual_tests->{$name} = [];
733 $match = $r;
734 $restricted_used->{$r} = 1;
735 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
736 push(@{$individual_tests->{$name}}, $r);
737 $match = $r;
738 $restricted_used->{$r} = 1;
741 if ($match) {
742 if (defined($skipreason)) {
743 if (not $opt_list) {
744 Subunit::skip_testsuite($name, $skipreason);
746 } else {
747 push(@todo, $testsuite);
750 } elsif (defined($skipreason)) {
751 if (not $opt_list) {
752 Subunit::skip_testsuite($name, $skipreason);
754 } else {
755 push(@todo, $testsuite);
759 if (defined($restricted)) {
760 foreach (@$restricted) {
761 unless (defined($restricted_used->{$_})) {
762 print "No test or testsuite found matching $_\n";
765 } elsif ($#todo == -1) {
766 print STDERR "No tests to run\n";
767 exit(1);
770 my $suitestotal = $#todo + 1;
772 unless ($opt_list) {
773 Subunit::progress($suitestotal);
774 Subunit::report_time();
777 my $i = 0;
778 $| = 1;
780 my %running_envs = ();
782 sub get_running_env($)
784 my ($name) = @_;
786 my $envname = $name;
788 $envname =~ s/:.*//;
790 return $running_envs{$envname};
793 sub sighandler($)
795 my $signame = shift;
797 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
798 $SIG{PIPE} = 'IGNORE';
800 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
802 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
803 teardown_env($_) foreach(keys %running_envs);
804 system("pstree -p $$");
805 print "$0: PID[$$]: Exiting...\n";
806 exit(1);
809 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
811 sub setup_env($$)
813 my ($name, $prefix) = @_;
815 my $testenv_vars = undef;
817 my $envname = $name;
818 my $option = $name;
820 $envname =~ s/:.*//;
821 $option =~ s/^[^:]*//;
822 $option =~ s/^://;
824 $option = "client" if $option eq "";
826 # Initially clear out the environment for the provision, so previous envs'
827 # variables don't leak in. Provisioning steps must explicitly set their
828 # necessary variables when calling out to other executables
829 Samba::clear_exported_envvars();
830 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
831 delete $ENV{SMB_CONF_PATH};
833 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
834 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
835 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
837 if (defined(get_running_env($envname))) {
838 $testenv_vars = get_running_env($envname);
839 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
840 print $testenv_vars->{target}->getlog_env($testenv_vars);
841 $testenv_vars = undef;
843 } else {
844 $testenv_vars = $target->setup_env($envname, $prefix);
845 if (not defined($testenv_vars)) {
846 my $msg = "$opt_target can't start up known environment '$envname'";
847 if ($opt_one) {
848 die($msg);
850 warn $msg;
851 return;
853 if (ref $testenv_vars ne "HASH") {
854 return $testenv_vars;
856 if (defined($testenv_vars->{target})) {
857 $testenv_vars->{target} = $target;
861 return undef unless defined($testenv_vars);
863 $running_envs{$envname} = $testenv_vars;
865 if ($option eq "local") {
866 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
867 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
868 } elsif ($option eq "client") {
869 SocketWrapper::set_default_iface(11);
870 write_clientconf($conffile, $clientdir, $testenv_vars);
871 $ENV{SMB_CONF_PATH} = $conffile;
872 } else {
873 die("Unknown option[$option] for envname[$envname]");
876 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
877 Samba::export_envvars($testenv_vars);
879 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
880 unlink($krb5_ccache_path);
881 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
882 return $testenv_vars;
885 sub getlog_env($)
887 my ($envname) = @_;
888 return "" if ($envname eq "none");
889 my $env = get_running_env($envname);
890 return $env->{target}->getlog_env($env);
893 sub check_env($)
895 my ($envname) = @_;
896 my $env = get_running_env($envname);
897 return $env->{target}->check_env($env);
900 sub teardown_env($)
902 my ($envname) = @_;
903 return if ($envname eq "none");
904 print STDERR "teardown_env($envname)\n";
905 my $env = get_running_env($envname);
906 $env->{target}->teardown_env($env);
907 delete $running_envs{$envname};
910 # This 'global' file needs to be empty when we start
911 unlink("$prefix_abs/dns_host_file");
912 unlink("$prefix_abs/hosts");
914 if ($opt_random_order) {
915 require List::Util;
916 my @newtodo = List::Util::shuffle(@todo);
917 @todo = @newtodo;
920 if ($opt_testenv) {
921 my $testenv_name = $ENV{SELFTEST_TESTENV};
922 $testenv_name = $testenv_default unless defined($testenv_name);
924 my $testenv_vars = setup_env($testenv_name, $prefix);
926 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
927 die("Unable to setup environment $testenv_name");
930 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
931 $ENV{ENVNAME} = $testenv_name;
933 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
935 my @term_args = ("echo -e \"
936 Welcome to the Samba4 Test environment '$testenv_name'
938 This matches the client environment used in make test
939 server is pid `cat \$PIDDIR/samba.pid`
941 Some useful environment variables:
942 TORTURE_OPTIONS=\$TORTURE_OPTIONS
943 SMB_CONF_PATH=\$SMB_CONF_PATH
945 $envvarstr
946 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
947 my @term = ();
948 if ($ENV{TERMINAL}) {
949 @term = ($ENV{TERMINAL});
950 # override the default terminal args (if specified)
951 if (defined($ENV{TERMINAL_ARGS})) {
952 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
954 } else {
955 @term = ("xterm", "-e");
956 unshift(@term_args, ("bash", "-c"));
959 system(@term, @term_args);
961 teardown_env($testenv_name);
962 } elsif ($opt_list) {
963 foreach (@todo) {
964 my $name = $$_[0];
965 my $envname = $$_[1];
966 my $cmd = $$_[2];
967 my $listcmd = $$_[3];
969 unless (defined($listcmd)) {
970 warn("Unable to list tests in $name");
971 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
972 # a single "test".
973 print "$name\n";
974 next;
977 system($listcmd);
979 if ($? == -1) {
980 die("Unable to run $listcmd: $!");
981 } elsif ($? & 127) {
982 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
985 my $exitcode = $? >> 8;
986 if ($exitcode != 0) {
987 die("$cmd exited with exit code $exitcode");
990 } else {
991 foreach (@todo) {
992 $i++;
993 my $cmd = $$_[2];
994 my $name = $$_[0];
995 my $envname = $$_[1];
996 my $envvars = setup_env($envname, $prefix);
998 if (not defined($envvars)) {
999 Subunit::start_testsuite($name);
1000 Subunit::end_testsuite($name, "error",
1001 "unable to set up environment $envname - exiting");
1002 next;
1003 } elsif ($envvars eq "UNKNOWN") {
1004 Subunit::start_testsuite($name);
1005 Subunit::end_testsuite($name, "error",
1006 "environment $envname is unknown - exiting");
1007 next;
1010 # Generate a file with the individual tests to run, if the
1011 # test runner for this test suite supports it.
1012 if ($individual_tests and $individual_tests->{$name}) {
1013 if ($$_[3]) {
1014 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1015 foreach my $test (@{$individual_tests->{$name}}) {
1016 print $fh substr($test, length($name)+1) . "\n";
1018 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1019 } else {
1020 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1024 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1026 teardown_env($envname) if ($opt_resetup_env);
1030 print "\n";
1032 teardown_env($_) foreach (keys %running_envs);
1034 my $failed = 0;
1036 # if there were any valgrind failures, show them
1037 foreach (<$prefix/valgrind.log*>) {
1038 next unless (-s $_);
1039 print "VALGRIND FAILURE\n";
1040 $failed++;
1041 system("cat $_");
1043 exit 0;