2 # vim: expandtab ft=python
6 import Scripting
, os
, Options
, Utils
, Environment
, optparse
, sys
7 from samba_utils
import *
8 from samba_autoconf
import *
11 DEFAULT_SELFTEST_PREFIX
="./st"
15 opt
.add_option('--enable-selftest',
16 help=("enable options necessary for selftest (default=no)"),
17 action
="store_true", dest
='enable_selftest', default
=False)
18 opt
.add_option('--enable-coverage',
19 help=("enable options necessary for code coverage "
20 "reporting on selftest (default=no)"),
21 action
="store_true", dest
='enable_coverage', default
=False)
22 opt
.add_option('--with-selftest-prefix',
23 help=("specify location of selftest directory "
24 "(default=%s)" % DEFAULT_SELFTEST_PREFIX
),
25 action
="store", dest
='SELFTEST_PREFIX', default
=DEFAULT_SELFTEST_PREFIX
)
27 opt
.ADD_COMMAND('test', cmd_test
)
28 opt
.ADD_COMMAND('testonly', cmd_testonly
)
30 gr
= opt
.add_option_group('test options')
32 gr
.add_option('--load-list',
33 help=("Load a test id list from a text file"),
34 action
="store", dest
='LOAD_LIST', default
=None)
35 gr
.add_option('--list',
36 help=("List available tests"),
37 action
="store_true", dest
='LIST', default
=False)
38 gr
.add_option('--tests',
39 help=("wildcard pattern of tests to run"),
40 action
="store", dest
='TESTS', default
='')
41 gr
.add_option('--filtered-subunit',
42 help=("output (xfail) filtered subunit"),
43 action
="store_true", dest
='FILTERED_SUBUNIT', default
=False)
44 gr
.add_option('--quick',
45 help=("enable only quick tests"),
46 action
="store_true", dest
='QUICKTEST', default
=False)
47 gr
.add_option('--slow',
48 help=("enable the really slow tests"),
49 action
="store_true", dest
='SLOWTEST', default
=False)
50 gr
.add_option('--nb-slowest',
51 help=("Show the n slowest tests (default=10)"),
52 type=int, default
=10, dest
='NB_SLOWEST')
53 gr
.add_option('--testenv',
54 help=("start a terminal with the test environment setup"),
55 action
="store_true", dest
='TESTENV', default
=False)
56 gr
.add_option('--valgrind',
57 help=("use valgrind on client programs in the tests"),
58 action
="store_true", dest
='VALGRIND', default
=False)
59 gr
.add_option('--valgrind-log',
60 help=("where to put the valgrind log"),
61 action
="store", dest
='VALGRINDLOG', default
=None)
62 gr
.add_option('--valgrind-server',
63 help=("use valgrind on the server in the tests (opens an xterm)"),
64 action
="store_true", dest
='VALGRIND_SERVER', default
=False)
65 gr
.add_option('--screen',
66 help=("run the samba servers in screen sessions"),
67 action
="store_true", dest
='SCREEN', default
=False)
68 gr
.add_option('--gdbtest',
69 help=("run the servers within a gdb window"),
70 action
="store_true", dest
='GDBTEST', default
=False)
71 gr
.add_option('--fail-immediately',
72 help=("stop tests on first failure"),
73 action
="store_true", dest
='FAIL_IMMEDIATELY', default
=False)
74 gr
.add_option('--socket-wrapper-pcap',
75 help=("create a pcap file for each failing test"),
76 action
="store_true", dest
='SOCKET_WRAPPER_PCAP', default
=False)
77 gr
.add_option('--socket-wrapper-keep-pcap',
78 help=("create a pcap file for all individual test"),
79 action
="store_true", dest
='SOCKET_WRAPPER_KEEP_PCAP', default
=False)
80 gr
.add_option('--random-order', dest
='RANDOM_ORDER', default
=False,
81 action
="store_true", help="Run testsuites in random order")
82 gr
.add_option('--perf-test', dest
='PERF_TEST', default
=False,
83 action
="store_true", help="run performance tests only")
84 gr
.add_option('--test-list', dest
='TEST_LIST', default
='',
85 help=("use tests listed here, not defaults "
86 "(--test-list='FOO|' will execute FOO; "
87 "--test-list='FOO' will read it)"))
90 conf
.env
.SELFTEST_PREFIX
= Options
.options
.SELFTEST_PREFIX
91 conf
.env
.enable_coverage
= Options
.options
.enable_coverage
92 if conf
.env
.enable_coverage
:
93 conf
.ADD_LDFLAGS('-lgcov', testflags
=True)
94 conf
.ADD_CFLAGS('--coverage')
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')
119 if not env
.SUBUNIT_FORMATTER
:
120 if Options
.options
.PERF_TEST
:
121 env
.SUBUNIT_FORMATTER
= '${PYTHON} -u ${srcdir}/selftest/format-subunit-json --prefix=${SELFTEST_PREFIX}'
123 env
.SUBUNIT_FORMATTER
= '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
124 env
.FILTER_XFAIL
= ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
125 '--expected-failures=${srcdir}/selftest/knownfail '
126 '--expected-failures=${srcdir}/selftest/knownfail.d '
127 '--flapping=${srcdir}/selftest/flapping '
128 '--flapping=${srcdir}/selftest/flapping.d')
130 if Options
.options
.FAIL_IMMEDIATELY
:
131 env
.FILTER_XFAIL
+= ' --fail-immediately'
133 env
.FORMAT_TEST_OUTPUT
= '${SUBUNIT_FORMATTER}'
135 # clean any previous temporary files
136 os
.system("rm -rf %s/tmp" % env
.SELFTEST_PREFIX
);
138 # put all command line options in the environment as TESTENV_*=*
139 for o
in dir(Options
.options
):
141 val
= getattr(Options
.options
, o
, '')
142 if not issubclass(type(val
), types
.FunctionType
) \
143 and not issubclass(type(val
), types
.MethodType
):
144 os
.environ
['TESTENV_%s' % o
.upper()] = str(getattr(Options
.options
, o
, ''))
147 if not Options
.options
.SLOWTEST
:
148 env
.OPTIONS
+= ' --exclude=${srcdir}/selftest/slow'
149 if Options
.options
.QUICKTEST
:
150 env
.OPTIONS
+= ' --quick --include=${srcdir}/selftest/quick'
151 if Options
.options
.LOAD_LIST
:
152 env
.OPTIONS
+= ' --load-list=%s' % Options
.options
.LOAD_LIST
153 if Options
.options
.TESTENV
:
154 env
.OPTIONS
+= ' --testenv'
155 if Options
.options
.SOCKET_WRAPPER_PCAP
:
156 env
.OPTIONS
+= ' --socket-wrapper-pcap'
157 if Options
.options
.SOCKET_WRAPPER_KEEP_PCAP
:
158 env
.OPTIONS
+= ' --socket-wrapper-keep-pcap'
159 if Options
.options
.RANDOM_ORDER
:
160 env
.OPTIONS
+= ' --random-order'
161 if Options
.options
.PERF_TEST
:
162 env
.FILTER_OPTIONS
= ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
163 '--perf-test-output')
164 elif os
.environ
.get('RUN_FROM_BUILD_FARM') is not None:
165 env
.FILTER_OPTIONS
= '${FILTER_XFAIL} --strip-passed-output'
167 env
.FILTER_OPTIONS
= '${FILTER_XFAIL}'
169 if Options
.options
.VALGRIND
:
170 os
.environ
['VALGRIND'] = 'valgrind -q --num-callers=30'
171 if Options
.options
.VALGRINDLOG
is not None:
172 os
.environ
['VALGRIND'] += ' --log-file=%s' % Options
.options
.VALGRINDLOG
176 if Options
.options
.VALGRIND_SERVER
:
177 server_wrapper
= '${srcdir}/selftest/valgrind_run _DUMMY=X'
178 elif Options
.options
.GDBTEST
:
179 server_wrapper
= '${srcdir}/selftest/gdb_run _DUMMY=X'
181 if Options
.options
.SCREEN
:
182 server_wrapper
= '${srcdir}/selftest/in_screen %s' % server_wrapper
183 os
.environ
['TERMINAL'] = EXPAND_VARIABLES(opt
, '${srcdir}/selftest/in_screen')
184 elif server_wrapper
!= '':
185 server_wrapper
= 'xterm -n server -l -e %s' % server_wrapper
187 if server_wrapper
!= '':
188 os
.environ
['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
189 os
.environ
['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
190 os
.environ
['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
191 os
.environ
['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
193 # this is needed for systems without rpath, or with rpath disabled
194 ADD_LD_LIBRARY_PATH('bin/shared')
195 ADD_LD_LIBRARY_PATH('bin/shared/private')
197 # if we are using a system version of ldb then we need to tell it to
198 # load modules from our modules path
199 if env
.USING_SYSTEM_LDB
:
200 os
.environ
['LDB_MODULES_PATH'] = os
.path
.abspath(os
.path
.join(env
.cwd
, 'bin/modules/ldb'))
202 # tell build system where to find config.h
203 os
.environ
['CONFIG_H'] = 'bin/default/include/config.h'
205 st_done
= os
.path
.join(env
.SELFTEST_PREFIX
, 'st_done')
206 if os
.path
.exists(st_done
):
209 if not os
.path
.isdir(env
.SELFTEST_PREFIX
):
210 os
.makedirs(env
.SELFTEST_PREFIX
, int('755', 8))
212 if Options
.options
.TEST_LIST
:
213 env
.TESTLISTS
= '--testlist=%r' % Options
.options
.TEST_LIST
214 elif Options
.options
.PERF_TEST
:
215 env
.TESTLISTS
= '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
216 elif CONFIG_SET(opt
, 'EXTRA_PYTHON'):
217 env
.TESTLISTS
= ('--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/selftest/tests.py|" ' +
218 '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
219 '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
221 env
.TESTLISTS
= ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
222 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
223 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
225 if CONFIG_SET(opt
, 'AD_DC_BUILD_IS_ENABLED'):
226 env
.SELFTEST_TARGET
= "samba"
228 env
.SELFTEST_TARGET
= "samba3"
230 env
.OPTIONS
+= " --nss_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBNSS_WRAPPER_SO_PATH')
231 env
.OPTIONS
+= " --resolv_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBRESOLV_WRAPPER_SO_PATH')
232 env
.OPTIONS
+= " --socket_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBSOCKET_WRAPPER_SO_PATH')
233 env
.OPTIONS
+= " --uid_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBUID_WRAPPER_SO_PATH')
235 #if unversioned_sys_platform in ('freebsd', 'netbsd', 'openbsd', 'sunos'):
236 # env.OPTIONS += " --use-dns-faking"
239 env
.OPTIONS
+= " --use-dns-faking"
241 if CONFIG_GET(opt
, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt
, 'MIT_KDC_PATH'):
242 env
.OPTIONS
+= " --mitkrb5 --exclude=${srcdir}/selftest/skip_mit_kdc"
244 if not CONFIG_GET(opt
, 'HAVE_GSS_KRB5_CRED_NO_CI_FLAGS_X'):
245 # older MIT krb5 libraries (< 1.14) don't have
246 # GSS_KRB5_CRED_NO_CI_FLAGS_X
247 env
.OPTIONS
+= " --exclude=${srcdir}/selftest/skip.no-GSS_KRB5_CRED_NO_CI_FLAGS_X"
250 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
251 env
.CORE_COMMAND
= '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
252 if Options
.options
.LIST
:
253 cmd
= '${CORE_COMMAND} --list'
255 env
.OPTIONS
+= ' --socket-wrapper'
256 cmd
= '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
257 if (os
.environ
.get('RUN_FROM_BUILD_FARM') is None and
258 not Options
.options
.FILTERED_SUBUNIT
):
259 subunit_cache
= os
.path
.join(env
.SELFTEST_PREFIX
, "subunit")
260 cmd
+= ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
262 cmd
+= ' | ${FILTER_OPTIONS}'
263 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
265 print("test: running %s" % runcmd
)
266 ret
= RUN_COMMAND(cmd
, env
=env
)
268 if (os
.path
.exists(".testrepository") and
269 not Options
.options
.LIST
and
270 not Options
.options
.LOAD_LIST
and
271 subunit_cache
is not None):
272 testrcmd
= 'testr load -q < %s > /dev/null' % subunit_cache
273 runcmd
= EXPAND_VARIABLES(opt
, testrcmd
)
274 RUN_COMMAND(runcmd
, env
=env
)
276 if subunit_cache
is not None:
277 nb
= Options
.options
.NB_SLOWEST
278 cmd
= "./script/show_testsuite_time %s %d" % (subunit_cache
, nb
)
279 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
280 RUN_COMMAND(runcmd
, env
=env
)
283 print("ERROR: test failed with exit code %d" % ret
)
286 if not Options
.options
.LIST
and not os
.path
.exists(st_done
):
287 print("ERROR: test command failed to complete")
291 ########################################################################
292 # main test entry point
294 '''Run the test suite (see test options below)'''
296 # if running all tests, then force a symbol check
297 env
= LOAD_ENVIRONMENT()
299 Scripting
.commands
.append('build')
300 Scripting
.commands
.append('testonly')