samba-tool: Test gpo manage symlink add command
[Samba.git] / selftest / selftest.pl
blob6ea21fa6bfeab3fdcce5edc7e46ced371fc8ed5f
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_resetup_env = undef;
59 my $opt_load_list = undef;
60 my $opt_libnss_wrapper_so_path = "";
61 my $opt_libresolv_wrapper_so_path = "";
62 my $opt_libsocket_wrapper_so_path = "";
63 my $opt_libuid_wrapper_so_path = "";
64 my $opt_libasan_so_path = "";
65 my $opt_use_dns_faking = 0;
66 my @testlists = ();
68 my $srcdir = ".";
69 my $bindir = "./bin";
70 my $prefix = "./st";
72 my @includes = ();
73 my @excludes = ();
75 sub find_in_list($$)
77 my ($list, $fullname) = @_;
79 foreach (@$list) {
80 if ($fullname =~ /$$_[0]/) {
81 return ($$_[1]) if ($$_[1]);
82 return "";
86 return undef;
89 sub skip
91 my ($name, $envname) = @_;
92 my ($env_basename, $env_localpart) = split(/:/, $envname);
94 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
95 return "environment $envname is disabled as this build does not include an AD DC";
98 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
99 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
100 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
101 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
104 return find_in_list(\@excludes, $name);
107 sub getlog_env($);
109 # expand strings from %ENV
110 sub expand_environment_strings($)
112 my $s = shift;
113 # we use a reverse sort so we do the longer ones first
114 foreach my $k (sort { $b cmp $a } keys %ENV) {
115 $s =~ s/\$$k/$ENV{$k}/g;
117 return $s;
120 my $target;
122 sub run_testsuite($$$$$)
124 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
125 my $pcap_file = $target->setup_pcap($name);
127 Subunit::start_testsuite($name);
128 Subunit::progress_push();
129 Subunit::report_time();
130 system($cmd);
131 Subunit::report_time();
132 Subunit::progress_pop();
134 if ($? == -1) {
135 print "command: $cmd\n";
136 printf "expanded command: %s\n", expand_environment_strings($cmd);
137 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
138 exit(1);
139 } elsif ($? & 127) {
140 print "command: $cmd\n";
141 printf "expanded command: %s\n", expand_environment_strings($cmd);
142 Subunit::end_testsuite($name, "error",
143 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
144 exit(1);
147 my $exitcode = $? >> 8;
149 my $envlog = getlog_env($envname);
150 if ($envlog ne "") {
151 print "envlog: $envlog\n";
154 print "command: $cmd\n";
155 printf "expanded command: %s\n", expand_environment_strings($cmd);
157 if ($exitcode == 0) {
158 Subunit::end_testsuite($name, "success");
159 } else {
160 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
163 $target->cleanup_pcap($pcap_file, $exitcode);
165 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
166 print "PCAP FILE: $pcap_file\n";
169 if ($exitcode != 0) {
170 exit(1) if ($opt_one);
173 return $exitcode;
176 sub ShowHelp()
178 print "Samba test runner
179 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
180 Copyright (C) Stefan Metzmacher <metze\@samba.org>
182 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
184 Generic options:
185 --help this help page
186 --target=samba[3]|win Samba version to target
187 --testlist=FILE file to read available tests from
188 --exclude=FILE Exclude tests listed in the file
189 --include=FILE Include tests listed in the file
190 --exclude-env=ENV Exclude tests for the specified environment
191 --include-env=ENV Include tests for the specified environment
193 Paths:
194 --prefix=DIR prefix to run tests in [st]
195 --srcdir=DIR source directory [.]
196 --bindir=DIR binaries directory [./bin]
198 Preload cwrap:
199 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
200 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
201 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
202 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
203 --asan_so_path=FILE the asan library to preload
205 DNS:
206 --use-dns-faking Fake DNS entries rather than talking to our
207 DNS implementation.
209 Target Specific:
210 --socket-wrapper-pcap save traffic to pcap directories
211 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
212 failed
213 --socket-wrapper enable socket wrapper
215 Behaviour:
216 --quick run quick overall test
217 --one abort when the first test fails
218 --testenv run a shell in the requested test environment
219 --list list available tests
221 exit(0);
224 my $result = GetOptions (
225 'help|h|?' => \$opt_help,
226 'target=s' => \$opt_target,
227 'prefix=s' => \$prefix,
228 'socket-wrapper' => \$opt_socket_wrapper,
229 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
230 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
231 'quick' => \$opt_quick,
232 'one' => \$opt_one,
233 'exclude=s' => \@opt_exclude,
234 'include=s' => \@opt_include,
235 'exclude-env=s' => \@opt_exclude_env,
236 'include-env=s' => \@opt_include_env,
237 'srcdir=s' => \$srcdir,
238 'bindir=s' => \$bindir,
239 'testenv' => \$opt_testenv,
240 'list' => \$opt_list,
241 'mitkrb5' => \$opt_mitkrb5,
242 'resetup-environment' => \$opt_resetup_env,
243 'testlist=s' => \@testlists,
244 'random-order' => \$opt_random_order,
245 'load-list=s' => \$opt_load_list,
246 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
247 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
248 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
249 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
250 'asan_so_path=s' => \$opt_libasan_so_path,
251 'use-dns-faking' => \$opt_use_dns_faking
254 exit(1) if (not $result);
256 ShowHelp() if ($opt_help);
258 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
260 # we want unbuffered output
261 $| = 1;
263 my @tests = @ARGV;
265 # quick hack to disable rpc validation when using valgrind - its way too slow
266 unless (defined($ENV{VALGRIND})) {
267 $ENV{VALIDATE} = "validate";
268 $ENV{MALLOC_CHECK_} = 3;
271 # make all our python scripts unbuffered
272 $ENV{PYTHONUNBUFFERED} = 1;
274 # do not depend on the users setup
275 # see also bootstrap/config.py
276 $ENV{TZ} = "UTC";
277 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
279 my $bindir_abs = abs_path($bindir);
281 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
283 $prefix =~ s+//+/+;
284 $prefix =~ s+/./+/+;
285 $prefix =~ s+/$++;
287 die("using an empty prefix isn't allowed") unless $prefix ne "";
289 # Ensure we have the test prefix around.
291 # We need restrictive
292 # permissions on this as some subdirectories in this tree will have
293 # wider permissions (ie 0777) and this would allow other users on the
294 # host to subvert the test process.
295 umask 0077;
296 mkdir($prefix, 0700) unless -d $prefix;
297 chmod 0700, $prefix;
298 # We need to have no umask limitations for the tests.
299 umask 0000;
301 my $prefix_abs = abs_path($prefix);
302 my $tmpdir_abs = abs_path("$prefix/tmp");
303 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
305 my $srcdir_abs = abs_path($srcdir);
307 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
308 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
310 $ENV{SAMBA_SELFTEST} = "1";
312 $ENV{PREFIX} = $prefix;
313 $ENV{PREFIX_ABS} = $prefix_abs;
314 $ENV{SRCDIR} = $srcdir;
315 $ENV{SRCDIR_ABS} = $srcdir_abs;
316 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
317 $ENV{BINDIR} = $bindir_abs;
319 my $tls_enabled = not $opt_quick;
320 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
322 sub prefix_pathvar($$)
324 my ($name, $newpath) = @_;
325 if (defined($ENV{$name})) {
326 $ENV{$name} = "$newpath:$ENV{$name}";
327 } else {
328 $ENV{$name} = $newpath;
331 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
332 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
334 if ($opt_socket_wrapper_keep_pcap) {
335 # Socket wrapper keep pcap implies socket wrapper pcap
336 $opt_socket_wrapper_pcap = 1;
339 if ($opt_socket_wrapper_pcap) {
340 # Socket wrapper pcap implies socket wrapper
341 $opt_socket_wrapper = 1;
344 my $ld_preload = $ENV{LD_PRELOAD};
346 if ($opt_libasan_so_path) {
347 if ($ld_preload) {
348 $ld_preload = "$ld_preload:$opt_libasan_so_path";
349 } else {
350 $ld_preload = "$opt_libasan_so_path";
354 if ($opt_libnss_wrapper_so_path) {
355 if ($ld_preload) {
356 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
357 } else {
358 $ld_preload = "$opt_libnss_wrapper_so_path";
362 if ($opt_libresolv_wrapper_so_path) {
363 if ($ld_preload) {
364 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
365 } else {
366 $ld_preload = "$opt_libresolv_wrapper_so_path";
370 if ($opt_libsocket_wrapper_so_path) {
371 if ($ld_preload) {
372 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
373 } else {
374 $ld_preload = "$opt_libsocket_wrapper_so_path";
378 if ($opt_libuid_wrapper_so_path) {
379 if ($ld_preload) {
380 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
381 } else {
382 $ld_preload = "$opt_libuid_wrapper_so_path";
386 if (defined($ENV{USE_NAMESPACES})) {
387 print "Using linux containerization for selftest testenv(s)...\n";
389 # Create a common bridge to connect up the testenv namespaces. We give
390 # it the client's IP address, as this is where the tests will run from
391 my $ipv4_addr = Samba::get_ipv4_addr("client");
392 my $ipv6_addr = Samba::get_ipv6_addr("client");
393 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
396 $ENV{LD_PRELOAD} = $ld_preload;
397 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
399 # Enable uid_wrapper globally
400 $ENV{UID_WRAPPER} = 1;
402 # We are already hitting the limit, so double it.
403 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
405 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
407 # This is needed in order to allow the ldb_*ldap module
408 # to work with a preloaded socket wrapper.
409 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
411 my $socket_wrapper_dir;
412 if ($opt_socket_wrapper) {
413 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
414 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
415 } elsif (not $opt_list) {
416 unless ($< == 0) {
417 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
421 if ($opt_use_dns_faking) {
422 print "DNS: Faking nameserver\n";
423 $ENV{SAMBA_DNS_FAKING} = 1;
426 my $testenv_default = "none";
428 if ($opt_mitkrb5 == 1) {
429 $ENV{MITKRB5} = $opt_mitkrb5;
430 $ENV{KRB5RCACHETYPE} = "none";
433 # After this many seconds, the server will self-terminate. All tests
434 # must terminate in this time, and testenv will only stay alive this
435 # long
437 my $server_maxtime;
438 if ($opt_testenv) {
439 # 1 year should be enough :-)
440 $server_maxtime = 365 * 24 * 60 * 60;
441 } else {
442 # make test should run under 5 hours
443 $server_maxtime = 5 * 60 * 60;
446 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
447 $server_maxtime = $ENV{SMBD_MAXTIME};
450 $target = new Samba($bindir, $srcdir, $server_maxtime,
451 $opt_socket_wrapper_pcap,
452 $opt_socket_wrapper_keep_pcap);
453 unless ($opt_list) {
454 if ($opt_target eq "samba") {
455 $testenv_default = "ad_dc";
456 } elsif ($opt_target eq "samba3") {
457 $testenv_default = "nt4_member";
461 sub read_test_regexes($)
463 my ($name) = @_;
464 my @ret = ();
465 open(LF, "<$name") or die("unable to read $name: $!");
466 while (<LF>) {
467 chomp;
468 next if (/^#/);
469 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
470 push (@ret, [$1, $4]);
471 } else {
472 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
473 push (@ret, [$_, undef]);
476 close(LF);
477 return @ret;
480 foreach (@opt_exclude) {
481 push (@excludes, read_test_regexes($_));
484 foreach (@opt_include) {
485 push (@includes, read_test_regexes($_));
488 # We give the selftest client 6 different IPv4 addresses to use. Most tests
489 # only use the first (.11) IP. Note that winsreplication.c is one test that
490 # uses the other IPs (search for iface_list_count()).
491 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
492 my $interfaces = Samba::get_interfaces_config("client", 6);
494 my $clientdir = "$prefix_abs/client";
496 my $conffile = "$clientdir/client.conf";
497 $ENV{SMB_CONF_PATH} = $conffile;
499 sub write_clientconf($$$)
501 my ($conffile, $clientdir, $vars) = @_;
503 mkdir("$clientdir", 0777) unless -d "$clientdir";
505 my @subdirs = (
506 { name => "private", mask => 0777 },
507 { name => "bind-dns", mask => 0777 },
508 { name => "lockdir", mask => 0777 },
509 { name => "statedir", mask => 0777 },
510 { name => "cachedir", mask => 0777 },
511 { name => "pkinit", mask => 0700 },
512 { name => "pid", mask => 0777 },
513 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
514 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
517 foreach my $sub (@subdirs) {
518 my $dir = "$clientdir/$sub->{name}";
519 remove_tree($dir);
520 my $mask = umask;
521 if (defined($sub->{umask})) {
522 umask $sub->{umask};
524 mkdir($dir, $sub->{mask});
525 umask $mask;
528 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
529 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
530 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
531 my $ca_users_dir = "$cadir/Users";
533 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
534 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
535 # We make a copy here and make the certificated easily
536 # accessable in the client environment.
537 my $mask = umask;
538 umask 0077;
539 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
540 for my $d (readdir USERS) {
541 my $user_dir = "${ca_users_dir}/${d}";
542 next if ${d} =~ /^\./;
543 next if (! -d "${user_dir}");
544 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
545 for my $l (readdir USER) {
546 my $user_link = "${user_dir}/${l}";
547 next if ${l} =~ /^\./;
548 next if (! -l "${user_link}");
550 my $dest = "${clientdir}/pkinit/${l}";
551 Samba::copy_file_content(${user_link}, ${dest});
553 closedir USER;
555 closedir USERS;
556 umask $mask;
558 open(CF, ">$conffile");
559 print CF "[global]\n";
560 print CF "\tnetbios name = client\n";
561 if (defined($vars->{DOMAIN})) {
562 print CF "\tworkgroup = $vars->{DOMAIN}\n";
564 if (defined($vars->{REALM})) {
565 print CF "\trealm = $vars->{REALM}\n";
567 if ($opt_socket_wrapper) {
568 print CF "\tinterfaces = $interfaces\n";
570 print CF "
571 private dir = $clientdir/private
572 binddns dir = $clientdir/bind-dns
573 lock dir = $clientdir/lockdir
574 state directory = $clientdir/statedir
575 cache directory = $clientdir/cachedir
576 ncalrpc dir = $clientdir/ncalrpcdir
577 pid directory = $clientdir/pid
578 panic action = $RealBin/gdb_backtrace \%d
579 max xmit = 32K
580 notify:inotify = false
581 ldb:nosync = true
582 system:anonymous = true
583 client lanman auth = Yes
584 client min protocol = CORE
585 log level = 1
586 torture:basedir = $clientdir
587 #We don't want to pass our self-tests if the PAC code is wrong
588 gensec:require_pac = true
589 #We don't want to run 'speed' tests for very long
590 torture:timelimit = 1
591 winbind separator = /
592 tls cafile = ${cacert}
593 tls crlfile = ${cacrl_pem}
594 tls verify peer = no_check
595 include system krb5 conf = no
596 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
598 close(CF);
601 my @todo = ();
603 sub should_run_test($)
605 my $name = shift;
606 if ($#tests == -1) {
607 return 1;
609 for (my $i=0; $i <= $#tests; $i++) {
610 if ($name =~ /$tests[$i]/i) {
611 return 1;
614 return 0;
617 sub read_testlist($)
619 my ($filename) = @_;
621 my @ret = ();
622 open(IN, $filename) or die("Unable to open $filename: $!");
624 while (<IN>) {
625 if (/-- TEST(-LOADLIST|) --\n/) {
626 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
627 my $name = <IN>;
628 $name =~ s/\n//g;
629 my $env = <IN>;
630 $env =~ s/\n//g;
631 my $loadlist;
632 if ($supports_loadlist) {
633 $loadlist = <IN>;
634 $loadlist =~ s/\n//g;
636 my $cmdline = <IN>;
637 $cmdline =~ s/\n//g;
638 if (should_run_test($name) == 1) {
639 push (@ret, [$name, $env, $cmdline, $loadlist]);
641 } else {
642 print;
645 close(IN) or die("Error creating recipe from $filename");
646 return @ret;
649 if ($#testlists == -1) {
650 die("No testlists specified");
653 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
654 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
655 $ENV{TMPDIR} = "$tmpdir_abs";
656 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
657 if ($opt_quick) {
658 $ENV{SELFTEST_QUICK} = "1";
659 } else {
660 $ENV{SELFTEST_QUICK} = "";
662 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
664 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
665 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
667 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
668 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
670 my @available = ();
671 foreach my $fn (@testlists) {
672 foreach (read_testlist($fn)) {
673 my $name = $$_[0];
674 next if (@includes and not defined(find_in_list(\@includes, $name)));
675 push (@available, $_);
679 my $restricted = undef;
680 my $restricted_used = {};
682 if ($opt_load_list) {
683 $restricted = [];
684 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
685 while (<LOAD_LIST>) {
686 chomp;
687 push (@$restricted, $_);
689 close(LOAD_LIST);
692 my $individual_tests = undef;
693 $individual_tests = {};
695 foreach my $testsuite (@available) {
696 my $name = $$testsuite[0];
697 my $skipreason = skip(@$testsuite);
698 if (defined($restricted)) {
699 # Find the testsuite for this test
700 my $match = undef;
701 foreach my $r (@$restricted) {
702 if ($r eq $name) {
703 $individual_tests->{$name} = [];
704 $match = $r;
705 $restricted_used->{$r} = 1;
706 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
707 push(@{$individual_tests->{$name}}, $r);
708 $match = $r;
709 $restricted_used->{$r} = 1;
712 if ($match) {
713 if (defined($skipreason)) {
714 if (not $opt_list) {
715 Subunit::skip_testsuite($name, $skipreason);
717 } else {
718 push(@todo, $testsuite);
721 } elsif (defined($skipreason)) {
722 if (not $opt_list) {
723 Subunit::skip_testsuite($name, $skipreason);
725 } else {
726 push(@todo, $testsuite);
730 if (defined($restricted)) {
731 foreach (@$restricted) {
732 unless (defined($restricted_used->{$_})) {
733 print "No test or testsuite found matching $_\n";
736 } elsif ($#todo == -1) {
737 print STDERR "No tests to run\n";
738 exit(1);
741 my $suitestotal = $#todo + 1;
743 unless ($opt_list) {
744 Subunit::progress($suitestotal);
745 Subunit::report_time();
748 my $i = 0;
749 $| = 1;
751 my %running_envs = ();
753 sub get_running_env($)
755 my ($name) = @_;
757 my $envname = $name;
759 $envname =~ s/:.*//;
761 return $running_envs{$envname};
764 sub sighandler($)
766 my $signame = shift;
768 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
769 $SIG{PIPE} = 'IGNORE';
771 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
773 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
774 teardown_env($_) foreach(keys %running_envs);
775 system("pstree -p $$");
776 print "$0: PID[$$]: Exiting...\n";
777 exit(1);
780 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
782 sub setup_env($$)
784 my ($name, $prefix) = @_;
786 my $testenv_vars = undef;
788 my $envname = $name;
789 my $option = $name;
791 $envname =~ s/:.*//;
792 $option =~ s/^[^:]*//;
793 $option =~ s/^://;
795 $option = "client" if $option eq "";
797 # Initially clear out the environment for the provision, so previous envs'
798 # variables don't leak in. Provisioning steps must explicitly set their
799 # necessary variables when calling out to other executables
800 Samba::clear_exported_envvars();
801 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
802 delete $ENV{SMB_CONF_PATH};
804 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
805 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
807 if (defined(get_running_env($envname))) {
808 $testenv_vars = get_running_env($envname);
809 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
810 print $testenv_vars->{target}->getlog_env($testenv_vars);
811 $testenv_vars = undef;
813 } else {
814 $testenv_vars = $target->setup_env($envname, $prefix);
815 if (not defined($testenv_vars)) {
816 my $msg = "$opt_target can't start up known environment '$envname'";
817 if ($opt_one) {
818 die($msg);
820 warn $msg;
821 return;
823 if (ref $testenv_vars ne "HASH") {
824 return $testenv_vars;
826 if (defined($testenv_vars->{target})) {
827 $testenv_vars->{target} = $target;
831 return undef unless defined($testenv_vars);
833 $running_envs{$envname} = $testenv_vars;
835 if ($option eq "local") {
836 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
837 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
838 } elsif ($option eq "client") {
839 SocketWrapper::set_default_iface(11);
840 write_clientconf($conffile, $clientdir, $testenv_vars);
841 $ENV{SMB_CONF_PATH} = $conffile;
842 } else {
843 die("Unknown option[$option] for envname[$envname]");
846 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
847 Samba::export_envvars($testenv_vars);
849 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
850 unlink($krb5_ccache_path);
851 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
852 return $testenv_vars;
855 sub getlog_env($)
857 my ($envname) = @_;
858 return "" if ($envname eq "none");
859 my $env = get_running_env($envname);
860 return $env->{target}->getlog_env($env);
863 sub check_env($)
865 my ($envname) = @_;
866 my $env = get_running_env($envname);
867 return $env->{target}->check_env($env);
870 sub teardown_env($)
872 my ($envname) = @_;
873 return if ($envname eq "none");
874 print STDERR "teardown_env($envname)\n";
875 my $env = get_running_env($envname);
876 $env->{target}->teardown_env($env);
877 delete $running_envs{$envname};
880 # This 'global' file needs to be empty when we start
881 unlink("$prefix_abs/dns_host_file");
882 unlink("$prefix_abs/hosts");
884 if ($opt_random_order) {
885 require List::Util;
886 my @newtodo = List::Util::shuffle(@todo);
887 @todo = @newtodo;
890 if ($opt_testenv) {
891 my $testenv_name = $ENV{SELFTEST_TESTENV};
892 $testenv_name = $testenv_default unless defined($testenv_name);
894 my $testenv_vars = setup_env($testenv_name, $prefix);
896 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
897 die("Unable to setup environment $testenv_name");
900 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
901 $ENV{ENVNAME} = $testenv_name;
903 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
905 my @term_args = ("echo -e \"
906 Welcome to the Samba4 Test environment '$testenv_name'
908 This matches the client environment used in make test
909 server is pid `cat \$PIDDIR/samba.pid`
911 Some useful environment variables:
912 TORTURE_OPTIONS=\$TORTURE_OPTIONS
913 SMB_CONF_PATH=\$SMB_CONF_PATH
915 $envvarstr
916 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
917 my @term = ();
918 if ($ENV{TERMINAL}) {
919 @term = ($ENV{TERMINAL});
920 # override the default terminal args (if specified)
921 if (defined($ENV{TERMINAL_ARGS})) {
922 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
924 } else {
925 @term = ("xterm", "-e");
926 unshift(@term_args, ("bash", "-c"));
929 system(@term, @term_args);
931 teardown_env($testenv_name);
932 } elsif ($opt_list) {
933 foreach (@todo) {
934 my $name = $$_[0];
935 my $envname = $$_[1];
936 my $cmd = $$_[2];
937 my $listcmd = $$_[3];
939 unless (defined($listcmd)) {
940 warn("Unable to list tests in $name");
941 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
942 # a single "test".
943 print "$name\n";
944 next;
947 system($listcmd);
949 if ($? == -1) {
950 die("Unable to run $listcmd: $!");
951 } elsif ($? & 127) {
952 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
955 my $exitcode = $? >> 8;
956 if ($exitcode != 0) {
957 die("$cmd exited with exit code $exitcode");
960 } else {
961 foreach (@todo) {
962 $i++;
963 my $cmd = $$_[2];
964 my $name = $$_[0];
965 my $envname = $$_[1];
966 my $envvars = setup_env($envname, $prefix);
968 if (not defined($envvars)) {
969 Subunit::start_testsuite($name);
970 Subunit::end_testsuite($name, "error",
971 "unable to set up environment $envname - exiting");
972 next;
973 } elsif ($envvars eq "UNKNOWN") {
974 Subunit::start_testsuite($name);
975 Subunit::end_testsuite($name, "error",
976 "environment $envname is unknown - exiting");
977 next;
980 # Generate a file with the individual tests to run, if the
981 # test runner for this test suite supports it.
982 if ($individual_tests and $individual_tests->{$name}) {
983 if ($$_[3]) {
984 my ($fh, $listid_file) = tempfile(UNLINK => 0);
985 foreach my $test (@{$individual_tests->{$name}}) {
986 print $fh substr($test, length($name)+1) . "\n";
988 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
989 } else {
990 warn("Unable to run individual tests in $name, it does not support --loadlist.");
994 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
996 teardown_env($envname) if ($opt_resetup_env);
1000 print "\n";
1002 teardown_env($_) foreach (keys %running_envs);
1004 my $failed = 0;
1006 # if there were any valgrind failures, show them
1007 foreach (<$prefix/valgrind.log*>) {
1008 next unless (-s $_);
1009 print "VALGRIND FAILURE\n";
1010 $failed++;
1011 system("cat $_");
1013 exit 0;