selftest/wscript: properly handle env.cwd which is a list, not a string
[Samba.git] / selftest / wscript
blobce1e117cbdcdba64599bcfb445338f3c40b48507
1 #!/usr/bin/env python
2 # vim: expandtab ft=python
4 # selftest main code.
6 import sys
7 import os
8 import optparse
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 *
14 import types
16 DEFAULT_SELFTEST_PREFIX="./st"
18 def options(opt):
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('--enable-coverage',
24 help=("enable options necessary for code coverage "
25 "reporting on selftest (default=no)"),
26 action="store_true", dest='enable_coverage', default=False)
27 opt.add_option('--with-selftest-prefix',
28 help=("specify location of selftest directory "
29 "(default=%s)" % DEFAULT_SELFTEST_PREFIX),
30 action="store", dest='SELFTEST_PREFIX', default=DEFAULT_SELFTEST_PREFIX)
32 opt.ADD_COMMAND('test', cmd_test)
33 opt.ADD_COMMAND('testonly', cmd_testonly)
35 gr = opt.add_option_group('test options')
37 gr.add_option('--load-list',
38 help=("Load a test id list from a text file"),
39 action="store", dest='LOAD_LIST', default=None)
40 gr.add_option('--list',
41 help=("List available tests"),
42 action="store_true", dest='LIST', default=False)
43 gr.add_option('--tests',
44 help=("wildcard pattern of tests to run"),
45 action="store", dest='TESTS', default='')
46 gr.add_option('--filtered-subunit',
47 help=("output (xfail) filtered subunit"),
48 action="store_true", dest='FILTERED_SUBUNIT', default=False)
49 gr.add_option('--quick',
50 help=("enable only quick tests"),
51 action="store_true", dest='QUICKTEST', default=False)
52 gr.add_option('--slow',
53 help=("enable the really slow tests"),
54 action="store_true", dest='SLOWTEST', default=False)
55 gr.add_option('--nb-slowest',
56 help=("Show the n slowest tests (default=10)"),
57 type=int, default=10, dest='NB_SLOWEST')
58 gr.add_option('--testenv',
59 help=("start a terminal with the test environment setup"),
60 action="store_true", dest='TESTENV', default=False)
61 gr.add_option('--valgrind',
62 help=("use valgrind on client programs in the tests"),
63 action="store_true", dest='VALGRIND', default=False)
64 gr.add_option('--valgrind-log',
65 help=("where to put the valgrind log"),
66 action="store", dest='VALGRINDLOG', default=None)
67 gr.add_option('--valgrind-server',
68 help=("use valgrind on the server in the tests (opens an xterm)"),
69 action="store_true", dest='VALGRIND_SERVER', default=False)
70 gr.add_option('--screen',
71 help=("run the samba servers in screen sessions"),
72 action="store_true", dest='SCREEN', default=False)
73 gr.add_option('--gdbtest',
74 help=("run the servers within a gdb window"),
75 action="store_true", dest='GDBTEST', default=False)
76 gr.add_option('--fail-immediately',
77 help=("stop tests on first failure"),
78 action="store_true", dest='FAIL_IMMEDIATELY', default=False)
79 gr.add_option('--socket-wrapper-pcap',
80 help=("create a pcap file for each failing test"),
81 action="store_true", dest='SOCKET_WRAPPER_PCAP', default=False)
82 gr.add_option('--socket-wrapper-keep-pcap',
83 help=("create a pcap file for all individual test"),
84 action="store_true", dest='SOCKET_WRAPPER_KEEP_PCAP', default=False)
85 gr.add_option('--random-order', dest='RANDOM_ORDER', default=False,
86 action="store_true", help="Run testsuites in random order")
87 gr.add_option('--perf-test', dest='PERF_TEST', default=False,
88 action="store_true", help="run performance tests only")
89 gr.add_option('--test-list', dest='TEST_LIST', default='',
90 help=("use tests listed here, not defaults "
91 "(--test-list='FOO|' will execute FOO; "
92 "--test-list='FOO' will read it)"))
94 def configure(conf):
95 conf.env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
96 conf.env.enable_coverage = Options.options.enable_coverage
97 if conf.env.enable_coverage:
98 conf.ADD_LDFLAGS('-lgcov', testflags=True)
99 conf.ADD_CFLAGS('--coverage')
101 if Options.options.enable_selftest or Options.options.developer:
102 conf.DEFINE('ENABLE_SELFTEST', 1)
105 def cmd_testonly(opt):
106 '''run tests without doing a build first'''
107 env = LOAD_ENVIRONMENT()
108 opt.env = env
110 if Options.options.SELFTEST_PREFIX != DEFAULT_SELFTEST_PREFIX:
111 env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
113 if (not CONFIG_SET(opt, 'NSS_WRAPPER') or
114 not CONFIG_SET(opt, 'UID_WRAPPER') or
115 not CONFIG_SET(opt, 'SOCKET_WRAPPER')):
116 print("ERROR: You must use --enable-selftest to enable selftest")
117 sys.exit(1)
119 os.environ['SAMBA_SELFTEST'] = '1'
121 env.TESTS = Options.options.TESTS
123 env.SUBUNIT_FORMATTER = os.getenv('SUBUNIT_FORMATTER')
124 if not env.SUBUNIT_FORMATTER:
125 if Options.options.PERF_TEST:
126 env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit-json --prefix=${SELFTEST_PREFIX}'
127 else:
128 env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
129 env.FILTER_XFAIL = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
130 '--expected-failures=${srcdir}/selftest/knownfail '
131 '--expected-failures=${srcdir}/selftest/knownfail.d '
132 '--flapping=${srcdir}/selftest/flapping '
133 '--flapping=${srcdir}/selftest/flapping.d')
135 if Options.options.FAIL_IMMEDIATELY:
136 env.FILTER_XFAIL += ' --fail-immediately'
138 env.FORMAT_TEST_OUTPUT = '${SUBUNIT_FORMATTER}'
140 # clean any previous temporary files
141 os.system("rm -rf %s/tmp" % env.SELFTEST_PREFIX);
143 # put all command line options in the environment as TESTENV_*=*
144 for o in dir(Options.options):
145 if o[0:1] != '_':
146 val = getattr(Options.options, o, '')
147 if not issubclass(type(val), types.FunctionType) \
148 and not issubclass(type(val), types.MethodType):
149 os.environ['TESTENV_%s' % o.upper()] = str(getattr(Options.options, o, ''))
151 env.OPTIONS = ''
152 if not Options.options.SLOWTEST:
153 env.OPTIONS += ' --exclude=${srcdir}/selftest/slow'
154 if Options.options.QUICKTEST:
155 env.OPTIONS += ' --quick --include=${srcdir}/selftest/quick'
156 if Options.options.LOAD_LIST:
157 env.OPTIONS += ' --load-list=%s' % Options.options.LOAD_LIST
158 if Options.options.TESTENV:
159 env.OPTIONS += ' --testenv'
160 if Options.options.SOCKET_WRAPPER_PCAP:
161 env.OPTIONS += ' --socket-wrapper-pcap'
162 if Options.options.SOCKET_WRAPPER_KEEP_PCAP:
163 env.OPTIONS += ' --socket-wrapper-keep-pcap'
164 if Options.options.RANDOM_ORDER:
165 env.OPTIONS += ' --random-order'
166 if Options.options.PERF_TEST:
167 env.FILTER_OPTIONS = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
168 '--perf-test-output')
169 elif os.environ.get('RUN_FROM_BUILD_FARM') is not None:
170 env.FILTER_OPTIONS = '${FILTER_XFAIL} --strip-passed-output'
171 else:
172 env.FILTER_OPTIONS = '${FILTER_XFAIL}'
174 if Options.options.VALGRIND:
175 os.environ['VALGRIND'] = 'valgrind -q --num-callers=30'
176 if Options.options.VALGRINDLOG is not None:
177 os.environ['VALGRIND'] += ' --log-file=%s' % Options.options.VALGRINDLOG
179 server_wrapper=''
181 if Options.options.VALGRIND_SERVER:
182 server_wrapper = '${srcdir}/selftest/valgrind_run _DUMMY=X'
183 elif Options.options.GDBTEST:
184 server_wrapper = '${srcdir}/selftest/gdb_run _DUMMY=X'
186 if Options.options.SCREEN:
187 server_wrapper = '${srcdir}/selftest/in_screen %s' % server_wrapper
188 os.environ['TERMINAL'] = EXPAND_VARIABLES(opt, '${srcdir}/selftest/in_screen')
189 elif server_wrapper != '':
190 server_wrapper = 'xterm -n server -l -e %s' % server_wrapper
192 if server_wrapper != '':
193 os.environ['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
194 os.environ['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
195 os.environ['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
196 os.environ['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
198 # this is needed for systems without rpath, or with rpath disabled
199 ADD_LD_LIBRARY_PATH('bin/shared')
200 ADD_LD_LIBRARY_PATH('bin/shared/private')
202 # if we are using a system version of ldb then we need to tell it to
203 # load modules from our modules path
204 if env.USING_SYSTEM_LDB:
205 os.environ['LDB_MODULES_PATH'] = os.path.abspath(
206 os.path.join(*(env.cwd + ['bin/modules/ldb'])))
208 # tell build system where to find config.h
209 os.environ['CONFIG_H'] = 'bin/default/include/config.h'
211 # tell the test system where perl is
212 if isinstance(env.PERL, list):
213 perl = ' '.join(env.PERL)
214 else:
215 perl = env.PERL
216 os.environ['PERL'] = perl
218 st_done = os.path.join(env.SELFTEST_PREFIX, 'st_done')
219 if os.path.exists(st_done):
220 os.unlink(st_done)
222 if not os.path.isdir(env.SELFTEST_PREFIX):
223 os.makedirs(env.SELFTEST_PREFIX, int('755', 8))
225 if Options.options.TEST_LIST:
226 env.TESTLISTS = '--testlist=%r' % Options.options.TEST_LIST
227 elif Options.options.PERF_TEST:
228 env.TESTLISTS = '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
229 elif CONFIG_SET(opt, 'EXTRA_PYTHON'):
230 env.TESTLISTS = ('--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/selftest/tests.py|" ' +
231 '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
232 '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
233 else:
234 env.TESTLISTS = ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
235 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
236 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
238 if CONFIG_SET(opt, 'AD_DC_BUILD_IS_ENABLED'):
239 env.SELFTEST_TARGET = "samba"
240 else:
241 env.SELFTEST_TARGET = "samba3"
243 env.OPTIONS += " --nss_wrapper_so_path=" + CONFIG_GET(opt, 'LIBNSS_WRAPPER_SO_PATH')
244 env.OPTIONS += " --resolv_wrapper_so_path=" + CONFIG_GET(opt, 'LIBRESOLV_WRAPPER_SO_PATH')
245 env.OPTIONS += " --socket_wrapper_so_path=" + CONFIG_GET(opt, 'LIBSOCKET_WRAPPER_SO_PATH')
246 env.OPTIONS += " --uid_wrapper_so_path=" + CONFIG_GET(opt, 'LIBUID_WRAPPER_SO_PATH')
248 #if unversioned_sys_platform in ('freebsd', 'netbsd', 'openbsd', 'sunos'):
249 # env.OPTIONS += " --use-dns-faking"
251 # FIXME REMOVE ME!
252 env.OPTIONS += " --use-dns-faking"
254 if CONFIG_GET(opt, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt, 'MIT_KDC_PATH'):
255 env.OPTIONS += " --mitkrb5 --exclude=${srcdir}/selftest/skip_mit_kdc"
257 if not CONFIG_GET(opt, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
258 # older MIT krb5 libraries (< 1.14) don't have
259 # GSS_KRB5_CRED_NO_CI_FLAGS_X
260 env.OPTIONS += " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
262 if env.ADDRESS_SANITIZER:
263 # We try to find the correct libasan automatically
264 libasan = Utils.cmd_output('ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
265 silent=True).strip()
267 # Have the selftest.pl LD_PRELOAD libasan in the right spot
268 env.OPTIONS += " --asan_so_path=" + libasan
270 subunit_cache = None
271 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
272 env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
274 if env.ADDRESS_SANITIZER:
275 # For now we cannot run with leak detection
276 no_leak_check = "ASAN_OPTIONS=detect_leaks=0"
277 env.CORE_COMMAND = no_leak_check + " " + env.CORE_COMMAND
279 # We need to have the subunit filter and formatter preload
280 # libasan otherwise the tests fail at startup.
282 # Also, we do not care about leaks in python
284 asan_envs = no_leak_check + " LD_PRELOAD=" + libasan + ' '
285 env.FILTER_OPTIONS = asan_envs + env.FILTER_OPTIONS
286 env.SUBUNIT_FORMATTER = asan_envs + env.SUBUNIT_FORMATTER
288 if Options.options.LIST:
289 cmd = '${CORE_COMMAND} --list'
290 else:
291 env.OPTIONS += ' --socket-wrapper'
292 cmd = '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
293 if (os.environ.get('RUN_FROM_BUILD_FARM') is None and
294 not Options.options.FILTERED_SUBUNIT):
295 subunit_cache = os.path.join(env.SELFTEST_PREFIX, "subunit")
296 cmd += ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
297 else:
298 cmd += ' | ${FILTER_OPTIONS}'
300 runcmd = EXPAND_VARIABLES(opt, cmd)
302 print("test: running %s" % runcmd)
303 ret = RUN_COMMAND(cmd, env=env)
305 if (os.path.exists(".testrepository") and
306 not Options.options.LIST and
307 not Options.options.LOAD_LIST and
308 subunit_cache is not None):
309 testrcmd = 'testr load -q < %s > /dev/null' % subunit_cache
310 runcmd = EXPAND_VARIABLES(opt, testrcmd)
311 RUN_COMMAND(runcmd, env=env)
313 if subunit_cache is not None:
314 nb = Options.options.NB_SLOWEST
315 cmd = "./script/show_testsuite_time %s %d" % (subunit_cache, nb)
316 runcmd = EXPAND_VARIABLES(opt, cmd)
317 RUN_COMMAND(runcmd, env=env)
319 if ret != 0:
320 print("ERROR: test failed with exit code %d" % ret)
321 sys.exit(ret)
323 if not Options.options.LIST and not os.path.exists(st_done):
324 print("ERROR: test command failed to complete")
325 sys.exit(1)
328 ########################################################################
329 # main test entry point
330 def cmd_test(opt):
331 '''Run the test suite (see test options below)'''
333 # if running all tests, then force a symbol check
334 env = LOAD_ENVIRONMENT()
335 CHECK_MAKEFLAGS(env)
336 Options.commands.append('build')
337 Options.commands.append('testonly')