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