2010-03-28 01:48:49 +03:00
#!/usr/bin/env python
2010-06-25 03:21:14 +04:00
# vim: expandtab ft=python
2010-03-28 01:48:49 +03:00
2010-03-17 12:38:03 +03:00
# selftest main code.
2018-02-02 17:34:32 +03:00
import sys
import os
import optparse
from waflib import Scripting, Options, Utils
from waflib.ConfigSet import ConfigSet as Environment
2010-03-30 04:37:07 +04:00
from samba_utils import *
2010-03-30 06:05:15 +04:00
from samba_autoconf import *
2013-01-04 02:34:13 +04:00
import types
2010-03-17 07:22:36 +03:00
2014-10-22 12:08:17 +04:00
DEFAULT_SELFTEST_PREFIX="./st"
2018-02-02 17:34:32 +03:00
def options(opt):
2010-04-15 10:28:20 +04:00
opt.add_option('--enable-selftest',
help=("enable options necessary for selftest (default=no)"),
action="store_true", dest='enable_selftest', default=False)
opt.add_option('--with-selftest-prefix',
2014-10-13 06:52:49 +04:00
help=("specify location of selftest directory "
2014-10-22 12:08:17 +04:00
"(default=%s)" % DEFAULT_SELFTEST_PREFIX),
action="store", dest='SELFTEST_PREFIX', default=DEFAULT_SELFTEST_PREFIX)
2010-04-15 10:28:20 +04:00
2010-03-17 07:22:36 +03:00
opt.ADD_COMMAND('test', cmd_test)
opt.ADD_COMMAND('testonly', cmd_testonly)
gr = opt.add_option_group('test options')
2014-10-13 06:52:49 +04:00
gr.add_option('--load-list',
2010-04-08 17:24:33 +04:00
help=("Load a test id list from a text file"),
action="store", dest='LOAD_LIST', default=None)
2014-10-13 06:52:49 +04:00
gr.add_option('--list',
2010-12-09 16:46:09 +03:00
help=("List available tests"),
action="store_true", dest='LIST', default=False)
2010-03-17 07:22:36 +03:00
gr.add_option('--tests',
help=("wildcard pattern of tests to run"),
action="store", dest='TESTS', default='')
2010-04-08 18:48:33 +04:00
gr.add_option('--filtered-subunit',
help=("output (xfail) filtered subunit"),
action="store_true", dest='FILTERED_SUBUNIT', default=False)
2010-03-17 07:22:36 +03:00
gr.add_option('--quick',
help=("enable only quick tests"),
action="store_true", dest='QUICKTEST', default=False)
gr.add_option('--slow',
help=("enable the really slow tests"),
action="store_true", dest='SLOWTEST', default=False)
2014-10-13 04:57:20 +04:00
gr.add_option('--nb-slowest',
help=("Show the n slowest tests (default=10)"),
type=int, default=10, dest='NB_SLOWEST')
2010-03-17 07:22:36 +03:00
gr.add_option('--testenv',
help=("start a terminal with the test environment setup"),
action="store_true", dest='TESTENV', default=False)
gr.add_option('--valgrind',
help=("use valgrind on client programs in the tests"),
action="store_true", dest='VALGRIND', default=False)
gr.add_option('--valgrind-log',
help=("where to put the valgrind log"),
action="store", dest='VALGRINDLOG', default=None)
gr.add_option('--valgrind-server',
help=("use valgrind on the server in the tests (opens an xterm)"),
action="store_true", dest='VALGRIND_SERVER', default=False)
2010-10-02 23:43:50 +04:00
gr.add_option('--screen',
help=("run the samba servers in screen sessions"),
action="store_true", dest='SCREEN', default=False)
2010-04-09 14:44:09 +04:00
gr.add_option('--gdbtest',
2010-10-02 23:43:50 +04:00
help=("run the servers within a gdb window"),
2010-04-09 14:44:09 +04:00
action="store_true", dest='GDBTEST', default=False)
2010-09-27 05:58:05 +04:00
gr.add_option('--fail-immediately',
help=("stop tests on first failure"),
action="store_true", dest='FAIL_IMMEDIATELY', default=False)
2010-05-08 15:55:25 +04:00
gr.add_option('--socket-wrapper-pcap',
help=("create a pcap file for each failing test"),
action="store_true", dest='SOCKET_WRAPPER_PCAP', default=False)
gr.add_option('--socket-wrapper-keep-pcap',
help=("create a pcap file for all individual test"),
action="store_true", dest='SOCKET_WRAPPER_KEEP_PCAP', default=False)
2012-10-27 03:53:57 +04:00
gr.add_option('--random-order', dest='RANDOM_ORDER', default=False,
action="store_true", help="Run testsuites in random order")
2016-08-17 01:56:50 +03:00
gr.add_option('--perf-test', dest='PERF_TEST', default=False,
action="store_true", help="run performance tests only")
2016-08-31 05:56:25 +03:00
gr.add_option('--test-list', dest='TEST_LIST', default='',
help=("use tests listed here, not defaults "
"(--test-list='FOO|' will execute FOO; "
"--test-list='FOO' will read it)"))
2019-02-18 02:52:41 +03:00
gr.add_option('--no-subunit-filter',
help=("no (xfail) subunit filtering"),
action="store_true", dest='NO_SUBUNIT_FILTER', default=False)
2010-03-17 12:12:16 +03:00
2010-04-15 10:28:20 +04:00
def configure(conf):
conf.env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
2012-08-06 12:32:21 +04:00
if Options.options.enable_selftest or Options.options.developer:
conf.DEFINE('ENABLE_SELFTEST', 1)
2010-03-15 10:03:55 +03:00
def cmd_testonly(opt):
2010-03-17 07:22:36 +03:00
'''run tests without doing a build first'''
2010-03-30 04:37:07 +04:00
env = LOAD_ENVIRONMENT()
2010-03-30 04:54:23 +04:00
opt.env = env
2010-03-15 09:46:09 +03:00
2014-10-22 12:08:17 +04:00
if Options.options.SELFTEST_PREFIX != DEFAULT_SELFTEST_PREFIX:
env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
2014-10-13 06:52:49 +04:00
2010-03-30 06:05:15 +04:00
if (not CONFIG_SET(opt, 'NSS_WRAPPER') or
not CONFIG_SET(opt, 'UID_WRAPPER') or
not CONFIG_SET(opt, 'SOCKET_WRAPPER')):
2010-04-08 01:45:46 +04:00
print("ERROR: You must use --enable-selftest to enable selftest")
2010-03-30 06:05:15 +04:00
sys.exit(1)
2010-10-03 01:21:52 +04:00
os.environ['SAMBA_SELFTEST'] = '1'
2010-03-17 07:22:36 +03:00
env.TESTS = Options.options.TESTS
2011-03-04 16:22:35 +03:00
env.SUBUNIT_FORMATTER = os.getenv('SUBUNIT_FORMATTER')
2018-11-26 17:55:02 +03:00
# Lots of test scripts need to run with the correct version
# of python. With the correct shebang the script should run with the
# correct version, the problem is that not all scripts are part
# of the installation, some scripts are part of the source code,
# and the shebang is not dynamically generated as yet.
# It is safer if we are somewhat version neutral at the moment and
# ignore the shebang and always run scripts from the test environment
# with the python version (determined by PYTHON env variable) If this
# env variable isn't set then set it according to the python version
# that is running the tests
if not os.getenv('PYTHON', None):
from sys import executable as exe
os.environ['PYTHON'] = os.path.basename(exe)
2011-03-04 16:22:35 +03:00
if not env.SUBUNIT_FORMATTER:
2016-08-04 06:35:46 +03:00
if Options.options.PERF_TEST:
env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit-json --prefix=${SELFTEST_PREFIX}'
else:
env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
2017-06-01 06:26:48 +03:00
env.FILTER_XFAIL = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
'--expected-failures=${srcdir}/selftest/knownfail '
'--expected-failures=${srcdir}/selftest/knownfail.d '
'--flapping=${srcdir}/selftest/flapping '
'--flapping=${srcdir}/selftest/flapping.d')
2010-09-27 05:58:05 +04:00
if Options.options.FAIL_IMMEDIATELY:
env.FILTER_XFAIL += ' --fail-immediately'
2010-03-17 07:22:36 +03:00
env.FORMAT_TEST_OUTPUT = '${SUBUNIT_FORMATTER}'
2010-03-15 09:46:09 +03:00
2010-10-03 01:23:43 +04:00
# clean any previous temporary files
os.system("rm -rf %s/tmp" % env.SELFTEST_PREFIX);
2010-10-02 23:43:50 +04:00
# put all command line options in the environment as TESTENV_*=*
for o in dir(Options.options):
if o[0:1] != '_':
2013-01-04 02:34:13 +04:00
val = getattr(Options.options, o, '')
if not issubclass(type(val), types.FunctionType) \
and not issubclass(type(val), types.MethodType):
os.environ['TESTENV_%s' % o.upper()] = str(getattr(Options.options, o, ''))
2015-06-05 03:22:45 +03:00
env.OPTIONS = ''
2010-03-17 07:22:36 +03:00
if not Options.options.SLOWTEST:
2011-10-26 06:38:15 +04:00
env.OPTIONS += ' --exclude=${srcdir}/selftest/slow'
2010-03-15 09:46:09 +03:00
if Options.options.QUICKTEST:
2011-10-26 06:38:15 +04:00
env.OPTIONS += ' --quick --include=${srcdir}/selftest/quick'
2010-04-08 17:24:33 +04:00
if Options.options.LOAD_LIST:
env.OPTIONS += ' --load-list=%s' % Options.options.LOAD_LIST
2010-03-17 07:22:36 +03:00
if Options.options.TESTENV:
env.OPTIONS += ' --testenv'
2010-05-08 15:55:25 +04:00
if Options.options.SOCKET_WRAPPER_PCAP:
env.OPTIONS += ' --socket-wrapper-pcap'
if Options.options.SOCKET_WRAPPER_KEEP_PCAP:
env.OPTIONS += ' --socket-wrapper-keep-pcap'
2012-10-27 03:53:57 +04:00
if Options.options.RANDOM_ORDER:
env.OPTIONS += ' --random-order'
2016-08-17 01:56:50 +03:00
if Options.options.PERF_TEST:
env.FILTER_OPTIONS = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
'--perf-test-output')
2010-03-17 07:22:36 +03:00
else:
2010-04-08 18:48:33 +04:00
env.FILTER_OPTIONS = '${FILTER_XFAIL}'
2010-03-17 07:22:36 +03:00
if Options.options.VALGRIND:
2010-04-08 01:45:46 +04:00
os.environ['VALGRIND'] = 'valgrind -q --num-callers=30'
2010-03-17 07:22:36 +03:00
if Options.options.VALGRINDLOG is not None:
os.environ['VALGRIND'] += ' --log-file=%s' % Options.options.VALGRINDLOG
2010-10-02 23:43:50 +04:00
server_wrapper=''
2010-03-17 07:22:36 +03:00
if Options.options.VALGRIND_SERVER:
2011-02-03 07:14:31 +03:00
server_wrapper = '${srcdir}/selftest/valgrind_run _DUMMY=X'
2010-10-02 23:43:50 +04:00
elif Options.options.GDBTEST:
2011-02-03 07:14:31 +03:00
server_wrapper = '${srcdir}/selftest/gdb_run _DUMMY=X'
2010-10-02 23:43:50 +04:00
if Options.options.SCREEN:
2011-02-08 12:38:05 +03:00
server_wrapper = '${srcdir}/selftest/in_screen %s' % server_wrapper
2011-02-10 08:49:10 +03:00
os.environ['TERMINAL'] = EXPAND_VARIABLES(opt, '${srcdir}/selftest/in_screen')
2010-10-02 23:43:50 +04:00
elif server_wrapper != '':
server_wrapper = 'xterm -n server -l -e %s' % server_wrapper
2010-04-09 14:44:09 +04:00
2010-10-02 23:43:50 +04:00
if server_wrapper != '':
2011-02-08 12:38:05 +03:00
os.environ['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
2013-03-19 18:44:02 +04:00
os.environ['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
os.environ['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
os.environ['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
2010-03-15 09:46:09 +03:00
2010-03-20 15:41:15 +03:00
# this is needed for systems without rpath, or with rpath disabled
2010-03-25 06:20:45 +03:00
ADD_LD_LIBRARY_PATH('bin/shared')
2010-11-05 04:30:01 +03:00
ADD_LD_LIBRARY_PATH('bin/shared/private')
2010-03-20 15:41:15 +03:00
2010-11-03 06:26:04 +03:00
# if we are using a system version of ldb then we need to tell it to
# load modules from our modules path
if env.USING_SYSTEM_LDB:
2018-07-03 11:18:05 +03:00
os.environ['LDB_MODULES_PATH'] = os.path.abspath(
os.path.join(*(env.cwd + ['bin/modules/ldb'])))
2010-11-03 06:26:04 +03:00
2010-03-27 04:44:36 +03:00
# tell build system where to find config.h
2011-04-16 02:47:50 +04:00
os.environ['CONFIG_H'] = 'bin/default/include/config.h'
2010-03-27 04:44:36 +03:00
2017-12-21 20:49:39 +03:00
# tell the test system where perl is
2018-06-27 12:26:09 +03:00
if isinstance(env.PERL, list):
perl = ' '.join(env.PERL)
else:
perl = env.PERL
os.environ['PERL'] = perl
2017-12-21 20:49:39 +03:00
2010-03-23 01:51:09 +03:00
st_done = os.path.join(env.SELFTEST_PREFIX, 'st_done')
2010-03-17 07:22:36 +03:00
if os.path.exists(st_done):
os.unlink(st_done)
2010-11-09 02:22:48 +03:00
if not os.path.isdir(env.SELFTEST_PREFIX):
os.makedirs(env.SELFTEST_PREFIX, int('755', 8))
2016-08-31 05:56:25 +03:00
if Options.options.TEST_LIST:
env.TESTLISTS = '--testlist=%r' % Options.options.TEST_LIST
elif Options.options.PERF_TEST:
2016-08-17 01:56:50 +03:00
env.TESTLISTS = '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
else:
env.TESTLISTS = ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
'--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
'--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
2011-04-28 07:58:07 +04:00
2012-05-24 10:53:34 +04:00
if CONFIG_SET(opt, 'AD_DC_BUILD_IS_ENABLED'):
2014-10-13 06:52:49 +04:00
env.SELFTEST_TARGET = "samba"
2012-05-24 10:53:34 +04:00
else:
2014-10-13 06:52:49 +04:00
env.SELFTEST_TARGET = "samba3"
2012-05-24 10:53:34 +04:00
2014-01-31 19:34:25 +04:00
env.OPTIONS += " --nss_wrapper_so_path=" + CONFIG_GET(opt, 'LIBNSS_WRAPPER_SO_PATH')
2014-09-03 19:16:36 +04:00
env.OPTIONS += " --resolv_wrapper_so_path=" + CONFIG_GET(opt, 'LIBRESOLV_WRAPPER_SO_PATH')
2013-07-03 14:55:29 +04:00
env.OPTIONS += " --uid_wrapper_so_path=" + CONFIG_GET(opt, 'LIBUID_WRAPPER_SO_PATH')
2019-05-23 08:44:37 +03:00
# selftest can optionally use kernel namespaces instead of socket-wrapper
if os.environ.get('USE_NAMESPACES') is None:
env.OPTIONS += " --socket_wrapper_so_path=" + CONFIG_GET(opt, 'LIBSOCKET_WRAPPER_SO_PATH')
2021-10-07 16:55:37 +03:00
if not CONFIG_SET(opt, 'HAVE_RESOLV_CONF_SUPPORT'):
2019-01-24 02:23:34 +03:00
env.OPTIONS += " --use-dns-faking"
2014-11-20 12:48:47 +03:00
2014-05-05 15:27:58 +04:00
if CONFIG_GET(opt, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt, 'MIT_KDC_PATH'):
2016-09-20 13:43:38 +03:00
env.OPTIONS += " --mitkrb5 --exclude=${srcdir}/selftest/skip_mit_kdc"
2020-11-02 23:25:48 +03:00
env.FILTER_XFAIL += " --expected-failures=${srcdir}/selftest/"\
"knownfail_mit_kdc"
2020-11-04 03:54:46 +03:00
else:
env.FILTER_XFAIL += " --expected-failures=${srcdir}/selftest/"\
"knownfail_heimdal_kdc"
2014-05-05 15:27:58 +04:00
2016-12-23 16:07:51 +03:00
if not CONFIG_GET(opt, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
# older MIT krb5 libraries (< 1.14) don't have
# GSS_KRB5_CRED_NO_CI_FLAGS_X
env.OPTIONS += " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
2014-11-03 15:22:38 +03:00
2020-06-22 14:32:45 +03:00
if os.environ.get('DISABLE_OPATH'):
env.OPTIONS += " --exclude=${srcdir}/selftest/skip.opath-required"
2018-05-03 06:47:45 +03:00
if env.ADDRESS_SANITIZER:
# We try to find the correct libasan automatically
2019-05-14 02:25:07 +03:00
libasan = Utils.cmd_output(
'ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
silent=True).strip()
2019-05-06 04:14:37 +03:00
libasan = libasan.decode('utf8')
2018-05-03 06:47:45 +03:00
# Have the selftest.pl LD_PRELOAD libasan in the right spot
env.OPTIONS += " --asan_so_path=" + libasan
2014-11-02 23:36:13 +03:00
subunit_cache = None
2014-10-13 06:52:49 +04:00
# We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
2012-05-24 10:53:34 +04:00
env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
2018-05-03 06:47:45 +03:00
2019-05-23 08:44:37 +03:00
# If using namespaces (rather than socket-wrapper), run the selftest script
# in its own network namespace (by doing an 'unshare'). (To create a new
# namespace as a non-root user, we have to also unshare the current user
# namespace, and remap ourself as root in the namespace created)
if os.environ.get('USE_NAMESPACES') is not None:
env.CORE_COMMAND = 'unshare --net --user --map-root-user ' + env.CORE_COMMAND
2018-05-03 06:47:45 +03:00
if env.ADDRESS_SANITIZER:
2021-02-01 14:06:16 +03:00
# For now we cannot run with leak and odr detection
no_leak_check = "ASAN_OPTIONS=detect_leaks=0:detect_odr_violation=0 "
2019-05-20 23:49:08 +03:00
# And we need to disable RTLD_DEEPBIND in ldb and socket wrapper
no_leak_check += "LDB_MODULES_DISABLE_DEEPBIND=1 "
no_leak_check += "SOCKET_WRAPPER_DISABLE_DEEP_BIND=1"
2018-05-03 06:47:45 +03:00
env.CORE_COMMAND = no_leak_check + " " + env.CORE_COMMAND
# We need to have the subunit filter and formatter preload
# libasan otherwise the tests fail at startup.
#
# Also, we do not care about leaks in python
asan_envs = no_leak_check + " LD_PRELOAD=" + libasan + ' '
env.FILTER_OPTIONS = asan_envs + env.FILTER_OPTIONS
env.SUBUNIT_FORMATTER = asan_envs + env.SUBUNIT_FORMATTER
2019-05-14 02:25:07 +03:00
if env.UNDEFINED_SANITIZER:
# print a stack trace with the error.
print_stack_trace = "UBSAN_OPTIONS=print_stacktrace=1"
print_stack_trace += ",suppressions=${srcdir}/selftest/ubsan.supp"
env.CORE_COMMAND = print_stack_trace + " " + env.CORE_COMMAND
2011-11-27 22:57:53 +04:00
if Options.options.LIST:
cmd = '${CORE_COMMAND} --list'
2010-11-22 01:09:49 +03:00
else:
2011-11-27 22:57:53 +04:00
env.OPTIONS += ' --socket-wrapper'
2011-11-28 00:50:23 +04:00
cmd = '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
2019-02-18 02:52:41 +03:00
if Options.options.NO_SUBUNIT_FILTER:
# Skip subunit filtering (i.e. because python is disabled).
# Use --one to bail out upon any failure
cmd = '(${CORE_COMMAND} --one && touch ${SELFTEST_PREFIX}/st_done)'
2019-03-11 06:28:16 +03:00
elif not Options.options.FILTERED_SUBUNIT:
2015-01-10 11:51:45 +03:00
subunit_cache = os.path.join(env.SELFTEST_PREFIX, "subunit")
2014-11-02 23:36:13 +03:00
cmd += ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
2012-02-03 05:04:32 +04:00
else:
cmd += ' | ${FILTER_OPTIONS}'
2018-05-03 06:47:45 +03:00
2011-04-16 02:47:50 +04:00
runcmd = EXPAND_VARIABLES(opt, cmd)
2010-03-15 09:46:09 +03:00
2011-04-16 02:47:50 +04:00
print("test: running %s" % runcmd)
2010-03-19 06:25:50 +03:00
ret = RUN_COMMAND(cmd, env=env)
2011-11-28 00:50:23 +04:00
2014-10-13 04:58:41 +04:00
if (os.path.exists(".testrepository") and
not Options.options.LIST and
2014-11-02 23:36:13 +03:00
not Options.options.LOAD_LIST and
subunit_cache is not None):
testrcmd = 'testr load -q < %s > /dev/null' % subunit_cache
2011-04-16 02:47:50 +04:00
runcmd = EXPAND_VARIABLES(opt, testrcmd)
RUN_COMMAND(runcmd, env=env)
2010-06-25 03:21:14 +04:00
2014-11-02 23:36:13 +03:00
if subunit_cache is not None:
2014-10-13 04:57:20 +04:00
nb = Options.options.NB_SLOWEST
2014-11-02 23:36:13 +03:00
cmd = "./script/show_testsuite_time %s %d" % (subunit_cache, nb)
2014-10-13 04:57:20 +04:00
runcmd = EXPAND_VARIABLES(opt, cmd)
RUN_COMMAND(runcmd, env=env)
2010-03-17 07:22:36 +03:00
if ret != 0:
2010-03-19 02:37:01 +03:00
print("ERROR: test failed with exit code %d" % ret)
2010-03-17 07:22:36 +03:00
sys.exit(ret)
2011-11-28 00:07:07 +04:00
if not Options.options.LIST and not os.path.exists(st_done):
2010-03-19 02:37:01 +03:00
print("ERROR: test command failed to complete")
2010-03-17 07:22:36 +03:00
sys.exit(1)
2010-03-17 12:38:03 +03:00
2010-03-15 10:03:55 +03:00
########################################################################
# main test entry point
def cmd_test(opt):
2010-03-17 07:22:36 +03:00
'''Run the test suite (see test options below)'''
2011-11-09 06:08:37 +04:00
# if running all tests, then force a symbol check
env = LOAD_ENVIRONMENT()
CHECK_MAKEFLAGS(env)
2016-03-26 15:18:07 +03:00
Options.commands.append('build')
Options.commands.append('testonly')