tests/fsmonitor-run-tests.py
author Kevin Bullock <kbullock+mercurial@ringworld.org>
Tue, 06 Mar 2018 13:17:07 -0600
branchstable
changeset 36757 8bba684efde7
parent 32769 efd6e941e933
child 40208 b7ba1cfba174
permissions -rwxr-xr-x
merge with security patches
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32769
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     1
#!/usr/bin/env python
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     2
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     3
# fsmonitor-run-tests.py - Run Mercurial tests with fsmonitor enabled
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     4
#
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     5
# Copyright 2017 Facebook, Inc.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     6
#
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     7
# This software may be used and distributed according to the terms of the
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     8
# GNU General Public License version 2 or any later version.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     9
#
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    10
# This is a wrapper around run-tests.py that spins up an isolated instance of
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    11
# Watchman and runs the Mercurial tests against it. This ensures that the global
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    12
# version of Watchman isn't affected by anything this test does.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    13
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    14
from __future__ import absolute_import
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    15
from __future__ import print_function
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    16
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    17
import argparse
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    18
import contextlib
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    19
import json
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    20
import os
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    21
import shutil
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    22
import subprocess
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    23
import sys
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    24
import tempfile
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    25
import uuid
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    26
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    27
osenvironb = getattr(os, 'environb', os.environ)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    28
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    29
if sys.version_info > (3, 5, 0):
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    30
    PYTHON3 = True
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    31
    xrange = range # we use xrange in one place, and we'd rather not use range
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    32
    def _bytespath(p):
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    33
        return p.encode('utf-8')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    34
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    35
elif sys.version_info >= (3, 0, 0):
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    36
    print('%s is only supported on Python 3.5+ and 2.7, not %s' %
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    37
          (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3])))
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    38
    sys.exit(70) # EX_SOFTWARE from `man 3 sysexit`
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    39
else:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    40
    PYTHON3 = False
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    41
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    42
    # In python 2.x, path operations are generally done using
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    43
    # bytestrings by default, so we don't have to do any extra
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    44
    # fiddling there. We define the wrapper functions anyway just to
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    45
    # help keep code consistent between platforms.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    46
    def _bytespath(p):
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    47
        return p
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    48
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    49
def getparser():
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    50
    """Obtain the argument parser used by the CLI."""
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    51
    parser = argparse.ArgumentParser(
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    52
        description='Run tests with fsmonitor enabled.',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    53
        epilog='Unrecognized options are passed to run-tests.py.')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    54
    # - keep these sorted
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    55
    # - none of these options should conflict with any in run-tests.py
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    56
    parser.add_argument('--keep-fsmonitor-tmpdir', action='store_true',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    57
        help='keep temporary directory with fsmonitor state')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    58
    parser.add_argument('--watchman',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    59
        help='location of watchman binary (default: watchman in PATH)',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    60
        default='watchman')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    61
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    62
    return parser
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    63
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    64
@contextlib.contextmanager
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    65
def watchman(args):
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    66
    basedir = tempfile.mkdtemp(prefix='hg-fsmonitor')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    67
    try:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    68
        # Much of this configuration is borrowed from Watchman's test harness.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    69
        cfgfile = os.path.join(basedir, 'config.json')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    70
        # TODO: allow setting a config
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    71
        with open(cfgfile, 'w') as f:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    72
            f.write(json.dumps({}))
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    73
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    74
        logfile = os.path.join(basedir, 'log')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    75
        clilogfile = os.path.join(basedir, 'cli-log')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    76
        if os.name == 'nt':
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    77
            sockfile = '\\\\.\\pipe\\watchman-test-%s' % uuid.uuid4().hex
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    78
        else:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    79
            sockfile = os.path.join(basedir, 'sock')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    80
        pidfile = os.path.join(basedir, 'pid')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    81
        statefile = os.path.join(basedir, 'state')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    82
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    83
        argv = [
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    84
            args.watchman,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    85
            '--sockname', sockfile,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    86
            '--logfile', logfile,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    87
            '--pidfile', pidfile,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    88
            '--statefile', statefile,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    89
            '--foreground',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    90
            '--log-level=2', # debug logging for watchman
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    91
        ]
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    92
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    93
        envb = osenvironb.copy()
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    94
        envb[b'WATCHMAN_CONFIG_FILE'] = _bytespath(cfgfile)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    95
        with open(clilogfile, 'wb') as f:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    96
            proc = subprocess.Popen(
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    97
                argv, env=envb, stdin=None, stdout=f, stderr=f)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    98
            try:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    99
                yield sockfile
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   100
            finally:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   101
                proc.terminate()
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   102
                proc.kill()
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   103
    finally:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   104
        if args.keep_fsmonitor_tmpdir:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   105
            print('fsmonitor dir available at %s' % basedir)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   106
        else:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   107
            shutil.rmtree(basedir, ignore_errors=True)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   108
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   109
def run():
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   110
    parser = getparser()
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   111
    args, runtestsargv = parser.parse_known_args()
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   112
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   113
    with watchman(args) as sockfile:
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   114
        osenvironb[b'WATCHMAN_SOCK'] = _bytespath(sockfile)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   115
        # Indicate to hghave that we're running with fsmonitor enabled.
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   116
        osenvironb[b'HGFSMONITOR_TESTS'] = b'1'
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   117
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   118
        runtestdir = os.path.dirname(__file__)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   119
        runtests = os.path.join(runtestdir, 'run-tests.py')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   120
        blacklist = os.path.join(runtestdir, 'blacklists', 'fsmonitor')
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   121
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   122
        runtestsargv.insert(0, runtests)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   123
        runtestsargv.extend([
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   124
            '--extra-config',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   125
            'extensions.fsmonitor=',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   126
            '--blacklist',
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   127
            blacklist,
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   128
        ])
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   129
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   130
        return subprocess.call(runtestsargv)
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   131
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   132
if __name__ == '__main__':
efd6e941e933 tests: add a wrapper to run fsmonitor tests
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   133
    sys.exit(run())