flakytestreporter.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright (c) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import codecs
  29. import logging
  30. import os.path
  31. from webkitpy.common.net.layouttestresults import path_for_layout_test, LayoutTestResults
  32. from webkitpy.common.config import urls
  33. from webkitpy.tool.bot.botinfo import BotInfo
  34. from webkitpy.tool.grammar import plural, pluralize, join_with_separators
  35. _log = logging.getLogger(__name__)
  36. class FlakyTestReporter(object):
  37. def __init__(self, tool, bot_name):
  38. self._tool = tool
  39. self._bot_name = bot_name
  40. # FIXME: Use the real port object
  41. self._bot_info = BotInfo(tool, tool.deprecated_port().name())
  42. def _author_emails_for_test(self, flaky_test):
  43. test_path = path_for_layout_test(flaky_test)
  44. commit_infos = self._tool.checkout().recent_commit_infos_for_files([test_path])
  45. # This ignores authors which are not committers because we don't have their bugzilla_email.
  46. return set([commit_info.author().bugzilla_email() for commit_info in commit_infos if commit_info.author()])
  47. def _bugzilla_email(self):
  48. # FIXME: This is kinda a funny way to get the bugzilla email,
  49. # we could also just create a Credentials object directly
  50. # but some of the Credentials logic is in bugzilla.py too...
  51. self._tool.bugs.authenticate()
  52. return self._tool.bugs.username
  53. # FIXME: This should move into common.config
  54. _bot_emails = set([
  55. "commit-queue@webkit.org", # commit-queue
  56. "eseidel@chromium.org", # old commit-queue
  57. "webkit.review.bot@gmail.com", # style-queue, sheriff-bot, CrLx/Gtk EWS
  58. "buildbot@hotmail.com", # Win EWS
  59. # Mac EWS currently uses eric@webkit.org, but that's not normally a bot
  60. ])
  61. def _lookup_bug_for_flaky_test(self, flaky_test):
  62. bugs = self._tool.bugs.queries.fetch_bugs_matching_search(search_string=flaky_test)
  63. if not bugs:
  64. return None
  65. # Match any bugs which are from known bots or the email this bot is using.
  66. allowed_emails = self._bot_emails | set([self._bugzilla_email])
  67. bugs = filter(lambda bug: bug.reporter_email() in allowed_emails, bugs)
  68. if not bugs:
  69. return None
  70. if len(bugs) > 1:
  71. # FIXME: There are probably heuristics we could use for finding
  72. # the right bug instead of the first, like open vs. closed.
  73. _log.warn("Found %s %s matching '%s' filed by a bot, using the first." % (pluralize('bug', len(bugs)), [bug.id() for bug in bugs], flaky_test))
  74. return bugs[0]
  75. def _view_source_url_for_test(self, test_path):
  76. return urls.view_source_url("LayoutTests/%s" % test_path)
  77. def _create_bug_for_flaky_test(self, flaky_test, author_emails, latest_flake_message):
  78. format_values = {
  79. 'test': flaky_test,
  80. 'authors': join_with_separators(sorted(author_emails)),
  81. 'flake_message': latest_flake_message,
  82. 'test_url': self._view_source_url_for_test(flaky_test),
  83. 'bot_name': self._bot_name,
  84. }
  85. title = "Flaky Test: %(test)s" % format_values
  86. description = """This is an automatically generated bug from the %(bot_name)s.
  87. %(test)s has been flaky on the %(bot_name)s.
  88. %(test)s was authored by %(authors)s.
  89. %(test_url)s
  90. %(flake_message)s
  91. The bots will update this with information from each new failure.
  92. If you believe this bug to be fixed or invalid, feel free to close. The bots will re-open if the flake re-occurs.
  93. If you would like to track this test fix with another bug, please close this bug as a duplicate. The bots will follow the duplicate chain when making future comments.
  94. """ % format_values
  95. master_flake_bug = 50856 # MASTER: Flaky tests found by the commit-queue
  96. return self._tool.bugs.create_bug(title, description,
  97. component="Tools / Tests",
  98. cc=",".join(author_emails),
  99. blocked="50856")
  100. # This is over-engineered, but it makes for pretty bug messages.
  101. def _optional_author_string(self, author_emails):
  102. if not author_emails:
  103. return ""
  104. heading_string = plural('author') if len(author_emails) > 1 else 'author'
  105. authors_string = join_with_separators(sorted(author_emails))
  106. return " (%s: %s)" % (heading_string, authors_string)
  107. def _latest_flake_message(self, flaky_result, patch):
  108. failure_messages = [failure.message() for failure in flaky_result.failures]
  109. flake_message = "The %s just saw %s flake (%s) while processing attachment %s on bug %s." % (self._bot_name, flaky_result.test_name, ", ".join(failure_messages), patch.id(), patch.bug_id())
  110. return "%s\n%s" % (flake_message, self._bot_info.summary_text())
  111. def _results_diff_path_for_test(self, test_path):
  112. # FIXME: This is a big hack. We should get this path from results.json
  113. # except that old-run-webkit-tests doesn't produce a results.json
  114. # so we just guess at the file path.
  115. (test_path_root, _) = os.path.splitext(test_path)
  116. return "%s-diffs.txt" % test_path_root
  117. def _follow_duplicate_chain(self, bug):
  118. while bug.is_closed() and bug.duplicate_of():
  119. bug = self._tool.bugs.fetch_bug(bug.duplicate_of())
  120. return bug
  121. # Maybe this logic should move into Bugzilla? a reopen=True arg to post_comment?
  122. def _update_bug_for_flaky_test(self, bug, latest_flake_message):
  123. if bug.is_closed():
  124. self._tool.bugs.reopen_bug(bug.id(), latest_flake_message)
  125. else:
  126. self._tool.bugs.post_comment_to_bug(bug.id(), latest_flake_message)
  127. # This method is needed because our archive paths include a leading tmp/layout-test-results
  128. def _find_in_archive(self, path, archive):
  129. for archived_path in archive.namelist():
  130. # Archives are currently created with full paths.
  131. if archived_path.endswith(path):
  132. return archived_path
  133. return None
  134. def _attach_failure_diff(self, flake_bug_id, flaky_test, results_archive_zip):
  135. results_diff_path = self._results_diff_path_for_test(flaky_test)
  136. # Check to make sure that the path makes sense.
  137. # Since we're not actually getting this path from the results.html
  138. # there is a chance it's wrong.
  139. bot_id = self._tool.status_server.bot_id or "bot"
  140. archive_path = self._find_in_archive(results_diff_path, results_archive_zip)
  141. if archive_path:
  142. results_diff = results_archive_zip.read(archive_path)
  143. description = "Failure diff from %s" % bot_id
  144. self._tool.bugs.add_attachment_to_bug(flake_bug_id, results_diff, description, filename="failure.diff")
  145. else:
  146. _log.warn("%s does not exist in results archive, uploading entire archive." % results_diff_path)
  147. description = "Archive of layout-test-results from %s" % bot_id
  148. # results_archive is a ZipFile object, grab the File object (.fp) to pass to Mechanize for uploading.
  149. results_archive_file = results_archive_zip.fp
  150. # Rewind the file object to start (since Mechanize won't do that automatically)
  151. # See https://bugs.webkit.org/show_bug.cgi?id=54593
  152. results_archive_file.seek(0)
  153. self._tool.bugs.add_attachment_to_bug(flake_bug_id, results_archive_file, description, filename="layout-test-results.zip")
  154. def report_flaky_tests(self, patch, flaky_test_results, results_archive):
  155. message = "The %s encountered the following flaky tests while processing attachment %s:\n\n" % (self._bot_name, patch.id())
  156. for flaky_result in flaky_test_results:
  157. flaky_test = flaky_result.test_name
  158. bug = self._lookup_bug_for_flaky_test(flaky_test)
  159. latest_flake_message = self._latest_flake_message(flaky_result, patch)
  160. author_emails = self._author_emails_for_test(flaky_test)
  161. if not bug:
  162. _log.info("Bug does not already exist for %s, creating." % flaky_test)
  163. flake_bug_id = self._create_bug_for_flaky_test(flaky_test, author_emails, latest_flake_message)
  164. else:
  165. bug = self._follow_duplicate_chain(bug)
  166. # FIXME: Ideally we'd only make one comment per flake, not two. But that's not possible
  167. # in all cases (e.g. when reopening), so for now file attachment and comment are separate.
  168. self._update_bug_for_flaky_test(bug, latest_flake_message)
  169. flake_bug_id = bug.id()
  170. self._attach_failure_diff(flake_bug_id, flaky_test, results_archive)
  171. message += "%s bug %s%s\n" % (flaky_test, flake_bug_id, self._optional_author_string(author_emails))
  172. message += "The %s is continuing to process your patch." % self._bot_name
  173. self._tool.bugs.post_comment_to_bug(patch.bug_id(), message)