lib/util/time: strip a potential trailing newline in the asctime case.
[Samba.git] / selftest / selftesthelpers.py
blobcdbe9751f31b69441a19314b103ae30be88037b9
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.
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".
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.
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.
21 import errno
22 import os
23 import subprocess
24 import sys
26 def srcdir():
27 return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
29 def source4dir():
30 return os.path.normpath(os.path.join(srcdir(), "source4"))
32 def source3dir():
33 return os.path.normpath(os.path.join(srcdir(), "source3"))
35 def bindir():
36 return os.path.normpath(os.getenv("BINDIR", "./bin"))
38 binary_mapping = {}
40 def binpath(name):
41 if name in binary_mapping:
42 name = binary_mapping[name]
43 return os.path.join(bindir(), name)
45 binary_mapping_string = os.getenv("BINARY_MAPPING", None)
46 if binary_mapping_string is not None:
47 for binmapping_entry in binary_mapping_string.split(','):
48 try:
49 (from_path, to_path) = binmapping_entry.split(':', 1)
50 except ValueError:
51 continue
52 binary_mapping[from_path] = to_path
54 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
55 perl = os.getenv("PERL", "perl").split()
57 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
58 has_perl_test_more = True
59 else:
60 has_perl_test_more = False
62 try:
63 from subunit.run import TestProgram
64 except ImportError:
65 has_system_subunit_run = False
66 else:
67 has_system_subunit_run = True
69 python = os.getenv("PYTHON", "python")
71 # Set a default value, overridden if we find a working one on the system
72 tap2subunit = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools %s %s/lib/subunit/filters/tap2subunit" % (srcdir(), srcdir(), python, srcdir())
74 sub = subprocess.Popen("tap2subunit", stdin=subprocess.PIPE,
75 stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
76 sub.communicate("")
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"
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
93 def plantestsuite(name, env, cmdline, allow_empty_output=False):
94 """Plan a test suite.
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 = []
106 if not allow_empty_output:
107 filter_subunit_args.append("--fail-on-empty")
108 if "$LISTOPT" in cmdline:
109 filter_subunit_args.append("$LISTOPT")
110 print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\" --suffix=\"(%s)\"" % (cmdline,
111 srcdir(),
112 " ".join(filter_subunit_args),
113 name, env)
114 if allow_empty_output:
115 print >>sys.stderr, "WARNING: allowing empty subunit output from %s" % name
118 def add_prefix(prefix, env, support_list=False):
119 if support_list:
120 listopt = "$LISTOPT "
121 else:
122 listopt = ""
123 return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (srcdir(), listopt, prefix, env)
126 def plantestsuite_loadlist(name, env, cmdline):
127 print "-- TEST-LOADLIST --"
128 if env == "none":
129 fullname = name
130 else:
131 fullname = "%s(%s)" % (name, env)
132 print fullname
133 print env
134 if isinstance(cmdline, list):
135 cmdline = " ".join(cmdline)
136 support_list = ("$LISTOPT" in cmdline)
137 print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, env, support_list))
140 def plantestsuite_idlist(name, env, cmdline):
141 print "-- TEST-IDLIST --"
142 if env == "none":
143 fullname = name
144 else:
145 fullname = "%s(%s)" % (name, env)
146 print fullname
147 print env
148 if isinstance(cmdline, list):
149 cmdline = " ".join(cmdline)
150 print cmdline
153 def skiptestsuite(name, reason):
154 """Indicate that a testsuite was skipped.
156 :param name: Test suite name
157 :param reason: Reason the test suite was skipped
159 # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
160 print >>sys.stderr, "skipping %s (%s)" % (name, reason)
163 def planperltestsuite(name, path):
164 """Run a perl test suite.
166 :param name: Name of the test suite
167 :param path: Path to the test runner
169 if has_perl_test_more:
170 plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
171 else:
172 skiptestsuite(name, "Test::More not available")
175 def planpythontestsuite(env, module, name=None, extra_path=[]):
176 if name is None:
177 name = module
178 pypath = list(extra_path)
179 if not has_system_subunit_run:
180 pypath.extend(["%s/lib/subunit/python" % srcdir(),
181 "%s/lib/testtools" % srcdir()])
182 args = [python, "-m", "subunit.run", "$LISTOPT", module]
183 if pypath:
184 args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
185 plantestsuite_idlist(name, env, args)
188 def get_env_torture_options():
189 ret = []
190 if not os.getenv("SELFTEST_VERBOSE"):
191 ret.append("--option=torture:progress=no")
192 if os.getenv("SELFTEST_QUICK"):
193 ret.append("--option=torture:quick=yes")
194 return ret
197 samba4srcdir = source4dir()
198 samba3srcdir = source3dir()
199 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
200 configuration = "--configfile=$SMB_CONF_PATH"
202 smbtorture4 = binpath("smbtorture4")
203 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
205 smbtorture4_options = [
206 configuration,
207 "--maximum-runtime=$SELFTEST_MAXTIME",
208 "--basedir=$SELFTEST_TMPDIR",
209 "--format=subunit"
210 ] + get_env_torture_options()
213 def print_smbtorture4_version():
214 """Print the version of Samba smbtorture4 comes from.
216 :return: Whether smbtorture4 was successfully run
218 try:
219 sub = subprocess.Popen([smbtorture4, "-V"], stdout=sys.stderr)
220 except OSError, e:
221 if e.errno == errno.ENOENT:
222 return False
223 raise
224 sub.communicate("")
225 return (sub.returncode == 0)
228 def plansmbtorture4testsuite(name, env, options, target, modname=None):
229 if modname is None:
230 modname = "samba4.%s" % name
231 if isinstance(options, list):
232 options = " ".join(options)
233 options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
234 cmdline = "%s $LISTOPT %s %s" % (valgrindify(smbtorture4), options, name)
235 plantestsuite_loadlist(modname, env, cmdline)
238 def smbtorture4_testsuites(prefix):
239 return filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list)
242 smbclient3 = binpath('smbclient3')
243 smbtorture3 = binpath('smbtorture3')
244 ntlm_auth3 = binpath('ntlm_auth3')
245 net = binpath('net')
246 scriptdir = os.path.join(srcdir(), "script/tests")
248 wbinfo = binpath('wbinfo')
249 dbwrap_tool = binpath('dbwrap_tool')
250 vfstest = binpath('vfstest')