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")
84 conf
.env
.SELFTEST_PREFIX
= Options
.options
.SELFTEST_PREFIX
85 conf
.env
.enable_coverage
= Options
.options
.enable_coverage
86 if conf
.env
.enable_coverage
:
87 conf
.ADD_LDFLAGS('-lgcov', testflags
=True)
88 conf
.ADD_CFLAGS('--coverage')
90 if Options
.options
.enable_selftest
or Options
.options
.developer
:
91 conf
.DEFINE('ENABLE_SELFTEST', 1)
94 def cmd_testonly(opt
):
95 '''run tests without doing a build first'''
96 env
= LOAD_ENVIRONMENT()
99 if Options
.options
.SELFTEST_PREFIX
!= DEFAULT_SELFTEST_PREFIX
:
100 env
.SELFTEST_PREFIX
= Options
.options
.SELFTEST_PREFIX
102 if (not CONFIG_SET(opt
, 'NSS_WRAPPER') or
103 not CONFIG_SET(opt
, 'UID_WRAPPER') or
104 not CONFIG_SET(opt
, 'SOCKET_WRAPPER')):
105 print("ERROR: You must use --enable-selftest to enable selftest")
108 os
.environ
['SAMBA_SELFTEST'] = '1'
110 env
.TESTS
= Options
.options
.TESTS
112 env
.SUBUNIT_FORMATTER
= os
.getenv('SUBUNIT_FORMATTER')
113 if not env
.SUBUNIT_FORMATTER
:
114 env
.SUBUNIT_FORMATTER
= '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
115 env
.FILTER_XFAIL
= '${PYTHON} -u ${srcdir}/selftest/filter-subunit --expected-failures=${srcdir}/selftest/knownfail --flapping=${srcdir}/selftest/flapping'
117 if Options
.options
.FAIL_IMMEDIATELY
:
118 env
.FILTER_XFAIL
+= ' --fail-immediately'
120 env
.FORMAT_TEST_OUTPUT
= '${SUBUNIT_FORMATTER}'
122 # clean any previous temporary files
123 os
.system("rm -rf %s/tmp" % env
.SELFTEST_PREFIX
);
125 # put all command line options in the environment as TESTENV_*=*
126 for o
in dir(Options
.options
):
128 val
= getattr(Options
.options
, o
, '')
129 if not issubclass(type(val
), types
.FunctionType
) \
130 and not issubclass(type(val
), types
.MethodType
):
131 os
.environ
['TESTENV_%s' % o
.upper()] = str(getattr(Options
.options
, o
, ''))
134 binary_mapping
= ('nmblookup3:nmblookup,' +
135 'nmblookup4:nmblookup4,' +
136 'smbclient3:smbclient,' +
137 'smbclient4:smbclient4,' +
138 'smbtorture4:smbtorture,' +
139 'ntlm_auth3:ntlm_auth')
141 env
.OPTIONS
= '--binary-mapping=%s' % binary_mapping
142 if not Options
.options
.SLOWTEST
:
143 env
.OPTIONS
+= ' --exclude=${srcdir}/selftest/slow'
144 if Options
.options
.QUICKTEST
:
145 env
.OPTIONS
+= ' --quick --include=${srcdir}/selftest/quick'
146 if Options
.options
.LOAD_LIST
:
147 env
.OPTIONS
+= ' --load-list=%s' % Options
.options
.LOAD_LIST
148 if Options
.options
.TESTENV
:
149 env
.OPTIONS
+= ' --testenv'
150 if Options
.options
.SOCKET_WRAPPER_PCAP
:
151 env
.OPTIONS
+= ' --socket-wrapper-pcap'
152 if Options
.options
.SOCKET_WRAPPER_KEEP_PCAP
:
153 env
.OPTIONS
+= ' --socket-wrapper-keep-pcap'
154 if Options
.options
.RANDOM_ORDER
:
155 env
.OPTIONS
+= ' --random-order'
156 if os
.environ
.get('RUN_FROM_BUILD_FARM') is not None:
157 env
.FILTER_OPTIONS
= '${FILTER_XFAIL} --strip-passed-output'
159 env
.FILTER_OPTIONS
= '${FILTER_XFAIL}'
161 if Options
.options
.VALGRIND
:
162 os
.environ
['VALGRIND'] = 'valgrind -q --num-callers=30'
163 if Options
.options
.VALGRINDLOG
is not None:
164 os
.environ
['VALGRIND'] += ' --log-file=%s' % Options
.options
.VALGRINDLOG
168 if Options
.options
.VALGRIND_SERVER
:
169 server_wrapper
= '${srcdir}/selftest/valgrind_run _DUMMY=X'
170 elif Options
.options
.GDBTEST
:
171 server_wrapper
= '${srcdir}/selftest/gdb_run _DUMMY=X'
173 if Options
.options
.SCREEN
:
174 server_wrapper
= '${srcdir}/selftest/in_screen %s' % server_wrapper
175 os
.environ
['TERMINAL'] = EXPAND_VARIABLES(opt
, '${srcdir}/selftest/in_screen')
176 elif server_wrapper
!= '':
177 server_wrapper
= 'xterm -n server -l -e %s' % server_wrapper
179 if server_wrapper
!= '':
180 os
.environ
['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
181 os
.environ
['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
182 os
.environ
['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
183 os
.environ
['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt
, server_wrapper
)
185 # this is needed for systems without rpath, or with rpath disabled
186 ADD_LD_LIBRARY_PATH('bin/shared')
187 ADD_LD_LIBRARY_PATH('bin/shared/private')
189 # if we are using a system version of ldb then we need to tell it to
190 # load modules from our modules path
191 if env
.USING_SYSTEM_LDB
:
192 os
.environ
['LDB_MODULES_PATH'] = os
.path
.abspath(os
.path
.join(env
.cwd
, 'bin/modules/ldb'))
194 # tell build system where to find config.h
195 os
.environ
['CONFIG_H'] = 'bin/default/include/config.h'
197 st_done
= os
.path
.join(env
.SELFTEST_PREFIX
, 'st_done')
198 if os
.path
.exists(st_done
):
201 if not os
.path
.isdir(env
.SELFTEST_PREFIX
):
202 os
.makedirs(env
.SELFTEST_PREFIX
, int('755', 8))
204 env
.TESTLISTS
= ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
205 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
206 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
208 if CONFIG_SET(opt
, 'AD_DC_BUILD_IS_ENABLED'):
209 env
.SELFTEST_TARGET
= "samba"
211 env
.SELFTEST_TARGET
= "samba3"
213 env
.OPTIONS
+= " --nss_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBNSS_WRAPPER_SO_PATH')
214 env
.OPTIONS
+= " --socket_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBSOCKET_WRAPPER_SO_PATH')
215 env
.OPTIONS
+= " --uid_wrapper_so_path=" + CONFIG_GET(opt
, 'LIBUID_WRAPPER_SO_PATH')
217 # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
218 env
.CORE_COMMAND
= '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
219 if Options
.options
.LIST
:
220 cmd
= '${CORE_COMMAND} --list'
222 env
.OPTIONS
+= ' --socket-wrapper'
223 cmd
= '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
224 if (os
.environ
.get('RUN_FROM_BUILD_FARM') is None and
225 not Options
.options
.FILTERED_SUBUNIT
):
226 cmd
+= ' | tee ${SELFTEST_PREFIX}/subunit | ${FORMAT_TEST_OUTPUT}'
228 cmd
+= ' | ${FILTER_OPTIONS}'
229 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
231 print("test: running %s" % runcmd
)
232 ret
= RUN_COMMAND(cmd
, env
=env
)
234 if (os
.path
.exists(".testrepository") and
235 not Options
.options
.LIST
and
236 not Options
.options
.LOAD_LIST
):
237 testrcmd
= 'testr load -q < ${SELFTEST_PREFIX}/subunit > /dev/null'
238 runcmd
= EXPAND_VARIABLES(opt
, testrcmd
)
239 RUN_COMMAND(runcmd
, env
=env
)
241 subunit_file
= "%s/subunit" % env
.SELFTEST_PREFIX
242 if os
.path
.exists(subunit_file
):
243 nb
= Options
.options
.NB_SLOWEST
244 cmd
= "./script/show_testsuite_time %s %d" % (subunit_file
, nb
)
245 runcmd
= EXPAND_VARIABLES(opt
, cmd
)
246 RUN_COMMAND(runcmd
, env
=env
)
249 print("ERROR: test failed with exit code %d" % ret
)
252 if not Options
.options
.LIST
and not os
.path
.exists(st_done
):
253 print("ERROR: test command failed to complete")
257 ########################################################################
258 # main test entry point
260 '''Run the test suite (see test options below)'''
262 # if running all tests, then force a symbol check
263 env
= LOAD_ENVIRONMENT()
265 Scripting
.commands
.append('build')
266 Scripting
.commands
.append('testonly')