try_option_syntax.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. from __future__ import absolute_import, print_function, unicode_literals
  5. import argparse
  6. import copy
  7. import logging
  8. import re
  9. import shlex
  10. logger = logging.getLogger(__name__)
  11. TRY_DELIMITER = 'try:'
  12. # The build type aliases are very cryptic and only used in try flags these are
  13. # mappings from the single char alias to a longer more recognizable form.
  14. BUILD_TYPE_ALIASES = {
  15. 'o': 'opt',
  16. 'd': 'debug'
  17. }
  18. # consider anything in this whitelist of kinds to be governed by -b/-p
  19. BUILD_KINDS = set([
  20. 'build',
  21. 'artifact-build',
  22. 'hazard',
  23. 'l10n',
  24. 'upload-symbols',
  25. 'valgrind',
  26. 'static-analysis',
  27. 'spidermonkey',
  28. ])
  29. # anything in this list is governed by -j
  30. JOB_KINDS = set([
  31. 'source-check',
  32. 'toolchain',
  33. 'marionette-harness',
  34. 'android-stuff',
  35. ])
  36. # mapping from shortcut name (usable with -u) to a boolean function identifying
  37. # matching test names
  38. def alias_prefix(prefix):
  39. return lambda name: name.startswith(prefix)
  40. def alias_contains(infix):
  41. return lambda name: infix in name
  42. def alias_matches(pattern):
  43. pattern = re.compile(pattern)
  44. return lambda name: pattern.match(name)
  45. UNITTEST_ALIASES = {
  46. # Aliases specify shorthands that can be used in try syntax. The shorthand
  47. # is the dictionary key, with the value representing a pattern for matching
  48. # unittest_try_names.
  49. #
  50. # Note that alias expansion is performed in the absence of any chunk
  51. # prefixes. For example, the first example above would replace "foo-7"
  52. # with "foobar-7". Note that a few aliases allowed chunks to be specified
  53. # without a leading `-`, for example 'mochitest-dt1'. That's no longer
  54. # supported.
  55. 'cppunit': alias_prefix('cppunit'),
  56. 'crashtest': alias_prefix('crashtest'),
  57. 'crashtest-e10s': alias_prefix('crashtest-e10s'),
  58. 'e10s': alias_contains('e10s'),
  59. 'external-media-tests': alias_prefix('external-media-tests'),
  60. 'firefox-ui-functional': alias_prefix('firefox-ui-functional'),
  61. 'firefox-ui-functional-e10s': alias_prefix('firefox-ui-functional-e10s'),
  62. 'gaia-js-integration': alias_contains('gaia-js-integration'),
  63. 'gtest': alias_prefix('gtest'),
  64. 'jittest': alias_prefix('jittest'),
  65. 'jittests': alias_prefix('jittest'),
  66. 'jsreftest': alias_prefix('jsreftest'),
  67. 'jsreftest-e10s': alias_prefix('jsreftest-e10s'),
  68. 'marionette': alias_prefix('marionette'),
  69. 'marionette-e10s': alias_prefix('marionette-e10s'),
  70. 'mochitest': alias_prefix('mochitest'),
  71. 'mochitests': alias_prefix('mochitest'),
  72. 'mochitest-e10s': alias_prefix('mochitest-e10s'),
  73. 'mochitests-e10s': alias_prefix('mochitest-e10s'),
  74. 'mochitest-debug': alias_prefix('mochitest-debug-'),
  75. 'mochitest-a11y': alias_contains('mochitest-a11y'),
  76. 'mochitest-bc': alias_prefix('mochitest-browser-chrome'),
  77. 'mochitest-e10s-bc': alias_prefix('mochitest-e10s-browser-chrome'),
  78. 'mochitest-browser-chrome': alias_prefix('mochitest-browser-chrome'),
  79. 'mochitest-e10s-browser-chrome': alias_prefix('mochitest-e10s-browser-chrome'),
  80. 'mochitest-chrome': alias_contains('mochitest-chrome'),
  81. 'mochitest-dt': alias_prefix('mochitest-devtools-chrome'),
  82. 'mochitest-e10s-dt': alias_prefix('mochitest-e10s-devtools-chrome'),
  83. 'mochitest-gl': alias_prefix('mochitest-webgl'),
  84. 'mochitest-gl-e10s': alias_prefix('mochitest-webgl-e10s'),
  85. 'mochitest-gpu': alias_prefix('mochitest-gpu'),
  86. 'mochitest-gpu-e10s': alias_prefix('mochitest-gpu-e10s'),
  87. 'mochitest-clipboard': alias_prefix('mochitest-clipboard'),
  88. 'mochitest-clipboard-e10s': alias_prefix('mochitest-clipboard-e10s'),
  89. 'mochitest-jetpack': alias_prefix('mochitest-jetpack'),
  90. 'mochitest-media': alias_prefix('mochitest-media'),
  91. 'mochitest-media-e10s': alias_prefix('mochitest-media-e10s'),
  92. 'mochitest-vg': alias_prefix('mochitest-valgrind'),
  93. 'reftest': alias_matches(r'^(plain-)?reftest.*$'),
  94. 'reftest-no-accel': alias_matches(r'^(plain-)?reftest-no-accel.*$'),
  95. 'reftests': alias_matches(r'^(plain-)?reftest.*$'),
  96. 'reftests-e10s': alias_matches(r'^(plain-)?reftest-e10s.*$'),
  97. 'robocop': alias_prefix('robocop'),
  98. 'web-platform-test': alias_prefix('web-platform-tests'),
  99. 'web-platform-tests': alias_prefix('web-platform-tests'),
  100. 'web-platform-tests-e10s': alias_prefix('web-platform-tests-e10s'),
  101. 'web-platform-tests-reftests': alias_prefix('web-platform-tests-reftests'),
  102. 'web-platform-tests-reftests-e10s': alias_prefix('web-platform-tests-reftests-e10s'),
  103. 'xpcshell': alias_prefix('xpcshell'),
  104. }
  105. # unittest platforms can be specified by substring of the "pretty name", which
  106. # is basically the old Buildbot builder name. This dict has {pretty name,
  107. # [test_platforms]} translations, This includes only the most commonly-used
  108. # substrings. This is intended only for backward-compatibility. New test
  109. # platforms should have their `test_platform` spelled out fully in try syntax.
  110. UNITTEST_PLATFORM_PRETTY_NAMES = {
  111. 'Ubuntu': ['linux', 'linux64', 'linux64-asan'],
  112. 'x64': ['linux64', 'linux64-asan'],
  113. 'Android 4.3': ['android-4.3-arm7-api-15'],
  114. # other commonly-used substrings for platforms not yet supported with
  115. # in-tree taskgraphs:
  116. # '10.10': [..TODO..],
  117. # '10.10.5': [..TODO..],
  118. # '10.6': [..TODO..],
  119. # '10.8': [..TODO..],
  120. # 'Android 2.3 API9': [..TODO..],
  121. # 'Windows 7': [..TODO..],
  122. # 'Windows 7 VM': [..TODO..],
  123. # 'Windows 8': [..TODO..],
  124. # 'Windows XP': [..TODO..],
  125. # 'win32': [..TODO..],
  126. # 'win64': [..TODO..],
  127. }
  128. # We have a few platforms for which we want to do some "extra" builds, or at
  129. # least build-ish things. Sort of. Anyway, these other things are implemented
  130. # as different "platforms". These do *not* automatically ride along with "-p
  131. # all"
  132. RIDEALONG_BUILDS = {
  133. 'android-api-15': [
  134. 'android-api-15-l10n',
  135. ],
  136. 'linux': [
  137. 'linux-l10n',
  138. ],
  139. 'linux64': [
  140. 'linux64-l10n',
  141. 'sm-plain',
  142. 'sm-nonunified',
  143. 'sm-arm-sim',
  144. 'sm-arm64-sim',
  145. 'sm-compacting',
  146. 'sm-rootanalysis',
  147. 'sm-package',
  148. 'sm-tsan',
  149. 'sm-asan',
  150. 'sm-mozjs-sys',
  151. 'sm-msan',
  152. ],
  153. }
  154. TEST_CHUNK_SUFFIX = re.compile('(.*)-([0-9]+)$')
  155. class TryOptionSyntax(object):
  156. def __init__(self, message, full_task_graph):
  157. """
  158. Parse a "try syntax" formatted commit message. This is the old "-b do -p
  159. win32 -u all" format. Aliases are applied to map short names to full
  160. names.
  161. The resulting object has attributes:
  162. - build_types: a list containing zero or more of 'opt' and 'debug'
  163. - platforms: a list of selected platform names, or None for all
  164. - unittests: a list of tests, of the form given below, or None for all
  165. - jobs: a list of requested job names, or None for all
  166. - trigger_tests: the number of times tests should be triggered (--rebuild)
  167. - interactive: true if --interactive
  168. - notifications: either None if no notifications or one of 'all' or 'failure'
  169. Note that -t is currently completely ignored.
  170. The unittests and talos lists contain dictionaries of the form:
  171. {
  172. 'test': '<suite name>',
  173. 'platforms': [..platform names..], # to limit to only certain platforms
  174. 'only_chunks': set([..chunk numbers..]), # to limit only to certain chunks
  175. }
  176. """
  177. self.jobs = []
  178. self.build_types = []
  179. self.platforms = []
  180. self.unittests = []
  181. self.talos = []
  182. self.trigger_tests = 0
  183. self.interactive = False
  184. self.notifications = None
  185. # shlex used to ensure we split correctly when giving values to argparse.
  186. parts = shlex.split(self.escape_whitespace_in_brackets(message))
  187. try_idx = None
  188. for idx, part in enumerate(parts):
  189. if part == TRY_DELIMITER:
  190. try_idx = idx
  191. break
  192. if try_idx is None:
  193. return
  194. # Argument parser based on try flag flags
  195. parser = argparse.ArgumentParser()
  196. parser.add_argument('-b', '--build', dest='build_types')
  197. parser.add_argument('-p', '--platform', nargs='?',
  198. dest='platforms', const='all', default='all')
  199. parser.add_argument('-u', '--unittests', nargs='?',
  200. dest='unittests', const='all', default='all')
  201. parser.add_argument('-t', '--talos', nargs='?', dest='talos', const='all', default='all')
  202. parser.add_argument('-i', '--interactive',
  203. dest='interactive', action='store_true', default=False)
  204. parser.add_argument('-e', '--all-emails',
  205. dest='notifications', action='store_const', const='all')
  206. parser.add_argument('-f', '--failure-emails',
  207. dest='notifications', action='store_const', const='failure')
  208. parser.add_argument('-j', '--job', dest='jobs', action='append')
  209. # In order to run test jobs multiple times
  210. parser.add_argument('--rebuild', dest='trigger_tests', type=int, default=1)
  211. args, _ = parser.parse_known_args(parts[try_idx:])
  212. self.jobs = self.parse_jobs(args.jobs)
  213. self.build_types = self.parse_build_types(args.build_types)
  214. self.platforms = self.parse_platforms(args.platforms)
  215. self.unittests = self.parse_test_option(
  216. "unittest_try_name", args.unittests, full_task_graph)
  217. self.talos = self.parse_test_option("talos_try_name", args.talos, full_task_graph)
  218. self.trigger_tests = args.trigger_tests
  219. self.interactive = args.interactive
  220. self.notifications = args.notifications
  221. def parse_jobs(self, jobs_arg):
  222. if not jobs_arg or jobs_arg == ['all']:
  223. return None
  224. expanded = []
  225. for job in jobs_arg:
  226. expanded.extend(j.strip() for j in job.split(','))
  227. return expanded
  228. def parse_build_types(self, build_types_arg):
  229. if build_types_arg is None:
  230. build_types_arg = []
  231. build_types = filter(None, [BUILD_TYPE_ALIASES.get(build_type) for
  232. build_type in build_types_arg])
  233. return build_types
  234. def parse_platforms(self, platform_arg):
  235. if platform_arg == 'all':
  236. return None
  237. results = []
  238. for build in platform_arg.split(','):
  239. results.append(build)
  240. if build in RIDEALONG_BUILDS:
  241. results.extend(RIDEALONG_BUILDS[build])
  242. logger.info("platform %s triggers ridealong builds %s" %
  243. (build, ', '.join(RIDEALONG_BUILDS[build])))
  244. return results
  245. def parse_test_option(self, attr_name, test_arg, full_task_graph):
  246. '''
  247. Parse a unittest (-u) or talos (-t) option, in the context of a full
  248. task graph containing available `unittest_try_name` or `talos_try_name`
  249. attributes. There are three cases:
  250. - test_arg is == 'none' (meaning an empty list)
  251. - test_arg is == 'all' (meaning use the list of jobs for that job type)
  252. - test_arg is comma string which needs to be parsed
  253. '''
  254. # Empty job list case...
  255. if test_arg is None or test_arg == 'none':
  256. return []
  257. all_platforms = set(t.attributes['test_platform']
  258. for t in full_task_graph.tasks.itervalues()
  259. if 'test_platform' in t.attributes)
  260. tests = self.parse_test_opts(test_arg, all_platforms)
  261. if not tests:
  262. return []
  263. all_tests = set(t.attributes[attr_name]
  264. for t in full_task_graph.tasks.itervalues()
  265. if attr_name in t.attributes)
  266. # Special case where tests is 'all' and must be expanded
  267. if tests[0]['test'] == 'all':
  268. results = []
  269. all_entry = tests[0]
  270. for test in all_tests:
  271. entry = {'test': test}
  272. # If there are platform restrictions copy them across the list.
  273. if 'platforms' in all_entry:
  274. entry['platforms'] = list(all_entry['platforms'])
  275. results.append(entry)
  276. return self.parse_test_chunks(all_tests, results)
  277. else:
  278. return self.parse_test_chunks(all_tests, tests)
  279. def parse_test_opts(self, input_str, all_platforms):
  280. '''
  281. Parse `testspec,testspec,..`, where each testspec is a test name
  282. optionally followed by a list of test platforms or negated platforms in
  283. `[]`.
  284. No brackets indicates that tests should run on all platforms for which
  285. builds are available. If testspecs are provided, then each is treated,
  286. from left to right, as an instruction to include or (if negated)
  287. exclude a set of test platforms. A single spec may expand to multiple
  288. test platforms via UNITTEST_PLATFORM_PRETTY_NAMES. If the first test
  289. spec is negated, processing begins with the full set of available test
  290. platforms; otherwise, processing begins with an empty set of test
  291. platforms.
  292. '''
  293. # Final results which we will return.
  294. tests = []
  295. cur_test = {}
  296. token = ''
  297. in_platforms = False
  298. def normalize_platforms():
  299. if 'platforms' not in cur_test:
  300. return
  301. # if the first spec is a negation, start with all platforms
  302. if cur_test['platforms'][0][0] == '-':
  303. platforms = all_platforms.copy()
  304. else:
  305. platforms = []
  306. for platform in cur_test['platforms']:
  307. if platform[0] == '-':
  308. platforms = [p for p in platforms if p != platform[1:]]
  309. else:
  310. platforms.append(platform)
  311. cur_test['platforms'] = platforms
  312. def add_test(value):
  313. normalize_platforms()
  314. cur_test['test'] = value.strip()
  315. tests.insert(0, cur_test)
  316. def add_platform(value):
  317. platform = value.strip()
  318. if platform[0] == '-':
  319. negated = True
  320. platform = platform[1:]
  321. else:
  322. negated = False
  323. platforms = UNITTEST_PLATFORM_PRETTY_NAMES.get(platform, [platform])
  324. if negated:
  325. platforms = ["-" + p for p in platforms]
  326. cur_test['platforms'] = platforms + cur_test.get('platforms', [])
  327. # This might be somewhat confusing but we parse the string _backwards_ so
  328. # there is no ambiguity over what state we are in.
  329. for char in reversed(input_str):
  330. # , indicates exiting a state
  331. if char == ',':
  332. # Exit a particular platform.
  333. if in_platforms:
  334. add_platform(token)
  335. # Exit a particular test.
  336. else:
  337. add_test(token)
  338. cur_test = {}
  339. # Token must always be reset after we exit a state
  340. token = ''
  341. elif char == '[':
  342. # Exiting platform state entering test state.
  343. add_platform(token)
  344. token = ''
  345. in_platforms = False
  346. elif char == ']':
  347. # Entering platform state.
  348. in_platforms = True
  349. else:
  350. # Accumulator.
  351. token = char + token
  352. # Handle any left over tokens.
  353. if token:
  354. add_test(token)
  355. return tests
  356. def handle_alias(self, test, all_tests):
  357. '''
  358. Expand a test if its name refers to an alias, returning a list of test
  359. dictionaries cloned from the first (to maintain any metadata).
  360. '''
  361. if test['test'] not in UNITTEST_ALIASES:
  362. return [test]
  363. alias = UNITTEST_ALIASES[test['test']]
  364. def mktest(name):
  365. newtest = copy.deepcopy(test)
  366. newtest['test'] = name
  367. return newtest
  368. def exprmatch(alias):
  369. return [t for t in all_tests if alias(t)]
  370. return [mktest(t) for t in exprmatch(alias)]
  371. def parse_test_chunks(self, all_tests, tests):
  372. '''
  373. Test flags may include parameters to narrow down the number of chunks in a
  374. given push. We don't model 1 chunk = 1 job in taskcluster so we must check
  375. each test flag to see if it is actually specifying a chunk.
  376. '''
  377. results = []
  378. seen_chunks = {}
  379. for test in tests:
  380. matches = TEST_CHUNK_SUFFIX.match(test['test'])
  381. if matches:
  382. name = matches.group(1)
  383. chunk = matches.group(2)
  384. if name in seen_chunks:
  385. seen_chunks[name].add(chunk)
  386. else:
  387. seen_chunks[name] = {chunk}
  388. test['test'] = name
  389. test['only_chunks'] = seen_chunks[name]
  390. results.append(test)
  391. else:
  392. results.extend(self.handle_alias(test, all_tests))
  393. # uniquify the results over the test names
  394. results = {test['test']: test for test in results}.values()
  395. return results
  396. def find_all_attribute_suffixes(self, graph, prefix):
  397. rv = set()
  398. for t in graph.tasks.itervalues():
  399. for a in t.attributes:
  400. if a.startswith(prefix):
  401. rv.add(a[len(prefix):])
  402. return sorted(rv)
  403. def escape_whitespace_in_brackets(self, input_str):
  404. '''
  405. In tests you may restrict them by platform [] inside of the brackets
  406. whitespace may occur this is typically invalid shell syntax so we escape it
  407. with backslash sequences .
  408. '''
  409. result = ""
  410. in_brackets = False
  411. for char in input_str:
  412. if char == '[':
  413. in_brackets = True
  414. result += char
  415. continue
  416. if char == ']':
  417. in_brackets = False
  418. result += char
  419. continue
  420. if char == ' ' and in_brackets:
  421. result += '\ '
  422. continue
  423. result += char
  424. return result
  425. def task_matches(self, attributes):
  426. attr = attributes.get
  427. def check_run_on_projects():
  428. return set(['try', 'all']) & set(attr('run_on_projects', []))
  429. def match_test(try_spec, attr_name):
  430. if attr('build_type') not in self.build_types:
  431. return False
  432. if self.platforms is not None:
  433. if attr('build_platform') not in self.platforms:
  434. return False
  435. else:
  436. if not check_run_on_projects():
  437. return False
  438. if try_spec is None:
  439. return True
  440. # TODO: optimize this search a bit
  441. for test in try_spec:
  442. if attr(attr_name) == test['test']:
  443. break
  444. else:
  445. return False
  446. if 'platforms' in test and attr('test_platform') not in test['platforms']:
  447. return False
  448. if 'only_chunks' in test and attr('test_chunk') not in test['only_chunks']:
  449. return False
  450. return True
  451. if attr('kind') in ('desktop-test', 'android-test'):
  452. return match_test(self.unittests, 'unittest_try_name')
  453. elif attr('kind') in JOB_KINDS:
  454. if self.jobs is None:
  455. return True
  456. if attr('build_platform') in self.jobs:
  457. return True
  458. elif attr('kind') in BUILD_KINDS:
  459. if attr('build_type') not in self.build_types:
  460. return False
  461. elif self.platforms is None:
  462. # for "-p all", look for try in the 'run_on_projects' attribute
  463. return check_run_on_projects()
  464. else:
  465. if attr('build_platform') not in self.platforms:
  466. return False
  467. return True
  468. else:
  469. return False
  470. def __str__(self):
  471. def none_for_all(list):
  472. if list is None:
  473. return '<all>'
  474. return ', '.join(str(e) for e in list)
  475. return "\n".join([
  476. "build_types: " + ", ".join(self.build_types),
  477. "platforms: " + none_for_all(self.platforms),
  478. "unittests: " + none_for_all(self.unittests),
  479. "jobs: " + none_for_all(self.jobs),
  480. "trigger_tests: " + str(self.trigger_tests),
  481. "interactive: " + str(self.interactive),
  482. "notifications: " + self.notifications,
  483. ])