wscript 14.4 KB
Newer Older
1
#!/usr/bin/env python
2
# vim: expandtab ft=python
3

4 5
# 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
10

11 12
DEFAULT_SELFTEST_PREFIX="./st"

13
def set_options(opt):
14 15 16 17

    opt.add_option('--enable-selftest',
                   help=("enable options necessary for selftest (default=no)"),
                   action="store_true", dest='enable_selftest', default=False)
18
    opt.add_option('--enable-coverage',
19 20
                   help=("enable options necessary for code coverage "
                         "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 25
                         "(default=%s)" % DEFAULT_SELFTEST_PREFIX),
                   action="store", dest='SELFTEST_PREFIX', default=DEFAULT_SELFTEST_PREFIX)
26

27 28 29 30 31
    opt.ADD_COMMAND('test', cmd_test)
    opt.ADD_COMMAND('testonly', cmd_testonly)

    gr = opt.add_option_group('test options')

32
    gr.add_option('--load-list',
33 34
                  help=("Load a test id list from a text file"),
                  action="store", dest='LOAD_LIST', default=None)
35
    gr.add_option('--list',
36 37
                  help=("List available tests"),
                  action="store_true", dest='LIST', default=False)
38 39 40
    gr.add_option('--tests',
                  help=("wildcard pattern of tests to run"),
                  action="store", dest='TESTS', default='')
41 42 43
    gr.add_option('--filtered-subunit',
                  help=("output (xfail) filtered subunit"),
                  action="store_true", dest='FILTERED_SUBUNIT', default=False)
44 45 46 47 48 49
    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)
50 51 52
    gr.add_option('--nb-slowest',
                  help=("Show the n slowest tests (default=10)"),
                  type=int, default=10, dest='NB_SLOWEST')
53 54 55 56 57 58 59 60 61 62 63 64
    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)
65 66 67
    gr.add_option('--screen',
                  help=("run the samba servers in screen sessions"),
                  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 72 73
    gr.add_option('--fail-immediately',
                  help=("stop tests on first failure"),
                  action="store_true", dest='FAIL_IMMEDIATELY', default=False)
74 75 76 77 78 79
    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)
80 81
    gr.add_option('--random-order', dest='RANDOM_ORDER', default=False,
                  action="store_true", help="Run testsuites in random order")
82 83
    gr.add_option('--perf-test', dest='PERF_TEST', default=False,
                  action="store_true", help="run performance tests only")
84 85 86 87
    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)"))
88

89 90
def configure(conf):
    conf.env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
91 92 93 94
    conf.env.enable_coverage = Options.options.enable_coverage
    if conf.env.enable_coverage:
        conf.ADD_LDFLAGS('-lgcov', testflags=True)
        conf.ADD_CFLAGS('--coverage')
95

96 97 98 99
    if Options.options.enable_selftest or Options.options.developer:
        conf.DEFINE('ENABLE_SELFTEST', 1)


100
def cmd_testonly(opt):
101
    '''run tests without doing a build first'''
102
    env = LOAD_ENVIRONMENT()
103
    opt.env = env
104

105 106
    if Options.options.SELFTEST_PREFIX != DEFAULT_SELFTEST_PREFIX:
        env.SELFTEST_PREFIX = Options.options.SELFTEST_PREFIX
107

108 109 110
    if (not CONFIG_SET(opt, 'NSS_WRAPPER') or
        not CONFIG_SET(opt, 'UID_WRAPPER') or
        not CONFIG_SET(opt, 'SOCKET_WRAPPER')):
111
        print("ERROR: You must use --enable-selftest to enable selftest")
112 113
        sys.exit(1)

114 115
    os.environ['SAMBA_SELFTEST'] = '1'

116 117
    env.TESTS  = Options.options.TESTS

118 119
    env.SUBUNIT_FORMATTER = os.getenv('SUBUNIT_FORMATTER')
    if not env.SUBUNIT_FORMATTER:
120 121 122 123
        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'
124 125 126 127 128
    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')
129 130 131 132

    if Options.options.FAIL_IMMEDIATELY:
        env.FILTER_XFAIL += ' --fail-immediately'

133
    env.FORMAT_TEST_OUTPUT = '${SUBUNIT_FORMATTER}'
134

