s3:utils: Fix Inherit-Only flag being automatically propagated to children
[Samba.git] / selftest / selftesthelpers.py
blob908fe79cb195a100e50688d02e7666c260c5af7c
1 #!/usr/bin/env python3
3 # This script generates a list of testsuites that should be run as part of
4 # the Samba 4 test suite.
6 # The output of this script is parsed by selftest.pl, which then decides
7 # which of the tests to actually run. It will, for example, skip all tests
8 # listed in selftest/skip or only run a subset during "make quicktest".
10 # The idea is that this script outputs all of the tests of Samba 4, not
11 # just those that are known to pass, and list those that should be skipped
12 # or are known to fail in selftest/skip or selftest/knownfail. This makes it
13 # very easy to see what functionality is still missing in Samba 4 and makes
14 # it possible to run the testsuite against other servers, such as Samba 3 or
15 # Windows that have a different set of features.
17 # The syntax for a testsuite is "-- TEST --" on a single line, followed
18 # by the name of the test, the environment it needs and the command to run, all
19 # three separated by newlines. All other lines in the output are considered
20 # comments.
22 import os
23 import subprocess
24 import sys
27 def srcdir():
28 alternate_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
29 return os.path.normpath(os.getenv("SRCDIR", alternate_path))
32 def source4dir():
33 return os.path.normpath(os.path.join(srcdir(), "source4"))
36 def source3dir():
37 return os.path.normpath(os.path.join(srcdir(), "source3"))
40 def bindir():
41 return os.path.normpath(os.getenv("BINDIR", "./bin"))
44 def binpath(name):
45 return os.path.join(bindir(), name)
48 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
49 perl = os.getenv("PERL", "perl").split()
51 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
52 has_perl_test_more = True
53 else:
54 has_perl_test_more = False
56 python = os.getenv("PYTHON", "python")
58 tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
61 def valgrindify(cmdline):
62 """Run a command under valgrind, if $VALGRIND was set."""
63 valgrind = os.getenv("VALGRIND")
64 if valgrind is None:
65 return cmdline
66 return valgrind + " " + cmdline
69 def plantestsuite(name, env, cmd, environ=None):
70 """Plan a test suite.
72 :param name: Testsuite name
73 :param env: Environment to run the testsuite in
74 :param cmdline: Command line to run
75 """
76 if environ is None:
77 environ = {}
78 print("-- TEST --")
79 if env == "none":
80 fullname = name
81 else:
82 fullname = "%s(%s)" % (name, env)
83 print(fullname)
84 print(env)
86 cmdline = ""
87 if environ:
88 environ = dict(environ)
89 cmdline_env = ["%s=%s" % item for item in environ.items()]
90 cmdline = " ".join(cmdline_env) + " "
92 if isinstance(cmd, list):
93 cmdline += " ".join(cmd)
94 else:
95 cmdline += cmd
97 if "$LISTOPT" in cmdline:
98 raise AssertionError("test %s supports --list, but not --load-list" % name)
99 print(cmdline + " 2>&1 " + " | " + add_prefix(name, env))
102 def add_prefix(prefix, env, support_list=False):
103 if support_list:
104 listopt = "$LISTOPT "
105 else:
106 listopt = ""
107 return ("%s %s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" %
108 (python, srcdir(), listopt, prefix, env))
111 def plantestsuite_loadlist(name, env, cmdline):
112 print("-- TEST-LOADLIST --")
113 if env == "none":
114 fullname = name
115 else:
116 fullname = "%s(%s)" % (name, env)
117 print(fullname)
118 print(env)
119 if isinstance(cmdline, list):
120 cmdline = " ".join(cmdline)
121 support_list = ("$LISTOPT" in cmdline)
122 if "$LISTOPT" not in cmdline:
123 raise AssertionError("loadlist test %s does not support not --list" % name)
124 if "$LOADLIST" not in cmdline:
125 raise AssertionError("loadlist test %s does not support --load-list" % name)
126 print(("%s | %s" %
127 (cmdline.replace("$LOADLIST", ""),
128 add_prefix(name, env, support_list))).replace("$LISTOPT", "--list "))
129 print(cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False))
132 def skiptestsuite(name, reason):
133 """Indicate that a testsuite was skipped.
135 :param name: Test suite name
136 :param reason: Reason the test suite was skipped
138 # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
139 print("skipping %s (%s)" % (name, reason), file=sys.stderr)
142 def planperltestsuite(name, path):
143 """Run a perl test suite.
145 :param name: Name of the test suite
146 :param path: Path to the test runner
148 if has_perl_test_more:
149 plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
150 else:
151 skiptestsuite(name, "Test::More not available")
154 def planpythontestsuite(env, module, name=None, extra_path=None, environ=None, extra_args=None):
155 if extra_path is None:
156 extra_path = []
157 if environ is None:
158 environ = {}
159 if extra_args is None:
160 extra_args = []
161 environ = dict(environ)
162 py_path = list(extra_path)
163 if py_path is not None:
164 environ["PYTHONPATH"] = ":".join(["$PYTHONPATH"] + py_path)
165 args = ["%s=%s" % item for item in environ.items()]
166 args += [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
167 args += extra_args
168 if name is None:
169 name = module
171 plantestsuite_loadlist(name, env, args)
174 def get_env_torture_options():
175 ret = []
176 if not os.getenv("SELFTEST_VERBOSE"):
177 ret.append("--option=torture:progress=no")
178 if os.getenv("SELFTEST_QUICK"):
179 ret.append("--option=torture:quick=yes")
180 return ret
183 samba4srcdir = source4dir()
184 samba3srcdir = source3dir()
185 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
186 configuration = "--configfile=$SMB_CONF_PATH"
188 smbtorture4 = binpath("smbtorture")
189 smbtorture4_testsuite_list = subprocess.Popen(
190 [smbtorture4, "--list-suites"],
191 stdout=subprocess.PIPE,
192 stderr=subprocess.PIPE).communicate("")[0].decode('utf8').splitlines()
194 smbtorture4_options = [
195 configuration,
196 "--option=\'fss:sequence timeout=1\'",
197 "--maximum-runtime=$SELFTEST_MAXTIME",
198 "--basedir=$SELFTEST_TMPDIR",
199 "--format=subunit"
200 ] + get_env_torture_options()
203 def plansmbtorture4testsuite(name, env, options, target, modname=None, environ=None):
204 if environ is None:
205 environ = {}
206 if modname is None:
207 modname = "samba4.%s" % name
208 if isinstance(options, list):
209 options = " ".join(options)
210 options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
211 cmdline = ""
212 if environ:
213 environ = dict(environ)
214 cmdline_env = ["%s=%s" % item for item in environ.items()]
215 cmdline += " ".join(cmdline_env) + " "
216 cmdline += " %s $LISTOPT $LOADLIST %s %s" % (valgrindify(smbtorture4), options, name)
217 plantestsuite_loadlist(modname, env, cmdline)
220 def smbtorture4_testsuites(prefix):
221 return list(filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list))
224 smbclient3 = binpath('smbclient')
225 smbtorture3 = binpath('smbtorture3')
226 ntlm_auth3 = binpath('ntlm_auth')
227 net = binpath('net')
228 scriptdir = os.path.join(srcdir(), "script/tests")
230 wbinfo = binpath('wbinfo')
231 dbwrap_tool = binpath('dbwrap_tool')
232 vfstest = binpath('vfstest')
233 smbcquotas = binpath('smbcquotas')
234 smbget = binpath('smbget')
235 rpcclient = binpath('rpcclient')
236 smbcacls = binpath('smbcacls')
237 smbcontrol = binpath('smbcontrol')
238 smbstatus = binpath('smbstatus')
239 timelimit = binpath('timelimit')