tests/auto_log_pass_change.py: only care about the last expected message other than...
[samba.git] / selftest / wscript
1 #!/usr/bin/env python
2 # vim: expandtab ft=python
3
4 # selftest main code.
5
6 import sys
7 import os
8 import optparse
9 from waflib import Scripting, Options, Utils
10 from waflib.ConfigSet import ConfigSet as Environment
11
12 from samba_utils import *
13 from samba_autoconf import *
14 import types
15
16 DEFAULT_SELFTEST_PREFIX="./st"
17
18 def options(opt):
19
20     opt.add_option('--enable-selftest',
21                    help=("enable options necessary for selftest (default=no)"),
22                    action="store_true", dest='enable_selftest', default=False)
23     opt.add_option('--enable-coverage',
24                    help=("enable options necessary for code coverage "
25                          "reporting on selftest (default=no)"),
26                    action="store_true", dest='enable_coverage', default=False)
27     opt.add_option('--with-selftest-prefix',
28                    help=("specify location of selftest directory "
29                          "(default=%s)" % DEFAULT_SELFTEST_PREFIX),
30                    action="store", dest='SELFTEST_PREFIX', default=DEFAULT_SELFTEST_PREFIX)
31
32     opt.ADD_COMMAND('test', cmd_test)
33     opt.ADD_COMMAND('testonly', cmd_testonly)
34
35     gr = opt.add_option_group('test options')
36
37     gr.add_option('--load-list',
38                   help=("Load a test id list from a text file"),
39                   action="store", dest='LOAD_LIST', default=None)
40     gr.add_option('--list',
41                   help=("List available tests"),
42                   action="store_true", dest='LIST', default=False)
43     gr.add_option('--tests',
44                   help=("wildcard pattern of tests to run"),
45                   action="store", dest='TESTS', default='')
46     gr.add_option('--filtered-subunit',
47                   help=("output (xfail) filtered subunit"),
48                   action="store_true", dest='FILTERED_SUBUNIT', default=False)
49     gr.add_option('--quick',
50                   help=("enable only quick tests"),
51                   action="store_true", dest='QUICKTEST', default=False)
52     gr.add_option('--slow',
53                   help=("enable the really slow tests"),
54                   action="store_true", dest='SLOWTEST', default=False)
55     gr.add_option('--nb-slowest',
56                   help=("Show the n slowest tests (default=10)"),
57                   type=int, default=10, dest='NB_SLOWEST')
58     gr.add_option('--testenv',
59                   help=("start a terminal with the test environment setup"),
60                   action="store_true", dest='TESTENV', default=False)
61     gr.add_option('--valgrind',
62                   help=("use valgrind on client programs in the tests"),
63                   action="store_true", dest='VALGRIND', default=False)
64     gr.add_option('--valgrind-log',
65                   help=("where to put the valgrind log"),
66                   action="store", dest='VALGRINDLOG', default=None)
67     gr.add_option('--valgrind-server',
68                   help=("use valgrind on the server in the tests (opens an xterm)"),
69                   action="store_true", dest='VALGRIND_SERVER', default=False)
70     gr.add_option('--screen',
71                   help=("run the samba servers in screen sessions"),
72                   action="store_true", dest='SCREEN', default=False)
73     gr.add_option('--gdbtest',
74                   help=("run the servers within a gdb window"),
75                   action="store_true", dest='GDBTEST', default=False)
76     gr.add_option('--fail-immediately',
77                   help=("stop tests on first failure"),
78                   action="store_true", dest='FAIL_IMMEDIATELY', default=False)
79     gr.add_option('--socket-wrapper-pcap',
80                   help=("create a pcap file for each failing test"),
81                   action="store_true", dest='SOCKET_WRAPPER_PCAP', default=False)
82     gr.add_option('--socket-wrapper-keep-pcap',
83                   help=("create a pcap file for all individual test"),
84                   action="store_true", dest='SOCKET_WRAPPER_KEEP_PCAP', default=False)
85     gr.add_option('--random-order', dest='RANDOM_ORDER', default=False,
86                   action="store_true", help="Run testsuites in random order")
87     gr.add_option('--perf-test', dest='PERF_TEST', default=False,
88                   action="store_true", help="run performance tests only")
89     gr.add_option('--test-list', dest='TEST_LIST', default='',
90                   help=("use tests listed here, not defaults "
91                         "(--test-list='FOO|' will execute FOO; "
92                         "--test-list='FOO' will read it)"))
93     gr.add_option('--no-subunit-filter',
94                   help=("no (xfail) subunit filtering"),
95                   action="store_true", dest='NO_SUBUNIT_FILTER', default=False)
96
97
98 def configure(conf):
99     conf.env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
100     conf.env.enable_coverage = Options.options.enable_coverage
101     if conf.env.enable_coverage:
102         conf.ADD_LDFLAGS('-lgcov', testflags=True)
103         conf.ADD_CFLAGS('--coverage')
104
105     if Options.options.enable_selftest or Options.options.developer:
106         conf.DEFINE('ENABLE_SELFTEST', 1)
107
108
109 def cmd_testonly(opt):
110     '''run tests without doing a build first'''
111     env = LOAD_ENVIRONMENT()
112     opt.env = env
113
114     if Options.options.SELFTEST_PREFIX != DEFAULT_SELFTEST_PREFIX:
115         env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
116
117     if (not CONFIG_SET(opt, 'NSS_WRAPPER') or
118         not CONFIG_SET(opt, 'UID_WRAPPER') or
119         not CONFIG_SET(opt, 'SOCKET_WRAPPER')):
120         print("ERROR: You must use --enable-selftest to enable selftest")
121         sys.exit(1)
122
123     os.environ['SAMBA_SELFTEST'] = '1'
124
125     env.TESTS  = Options.options.TESTS
126
127     env.SUBUNIT_FORMATTER = os.getenv('SUBUNIT_FORMATTER')
128
129     # Lots of test scripts need to run with the correct version
130     # of python. With the correct shebang the script should run with the
131     # correct version, the problem is that not all scripts are part
132     # of the installation, some scripts are part of the source code,
133     # and the shebang is not dynamically generated as yet.
134     # It is safer if we are somewhat version neutral at the moment and
135     # ignore the shebang and always run scripts from the test environment
136     # with the python version (determined by PYTHON env variable) If this
137     # env variable isn't set then set it according to the python version
138     # that is running the tests
139     if not os.getenv('PYTHON', None):
140         from sys import executable as exe
141         os.environ['PYTHON'] = os.path.basename(exe)
142
143     if not env.SUBUNIT_FORMATTER:
144         if Options.options.PERF_TEST:
145             env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit-json --prefix=${SELFTEST_PREFIX}'
146         else:
147             env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
148     env.FILTER_XFAIL = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
149                         '--expected-failures=${srcdir}/selftest/knownfail '
150                         '--expected-failures=${srcdir}/selftest/knownfail.d '
151                         '--flapping=${srcdir}/selftest/flapping '
152                         '--flapping=${srcdir}/selftest/flapping.d')
153
154     if Options.options.FAIL_IMMEDIATELY:
155         env.FILTER_XFAIL += ' --fail-immediately'
156
157     env.FORMAT_TEST_OUTPUT = '${SUBUNIT_FORMATTER}'
158
159     # clean any previous temporary files
160     os.system("rm -rf %s/tmp" % env.SELFTEST_PREFIX);
161
162     # put all command line options in the environment as TESTENV_*=*
163     for o in dir(Options.options):
164         if o[0:1] != '_':
165             val = getattr(Options.options, o, '')
166             if not issubclass(type(val), types.FunctionType) \
167                     and not issubclass(type(val), types.MethodType):
168                 os.environ['TESTENV_%s' % o.upper()] = str(getattr(Options.options, o, ''))
169
170     env.OPTIONS = ''
171     if not Options.options.SLOWTEST:
172         env.OPTIONS += ' --exclude=${srcdir}/selftest/slow'
173     if Options.options.QUICKTEST:
174         env.OPTIONS += ' --quick --include=${srcdir}/selftest/quick'
175     if Options.options.LOAD_LIST:
176         env.OPTIONS += ' --load-list=%s' % Options.options.LOAD_LIST
177     if Options.options.TESTENV:
178         env.OPTIONS += ' --testenv'
179     if Options.options.SOCKET_WRAPPER_PCAP:
180         env.OPTIONS += ' --socket-wrapper-pcap'
181     if Options.options.SOCKET_WRAPPER_KEEP_PCAP:
182         env.OPTIONS += ' --socket-wrapper-keep-pcap'
183     if Options.options.RANDOM_ORDER:
184         env.OPTIONS += ' --random-order'
185     if Options.options.PERF_TEST:
186         env.FILTER_OPTIONS = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
187                               '--perf-test-output')
188     elif os.environ.get('RUN_FROM_BUILD_FARM') is not None:
189         env.FILTER_OPTIONS = '${FILTER_XFAIL} --strip-passed-output'
190     else:
191         env.FILTER_OPTIONS = '${FILTER_XFAIL}'
192
193     if Options.options.VALGRIND:
194         os.environ['VALGRIND'] = 'valgrind -q --num-callers=30'
195         if Options.options.VALGRINDLOG is not None:
196             os.environ['VALGRIND'] += ' --log-file=%s' % Options.options.VALGRINDLOG
197
198     server_wrapper=''
199
200     if Options.options.VALGRIND_SERVER:
201         server_wrapper = '${srcdir}/selftest/valgrind_run _DUMMY=X'
202     elif Options.options.GDBTEST:
203         server_wrapper = '${srcdir}/selftest/gdb_run _DUMMY=X'
204
205     if Options.options.SCREEN:
206         server_wrapper = '${srcdir}/selftest/in_screen %s' % server_wrapper
207         os.environ['TERMINAL'] = EXPAND_VARIABLES(opt, '${srcdir}/selftest/in_screen')
208     elif server_wrapper != '':
209         server_wrapper = 'xterm -n server -l -e %s' % server_wrapper
210
211     if server_wrapper != '':
212         os.environ['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
213         os.environ['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
214         os.environ['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
215         os.environ['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
216
217     # this is needed for systems without rpath, or with rpath disabled
218     ADD_LD_LIBRARY_PATH('bin/shared')
219     ADD_LD_LIBRARY_PATH('bin/shared/private')
220
221     # if we are using a system version of ldb then we need to tell it to
222     # load modules from our modules path
223     if env.USING_SYSTEM_LDB:
224         os.environ['LDB_MODULES_PATH'] = os.path.abspath(
225                 os.path.join(*(env.cwd + ['bin/modules/ldb'])))
226
227     # tell build system where to find config.h
228     os.environ['CONFIG_H'] = 'bin/default/include/config.h'
229
230     # tell the test system where perl is
231     if isinstance(env.PERL, list):
232         perl = ' '.join(env.PERL)
233     else:
234         perl = env.PERL
235     os.environ['PERL'] = perl
236
237     st_done = os.path.join(env.SELFTEST_PREFIX, 'st_done')
238     if os.path.exists(st_done):
239         os.unlink(st_done)
240
241     if not os.path.isdir(env.SELFTEST_PREFIX):
242         os.makedirs(env.SELFTEST_PREFIX, int('755', 8))
243
244     if Options.options.TEST_LIST:
245         env.TESTLISTS = '--testlist=%r' % Options.options.TEST_LIST
246     elif Options.options.PERF_TEST:
247         env.TESTLISTS = '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
248     elif CONFIG_SET(opt, 'EXTRA_PYTHON'):
249         env.TESTLISTS = ('--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/selftest/tests.py|" ' +
250                          '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
251                          '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
252     else:
253         env.TESTLISTS = ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
254                          '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
255                          '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
256
257     if CONFIG_SET(opt, 'AD_DC_BUILD_IS_ENABLED'):
258         env.SELFTEST_TARGET = "samba"
259     else:
260         env.SELFTEST_TARGET = "samba3"
261
262     env.OPTIONS += " --nss_wrapper_so_path=" + CONFIG_GET(opt, 'LIBNSS_WRAPPER_SO_PATH')
263     env.OPTIONS += " --resolv_wrapper_so_path=" + CONFIG_GET(opt, 'LIBRESOLV_WRAPPER_SO_PATH')
264     env.OPTIONS += " --socket_wrapper_so_path=" + CONFIG_GET(opt, 'LIBSOCKET_WRAPPER_SO_PATH')
265     env.OPTIONS += " --uid_wrapper_so_path=" + CONFIG_GET(opt, 'LIBUID_WRAPPER_SO_PATH')
266
267     #if unversioned_sys_platform in ('freebsd', 'netbsd', 'openbsd', 'sunos'):
268     #    env.OPTIONS += " --use-dns-faking"
269
270     # FIXME REMOVE ME!
271     env.OPTIONS += " --use-dns-faking"
272
273     if CONFIG_GET(opt, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt, 'MIT_KDC_PATH'):
274         env.OPTIONS += " --mitkrb5 --exclude=${srcdir}/selftest/skip_mit_kdc"
275
276     if not CONFIG_GET(opt, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
277         # older MIT krb5 libraries (< 1.14) don't have
278         # GSS_KRB5_CRED_NO_CI_FLAGS_X
279         env.OPTIONS += " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
280
281     if env.ADDRESS_SANITIZER:
282         # We try to find the correct libasan automatically
283         libasan = Utils.cmd_output('ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
284                                    silent=True).strip()
285
286         # Have the selftest.pl LD_PRELOAD libasan in the right spot
287         env.OPTIONS += " --asan_so_path=" + libasan
288
289     subunit_cache = None
290     # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
291     env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
292
293     if env.ADDRESS_SANITIZER:
294         # For now we cannot run with leak detection
295         no_leak_check = "ASAN_OPTIONS=detect_leaks=0"
296         env.CORE_COMMAND = no_leak_check + " " + env.CORE_COMMAND
297
298         # We need to have the subunit filter and formatter preload
299         # libasan otherwise the tests fail at startup.
300         #
301         # Also, we do not care about leaks in python
302
303         asan_envs = no_leak_check + " LD_PRELOAD=" + libasan + ' '
304         env.FILTER_OPTIONS = asan_envs + env.FILTER_OPTIONS
305         env.SUBUNIT_FORMATTER = asan_envs + env.SUBUNIT_FORMATTER
306
307     if Options.options.LIST:
308         cmd = '${CORE_COMMAND} --list'
309     else:
310         env.OPTIONS += ' --socket-wrapper'
311         cmd = '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
312
313         if Options.options.NO_SUBUNIT_FILTER:
314             # Skip subunit filtering (i.e. because python is disabled).
315             # Use --one to bail out upon any failure
316             cmd = '(${CORE_COMMAND} --one && touch ${SELFTEST_PREFIX}/st_done)'
317         elif (os.environ.get('RUN_FROM_BUILD_FARM') is None and
318             not Options.options.FILTERED_SUBUNIT):
319             subunit_cache = os.path.join(env.SELFTEST_PREFIX, "subunit")
320             cmd += ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
321         else:
322             cmd += ' | ${FILTER_OPTIONS}'
323
324     runcmd = EXPAND_VARIABLES(opt, cmd)
325
326     print("test: running %s" % runcmd)
327     ret = RUN_COMMAND(cmd, env=env)
328
329     if (os.path.exists(".testrepository") and
330         not Options.options.LIST and
331         not Options.options.LOAD_LIST and
332         subunit_cache is not None):
333         testrcmd = 'testr load -q < %s > /dev/null' % subunit_cache
334         runcmd = EXPAND_VARIABLES(opt, testrcmd)
335         RUN_COMMAND(runcmd, env=env)
336
337     if subunit_cache is not None:
338         nb = Options.options.NB_SLOWEST
339         cmd = "./script/show_testsuite_time %s %d" % (subunit_cache, nb)
340         runcmd = EXPAND_VARIABLES(opt, cmd)
341         RUN_COMMAND(runcmd, env=env)
342
343     if ret != 0:
344         print("ERROR: test failed with exit code %d" % ret)
345         sys.exit(ret)
346
347     if not Options.options.LIST and not os.path.exists(st_done):
348         print("ERROR: test command failed to complete")
349         sys.exit(1)
350
351
352 ########################################################################
353 # main test entry point
354 def cmd_test(opt):
355     '''Run the test suite (see test options below)'''
356
357     # if running all tests, then force a symbol check
358     env = LOAD_ENVIRONMENT()
359     CHECK_MAKEFLAGS(env)
360     Options.commands.append('build')
361     Options.commands.append('testonly')