initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / tool / bot / sheriffircbot_unittest.py
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
29 import unittest
30 import random
31
32 from webkitpy.common.system.outputcapture import OutputCapture
33 from webkitpy.tool.bot import irc_command
34 from webkitpy.tool.bot.queueengine import TerminateQueue
35 from webkitpy.tool.bot.sheriff import Sheriff
36 from webkitpy.tool.bot.sheriffircbot import SheriffIRCBot
37 from webkitpy.tool.bot.sheriff_unittest import MockSheriffBot
38 from webkitpy.tool.mocktool import MockTool
39
40
41 def run(message):
42     tool = MockTool()
43     tool.ensure_irc_connected(None)
44     bot = SheriffIRCBot(tool, Sheriff(tool, MockSheriffBot()))
45     bot._message_queue.post(["mock_nick", message])
46     bot.process_pending_messages()
47
48
49 class SheriffIRCBotTest(unittest.TestCase):
50     def test_parse_command_and_args(self):
51         tool = MockTool()
52         bot = SheriffIRCBot(tool, Sheriff(tool, MockSheriffBot()))
53         self.assertEqual(bot._parse_command_and_args(""), (irc_command.Eliza, [""]))
54         self.assertEqual(bot._parse_command_and_args("   "), (irc_command.Eliza, [""]))
55         self.assertEqual(bot._parse_command_and_args(" hi "), (irc_command.Hi, []))
56         self.assertEqual(bot._parse_command_and_args(" hi there "), (irc_command.Hi, ["there"]))
57
58     def test_exception_during_command(self):
59         tool = MockTool()
60         tool.ensure_irc_connected(None)
61         bot = SheriffIRCBot(tool, Sheriff(tool, MockSheriffBot()))
62
63         class CommandWithException(object):
64             def execute(self, nick, args, tool, sheriff):
65                 raise Exception("mock_exception")
66
67         bot._parse_command_and_args = lambda request: (CommandWithException, [])
68         expected_stderr = 'MOCK: irc.post: Exception executing command: mock_exception\n'
69         OutputCapture().assert_outputs(self, bot.process_message, args=["mock_nick", "ignored message"], expected_stderr=expected_stderr)
70
71         class CommandWithException(object):
72             def execute(self, nick, args, tool, sheriff):
73                 raise KeyboardInterrupt()
74
75         bot._parse_command_and_args = lambda request: (CommandWithException, [])
76         # KeyboardInterrupt and SystemExit are not subclasses of Exception and thus correctly will not be caught.
77         OutputCapture().assert_outputs(self, bot.process_message, args=["mock_nick", "ignored message"], expected_exception=KeyboardInterrupt)
78
79     def test_hi(self):
80         random.seed(23324)
81         expected_stderr = 'MOCK: irc.post: "Only you can prevent forest fires." -- Smokey the Bear\n'
82         OutputCapture().assert_outputs(self, run, args=["hi"], expected_stderr=expected_stderr)
83
84     def test_help(self):
85         expected_stderr = "MOCK: irc.post: mock_nick: Available commands: create-bug, help, hi, last-green-revision, restart, roll-chromium-deps, rollout, whois\n"
86         OutputCapture().assert_outputs(self, run, args=["help"], expected_stderr=expected_stderr)
87
88     def test_lgr(self):
89         expected_stderr = "MOCK: irc.post: mock_nick: http://trac.webkit.org/changeset/9479\n"
90         OutputCapture().assert_outputs(self, run, args=["last-green-revision"], expected_stderr=expected_stderr)
91
92     def test_restart(self):
93         expected_stderr = "MOCK: irc.post: Restarting...\n"
94         OutputCapture().assert_outputs(self, run, args=["restart"], expected_stderr=expected_stderr, expected_exception=TerminateQueue)
95
96     def test_rollout(self):
97         expected_stderr = "MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654...\nMOCK: irc.post: mock_nick, abarth, darin, eseidel: Created rollout: http://example.com/36936\n"
98         OutputCapture().assert_outputs(self, run, args=["rollout 21654 This patch broke the world"], expected_stderr=expected_stderr)
99
100     def test_roll_chromium_deps(self):
101         expected_stderr = "MOCK: irc.post: mock_nick: Rolling Chromium DEPS to r21654\nMOCK: irc.post: mock_nick: Created DEPS roll: http://example.com/36936\n"
102         OutputCapture().assert_outputs(self, run, args=["roll-chromium-deps 21654"], expected_stderr=expected_stderr)
103
104     def test_roll_chromium_deps_to_lkgr(self):
105         expected_stderr = "MOCK: irc.post: mock_nick: Rolling Chromium DEPS to last-known good revision\nMOCK: irc.post: mock_nick: Created DEPS roll: http://example.com/36936\n"
106         OutputCapture().assert_outputs(self, run, args=["roll-chromium-deps"], expected_stderr=expected_stderr)
107
108     def test_multi_rollout(self):
109         expected_stderr = "MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654, http://trac.webkit.org/changeset/21655, and http://trac.webkit.org/changeset/21656...\nMOCK: irc.post: mock_nick, abarth, darin, eseidel: Created rollout: http://example.com/36936\n"
110         OutputCapture().assert_outputs(self, run, args=["rollout 21654 21655 21656 This 21654 patch broke the world"], expected_stderr=expected_stderr)
111
112     def test_rollout_with_r_in_svn_revision(self):
113         expected_stderr = "MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654...\nMOCK: irc.post: mock_nick, abarth, darin, eseidel: Created rollout: http://example.com/36936\n"
114         OutputCapture().assert_outputs(self, run, args=["rollout r21654 This patch broke the world"], expected_stderr=expected_stderr)
115
116     def test_multi_rollout_with_r_in_svn_revision(self):
117         expected_stderr = "MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654, http://trac.webkit.org/changeset/21655, and http://trac.webkit.org/changeset/21656...\nMOCK: irc.post: mock_nick, abarth, darin, eseidel: Created rollout: http://example.com/36936\n"
118         OutputCapture().assert_outputs(self, run, args=["rollout r21654 21655 r21656 This r21654 patch broke the world"], expected_stderr=expected_stderr)
119
120     def test_rollout_bananas(self):
121         expected_stderr = "MOCK: irc.post: mock_nick: Usage: rollout SVN_REVISION [SVN_REVISIONS] REASON\n"
122         OutputCapture().assert_outputs(self, run, args=["rollout bananas"], expected_stderr=expected_stderr)
123
124     def test_rollout_invalidate_revision(self):
125         # When folks pass junk arguments, we should just spit the usage back at them.
126         expected_stderr = "MOCK: irc.post: mock_nick: Usage: rollout SVN_REVISION [SVN_REVISIONS] REASON\n"
127         OutputCapture().assert_outputs(self, run,
128                                        args=["rollout --component=Tools 21654"],
129                                        expected_stderr=expected_stderr)
130
131     def test_rollout_invalidate_reason(self):
132         # FIXME: I'm slightly confused as to why this doesn't return the USAGE message.
133         expected_stderr = """MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654...
134 MOCK: irc.post: mock_nick, abarth, darin, eseidel: Failed to create rollout patch:
135 MOCK: irc.post: The rollout reason may not begin with - (\"-bad (Requested by mock_nick on #webkit).\").
136 """
137         OutputCapture().assert_outputs(self, run,
138                                        args=["rollout 21654 -bad"],
139                                        expected_stderr=expected_stderr)
140
141     def test_multi_rollout_invalidate_reason(self):
142         expected_stderr = """MOCK: irc.post: mock_nick, abarth, darin, eseidel: Preparing rollout for http://trac.webkit.org/changeset/21654, http://trac.webkit.org/changeset/21655, and http://trac.webkit.org/changeset/21656...
143 MOCK: irc.post: mock_nick, abarth, darin, eseidel: Failed to create rollout patch:
144 MOCK: irc.post: The rollout reason may not begin with - (\"-bad (Requested by mock_nick on #webkit).\").
145 """
146         OutputCapture().assert_outputs(self, run,
147                                        args=["rollout "
148                                              "21654 21655 r21656 -bad"],
149                                        expected_stderr=expected_stderr)
150
151     def test_rollout_no_reason(self):
152         expected_stderr = "MOCK: irc.post: mock_nick: Usage: rollout SVN_REVISION [SVN_REVISIONS] REASON\n"
153         OutputCapture().assert_outputs(self, run, args=["rollout 21654"], expected_stderr=expected_stderr)
154
155     def test_multi_rollout_no_reason(self):
156         expected_stderr = "MOCK: irc.post: mock_nick: Usage: rollout SVN_REVISION [SVN_REVISIONS] REASON\n"
157         OutputCapture().assert_outputs(self, run, args=["rollout 21654 21655 r21656"], expected_stderr=expected_stderr)