summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/test/qa-tests/buildscripts/resmokelib/selector.py
blob: c2dc0fca41b79379cd8f15db7053660e1d0a996f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Test selection utility.

Defines filtering rules for what tests to include in a suite depending
on whether they apply to C++ unit tests, dbtests, or JS tests.
"""

from __future__ import absolute_import

import fnmatch
import os.path
import subprocess
import sys

from . import config
from . import errors
from . import utils
from .utils import globstar
from .utils import jscomment

def _filter_cpp_tests(kind, root, include_files, exclude_files):
    """
    Generic filtering logic for C++ tests that are sourced from a list
    of test executables.
    """
    include_files = utils.default_if_none(include_files, [])
    exclude_files = utils.default_if_none(exclude_files, [])

    tests = []
    with open(root, "r") as fp:
        for test_path in fp:
            test_path = test_path.rstrip()
            tests.append(test_path)

    (remaining, included, _) = _filter_by_filename(kind,
                                                   tests,
                                                   include_files,
                                                   exclude_files)

    if include_files:
        return list(included)
    elif exclude_files:
        return list(remaining)
    return tests

def filter_cpp_unit_tests(root="build/unittests.txt", include_files=None, exclude_files=None):
    """
    Filters out what C++ unit tests to run.
    """
    return _filter_cpp_tests("C++ unit test", root, include_files, exclude_files)


def filter_cpp_integration_tests(root="build/integration_tests.txt",
                                 include_files=None,
                                 exclude_files=None):
    """
    Filters out what C++ integration tests to run.
    """
    return _filter_cpp_tests("C++ integration test", root, include_files, exclude_files)


def filter_dbtests(binary=None, include_suites=None):
    """
    Filters out what dbtests to run.
    """

    # Command line option overrides the YAML configuration.
    binary = utils.default_if_none(config.DBTEST_EXECUTABLE, binary)
    # Use the default if nothing specified.
    binary = utils.default_if_none(binary, config.DEFAULT_DBTEST_EXECUTABLE)

    include_suites = utils.default_if_none(include_suites, [])

    if not utils.is_string_list(include_suites):
        raise TypeError("include_suites must be a list of strings")

    # Ensure that executable files on Windows have a ".exe" extension.
    if sys.platform == "win32" and os.path.splitext(binary)[1] != ".exe":
        binary += ".exe"

    program = subprocess.Popen([binary, "--list"], stdout=subprocess.PIPE)
    stdout = program.communicate()[0]

    if program.returncode != 0:
        raise errors.ResmokeError("Getting list of dbtest suites failed")

    dbtests = stdout.splitlines()

    if not include_suites:
        return dbtests

    dbtests = set(dbtests)

    (verbatim, globbed) = _partition(include_suites, normpath=False)
    included = _pop_all("dbtest suite", dbtests, verbatim)

    for suite_pattern in globbed:
        for suite_name in dbtests:
            if fnmatch.fnmatchcase(suite_name, suite_pattern):
                included.add(suite_name)

    return list(included)


def filter_jstests(roots,
                   include_files=None,
                   include_with_all_tags=None,
                   include_with_any_tags=None,
                   exclude_files=None,
                   exclude_with_all_tags=None,
                   exclude_with_any_tags=None):
    """
    Filters out what jstests to run.
    """

    include_files = utils.default_if_none(include_files, [])
    exclude_files = utils.default_if_none(exclude_files, [])

    # Command line options override the YAML options, and all should be defaulted to an empty list
    # if not specified.
    tags = {
        "exclude_with_all_tags": exclude_with_all_tags,
        "exclude_with_any_tags": exclude_with_any_tags,
        "include_with_all_tags": include_with_all_tags,
        "include_with_any_tags": include_with_any_tags,
    }
    cmd_line_values = (
        ("exclude_with_all_tags", config.EXCLUDE_WITH_ALL_TAGS),
        ("exclude_with_any_tags", config.EXCLUDE_WITH_ANY_TAGS),
        ("include_with_all_tags", config.INCLUDE_WITH_ALL_TAGS),
        ("include_with_any_tags", config.INCLUDE_WITH_ANY_TAGS),
    )
    for (tag_category, cmd_line_val) in cmd_line_values:
        if cmd_line_val is not None:
            # Ignore the empty string when it is used as a tag. Specifying an empty string on the
            # command line allows a user to unset the list of tags specified in the YAML
            # configuration.
            tags[tag_category] = set([tag for tag in cmd_line_val.split(",") if tag != ""])
        else:
            tags[tag_category] = set(utils.default_if_none(tags[tag_category], []))

    using_tags = 0
    for name in tags:
        if not utils.is_string_set(tags[name]):
            raise TypeError("%s must be a list of strings" % (name))
        if len(tags[name]) > 0:
            using_tags += 1

    if using_tags > 1:
        raise ValueError("Can only specify one of 'include_with_all_tags', 'include_with_any_tags',"
                         " 'exclude_with_all_tags', and 'exclude_with_any_tags'. If you wish to"
                         " unset one of these options, use --includeWithAllTags='' or similar")

    jstests = []
    for root in roots:
        jstests.extend(globstar.iglob(root))

    (remaining, included, _) = _filter_by_filename("jstest",
                                                   jstests,
                                                   include_files,
                                                   exclude_files)

    # Skip parsing comments if not using tags
    if not using_tags:
        if include_files:
            return list(included)
        elif exclude_files:
            return list(remaining)
        return jstests

    jstests = set(remaining)
    excluded = set()

    for filename in jstests:
        file_tags = set(jscomment.get_tags(filename))
        if tags["include_with_all_tags"] and not tags["include_with_all_tags"] - file_tags:
            included.add(filename)
        elif tags["include_with_any_tags"] and tags["include_with_any_tags"] & file_tags:
            included.add(filename)
        elif tags["exclude_with_all_tags"] and not tags["exclude_with_all_tags"] - file_tags:
            excluded.add(filename)
        elif tags["exclude_with_any_tags"] and tags["exclude_with_any_tags"] & file_tags:
            excluded.add(filename)

    if tags["include_with_all_tags"] or tags["include_with_any_tags"]:
        if exclude_files:
            return list((included & jstests) - excluded)
        return list(included)
    else:
        if include_files:
            return list(included | (jstests - excluded))
        return list(jstests - excluded)


def _filter_by_filename(kind, universe, include_files, exclude_files):
    """
    Filters out what tests to run solely by filename.

    Returns the triplet (remaining, included, excluded), where
    'remaining' is 'universe' after 'included' and 'excluded' were
    removed from it.
    """

    if not utils.is_string_list(include_files):
        raise TypeError("include_files must be a list of strings")
    elif not utils.is_string_list(exclude_files):
        raise TypeError("exclude_files must be a list of strings")
    elif include_files and exclude_files:
        raise ValueError("Cannot specify both include_files and exclude_files")

    universe = set(universe)
    if include_files:
        (verbatim, globbed) = _partition(include_files)
        # Remove all matching files of 'verbatim' from 'universe'.
        included_verbatim = _pop_all(kind, universe, verbatim)
        included_globbed = set()

        for file_pattern in globbed:
            included_globbed.update(globstar.iglob(file_pattern))

        # Remove all matching files of 'included_globbed' from 'universe' without checking whether
        # the same file is expanded to multiple times. This implicitly takes an intersection
        # between 'included_globbed' and 'universe'.
        included_globbed = _pop_all(kind, universe, included_globbed, validate=False)
        return (universe, included_verbatim | included_globbed, set())

    elif exclude_files:
        (verbatim, globbed) = _partition(exclude_files)

        # Remove all matching files of 'verbatim' from 'universe'.
        excluded_verbatim = _pop_all(kind, universe, verbatim)
        excluded_globbed = set()

        for file_pattern in globbed:
            excluded_globbed.update(globstar.iglob(file_pattern))

        # Remove all matching files of 'excluded_globbed' from 'universe' without checking whether
        # the same file is expanded to multiple times. This implicitly takes an intersection
        # between 'excluded_globbed' and 'universe'.
        excluded_globbed = _pop_all(kind, universe, excluded_globbed, validate=False)
        return (universe, set(), excluded_verbatim | excluded_globbed)

    return (universe, set(), set())


def _partition(pathnames, normpath=True):
    """
    Splits 'pathnames' into two separate lists based on whether they
    use a glob pattern.

    Returns the pair (non-globbed pathnames, globbed pathnames).
    """

    verbatim = []
    globbed = []

    for pathname in pathnames:
        if globstar.is_glob_pattern(pathname):
            globbed.append(pathname)
            continue

        # Normalize 'pathname' so exact string comparison can be used later.
        if normpath:
            pathname = os.path.normpath(pathname)
        verbatim.append(pathname)

    return (verbatim, globbed)


def _pop_all(kind, universe, iterable, validate=True):
    """
    Removes all elements of 'iterable' from 'universe' and returns them.

    If 'validate' is true, then a ValueError is raised if a element
    would be removed multiple times, or if an element of 'iterable' does
    not appear in 'universe' at all.
    """

    members = set()

    for elem in iterable:
        if validate and elem in members:
            raise ValueError("%s '%s' specified multiple times" % (kind, elem))

        if elem in universe:
            universe.remove(elem)
            members.add(elem)
        elif validate:
            raise ValueError("Unrecognized %s '%s'" % (kind, elem))

    return members