s4-tests: Removed search tests with anonymous credentials as they fail againts Windows
[Samba/gebeck_regimport.git] / selftest / selftest.pl
blob2680b6adcee6fc1577b9c870a5d77c3b98d4e8ac
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 =pod
21 =head1 NAME
23 selftest - Samba test runner
25 =head1 SYNOPSIS
27 selftest --help
29 selftest [--srcdir=DIR] [--builddir=DIR] [--exeext=EXT][--target=samba4|samba3|win|kvm] [--socket-wrapper] [--quick] [--exclude=FILE] [--include=FILE] [--one] [--prefix=prefix] [--testlist=FILE] [TESTS]
31 =head1 DESCRIPTION
33 A simple test runner. TESTS is a regular expression with tests to run.
35 =head1 OPTIONS
37 =over 4
39 =item I<--help>
41 Show list of available options.
43 =item I<--srcdir=DIR>
45 Source directory.
47 =item I<--builddir=DIR>
49 Build directory.
51 =item I<--exeext=EXT>
53 Executable extention
55 =item I<--prefix=DIR>
57 Change directory to run tests in. Default is 'st'.
59 =item I<--target samba4|samba3|win|kvm>
61 Specify test target against which to run. Default is 'samba4'.
63 =item I<--quick>
65 Run only a limited number of tests. Intended to run in about 30 seconds on
66 moderately recent systems.
68 =item I<--socket-wrapper>
70 Use socket wrapper library for communication with server. Only works
71 when the server is running locally.
73 Will prevent TCP and UDP ports being opened on the local host but
74 (transparently) redirects these calls to use unix domain sockets.
76 =item I<--exclude>
78 Specify a file containing a list of tests that should be skipped. Possible
79 candidates are tests that segfault the server, flip or don't end.
81 =item I<--include>
83 Specify a file containing a list of tests that should be run. Same format
84 as the --exclude flag.
86 Not includes specified means all tests will be run.
88 =item I<--one>
90 Abort as soon as one test fails.
92 =item I<--testlist>
94 Load a list of tests from the specified location.
96 =back
98 =head1 ENVIRONMENT
100 =over 4
102 =item I<SMBD_VALGRIND>
104 =item I<TORTURE_MAXTIME>
106 =item I<VALGRIND>
108 =item I<TLS_ENABLED>
110 =item I<srcdir>
112 =back
114 =head1 LICENSE
116 selftest is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
118 =head1 AUTHOR
120 Jelmer Vernooij
122 =cut
124 use strict;
126 use FindBin qw($RealBin $Script);
127 use File::Spec;
128 use File::Temp qw(tempfile);
129 use Getopt::Long;
130 use POSIX;
131 use Cwd qw(abs_path);
132 use lib "$RealBin";
133 use Subunit;
134 use SocketWrapper;
136 eval {
137 require Time::HiRes;
139 unless ($@) {
140 use Time::HiRes qw(time);
143 my $opt_help = 0;
144 my $opt_target = "samba4";
145 my $opt_quick = 0;
146 my $opt_socket_wrapper = 0;
147 my $opt_socket_wrapper_pcap = undef;
148 my $opt_socket_wrapper_keep_pcap = undef;
149 my $opt_one = 0;
150 my @opt_exclude = ();
151 my @opt_include = ();
152 my $opt_verbose = 0;
153 my $opt_image = undef;
154 my $opt_testenv = 0;
155 my $ldap = undef;
156 my $opt_resetup_env = undef;
157 my $opt_bindir = undef;
158 my $opt_load_list = undef;
159 my @testlists = ();
161 my $srcdir = ".";
162 my $builddir = ".";
163 my $exeext = "";
164 my $prefix = "./st";
166 my @includes = ();
167 my @excludes = ();
169 sub find_in_list($$)
171 my ($list, $fullname) = @_;
173 foreach (@$list) {
174 if ($fullname =~ /$$_[0]/) {
175 return ($$_[1]) if ($$_[1]);
176 return "";
180 return undef;
183 sub skip($)
185 my ($name) = @_;
187 return find_in_list(\@excludes, $name);
190 sub getlog_env($);
192 sub setup_pcap($)
194 my ($name) = @_;
196 return unless ($opt_socket_wrapper_pcap);
197 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
199 my $fname = $name;
200 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
202 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
204 SocketWrapper::setup_pcap($pcap_file);
206 return $pcap_file;
209 sub cleanup_pcap($$)
211 my ($pcap_file, $exitcode) = @_;
213 return unless ($opt_socket_wrapper_pcap);
214 return if ($opt_socket_wrapper_keep_pcap);
215 return unless ($exitcode == 0);
216 return unless defined($pcap_file);
218 unlink($pcap_file);
221 # expand strings from %ENV
222 sub expand_environment_strings($)
224 my $s = shift;
225 # we use a reverse sort so we do the longer ones first
226 foreach my $k (sort { $b cmp $a } keys %ENV) {
227 $s =~ s/\$$k/$ENV{$k}/g;
229 return $s;
232 sub run_testsuite($$$$$)
234 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
235 my $pcap_file = setup_pcap($name);
237 Subunit::start_testsuite($name);
238 Subunit::progress_push();
239 Subunit::report_time(time());
240 system($cmd);
241 Subunit::report_time(time());
242 Subunit::progress_pop();
244 if ($? == -1) {
245 Subunit::progress_pop();
246 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
247 return 0;
248 } elsif ($? & 127) {
249 Subunit::end_testsuite($name, "error",
250 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
251 exit(1);
254 my $exitcode = $? >> 8;
256 my $envlog = getlog_env($envname);
257 if ($envlog ne "") {
258 print "envlog: $envlog\n";
261 print "command: $cmd\n";
262 printf "expanded command: %s\n", expand_environment_strings($cmd);
264 if ($exitcode == 0) {
265 Subunit::end_testsuite($name, "success");
266 } else {
267 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
270 cleanup_pcap($pcap_file, $exitcode);
272 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
273 print "PCAP FILE: $pcap_file\n";
276 if ($exitcode != 0) {
277 exit(1) if ($opt_one);
280 return $exitcode;
283 sub ShowHelp()
285 print "Samba test runner
286 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
287 Copyright (C) Stefan Metzmacher <metze\@samba.org>
289 Usage: $Script [OPTIONS] TESTNAME-REGEX
291 Generic options:
292 --help this help page
293 --target=samba[34]|win|kvm Samba version to target
294 --testlist=FILE file to read available tests from
296 Paths:
297 --prefix=DIR prefix to run tests in [st]
298 --srcdir=DIR source directory [.]
299 --builddir=DIR output directory [.]
300 --exeext=EXT executable extention []
302 Target Specific:
303 --socket-wrapper-pcap save traffic to pcap directories
304 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
305 failed
306 --socket-wrapper enable socket wrapper
307 --bindir=PATH path to target binaries
309 Samba4 Specific:
310 --ldap=openldap|fedora-ds back samba onto specified ldap server
312 Kvm Specific:
313 --image=PATH path to KVM image
315 Behaviour:
316 --quick run quick overall test
317 --one abort when the first test fails
318 --verbose be verbose
319 --analyse-cmd CMD command to run after each test
321 exit(0);
324 my $result = GetOptions (
325 'help|h|?' => \$opt_help,
326 'target=s' => \$opt_target,
327 'prefix=s' => \$prefix,
328 'socket-wrapper' => \$opt_socket_wrapper,
329 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
330 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
331 'quick' => \$opt_quick,
332 'one' => \$opt_one,
333 'exclude=s' => \@opt_exclude,
334 'include=s' => \@opt_include,
335 'srcdir=s' => \$srcdir,
336 'builddir=s' => \$builddir,
337 'exeext=s' => \$exeext,
338 'verbose' => \$opt_verbose,
339 'testenv' => \$opt_testenv,
340 'ldap:s' => \$ldap,
341 'resetup-environment' => \$opt_resetup_env,
342 'bindir:s' => \$opt_bindir,
343 'image=s' => \$opt_image,
344 'testlist=s' => \@testlists,
345 'load-list=s' => \$opt_load_list,
348 exit(1) if (not $result);
350 ShowHelp() if ($opt_help);
352 # we want unbuffered output
353 $| = 1;
355 my @tests = @ARGV;
357 # quick hack to disable rpc validation when using valgrind - its way too slow
358 unless (defined($ENV{VALGRIND})) {
359 $ENV{VALIDATE} = "validate";
360 $ENV{MALLOC_CHECK_} = 2;
363 # make all our python scripts unbuffered
364 $ENV{PYTHONUNBUFFERED} = 1;
366 my $bindir = ($opt_bindir or "$builddir/bin");
367 my $bindir_abs = abs_path($bindir);
369 # Backwards compatibility:
370 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
371 if (defined($ENV{FEDORA_DS_ROOT})) {
372 $ldap = "fedora-ds";
373 } else {
374 $ldap = "openldap";
378 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
379 if ($ldap) {
380 # LDAP is slow
381 $torture_maxtime *= 2;
384 $prefix =~ s+//+/+;
385 $prefix =~ s+/./+/+;
386 $prefix =~ s+/$++;
388 die("using an empty prefix isn't allowed") unless $prefix ne "";
390 #Ensure we have the test prefix around
391 mkdir($prefix, 0777) unless -d $prefix;
393 my $prefix_abs = abs_path($prefix);
394 my $srcdir_abs = abs_path($srcdir);
395 my $builddir_abs = abs_path($builddir);
397 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
398 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
400 $ENV{PREFIX} = $prefix;
401 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
402 $ENV{PREFIX_ABS} = $prefix_abs;
403 $ENV{SRCDIR} = $srcdir;
404 $ENV{SRCDIR_ABS} = $srcdir_abs;
405 $ENV{BUILDDIR} = $builddir;
406 $ENV{BUILDDIR_ABS} = $builddir_abs;
407 $ENV{EXEEXT} = $exeext;
409 my $tls_enabled = not $opt_quick;
410 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
411 $ENV{LDB_MODULES_PATH} = "$bindir_abs/modules/ldb";
412 $ENV{LD_SAMBA_MODULE_PATH} = "$bindir_abs/modules";
413 sub prefix_pathvar($$)
415 my ($name, $newpath) = @_;
416 if (defined($ENV{$name})) {
417 $ENV{$name} = "$newpath:$ENV{$name}";
418 } else {
419 $ENV{$name} = $newpath;
422 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
423 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
425 if ($opt_socket_wrapper_keep_pcap) {
426 # Socket wrapper keep pcap implies socket wrapper pcap
427 $opt_socket_wrapper_pcap = 1;
430 if ($opt_socket_wrapper_pcap) {
431 # Socket wrapper pcap implies socket wrapper
432 $opt_socket_wrapper = 1;
435 my $socket_wrapper_dir;
436 if ($opt_socket_wrapper) {
437 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
438 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
439 } else {
440 unless ($< == 0) {
441 print "WARNING: Not using socket wrapper, but also not running as root. Will not be able to listen on proper ports\n";
445 my $target;
446 my $testenv_default = "none";
448 if ($opt_target eq "samba4") {
449 $testenv_default = "all";
450 require target::Samba4;
451 $target = new Samba4($bindir, $ldap, "$srcdir/setup", $exeext);
452 } elsif ($opt_target eq "samba3") {
453 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
454 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
456 $testenv_default = "member";
457 require target::Samba3;
458 $target = new Samba3($bindir);
459 } elsif ($opt_target eq "win") {
460 die("Windows tests will not run with socket wrapper enabled.")
461 if ($opt_socket_wrapper);
462 $testenv_default = "dc";
463 require target::Windows;
464 $target = new Windows();
465 } elsif ($opt_target eq "kvm") {
466 die("Kvm tests will not run with socket wrapper enabled.")
467 if ($opt_socket_wrapper);
468 require target::Kvm;
469 die("No image specified") unless ($opt_image);
470 $target = new Kvm($opt_image, undef);
474 # Start a Virtual Distributed Ethernet Switch
475 # Returns the pid of the switch.
477 sub start_vde_switch($)
479 my ($path) = @_;
481 system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
483 open(PID, "$path/vde.pid");
484 <PID> =~ /([0-9]+)/;
485 my $pid = $1;
486 close(PID);
488 return $pid;
491 # Stop a Virtual Distributed Ethernet Switch
492 sub stop_vde_switch($)
494 my ($pid) = @_;
495 kill 9, $pid;
498 sub read_test_regexes($)
500 my ($name) = @_;
501 my @ret = ();
502 open(LF, "<$name") or die("unable to read $name: $!");
503 while (<LF>) {
504 chomp;
505 next if (/^#/);
506 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
507 push (@ret, [$1, $4]);
508 } else {
509 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
510 push (@ret, [$_, undef]);
513 close(LF);
514 return @ret;
517 foreach (@opt_exclude) {
518 push (@excludes, read_test_regexes($_));
521 foreach (@opt_include) {
522 push (@includes, read_test_regexes($_));
525 my $interfaces = join(',', ("127.0.0.11/8",
526 "127.0.0.12/8",
527 "127.0.0.13/8",
528 "127.0.0.14/8",
529 "127.0.0.15/8",
530 "127.0.0.16/8"));
532 my $clientdir = "$prefix_abs/client";
534 my $conffile = "$clientdir/client.conf";
535 $ENV{SMB_CONF_PATH} = $conffile;
537 sub write_clientconf($$$)
539 my ($conffile, $clientdir, $vars) = @_;
541 mkdir("$clientdir", 0777) unless -d "$clientdir";
543 if ( -d "$clientdir/private" ) {
544 unlink <$clientdir/private/*>;
545 } else {
546 mkdir("$clientdir/private", 0777);
549 if ( -d "$clientdir/lockdir" ) {
550 unlink <$clientdir/lockdir/*>;
551 } else {
552 mkdir("$clientdir/lockdir", 0777);
555 if ( -d "$clientdir/ncalrpcdir" ) {
556 unlink <$clientdir/ncalrpcdir/*>;
557 } else {
558 mkdir("$clientdir/ncalrpcdir", 0777);
561 open(CF, ">$conffile");
562 print CF "[global]\n";
563 if (defined($ENV{VALGRIND})) {
564 print CF "\ticonv:native = true\n";
565 } else {
566 print CF "\ticonv:native = false\n";
568 print CF "\tnetbios name = client\n";
569 if (defined($vars->{DOMAIN})) {
570 print CF "\tworkgroup = $vars->{DOMAIN}\n";
572 if (defined($vars->{REALM})) {
573 print CF "\trealm = $vars->{REALM}\n";
575 if ($opt_socket_wrapper) {
576 print CF "\tinterfaces = $interfaces\n";
578 print CF "
579 private dir = $clientdir/private
580 lock dir = $clientdir/lockdir
581 ncalrpc dir = $clientdir/ncalrpcdir
582 name resolve order = bcast file
583 panic action = $RealBin/gdb_backtrace \%PID\% \%PROG\%
584 max xmit = 32K
585 notify:inotify = false
586 ldb:nosync = true
587 system:anonymous = true
588 client lanman auth = Yes
589 log level = 1
590 torture:basedir = $clientdir
591 #We don't want to pass our self-tests if the PAC code is wrong
592 gensec:require_pac = true
593 modules dir = $ENV{LD_SAMBA_MODULE_PATH}
594 setup directory = ./setup
595 resolv:host file = $prefix_abs/dns_host_file
596 #We don't want to run 'speed' tests for very long
597 torture:timelimit = 1
599 close(CF);
602 my @todo = ();
604 my $testsdir = "$srcdir/selftest";
606 sub should_run_test($)
608 my $name = shift;
609 if ($#tests == -1) {
610 return 1;
612 for (my $i=0; $i <= $#tests; $i++) {
613 if ($name =~ /$tests[$i]/i) {
614 return 1;
617 return 0;
620 sub read_testlist($)
622 my ($filename) = @_;
624 my @ret = ();
625 open(IN, $filename) or die("Unable to open $filename: $!");
627 while (<IN>) {
628 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
629 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
630 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
631 my $name = <IN>;
632 $name =~ s/\n//g;
633 my $env = <IN>;
634 $env =~ s/\n//g;
635 my $cmdline = <IN>;
636 $cmdline =~ s/\n//g;
637 if (should_run_test($name) == 1) {
638 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
640 } else {
641 print;
644 close(IN) or die("Error creating recipe");
645 return @ret;
648 if ($#testlists == -1) {
649 die("No testlists specified");
652 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
653 if ($opt_socket_wrapper) {
654 $ENV{SELFTEST_INTERFACES} = $interfaces;
655 } else {
656 $ENV{SELFTEST_INTERFACES} = "";
658 if ($opt_verbose) {
659 $ENV{SELFTEST_VERBOSE} = "1";
660 } else {
661 $ENV{SELFTEST_VERBOSE} = "";
663 if ($opt_quick) {
664 $ENV{SELFTEST_QUICK} = "1";
665 } else {
666 $ENV{SELFTEST_QUICK} = "";
668 $ENV{SELFTEST_TARGET} = $opt_target;
669 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
671 my @available = ();
672 foreach my $fn (@testlists) {
673 foreach (read_testlist($fn)) {
674 my $name = $$_[0];
675 next if (@includes and not defined(find_in_list(\@includes, $name)));
676 push (@available, $_);
680 my $restricted = undef;
681 my $restricted_used = {};
683 if ($opt_load_list) {
684 $restricted = [];
685 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
686 while (<LOAD_LIST>) {
687 chomp;
688 push (@$restricted, $_);
690 close(LOAD_LIST);
693 my $individual_tests = undef;
694 $individual_tests = {};
696 foreach my $testsuite (@available) {
697 my $name = $$testsuite[0];
698 my $skipreason = skip($name);
699 if (defined($restricted)) {
700 # Find the testsuite for this test
701 my $match = undef;
702 foreach my $r (@$restricted) {
703 if ($r eq $name) {
704 $individual_tests->{$name} = [];
705 $match = $r;
706 $restricted_used->{$r} = 1;
707 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
708 push(@{$individual_tests->{$name}}, $r);
709 $match = $r;
710 $restricted_used->{$r} = 1;
713 if ($match) {
714 if (defined($skipreason)) {
715 Subunit::skip_testsuite($name, $skipreason);
716 } else {
717 push(@todo, $testsuite);
720 } elsif (defined($skipreason)) {
721 Subunit::skip_testsuite($name, $skipreason);
722 } else {
723 push(@todo, $testsuite);
727 if (defined($restricted)) {
728 foreach (@$restricted) {
729 unless (defined($restricted_used->{$_})) {
730 print "No test or testsuite found matching $_\n";
733 } elsif ($#todo == -1) {
734 print STDERR "No tests to run\n";
735 exit(1);
738 my $suitestotal = $#todo + 1;
740 Subunit::progress($suitestotal);
741 Subunit::report_time(time());
743 my $i = 0;
744 $| = 1;
746 my %running_envs = ();
748 sub get_running_env($)
750 my ($name) = @_;
752 my $envname = $name;
754 $envname =~ s/:.*//;
756 return $running_envs{$envname};
759 my @exported_envvars = (
760 # domain stuff
761 "DOMAIN",
762 "REALM",
764 # domain controller stuff
765 "DC_SERVER",
766 "DC_SERVER_IP",
767 "DC_NETBIOSNAME",
768 "DC_NETBIOSALIAS",
770 # domain member
771 "MEMBER_SERVER",
772 "MEMBER_SERVER_IP",
773 "MEMBER_NETBIOSNAME",
774 "MEMBER_NETBIOSALIAS",
776 # rpc proxy controller stuff
777 "RPC_PROXY_SERVER",
778 "RPC_PROXY_SERVER_IP",
779 "RPC_PROXY_NETBIOSNAME",
780 "RPC_PROXY_NETBIOSALIAS",
782 # domain controller stuff for Vampired DC
783 "VAMPIRE_DC_SERVER",
784 "VAMPIRE_DC_SERVER_IP",
785 "VAMPIRE_DC_NETBIOSNAME",
786 "VAMPIRE_DC_NETBIOSALIAS",
788 # server stuff
789 "SERVER",
790 "SERVER_IP",
791 "NETBIOSNAME",
792 "NETBIOSALIAS",
794 # user stuff
795 "USERNAME",
796 "USERID",
797 "PASSWORD",
798 "DC_USERNAME",
799 "DC_PASSWORD",
801 # misc stuff
802 "KRB5_CONFIG",
803 "WINBINDD_SOCKET_DIR",
804 "WINBINDD_PRIV_PIPE_DIR",
805 "LOCAL_PATH"
808 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
809 my $signame = shift;
810 teardown_env($_) foreach(keys %running_envs);
811 die("Received signal $signame");
814 sub setup_env($$)
816 my ($name, $prefix) = @_;
818 my $testenv_vars = undef;
820 my $envname = $name;
821 my $option = $name;
823 $envname =~ s/:.*//;
824 $option =~ s/^[^:]*//;
825 $option =~ s/^://;
827 $option = "client" if $option eq "";
829 if ($envname eq "none") {
830 $testenv_vars = {};
831 } elsif (defined(get_running_env($envname))) {
832 $testenv_vars = get_running_env($envname);
833 if (not $target->check_env($testenv_vars)) {
834 print $target->getlog_env($testenv_vars);
835 $testenv_vars = undef;
837 } else {
838 $testenv_vars = $target->setup_env($envname, $prefix);
841 return undef unless defined($testenv_vars);
843 $running_envs{$envname} = $testenv_vars;
845 if ($option eq "local") {
846 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
847 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
848 } elsif ($option eq "client") {
849 SocketWrapper::set_default_iface(11);
850 write_clientconf($conffile, $clientdir, $testenv_vars);
851 $ENV{SMB_CONF_PATH} = $conffile;
852 } else {
853 die("Unknown option[$option] for envname[$envname]");
856 foreach (@exported_envvars) {
857 if (defined($testenv_vars->{$_})) {
858 $ENV{$_} = $testenv_vars->{$_};
859 } else {
860 delete $ENV{$_};
864 return $testenv_vars;
867 sub exported_envvars_str($)
869 my ($testenv_vars) = @_;
870 my $out = "";
872 foreach (@exported_envvars) {
873 next unless defined($testenv_vars->{$_});
874 $out .= $_."=".$testenv_vars->{$_}."\n";
877 return $out;
880 sub getlog_env($)
882 my ($envname) = @_;
883 return "" if ($envname eq "none");
884 return $target->getlog_env(get_running_env($envname));
887 sub check_env($)
889 my ($envname) = @_;
890 return 1 if ($envname eq "none");
891 return $target->check_env(get_running_env($envname));
894 sub teardown_env($)
896 my ($envname) = @_;
897 return if ($envname eq "none");
898 $target->teardown_env(get_running_env($envname));
899 delete $running_envs{$envname};
902 # This 'global' file needs to be empty when we start
903 unlink("$prefix_abs/dns_host_file");
905 if ($opt_testenv) {
906 my $testenv_name = $ENV{SELFTEST_TESTENV};
907 $testenv_name = $testenv_default unless defined($testenv_name);
909 my $testenv_vars = setup_env($testenv_name, $prefix);
911 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
913 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
915 my $envvarstr = exported_envvars_str($testenv_vars);
917 my $term = ($ENV{TERMINAL} or "xterm");
918 system("$term -e 'echo -e \"
919 Welcome to the Samba4 Test environment '$testenv_name'
921 This matches the client environment used in make test
922 server is pid `cat \$PIDDIR/samba.pid`
924 Some useful environment variables:
925 TORTURE_OPTIONS=\$TORTURE_OPTIONS
926 SMB_CONF_PATH=\$SMB_CONF_PATH
928 $envvarstr
929 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
930 teardown_env($testenv_name);
931 } else {
932 foreach (@todo) {
933 $i++;
934 my $cmd = $$_[2];
935 my $name = $$_[0];
936 my $envname = $$_[1];
938 my $envvars = setup_env($envname, $prefix);
939 if (not defined($envvars)) {
940 Subunit::start_testsuite($name);
941 Subunit::end_testsuite($name, "error",
942 "unable to set up environment $envname");
943 next;
946 # Generate a file with the individual tests to run, if the
947 # test runner for this test suite supports it.
948 if ($individual_tests and $individual_tests->{$name}) {
949 if ($$_[3]) {
950 my ($fh, $listid_file) = tempfile(UNLINK => 0);
951 foreach my $test (@{$individual_tests->{$name}}) {
952 print $fh substr($test, length($name)+1) . "\n";
954 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
955 } elsif ($$_[4]) {
956 $cmd =~ s/\s+[^\s]+\s*$//;
957 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
961 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
963 teardown_env($envname) if ($opt_resetup_env);
967 print "\n";
969 teardown_env($_) foreach (keys %running_envs);
971 my $failed = 0;
973 # if there were any valgrind failures, show them
974 foreach (<$prefix/valgrind.log*>) {
975 next unless (-s $_);
976 print "VALGRIND FAILURE\n";
977 $failed++;
978 system("cat $_");
980 exit 0;