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 CONFIG_GET(opt
, 'HAVE_MIT_KRB5_PRE_1_18'):
146 env
.FILTER_XFAIL
+= ' --expected-failures=${srcdir}/selftest/knownfail_mit_krb5_pre_1_18'
148 if Options
.options
.FAIL_IMMEDIATELY
:
149 env
.FILTER_XFAIL
+= ' --fail-immediately'
151 env
.FORMAT_TEST_OUTPUT
= '${SUBUNIT_FORMATTER}'
153 # clean any previous temporary files
154 os
.system("rm -rf %s/tmp" % env
.SELFTEST_PREFIX
);
156 # put all command line options in the environment as TESTENV_*=*
157 for o
in dir(Options
.options
):
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
, ''))
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')
183 env
.FILTER_OPTIONS
= '${FILTER_XFAIL}'
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
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'
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
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
)
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')
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'])))
219 # tell build system where to find config.h
220 os
.environ
['CONFIG_H'] = 'bin/default/include/config.h'
222 # tell the test system where perl is
223 if isinstance(env
.PERL
, list):
224 perl
= ' '.join(env
.PERL
)
227 os
.environ
['PERL'] = perl
229 st_done
= os
.path
.join(env
.SELFTEST_PREFIX
, 'st_done')
230 if os
.path
.exists(st_done
):
233 if not os
.path
.isdir(env
.SELFTEST_PREFIX
):
234 os
.makedirs(env
.SELFTEST_PREFIX
, int('755', 8))
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|" '
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|"')
245 if CONFIG_SET(opt
, 'AD_DC_BUILD_IS_ENABLED'):
246 env
.SELFTEST_TARGET
= "samba"
248 env
.SELFTEST_TARGET
= "samba3"
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')
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')
258 if Utils
.unversioned_sys_platform() in ('netbsd', 'openbsd', 'sunos'):
259 env
.OPTIONS
+= " --use-dns-faking"
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/"\
266 env
.FILTER_XFAIL
+= " --expected-failures=${srcdir}/selftest/"\
267 "knownfail_heimdal_kdc"
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"
274 if env
.ADDRESS_SANITIZER
:
275 # We try to find the correct libasan automatically
276 libasan
= Utils
.cmd_output(
277 'ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
279 libasan
= libasan
.decode('utf8')
281 # Have the selftest.pl LD_PRELOAD libasan in the right spot
282 env
.OPTIONS
+= " --asan_so_path=" + libasan
285 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
286 env
.CORE_COMMAND
= '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
288 # If using namespaces (rather than socket-wrapper), run the selftest script
289 # in its own network namespace (by doing an 'unshare'). (To create a new
290 # namespace as a non-root user, we have to also unshare the current user
291 # namespace, and remap ourself as root in the namespace created)
292 if os
.environ
.get('USE_NAMESPACES') is not None:
293 env
.CORE_COMMAND
= 'unshare --net --user --map-root-user ' + env
.CORE_COMMAND
295 if env
.ADDRESS_SANITIZER
:
296 # For now we cannot run with leak detection
297 no_leak_check
= "ASAN_OPTIONS=detect_leaks=0 "
298 # And we need to disable RTLD_DEEPBIND in ldb and socket wrapper
299 no_leak_check
+= "LDB_MODULES_DISABLE_DEEPBIND=1 "
300 no_leak_check
+= "SOCKET_WRAPPER_DISABLE_DEEP_BIND=1"
301 env
.CORE_COMMAND
= no_leak_check
+ " " + env
.CORE_COMMAND
303 # We need to have the subunit filter and formatter preload
304 # libasan otherwise the tests fail at startup.
306 # Also, we do not care about leaks in python
308 asan_envs
= no_leak_check
+ " LD_PRELOAD=" + libasan
+ ' '
309 env
.FILTER_OPTIONS
= asan_envs
+ env
.FILTER_OPTIONS
310 env
.SUBUNIT_FORMATTER
= asan_envs
+ env
.SUBUNIT_FORMATTER
312 if env
.UNDEFINED_SANITIZER
:
313 # print a stack trace with the error.
314 print_stack_trace
= "UBSAN_OPTIONS=print_stacktrace=1"
315 print_stack_trace
+= ",suppressions=${srcdir}/selftest/ubsan.supp"
316 env
.CORE_COMMAND
= print_stack_trace
+ " " + env
.CORE_COMMAND
318 if Options
.options
.LIST
:
319 cmd
= '${CORE_COMMAND} --list'
321 env
.OPTIONS
+= ' --socket-wrapper'
322 cmd
= '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
324 if Options
.options
.NO_SUBUNIT_FILTER
:
325 # Skip subunit filtering (i.e. because python is disabled).
326 # Use --one to bail out upon any failure
327 cmd
= '(${CORE_COMMAND} --one && touch ${SELFTEST_PREFIX}/st_done)'
328 elif not Options
.options
.FILTERED_SUBUNIT
:
329 subunit_cache
= os
.path
.join(env
.SELFTEST_PREFIX
, "subunit")
330 cmd
+= ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
332 cmd
+= ' | ${FILTER_OPTIONS}'
334 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
336 print("test: running %s" % runcmd
)
337 ret
= RUN_COMMAND(cmd
, env
=env
)
339 if (os
.path
.exists(".testrepository") and
340 not Options
.options
.LIST
and
341 not Options
.options
.LOAD_LIST
and
342 subunit_cache
is not None):
343 testrcmd
= 'testr load -q < %s > /dev/null' % subunit_cache
344 runcmd
= EXPAND_VARIABLES(opt
, testrcmd
)
345 RUN_COMMAND(runcmd
, env
=env
)
347 if subunit_cache
is not None:
348 nb
= Options
.options
.NB_SLOWEST
349 cmd
= "./script/show_testsuite_time %s %d" % (subunit_cache
, nb
)
350 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
351 RUN_COMMAND(runcmd
, env
=env
)
354 print("ERROR: test failed with exit code %d" % ret
)
357 if not Options
.options
.LIST
and not os
.path
.exists(st_done
):
358 print("ERROR: test command failed to complete")
362 ########################################################################
363 # main test entry point
365 '''Run the test suite (see test options below)'''
367 # if running all tests, then force a symbol check
368 env
= LOAD_ENVIRONMENT()
370 Options
.commands
.append('build')
371 Options
.commands
.append('testonly')