00fa8aae7c80de72086c8d057c3daeb569ec51c6
[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 bindir():
32     return os.path.normpath(os.getenv("BINDIR", "./bin"))
33
34 binary_mapping = {}
35
36 def binpath(name):
37     if name in binary_mapping:
38         name = binary_mapping[name]
39     return os.path.join(bindir(), name)
40
41 binary_mapping_string = os.getenv("BINARY_MAPPING", None)
42 if binary_mapping_string is not None:
43     for binmapping_entry in binary_mapping_string.split(','):
44         try:
45             (from_path, to_path) = binmapping_entry.split(':', 1)
46         except ValueError:
47             continue
48         binary_mapping[from_path] = to_path
49
50 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
51 perl = os.getenv("PERL", "perl").split()
52
53 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
54     has_perl_test_more = True
55 else:
56     has_perl_test_more = False
57
58 try:
59     from subunit.run import TestProgram
60 except ImportError:
61     has_system_subunit_run = False
62 else:
63     has_system_subunit_run = True
64
65 python = os.getenv("PYTHON", "python")
66
67 # Set a default value, overridden if we find a working one on the system
68 tap2subunit = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools %s %s/lib/subunit/filters/tap2subunit" % (srcdir(), srcdir(), python, srcdir())
69
70 sub = subprocess.Popen("tap2subunit", stdin=subprocess.PIPE,
71     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
72 sub.communicate("")
73
74 if sub.returncode == 0:
75     cmd = "echo -ne \"1..1\nok 1 # skip doesn't seem to work yet\n\" | tap2subunit | grep skip"
76     sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
77         stderr=subprocess.PIPE, shell=True)
78     if sub.returncode == 0:
79         tap2subunit = "tap2subunit"
80
81 def valgrindify(cmdline):
82     """Run a command under valgrind, if $VALGRIND was set."""
83     valgrind = os.getenv("VALGRIND")
84     if valgrind is None:
85         return cmdline
86     return valgrind + " " + cmdline
87
88
89 def plantestsuite(name, env, cmdline, allow_empty_output=False):
90     """Plan a test suite.
91
92     :param name: Testsuite name
93     :param env: Environment to run the testsuite in
94     :param cmdline: Command line to run
95     """
96     print "-- TEST --"
97     print name
98     print env
99     if isinstance(cmdline, list):
100         cmdline = " ".join(cmdline)
101     filter_subunit_args = []
102     if not allow_empty_output:
103         filter_subunit_args.append("--fail-on-empty")
104     if "$LISTOPT" in cmdline:
105         filter_subunit_args.append("$LISTOPT")
106     print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\" --suffix=\"(%s)\"" % (cmdline,
107                                                                         srcdir(),
108                                                                         " ".join(filter_subunit_args),
109                                                                         name, env)
110     if allow_empty_output:
111         print >>sys.stderr, "WARNING: allowing empty subunit output from %s" % name
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(["%s/lib/subunit/python" % srcdir(),
177             "%s/lib/testtools" % srcdir()])
178     args = [python, "-m", "subunit.run", "$LISTOPT", module]
179     if pypath:
180         args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
181     plantestsuite_idlist(name, env, args)
182
183
184 def get_env_torture_options():
185     ret = []
186     if not os.getenv("SELFTEST_VERBOSE"):
187         ret.append("--option=torture:progress=no")
188     if os.getenv("SELFTEST_QUICK"):
189         ret.append("--option=torture:quick=yes")
190     return ret
191
192
193 samba4srcdir = source4dir()
194 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
195 configuration = "--configfile=$SMB_CONF_PATH"
196
197 smbtorture4 = binpath("smbtorture4")
198 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
199
200 smbtorture4_options = [
201     configuration,
202     "--maximum-runtime=$SELFTEST_MAXTIME",
203     "--basedir=$SELFTEST_TMPDIR",
204     "--format=subunit"
205     ] + get_env_torture_options()
206
207
208 def print_smbtorture4_version():
209     """Print the version of Samba smbtorture4 comes from.
210
211     :return: Whether smbtorture4 was successfully run
212     """
213     sub = subprocess.Popen([smbtorture4, "-V"], stdout=sys.stderr)
214     sub.communicate("")
215     return (sub.returncode == 0)
216
217
218 def plansmbtorture4testsuite(name, env, options, target, modname=None):
219     if modname is None:
220         modname = "samba4.%s" % name
221     if isinstance(options, list):
222         options = " ".join(options)
223     options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
224     cmdline = "%s $LISTOPT %s %s" % (valgrindify(smbtorture4), options, name)
225     plantestsuite_loadlist(modname, env, cmdline)
226
227
228 def smbtorture4_testsuites(prefix):
229     return filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list)