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