VERSION: Bump version up to 4.15.0rc2...
[Samba.git] / selftest / selftesthelpers.py
blob3358374cbcac1b990c3d2f3b546e623552fec49c
1 #!/usr/bin/python
2 # This script generates a list of testsuites that should be run as part of
3 # the Samba 4 test suite.
5 # The output of this script is parsed by selftest.pl, which then decides
6 # which of the tests to actually run. It will, for example, skip all tests
7 # listed in selftest/skip or only run a subset during "make quicktest".
9 # The idea is that this script outputs all of the tests of Samba 4, not
10 # just those that are known to pass, and list those that should be skipped
11 # or are known to fail in selftest/skip or selftest/knownfail. This makes it
12 # very easy to see what functionality is still missing in Samba 4 and makes
13 # it possible to run the testsuite against other servers, such as Samba 3 or
14 # Windows that have a different set of features.
16 # The syntax for a testsuite is "-- TEST --" on a single line, followed
17 # by the name of the test, the environment it needs and the command to run, all
18 # three separated by newlines. All other lines in the output are considered
19 # comments.
21 import os
22 import subprocess
23 import sys
26 def srcdir():
27 return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
30 def source4dir():
31 return os.path.normpath(os.path.join(srcdir(), "source4"))
34 def source3dir():
35 return os.path.normpath(os.path.join(srcdir(), "source3"))
38 def bindir():
39 return os.path.normpath(os.getenv("BINDIR", "./bin"))
42 def binpath(name):
43 return os.path.join(bindir(), name)
46 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
47 perl = os.getenv("PERL", "perl").split()
49 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
50 has_perl_test_more = True
51 else:
52 has_perl_test_more = False
54 python = os.getenv("PYTHON", "python")
56 tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
59 def valgrindify(cmdline):
60 """Run a command under valgrind, if $VALGRIND was set."""
61 valgrind = os.getenv("VALGRIND")
62 if valgrind is None:
63 return cmdline
64 return valgrind + " " + cmdline
67 def plantestsuite(name, env, cmdline):
68 """Plan a test suite.
70 :param name: Testsuite name
71 :param env: Environment to run the testsuite in
72 :param cmdline: Command line to run
73 """
74 print("-- TEST --")
75 if env == "none":
76 fullname = name
77 else:
78 fullname = "%s(%s)" % (name, env)
79 print(fullname)
80 print(env)
81 if isinstance(cmdline, list):
82 cmdline = " ".join(cmdline)
83 if "$LISTOPT" in cmdline:
84 raise AssertionError("test %s supports --list, but not --load-list" % name)
85 print(cmdline + " 2>&1 " + " | " + add_prefix(name, env))
88 def add_prefix(prefix, env, support_list=False):
89 if support_list:
90 listopt = "$LISTOPT "
91 else:
92 listopt = ""
93 return "%s %s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (python, srcdir(), listopt, prefix, env)
96 def plantestsuite_loadlist(name, env, cmdline):
97 print("-- TEST-LOADLIST --")
98 if env == "none":
99 fullname = name
100 else:
101 fullname = "%s(%s)" % (name, env)
102 print(fullname)
103 print(env)
104 if isinstance(cmdline, list):
105 cmdline = " ".join(cmdline)
106 support_list = ("$LISTOPT" in cmdline)
107 if "$LISTOPT" not in cmdline:
108 raise AssertionError("loadlist test %s does not support not --list" % name)
109 if "$LOADLIST" not in cmdline:
110 raise AssertionError("loadlist test %s does not support --load-list" % name)
111 print(("%s | %s" % (cmdline.replace("$LOADLIST", ""), add_prefix(name, env, support_list))).replace("$LISTOPT", "--list "))
112 print(cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False))
115 def skiptestsuite(name, reason):
116 """Indicate that a testsuite was skipped.
118 :param name: Test suite name
119 :param reason: Reason the test suite was skipped
121 # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
122 print("skipping %s (%s)" % (name, reason), file=sys.stderr)
125 def planperltestsuite(name, path):
126 """Run a perl test suite.
128 :param name: Name of the test suite
129 :param path: Path to the test runner
131 if has_perl_test_more:
132 plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
133 else:
134 skiptestsuite(name, "Test::More not available")
137 def planpythontestsuite(env, module, name=None, extra_path=[], environ={}, extra_args=[]):
138 environ = dict(environ)
139 py_path = list(extra_path)
140 if py_path is not None:
141 environ["PYTHONPATH"] = ":".join(["$PYTHONPATH"] + py_path)
142 args = ["%s=%s" % item for item in environ.items()]
143 args += [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
144 args += extra_args
145 if name is None:
146 name = module
148 plantestsuite_loadlist(name, env, args)
151 def get_env_torture_options():
152 ret = []
153 if not os.getenv("SELFTEST_VERBOSE"):
154 ret.append("--option=torture:progress=no")
155 if os.getenv("SELFTEST_QUICK"):
156 ret.append("--option=torture:quick=yes")
157 return ret
160 samba4srcdir = source4dir()
161 samba3srcdir = source3dir()
162 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
163 configuration = "--configfile=$SMB_CONF_PATH"
165 smbtorture4 = binpath("smbtorture")
166 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].decode('utf8').splitlines()
168 smbtorture4_options = [
169 configuration,
170 "--option=\'fss:sequence timeout=1\'",
171 "--maximum-runtime=$SELFTEST_MAXTIME",
172 "--basedir=$SELFTEST_TMPDIR",
173 "--format=subunit"
174 ] + get_env_torture_options()
177 def plansmbtorture4testsuite(name, env, options, target, modname=None):
178 if modname is None:
179 modname = "samba4.%s" % name
180 if isinstance(options, list):
181 options = " ".join(options)
182 options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
183 cmdline = "%s $LISTOPT $LOADLIST %s %s" % (valgrindify(smbtorture4), options, name)
184 plantestsuite_loadlist(modname, env, cmdline)
187 def smbtorture4_testsuites(prefix):
188 return list(filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list))
191 smbclient3 = binpath('smbclient')
192 smbtorture3 = binpath('smbtorture3')
193 ntlm_auth3 = binpath('ntlm_auth')
194 net = binpath('net')
195 scriptdir = os.path.join(srcdir(), "script/tests")
197 wbinfo = binpath('wbinfo')
198 dbwrap_tool = binpath('dbwrap_tool')
199 vfstest = binpath('vfstest')
200 smbcquotas = binpath('smbcquotas')
201 smbget = binpath('smbget')
202 rpcclient = binpath('rpcclient')
203 smbcacls = binpath('smbcacls')
204 smbcontrol = binpath('smbcontrol')
205 smbstatus = binpath('smbstatus')