ctdb-scripts: Enhancements to hung script debugging
[Samba.git] / selftest / wscript
blob3c5457f9610100dbf532ba053135bbe835ab55a0
1 #!/usr/bin/env python
2 # vim: expandtab ft=python
4 # selftest main code.
6 import Scripting, os, Options, Utils, Environment, optparse, sys
7 from samba_utils import *
8 from samba_autoconf import *
9 import types
11 def set_options(opt):
13 opt.add_option('--enable-selftest',
14 help=("enable options necessary for selftest (default=no)"),
15 action="store_true", dest='enable_selftest', default=False)
16 opt.add_option('--enable-coverage',
17 help=("enable options necessary for code coverage reporting on selftest (default=no)"),
18 action="store_true", dest='enable_coverage', default=False)
19 opt.add_option('--with-selftest-prefix',
20 help=("specify location of selftest directory (default=./st)"),
21 action="store", dest='SELFTEST_PREFIX', default='./st')
23 opt.ADD_COMMAND('test', cmd_test)
24 opt.ADD_COMMAND('testonly', cmd_testonly)
26 gr = opt.add_option_group('test options')
28 gr.add_option('--load-list',
29 help=("Load a test id list from a text file"),
30 action="store", dest='LOAD_LIST', default=None)
31 gr.add_option('--list',
32 help=("List available tests"),
33 action="store_true", dest='LIST', default=False)
34 gr.add_option('--tests',
35 help=("wildcard pattern of tests to run"),
36 action="store", dest='TESTS', default='')
37 gr.add_option('--filtered-subunit',
38 help=("output (xfail) filtered subunit"),
39 action="store_true", dest='FILTERED_SUBUNIT', default=False)
40 gr.add_option('--quick',
41 help=("enable only quick tests"),
42 action="store_true", dest='QUICKTEST', default=False)
43 gr.add_option('--slow',
44 help=("enable the really slow tests"),
45 action="store_true", dest='SLOWTEST', default=False)
46 gr.add_option('--testenv',
47 help=("start a terminal with the test environment setup"),
48 action="store_true", dest='TESTENV', default=False)
49 gr.add_option('--valgrind',
50 help=("use valgrind on client programs in the tests"),
51 action="store_true", dest='VALGRIND', default=False)
52 gr.add_option('--valgrind-log',
53 help=("where to put the valgrind log"),
54 action="store", dest='VALGRINDLOG', default=None)
55 gr.add_option('--valgrind-server',
56 help=("use valgrind on the server in the tests (opens an xterm)"),
57 action="store_true", dest='VALGRIND_SERVER', default=False)
58 gr.add_option('--screen',
59 help=("run the samba servers in screen sessions"),
60 action="store_true", dest='SCREEN', default=False)
61 gr.add_option('--gdbtest',
62 help=("run the servers within a gdb window"),
63 action="store_true", dest='GDBTEST', default=False)
64 gr.add_option('--fail-immediately',
65 help=("stop tests on first failure"),
66 action="store_true", dest='FAIL_IMMEDIATELY', default=False)
67 gr.add_option('--socket-wrapper-pcap',
68 help=("create a pcap file for each failing test"),
69 action="store_true", dest='SOCKET_WRAPPER_PCAP', default=False)
70 gr.add_option('--socket-wrapper-keep-pcap',
71 help=("create a pcap file for all individual test"),
72 action="store_true", dest='SOCKET_WRAPPER_KEEP_PCAP', default=False)
73 gr.add_option('--random-order', dest='RANDOM_ORDER', default=False,
74 action="store_true", help="Run testsuites in random order")
76 def configure(conf):
77 conf.env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
78 conf.env.enable_coverage = Options.options.enable_coverage
79 if conf.env.enable_coverage:
80 conf.ADD_LDFLAGS('-lgcov', testflags=True)
81 conf.ADD_CFLAGS('--coverage')
83 if Options.options.enable_selftest or Options.options.developer:
84 conf.DEFINE('ENABLE_SELFTEST', 1)
87 def cmd_testonly(opt):
88 '''run tests without doing a build first'''
89 env = LOAD_ENVIRONMENT()
90 opt.env = env
92 if (not CONFIG_SET(opt, 'NSS_WRAPPER') or
93 not CONFIG_SET(opt, 'UID_WRAPPER') or
94 not CONFIG_SET(opt, 'SOCKET_WRAPPER')):
95 print("ERROR: You must use --enable-selftest to enable selftest")
96 sys.exit(1)
98 os.environ['SAMBA_SELFTEST'] = '1'
100 env.TESTS = Options.options.TESTS
102 env.SUBUNIT_FORMATTER = os.getenv('SUBUNIT_FORMATTER')
103 if not env.SUBUNIT_FORMATTER:
104 env.SUBUNIT_FORMATTER = '${PYTHON} -u ${srcdir}/selftest/format-subunit --prefix=${SELFTEST_PREFIX} --immediate'
105 env.FILTER_XFAIL = '${PYTHON} -u ${srcdir}/selftest/filter-subunit --expected-failures=${srcdir}/selftest/knownfail --flapping=${srcdir}/selftest/flapping'
107 if Options.options.FAIL_IMMEDIATELY:
108 env.FILTER_XFAIL += ' --fail-immediately'
110 env.FORMAT_TEST_OUTPUT = '${SUBUNIT_FORMATTER}'
112 # clean any previous temporary files
113 os.system("rm -rf %s/tmp" % env.SELFTEST_PREFIX);
115 # put all command line options in the environment as TESTENV_*=*
116 for o in dir(Options.options):
117 if o[0:1] != '_':
118 val = getattr(Options.options, o, '')
119 if not issubclass(type(val), types.FunctionType) \
120 and not issubclass(type(val), types.MethodType):
121 os.environ['TESTENV_%s' % o.upper()] = str(getattr(Options.options, o, ''))
124 binary_mapping = ('nmblookup3:nmblookup,' +
125 'nmblookup4:nmblookup4,' +
126 'smbclient3:smbclient,' +
127 'smbclient4:smbclient4,' +
128 'smbtorture4:smbtorture,' +
129 'ntlm_auth3:ntlm_auth')
131 env.OPTIONS = '--binary-mapping=%s' % binary_mapping
132 if not Options.options.SLOWTEST:
133 env.OPTIONS += ' --exclude=${srcdir}/selftest/slow'
134 if Options.options.QUICKTEST:
135 env.OPTIONS += ' --quick --include=${srcdir}/selftest/quick'
136 if Options.options.LOAD_LIST:
137 env.OPTIONS += ' --load-list=%s' % Options.options.LOAD_LIST
138 if Options.options.TESTENV:
139 env.OPTIONS += ' --testenv'
140 if Options.options.SOCKET_WRAPPER_PCAP:
141 env.OPTIONS += ' --socket-wrapper-pcap'
142 if Options.options.SOCKET_WRAPPER_KEEP_PCAP:
143 env.OPTIONS += ' --socket-wrapper-keep-pcap'
144 if Options.options.RANDOM_ORDER:
145 env.OPTIONS += ' --random-order'
146 if os.environ.get('RUN_FROM_BUILD_FARM') is not None:
147 env.FILTER_OPTIONS = '${FILTER_XFAIL} --strip-passed-output'
148 else:
149 env.FILTER_OPTIONS = '${FILTER_XFAIL}'
151 if Options.options.VALGRIND:
152 os.environ['VALGRIND'] = 'valgrind -q --num-callers=30'
153 if Options.options.VALGRINDLOG is not None:
154 os.environ['VALGRIND'] += ' --log-file=%s' % Options.options.VALGRINDLOG
156 server_wrapper=''
158 if Options.options.VALGRIND_SERVER:
159 server_wrapper = '${srcdir}/selftest/valgrind_run _DUMMY=X'
160 elif Options.options.GDBTEST:
161 server_wrapper = '${srcdir}/selftest/gdb_run _DUMMY=X'
163 if Options.options.SCREEN:
164 server_wrapper = '${srcdir}/selftest/in_screen %s' % server_wrapper
165 os.environ['TERMINAL'] = EXPAND_VARIABLES(opt, '${srcdir}/selftest/in_screen')
166 elif server_wrapper != '':
167 server_wrapper = 'xterm -n server -l -e %s' % server_wrapper
169 if server_wrapper != '':
170 os.environ['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
171 os.environ['NMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
172 os.environ['WINBINDD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
173 os.environ['SMBD_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
175 # this is needed for systems without rpath, or with rpath disabled
176 ADD_LD_LIBRARY_PATH('bin/shared')
177 ADD_LD_LIBRARY_PATH('bin/shared/private')
179 # if we are using a system version of ldb then we need to tell it to
180 # load modules from our modules path
181 if env.USING_SYSTEM_LDB:
182 os.environ['LDB_MODULES_PATH'] = 'bin/modules/ldb'
184 # tell build system where to find config.h
185 os.environ['CONFIG_H'] = 'bin/default/include/config.h'
187 st_done = os.path.join(env.SELFTEST_PREFIX, 'st_done')
188 if os.path.exists(st_done):
189 os.unlink(st_done)
191 if not os.path.isdir(env.SELFTEST_PREFIX):
192 os.makedirs(env.SELFTEST_PREFIX, int('755', 8))
194 env.TESTLISTS = ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
195 '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
196 '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
198 if CONFIG_SET(opt, 'AD_DC_BUILD_IS_ENABLED'):
199 env.SELFTEST_TARGET="samba"
200 else:
201 env.SELFTEST_TARGET="samba3"
203 # We use the full path rather than relative path because it cause problems on some plateforms (ie. solaris 8).
204 env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
205 if Options.options.LIST:
206 cmd = '${CORE_COMMAND} --list'
207 else:
208 env.OPTIONS += ' --socket-wrapper'
209 cmd = '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
210 if (os.environ.get('RUN_FROM_BUILD_FARM') is None and
211 not Options.options.FILTERED_SUBUNIT):
212 cmd += ' | tee ${SELFTEST_PREFIX}/subunit | ${FORMAT_TEST_OUTPUT}'
213 else:
214 cmd += ' | ${FILTER_OPTIONS}'
215 runcmd = EXPAND_VARIABLES(opt, cmd)
217 print("test: running %s" % runcmd)
218 ret = RUN_COMMAND(cmd, env=env)
220 if os.path.exists(".testrepository") and not Options.options.LIST:
221 testrcmd = 'testr load -q < ${SELFTEST_PREFIX}/subunit > /dev/null'
222 runcmd = EXPAND_VARIABLES(opt, testrcmd)
223 RUN_COMMAND(runcmd, env=env)
225 if ret != 0:
226 print("ERROR: test failed with exit code %d" % ret)
227 sys.exit(ret)
229 if not Options.options.LIST and not os.path.exists(st_done):
230 print("ERROR: test command failed to complete")
231 sys.exit(1)
234 ########################################################################
235 # main test entry point
236 def cmd_test(opt):
237 '''Run the test suite (see test options below)'''
239 # if running all tests, then force a symbol check
240 env = LOAD_ENVIRONMENT()
241 CHECK_MAKEFLAGS(env)
242 if not Options.options.TESTS and not Options.options.TESTENV:
243 Options.options.DUP_SYMBOLCHECK = True
245 Scripting.commands.append('build')
246 Scripting.commands.append('testonly')