Debian Codegraph @

testing/mozbase/moztest/tests/test_resolve.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 # flake8: noqa: E501
5
6 try:
7 import cPickle as pickle
8 except ImportError:
9 import pickle
10
11 import json
12 import os
13 import re
14 from collections import defaultdict
15
16 import manifestupdate
17 import mozpack.path as mozpath
18 import mozunit
19 import pytest
20 from mozbuild.frontend.reader import BuildReader
21 from mozbuild.test.common import MockConfig
22 from moztest.resolve import (
23 TEST_SUITES,
24 BuildBackendLoader,
25 TestManifestLoader,
26 TestResolver,
27 )
28
29 here = os.path.abspath(os.path.dirname(__file__))
30 data_path = os.path.join(here, "data")
31
32
33 @pytest.fixture(scope="module")
34 def topsrcdir():
35 return mozpath.join(data_path, "srcdir")
36
37
38 @pytest.fixture(scope="module")
39 def create_tests(topsrcdir):
40 def inner(*paths, **defaults):
41 tests = defaultdict(list)
42 for path in paths:
43 if isinstance(path, tuple):
44 path, kwargs = path
45 else:
46 kwargs = {}
47
48 path = mozpath.normpath(path)
49 manifest_name = kwargs.get("flavor", defaults.get("flavor", "manifest"))
50 manifest = kwargs.pop(
51 "manifest",
52 defaults.pop(
53 "manifest",
54 mozpath.join(mozpath.dirname(path), manifest_name + ".toml"),
55 ),
56 )
57
58 manifest_abspath = mozpath.join(topsrcdir, manifest)
59 relpath = mozpath.relpath(path, mozpath.dirname(manifest))
60 test = {
61 "name": relpath,
62 "path": mozpath.join(topsrcdir, path),
63 "relpath": relpath,
64 "file_relpath": path,
65 "flavor": "faketest",
66 "dir_relpath": mozpath.dirname(path),
67 "here": mozpath.dirname(manifest_abspath),
68 "manifest": manifest_abspath,
69 "manifest_relpath": manifest,
70 }
71 test.update(**defaults)
72 test.update(**kwargs)
73
74 # Normalize paths to ensure that the fixture matches reality.
75 for k in [
76 "ancestor_manifest",
77 "manifest",
78 "manifest_relpath",
79 "path",
80 "relpath",
81 ]:
82 p = test.get(k)
83 if p:
84 test[k] = p.replace("/", os.path.sep)
85
86 tests[path].append(test)
87
88 # dump tests to stdout for easier debugging on failure
89 print("The 'create_tests' fixture returned:")
90 print(json.dumps(dict(tests), indent=2, sort_keys=True))
91 return tests
92
93 return inner
94
95
96 @pytest.fixture(scope="module")
97 def all_tests(create_tests):
98 return create_tests(
99 *[
100 (
101 "apple/test_a11y.html",
102 {
103 "expected": "pass",
104 "manifest": "apple/a11y.toml",
105 "flavor": "a11y",
106 },
107 ),
108 (
109 "banana/currant/test_xpcshell_A.js",
110 {
111 "firefox-appdir": "browser",
112 "flavor": "xpcshell",
113 "head": "head_global.js head_helpers.js head_http.js",
114 },
115 ),
116 (
117 "banana/currant/test_xpcshell_B.js",
118 {
119 "firefox-appdir": "browser",
120 "flavor": "xpcshell",
121 "head": "head_global.js head_helpers.js head_http.js",
122 },
123 ),
124 (
125 "carrot/test_included.js",
126 {
127 "ancestor_manifest": "carrot/xpcshell-one.toml",
128 "manifest": "carrot/xpcshell-shared.toml",
129 "flavor": "xpcshell",
130 "stick": "one",
131 },
132 ),
133 (
134 "carrot/test_included.js",
135 {
136 "ancestor_manifest": "carrot/xpcshell-two.toml",
137 "manifest": "carrot/xpcshell-shared.toml",
138 "flavor": "xpcshell",
139 "stick": "two",
140 },
141 ),
142 (
143 "dragonfruit/elderberry/test_xpcshell_C.js",
144 {
145 "flavor": "xpcshell",
146 "generated-files": "head_update.js",
147 "head": "head_update.js",
148 "manifest": "dragonfruit/xpcshell.toml",
149 "reason": "busted",
150 "run-sequentially": "Launches application.",
151 "skip-if": "os == 'android'",
152 },
153 ),
154 (
155 "dragonfruit/elderberry/test_xpcshell_C.js",
156 {
157 "flavor": "xpcshell",
158 "generated-files": "head_update.js",
159 "head": "head_update.js head2.js",
160 "manifest": "dragonfruit/elderberry/xpcshell_updater.toml",
161 "reason": "don't work",
162 "run-sequentially": "Launches application.",
163 "skip-if": "os == 'android'",
164 },
165 ),
166 (
167 "fig/grape/src/TestInstrumentationA.java",
168 {
169 "flavor": "instrumentation",
170 "manifest": "fig/grape/instrumentation.toml",
171 "subsuite": "background",
172 },
173 ),
174 (
175 "fig/huckleberry/src/TestInstrumentationB.java",
176 {
177 "flavor": "instrumentation",
178 "manifest": "fig/huckleberry/instrumentation.toml",
179 "subsuite": "browser",
180 },
181 ),
182 (
183 "juniper/browser_chrome.js",
184 {
185 "flavor": "browser-chrome",
186 "manifest": "juniper/browser.toml",
187 "skip-if": "e10s # broken",
188 },
189 ),
190 (
191 "kiwi/browser_devtools.js",
192 {
193 "flavor": "browser-chrome",
194 "manifest": "kiwi/browser.toml",
195 "subsuite": "devtools",
196 "tags": "devtools",
197 },
198 ),
199 ]
200 )
201
202
203 @pytest.fixture(scope="module")
204 def defaults(topsrcdir):
205 def to_abspath(relpath):
206 # test-defaults.pkl uses absolute paths with platform-specific path separators.
207 # Use platform-specific separators if needed to avoid regressing on bug 1644223.
208 return os.path.normpath(os.path.join(topsrcdir, relpath))
209
210 def to_ancestor_manifest_path(relpath):
211 # ancestor_manifest uses relative paths with platform-specific path separators.
212 # This format must match the actual generation as defined in
213 # https://searchfox.org/mozilla-central/rev/1460e875c4189d975835af78fd06bd6daa9b553a/testing/mozbase/manifestparser/manifestparser/manifestparser.py#239-249
214 return os.path.normpath(relpath)
215
216 # This matches the format of "test-defaults.pkl", which is generated by
217 # testing/mozbase/manifestparser/manifestparser/manifestparser.py and
218 # test_test_defaults_metadata_with_include_and_ancestor_manifest in
219 # python/mozbuild/mozbuild/test/backend/test_test_manifest.py verifies that
220 # the format is as expected.
221 return {
222 (to_abspath("dragonfruit/elderberry/xpcshell_updater.toml")): {
223 "support-files": "data/**\nxpcshell_updater.toml"
224 },
225 (
226 to_ancestor_manifest_path("carrot/xpcshell-one.toml"),
227 to_abspath("carrot/xpcshell-shared.toml"),
228 ): {
229 "head": "head_one.js",
230 },
231 (
232 to_ancestor_manifest_path("carrot/xpcshell-two.toml"),
233 to_abspath("carrot/xpcshell-shared.toml"),
234 ): {
235 "head": "head_two.js",
236 },
237 }
238
239
240 class WPTManifestNamespace:
241 """Stand-in object for various WPT classes."""
242
243 def __init__(self, *args):
244 self.args = args
245
246 def __hash__(self):
247 return hash(str(self))
248
249 def __eq__(self, other):
250 return self.args == other.args
251
252 def __iter__(self):
253 yield tuple(self.args)
254
255
256 def fake_wpt_manifestupdate(topsrcdir, *args, **kwargs):
257 with open(os.path.join(topsrcdir, "wpt_manifest_data.json")) as fh:
258 data = json.load(fh)
259
260 items = {}
261 for tests_root, test_data in data.items():
262 kwargs = {"tests_path": os.path.join(topsrcdir, tests_root)}
263
264 for test_type, tests in test_data.items():
265 for test in tests:
266 obj = WPTManifestNamespace()
267 if "mozilla" in tests_root:
268 obj.id = "/_mozilla/" + test
269 else:
270 obj.id = "/" + test
271
272 items[WPTManifestNamespace(test_type, test, {obj})] = kwargs
273 return items
274
275
276 @pytest.fixture(params=[BuildBackendLoader, TestManifestLoader])
277 def resolver(request, tmpdir, monkeypatch, topsrcdir, all_tests, defaults):
278 topobjdir = tmpdir.mkdir("objdir").strpath
279 loader_cls = request.param
280
281 if loader_cls == BuildBackendLoader:
282 with open(os.path.join(topobjdir, "all-tests.pkl"), "wb") as fh:
283 pickle.dump(all_tests, fh)
284 with open(os.path.join(topobjdir, "test-defaults.pkl"), "wb") as fh:
285 pickle.dump(defaults, fh)
286
287 # The mock data already exists, so prevent BuildBackendLoader from regenerating
288 # the build information from the whole gecko tree...
289 class BuildBackendLoaderNeverOutOfDate(BuildBackendLoader):
290 def backend_out_of_date(self, backend_file):
291 return False
292
293 loader_cls = BuildBackendLoaderNeverOutOfDate
294
295 # Patch WPT's manifestupdate.run to return tests based on the contents of
296 # 'data/srcdir/wpt_manifest_data.json'.
297 monkeypatch.setattr(manifestupdate, "run", fake_wpt_manifestupdate)
298
299 resolver = TestResolver(
300 topsrcdir, None, None, topobjdir=topobjdir, loader_cls=loader_cls
301 )
302 resolver._puppeteer_loaded = True
303
304 if loader_cls == TestManifestLoader:
305 config = MockConfig(topsrcdir)
306 resolver.load_tests.reader = BuildReader(config)
307 return resolver
308
309
310 def test_load(resolver):
311 assert len(resolver.tests_by_path) == 9
312
313 assert len(resolver.tests_by_flavor["mochitest-plain"]) == 0
314 assert len(resolver.tests_by_flavor["xpcshell"]) == 4
315 assert len(resolver.tests_by_flavor["web-platform-tests"]) == 0
316
317 assert len(resolver.tests_by_manifest) == 9
318
319 resolver.add_wpt_manifest_data()
320 assert len(resolver.tests_by_path) == 11
321 assert len(resolver.tests_by_flavor["web-platform-tests"]) == 2
322 assert len(resolver.tests_by_manifest) == 11
323 assert "/html" in resolver.tests_by_manifest
324 assert "/_mozilla/html" in resolver.tests_by_manifest
325
326
327 def test_resolve_all(resolver):
328 assert len(list(resolver._resolve())) == 13
329
330
331 def test_resolve_filter_flavor(resolver):
332 assert len(list(resolver._resolve(flavor="xpcshell"))) == 6
333
334
335 def test_resolve_by_dir(resolver):
336 assert len(list(resolver._resolve(paths=["banana/currant"]))) == 2
337
338
339 def test_resolve_under_path(resolver):
340 assert len(list(resolver._resolve(under_path="banana"))) == 2
341 assert len(list(resolver._resolve(flavor="xpcshell", under_path="banana"))) == 2
342
343
344 def test_resolve_multiple_paths(resolver):
345 result = list(resolver.resolve_tests(paths=["banana", "dragonfruit"]))
346 assert len(result) == 4
347
348
349 def test_resolve_support_files(resolver):
350 expected_support_files = "data/**\nxpcshell_updater.toml"
351 tests = list(resolver.resolve_tests(paths=["dragonfruit"]))
352 assert len(tests) == 2
353
354 for test in tests:
355 if test["manifest"].endswith("xpcshell_updater.toml"):
356 assert test["support-files"] == expected_support_files
357 else:
358 assert "support-files" not in test
359
360
361 def test_resolve_path_prefix(resolver):
362 tests = list(resolver._resolve(paths=["juniper"]))
363 assert len(tests) == 1
364
365 # relative manifest
366 tests = list(resolver._resolve(paths=["apple/a11y.toml"]))
367 assert len(tests) == 1
368 assert tests[0]["name"] == "test_a11y.html"
369
370 # absolute manifest
371 tests = list(
372 resolver._resolve(paths=[os.path.join(resolver.topsrcdir, "apple/a11y.toml")])
373 )
374 assert len(tests) == 1
375 assert tests[0]["name"] == "test_a11y.html"
376
377
378 def test_cwd_children_only(resolver):
379 """If cwd is defined, only resolve tests under the specified cwd."""
380 # Pretend we're under '/services' and ask for 'common'. This should
381 # pick up all tests from '/services/common'
382 tests = list(
383 resolver.resolve_tests(
384 paths=["currant"], cwd=os.path.join(resolver.topsrcdir, "banana")
385 )
386 )
387
388 assert len(tests) == 2
389
390 # Tests should be rewritten to objdir.
391 for t in tests:
392 assert t["here"] == mozpath.join(
393 resolver.topobjdir, "_tests/xpcshell/banana/currant"
394 )
395
396
397 def test_various_cwd(resolver):
398 """Test various cwd conditions are all equal."""
399 expected = list(resolver.resolve_tests(paths=["banana"]))
400 actual = list(resolver.resolve_tests(paths=["banana"], cwd="/"))
401 assert actual == expected
402
403 actual = list(resolver.resolve_tests(paths=["banana"], cwd=resolver.topsrcdir))
404 assert actual == expected
405
406 actual = list(resolver.resolve_tests(paths=["banana"], cwd=resolver.topobjdir))
407 assert actual == expected
408
409
410 def test_subsuites(resolver):
411 """Test filtering by subsuite."""
412 tests = list(resolver.resolve_tests(paths=["fig"]))
413 assert len(tests) == 2
414
415 tests = list(resolver.resolve_tests(paths=["fig"], subsuite="browser"))
416 assert len(tests) == 1
417 assert tests[0]["name"] == "src/TestInstrumentationB.java"
418
419 tests = list(resolver.resolve_tests(paths=["fig"], subsuite="background"))
420 assert len(tests) == 1
421 assert tests[0]["name"] == "src/TestInstrumentationA.java"
422
423 # Resolve tests *without* a subsuite.
424 tests = list(resolver.resolve_tests(flavor="browser-chrome", subsuite="undefined"))
425 assert len(tests) == 1
426 assert tests[0]["name"] == "browser_chrome.js"
427
428
429 def test_wildcard_patterns(resolver):
430 """Test matching paths by wildcard."""
431 tests = list(resolver.resolve_tests(paths=["fig/**"]))
432 assert len(tests) == 2
433 for t in tests:
434 assert t["file_relpath"].startswith("fig")
435
436 tests = list(resolver.resolve_tests(paths=["**/**.js", "apple/**"]))
437 assert len(tests) == 9
438 for t in tests:
439 path = t["file_relpath"]
440 assert path.startswith("apple") or path.endswith(".js")
441
442
443 def test_resolve_metadata(resolver):
444 """Test finding metadata from outgoing files."""
445 suites, tests = resolver.resolve_metadata(["bc"])
446 assert suites == {"mochitest-browser-chrome"}
447 assert tests == []
448
449 suites, tests = resolver.resolve_metadata(
450 ["mochitest-a11y", "/browser", "xpcshell"]
451 )
452 assert suites == {"mochitest-a11y", "xpcshell"}
453 assert sorted(t["file_relpath"] for t in tests) == [
454 "juniper/browser_chrome.js",
455 "kiwi/browser_devtools.js",
456 ]
457
458
459 def test_ancestor_manifest_defaults(resolver, topsrcdir, defaults):
460 """Test that defaults from ancestor manifests are found."""
461 tests = list(resolver._resolve(paths=["carrot/test_included.js"]))
462 assert len(tests) == 2
463
464 if tests[0]["ancestor_manifest"] == os.path.join("carrot", "xpcshell-one.toml"):
465 [testOne, testTwo] = tests
466 else:
467 [testTwo, testOne] = tests
468
469 assert testOne["ancestor_manifest"] == os.path.join("carrot", "xpcshell-one.toml")
470 assert testOne["manifest_relpath"] == os.path.join("carrot", "xpcshell-shared.toml")
471 assert testOne["head"] == "head_one.js"
472 assert testOne["stick"] == "one"
473
474 assert testTwo["ancestor_manifest"] == os.path.join("carrot", "xpcshell-two.toml")
475 assert testTwo["manifest_relpath"] == os.path.join("carrot", "xpcshell-shared.toml")
476 assert testTwo["head"] == "head_two.js"
477 assert testTwo["stick"] == "two"
478
479
480 def test_task_regexes():
481 """Test the task_regexes defined in TEST_SUITES."""
482 task_labels = [
483 "test-linux64/opt-marionette",
484 "test-linux64/opt-mochitest-plain",
485 "test-linux64/debug-mochitest-plain-e10s",
486 "test-linux64/opt-mochitest-a11y",
487 "test-linux64/opt-mochitest-browser",
488 "test-linux64/opt-mochitest-browser-chrome",
489 "test-linux64/opt-mochitest-browser-chrome-e10s",
490 "test-linux64/opt-mochitest-browser-chrome-e10s-11",
491 "test-linux64/opt-mochitest-chrome",
492 "test-linux64/opt-mochitest-devtools",
493 "test-linux64/opt-mochitest-devtools-chrome",
494 "test-linux64/opt-mochitest-gpu",
495 "test-linux64/opt-mochitest-gpu-e10s",
496 "test-linux64/opt-mochitest-media-e10s-1",
497 "test-linux64/opt-mochitest-media-e10s-11",
498 "test-linux64/opt-mochitest-browser-screenshots-1",
499 "test-linux64/opt-mochitest-browser-screenshots-e10s-1",
500 "test-linux64/opt-reftest",
501 "test-linux64/opt-geckoview-reftest",
502 "test-linux64/debug-reftest-e10s-1",
503 "test-linux64/debug-reftest-e10s-11",
504 "test-linux64/opt-robocop",
505 "test-linux64/opt-robocop-1",
506 "test-linux64/opt-robocop-e10s",
507 "test-linux64/opt-robocop-e10s-1",
508 "test-linux64/opt-robocop-e10s-11",
509 "test-linux64/opt-web-platform-tests-e10s-1",
510 "test-linux64/opt-web-platform-tests-reftest-e10s-1",
511 "test-linux64/opt-web-platform-tests-wdspec-e10s-1",
512 "test-linux64/opt-web-platform-tests-1",
513 "test-linux64/opt-web-platform-test-e10s-1",
514 "test-linux64/opt-xpcshell",
515 "test-linux64/opt-xpcshell-1",
516 "test-linux64/opt-xpcshell-2",
517 ]
518
519 test_cases = {
520 "mochitest-browser-chrome": [
521 "test-linux64/opt-mochitest-browser-chrome",
522 "test-linux64/opt-mochitest-browser-chrome-e10s",
523 ],
524 "mochitest-chrome": [
525 "test-linux64/opt-mochitest-chrome",
526 ],
527 "mochitest-devtools-chrome": [
528 "test-linux64/opt-mochitest-devtools-chrome",
529 ],
530 "mochitest-media": [
531 "test-linux64/opt-mochitest-media-e10s-1",
532 ],
533 "mochitest-plain": [
534 "test-linux64/opt-mochitest-plain",
535 "test-linux64/debug-mochitest-plain-e10s",
536 ],
537 "mochitest-plain-gpu": [
538 "test-linux64/opt-mochitest-gpu",
539 "test-linux64/opt-mochitest-gpu-e10s",
540 ],
541 "mochitest-browser-screenshots": [
542 "test-linux64/opt-mochitest-browser-screenshots-1",
543 "test-linux64/opt-mochitest-browser-screenshots-e10s-1",
544 ],
545 "reftest": [
546 "test-linux64/opt-reftest",
547 "test-linux64/opt-geckoview-reftest",
548 "test-linux64/debug-reftest-e10s-1",
549 ],
550 "robocop": [
551 "test-linux64/opt-robocop",
552 "test-linux64/opt-robocop-1",
553 "test-linux64/opt-robocop-e10s",
554 "test-linux64/opt-robocop-e10s-1",
555 ],
556 "web-platform-tests": [
557 "test-linux64/opt-web-platform-tests-e10s-1",
558 "test-linux64/opt-web-platform-tests-1",
559 ],
560 "web-platform-tests-reftest": [
561 "test-linux64/opt-web-platform-tests-reftest-e10s-1",
562 ],
563 "web-platform-tests-wdspec": [
564 "test-linux64/opt-web-platform-tests-wdspec-e10s-1",
565 ],
566 "xpcshell": [
567 "test-linux64/opt-xpcshell",
568 "test-linux64/opt-xpcshell-1",
569 ],
570 }
571
572 regexes = []
573
574 def match_task(task):
575 return any(re.search(pattern, task) for pattern in regexes)
576
577 for suite, expected in sorted(test_cases.items()):
578 print(suite)
579 regexes = TEST_SUITES[suite]["task_regex"]
580 assert set(filter(match_task, task_labels)) == set(expected)
581
582
583 if __name__ == "__main__":
584 mozunit.main()