ebdae12866a7a74a87f8eb65b08d35aec1efd685
[nivanova/samba-autobuild/.git] / selftest / selftesthelpers.py
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.
4
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".
8
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.
15
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.
20 from __future__ import print_function
21
22 import os
23 import subprocess
24 import sys
25
26
27 def srcdir():
28     return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
29
30
31 def source4dir():
32     return os.path.normpath(os.path.join(srcdir(), "source4"))
33
34
35 def source3dir():
36     return os.path.normpath(os.path.join(srcdir(), "source3"))
37
38
39 def bindir():
40     return os.path.normpath(os.getenv("BINDIR", "./bin"))
41
42
43 def binpath(name):
44     return os.path.join(bindir(), name)
45
46
47 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
48 perl = os.getenv("PERL", "perl").split()
49
50 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
51     has_perl_test_more = True
52 else:
53     has_perl_test_more = False
54
55 python = os.getenv("PYTHON", "python")
56 extra_python = os.getenv("EXTRA_PYTHON", None)
57
58 tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
59
60
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
67
68
69 def plantestsuite(name, env, cmdline):
70     """Plan a test suite.
71
72     :param name: Testsuite name
73     :param env: Environment to run the testsuite in
74     :param cmdline: Command line to run
75     """
76     print("-- TEST --")
77     if env == "none":
78         fullname = name
79     else:
80         fullname = "%s(%s)" % (name, env)
81     print(fullname)
82     print(env)
83     if isinstance(cmdline, list):
84         cmdline = " ".join(cmdline)
85     if "$LISTOPT" in cmdline:
86         raise AssertionError("test %s supports --list, but not --load-list" % name)
87     print(cmdline + " 2>&1 " + " | " + add_prefix(name, env))
88
89
90 def add_prefix(prefix, env, support_list=False):
91     if support_list:
92         listopt = "$LISTOPT "
93     else:
94         listopt = ""
95     return "%s %s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (python, srcdir(), listopt, prefix, env)
96
97
98 def plantestsuite_loadlist(name, env, cmdline):
99     print("-- TEST-LOADLIST --")
100     if env == "none":
101         fullname = name
102     else:
103         fullname = "%s(%s)" % (name, env)
104     print(fullname)
105     print(env)
106     if isinstance(cmdline, list):
107         cmdline = " ".join(cmdline)
108     support_list = ("$LISTOPT" in cmdline)
109     if "$LISTOPT" not in cmdline:
110         raise AssertionError("loadlist test %s does not support not --list" % name)
111     if "$LOADLIST" not in cmdline:
112         raise AssertionError("loadlist test %s does not support --load-list" % name)
113     print(("%s | %s" % (cmdline.replace("$LOADLIST", ""), add_prefix(name, env, support_list))).replace("$LISTOPT", "--list"))
114     print(cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False))
115
116
117 def skiptestsuite(name, reason):
118     """Indicate that a testsuite was skipped.
119
120     :param name: Test suite name
121     :param reason: Reason the test suite was skipped
122     """
123     # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
124     print("skipping %s (%s)" % (name, reason), file=sys.stderr)
125
126
127 def planperltestsuite(name, path):
128     """Run a perl test suite.
129
130     :param name: Name of the test suite
131     :param path: Path to the test runner
132     """
133     if has_perl_test_more:
134         plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
135     else:
136         skiptestsuite(name, "Test::More not available")
137
138
139 def planpythontestsuite(env, module, name=None, extra_path=None,
140                         py3_compatible=False):
141     if name is None:
142         name = module
143     args = [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
144     if extra_path:
145         pypath = ["PYTHONPATH=$PYTHONPATH:%s" % ":".join(extra_path)]
146     else:
147         pypath = []
148
149     plantestsuite_loadlist(name, env, pypath + args)
150     if py3_compatible and extra_python is not None:
151         # Plan one more test for Python 3 compatible module
152         args[0] = extra_python
153         python_name = os.path.basename(extra_python)
154         plantestsuite_loadlist(name + "." + python_name, env, pypath + args)
155
156
157 def get_env_torture_options():
158     ret = []
159     if not os.getenv("SELFTEST_VERBOSE"):
160         ret.append("--option=torture:progress=no")
161     if os.getenv("SELFTEST_QUICK"):
162         ret.append("--option=torture:quick=yes")
163     return ret
164
165
166 samba4srcdir = source4dir()
167 samba3srcdir = source3dir()
168 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
169 configuration = "--configfile=$SMB_CONF_PATH"
170
171 smbtorture4 = binpath("smbtorture")
172 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].decode('utf8').splitlines()
173
174 smbtorture4_options = [
175     configuration,
176     "--option=\'fss:sequence timeout=1\'",
177     "--maximum-runtime=$SELFTEST_MAXTIME",
178     "--basedir=$SELFTEST_TMPDIR",
179     "--format=subunit"
180 ] + get_env_torture_options()
181
182
183 def plansmbtorture4testsuite(name, env, options, target, modname=None):
184     if modname is None:
185         modname = "samba4.%s" % name
186     if isinstance(options, list):
187         options = " ".join(options)
188     options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
189     cmdline = "%s $LISTOPT $LOADLIST %s %s" % (valgrindify(smbtorture4), options, name)
190     plantestsuite_loadlist(modname, env, cmdline)
191
192
193 def smbtorture4_testsuites(prefix):
194     return list(filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list))
195
196
197 smbclient3 = binpath('smbclient')
198 smbtorture3 = binpath('smbtorture3')
199 ntlm_auth3 = binpath('ntlm_auth')
200 net = binpath('net')
201 scriptdir = os.path.join(srcdir(), "script/tests")
202
203 wbinfo = binpath('wbinfo')
204 dbwrap_tool = binpath('dbwrap_tool')
205 vfstest = binpath('vfstest')
206 smbcquotas = binpath('smbcquotas')
207 smbget = binpath('smbget')
208 rpcclient = binpath('rpcclient')
209 smbcacls = binpath('smbcacls')