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