libcli:smb: add defines for SMB2.2 global capabilities
[Samba/gbeck.git] / selftest / selftest.pl
bloba6d4e140954abe4a3c832e46d66ddda436a4f73b
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;
21 use FindBin qw($RealBin $Script);
22 use File::Spec;
23 use File::Temp qw(tempfile);
24 use Getopt::Long;
25 use POSIX;
26 use Cwd qw(abs_path);
27 use lib "$RealBin";
28 use Subunit;
29 use SocketWrapper;
31 eval {
32 require Time::HiRes;
33 Time::HiRes->import("time");
35 if ($@) {
36 print "You don't have Time::Hires installed !\n";
39 my $opt_help = 0;
40 my $opt_target = "samba";
41 my $opt_quick = 0;
42 my $opt_socket_wrapper = 0;
43 my $opt_socket_wrapper_pcap = undef;
44 my $opt_socket_wrapper_keep_pcap = undef;
45 my $opt_one = 0;
46 my @opt_exclude = ();
47 my @opt_include = ();
48 my $opt_verbose = 0;
49 my $opt_testenv = 0;
50 my $opt_list = 0;
51 my $ldap = undef;
52 my $opt_resetup_env = undef;
53 my $opt_binary_mapping = "";
54 my $opt_load_list = undef;
55 my @testlists = ();
57 my $srcdir = ".";
58 my $bindir = "./bin";
59 my $prefix = "./st";
61 my @includes = ();
62 my @excludes = ();
64 sub pipe_handler {
65 my $sig = shift @_;
66 print STDERR "Exiting early because of SIGPIPE.\n";
67 exit(1);
70 $SIG{PIPE} = \&pipe_handler;
72 sub find_in_list($$)
74 my ($list, $fullname) = @_;
76 foreach (@$list) {
77 if ($fullname =~ /$$_[0]/) {
78 return ($$_[1]) if ($$_[1]);
79 return "";
83 return undef;
86 sub skip($)
88 my ($name) = @_;
90 return find_in_list(\@excludes, $name);
93 sub getlog_env($);
95 sub setup_pcap($)
97 my ($name) = @_;
99 return unless ($opt_socket_wrapper_pcap);
100 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
102 my $fname = $name;
103 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
105 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
107 SocketWrapper::setup_pcap($pcap_file);
109 return $pcap_file;
112 sub cleanup_pcap($$)
114 my ($pcap_file, $exitcode) = @_;
116 return unless ($opt_socket_wrapper_pcap);
117 return if ($opt_socket_wrapper_keep_pcap);
118 return unless ($exitcode == 0);
119 return unless defined($pcap_file);
121 unlink($pcap_file);
124 # expand strings from %ENV
125 sub expand_environment_strings($)
127 my $s = shift;
128 # we use a reverse sort so we do the longer ones first
129 foreach my $k (sort { $b cmp $a } keys %ENV) {
130 $s =~ s/\$$k/$ENV{$k}/g;
132 return $s;
135 sub run_testsuite($$$$$)
137 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
138 my $pcap_file = setup_pcap($name);
140 Subunit::start_testsuite($name);
141 Subunit::progress_push();
142 Subunit::report_time(time());
143 system($cmd);
144 Subunit::report_time(time());
145 Subunit::progress_pop();
147 if ($? == -1) {
148 Subunit::progress_pop();
149 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
150 exit(1);
151 } elsif ($? & 127) {
152 Subunit::end_testsuite($name, "error",
153 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
154 exit(1);
157 my $exitcode = $? >> 8;
159 my $envlog = getlog_env($envname);
160 if ($envlog ne "") {
161 print "envlog: $envlog\n";
164 print "command: $cmd\n";
165 printf "expanded command: %s\n", expand_environment_strings($cmd);
167 if ($exitcode == 0) {
168 Subunit::end_testsuite($name, "success");
169 } else {
170 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
173 cleanup_pcap($pcap_file, $exitcode);
175 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
176 print "PCAP FILE: $pcap_file\n";
179 if ($exitcode != 0) {
180 exit(1) if ($opt_one);
183 return $exitcode;
186 sub ShowHelp()
188 print "Samba test runner
189 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
190 Copyright (C) Stefan Metzmacher <metze\@samba.org>
192 Usage: $Script [OPTIONS] TESTNAME-REGEX
194 Generic options:
195 --help this help page
196 --target=samba[3]|win Samba version to target
197 --testlist=FILE file to read available tests from
199 Paths:
200 --prefix=DIR prefix to run tests in [st]
201 --srcdir=DIR source directory [.]
202 --bindir=DIR binaries directory [./bin]
204 Target Specific:
205 --socket-wrapper-pcap save traffic to pcap directories
206 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
207 failed
208 --socket-wrapper enable socket wrapper
210 Samba4 Specific:
211 --ldap=openldap|fedora-ds back samba onto specified ldap server
213 Behaviour:
214 --quick run quick overall test
215 --one abort when the first test fails
216 --verbose be verbose
217 --testenv run a shell in the requested test environment
218 --list list available tests
220 exit(0);
223 my $result = GetOptions (
224 'help|h|?' => \$opt_help,
225 'target=s' => \$opt_target,
226 'prefix=s' => \$prefix,
227 'socket-wrapper' => \$opt_socket_wrapper,
228 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
229 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
230 'quick' => \$opt_quick,
231 'one' => \$opt_one,
232 'exclude=s' => \@opt_exclude,
233 'include=s' => \@opt_include,
234 'srcdir=s' => \$srcdir,
235 'bindir=s' => \$bindir,
236 'verbose' => \$opt_verbose,
237 'testenv' => \$opt_testenv,
238 'list' => \$opt_list,
239 'ldap:s' => \$ldap,
240 'resetup-environment' => \$opt_resetup_env,
241 'testlist=s' => \@testlists,
242 'load-list=s' => \$opt_load_list,
243 'binary-mapping=s' => \$opt_binary_mapping
246 exit(1) if (not $result);
248 ShowHelp() if ($opt_help);
250 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
252 # we want unbuffered output
253 $| = 1;
255 my @tests = @ARGV;
257 # quick hack to disable rpc validation when using valgrind - its way too slow
258 unless (defined($ENV{VALGRIND})) {
259 $ENV{VALIDATE} = "validate";
260 $ENV{MALLOC_CHECK_} = 2;
263 # make all our python scripts unbuffered
264 $ENV{PYTHONUNBUFFERED} = 1;
266 my $bindir_abs = abs_path($bindir);
268 # Backwards compatibility:
269 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
270 if (defined($ENV{FEDORA_DS_ROOT})) {
271 $ldap = "fedora-ds";
272 } else {
273 $ldap = "openldap";
277 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
278 if ($ldap) {
279 # LDAP is slow
280 $torture_maxtime *= 2;
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 mkdir($prefix, 0700) unless -d $prefix;
296 chmod 0700, $prefix;
298 my $prefix_abs = abs_path($prefix);
299 my $tmpdir_abs = abs_path("$prefix/tmp");
300 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
302 my $srcdir_abs = abs_path($srcdir);
304 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
305 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
307 $ENV{PREFIX} = $prefix;
308 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
309 $ENV{PREFIX_ABS} = $prefix_abs;
310 $ENV{SRCDIR} = $srcdir;
311 $ENV{SRCDIR_ABS} = $srcdir_abs;
312 $ENV{BINDIR} = $bindir_abs;
314 my $tls_enabled = not $opt_quick;
315 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
317 sub prefix_pathvar($$)
319 my ($name, $newpath) = @_;
320 if (defined($ENV{$name})) {
321 $ENV{$name} = "$newpath:$ENV{$name}";
322 } else {
323 $ENV{$name} = $newpath;
326 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
327 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
329 if ($opt_socket_wrapper_keep_pcap) {
330 # Socket wrapper keep pcap implies socket wrapper pcap
331 $opt_socket_wrapper_pcap = 1;
334 if ($opt_socket_wrapper_pcap) {
335 # Socket wrapper pcap implies socket wrapper
336 $opt_socket_wrapper = 1;
339 my $socket_wrapper_dir;
340 if ($opt_socket_wrapper) {
341 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
342 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
343 } elsif (not $opt_list) {
344 unless ($< == 0) {
345 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
349 my $target;
350 my $testenv_default = "none";
352 my %binary_mapping = ();
353 if ($opt_binary_mapping) {
354 my @binmapping_list = split(/,/, $opt_binary_mapping);
355 foreach my $mapping (@binmapping_list) {
356 my ($bin, $map) = split(/\:/, $mapping);
357 $binary_mapping{$bin} = $map;
361 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
363 # After this many seconds, the server will self-terminate. All tests
364 # must terminate in this time, and testenv will only stay alive this
365 # long
367 my $server_maxtime = 7500;
368 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
369 $server_maxtime = $ENV{SMBD_MAXTIME};
372 unless ($opt_list) {
373 if ($opt_target eq "samba") {
374 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
375 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
377 $testenv_default = "dc";
378 require target::Samba;
379 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
380 } elsif ($opt_target eq "samba3") {
381 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
382 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
384 $testenv_default = "member";
385 require target::Samba3;
386 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
387 } elsif ($opt_target eq "win") {
388 die("Windows tests will not run with socket wrapper enabled.")
389 if ($opt_socket_wrapper);
390 $testenv_default = "dc";
391 require target::Windows;
392 $target = new Windows();
397 # Start a Virtual Distributed Ethernet Switch
398 # Returns the pid of the switch.
400 sub start_vde_switch($)
402 my ($path) = @_;
404 system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
406 open(PID, "$path/vde.pid");
407 <PID> =~ /([0-9]+)/;
408 my $pid = $1;
409 close(PID);
411 return $pid;
414 # Stop a Virtual Distributed Ethernet Switch
415 sub stop_vde_switch($)
417 my ($pid) = @_;
418 kill 9, $pid;
421 sub read_test_regexes($)
423 my ($name) = @_;
424 my @ret = ();
425 open(LF, "<$name") or die("unable to read $name: $!");
426 while (<LF>) {
427 chomp;
428 next if (/^#/);
429 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
430 push (@ret, [$1, $4]);
431 } else {
432 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
433 push (@ret, [$_, undef]);
436 close(LF);
437 return @ret;
440 foreach (@opt_exclude) {
441 push (@excludes, read_test_regexes($_));
444 foreach (@opt_include) {
445 push (@includes, read_test_regexes($_));
448 my $interfaces = join(',', ("127.0.0.11/8",
449 "127.0.0.12/8",
450 "127.0.0.13/8",
451 "127.0.0.14/8",
452 "127.0.0.15/8",
453 "127.0.0.16/8"));
455 my $clientdir = "$prefix_abs/client";
457 my $conffile = "$clientdir/client.conf";
458 $ENV{SMB_CONF_PATH} = $conffile;
460 sub write_clientconf($$$)
462 my ($conffile, $clientdir, $vars) = @_;
464 mkdir("$clientdir", 0777) unless -d "$clientdir";
466 if ( -d "$clientdir/private" ) {
467 unlink <$clientdir/private/*>;
468 } else {
469 mkdir("$clientdir/private", 0777);
472 if ( -d "$clientdir/lockdir" ) {
473 unlink <$clientdir/lockdir/*>;
474 } else {
475 mkdir("$clientdir/lockdir", 0777);
478 if ( -d "$clientdir/statedir" ) {
479 unlink <$clientdir/statedir/*>;
480 } else {
481 mkdir("$clientdir/statedir", 0777);
484 if ( -d "$clientdir/cachedir" ) {
485 unlink <$clientdir/cachedir/*>;
486 } else {
487 mkdir("$clientdir/cachedir", 0777);
490 # this is ugly, but the ncalrpcdir needs exactly 0755
491 # otherwise tests fail.
492 my $mask = umask;
493 umask 0022;
494 if ( -d "$clientdir/ncalrpcdir/np" ) {
495 unlink <$clientdir/ncalrpcdir/np/*>;
496 rmdir "$clientdir/ncalrpcdir/np";
498 if ( -d "$clientdir/ncalrpcdir" ) {
499 unlink <$clientdir/ncalrpcdir/*>;
500 rmdir "$clientdir/ncalrpcdir";
502 mkdir("$clientdir/ncalrpcdir", 0755);
503 umask $mask;
505 open(CF, ">$conffile");
506 print CF "[global]\n";
507 print CF "\tnetbios name = client\n";
508 if (defined($vars->{DOMAIN})) {
509 print CF "\tworkgroup = $vars->{DOMAIN}\n";
511 if (defined($vars->{REALM})) {
512 print CF "\trealm = $vars->{REALM}\n";
514 if ($opt_socket_wrapper) {
515 print CF "\tinterfaces = $interfaces\n";
517 print CF "
518 private dir = $clientdir/private
519 lock dir = $clientdir/lockdir
520 state directory = $clientdir/statedir
521 cache directory = $clientdir/cachedir
522 ncalrpc dir = $clientdir/ncalrpcdir
523 name resolve order = file bcast
524 panic action = $RealBin/gdb_backtrace \%d
525 max xmit = 32K
526 notify:inotify = false
527 ldb:nosync = true
528 system:anonymous = true
529 client lanman auth = Yes
530 log level = 1
531 torture:basedir = $clientdir
532 #We don't want to pass our self-tests if the PAC code is wrong
533 gensec:require_pac = true
534 resolv:host file = $prefix_abs/dns_host_file
535 #We don't want to run 'speed' tests for very long
536 torture:timelimit = 1
538 close(CF);
541 my @todo = ();
543 sub should_run_test($)
545 my $name = shift;
546 if ($#tests == -1) {
547 return 1;
549 for (my $i=0; $i <= $#tests; $i++) {
550 if ($name =~ /$tests[$i]/i) {
551 return 1;
554 return 0;
557 sub read_testlist($)
559 my ($filename) = @_;
561 my @ret = ();
562 open(IN, $filename) or die("Unable to open $filename: $!");
564 while (<IN>) {
565 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
566 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
567 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
568 my $name = <IN>;
569 $name =~ s/\n//g;
570 my $env = <IN>;
571 $env =~ s/\n//g;
572 my $cmdline = <IN>;
573 $cmdline =~ s/\n//g;
574 if (should_run_test($name) == 1) {
575 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
577 } else {
578 print;
581 close(IN) or die("Error creating recipe");
582 return @ret;
585 if ($#testlists == -1) {
586 die("No testlists specified");
589 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
590 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
591 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
592 if ($opt_socket_wrapper) {
593 $ENV{SELFTEST_INTERFACES} = $interfaces;
594 } else {
595 $ENV{SELFTEST_INTERFACES} = "";
597 if ($opt_verbose) {
598 $ENV{SELFTEST_VERBOSE} = "1";
599 } else {
600 $ENV{SELFTEST_VERBOSE} = "";
602 if ($opt_quick) {
603 $ENV{SELFTEST_QUICK} = "1";
604 } else {
605 $ENV{SELFTEST_QUICK} = "";
607 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
609 my @available = ();
610 foreach my $fn (@testlists) {
611 foreach (read_testlist($fn)) {
612 my $name = $$_[0];
613 next if (@includes and not defined(find_in_list(\@includes, $name)));
614 push (@available, $_);
618 my $restricted = undef;
619 my $restricted_used = {};
621 if ($opt_load_list) {
622 $restricted = [];
623 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
624 while (<LOAD_LIST>) {
625 chomp;
626 push (@$restricted, $_);
628 close(LOAD_LIST);
631 my $individual_tests = undef;
632 $individual_tests = {};
634 foreach my $testsuite (@available) {
635 my $name = $$testsuite[0];
636 my $skipreason = skip($name);
637 if (defined($restricted)) {
638 # Find the testsuite for this test
639 my $match = undef;
640 foreach my $r (@$restricted) {
641 if ($r eq $name) {
642 $individual_tests->{$name} = [];
643 $match = $r;
644 $restricted_used->{$r} = 1;
645 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
646 push(@{$individual_tests->{$name}}, $r);
647 $match = $r;
648 $restricted_used->{$r} = 1;
651 if ($match) {
652 if (defined($skipreason)) {
653 if (not $opt_list) {
654 Subunit::skip_testsuite($name, $skipreason);
656 } else {
657 push(@todo, $testsuite);
660 } elsif (defined($skipreason)) {
661 if (not $opt_list) {
662 Subunit::skip_testsuite($name, $skipreason);
664 } else {
665 push(@todo, $testsuite);
669 if (defined($restricted)) {
670 foreach (@$restricted) {
671 unless (defined($restricted_used->{$_})) {
672 print "No test or testsuite found matching $_\n";
675 } elsif ($#todo == -1) {
676 print STDERR "No tests to run\n";
677 exit(1);
680 my $suitestotal = $#todo + 1;
682 unless ($opt_list) {
683 Subunit::progress($suitestotal);
684 Subunit::report_time(time());
687 my $i = 0;
688 $| = 1;
690 my %running_envs = ();
692 sub get_running_env($)
694 my ($name) = @_;
696 my $envname = $name;
698 $envname =~ s/:.*//;
700 return $running_envs{$envname};
703 my @exported_envvars = (
704 # domain stuff
705 "DOMAIN",
706 "REALM",
708 # domain controller stuff
709 "DC_SERVER",
710 "DC_SERVER_IP",
711 "DC_NETBIOSNAME",
712 "DC_NETBIOSALIAS",
714 # domain member
715 "MEMBER_SERVER",
716 "MEMBER_SERVER_IP",
717 "MEMBER_NETBIOSNAME",
718 "MEMBER_NETBIOSALIAS",
720 # rpc proxy controller stuff
721 "RPC_PROXY_SERVER",
722 "RPC_PROXY_SERVER_IP",
723 "RPC_PROXY_NETBIOSNAME",
724 "RPC_PROXY_NETBIOSALIAS",
726 # domain controller stuff for Vampired DC
727 "VAMPIRE_DC_SERVER",
728 "VAMPIRE_DC_SERVER_IP",
729 "VAMPIRE_DC_NETBIOSNAME",
730 "VAMPIRE_DC_NETBIOSALIAS",
732 # server stuff
733 "SERVER",
734 "SERVER_IP",
735 "NETBIOSNAME",
736 "NETBIOSALIAS",
738 # user stuff
739 "USERNAME",
740 "USERID",
741 "PASSWORD",
742 "DC_USERNAME",
743 "DC_PASSWORD",
745 # misc stuff
746 "KRB5_CONFIG",
747 "WINBINDD_SOCKET_DIR",
748 "WINBINDD_PRIV_PIPE_DIR",
749 "NMBD_SOCKET_DIR",
750 "LOCAL_PATH"
753 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
754 my $signame = shift;
755 teardown_env($_) foreach(keys %running_envs);
756 die("Received signal $signame");
759 sub setup_env($$)
761 my ($name, $prefix) = @_;
763 my $testenv_vars = undef;
765 my $envname = $name;
766 my $option = $name;
768 $envname =~ s/:.*//;
769 $option =~ s/^[^:]*//;
770 $option =~ s/^://;
772 $option = "client" if $option eq "";
774 if ($envname eq "none") {
775 $testenv_vars = {};
776 } elsif (defined(get_running_env($envname))) {
777 $testenv_vars = get_running_env($envname);
778 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
779 print $testenv_vars->{target}->getlog_env($testenv_vars);
780 $testenv_vars = undef;
782 } else {
783 $testenv_vars = $target->setup_env($envname, $prefix);
784 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
785 return $testenv_vars;
786 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
787 $testenv_vars->{target} = $target;
789 if (not defined($testenv_vars)) {
790 warn("$opt_target can't start up known environment '$envname'");
795 return undef unless defined($testenv_vars);
797 $running_envs{$envname} = $testenv_vars;
799 if ($option eq "local") {
800 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
801 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
802 } elsif ($option eq "client") {
803 SocketWrapper::set_default_iface(11);
804 write_clientconf($conffile, $clientdir, $testenv_vars);
805 $ENV{SMB_CONF_PATH} = $conffile;
806 } else {
807 die("Unknown option[$option] for envname[$envname]");
810 foreach (@exported_envvars) {
811 if (defined($testenv_vars->{$_})) {
812 $ENV{$_} = $testenv_vars->{$_};
813 } else {
814 delete $ENV{$_};
818 return $testenv_vars;
821 sub exported_envvars_str($)
823 my ($testenv_vars) = @_;
824 my $out = "";
826 foreach (@exported_envvars) {
827 next unless defined($testenv_vars->{$_});
828 $out .= $_."=".$testenv_vars->{$_}."\n";
831 return $out;
834 sub getlog_env($)
836 my ($envname) = @_;
837 return "" if ($envname eq "none");
838 my $env = get_running_env($envname);
839 return $env->{target}->getlog_env($env);
842 sub check_env($)
844 my ($envname) = @_;
845 return 1 if ($envname eq "none");
846 my $env = get_running_env($envname);
847 return $env->{target}->check_env($env);
850 sub teardown_env($)
852 my ($envname) = @_;
853 return if ($envname eq "none");
854 my $env = get_running_env($envname);
855 $env->{target}->teardown_env($env);
856 delete $running_envs{$envname};
859 # This 'global' file needs to be empty when we start
860 unlink("$prefix_abs/dns_host_file");
862 if ($opt_testenv) {
863 my $testenv_name = $ENV{SELFTEST_TESTENV};
864 $testenv_name = $testenv_default unless defined($testenv_name);
866 my $testenv_vars = setup_env($testenv_name, $prefix);
868 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
870 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
871 $ENV{ENVNAME} = $testenv_name;
873 my $envvarstr = exported_envvars_str($testenv_vars);
875 my $term = ($ENV{TERMINAL} or "xterm -e");
876 system("$term 'echo -e \"
877 Welcome to the Samba4 Test environment '$testenv_name'
879 This matches the client environment used in make test
880 server is pid `cat \$PIDDIR/samba.pid`
882 Some useful environment variables:
883 TORTURE_OPTIONS=\$TORTURE_OPTIONS
884 SMB_CONF_PATH=\$SMB_CONF_PATH
886 $envvarstr
887 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
888 teardown_env($testenv_name);
889 } elsif ($opt_list) {
890 foreach (@todo) {
891 my $cmd = $$_[2];
892 my $name = $$_[0];
893 my $envname = $$_[1];
895 unless($cmd =~ /\$LISTOPT/) {
896 warn("Unable to list tests in $name");
897 next;
900 $cmd =~ s/\$LISTOPT/--list/g;
902 system($cmd);
904 if ($? == -1) {
905 die("Unable to run $cmd: $!");
906 } elsif ($? & 127) {
907 die(snprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
910 my $exitcode = $? >> 8;
911 if ($exitcode != 0) {
912 die("$cmd exited with exit code $exitcode");
915 } else {
916 foreach (@todo) {
917 $i++;
918 my $cmd = $$_[2];
919 my $name = $$_[0];
920 my $envname = $$_[1];
922 my $envvars = setup_env($envname, $prefix);
923 if (not defined($envvars)) {
924 Subunit::start_testsuite($name);
925 Subunit::end_testsuite($name, "error",
926 "unable to set up environment $envname - exiting");
927 next;
928 } elsif ($envvars eq "UNKNOWN") {
929 Subunit::start_testsuite($name);
930 Subunit::end_testsuite($name, "skip",
931 "environment $envname is unknown in this test backend - skipping");
932 next;
935 # Generate a file with the individual tests to run, if the
936 # test runner for this test suite supports it.
937 if ($individual_tests and $individual_tests->{$name}) {
938 if ($$_[3]) {
939 my ($fh, $listid_file) = tempfile(UNLINK => 0);
940 foreach my $test (@{$individual_tests->{$name}}) {
941 print $fh substr($test, length($name)+1) . "\n";
943 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
944 } elsif ($$_[4]) {
945 $cmd =~ s/\s+[^\s]+\s*$//;
946 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
950 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
952 teardown_env($envname) if ($opt_resetup_env);
956 print "\n";
958 teardown_env($_) foreach (keys %running_envs);
960 my $failed = 0;
962 # if there were any valgrind failures, show them
963 foreach (<$prefix/valgrind.log*>) {
964 next unless (-s $_);
965 print "VALGRIND FAILURE\n";
966 $failed++;
967 system("cat $_");
969 exit 0;