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
)
206 # this is needed for systems without rpath, or with rpath disabled
207 ADD_LD_LIBRARY_PATH('bin/shared')
208 ADD_LD_LIBRARY_PATH('bin/shared/private')
210 # if we are using a system version of ldb then we need to tell it to
211 # load modules from our modules path
212 if env
.USING_SYSTEM_LDB
:
213 os
.environ
['LDB_MODULES_PATH'] = os
.path
.abspath(
214 os
.path
.join(*(env
.cwd
+ ['bin/modules/ldb'])))
216 # tell build system where to find config.h
217 os
.environ
['CONFIG_H'] = 'bin/default/include/config.h'
219 # tell the test system where perl is
220 if isinstance(env
.PERL
, list):
221 perl
= ' '.join(env
.PERL
)
224 os
.environ
['PERL'] = perl
226 st_done
= os
.path
.join(env
.SELFTEST_PREFIX
, 'st_done')
227 if os
.path
.exists(st_done
):
230 if not os
.path
.isdir(env
.SELFTEST_PREFIX
):
231 os
.makedirs(env
.SELFTEST_PREFIX
, int('755', 8))
233 if Options
.options
.TEST_LIST
:
234 env
.TESTLISTS
= '--testlist=%r' % Options
.options
.TEST_LIST
235 elif Options
.options
.PERF_TEST
:
236 env
.TESTLISTS
= '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
238 env
.TESTLISTS
= ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
239 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
240 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
242 if CONFIG_SET(opt
, 'AD_DC_BUILD_IS_ENABLED'):
243 env
.SELFTEST_TARGET
= "samba"
245 env
.SELFTEST_TARGET
= "samba3"
247 env
.OPTIONS
+= " --nss_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBNSS_WRAPPER_SO_PATH')
248 env
.OPTIONS
+= " --resolv_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBRESOLV_WRAPPER_SO_PATH')
249 env
.OPTIONS
+= " --uid_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBUID_WRAPPER_SO_PATH')
251 # selftest can optionally use kernel namespaces instead of socket-wrapper
252 if os
.environ
.get('USE_NAMESPACES') is None:
253 env
.OPTIONS
+= " --socket_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBSOCKET_WRAPPER_SO_PATH')
255 #if unversioned_sys_platform in ('freebsd', 'netbsd', 'openbsd', 'sunos'):
256 # env.OPTIONS += " --use-dns-faking"
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"
264 if not CONFIG_GET(opt
, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
265 # older MIT krb5 libraries (< 1.14) don't have
266 # GSS_KRB5_CRED_NO_CI_FLAGS_X
267 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
269 if env
.ADDRESS_SANITIZER
:
270 # We try to find the correct libasan automatically
271 libasan
= Utils
.cmd_output(
272 'ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
274 libasan
= libasan
.decode('utf8')
276 # Have the selftest.pl LD_PRELOAD libasan in the right spot
277 env
.OPTIONS
+= " --asan_so_path=" + libasan
280 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
281 env
.CORE_COMMAND
= '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
283 # If using namespaces (rather than socket-wrapper), run the selftest script
284 # in its own network namespace (by doing an 'unshare'). (To create a new
285 # namespace as a non-root user, we have to also unshare the current user
286 # namespace, and remap ourself as root in the namespace created)
287 if os
.environ
.get('USE_NAMESPACES') is not None:
288 env
.CORE_COMMAND
= 'unshare --net --user --map-root-user ' + env
.CORE_COMMAND
290 if env
.ADDRESS_SANITIZER
:
291 # For now we cannot run with leak detection
292 no_leak_check
= "ASAN_OPTIONS=detect_leaks=0 "
293 # And we need to disable RTLD_DEEPBIND in ldb and socket wrapper
294 no_leak_check
+= "LDB_MODULES_DISABLE_DEEPBIND=1 "
295 no_leak_check
+= "SOCKET_WRAPPER_DISABLE_DEEP_BIND=1"
296 env
.CORE_COMMAND
= no_leak_check
+ " " + env
.CORE_COMMAND
298 # We need to have the subunit filter and formatter preload
299 # libasan otherwise the tests fail at startup.
301 # Also, we do not care about leaks in python
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
307 if env
.UNDEFINED_SANITIZER
:
308 # print a stack trace with the error.
309 print_stack_trace
= "UBSAN_OPTIONS=print_stacktrace=1"
310 print_stack_trace
+= ",suppressions=${srcdir}/selftest/ubsan.supp"
311 env
.CORE_COMMAND
= print_stack_trace
+ " " + env
.CORE_COMMAND
313 if Options
.options
.LIST
:
314 cmd
= '${CORE_COMMAND} --list'
316 env
.OPTIONS
+= ' --socket-wrapper'
317 cmd
= '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
319 if Options
.options
.NO_SUBUNIT_FILTER
:
320 # Skip subunit filtering (i.e. because python is disabled).
321 # Use --one to bail out upon any failure
322 cmd
= '(${CORE_COMMAND} --one && touch ${SELFTEST_PREFIX}/st_done)'
323 elif not Options
.options
.FILTERED_SUBUNIT
:
324 subunit_cache
= os
.path
.join(env
.SELFTEST_PREFIX
, "subunit")
325 cmd
+= ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
327 cmd
+= ' | ${FILTER_OPTIONS}'
329 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
331 print("test: running %s" % runcmd
)
332 ret
= RUN_COMMAND(cmd
, env
=env
)
334 if (os
.path
.exists(".testrepository") and
335 not Options
.options
.LIST
and
336 not Options
.options
.LOAD_LIST
and
337 subunit_cache
is not None):
338 testrcmd
= 'testr load -q < %s > /dev/null' % subunit_cache
339 runcmd
= EXPAND_VARIABLES(opt
, testrcmd
)
340 RUN_COMMAND(runcmd
, env
=env
)
342 if subunit_cache
is not None:
343 nb
= Options
.options
.NB_SLOWEST
344 cmd
= "./script/show_testsuite_time %s %d" % (subunit_cache
, nb
)
345 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
346 RUN_COMMAND(runcmd
, env
=env
)
349 print("ERROR: test failed with exit code %d" % ret
)
352 if not Options
.options
.LIST
and not os
.path
.exists(st_done
):
353 print("ERROR: test command failed to complete")
357 ########################################################################
358 # main test entry point
360 '''Run the test suite (see test options below)'''
362 # if running all tests, then force a symbol check
363 env
= LOAD_ENVIRONMENT()
365 Options
.commands
.append('build')
366 Options
.commands
.append('testonly')