earlywarningsystem.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (c) 2009 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 json
  29. import logging
  30. from optparse import make_option
  31. from webkitpy.common.config.committers import CommitterList
  32. from webkitpy.common.config.ports import DeprecatedPort
  33. from webkitpy.common.system.filesystem import FileSystem
  34. from webkitpy.common.system.executive import ScriptError
  35. from webkitpy.tool.bot.earlywarningsystemtask import EarlyWarningSystemTask, EarlyWarningSystemTaskDelegate
  36. from webkitpy.tool.bot.expectedfailures import ExpectedFailures
  37. from webkitpy.tool.bot.layouttestresultsreader import LayoutTestResultsReader
  38. from webkitpy.tool.bot.patchanalysistask import UnableToApplyPatch
  39. from webkitpy.tool.bot.queueengine import QueueEngine
  40. from webkitpy.tool.commands.queues import AbstractReviewQueue
  41. _log = logging.getLogger(__name__)
  42. class AbstractEarlyWarningSystem(AbstractReviewQueue, EarlyWarningSystemTaskDelegate):
  43. _build_style = "release"
  44. # FIXME: Switch _default_run_tests from opt-in to opt-out once more bots are ready to run tests.
  45. run_tests = False
  46. def __init__(self):
  47. options = [make_option("--run-tests", action="store_true", dest="run_tests", default=self.run_tests, help="Run the Layout tests for each patch")]
  48. AbstractReviewQueue.__init__(self, options=options)
  49. def begin_work_queue(self):
  50. AbstractReviewQueue.begin_work_queue(self)
  51. self._expected_failures = ExpectedFailures()
  52. self._layout_test_results_reader = LayoutTestResultsReader(self._tool, self._port.results_directory(), self._log_directory())
  53. def _failing_tests_message(self, task, patch):
  54. results = task.results_from_patch_test_run(patch)
  55. unexpected_failures = self._expected_failures.unexpected_failures_observed(results)
  56. if not unexpected_failures:
  57. return None
  58. return "New failing tests:\n%s" % "\n".join(unexpected_failures)
  59. def _post_reject_message_on_bug(self, tool, patch, status_id, extra_message_text=None):
  60. results_link = tool.status_server.results_url_for_status(status_id)
  61. message = "Attachment %s did not pass %s (%s):\nOutput: %s" % (patch.id(), self.name, self.port_name, results_link)
  62. if extra_message_text:
  63. message += "\n\n%s" % extra_message_text
  64. # FIXME: We might want to add some text about rejecting from the commit-queue in
  65. # the case where patch.commit_queue() isn't already set to '-'.
  66. if self.watchers:
  67. tool.bugs.add_cc_to_bug(patch.bug_id(), self.watchers)
  68. tool.bugs.set_flag_on_attachment(patch.id(), "commit-queue", "-", message)
  69. def review_patch(self, patch):
  70. task = EarlyWarningSystemTask(self, patch, self._options.run_tests)
  71. if not task.validate():
  72. self._did_error(patch, "%s did not process patch." % self.name)
  73. return False
  74. try:
  75. return task.run()
  76. except UnableToApplyPatch, e:
  77. self._did_error(patch, "%s unable to apply patch." % self.name)
  78. return False
  79. except ScriptError, e:
  80. self._post_reject_message_on_bug(self._tool, patch, task.failure_status_id, self._failing_tests_message(task, patch))
  81. results_archive = task.results_archive_from_patch_test_run(patch)
  82. if results_archive:
  83. self._upload_results_archive_for_patch(patch, results_archive)
  84. self._did_fail(patch)
  85. # FIXME: We're supposed to be able to raise e again here and have
  86. # one of our base classes mark the patch as fail, but there seems
  87. # to be an issue with the exit_code.
  88. return False
  89. # EarlyWarningSystemDelegate methods
  90. def parent_command(self):
  91. return self.name
  92. def run_command(self, command):
  93. self.run_webkit_patch(command + [self._deprecated_port.flag()])
  94. def command_passed(self, message, patch):
  95. pass
  96. def command_failed(self, message, script_error, patch):
  97. failure_log = self._log_from_script_error_for_upload(script_error)
  98. return self._update_status(message, patch=patch, results_file=failure_log)
  99. def expected_failures(self):
  100. return self._expected_failures
  101. def test_results(self):
  102. return self._layout_test_results_reader.results()
  103. def archive_last_test_results(self, patch):
  104. return self._layout_test_results_reader.archive(patch)
  105. def build_style(self):
  106. return self._build_style
  107. def refetch_patch(self, patch):
  108. return self._tool.bugs.fetch_attachment(patch.id())
  109. def report_flaky_tests(self, patch, flaky_test_results, results_archive):
  110. pass
  111. # StepSequenceErrorHandler methods
  112. @classmethod
  113. def handle_script_error(cls, tool, state, script_error):
  114. # FIXME: Why does this not exit(1) like the superclass does?
  115. _log.error(script_error.message_with_output())
  116. @classmethod
  117. def load_ews_classes(cls):
  118. filesystem = FileSystem()
  119. json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'ews.json')
  120. try:
  121. ewses = json.loads(filesystem.read_text_file(json_path))
  122. except ValueError:
  123. return None
  124. classes = []
  125. for name, config in ewses.iteritems():
  126. classes.append(type(str(name.replace(' ', '')), (AbstractEarlyWarningSystem,), {
  127. 'name': config['port'] + '-ews',
  128. 'port_name': config['port'],
  129. 'watchers': config.get('watchers', []),
  130. 'run_tests': config.get('runTests', cls.run_tests),
  131. }))
  132. return classes