selftest: add snapshot share configuration
[sfrench/samba-autobuild/.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 python = os.getenv("PYTHON", "python")
62
63 tap2subunit = python + " " + os.path.join(srcdir(), "selftest", "tap2subunit")
64
65
66 def valgrindify(cmdline):
67     """Run a command under valgrind, if $VALGRIND was set."""
68     valgrind = os.getenv("VALGRIND")
69     if valgrind is None:
70         return cmdline
71     return valgrind + " " + cmdline
72
73
74 def plantestsuite(name, env, cmdline):
75     """Plan a test suite.
76
77     :param name: Testsuite name
78     :param env: Environment to run the testsuite in
79     :param cmdline: Command line to run
80     """
81     print "-- TEST --"
82     print name
83     print env
84     if isinstance(cmdline, list):
85         cmdline = " ".join(cmdline)
86     if "$LISTOPT" in cmdline:
87         raise AssertionError("test %s supports --list, but not --load-list" % name)
88     print cmdline + " 2>&1 " + " | " + add_prefix(name, env)
89
90
91 def add_prefix(prefix, env, support_list=False):
92     if support_list:
93         listopt = "$LISTOPT "
94     else:
95         listopt = ""
96     return "%s/selftest/filter-subunit %s--fail-on-empty --prefix=\"%s.\" --suffix=\"(%s)\"" % (srcdir(), listopt, prefix, env)
97
98
99 def plantestsuite_loadlist(name, env, cmdline):
100     print "-- TEST-LOADLIST --"
101     if env == "none":
102         fullname = name
103     else:
104         fullname = "%s(%s)" % (name, env)
105     print fullname
106     print env
107     if isinstance(cmdline, list):
108         cmdline = " ".join(cmdline)
109     support_list = ("$LISTOPT" in cmdline)
110     if not "$LISTOPT" in cmdline:
111         raise AssertionError("loadlist test %s does not support not --list" % name)
112     if not "$LOADLIST" in cmdline:
113         raise AssertionError("loadlist test %s does not support --load-list" % name)
114     print ("%s | %s" % (cmdline.replace("$LOADLIST", ""), add_prefix(name, env, support_list))).replace("$LISTOPT", "--list")
115     print cmdline.replace("$LISTOPT", "") + " 2>&1 " + " | " + add_prefix(name, env, False)
116
117
118 def skiptestsuite(name, reason):
119     """Indicate that a testsuite was skipped.
120
121     :param name: Test suite name
122     :param reason: Reason the test suite was skipped
123     """
124     # FIXME: Report this using subunit, but re-adjust the testsuite count somehow
125     print >>sys.stderr, "skipping %s (%s)" % (name, reason)
126
127
128 def planperltestsuite(name, path):
129     """Run a perl test suite.
130
131     :param name: Name of the test suite
132     :param path: Path to the test runner
133     """
134     if has_perl_test_more:
135         plantestsuite(name, "none", "%s %s | %s" % (" ".join(perl), path, tap2subunit))
136     else:
137         skiptestsuite(name, "Test::More not available")
138
139
140 def planpythontestsuite(env, module, name=None, extra_path=[]):
141     if name is None:
142         name = module
143     pypath = list(extra_path)
144     args = [python, "-m", "samba.subunit.run", "$LISTOPT", "$LOADLIST", module]
145     if pypath:
146         args.insert(0, "PYTHONPATH=%s" % ":".join(["$PYTHONPATH"] + pypath))
147     plantestsuite_loadlist(name, env, args)
148
149
150 def get_env_torture_options():
151     ret = []
152     if not os.getenv("SELFTEST_VERBOSE"):
153         ret.append("--option=torture:progress=no")
154     if os.getenv("SELFTEST_QUICK"):
155         ret.append("--option=torture:quick=yes")
156     return ret
157
158
159 samba4srcdir = source4dir()
160 samba3srcdir = source3dir()
161 bbdir = os.path.join(srcdir(), "testprogs/blackbox")
162 configuration = "--configfile=$SMB_CONF_PATH"
163
164 smbtorture4 = binpath("smbtorture4")
165 smbtorture4_testsuite_list = subprocess.Popen([smbtorture4, "--list-suites"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate("")[0].splitlines()
166
167 smbtorture4_options = [
168     configuration,
169     "--option=\'fss:sequence timeout=1\'",
170     "--maximum-runtime=$SELFTEST_MAXTIME",
171     "--basedir=$SELFTEST_TMPDIR",
172     "--format=subunit"
173     ] + get_env_torture_options()
174
175
176 def plansmbtorture4testsuite(name, env, options, target, modname=None):
177     if modname is None:
178         modname = "samba4.%s" % name
179     if isinstance(options, list):
180         options = " ".join(options)
181     options = " ".join(smbtorture4_options + ["--target=%s" % target]) + " " + options
182     cmdline = "%s $LISTOPT $LOADLIST %s %s" % (valgrindify(smbtorture4), options, name)
183     plantestsuite_loadlist(modname, env, cmdline)
184
185
186 def smbtorture4_testsuites(prefix):
187     return filter(lambda x: x.startswith(prefix), smbtorture4_testsuite_list)
188
189
190 smbclient3 = binpath('smbclient3')
191 smbtorture3 = binpath('smbtorture3')
192 ntlm_auth3 = binpath('ntlm_auth3')
193 net = binpath('net')
194 scriptdir = os.path.join(srcdir(), "script/tests")
195
196 wbinfo = binpath('wbinfo')
197 dbwrap_tool = binpath('dbwrap_tool')
198 vfstest = binpath('vfstest')