s3-passdb: make pdb_password_change_time_max static.
[mat/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
21 import os
22 import subprocess
23
24 def srcdir():
25     return os.path.normpath(os.getenv("SRCDIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")))
26
27 def source4dir():
28     return os.path.normpath(os.path.join(srcdir(), "source4"))
29
30 def bindir():
31     return os.path.normpath(os.getenv("BINDIR", "./bin"))
32
33 binary_mapping = {}
34
35 def binpath(name):
36     if name in binary_mapping:
37         name = binary_mapping[name]
38     return os.path.join(bindir(), "%s%s" % (name, os.getenv("EXEEXT", "")))
39
40 binary_mapping_string = os.getenv("BINARY_MAPPING", None)
41 if binary_mapping_string is not None:
42     for binmapping_entry in binary_mapping_string.split(','):
43         try:
44             (from_path, to_path) = binmapping_entry.split(':', 1)
45         except ValueError:
46             continue
47         binary_mapping[from_path] = to_path
48
49 # Split perl variable to allow $PERL to be set to e.g. "perl -W"
50 perl = os.getenv("PERL", "perl").split()
51
52 if subprocess.call(perl + ["-e", "eval require Test::More;"]) == 0:
53     has_perl_test_more = True
54 else:
55     has_perl_test_more = False
56
57 try:
58     from subunit.run import TestProgram
59 except ImportError:
60     has_system_subunit_run = False
61 else:
62     has_system_subunit_run = True
63
64 python = os.getenv("PYTHON", "python")
65
66 #Set a default value, overridden if we find a working one on the system
67 tap2subunit = "PYTHONPATH=%s/lib/subunit/python:%s/lib/testtools %s %s/lib/subunit/filters/tap2subunit" % (srcdir(), srcdir(), python, srcdir())
68
69 sub = subprocess.Popen("tap2subunit 2> /dev/null", stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
70 sub.communicate("")
71
72 if sub.returncode == 0:
73     cmd = "echo -ne \"1..1\nok 1 # skip doesn't seem to work yet\n\" | tap2subunit 2> /dev/null | grep skip"
74     sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
75     if sub.returncode == 0:
76         tap2subunit = "tap2subunit"
77
78 def valgrindify(cmdline):
79     """Run a command under valgrind, if $VALGRIND was set."""
80     valgrind = os.getenv("VALGRIND")
81     if valgrind is None:
82         return cmdline
83     return valgrind + " " + cmdline
84
85
86 def plantestsuite(name, env, cmdline, allow_empty_output=False):
87     """Plan a test suite.
88
89     :param name: Testsuite name
90     :param env: Environment to run the testsuite in
91     :param cmdline: Command line to run
92     """
93     print "-- TEST --"
94     print name
95     print env
96     if isinstance(cmdline, list):
97         cmdline = " ".join(cmdline)
98     filter_subunit_args = []
99     if not allow_empty_output:
100         filter_subunit_args.append("--fail-on-empty")
101     if "$LISTOPT" in cmdline:
102         filter_subunit_args.append("$LISTOPT")
103     print "%s 2>&1 | %s/selftest/filter-subunit %s --prefix=\"%s.\"" % (cmdline,
104                                                                         srcdir(),
105                                                                         " ".join(filter_subunit_args),
106                                                                         name)
107     if allow_empty_output:
108         print "WARNING: allowing empty subunit output from %s" % name
109
110
111 def add_prefix(prefix, support_list=False):
112     if support_list:
113         listopt = "$LISTOPT "
114     else:
115         listopt = ""
116     return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\"" % (srcdir(), listopt, prefix)
117
118
119 def plantestsuite_loadlist(name, env, cmdline):
120     print "-- TEST-LOADLIST --"
121     if env == "none":
122         fullname = name
123     else:
124         fullname = "%s(%s)" % (name, env)
125     print fullname
126     print env
127     if isinstance(cmdline, list):
128         cmdline = " ".join(cmdline)
129     support_list = ("$LISTOPT" in cmdline)
130     print "%s $LOADLIST 2>&1 | %s" % (cmdline, add_prefix(name, support_list))
131
132
133 def plantestsuite_idlist(name, env, cmdline):
134     print "-- TEST-IDLIST --"
135     if env == "none":
136         fullname = name
137     else:
138         fullname = "%s(%s)" % (name, env)
139     print fullname
140     print env
141     if isinstance(cmdline, list):
142         cmdline = " ".join(cmdline)
143     print cmdline
144
145
146 def skiptestsuite(name, reason):
147     """Indicate that a testsuite was skipped.
148
149     :param name: Test suite name
150     :param reason: Reason the test suite was skipped
151     """
152     # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
153     print "skipping %s (%s)" % (name, reason)
154
155
156 def planperltestsuite(name, path):
157     """Run a perl test suite.
158
159     :param name: Name of the test suite
160     :param path: Path to the test runner
161     """
162     if has_perl_test_more:
163         plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
164     else:
165         skiptestsuite(name, "Test::More not available")
166
167
168 def planpythontestsuite(env, module, name=None, extra_path=[]):
169     if name is None:
170         name = module
171     pypath = list(extra_path)
172     if not has_system_subunit_run:
173         pypath.extend(["%s/lib/subunit/python" % srcdir(),
174             "%s/lib/testtools" % srcdir()])
175     args = [python, "-m", "subunit.run", "$LISTOPT", module]
176     if pypath:
177         args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
178     plantestsuite_idlist(name, env, args)