2 # vim: expandtab ft=python
9 from waflib
import Scripting
, Options
, Utils
10 from waflib
.ConfigSet
import ConfigSet
as Environment
12 from samba_utils
import *
13 from samba_autoconf
import *
16 DEFAULT_SELFTEST_PREFIX
="./st"
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
)
28 opt
.ADD_COMMAND('test', cmd_test
)
29 opt
.ADD_COMMAND('testonly', cmd_testonly
)
31 gr
= opt
.add_option_group('test options')
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)
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)
100 def cmd_testonly(opt
):
101 '''run tests without doing a build first'''
102 env
= LOAD_ENVIRONMENT()
105 if Options
.options
.SELFTEST_PREFIX
!= DEFAULT_SELFTEST_PREFIX
:
106 env
.SELFTEST_PREFIX
= Options
.options
.SELFTEST_PREFIX
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")
114 os
.environ
['SAMBA_SELFTEST'] = '1'
116 env
.TESTS
= Options
.options
.TESTS
118 env
.SUBUNIT_FORMATTER
= os
.getenv('SUBUNIT_FORMATTER')
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
)
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}'
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')
145 if Options
.options
.FAIL_IMMEDIATELY
:
146 env
.FILTER_XFAIL
+= ' --fail-immediately'
148 env
.FORMAT_TEST_OUTPUT
= '${SUBUNIT_FORMATTER}'
150 # clean any previous temporary files
151 os
.system("rm -rf %s/tmp" % env
.SELFTEST_PREFIX
);
153 # put all command line options in the environment as TESTENV_*=*
154 for o
in dir(Options
.options
):
156 val
= getattr(Options
.options
, o
, '')
157 if not issubclass(type(val
), types
.FunctionType
) \
158 and not issubclass(type(val
), types
.MethodType
):
159 os
.environ
['TESTENV_%s' % o
.upper()] = str(getattr(Options
.options
, o
, ''))
162 if not Options
.options
.SLOWTEST
:
163 env
.OPTIONS
+= ' --exclude=${srcdir}/selftest/slow'
164 if Options
.options
.QUICKTEST
:
165 env
.OPTIONS
+= ' --quick --include=${srcdir}/selftest/quick'
166 if Options
.options
.LOAD_LIST
:
167 env
.OPTIONS
+= ' --load-list=%s' % Options
.options
.LOAD_LIST
168 if Options
.options
.TESTENV
:
169 env
.OPTIONS
+= ' --testenv'
170 if Options
.options
.SOCKET_WRAPPER_PCAP
:
171 env
.OPTIONS
+= ' --socket-wrapper-pcap'
172 if Options
.options
.SOCKET_WRAPPER_KEEP_PCAP
:
173 env
.OPTIONS
+= ' --socket-wrapper-keep-pcap'
174 if Options
.options
.RANDOM_ORDER
:
175 env
.OPTIONS
+= ' --random-order'
176 if Options
.options
.PERF_TEST
:
177 env
.FILTER_OPTIONS
= ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
178 '--perf-test-output')
180 env
.FILTER_OPTIONS
= '${FILTER_XFAIL}'
182 if Options
.options
.VALGRIND
:
183 os
.environ
['VALGRIND'] = 'valgrind -q --num-callers=30'
184 if Options
.options
.VALGRINDLOG
is not None:
185 os
.environ
['VALGRIND'] += ' --log-file=%s' % Options
.options
.VALGRINDLOG
189 if Options
.options
.VALGRIND_SERVER
:
190 server_wrapper
= '${srcdir}/selftest/valgrind_run _DUMMY=X'
191 elif Options
.options
.GDBTEST
:
192 server_wrapper
= '${srcdir}/selftest/gdb_run _DUMMY=X'
194 if Options
.options
.SCREEN
:
195 server_wrapper
= '${srcdir}/selftest/in_screen %s' % server_wrapper
196 os
.environ
['TERMINAL'] = EXPAND_VARIABLES(opt
, '${srcdir}/selftest/in_screen')
197 elif server_wrapper
!= '':
198 server_wrapper
= 'xterm -n server -l -e %s' % server_wrapper
200 if server_wrapper
!= '':
201 os
.environ
['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
202 os
.environ
['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
203 os
.environ
['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
204 os
.environ
['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
205 os
.environ
['SAMBA_DCERPCD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
207 # this is needed for systems without rpath, or with rpath disabled
208 ADD_LD_LIBRARY_PATH('bin/shared')
209 ADD_LD_LIBRARY_PATH('bin/shared/private')
211 # if we are using a system version of ldb then we need to tell it to
212 # load modules from our modules path
213 if env
.USING_SYSTEM_LDB
:
214 os
.environ
['LDB_MODULES_PATH'] = os
.path
.abspath(
215 os
.path
.join(*(env
.cwd
+ ['bin/modules/ldb'])))
217 # tell build system where to find config.h
218 os
.environ
['CONFIG_H'] = 'bin/default/include/config.h'
220 # tell the test system where perl is
221 if isinstance(env
.PERL
, list):
222 perl
= ' '.join(env
.PERL
)
225 os
.environ
['PERL'] = perl
227 st_done
= os
.path
.join(env
.SELFTEST_PREFIX
, 'st_done')
228 if os
.path
.exists(st_done
):
231 if not os
.path
.isdir(env
.SELFTEST_PREFIX
):
232 os
.makedirs(env
.SELFTEST_PREFIX
, int('755', 8))
234 if Options
.options
.TEST_LIST
:
235 env
.TESTLISTS
= '--testlist=%r' % Options
.options
.TEST_LIST
236 elif Options
.options
.PERF_TEST
:
237 env
.TESTLISTS
= '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
239 env
.TESTLISTS
= ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
240 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
241 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
243 if CONFIG_SET(opt
, 'AD_DC_BUILD_IS_ENABLED'):
244 env
.SELFTEST_TARGET
= "samba"
246 env
.SELFTEST_TARGET
= "samba3"
248 env
.OPTIONS
+= " --nss_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBNSS_WRAPPER_SO_PATH')
249 env
.OPTIONS
+= " --resolv_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBRESOLV_WRAPPER_SO_PATH')
250 env
.OPTIONS
+= " --uid_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBUID_WRAPPER_SO_PATH')
252 # selftest can optionally use kernel namespaces instead of socket-wrapper
253 if os
.environ
.get('USE_NAMESPACES') is None:
254 env
.OPTIONS
+= " --socket_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBSOCKET_WRAPPER_SO_PATH')
256 if not CONFIG_SET(opt
, 'HAVE_RESOLV_CONF_SUPPORT'):
257 env
.OPTIONS
+= " --use-dns-faking"
259 if CONFIG_GET(opt
, 'USING_SYSTEM_KRB5'):
260 env
.OPTIONS
+= " --mitkrb5"
262 if CONFIG_GET(opt
, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt
, 'MIT_KDC_PATH'):
263 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip_mit_kdc"
264 if CONFIG_GET(opt
, 'HAVE_MIT_KRB5_PRE_1_20'):
265 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip_mit_kdc_pre_1_20"
267 env
.FILTER_XFAIL
+= " --expected-failures=${srcdir}/selftest/"\
270 if CONFIG_GET(opt
, 'HAVE_MIT_KRB5_PRE_1_20'):
271 env
.FILTER_XFAIL
+= ' --expected-failures=${srcdir}/selftest/knownfail_mit_kdc_pre_1_20'
273 if CONFIG_GET(opt
, 'HAVE_MIT_KRB5_1_20'):
274 env
.FILTER_XFAIL
+= ' --expected-failures=${srcdir}/selftest/knownfail_mit_kdc_1_20'
276 env
.FILTER_XFAIL
+= " --expected-failures=${srcdir}/selftest/"\
277 "knownfail_heimdal_kdc"
279 if CONFIG_GET(opt
, 'SIZEOF_VOID_P') == 4:
280 env
.FILTER_XFAIL
+= " --expected-failures=${srcdir}/selftest/knownfail-32bit"
281 env
.OPTIONS
+= " --default-ldb-backend=tdb --exclude=${srcdir}/selftest/skip-32bit"
283 if not CONFIG_GET(opt
, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
284 # older MIT krb5 libraries (< 1.14) don't have
285 # GSS_KRB5_CRED_NO_CI_FLAGS_X
286 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
288 if os
.environ
.get('DISABLE_OPATH'):
289 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip.opath-required"
292 if env
.ADDRESS_SANITIZER
:
293 # We try to find the correct libasan automatically
294 libasan
= Utils
.cmd_output(
295 r
'ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
297 libasan
= libasan
.decode('utf8')
299 # Have the selftest.pl LD_PRELOAD libasan in the right spot
300 env
.OPTIONS
+= " --asan_so_path=" + libasan
302 if CONFIG_SET(opt
, 'HAVE_CRYPT_R'):
303 # We try to find the correct libcrypt automatically
304 libcrypt
= Utils
.cmd_output(
305 'ldd bin/modules/ldb/password_hash.so | awk \'/libcrypt.so/ { print $3 }\'',
307 libcrypt
= libcrypt
.decode('utf8')
308 env
.OPTIONS
+= " --crypt_so_path=" + libcrypt
311 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
312 env
.CORE_COMMAND
= '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
314 # If using namespaces (rather than socket-wrapper), run the selftest script
315 # in its own network namespace (by doing an 'unshare'). (To create a new
316 # namespace as a non-root user, we have to also unshare the current user
317 # namespace, and remap ourself as root in the namespace created)
318 if os
.environ
.get('USE_NAMESPACES') is not None:
319 env
.CORE_COMMAND
= 'unshare --net --user --map-root-user ' + env
.CORE_COMMAND
321 if env
.ADDRESS_SANITIZER
and libasan
:
322 # For now we cannot run with leak and odr detection
323 asan_options
= "ASAN_OPTIONS=detect_leaks=0"
324 asan_options
+= ":detect_odr_violation=0"
325 # uncomment if you need asan logs
326 # asan_options += ":verbosity=111"
327 asan_options
+= ":suppressions=${srcdir}/selftest/sanitizer/asan.supp"
330 # And we need to disable RTLD_DEEPBIND in ldb and socket wrapper
331 no_leak_check
= "LDB_MODULES_DISABLE_DEEPBIND=1 "
332 no_leak_check
+= "SOCKET_WRAPPER_DISABLE_DEEP_BIND=1"
334 env
.CORE_COMMAND
= asan_options
+ no_leak_check
+ env
.CORE_COMMAND
336 # We need to have the subunit filter and formatter preload
337 # libasan otherwise the tests fail at startup.
339 # Also, we do not care about leaks in python
341 asan_envs
= (asan_options
+ no_leak_check
+ "LD_PRELOAD=" + libasan
343 env
.FILTER_OPTIONS
= asan_envs
+ env
.FILTER_OPTIONS
344 env
.SUBUNIT_FORMATTER
= asan_envs
+ env
.SUBUNIT_FORMATTER
346 if env
.UNDEFINED_SANITIZER
:
347 # print a stack trace with the error.
348 print_stack_trace
= "UBSAN_OPTIONS=print_stacktrace=1"
349 print_stack_trace
+= ",suppressions=${srcdir}/selftest/ubsan.supp"
350 env
.CORE_COMMAND
= print_stack_trace
+ " " + env
.CORE_COMMAND
352 if Options
.options
.LIST
:
353 cmd
= '${CORE_COMMAND} --list'
355 env
.OPTIONS
+= ' --socket-wrapper'
356 cmd
= '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
358 if Options
.options
.NO_SUBUNIT_FILTER
:
359 # Skip subunit filtering (i.e. because python is disabled).
360 # Use --one to bail out upon any failure
361 cmd
= '(${CORE_COMMAND} --one && touch ${SELFTEST_PREFIX}/st_done)'
362 elif not Options
.options
.FILTERED_SUBUNIT
:
363 subunit_cache
= os
.path
.join(env
.SELFTEST_PREFIX
, "subunit")
364 cmd
+= ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
366 cmd
+= ' | ${FILTER_OPTIONS}'
368 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
370 print("test: running %s" % runcmd
)
371 ret
= RUN_COMMAND(cmd
, env
=env
)
373 if (os
.path
.exists(".testrepository") and
374 not Options
.options
.LIST
and
375 not Options
.options
.LOAD_LIST
and
376 subunit_cache
is not None):
377 testrcmd
= 'testr load -q < %s > /dev/null' % subunit_cache
378 runcmd
= EXPAND_VARIABLES(opt
, testrcmd
)
379 RUN_COMMAND(runcmd
, env
=env
)
381 if subunit_cache
is not None:
382 nb
= Options
.options
.NB_SLOWEST
383 cmd
= "./script/show_testsuite_time %s %d" % (subunit_cache
, nb
)
384 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
385 RUN_COMMAND(runcmd
, env
=env
)
388 print("ERROR: test failed with exit code %d" % ret
)
391 if not Options
.options
.LIST
and not os
.path
.exists(st_done
):
392 print("ERROR: test command failed to complete")
396 ########################################################################
397 # main test entry point
399 '''Run the test suite (see test options below)'''
401 # if running all tests, then force a symbol check
402 env
= LOAD_ENVIRONMENT()
404 Options
.commands
.append('build')
405 Options
.commands
.append('testonly')