selftesthelpers: Share code for listing smbtorture4 tests.
[obnox/samba/samba-obnox.git] / selftest / selftesthelpers.py
index e274cc4242edd7cb632e75eca4acea1c8de24f08..b22330ae0af1293357a6a5fc0023f56306a5a351 100644 (file)
 
 import os
 import subprocess
+import sys
 
 def srcdir():
-    return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
+    return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
 
 def source4dir():
-    return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../source4"))
+    return os.path.normpath(os.path.join(srcdir(), "source4"))
 
 def bindir():
-    return os.path.normpath(os.path.join(os.getenv("BUILDDIR", "."), "bin"))
+    return os.path.normpath(os.getenv("BINDIR", "./bin"))
 
-def binpath(name):
-    return os.path.join(bindir(), "%s%s" % (name, os.getenv("EXEEXT", "")))
-
-perl = os.getenv("PERL", "perl")
+binary_mapping = {}
 
-if subprocess.call([perl, "-e", "eval require Test::More;"]) == 0:
+def binpath(name):
+    if name in binary_mapping:
+        name = binary_mapping[name]
+    return os.path.join(bindir(), name)
+
+binary_mapping_string = os.getenv("BINARY_MAPPING", None)
+if binary_mapping_string is not None:
+    for binmapping_entry in binary_mapping_string.split(','):
+        try:
+            (from_path, to_path) = binmapping_entry.split(':', 1)
+        except ValueError:
+            continue
+        binary_mapping[from_path] = to_path
+
+# Split perl variable to allow $PERL to be set to e.g. "perl -W"
+perl = os.getenv("PERL", "perl").split()
+
+if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
     has_perl_test_more = True
 else:
     has_perl_test_more = False
@@ -86,20 +101,20 @@ def plantestsuite(name, env, cmdline, allow_empty_output=False):
         filter_subunit_args.append("--fail-on-empty")
     if "$LISTOPT" in cmdline:
         filter_subunit_args.append("$LISTOPT")
-    print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\"" % (cmdline,
+    print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\" --suffix=\"(%s)\"" % (cmdline,
                                                                         srcdir(),
                                                                         " ".join(filter_subunit_args),
-                                                                        name)
+                                                                        name, env)
     if allow_empty_output:
-        print "WARNING: allowing empty subunit output from %s" % name
+        print >>sys.stderr, "WARNING: allowing empty subunit output from %s" % name
 
 
-def add_prefix(prefix, support_list=False):
+def add_prefix(prefix, env, support_list=False):
     if support_list:
         listopt = "$LISTOPT "
     else:
         listopt = ""
-    return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\"" % (srcdir(), listopt, prefix)
+    return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (srcdir(), listopt, prefix, env)
 
 
 def plantestsuite_loadlist(name, env, cmdline):
@@ -113,12 +128,16 @@ def plantestsuite_loadlist(name, env, cmdline):
     if isinstance(cmdline, list):
         cmdline = " ".join(cmdline)
     support_list = ("$LISTOPT" in cmdline)
-    print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, support_list))
+    print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, env, support_list))
 
 
 def plantestsuite_idlist(name, env, cmdline):
     print "-- TEST-IDLIST --"
-    print name
+    if env == "none":
+        fullname = name
+    else:
+        fullname = "%s(%s)" % (name, env)
+    print fullname
     print env
     if isinstance(cmdline, list):
         cmdline = " ".join(cmdline)
@@ -132,7 +151,7 @@ def skiptestsuite(name, reason):
     :param reason: Reason the test suite was skipped
     """
     # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
-    print "skipping %s (%s)" % (name, reason)
+    print >>sys.stderr, "skipping %s (%s)" % (name, reason)
 
 
 def planperltestsuite(name, path):
@@ -142,15 +161,38 @@ def planperltestsuite(name, path):
     :param path: Path to the test runner
     """
     if has_perl_test_more:
-        plantestsuite(name, "none", "%s %s | %s" % (perl, path, tap2subunit))
+        plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
     else:
         skiptestsuite(name, "Test::More not available")
 
 
-def planpythontestsuite(env, module):
-    if has_system_subunit_run:
-        plantestsuite_idlist(module, env, [python, "-m", "subunit.run", "$LISTOPT", module])
-    else:
-        plantestsuite_idlist(module, env, "PYTHONPATH=$PYTHONPATH:%s/lib/subunit/python:%s/lib/testtools %s -m subunit.run $LISTOPT %s" % (srcdir(), srcdir(), python, module))
-
-
+def planpythontestsuite(env, module, name=None, extra_path=[]):
+    if name is None:
+        name = module
+    pypath = list(extra_path)
+    if not has_system_subunit_run:
+        pypath.extend(["%s/lib/subunit/python" % srcdir(),
+            "%s/lib/testtools" % srcdir()])
+    args = [python, "-m", "subunit.run", "$LISTOPT", module]
+    if pypath:
+        args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
+    plantestsuite_idlist(name, env, args)
+
+
+samba4srcdir = source4dir()
+bbdir = os.path.join(srcdir(), "testprogs/blackbox")
+configuration = "--configfile=$SMB_CONF_PATH"
+
+smbtorture4 = binpath("smbtorture4")
+smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
+
+def plansmbtorture4testsuite(name, env, options, modname=None):
+    if modname is None:
+        modname = "samba4.%s" % name
+    if isinstance(options, list):
+        options = " ".join(options)
+    cmdline = "%s $LISTOPT %s %s" % (valgrindify(smbtorture4), options, name)
+    plantestsuite_loadlist(modname, env, cmdline)
+
+def smbtorture4_testsuites(prefix):
+    return filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list)