source3/lib/util_tdb.c: operation timeout support for TDB2.
[Samba/gbeck.git] / selftest / selftest.pl
blob23434fc86ce3b9c0de8248ce136d032beed63b69
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] [--bindir=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<--bindir=DIR>
49 Built binaries 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;
138 Time::HiRes->import("time");
140 if ($@) {
141 print "You don't have Time::Hires installed !\n";
144 my $opt_help = 0;
145 my $opt_target = "samba4";
146 my $opt_quick = 0;
147 my $opt_socket_wrapper = 0;
148 my $opt_socket_wrapper_pcap = undef;
149 my $opt_socket_wrapper_keep_pcap = undef;
150 my $opt_one = 0;
151 my @opt_exclude = ();
152 my @opt_include = ();
153 my $opt_verbose = 0;
154 my $opt_image = undef;
155 my $opt_testenv = 0;
156 my $opt_list = 0;
157 my $ldap = undef;
158 my $opt_resetup_env = undef;
159 my $opt_binary_mapping = "";
160 my $opt_load_list = undef;
161 my @testlists = ();
163 my $srcdir = ".";
164 my $bindir = "./bin";
165 my $exeext = "";
166 my $prefix = "./st";
168 my @includes = ();
169 my @excludes = ();
171 sub pipe_handler {
172 my $sig = shift @_;
173 print STDERR "Exiting early because of SIGPIPE.\n";
174 exit(1);
177 $SIG{PIPE} = \&pipe_handler;
179 sub find_in_list($$)
181 my ($list, $fullname) = @_;
183 foreach (@$list) {
184 if ($fullname =~ /$$_[0]/) {
185 return ($$_[1]) if ($$_[1]);
186 return "";
190 return undef;
193 sub skip($)
195 my ($name) = @_;
197 return find_in_list(\@excludes, $name);
200 sub getlog_env($);
202 sub setup_pcap($)
204 my ($name) = @_;
206 return unless ($opt_socket_wrapper_pcap);
207 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
209 my $fname = $name;
210 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
212 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
214 SocketWrapper::setup_pcap($pcap_file);
216 return $pcap_file;
219 sub cleanup_pcap($$)
221 my ($pcap_file, $exitcode) = @_;
223 return unless ($opt_socket_wrapper_pcap);
224 return if ($opt_socket_wrapper_keep_pcap);
225 return unless ($exitcode == 0);
226 return unless defined($pcap_file);
228 unlink($pcap_file);
231 # expand strings from %ENV
232 sub expand_environment_strings($)
234 my $s = shift;
235 # we use a reverse sort so we do the longer ones first
236 foreach my $k (sort { $b cmp $a } keys %ENV) {
237 $s =~ s/\$$k/$ENV{$k}/g;
239 return $s;
242 sub run_testsuite($$$$$)
244 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
245 my $pcap_file = setup_pcap($name);
247 Subunit::start_testsuite($name);
248 Subunit::progress_push();
249 Subunit::report_time(time());
250 system($cmd);
251 Subunit::report_time(time());
252 Subunit::progress_pop();
254 if ($? == -1) {
255 Subunit::progress_pop();
256 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
257 exit(1);
258 } elsif ($? & 127) {
259 Subunit::end_testsuite($name, "error",
260 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
261 exit(1);
264 my $exitcode = $? >> 8;
266 my $envlog = getlog_env($envname);
267 if ($envlog ne "") {
268 print "envlog: $envlog\n";
271 print "command: $cmd\n";
272 printf "expanded command: %s\n", expand_environment_strings($cmd);
274 if ($exitcode == 0) {
275 Subunit::end_testsuite($name, "success");
276 } else {
277 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
280 cleanup_pcap($pcap_file, $exitcode);
282 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
283 print "PCAP FILE: $pcap_file\n";
286 if ($exitcode != 0) {
287 exit(1) if ($opt_one);
290 return $exitcode;
293 sub ShowHelp()
295 print "Samba test runner
296 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
297 Copyright (C) Stefan Metzmacher <metze\@samba.org>
299 Usage: $Script [OPTIONS] TESTNAME-REGEX
301 Generic options:
302 --help this help page
303 --target=samba[34]|win|kvm Samba version to target
304 --testlist=FILE file to read available tests from
306 Paths:
307 --prefix=DIR prefix to run tests in [st]
308 --srcdir=DIR source directory [.]
309 --bindir=DIR binaries directory [./bin]
310 --exeext=EXT executable extention []
312 Target Specific:
313 --socket-wrapper-pcap save traffic to pcap directories
314 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
315 failed
316 --socket-wrapper enable socket wrapper
318 Samba4 Specific:
319 --ldap=openldap|fedora-ds back samba onto specified ldap server
321 Kvm Specific:
322 --image=PATH path to KVM image
324 Behaviour:
325 --quick run quick overall test
326 --one abort when the first test fails
327 --verbose be verbose
328 --testenv run a shell in the requested test environment
329 --list list available tests
331 exit(0);
334 my $result = GetOptions (
335 'help|h|?' => \$opt_help,
336 'target=s' => \$opt_target,
337 'prefix=s' => \$prefix,
338 'socket-wrapper' => \$opt_socket_wrapper,
339 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
340 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
341 'quick' => \$opt_quick,
342 'one' => \$opt_one,
343 'exclude=s' => \@opt_exclude,
344 'include=s' => \@opt_include,
345 'srcdir=s' => \$srcdir,
346 'bindir=s' => \$bindir,
347 'exeext=s' => \$exeext,
348 'verbose' => \$opt_verbose,
349 'testenv' => \$opt_testenv,
350 'list' => \$opt_list,
351 'ldap:s' => \$ldap,
352 'resetup-environment' => \$opt_resetup_env,
353 'image=s' => \$opt_image,
354 'testlist=s' => \@testlists,
355 'load-list=s' => \$opt_load_list,
356 'binary-mapping=s' => \$opt_binary_mapping
359 exit(1) if (not $result);
361 ShowHelp() if ($opt_help);
363 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
365 # we want unbuffered output
366 $| = 1;
368 my @tests = @ARGV;
370 # quick hack to disable rpc validation when using valgrind - its way too slow
371 unless (defined($ENV{VALGRIND})) {
372 $ENV{VALIDATE} = "validate";
373 $ENV{MALLOC_CHECK_} = 2;
376 # make all our python scripts unbuffered
377 $ENV{PYTHONUNBUFFERED} = 1;
379 my $bindir_abs = abs_path($bindir);
381 # Backwards compatibility:
382 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
383 if (defined($ENV{FEDORA_DS_ROOT})) {
384 $ldap = "fedora-ds";
385 } else {
386 $ldap = "openldap";
390 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
391 if ($ldap) {
392 # LDAP is slow
393 $torture_maxtime *= 2;
396 $prefix =~ s+//+/+;
397 $prefix =~ s+/./+/+;
398 $prefix =~ s+/$++;
400 die("using an empty prefix isn't allowed") unless $prefix ne "";
402 # Ensure we have the test prefix around.
404 # We need restrictive
405 # permissions on this as some subdirectories in this tree will have
406 # wider permissions (ie 0777) and this would allow other users on the
407 # host to subvert the test process.
408 mkdir($prefix, 0700) unless -d $prefix;
409 chmod 0700, $prefix;
411 my $prefix_abs = abs_path($prefix);
412 my $tmpdir_abs = abs_path("$prefix/tmp");
413 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
415 my $srcdir_abs = abs_path($srcdir);
417 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
418 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
420 $ENV{PREFIX} = $prefix;
421 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
422 $ENV{PREFIX_ABS} = $prefix_abs;
423 $ENV{SRCDIR} = $srcdir;
424 $ENV{SRCDIR_ABS} = $srcdir_abs;
425 $ENV{BINDIR} = $bindir_abs;
426 $ENV{EXEEXT} = $exeext;
428 my $tls_enabled = not $opt_quick;
429 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
431 sub prefix_pathvar($$)
433 my ($name, $newpath) = @_;
434 if (defined($ENV{$name})) {
435 $ENV{$name} = "$newpath:$ENV{$name}";
436 } else {
437 $ENV{$name} = $newpath;
440 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
441 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
443 if ($opt_socket_wrapper_keep_pcap) {
444 # Socket wrapper keep pcap implies socket wrapper pcap
445 $opt_socket_wrapper_pcap = 1;
448 if ($opt_socket_wrapper_pcap) {
449 # Socket wrapper pcap implies socket wrapper
450 $opt_socket_wrapper = 1;
453 my $socket_wrapper_dir;
454 if ($opt_socket_wrapper) {
455 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
456 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
457 } else {
458 unless ($< == 0) {
459 print "WARNING: Not using socket wrapper, but also not running as root. Will not be able to listen on proper ports\n";
463 my $target;
464 my $testenv_default = "none";
466 my %binary_mapping = {};
467 if ($opt_binary_mapping) {
468 my @binmapping_list = split(/,/, $opt_binary_mapping);
469 foreach my $mapping (@binmapping_list) {
470 my ($bin, $map) = split(/\:/, $mapping);
471 $binary_mapping{$bin} = $map;
475 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
477 # After this many seconds, the server will self-terminate. All tests
478 # must terminate in this time, and testenv will only stay alive this
479 # long
481 my $server_maxtime = 7500;
482 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
483 $server_maxtime = $ENV{SMBD_MAXTIME};
486 if ($opt_target eq "samba") {
487 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
488 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
490 $testenv_default = "all";
491 require target::Samba;
492 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $exeext, $server_maxtime);
493 } elsif ($opt_target eq "samba4") {
494 $testenv_default = "all";
495 require target::Samba4;
496 $target = new Samba4($bindir, \%binary_mapping, $ldap, $srcdir, $exeext, $server_maxtime);
497 } elsif ($opt_target eq "samba3") {
498 if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
499 die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'. Exiting....");
501 $testenv_default = "member";
502 require target::Samba3;
503 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $exeext, $server_maxtime);
504 } elsif ($opt_target eq "win") {
505 die("Windows tests will not run with socket wrapper enabled.")
506 if ($opt_socket_wrapper);
507 $testenv_default = "dc";
508 require target::Windows;
509 $target = new Windows();
510 } elsif ($opt_target eq "kvm") {
511 die("Kvm tests will not run with socket wrapper enabled.")
512 if ($opt_socket_wrapper);
513 require target::Kvm;
514 die("No image specified") unless ($opt_image);
515 $target = new Kvm($opt_image, undef);
519 # Start a Virtual Distributed Ethernet Switch
520 # Returns the pid of the switch.
522 sub start_vde_switch($)
524 my ($path) = @_;
526 system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
528 open(PID, "$path/vde.pid");
529 <PID> =~ /([0-9]+)/;
530 my $pid = $1;
531 close(PID);
533 return $pid;
536 # Stop a Virtual Distributed Ethernet Switch
537 sub stop_vde_switch($)
539 my ($pid) = @_;
540 kill 9, $pid;
543 sub read_test_regexes($)
545 my ($name) = @_;
546 my @ret = ();
547 open(LF, "<$name") or die("unable to read $name: $!");
548 while (<LF>) {
549 chomp;
550 next if (/^#/);
551 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
552 push (@ret, [$1, $4]);
553 } else {
554 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
555 push (@ret, [$_, undef]);
558 close(LF);
559 return @ret;
562 foreach (@opt_exclude) {
563 push (@excludes, read_test_regexes($_));
566 foreach (@opt_include) {
567 push (@includes, read_test_regexes($_));
570 my $interfaces = join(',', ("127.0.0.11/8",
571 "127.0.0.12/8",
572 "127.0.0.13/8",
573 "127.0.0.14/8",
574 "127.0.0.15/8",
575 "127.0.0.16/8"));
577 my $clientdir = "$prefix_abs/client";
579 my $conffile = "$clientdir/client.conf";
580 $ENV{SMB_CONF_PATH} = $conffile;
582 sub write_clientconf($$$)
584 my ($conffile, $clientdir, $vars) = @_;
586 mkdir("$clientdir", 0777) unless -d "$clientdir";
588 if ( -d "$clientdir/private" ) {
589 unlink <$clientdir/private/*>;
590 } else {
591 mkdir("$clientdir/private", 0777);
594 if ( -d "$clientdir/lockdir" ) {
595 unlink <$clientdir/lockdir/*>;
596 } else {
597 mkdir("$clientdir/lockdir", 0777);
600 # this is ugly, but the ncalrpcdir needs exactly 0755
601 # otherwise tests fail.
602 my $mask = umask;
603 umask 0022;
604 if ( -d "$clientdir/ncalrpcdir/np" ) {
605 unlink <$clientdir/ncalrpcdir/np/*>;
606 rmdir <$clientdir/ncalrpcdir/np>;
608 if ( -d "$clientdir/ncalrpcdir" ) {
609 unlink <$clientdir/ncalrpcdir/*>;
610 rmdir <$clientdir/ncalrpcdir>;
612 mkdir("$clientdir/ncalrpcdir", 0755);
613 umask $mask;
615 open(CF, ">$conffile");
616 print CF "[global]\n";
617 print CF "\tnetbios name = client\n";
618 if (defined($vars->{DOMAIN})) {
619 print CF "\tworkgroup = $vars->{DOMAIN}\n";
621 if (defined($vars->{REALM})) {
622 print CF "\trealm = $vars->{REALM}\n";
624 if ($opt_socket_wrapper) {
625 print CF "\tinterfaces = $interfaces\n";
627 print CF "
628 private dir = $clientdir/private
629 lock dir = $clientdir/lockdir
630 ncalrpc dir = $clientdir/ncalrpcdir
631 name resolve order = file bcast
632 panic action = $RealBin/gdb_backtrace \%PID\% \%PROG\%
633 max xmit = 32K
634 notify:inotify = false
635 ldb:nosync = true
636 system:anonymous = true
637 client lanman auth = Yes
638 log level = 1
639 torture:basedir = $clientdir
640 #We don't want to pass our self-tests if the PAC code is wrong
641 gensec:require_pac = true
642 resolv:host file = $prefix_abs/dns_host_file
643 #We don't want to run 'speed' tests for very long
644 torture:timelimit = 1
646 close(CF);
649 my @todo = ();
651 sub should_run_test($)
653 my $name = shift;
654 if ($#tests == -1) {
655 return 1;
657 for (my $i=0; $i <= $#tests; $i++) {
658 if ($name =~ /$tests[$i]/i) {
659 return 1;
662 return 0;
665 sub read_testlist($)
667 my ($filename) = @_;
669 my @ret = ();
670 open(IN, $filename) or die("Unable to open $filename: $!");
672 while (<IN>) {
673 if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
674 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
675 my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
676 my $name = <IN>;
677 $name =~ s/\n//g;
678 my $env = <IN>;
679 $env =~ s/\n//g;
680 my $cmdline = <IN>;
681 $cmdline =~ s/\n//g;
682 if (should_run_test($name) == 1) {
683 push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
685 } else {
686 print;
689 close(IN) or die("Error creating recipe");
690 return @ret;
693 if ($#testlists == -1) {
694 die("No testlists specified");
697 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
698 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
699 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
700 if ($opt_socket_wrapper) {
701 $ENV{SELFTEST_INTERFACES} = $interfaces;
702 } else {
703 $ENV{SELFTEST_INTERFACES} = "";
705 if ($opt_verbose) {
706 $ENV{SELFTEST_VERBOSE} = "1";
707 } else {
708 $ENV{SELFTEST_VERBOSE} = "";
710 if ($opt_quick) {
711 $ENV{SELFTEST_QUICK} = "1";
712 } else {
713 $ENV{SELFTEST_QUICK} = "";
715 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
717 my @available = ();
718 foreach my $fn (@testlists) {
719 foreach (read_testlist($fn)) {
720 my $name = $$_[0];
721 next if (@includes and not defined(find_in_list(\@includes, $name)));
722 push (@available, $_);
726 my $restricted = undef;
727 my $restricted_used = {};
729 if ($opt_load_list) {
730 $restricted = [];
731 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
732 while (<LOAD_LIST>) {
733 chomp;
734 push (@$restricted, $_);
736 close(LOAD_LIST);
739 my $individual_tests = undef;
740 $individual_tests = {};
742 foreach my $testsuite (@available) {
743 my $name = $$testsuite[0];
744 my $skipreason = skip($name);
745 if (defined($restricted)) {
746 # Find the testsuite for this test
747 my $match = undef;
748 foreach my $r (@$restricted) {
749 if ($r eq $name) {
750 $individual_tests->{$name} = [];
751 $match = $r;
752 $restricted_used->{$r} = 1;
753 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
754 push(@{$individual_tests->{$name}}, $r);
755 $match = $r;
756 $restricted_used->{$r} = 1;
759 if ($match) {
760 if (defined($skipreason)) {
761 Subunit::skip_testsuite($name, $skipreason);
762 } else {
763 push(@todo, $testsuite);
766 } elsif (defined($skipreason)) {
767 Subunit::skip_testsuite($name, $skipreason);
768 } else {
769 push(@todo, $testsuite);
773 if (defined($restricted)) {
774 foreach (@$restricted) {
775 unless (defined($restricted_used->{$_})) {
776 print "No test or testsuite found matching $_\n";
779 } elsif ($#todo == -1) {
780 print STDERR "No tests to run\n";
781 exit(1);
784 my $suitestotal = $#todo + 1;
786 Subunit::progress($suitestotal);
787 Subunit::report_time(time());
789 my $i = 0;
790 $| = 1;
792 my %running_envs = ();
794 sub get_running_env($)
796 my ($name) = @_;
798 my $envname = $name;
800 $envname =~ s/:.*//;
802 return $running_envs{$envname};
805 my @exported_envvars = (
806 # domain stuff
807 "DOMAIN",
808 "REALM",
810 # domain controller stuff
811 "DC_SERVER",
812 "DC_SERVER_IP",
813 "DC_NETBIOSNAME",
814 "DC_NETBIOSALIAS",
816 # domain member
817 "MEMBER_SERVER",
818 "MEMBER_SERVER_IP",
819 "MEMBER_NETBIOSNAME",
820 "MEMBER_NETBIOSALIAS",
822 # rpc proxy controller stuff
823 "RPC_PROXY_SERVER",
824 "RPC_PROXY_SERVER_IP",
825 "RPC_PROXY_NETBIOSNAME",
826 "RPC_PROXY_NETBIOSALIAS",
828 # domain controller stuff for Vampired DC
829 "VAMPIRE_DC_SERVER",
830 "VAMPIRE_DC_SERVER_IP",
831 "VAMPIRE_DC_NETBIOSNAME",
832 "VAMPIRE_DC_NETBIOSALIAS",
834 # server stuff
835 "SERVER",
836 "SERVER_IP",
837 "NETBIOSNAME",
838 "NETBIOSALIAS",
840 # user stuff
841 "USERNAME",
842 "USERID",
843 "PASSWORD",
844 "DC_USERNAME",
845 "DC_PASSWORD",
847 # misc stuff
848 "KRB5_CONFIG",
849 "WINBINDD_SOCKET_DIR",
850 "WINBINDD_PRIV_PIPE_DIR",
851 "NMBD_SOCKET_DIR",
852 "LOCAL_PATH"
855 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
856 my $signame = shift;
857 teardown_env($_) foreach(keys %running_envs);
858 die("Received signal $signame");
861 sub setup_env($$)
863 my ($name, $prefix) = @_;
865 my $testenv_vars = undef;
867 my $envname = $name;
868 my $option = $name;
870 $envname =~ s/:.*//;
871 $option =~ s/^[^:]*//;
872 $option =~ s/^://;
874 $option = "client" if $option eq "";
876 if ($envname eq "none") {
877 $testenv_vars = {};
878 } elsif (defined(get_running_env($envname))) {
879 $testenv_vars = get_running_env($envname);
880 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
881 print $testenv_vars->{target}->getlog_env($testenv_vars);
882 $testenv_vars = undef;
884 } else {
885 $testenv_vars = $target->setup_env($envname, $prefix);
886 if (defined($testenv_vars) && not defined($testenv_vars->{target})) {
887 $testenv_vars->{target} = $target;
889 if (not defined($testenv_vars)) {
890 warn("$opt_target can't provide environment '$envname'");
895 return undef unless defined($testenv_vars);
897 $running_envs{$envname} = $testenv_vars;
899 if ($option eq "local") {
900 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
901 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
902 } elsif ($option eq "client") {
903 SocketWrapper::set_default_iface(11);
904 write_clientconf($conffile, $clientdir, $testenv_vars);
905 $ENV{SMB_CONF_PATH} = $conffile;
906 } else {
907 die("Unknown option[$option] for envname[$envname]");
910 foreach (@exported_envvars) {
911 if (defined($testenv_vars->{$_})) {
912 $ENV{$_} = $testenv_vars->{$_};
913 } else {
914 delete $ENV{$_};
918 return $testenv_vars;
921 sub exported_envvars_str($)
923 my ($testenv_vars) = @_;
924 my $out = "";
926 foreach (@exported_envvars) {
927 next unless defined($testenv_vars->{$_});
928 $out .= $_."=".$testenv_vars->{$_}."\n";
931 return $out;
934 sub getlog_env($)
936 my ($envname) = @_;
937 return "" if ($envname eq "none");
938 my $env = get_running_env($envname);
939 return $env->{target}->getlog_env($env);
942 sub check_env($)
944 my ($envname) = @_;
945 return 1 if ($envname eq "none");
946 my $env = get_running_env($envname);
947 return $env->{target}->check_env($env);
950 sub teardown_env($)
952 my ($envname) = @_;
953 return if ($envname eq "none");
954 my $env = get_running_env($envname);
955 $env->{target}->teardown_env($env);
956 delete $running_envs{$envname};
959 # This 'global' file needs to be empty when we start
960 unlink("$prefix_abs/dns_host_file");
962 if ($opt_testenv) {
963 my $testenv_name = $ENV{SELFTEST_TESTENV};
964 $testenv_name = $testenv_default unless defined($testenv_name);
966 my $testenv_vars = setup_env($testenv_name, $prefix);
968 die("Unable to setup environment $testenv_name") unless ($testenv_vars);
970 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
971 $ENV{ENVNAME} = $testenv_name;
973 my $envvarstr = exported_envvars_str($testenv_vars);
975 my $term = ($ENV{TERMINAL} or "xterm -e");
976 system("$term 'echo -e \"
977 Welcome to the Samba4 Test environment '$testenv_name'
979 This matches the client environment used in make test
980 server is pid `cat \$PIDDIR/samba.pid`
982 Some useful environment variables:
983 TORTURE_OPTIONS=\$TORTURE_OPTIONS
984 SMB_CONF_PATH=\$SMB_CONF_PATH
986 $envvarstr
987 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
988 teardown_env($testenv_name);
989 } elsif ($opt_list) {
990 foreach (@todo) {
991 my $cmd = $$_[2];
992 my $name = $$_[0];
993 my $envname = $$_[1];
995 unless($cmd =~ /\$LISTOPT/) {
996 warn("Unable to list tests in $name");
997 next;
1000 $cmd =~ s/\$LISTOPT/--list/g;
1002 system($cmd);
1004 if ($? == -1) {
1005 die("Unable to run $cmd: $!");
1006 } elsif ($? & 127) {
1007 die(snprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1010 my $exitcode = $? >> 8;
1011 if ($exitcode != 0) {
1012 die("$cmd exited with exit code $exitcode");
1015 } else {
1016 foreach (@todo) {
1017 $i++;
1018 my $cmd = $$_[2];
1019 my $name = $$_[0];
1020 my $envname = $$_[1];
1022 my $envvars = setup_env($envname, $prefix);
1023 if (not defined($envvars)) {
1024 Subunit::start_testsuite($name);
1025 Subunit::end_testsuite($name, "error",
1026 "unable to set up environment $envname - exiting");
1027 next;
1030 # Generate a file with the individual tests to run, if the
1031 # test runner for this test suite supports it.
1032 if ($individual_tests and $individual_tests->{$name}) {
1033 if ($$_[3]) {
1034 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1035 foreach my $test (@{$individual_tests->{$name}}) {
1036 print $fh substr($test, length($name)+1) . "\n";
1038 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1039 } elsif ($$_[4]) {
1040 $cmd =~ s/\s+[^\s]+\s*$//;
1041 $cmd .= " " . join(' ', @{$individual_tests->{$name}});
1045 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1047 teardown_env($envname) if ($opt_resetup_env);
1051 print "\n";
1053 teardown_env($_) foreach (keys %running_envs);
1055 my $failed = 0;
1057 # if there were any valgrind failures, show them
1058 foreach (<$prefix/valgrind.log*>) {
1059 next unless (-s $_);
1060 print "VALGRIND FAILURE\n";
1061 $failed++;
1062 system("cat $_");
1064 exit 0;