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
29import os
30
31from webkitpy.thirdparty.mock import Mock
32from webkitpy.common.system.outputcapture import OutputCapture
33from webkitpy.tool.bot.queueengine import QueueEngine
34from webkitpy.tool.commands.earlywarningsystem import *
35from webkitpy.tool.commands.queuestest import QueuesTest
36from webkitpy.tool.mocktool import MockTool, MockOptions
37
38
39class AbstractEarlyWarningSystemTest(QueuesTest):
40    def test_can_build(self):
41        # Needed to define port_name, used in AbstractEarlyWarningSystem.__init__
42        class TestEWS(AbstractEarlyWarningSystem):
43            port_name = "win"  # Needs to be a port which port/factory understands.
44
45        queue = TestEWS()
46        queue.bind_to_tool(MockTool(log_executive=True))
47        queue._options = MockOptions(port=None)
48        expected_stderr = "MOCK run_and_throw_if_fail: ['echo', '--status-host=example.com', 'build', '--port=win', '--build-style=release', '--force-clean', '--no-update']\n"
49        OutputCapture().assert_outputs(self, queue._can_build, [], expected_stderr=expected_stderr)
50
51        def mock_run_webkit_patch(args):
52            raise ScriptError("MOCK script error")
53
54        queue.run_webkit_patch = mock_run_webkit_patch
55        expected_stderr = "MOCK: update_status: None Unable to perform a build\n"
56        OutputCapture().assert_outputs(self, queue._can_build, [], expected_stderr=expected_stderr)
57
58    # FIXME: This belongs on an AbstractReviewQueueTest object in queues_unittest.py
59    def test_subprocess_handled_error(self):
60        queue = AbstractReviewQueue()
61        queue.bind_to_tool(MockTool())
62
63        def mock_review_patch(patch):
64            raise ScriptError('MOCK script error', exit_code=QueueEngine.handled_error_code)
65
66        queue.review_patch = mock_review_patch
67        mock_patch = queue._tool.bugs.fetch_attachment(197)
68        expected_stderr = "MOCK: release_work_item: None 197\n"
69        OutputCapture().assert_outputs(self, queue.process_work_item, [mock_patch], expected_stderr=expected_stderr, expected_exception=ScriptError)
70
71
72class EarlyWarningSytemTest(QueuesTest):
73    def test_failed_builds(self):
74        ews = ChromiumLinuxEWS()
75        ews.bind_to_tool(MockTool())
76        ews._build = lambda patch, first_run=False: False
77        ews._can_build = lambda: True
78        mock_patch = ews._tool.bugs.fetch_attachment(197)
79        ews.review_patch(mock_patch)
80
81    def _default_expected_stderr(self, ews):
82        string_replacemnts = {
83            "name": ews.name,
84            "port": ews.port_name,
85            "watchers": ews.watchers,
86        }
87        expected_stderr = {
88            "begin_work_queue": self._default_begin_work_queue_stderr(ews.name, ews._tool.scm().checkout_root),
89            "handle_unexpected_error": "Mock error message\n",
90            "next_work_item": "",
91            "process_work_item": "MOCK: update_status: %(name)s Pass\nMOCK: release_work_item: %(name)s 197\n" % string_replacemnts,
92            "handle_script_error": "MOCK: update_status: %(name)s ScriptError error message\nMOCK bug comment: bug_id=42, cc=%(watchers)s\n--- Begin comment ---\nAttachment 197 did not build on %(port)s:\nBuild output: http://dummy_url\n--- End comment ---\n\n" % string_replacemnts,
93        }
94        return expected_stderr
95
96    def _test_ews(self, ews):
97        ews.bind_to_tool(MockTool())
98        expected_exceptions = {
99            "handle_script_error": SystemExit,
100        }
101        self.assert_queue_outputs(ews, expected_stderr=self._default_expected_stderr(ews), expected_exceptions=expected_exceptions)
102
103    def _test_committer_only_ews(self, ews):
104        ews.bind_to_tool(MockTool())
105        expected_stderr = self._default_expected_stderr(ews)
106        string_replacemnts = {"name": ews.name}
107        expected_stderr["process_work_item"] = "MOCK: update_status: %(name)s Error: %(name)s cannot process patches from non-committers :(\nMOCK: release_work_item: %(name)s 197\n" % string_replacemnts
108        expected_exceptions = {"handle_script_error": SystemExit}
109        self.assert_queue_outputs(ews, expected_stderr=expected_stderr, expected_exceptions=expected_exceptions)
110
111    # FIXME: If all EWSes are going to output the same text, we
112    # could test them all in one method with a for loop over an array.
113    def test_chromium_linux_ews(self):
114        self._test_ews(ChromiumLinuxEWS())
115
116    def test_chromium_windows_ews(self):
117        self._test_ews(ChromiumWindowsEWS())
118
119    def test_qt_ews(self):
120        self._test_ews(QtEWS())
121
122    def test_gtk_ews(self):
123        self._test_ews(GtkEWS())
124
125    def test_efl_ews(self):
126        self._test_ews(EflEWS())
127
128    def test_mac_ews(self):
129        self._test_committer_only_ews(MacEWS())
130
131    def test_chromium_mac_ews(self):
132        self._test_committer_only_ews(ChromiumMacEWS())
133