1d0825bca7fe65beaee391d30da42e937db621564Steve Block# Copyright (C) 2009 Google Inc. All rights reserved.
2d0825bca7fe65beaee391d30da42e937db621564Steve Block#
3d0825bca7fe65beaee391d30da42e937db621564Steve Block# Redistribution and use in source and binary forms, with or without
4d0825bca7fe65beaee391d30da42e937db621564Steve Block# modification, are permitted provided that the following conditions are
5d0825bca7fe65beaee391d30da42e937db621564Steve Block# met:
6d0825bca7fe65beaee391d30da42e937db621564Steve Block#
7d0825bca7fe65beaee391d30da42e937db621564Steve Block#    * Redistributions of source code must retain the above copyright
8d0825bca7fe65beaee391d30da42e937db621564Steve Block# notice, this list of conditions and the following disclaimer.
9d0825bca7fe65beaee391d30da42e937db621564Steve Block#    * Redistributions in binary form must reproduce the above
10d0825bca7fe65beaee391d30da42e937db621564Steve Block# copyright notice, this list of conditions and the following disclaimer
11d0825bca7fe65beaee391d30da42e937db621564Steve Block# in the documentation and/or other materials provided with the
12d0825bca7fe65beaee391d30da42e937db621564Steve Block# distribution.
13d0825bca7fe65beaee391d30da42e937db621564Steve Block#    * Neither the name of Google Inc. nor the names of its
14d0825bca7fe65beaee391d30da42e937db621564Steve Block# contributors may be used to endorse or promote products derived from
15d0825bca7fe65beaee391d30da42e937db621564Steve Block# this software without specific prior written permission.
16d0825bca7fe65beaee391d30da42e937db621564Steve Block#
17d0825bca7fe65beaee391d30da42e937db621564Steve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18d0825bca7fe65beaee391d30da42e937db621564Steve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19d0825bca7fe65beaee391d30da42e937db621564Steve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20d0825bca7fe65beaee391d30da42e937db621564Steve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21d0825bca7fe65beaee391d30da42e937db621564Steve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22d0825bca7fe65beaee391d30da42e937db621564Steve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23d0825bca7fe65beaee391d30da42e937db621564Steve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d0825bca7fe65beaee391d30da42e937db621564Steve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d0825bca7fe65beaee391d30da42e937db621564Steve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d0825bca7fe65beaee391d30da42e937db621564Steve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27d0825bca7fe65beaee391d30da42e937db621564Steve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d0825bca7fe65beaee391d30da42e937db621564Steve Block
29d0825bca7fe65beaee391d30da42e937db621564Steve Blockimport unittest
30d0825bca7fe65beaee391d30da42e937db621564Steve Block
31dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.net.bugzilla import Attachment
32dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.common.system.outputcapture import OutputCapture
33545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochfrom webkitpy.common.system.executive import ScriptError
34dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.thirdparty.mock import Mock
35bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenfrom webkitpy.tool.commands.stepsequence import StepSequenceErrorHandler
36dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockfrom webkitpy.tool.mocktool import MockTool
37d0825bca7fe65beaee391d30da42e937db621564Steve Block
38d0825bca7fe65beaee391d30da42e937db621564Steve Block
39d0825bca7fe65beaee391d30da42e937db621564Steve Blockclass MockQueueEngine(object):
40dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block    def __init__(self, name, queue, wakeup_event):
41d0825bca7fe65beaee391d30da42e937db621564Steve Block        pass
42d0825bca7fe65beaee391d30da42e937db621564Steve Block
43d0825bca7fe65beaee391d30da42e937db621564Steve Block    def run(self):
44d0825bca7fe65beaee391d30da42e937db621564Steve Block        pass
45d0825bca7fe65beaee391d30da42e937db621564Steve Block
46d0825bca7fe65beaee391d30da42e937db621564Steve Block
47d0825bca7fe65beaee391d30da42e937db621564Steve Blockclass QueuesTest(unittest.TestCase):
48e14391e94c850b8bd03680c23b38978db68687a8John Reck    # This is _patch1 in mocktool.py
49e14391e94c850b8bd03680c23b38978db68687a8John Reck    mock_work_item = MockTool().bugs.fetch_attachment(197)
50d0825bca7fe65beaee391d30da42e937db621564Steve Block
51545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    def assert_outputs(self, func, func_name, args, expected_stdout, expected_stderr, expected_exceptions):
52545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        exception = None
53545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        if expected_exceptions and func_name in expected_exceptions:
54545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch            exception = expected_exceptions[func_name]
55545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
56545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        OutputCapture().assert_outputs(self,
57545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch                func,
58545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch                args=args,
59545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch                expected_stdout=expected_stdout.get(func_name, ""),
60545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch                expected_stderr=expected_stderr.get(func_name, ""),
61545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch                expected_exception=exception)
62545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch
6368513a70bcd92384395513322f1b801e7bf9c729Steve Block    def _default_begin_work_queue_stderr(self, name, checkout_dir):
6468513a70bcd92384395513322f1b801e7bf9c729Steve Block        string_replacements = {"name": name, 'checkout_dir': checkout_dir}
6568513a70bcd92384395513322f1b801e7bf9c729Steve Block        return "CAUTION: %(name)s will discard all local changes in \"%(checkout_dir)s\"\nRunning WebKit %(name)s.\nMOCK: update_status: %(name)s Starting Queue\n" % string_replacements
6668513a70bcd92384395513322f1b801e7bf9c729Steve Block
67a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    def assert_queue_outputs(self, queue, args=None, work_item=None, expected_stdout=None, expected_stderr=None, expected_exceptions=None, options=None, tool=None):
68a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        if not tool:
69a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            tool = MockTool()
702daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch            # This is a hack to make it easy for callers to not have to setup a custom MockFileSystem just to test the commit-queue
712daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch            # the cq tries to read the layout test results, and will hit a KeyError in MockFileSystem if we don't do this.
722daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch            tool.filesystem.write_text_file('/mock/results.html', "")
73d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not expected_stdout:
74d0825bca7fe65beaee391d30da42e937db621564Steve Block            expected_stdout = {}
75d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not expected_stderr:
76d0825bca7fe65beaee391d30da42e937db621564Steve Block            expected_stderr = {}
77d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not args:
78d0825bca7fe65beaee391d30da42e937db621564Steve Block            args = []
79a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        if not options:
80a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            options = Mock()
81a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            options.port = None
82d0825bca7fe65beaee391d30da42e937db621564Steve Block        if not work_item:
83d0825bca7fe65beaee391d30da42e937db621564Steve Block            work_item = self.mock_work_item
84d0825bca7fe65beaee391d30da42e937db621564Steve Block        tool.user.prompt = lambda message: "yes"
85d0825bca7fe65beaee391d30da42e937db621564Steve Block
86d0825bca7fe65beaee391d30da42e937db621564Steve Block        queue.execute(options, args, tool, engine=MockQueueEngine)
87d0825bca7fe65beaee391d30da42e937db621564Steve Block
88545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.queue_log_path, "queue_log_path", [], expected_stdout, expected_stderr, expected_exceptions)
89545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.work_item_log_path, "work_item_log_path", [work_item], expected_stdout, expected_stderr, expected_exceptions)
90545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.begin_work_queue, "begin_work_queue", [], expected_stdout, expected_stderr, expected_exceptions)
91545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.should_continue_work_queue, "should_continue_work_queue", [], expected_stdout, expected_stderr, expected_exceptions)
92545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.next_work_item, "next_work_item", [], expected_stdout, expected_stderr, expected_exceptions)
93545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.should_proceed_with_work_item, "should_proceed_with_work_item", [work_item], expected_stdout, expected_stderr, expected_exceptions)
94545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.process_work_item, "process_work_item", [work_item], expected_stdout, expected_stderr, expected_exceptions)
95545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        self.assert_outputs(queue.handle_unexpected_error, "handle_unexpected_error", [work_item, "Mock error message"], expected_stdout, expected_stderr, expected_exceptions)
96bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        # Should we have a different function for testing StepSequenceErrorHandlers?
97bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        if isinstance(queue, StepSequenceErrorHandler):
98e14391e94c850b8bd03680c23b38978db68687a8John Reck            self.assert_outputs(queue.handle_script_error, "handle_script_error", [tool, {"patch": self.mock_work_item}, ScriptError(message="ScriptError error message", script_args="MockErrorCommand")], expected_stdout, expected_stderr, expected_exceptions)
99