135 136 137
    # clean any previous temporary files
    os.system("rm -rf %s/tmp" % env.SELFTEST_PREFIX);

138 139 140
    # put all command line options in the environment as TESTENV_*=*
    for o in dir(Options.options):
        if o[0:1] != '_':
141 142 143 144 145
            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, ''))

146
    env.OPTIONS = ''
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 152
    if Options.options.LOAD_LIST:
        env.OPTIONS += ' --load-list=%s' % Options.options.LOAD_LIST
153 154
    if Options.options.TESTENV:
        env.OPTIONS += ' --testenv'
155 156 157 158
    if Options.options.SOCKET_WRAPPER_PCAP:
        env.OPTIONS += ' --socket-wrapper-pcap'
    if Options.options.SOCKET_WRAPPER_KEEP_PCAP:
        env.OPTIONS += ' --socket-wrapper-keep-pcap'
159 160
    if Options.options.RANDOM_ORDER:
        env.OPTIONS += ' --random-order'
161 162 163 164
    if Options.options.PERF_TEST:
        env.FILTER_OPTIONS = ('${PYTHON} -u ${srcdir}/selftest/filter-subunit '
                              '--perf-test-output')
    elif os.environ.get('RUN_FROM_BUILD_FARM') is not None:
165 166
        env.FILTER_OPTIONS = '${FILTER_XFAIL} --strip-passed-output'
    else:
167
        env.FILTER_OPTIONS = '${FILTER_XFAIL}'
168 169

    if Options.options.VALGRIND:
170
        os.environ['VALGRIND'] = 'valgrind -q --num-callers=30'
171 172 173
        if Options.options.VALGRINDLOG is not None:
            os.environ['VALGRIND'] += ' --log-file=%s' % Options.options.VALGRINDLOG

174 175
    server_wrapper=''

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'
180 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 185
    elif server_wrapper != '':
        server_wrapper = 'xterm -n server -l -e %s' % server_wrapper
186

187
    if server_wrapper != '':
188
        os.environ['SAMBA_VALGRIND'] = EXPAND_VARIABLES(opt, server_wrapper)
189 190 191
        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)
192

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')
196

197 198 199
    # 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:
200
        os.environ['LDB_MODULES_PATH'] = os.path.abspath(os.path.join(env.cwd, 'bin/modules/ldb'))
201

202
    # tell build system where to find config.h
203
    os.environ['CONFIG_H'] = 'bin/default/include/config.h'
204

205 206 207
    # tell the test system where perl is
    os.environ['PERL'] = env.PERL

208
    st_done = os.path.join(env.SELFTEST_PREFIX, 'st_done')
209 210 211
    if os.path.exists(st_done):
        os.unlink(st_done)

212 213 214
    if not os.path.isdir(env.SELFTEST_PREFIX):
        os.makedirs(env.SELFTEST_PREFIX, int('755', 8))

215 216 217
    if Options.options.TEST_LIST:
        env.TESTLISTS = '--testlist=%r' % Options.options.TEST_LIST
    elif Options.options.PERF_TEST:
218
        env.TESTLISTS = '--testlist="${PYTHON} ${srcdir}/selftest/perf_tests.py|" '
