validate_failures.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #!/usr/bin/env python3
  2. # Script to compare testsuite failures against a list of known-to-fail
  3. # tests.
  4. # Contributed by Diego Novillo <dnovillo@google.com>
  5. #
  6. # Copyright (C) 2011-2013 Free Software Foundation, Inc.
  7. #
  8. # This file is part of GCC.
  9. #
  10. # GCC is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 3, or (at your option)
  13. # any later version.
  14. #
  15. # GCC is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with GCC; see the file COPYING. If not, write to
  22. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  23. # Boston, MA 02110-1301, USA.
  24. """This script provides a coarser XFAILing mechanism that requires no
  25. detailed DejaGNU markings. This is useful in a variety of scenarios:
  26. - Development branches with many known failures waiting to be fixed.
  27. - Release branches with known failures that are not considered
  28. important for the particular release criteria used in that branch.
  29. The script must be executed from the toplevel build directory. When
  30. executed it will:
  31. 1- Determine the target built: TARGET
  32. 2- Determine the source directory: SRCDIR
  33. 3- Look for a failure manifest file in
  34. <SRCDIR>/<MANIFEST_SUBDIR>/<MANIFEST_NAME>.xfail
  35. 4- Collect all the <tool>.sum files from the build tree.
  36. 5- Produce a report stating:
  37. a- Failures expected in the manifest but not present in the build.
  38. b- Failures in the build not expected in the manifest.
  39. 6- If all the build failures are expected in the manifest, it exits
  40. with exit code 0. Otherwise, it exits with error code 1.
  41. Manifest files contain expected DejaGNU results that are otherwise
  42. treated as failures.
  43. They may also contain additional text:
  44. # This is a comment. - self explanatory
  45. @include file - the file is a path relative to the includer
  46. @remove result text - result text is removed from the expected set
  47. """
  48. import datetime
  49. import optparse
  50. import os
  51. import re
  52. import sys
  53. # Handled test results.
  54. _VALID_TEST_RESULTS = [ 'FAIL', 'UNRESOLVED', 'XPASS', 'ERROR' ]
  55. _VALID_TEST_RESULTS_REX = re.compile("%s" % "|".join(_VALID_TEST_RESULTS))
  56. # Subdirectory of srcdir in which to find the manifest file.
  57. _MANIFEST_SUBDIR = 'contrib/testsuite-management'
  58. # Pattern for naming manifest files.
  59. # The first argument should be the toplevel GCC(/GNU tool) source directory.
  60. # The second argument is the manifest subdir.
  61. # The third argument is the manifest target, which defaults to the target
  62. # triplet used during the build.
  63. _MANIFEST_PATH_PATTERN = '%s/%s/%s.xfail'
  64. # The options passed to the program.
  65. _OPTIONS = None
  66. def Error(msg):
  67. print('error: %s' % msg, file=sys.stderr)
  68. sys.exit(1)
  69. class TestResult(object):
  70. """Describes a single DejaGNU test result as emitted in .sum files.
  71. We are only interested in representing unsuccessful tests. So, only
  72. a subset of all the tests are loaded.
  73. The summary line used to build the test result should have this format:
  74. attrlist | XPASS: gcc.dg/unroll_1.c (test for excess errors)
  75. ^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
  76. optional state name description
  77. attributes
  78. Attributes:
  79. attrlist: A comma separated list of attributes.
  80. Valid values:
  81. flaky Indicates that this test may not always fail. These
  82. tests are reported, but their presence does not affect
  83. the results.
  84. expire=YYYYMMDD After this date, this test will produce an error
  85. whether it is in the manifest or not.
  86. state: One of UNRESOLVED, XPASS or FAIL.
  87. name: File name for the test.
  88. description: String describing the test (flags used, dejagnu message, etc)
  89. ordinal: Monotonically increasing integer.
  90. It is used to keep results for one .exp file sorted
  91. by the order the tests were run.
  92. """
  93. def __init__(self, summary_line, ordinal=-1):
  94. try:
  95. (self.attrs, summary_line) = SplitAttributesFromSummaryLine(summary_line)
  96. try:
  97. (self.state,
  98. self.name,
  99. self.description) = re.match(r'([A-Z]+):\s*(\S+)\s*(.*)',
  100. summary_line).groups()
  101. except:
  102. print('Failed to parse summary line: "%s"' % summary_line)
  103. raise
  104. self.ordinal = ordinal
  105. except ValueError:
  106. Error('Cannot parse summary line "%s"' % summary_line)
  107. if self.state not in _VALID_TEST_RESULTS:
  108. Error('Invalid test result %s in "%s" (parsed as "%s")' % (
  109. self.state, summary_line, self))
  110. def __lt__(self, other):
  111. return (self.name < other.name or
  112. (self.name == other.name and self.ordinal < other.ordinal))
  113. def __hash__(self):
  114. return hash(self.state) ^ hash(self.name) ^ hash(self.description)
  115. def __eq__(self, other):
  116. return (self.state == other.state and
  117. self.name == other.name and
  118. self.description == other.description)
  119. def __ne__(self, other):
  120. return not (self == other)
  121. def __str__(self):
  122. attrs = ''
  123. if self.attrs:
  124. attrs = '%s | ' % self.attrs
  125. return '%s%s: %s %s' % (attrs, self.state, self.name, self.description)
  126. def ExpirationDate(self):
  127. # Return a datetime.date object with the expiration date for this
  128. # test result. Return None, if no expiration has been set.
  129. if re.search(r'expire=', self.attrs):
  130. expiration = re.search(r'expire=(\d\d\d\d)(\d\d)(\d\d)', self.attrs)
  131. if not expiration:
  132. Error('Invalid expire= format in "%s". Must be of the form '
  133. '"expire=YYYYMMDD"' % self)
  134. return datetime.date(int(expiration.group(1)),
  135. int(expiration.group(2)),
  136. int(expiration.group(3)))
  137. return None
  138. def HasExpired(self):
  139. # Return True if the expiration date of this result has passed.
  140. expiration_date = self.ExpirationDate()
  141. if expiration_date:
  142. now = datetime.date.today()
  143. return now > expiration_date
  144. def GetMakefileValue(makefile_name, value_name):
  145. if os.path.exists(makefile_name):
  146. makefile = open(makefile_name, encoding='latin-1', mode='r')
  147. for line in makefile:
  148. if line.startswith(value_name):
  149. (_, value) = line.split('=', 1)
  150. value = value.strip()
  151. makefile.close()
  152. return value
  153. makefile.close()
  154. return None
  155. def ValidBuildDirectory(builddir):
  156. if (not os.path.exists(builddir) or
  157. not os.path.exists('%s/Makefile' % builddir)):
  158. return False
  159. return True
  160. def IsComment(line):
  161. """Return True if line is a comment."""
  162. return line.startswith('#')
  163. def SplitAttributesFromSummaryLine(line):
  164. """Splits off attributes from a summary line, if present."""
  165. if '|' in line and not _VALID_TEST_RESULTS_REX.match(line):
  166. (attrs, line) = line.split('|', 1)
  167. attrs = attrs.strip()
  168. else:
  169. attrs = ''
  170. line = line.strip()
  171. return (attrs, line)
  172. def IsInterestingResult(line):
  173. """Return True if line is one of the summary lines we care about."""
  174. (_, line) = SplitAttributesFromSummaryLine(line)
  175. return bool(_VALID_TEST_RESULTS_REX.match(line))
  176. def IsInclude(line):
  177. """Return True if line is an include of another file."""
  178. return line.startswith("@include ")
  179. def GetIncludeFile(line, includer):
  180. """Extract the name of the include file from line."""
  181. includer_dir = os.path.dirname(includer)
  182. include_file = line[len("@include "):]
  183. return os.path.join(includer_dir, include_file.strip())
  184. def IsNegativeResult(line):
  185. """Return True if line should be removed from the expected results."""
  186. return line.startswith("@remove ")
  187. def GetNegativeResult(line):
  188. """Extract the name of the negative result from line."""
  189. line = line[len("@remove "):]
  190. return line.strip()
  191. def ParseManifestWorker(result_set, manifest_path):
  192. """Read manifest_path, adding the contents to result_set."""
  193. if _OPTIONS.verbosity >= 1:
  194. print('Parsing manifest file %s.' % manifest_path)
  195. manifest_file = open(manifest_path, encoding='latin-1', mode='r')
  196. for line in manifest_file:
  197. line = line.strip()
  198. if line == "":
  199. pass
  200. elif IsComment(line):
  201. pass
  202. elif IsNegativeResult(line):
  203. result_set.remove(TestResult(GetNegativeResult(line)))
  204. elif IsInclude(line):
  205. ParseManifestWorker(result_set, GetIncludeFile(line, manifest_path))
  206. elif IsInterestingResult(line):
  207. result_set.add(TestResult(line))
  208. else:
  209. Error('Unrecognized line in manifest file: %s' % line)
  210. manifest_file.close()
  211. def ParseManifest(manifest_path):
  212. """Create a set of TestResult instances from the given manifest file."""
  213. result_set = set()
  214. ParseManifestWorker(result_set, manifest_path)
  215. return result_set
  216. def ParseSummary(sum_fname):
  217. """Create a set of TestResult instances from the given summary file."""
  218. result_set = set()
  219. # ordinal is used when sorting the results so that tests within each
  220. # .exp file are kept sorted.
  221. ordinal=0
  222. sum_file = open(sum_fname, encoding='latin-1', mode='r')
  223. for line in sum_file:
  224. if IsInterestingResult(line):
  225. result = TestResult(line, ordinal)
  226. ordinal += 1
  227. if result.HasExpired():
  228. # Tests that have expired are not added to the set of expected
  229. # results. If they are still present in the set of actual results,
  230. # they will cause an error to be reported.
  231. print('WARNING: Expected failure "%s" has expired.' % line.strip())
  232. continue
  233. result_set.add(result)
  234. sum_file.close()
  235. return result_set
  236. def GetManifest(manifest_path):
  237. """Build a set of expected failures from the manifest file.
  238. Each entry in the manifest file should have the format understood
  239. by the TestResult constructor.
  240. If no manifest file exists for this target, it returns an empty set.
  241. """
  242. if os.path.exists(manifest_path):
  243. return ParseManifest(manifest_path)
  244. else:
  245. return set()
  246. def CollectSumFiles(builddir):
  247. sum_files = []
  248. for root, dirs, files in os.walk(builddir):
  249. for ignored in ('.svn', '.git'):
  250. if ignored in dirs:
  251. dirs.remove(ignored)
  252. for fname in files:
  253. if fname.endswith('.sum'):
  254. sum_files.append(os.path.join(root, fname))
  255. return sum_files
  256. def GetResults(sum_files):
  257. """Collect all the test results from the given .sum files."""
  258. build_results = set()
  259. for sum_fname in sum_files:
  260. print('\t%s' % sum_fname)
  261. build_results |= ParseSummary(sum_fname)
  262. return build_results
  263. def CompareResults(manifest, actual):
  264. """Compare sets of results and return two lists:
  265. - List of results present in ACTUAL but missing from MANIFEST.
  266. - List of results present in MANIFEST but missing from ACTUAL.
  267. """
  268. # Collect all the actual results not present in the manifest.
  269. # Results in this set will be reported as errors.
  270. actual_vs_manifest = set()
  271. for actual_result in actual:
  272. if actual_result not in manifest:
  273. actual_vs_manifest.add(actual_result)
  274. # Collect all the tests in the manifest that were not found
  275. # in the actual results.
  276. # Results in this set will be reported as warnings (since
  277. # they are expected failures that are not failing anymore).
  278. manifest_vs_actual = set()
  279. for expected_result in manifest:
  280. # Ignore tests marked flaky.
  281. if 'flaky' in expected_result.attrs:
  282. continue
  283. if expected_result not in actual:
  284. manifest_vs_actual.add(expected_result)
  285. return actual_vs_manifest, manifest_vs_actual
  286. def GetManifestPath(srcdir, target, user_provided_must_exist):
  287. """Return the full path to the manifest file."""
  288. manifest_path = _OPTIONS.manifest
  289. if manifest_path:
  290. if user_provided_must_exist and not os.path.exists(manifest_path):
  291. Error('Manifest does not exist: %s' % manifest_path)
  292. return manifest_path
  293. else:
  294. if not srcdir:
  295. Error('Could not determine the location of GCC\'s source tree. '
  296. 'The Makefile does not contain a definition for "srcdir".')
  297. if not target:
  298. Error('Could not determine the target triplet for this build. '
  299. 'The Makefile does not contain a definition for "target_alias".')
  300. return _MANIFEST_PATH_PATTERN % (srcdir, _MANIFEST_SUBDIR, target)
  301. def GetBuildData():
  302. if not ValidBuildDirectory(_OPTIONS.build_dir):
  303. # If we have been given a set of results to use, we may
  304. # not be inside a valid GCC build directory. In that case,
  305. # the user must provide both a manifest file and a set
  306. # of results to check against it.
  307. if not _OPTIONS.results or not _OPTIONS.manifest:
  308. Error('%s is not a valid GCC top level build directory. '
  309. 'You must use --manifest and --results to do the validation.' %
  310. _OPTIONS.build_dir)
  311. else:
  312. return None, None
  313. srcdir = GetMakefileValue('%s/Makefile' % _OPTIONS.build_dir, 'srcdir =')
  314. target = GetMakefileValue('%s/Makefile' % _OPTIONS.build_dir, 'target_alias=')
  315. print('Source directory: %s' % srcdir)
  316. print('Build target: %s' % target)
  317. return srcdir, target
  318. def PrintSummary(msg, summary):
  319. print('\n\n%s' % msg)
  320. for result in sorted(summary):
  321. print(result)
  322. def GetSumFiles(results, build_dir):
  323. if not results:
  324. print('Getting actual results from build directory %s' % build_dir)
  325. sum_files = CollectSumFiles(build_dir)
  326. else:
  327. print('Getting actual results from user-provided results')
  328. sum_files = results.split()
  329. return sum_files
  330. def PerformComparison(expected, actual, ignore_missing_failures):
  331. actual_vs_expected, expected_vs_actual = CompareResults(expected, actual)
  332. tests_ok = True
  333. if len(actual_vs_expected) > 0:
  334. PrintSummary('Unexpected results in this build (new failures)',
  335. actual_vs_expected)
  336. tests_ok = False
  337. if not ignore_missing_failures and len(expected_vs_actual) > 0:
  338. PrintSummary('Expected results not present in this build (fixed tests)'
  339. '\n\nNOTE: This is not a failure. It just means that these '
  340. 'tests were expected\nto fail, but either they worked in '
  341. 'this configuration or they were not\npresent at all.\n',
  342. expected_vs_actual)
  343. if tests_ok:
  344. print('\nSUCCESS: No unexpected failures.')
  345. return tests_ok
  346. def CheckExpectedResults():
  347. srcdir, target = GetBuildData()
  348. manifest_path = GetManifestPath(srcdir, target, True)
  349. print('Manifest: %s' % manifest_path)
  350. manifest = GetManifest(manifest_path)
  351. sum_files = GetSumFiles(_OPTIONS.results, _OPTIONS.build_dir)
  352. actual = GetResults(sum_files)
  353. if _OPTIONS.verbosity >= 1:
  354. PrintSummary('Tests expected to fail', manifest)
  355. PrintSummary('\nActual test results', actual)
  356. return PerformComparison(manifest, actual, _OPTIONS.ignore_missing_failures)
  357. def ProduceManifest():
  358. (srcdir, target) = GetBuildData()
  359. manifest_path = GetManifestPath(srcdir, target, False)
  360. print('Manifest: %s' % manifest_path)
  361. if os.path.exists(manifest_path) and not _OPTIONS.force:
  362. Error('Manifest file %s already exists.\nUse --force to overwrite.' %
  363. manifest_path)
  364. sum_files = GetSumFiles(_OPTIONS.results, _OPTIONS.build_dir)
  365. actual = GetResults(sum_files)
  366. manifest_file = open(manifest_path, encoding='latin-1', mode='w')
  367. for result in sorted(actual):
  368. print(result)
  369. manifest_file.write('%s\n' % result)
  370. manifest_file.close()
  371. return True
  372. def CompareBuilds():
  373. (srcdir, target) = GetBuildData()
  374. sum_files = GetSumFiles(_OPTIONS.results, _OPTIONS.build_dir)
  375. actual = GetResults(sum_files)
  376. clean_sum_files = GetSumFiles(_OPTIONS.results, _OPTIONS.clean_build)
  377. clean = GetResults(clean_sum_files)
  378. return PerformComparison(clean, actual, _OPTIONS.ignore_missing_failures)
  379. def Main(argv):
  380. parser = optparse.OptionParser(usage=__doc__)
  381. # Keep the following list sorted by option name.
  382. parser.add_option('--build_dir', action='store', type='string',
  383. dest='build_dir', default='.',
  384. help='Build directory to check (default = .)')
  385. parser.add_option('--clean_build', action='store', type='string',
  386. dest='clean_build', default=None,
  387. help='Compare test results from this build against '
  388. 'those of another (clean) build. Use this option '
  389. 'when comparing the test results of your patch versus '
  390. 'the test results of a clean build without your patch. '
  391. 'You must provide the path to the top directory of your '
  392. 'clean build.')
  393. parser.add_option('--force', action='store_true', dest='force',
  394. default=False, help='When used with --produce_manifest, '
  395. 'it will overwrite an existing manifest file '
  396. '(default = False)')
  397. parser.add_option('--ignore_missing_failures', action='store_true',
  398. dest='ignore_missing_failures', default=False,
  399. help='When a failure is expected in the manifest but '
  400. 'it is not found in the actual results, the script '
  401. 'produces a note alerting to this fact. This means '
  402. 'that the expected failure has been fixed, or '
  403. 'it did not run, or it may simply be flaky '
  404. '(default = False)')
  405. parser.add_option('--manifest', action='store', type='string',
  406. dest='manifest', default=None,
  407. help='Name of the manifest file to use (default = '
  408. 'taken from '
  409. 'contrib/testsuite-managment/<target_alias>.xfail)')
  410. parser.add_option('--produce_manifest', action='store_true',
  411. dest='produce_manifest', default=False,
  412. help='Produce the manifest for the current '
  413. 'build (default = False)')
  414. parser.add_option('--results', action='store', type='string',
  415. dest='results', default=None, help='Space-separated list '
  416. 'of .sum files with the testing results to check. The '
  417. 'only content needed from these files are the lines '
  418. 'starting with FAIL, XPASS or UNRESOLVED (default = '
  419. '.sum files collected from the build directory).')
  420. parser.add_option('--verbosity', action='store', dest='verbosity',
  421. type='int', default=0, help='Verbosity level (default = 0)')
  422. global _OPTIONS
  423. (_OPTIONS, _) = parser.parse_args(argv[1:])
  424. if _OPTIONS.produce_manifest:
  425. retval = ProduceManifest()
  426. elif _OPTIONS.clean_build:
  427. retval = CompareBuilds()
  428. else:
  429. retval = CheckExpectedResults()
  430. if retval:
  431. return 0
  432. else:
  433. return 1
  434. if __name__ == '__main__':
  435. retval = Main(sys.argv)
  436. sys.exit(retval)