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