219 220 221 222
    elif CONFIG_SET(opt, 'EXTRA_PYTHON'):
        env.TESTLISTS = ('--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/selftest/tests.py|" ' +
                         '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
                         '--testlist="EXTRA_PYTHON=${EXTRA_PYTHON} ${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
223 224 225 226
    else:
        env.TESTLISTS = ('--testlist="${PYTHON} ${srcdir}/selftest/tests.py|" ' +
                         '--testlist="${PYTHON} ${srcdir}/source3/selftest/tests.py|" ' +
                         '--testlist="${PYTHON} ${srcdir}/source4/selftest/tests.py|"')
227

228
    if CONFIG_SET(opt, 'AD_DC_BUILD_IS_ENABLED'):
229
        env.SELFTEST_TARGET = "samba"
230
    else:
231
        env.SELFTEST_TARGET = "samba3"
232

233
    env.OPTIONS += " --nss_wrapper_so_path=" + CONFIG_GET(opt, 'LIBNSS_WRAPPER_SO_PATH')
234
    env.OPTIONS += " --resolv_wrapper_so_path=" + CONFIG_GET(opt, 'LIBRESOLV_WRAPPER_SO_PATH')
235
    env.OPTIONS += " --socket_wrapper_so_path=" + CONFIG_GET(opt, 'LIBSOCKET_WRAPPER_SO_PATH')
236 237
    env.OPTIONS += " --uid_wrapper_so_path=" + CONFIG_GET(opt, 'LIBUID_WRAPPER_SO_PATH')

238 239 240 241 242 243
    #if unversioned_sys_platform in ('freebsd', 'netbsd', 'openbsd', 'sunos'):
    #    env.OPTIONS += " --use-dns-faking"

    # FIXME REMOVE ME!
    env.OPTIONS += " --use-dns-faking"

244
    if CONFIG_GET(opt, 'USING_SYSTEM_KRB5') and CONFIG_GET(opt, 'MIT_KDC_PATH'):
245
        env.OPTIONS += " --mitkrb5 --exclude=${srcdir}/selftest/skip_mit_kdc"
246

247 248 249 250
    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"
251

252 253 254 255 256 257 258 259
    if env.ADDRESS_SANITIZER:
        # We try to find the correct libasan automatically
        libasan = Utils.cmd_output('ldd bin/texpect | grep libasan| cut -f 3 -d \ ',
                                   silent=True).strip()

        # Have the selftest.pl LD_PRELOAD libasan in the right spot
        env.OPTIONS += " --asan_so_path=" + libasan

260
    subunit_cache = None
261
    # We use the full path rather than relative path to avoid problems on some platforms (ie. solaris 8).
262
    env.CORE_COMMAND = '${PERL} ${srcdir}/selftest/selftest.pl --target=${SELFTEST_TARGET} --prefix=${SELFTEST_PREFIX} --srcdir=${srcdir} --exclude=${srcdir}/selftest/skip ${TESTLISTS} ${OPTIONS} ${TESTS}'
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    if env.ADDRESS_SANITIZER:
        # For now we cannot run with leak detection
        no_leak_check = "ASAN_OPTIONS=detect_leaks=0"
        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

278 279
    if Options.options.LIST:
        cmd = '${CORE_COMMAND} --list'
280
    else:
281
        env.OPTIONS += ' --socket-wrapper'
282
        cmd = '(${CORE_COMMAND} && touch ${SELFTEST_PREFIX}/st_done) | ${FILTER_OPTIONS}'
283 284
        if (os.environ.get('RUN_FROM_BUILD_FARM') is None and
            not Options.options.FILTERED_SUBUNIT):
285
            subunit_cache = os.path.join(env.SELFTEST_PREFIX, "subunit")
286
            cmd += ' | tee %s | ${FORMAT_TEST_OUTPUT}' % subunit_cache
287 288
        else:
            cmd += ' | ${FILTER_OPTIONS}'
289

290
    runcmd = EXPAND_VARIABLES(opt, cmd)
291

292
    print("test: running %s" % runcmd)
293
    ret = RUN_COMMAND(cmd, env=env)
294

295 296
    if (os.path.exists(".testrepository") and
        not Options.options.LIST and
297 298 299
        not Options.options.LOAD_LIST and
        subunit_cache is not None):
        testrcmd = 'testr load -q < %s > /dev/null' % subunit_cache
300 301
        runcmd = EXPAND_VARIABLES(opt, testrcmd)
        RUN_COMMAND(runcmd, env=env)
302

303
    if subunit_cache is not None:
304
        nb = Options.options.NB_SLOWEST
305
        cmd = "./script/show_testsuite_time %s %d" % (subunit_cache, nb)
306 307 308
        runcmd = EXPAND_VARIABLES(opt, cmd)
        RUN_COMMAND(runcmd, env=env)

309
    if ret != 0:
310
        print("ERROR: test failed with exit code %d" % ret)
311 312
        sys.exit(ret)

313
    if not Options.options.LIST and not os.path.exists(st_done):
314
        print("ERROR: test command failed to complete")
315 316
        sys.exit(1)

317

318 319 320
########################################################################
# main test entry point
def cmd_test(opt):
321
    '''Run the test suite (see test options below)'''
322 323 324 325

    # if running all tests, then force a symbol check
    env = LOAD_ENVIRONMENT()
    CHECK_MAKEFLAGS(env)
326 327
    Scripting.commands.append('build')
    Scripting.commands.append('testonly')