Include mimeparse, which is used by subunit/testtools.
[obnox/samba/samba-obnox.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
21 import os
22 import subprocess
23 import sys
24
25 def srcdir():
26     return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
27
28 def source4dir():
29     return os.path.normpath(os.path.join(srcdir(), "source4"))
30
31 def source3dir():
32     return os.path.normpath(os.path.join(srcdir(), "source3"))
33
34 def bindir():
35     return os.path.normpath(os.getenv("BINDIR", "./bin"))
36
37 binary_mapping = {}
38
39 def binpath(name):
40     if name in binary_mapping:
41         name = binary_mapping[name]
42     return os.path.join(bindir(), name)
43
44 binary_mapping_string = os.getenv("BINARY_MAPPING", None)
45 if binary_mapping_string is not None:
46     for binmapping_entry in binary_mapping_string.split(','):
47         try:
48             (from_path, to_path) = binmapping_entry.split(':', 1)
49         except ValueError:
50             continue
51         binary_mapping[from_path] = to_path
52
53 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
54 perl = os.getenv("PERL", "perl").split()
55
56 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
57     has_perl_test_more = True
58 else:
59     has_perl_test_more = False
60
61 try:
62     from subunit.run import TestProgram
63 except ImportError:
64     has_system_subunit_run = False
65 else:
66     has_system_subunit_run = True
67
68 python = os.getenv("PYTHON", "python")
69
70 # Set a default value, overridden if we find a working one on the system
71 tap2subunit = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools:%s/lib/extras:%s/lib/mimeparse %s %s/lib/subunit/filters/tap2subunit" % (srcdir(), srcdir(), srcdir(), srcdir(), python, srcdir())
72 subunit2to1 = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools:%s/lib/extras:%s/lib/mimeparse %s %s/lib/subunit/filters/subunit-2to1" % (srcdir(), srcdir(), srcdir(), srcdir(), python, srcdir())
73
74 sub = subprocess.Popen("tap2subunit", stdin=subprocess.PIPE,
75     stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
76 sub.communicate("")
77
78 if sub.returncode == 0:
79     cmd = "echo -ne \"1..1\nok 1 # skip doesn't seem to work yet\n\" | tap2subunit | grep skip"
80     sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
81         stderr=subprocess.PIPE, shell=True)
82     if sub.returncode == 0:
83         tap2subunit = "tap2subunit"
84
85 def valgrindify(cmdline):
86     """Run a command under valgrind, if $VALGRIND was set."""
87     valgrind = os.getenv("VALGRIND")
88     if valgrind is None:
89         return cmdline
90     return valgrind + " " + cmdline
91
92
93 def plantestsuite(name, env, cmdline):
94     """Plan a test suite.
95
96     :param name: Testsuite name
97     :param env: Environment to run the testsuite in
98     :param cmdline: Command line to run
99     """
100     print "-- TEST --"
101     print name
102     print env
103     if isinstance(cmdline, list):
104         cmdline = " ".join(cmdline)
105     filter_subunit_args = ["--fail-on-empty"]
106     if "$LISTOPT" in cmdline:
107         filter_subunit_args.append("$LISTOPT")
108     print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\" --suffix=\"(%s)\"" % (cmdline,
109                                                                         srcdir(),
110                                                                         " ".join(filter_subunit_args),
111                                                                         name, env)
112
113
114 def add_prefix(prefix, env, support_list=False):
115     if support_list:
116         listopt = "$LISTOPT "
117     else:
118         listopt = ""
119     return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (srcdir(), listopt, prefix, env)
120
121
122 def plantestsuite_loadlist(name, env, cmdline):
123     print "-- TEST-LOADLIST --"
124     if env == "none":
125         fullname = name
126     else:
127         fullname = "%s(%s)" % (name, env)
128     print fullname
129     print env
130     if isinstance(cmdline, list):
131         cmdline = " ".join(cmdline)
132     support_list = ("$LISTOPT" in cmdline)
133     print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, env, support_list))
134
135
136 def plantestsuite_idlist(name, env, cmdline):
137     print "-- TEST-IDLIST --"
138     if env == "none":
139         fullname = name
140     else:
141         fullname = "%s(%s)" % (name, env)
142     print fullname
143     print env
144     if isinstance(cmdline, list):
145         cmdline = " ".join(cmdline)
146     print cmdline
147
148
149 def skiptestsuite(name, reason):
150     """Indicate that a testsuite was skipped.
151
152     :param name: Test suite name
153     :param reason: Reason the test suite was skipped
154     """
155     # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
156     print >>sys.stderr, "skipping %s (%s)" % (name, reason)
157
158
159 def planperltestsuite(name, path):
160     """Run a perl test suite.
161
162     :param name: Name of the test suite
163     :param path: Path to the test runner
164     """
165     if has_perl_test_more:
166         plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
167     else:
168         skiptestsuite(name, "Test::More not available")
169
170
171 def planpythontestsuite(env, module, name=None, extra_path=[]):
172     if name is None:
173         name = module
174     pypath = list(extra_path)
175     if not has_system_subunit_run:
176         pypath.extend([
177             "%s/lib/subunit/python" % srcdir(),
178             "%s/lib/testtools" % srcdir(),
179             "%s/lib/extras" % srcdir(),
180             "%s/lib/mimeparse" % srcdir()])
181     args = [python, "-m", "subunit.run", "$LISTOPT", module]
182     if pypath:
183         args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
184     plantestsuite_idlist(name, env, args)
185
186
187 def get_env_torture_options():
188     ret = []
189     if not os.getenv("SELFTEST_VERBOSE"):
190         ret.append("--option=torture:progress=no")
191     if os.getenv("SELFTEST_QUICK"):
192         ret.append("--option=torture:quick=yes")
193     return ret
194
195
196 samba4srcdir = source4dir()
197 samba3srcdir = source3dir()
198 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
199 configuration = "--configfile=$SMB_CONF_PATH"
200
201 smbtorture4 = binpath("smbtorture4")
202 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
203
204 smbtorture4_options = [
205     configuration,
206     "--maximum-runtime=$SELFTEST_MAXTIME",
207     "--basedir=$SELFTEST_TMPDIR",
208     "--format=subunit"
209     ] + get_env_torture_options()
210
211
212 def plansmbtorture4testsuite(name, env, options, target, modname=None):
213     if modname is None:
214         modname = "samba4.%s" % name
215     if isinstance(options, list):
216         options = " ".join(options)
217     options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
218     cmdline = "%s $LISTOPT %s %s" % (valgrindify(smbtorture4), options, name)
219     plantestsuite_loadlist(modname, env, cmdline)
220
221
222 def smbtorture4_testsuites(prefix):
223     return filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list)
224
225
226 smbclient3 = binpath('smbclient3')
227 smbtorture3 = binpath('smbtorture3')
228 ntlm_auth3 = binpath('ntlm_auth3')
229 net = binpath('net')
230 scriptdir = os.path.join(srcdir(), "script/tests")
231
232 wbinfo = binpath('wbinfo')
233 dbwrap_tool = binpath('dbwrap_tool')
234 vfstest = binpath('vfstest')