2011-02-06 Maciej Stachowiak <mjs@apple.com>
[WebKit.git] / Tools / Scripts / webkitpy / common / net / buildbot / buildbot_unittest.py
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
29 import unittest
30
31 from webkitpy.common.net.layouttestresults import LayoutTestResults
32 from webkitpy.common.net.buildbot import BuildBot, Builder, Build
33 from webkitpy.layout_tests.layout_package import test_results
34 from webkitpy.layout_tests.layout_package import test_failures
35 from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup
36
37
38 class BuilderTest(unittest.TestCase):
39     def _mock_test_result(self, testname):
40         return test_results.TestResult(testname, [test_failures.FailureTextMismatch()])
41
42     def _install_fetch_build(self, failure):
43         def _mock_fetch_build(build_number):
44             build = Build(
45                 builder=self.builder,
46                 build_number=build_number,
47                 revision=build_number + 1000,
48                 is_green=build_number < 4
49             )
50             results = [self._mock_test_result(testname) for testname in failure(build_number)]
51             build._layout_test_results = LayoutTestResults(results)
52             return build
53         self.builder._fetch_build = _mock_fetch_build
54
55     def setUp(self):
56         self.buildbot = BuildBot()
57         self.builder = Builder(u"Test Builder \u2661", self.buildbot)
58         self._install_fetch_build(lambda build_number: ["test1", "test2"])
59
60     def test_find_regression_window(self):
61         regression_window = self.builder.find_regression_window(self.builder.build(10))
62         self.assertEqual(regression_window.build_before_failure().revision(), 1003)
63         self.assertEqual(regression_window.failing_build().revision(), 1004)
64
65         regression_window = self.builder.find_regression_window(self.builder.build(10), look_back_limit=2)
66         self.assertEqual(regression_window.build_before_failure(), None)
67         self.assertEqual(regression_window.failing_build().revision(), 1008)
68
69     def test_none_build(self):
70         self.builder._fetch_build = lambda build_number: None
71         regression_window = self.builder.find_regression_window(self.builder.build(10))
72         self.assertEqual(regression_window.build_before_failure(), None)
73         self.assertEqual(regression_window.failing_build(), None)
74
75     def test_flaky_tests(self):
76         self._install_fetch_build(lambda build_number: ["test1"] if build_number % 2 else ["test2"])
77         regression_window = self.builder.find_regression_window(self.builder.build(10))
78         self.assertEqual(regression_window.build_before_failure().revision(), 1009)
79         self.assertEqual(regression_window.failing_build().revision(), 1010)
80
81     def test_failure_and_flaky(self):
82         self._install_fetch_build(lambda build_number: ["test1", "test2"] if build_number % 2 else ["test2"])
83         regression_window = self.builder.find_regression_window(self.builder.build(10))
84         self.assertEqual(regression_window.build_before_failure().revision(), 1003)
85         self.assertEqual(regression_window.failing_build().revision(), 1004)
86
87     def test_no_results(self):
88         self._install_fetch_build(lambda build_number: ["test1", "test2"] if build_number % 2 else ["test2"])
89         regression_window = self.builder.find_regression_window(self.builder.build(10))
90         self.assertEqual(regression_window.build_before_failure().revision(), 1003)
91         self.assertEqual(regression_window.failing_build().revision(), 1004)
92
93     def test_failure_after_flaky(self):
94         self._install_fetch_build(lambda build_number: ["test1", "test2"] if build_number > 6 else ["test3"])
95         regression_window = self.builder.find_regression_window(self.builder.build(10))
96         self.assertEqual(regression_window.build_before_failure().revision(), 1006)
97         self.assertEqual(regression_window.failing_build().revision(), 1007)
98
99     def test_find_blameworthy_regression_window(self):
100         self.assertEqual(self.builder.find_blameworthy_regression_window(10).revisions(), [1004])
101         self.assertEqual(self.builder.find_blameworthy_regression_window(10, look_back_limit=2), None)
102         # Flakey test avoidance requires at least 2 red builds:
103         self.assertEqual(self.builder.find_blameworthy_regression_window(4), None)
104         self.assertEqual(self.builder.find_blameworthy_regression_window(4, avoid_flakey_tests=False).revisions(), [1004])
105         # Green builder:
106         self.assertEqual(self.builder.find_blameworthy_regression_window(3), None)
107
108     def test_build_caching(self):
109         self.assertEqual(self.builder.build(10), self.builder.build(10))
110
111     def test_build_and_revision_for_filename(self):
112         expectations = {
113             "r47483 (1)/" : (47483, 1),
114             "r47483 (1).zip" : (47483, 1),
115         }
116         for filename, revision_and_build in expectations.items():
117             self.assertEqual(self.builder._revision_and_build_for_filename(filename), revision_and_build)
118
119
120 class BuildTest(unittest.TestCase):
121     def test_layout_test_results(self):
122         build = Build(None, None, None, None)
123         build._fetch_results_html = lambda: None
124         # Test that layout_test_results() returns None if the fetch fails.
125         self.assertEqual(build.layout_test_results(), None)
126
127
128 class BuildBotTest(unittest.TestCase):
129
130     _example_one_box_status = '''
131     <table>
132     <tr>
133     <td class="box"><a href="builders/Windows%20Debug%20%28Tests%29">Windows Debug (Tests)</a></td>
134       <td align="center" class="LastBuild box success"><a href="builders/Windows%20Debug%20%28Tests%29/builds/3693">47380</a><br />build<br />successful</td>
135       <td align="center" class="Activity building">building<br />ETA in<br />~ 14 mins<br />at 13:40</td>
136     <tr>
137     <td class="box"><a href="builders/SnowLeopard%20Intel%20Release">SnowLeopard Intel Release</a></td>
138       <td class="LastBuild box" >no build</td>
139       <td align="center" class="Activity building">building<br />< 1 min</td>
140     <tr>
141     <td class="box"><a href="builders/Qt%20Linux%20Release">Qt Linux Release</a></td>
142       <td align="center" class="LastBuild box failure"><a href="builders/Qt%20Linux%20Release/builds/654">47383</a><br />failed<br />compile-webkit</td>
143       <td align="center" class="Activity idle">idle<br />3 pending</td>
144     <tr>
145     <td class="box"><a href="builders/Qt%20Windows%2032-bit%20Debug">Qt Windows 32-bit Debug</a></td>
146       <td align="center" class="LastBuild box failure"><a href="builders/Qt%20Windows%2032-bit%20Debug/builds/2090">60563</a><br />failed<br />failed<br />slave<br />lost</td>
147       <td align="center" class="Activity building">building<br />ETA in<br />~ 5 mins<br />at 08:25</td>
148     </table>
149 '''
150     _expected_example_one_box_parsings = [
151         {
152             'is_green': True,
153             'build_number' : 3693,
154             'name': u'Windows Debug (Tests)',
155             'built_revision': 47380,
156             'activity': 'building',
157             'pending_builds': 0,
158         },
159         {
160             'is_green': False,
161             'build_number' : None,
162             'name': u'SnowLeopard Intel Release',
163             'built_revision': None,
164             'activity': 'building',
165             'pending_builds': 0,
166         },
167         {
168             'is_green': False,
169             'build_number' : 654,
170             'name': u'Qt Linux Release',
171             'built_revision': 47383,
172             'activity': 'idle',
173             'pending_builds': 3,
174         },
175         {
176             'is_green': True,
177             'build_number' : 2090,
178             'name': u'Qt Windows 32-bit Debug',
179             'built_revision': 60563,
180             'activity': 'building',
181             'pending_builds': 0,
182         },
183     ]
184
185     def test_status_parsing(self):
186         buildbot = BuildBot()
187
188         soup = BeautifulSoup(self._example_one_box_status)
189         status_table = soup.find("table")
190         input_rows = status_table.findAll('tr')
191
192         for x in range(len(input_rows)):
193             status_row = input_rows[x]
194             expected_parsing = self._expected_example_one_box_parsings[x]
195
196             builder = buildbot._parse_builder_status_from_row(status_row)
197
198             # Make sure we aren't parsing more or less than we expect
199             self.assertEquals(builder.keys(), expected_parsing.keys())
200
201             for key, expected_value in expected_parsing.items():
202                 self.assertEquals(builder[key], expected_value, ("Builder %d parse failure for key: %s: Actual='%s' Expected='%s'" % (x, key, builder[key], expected_value)))
203
204     def test_core_builder_methods(self):
205         buildbot = BuildBot()
206
207         # Override builder_statuses function to not touch the network.
208         def example_builder_statuses(): # We could use instancemethod() to bind 'self' but we don't need to.
209             return BuildBotTest._expected_example_one_box_parsings
210         buildbot.builder_statuses = example_builder_statuses
211
212         buildbot.core_builder_names_regexps = [ 'Leopard', "Windows.*Build" ]
213         self.assertEquals(buildbot.red_core_builders_names(), [])
214         self.assertTrue(buildbot.core_builders_are_green())
215
216         buildbot.core_builder_names_regexps = [ 'SnowLeopard', 'Qt' ]
217         self.assertEquals(buildbot.red_core_builders_names(), [ u'SnowLeopard Intel Release', u'Qt Linux Release' ])
218         self.assertFalse(buildbot.core_builders_are_green())
219
220     def test_builder_name_regexps(self):
221         buildbot = BuildBot()
222
223         # For complete testing, this list should match the list of builders at build.webkit.org:
224         example_builders = [
225             {'name': u'Tiger Intel Release', },
226             {'name': u'Leopard Intel Release (Build)', },
227             {'name': u'Leopard Intel Release (Tests)', },
228             {'name': u'Leopard Intel Debug (Build)', },
229             {'name': u'Leopard Intel Debug (Tests)', },
230             {'name': u'SnowLeopard Intel Release (Build)', },
231             {'name': u'SnowLeopard Intel Release (Tests)', },
232             {'name': u'SnowLeopard Intel Release (WebKit2 Tests)', },
233             {'name': u'SnowLeopard Intel Leaks', },
234             {'name': u'Windows Release (Build)', },
235             {'name': u'Windows Release (Tests)', },
236             {'name': u'Windows Debug (Build)', },
237             {'name': u'Windows Debug (Tests)', },
238             {'name': u'GTK Linux 32-bit Release', },
239             {'name': u'GTK Linux 32-bit Debug', },
240             {'name': u'GTK Linux 64-bit Debug', },
241             {'name': u'GTK Linux 64-bit Release', },
242             {'name': u'Qt Linux Release', },
243             {'name': u'Qt Linux Release minimal', },
244             {'name': u'Qt Linux ARMv5 Release', },
245             {'name': u'Qt Linux ARMv7 Release', },
246             {'name': u'Qt Windows 32-bit Release', },
247             {'name': u'Qt Windows 32-bit Debug', },
248             {'name': u'Chromium Linux Release', },
249             {'name': u'Chromium Mac Release', },
250             {'name': u'Chromium Win Release', },
251             {'name': u'Chromium Linux Release (Tests)', },
252             {'name': u'Chromium Mac Release (Tests)', },
253             {'name': u'Chromium Win Release (Tests)', },
254             {'name': u'New run-webkit-tests', },
255         ]
256         name_regexps = [
257             "SnowLeopard.*Build",
258             "SnowLeopard.*\(Test",
259             "SnowLeopard.*\(WebKit2 Test",
260             "Leopard",
261             "Tiger",
262             "Windows.*Build",
263             "EFL",
264             "GTK.*32",
265             "GTK.*64.*Debug",  # Disallow the 64-bit Release bot which is broken.
266             "Qt",
267             "Chromium.*Release$",
268         ]
269         expected_builders = [
270             {'name': u'Tiger Intel Release', },
271             {'name': u'Leopard Intel Release (Build)', },
272             {'name': u'Leopard Intel Release (Tests)', },
273             {'name': u'Leopard Intel Debug (Build)', },
274             {'name': u'Leopard Intel Debug (Tests)', },
275             {'name': u'SnowLeopard Intel Release (Build)', },
276             {'name': u'SnowLeopard Intel Release (Tests)', },
277             {'name': u'Windows Release (Build)', },
278             {'name': u'Windows Debug (Build)', },
279             {'name': u'GTK Linux 32-bit Release', },
280             {'name': u'GTK Linux 32-bit Debug', },
281             {'name': u'GTK Linux 64-bit Debug', },
282             {'name': u'Qt Linux Release', },
283             {'name': u'Qt Linux Release minimal', },
284             {'name': u'Qt Linux ARMv5 Release', },
285             {'name': u'Qt Linux ARMv7 Release', },
286             {'name': u'Qt Windows 32-bit Release', },
287             {'name': u'Qt Windows 32-bit Debug', },
288             {'name': u'Chromium Linux Release', },
289             {'name': u'Chromium Mac Release', },
290             {'name': u'Chromium Win Release', },
291         ]
292
293         # This test should probably be updated if the default regexp list changes
294         self.assertEquals(buildbot.core_builder_names_regexps, name_regexps)
295
296         builders = buildbot._builder_statuses_with_names_matching_regexps(example_builders, name_regexps)
297         self.assertEquals(builders, expected_builders)
298
299     def test_builder_with_name(self):
300         buildbot = BuildBot()
301
302         builder = buildbot.builder_with_name("Test Builder")
303         self.assertEqual(builder.name(), "Test Builder")
304         self.assertEqual(builder.url(), "http://build.webkit.org/builders/Test%20Builder")
305         self.assertEqual(builder.url_encoded_name(), "Test%20Builder")
306         self.assertEqual(builder.results_url(), "http://build.webkit.org/results/Test%20Builder")
307
308         # Override _fetch_build_dictionary function to not touch the network.
309         def mock_fetch_build_dictionary(self, build_number):
310             build_dictionary = {
311                 "sourceStamp": {
312                     "revision" : 2 * build_number,
313                     },
314                 "number" : int(build_number),
315                 "results" : build_number % 2, # 0 means pass
316             }
317             return build_dictionary
318         buildbot._fetch_build_dictionary = mock_fetch_build_dictionary
319
320         build = builder.build(10)
321         self.assertEqual(build.builder(), builder)
322         self.assertEqual(build.url(), "http://build.webkit.org/builders/Test%20Builder/builds/10")
323         self.assertEqual(build.results_url(), "http://build.webkit.org/results/Test%20Builder/r20%20%2810%29")
324         self.assertEqual(build.revision(), 20)
325         self.assertEqual(build.is_green(), True)
326
327         build = build.previous_build()
328         self.assertEqual(build.builder(), builder)
329         self.assertEqual(build.url(), "http://build.webkit.org/builders/Test%20Builder/builds/9")
330         self.assertEqual(build.results_url(), "http://build.webkit.org/results/Test%20Builder/r18%20%289%29")
331         self.assertEqual(build.revision(), 18)
332         self.assertEqual(build.is_green(), False)
333
334         self.assertEqual(builder.build(None), None)
335
336     _example_directory_listing = '''
337 <h1>Directory listing for /results/SnowLeopard Intel Leaks/</h1>
338
339 <table>
340         <tr class="alt">
341             <th>Filename</th>
342             <th>Size</th>
343             <th>Content type</th>
344             <th>Content encoding</th>
345         </tr>
346 <tr class="directory ">
347     <td><a href="r47483%20%281%29/"><b>r47483 (1)/</b></a></td>
348     <td><b></b></td>
349     <td><b>[Directory]</b></td>
350     <td><b></b></td>
351 </tr>
352 <tr class="file alt">
353     <td><a href="r47484%20%282%29.zip">r47484 (2).zip</a></td>
354     <td>89K</td>
355     <td>[application/zip]</td>
356     <td></td>
357 </tr>
358 '''
359     _expected_files = [
360         {
361             "filename" : "r47483 (1)/",
362             "size" : "",
363             "type" : "[Directory]",
364             "encoding" : "",
365         },
366         {
367             "filename" : "r47484 (2).zip",
368             "size" : "89K",
369             "type" : "[application/zip]",
370             "encoding" : "",
371         },
372     ]
373
374     def test_parse_build_to_revision_map(self):
375         buildbot = BuildBot()
376         files = buildbot._parse_twisted_directory_listing(self._example_directory_listing)
377         self.assertEqual(self._expected_files, files)
378
379     # Revision, is_green
380     # Ordered from newest (highest number) to oldest.
381     fake_builder1 = [
382         [2, False],
383         [1, True],
384     ]
385     fake_builder2 = [
386         [2, False],
387         [1, True],
388     ]
389     fake_builders = [
390         fake_builder1,
391         fake_builder2,
392     ]
393     def _build_from_fake(self, fake_builder, index):
394         if index >= len(fake_builder):
395             return None
396         fake_build = fake_builder[index]
397         build = Build(
398             builder=fake_builder,
399             build_number=index,
400             revision=fake_build[0],
401             is_green=fake_build[1],
402         )
403         def mock_previous_build():
404             return self._build_from_fake(fake_builder, index + 1)
405         build.previous_build = mock_previous_build
406         return build
407
408     def _fake_builds_at_index(self, index):
409         return [self._build_from_fake(builder, index) for builder in self.fake_builders]
410
411     def test_last_green_revision(self):
412         buildbot = BuildBot()
413         def mock_builds_from_builders(only_core_builders):
414             return self._fake_builds_at_index(0)
415         buildbot._latest_builds_from_builders = mock_builds_from_builders
416         self.assertEqual(buildbot.last_green_revision(), 1)
417
418
419 if __name__ == '__main__':
420     unittest.main()