python/samba/netcmd/schema.py: add schema show_oc for attribute
[kai/samba-autobuild/.git] / selftest / selftesthelpers.py
index e274cc4242edd7cb632e75eca4acea1c8de24f08..0d8014c7d139965b719160592eeb465ea4171ce9 100644 (file)
 # by the name of the test, the environment it needs and the command to run, all
 # three separated by newlines. All other lines in the output are considered
 # comments.
+from __future__ import print_function
 
 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 source3dir():
+    return os.path.normpath(os.path.join(srcdir(), "source3"))
 
 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", "")))
+    return os.path.join(bindir(), name)
 
-perl = os.getenv("PERL", "perl")
+# 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:
+if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
     has_perl_test_more = True
 else:
     has_perl_test_more = False
 
-try:
-    from subunit.run import TestProgram
-except ImportError:
-    has_system_subunit_run = False
-else:
-    has_system_subunit_run = True
-
 python = os.getenv("PYTHON", "python")
+extra_python = os.getenv("EXTRA_PYTHON", None)
 
-#Set a default value, overridden if we find a working one on the system
-tap2subunit = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools %s %s/lib/subunit/filters/tap2subunit" % (srcdir(), srcdir(), python, srcdir())
-
-sub = subprocess.Popen("tap2subunit 2> /dev/null", stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
-sub.communicate("")
+tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
 
-if sub.returncode == 0:
-    cmd = "echo -ne \"1..1\nok 1 # skip doesn't seem to work yet\n\" | tap2subunit 2> /dev/null | grep skip"
-    sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
-    if sub.returncode == 0:
-        tap2subunit = "tap2subunit"
 
 def valgrindify(cmdline):
     """Run a command under valgrind, if $VALGRIND was set."""
@@ -69,60 +60,52 @@ def valgrindify(cmdline):
     return valgrind + " " + cmdline
 
 
-def plantestsuite(name, env, cmdline, allow_empty_output=False):
+def plantestsuite(name, env, cmdline):
     """Plan a test suite.
 
     :param name: Testsuite name
     :param env: Environment to run the testsuite in
     :param cmdline: Command line to run
     """
-    print "-- TEST --"
-    print name
-    print env
+    print("-- TEST --")
+    if env == "none":
+        fullname = name
+    else:
+        fullname = "%s(%s)" % (name, env)
+    print(fullname)
+    print(env)
     if isinstance(cmdline, list):
         cmdline = " ".join(cmdline)
-    filter_subunit_args = []
-    if not allow_empty_output:
-        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,
-                                                                        srcdir(),
-                                                                        " ".join(filter_subunit_args),
-                                                                        name)
-    if allow_empty_output:
-        print "WARNING: allowing empty subunit output from %s" % name
+        raise AssertionError("test %s supports --list, but not --load-list" % name)
+    print(cmdline + " 2>&1 " + " | " + add_prefix(name, env))
 
 
-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):
-    print "-- TEST-LOADLIST --"
+    print("-- TEST-LOADLIST --")
     if env == "none":
         fullname = name
     else:
         fullname = "%s(%s)" % (name, env)
-    print fullname
-    print env
+    print(fullname)
+    print(env)
     if isinstance(cmdline, list):
         cmdline = " ".join(cmdline)
     support_list = ("$LISTOPT" in cmdline)
-    print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, support_list))
-
-
-def plantestsuite_idlist(name, env, cmdline):
-    print "-- TEST-IDLIST --"
-    print name
-    print env
-    if isinstance(cmdline, list):
-        cmdline = " ".join(cmdline)
-    print cmdline
+    if not "$LISTOPT" in cmdline:
+        raise AssertionError("loadlist test %s does not support not --list" % name)
+    if not "$LOADLIST" in cmdline:
+        raise AssertionError("loadlist test %s does not support --load-list" % name)
+    print(("%s | %s" % (cmdline.replace("$LOADLIST", ""), add_prefix(name, env, support_list))).replace("$LISTOPT", "--list"))
+    print(cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False))
 
 
 def skiptestsuite(name, reason):
@@ -132,7 +115,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("skipping %s (%s)" % (name, reason), file=sys.stderr)
 
 
 def planperltestsuite(name, path):
@@ -142,15 +125,75 @@ 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=[], py3_compatible=False):
+    if name is None:
+        name = module
+    pypath = list(extra_path)
+    args = [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
+    if pypath:
+        args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
+    plantestsuite_loadlist(name, env, args)
+    if py3_compatible and extra_python is not None:
+        # Plan one more test for Python 3 compatible module
+        args[0] = extra_python
+        plantestsuite_loadlist(name + ".python3", env, args)
+
+
+def get_env_torture_options():
+    ret = []
+    if not os.getenv("SELFTEST_VERBOSE"):
+        ret.append("--option=torture:progress=no")
+    if os.getenv("SELFTEST_QUICK"):
+        ret.append("--option=torture:quick=yes")
+    return ret
+
+
+samba4srcdir = source4dir()
+samba3srcdir = source3dir()
+bbdir = os.path.join(srcdir(), "testprogs/blackbox")
+configuration = "--configfile=$SMB_CONF_PATH"
+
+smbtorture4 = binpath("smbtorture")
+smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
+
+smbtorture4_options = [
+    configuration,
+    "--option=\'fss:sequence timeout=1\'",
+    "--maximum-runtime=$SELFTEST_MAXTIME",
+    "--basedir=$SELFTEST_TMPDIR",
+    "--format=subunit"
+    ] + get_env_torture_options()
+
+
+def plansmbtorture4testsuite(name, env, options, target, modname=None):
+    if modname is None:
+        modname = "samba4.%s" % name
+    if isinstance(options, list):
+        options = " ".join(options)
+    options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
+    cmdline = "%s $LISTOPT $LOADLIST %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)
+
+
+smbclient3 = binpath('smbclient')
+smbtorture3 = binpath('smbtorture3')
+ntlm_auth3 = binpath('ntlm_auth')
+net = binpath('net')
+scriptdir = os.path.join(srcdir(), "script/tests")
+
+wbinfo = binpath('wbinfo')
+dbwrap_tool = binpath('dbwrap_tool')
+vfstest = binpath('vfstest')
+smbcquotas = binpath('smbcquotas')
+smbget = binpath('smbget')
+rpcclient = binpath('rpcclient')
+smbcacls = binpath('smbcacls')