f72fb28f190f4e9dffb88c5c3c87a0ebde5a86dc
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / bot / flakytestreporter_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
31 from webkitpy.common.config.committers import Committer
32 from webkitpy.common.system.filesystem_mock import MockFileSystem
33 from webkitpy.common.system.outputcapture import OutputCapture
34 from webkitpy.tool.bot.flakytestreporter import FlakyTestReporter
35 from webkitpy.tool.mocktool import MockTool, MockStatusServer
36
37
38 # Creating fake CommitInfos is a pain, so we use a mock one here.
39 class MockCommitInfo(object):
40     def __init__(self, author_email):
41         self._author_email = author_email
42
43     def author(self):
44         # It's definitely possible to have commits with authors who
45         # are not in our committers.py list.
46         if not self._author_email:
47             return None
48         return Committer("Mock Committer", self._author_email)
49
50
51 class FlakyTestReporterTest(unittest.TestCase):
52     def _assert_emails_for_test(self, emails):
53         tool = MockTool()
54         reporter = FlakyTestReporter(tool, 'dummy-queue')
55         commit_infos = [MockCommitInfo(email) for email in emails]
56         tool.checkout().recent_commit_infos_for_files = lambda paths: set(commit_infos)
57         self.assertEqual(reporter._author_emails_for_test([]), set(emails))
58
59     def test_author_emails_for_test(self):
60         self._assert_emails_for_test([])
61         self._assert_emails_for_test(["test1@test.com", "test1@test.com"])
62         self._assert_emails_for_test(["test1@test.com", "test2@test.com"])
63
64     def test_create_bug_for_flaky_test(self):
65         reporter = FlakyTestReporter(MockTool(), 'dummy-queue')
66         expected_stderr = """MOCK create_bug
67 bug_title: Flaky Test: foo/bar.html
68 bug_description: This is an automatically generated bug from the dummy-queue.
69 foo/bar.html has been flaky on the dummy-queue.
70
71 foo/bar.html was authored by test@test.com.
72 http://trac.webkit.org/browser/trunk/LayoutTests/foo/bar.html
73
74 FLAKE_MESSAGE
75
76 The bots will update this with information from each new failure.
77
78 If you would like to track this test fix with another bug, please close this bug as a duplicate.
79
80 component: Tools / Tests
81 cc: test@test.com
82 blocked: 50856
83 """
84         OutputCapture().assert_outputs(self, reporter._create_bug_for_flaky_test, ['foo/bar.html', ['test@test.com'], 'FLAKE_MESSAGE'], expected_stderr=expected_stderr)
85
86     def test_follow_duplicate_chain(self):
87         tool = MockTool()
88         reporter = FlakyTestReporter(tool, 'dummy-queue')
89         bug = tool.bugs.fetch_bug(78)
90         self.assertEqual(reporter._follow_duplicate_chain(bug).id(), 76)
91
92     def test_bot_information(self):
93         tool = MockTool()
94         tool.status_server = MockStatusServer("MockBotId")
95         reporter = FlakyTestReporter(tool, 'dummy-queue')
96         self.assertEqual(reporter._bot_information(), "Bot: MockBotId  Port: MockPort  Platform: MockPlatform 1.0")
97
98     def test_report_flaky_tests_creating_bug(self):
99         tool = MockTool()
100         tool.filesystem = MockFileSystem({"/mock/foo/bar-diffs.txt": "mock"})
101         tool.status_server = MockStatusServer(bot_id="mock-bot-id")
102         reporter = FlakyTestReporter(tool, 'dummy-queue')
103         reporter._lookup_bug_for_flaky_test = lambda bug_id: None
104         patch = tool.bugs.fetch_attachment(197)
105         expected_stderr = """MOCK create_bug
106 bug_title: Flaky Test: foo/bar.html
107 bug_description: This is an automatically generated bug from the dummy-queue.
108 foo/bar.html has been flaky on the dummy-queue.
109
110 foo/bar.html was authored by abarth@webkit.org.
111 http://trac.webkit.org/browser/trunk/LayoutTests/foo/bar.html
112
113 The dummy-queue just saw foo/bar.html flake while processing attachment 197 on bug 42.
114 Bot: mock-bot-id  Port: MockPort  Platform: MockPlatform 1.0
115
116 The bots will update this with information from each new failure.
117
118 If you would like to track this test fix with another bug, please close this bug as a duplicate.
119
120 component: Tools / Tests
121 cc: abarth@webkit.org
122 blocked: 50856
123 MOCK add_attachment_to_bug: bug_id=78, description=Failure diff from mock-bot-id filename=failure.diff
124 MOCK bug comment: bug_id=42, cc=None
125 --- Begin comment ---
126 The dummy-queue encountered the following flaky tests while processing attachment 197:
127
128 foo/bar.html bug 78 (author: abarth@webkit.org)
129 The dummy-queue is continuing to process your patch.
130 --- End comment ---
131
132 """
133         OutputCapture().assert_outputs(self, reporter.report_flaky_tests, [['foo/bar.html'], patch], expected_stderr=expected_stderr)
134
135     def test_optional_author_string(self):
136         reporter = FlakyTestReporter(MockTool(), 'dummy-queue')
137         self.assertEqual(reporter._optional_author_string([]), "")
138         self.assertEqual(reporter._optional_author_string(["foo@bar.com"]), " (author: foo@bar.com)")
139         self.assertEqual(reporter._optional_author_string(["a@b.com", "b@b.com"]), " (authors: a@b.com and b@b.com)")
140
141     def test_results_diff_path_for_test(self):
142         reporter = FlakyTestReporter(MockTool(), 'dummy-queue')
143         self.assertEqual(reporter._results_diff_path_for_test("test.html"), "/mock/test-diffs.txt")
144
145     # report_flaky_tests is also tested by queues_unittest