selftest: Support multiple instances of $LISTOPT.
[samba.git] / selftest / selftest.pl
index 254c347b807a4c3fe03d18ecf2efaee9f904f7df..9759220c685b732d12a89ef180da6e169b93ca60 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # Bootstrap Samba and run a number of tests against it.
-# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>
+# Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
 
 # This program is free software; you can redistribute it and/or modify
@@ -135,9 +135,10 @@ use SocketWrapper;
 
 eval {
 require Time::HiRes;
+Time::HiRes->import("time");
 };
-unless ($@) {
-       use Time::HiRes qw(time);
+if ($@) {
+       print "You don't have Time::Hires installed !\n";
 }
 
 my $opt_help = 0;
@@ -152,6 +153,7 @@ my @opt_include = ();
 my $opt_verbose = 0;
 my $opt_image = undef;
 my $opt_testenv = 0;
+my $opt_list = 0;
 my $ldap = undef;
 my $opt_resetup_env = undef;
 my $opt_bindir = undef;
@@ -166,6 +168,14 @@ my $prefix = "./st";
 my @includes = ();
 my @excludes = ();
 
+sub pipe_handler {
+       my $sig = shift @_;
+       print STDERR "Exiting early because of SIGPIPE.\n";
+       exit(1);
+}
+
+$SIG{PIPE} = \&pipe_handler;
+
 sub find_in_list($$)
 {
        my ($list, $fullname) = @_;
@@ -244,11 +254,11 @@ sub run_testsuite($$$$$)
        if ($? == -1) {
                Subunit::progress_pop();
                Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
-               return 0;
+               exit(1);
        } elsif ($? & 127) {
                Subunit::end_testsuite($name, "error",
                        sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
-               return 0;
+               exit(1);
        }
 
        my $exitcode = $? >> 8;
@@ -316,7 +326,8 @@ Behaviour:
  --quick                    run quick overall test
  --one                      abort when the first test fails
  --verbose                  be verbose
- --analyse-cmd CMD          command to run after each test
+ --testenv                  run a shell in the requested test environment
+ --list                     list available tests
 ";
        exit(0);
 }
@@ -337,6 +348,7 @@ my $result = GetOptions (
                'exeext=s' => \$exeext,
                'verbose' => \$opt_verbose,
                'testenv' => \$opt_testenv,
+               'list' => \$opt_list,
                'ldap:s' => \$ldap,
                'resetup-environment' => \$opt_resetup_env,
                'bindir:s' => \$opt_bindir,
@@ -349,6 +361,8 @@ exit(1) if (not $result);
 
 ShowHelp() if ($opt_help);
 
+die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
+
 # we want unbuffered output
 $| = 1;
 
@@ -391,6 +405,9 @@ die("using an empty prefix isn't allowed") unless $prefix ne "";
 mkdir($prefix, 0777) unless -d $prefix;
 
 my $prefix_abs = abs_path($prefix);
+my $tmpdir_abs = abs_path("$prefix/tmp");
+mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
+
 my $srcdir_abs = abs_path($srcdir);
 my $builddir_abs = abs_path($builddir);
 
@@ -408,8 +425,7 @@ $ENV{EXEEXT} = $exeext;
 
 my $tls_enabled = not $opt_quick;
 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
-$ENV{LDB_MODULES_PATH} = "$bindir_abs/modules/ldb";
-$ENV{LD_SAMBA_MODULE_PATH} = "$bindir_abs/modules";
+
 sub prefix_pathvar($$)
 {
        my ($name, $newpath) = @_;
@@ -590,7 +606,6 @@ sub write_clientconf($$$)
        torture:basedir = $clientdir
 #We don't want to pass our self-tests if the PAC code is wrong
        gensec:require_pac = true
-       modules dir = $ENV{LD_SAMBA_MODULE_PATH}
        setup directory = ./setup
        resolv:host file = $prefix_abs/dns_host_file
 #We don't want to run 'speed' tests for very long
@@ -650,6 +665,7 @@ if ($#testlists == -1) {
 }
 
 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
+$ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
 if ($opt_socket_wrapper) {
        $ENV{SELFTEST_INTERFACES} = $interfaces;
 } else {
@@ -911,11 +927,12 @@ if ($opt_testenv) {
        die("Unable to setup environment $testenv_name") unless ($testenv_vars);
 
        $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
+       $ENV{ENVNAME} = $testenv_name;
 
        my $envvarstr = exported_envvars_str($testenv_vars);
 
-       my $term = ($ENV{TERMINAL} or "xterm");
-       system("$term -e 'echo -e \"
+       my $term = ($ENV{TERMINAL} or "xterm -e");
+       system("$term 'echo -e \"
 Welcome to the Samba4 Test environment '$testenv_name'
 
 This matches the client environment used in make test
@@ -928,6 +945,32 @@ SMB_CONF_PATH=\$SMB_CONF_PATH
 $envvarstr
 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
        teardown_env($testenv_name);
+} elsif ($opt_list) {
+       foreach (@todo) {
+               my $cmd = $$_[2];
+               my $name = $$_[0];
+               my $envname = $$_[1];
+
+               unless($cmd =~ /\$LISTOPT/) {
+                       warn("Unable to list tests in $name");
+                       next;
+               }
+
+               $cmd =~ s/\$LISTOPT/--list/g;
+
+               system($cmd);
+
+               if ($? == -1) {
+                       die("Unable to run $cmd: $!");
+               } elsif ($? & 127) {
+                       die(snprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
+               }
+
+               my $exitcode = $? >> 8;
+               if ($exitcode != 0) {
+                       die("$cmd exited with exit code $exitcode");
+               }
+       }
 } else {
        foreach (@todo) {
                $i++;
@@ -939,7 +982,7 @@ $envvarstr
                if (not defined($envvars)) {
                        Subunit::start_testsuite($name);
                        Subunit::end_testsuite($name, "error",
-                               "unable to set up environment $envname");
+                               "unable to set up environment $envname - exiting");
                        next;
                }