1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7import mock
8import webapp2
9import webtest
10
11from dashboard import bisect_fyi
12from dashboard import issue_tracker_service
13from dashboard import stored_object
14from dashboard import testing_common
15from dashboard import utils
16
17TEST_FYI_CONFIGS = {
18    'positive_culprit': {
19        'bisect_config': {
20            'bad_revision': '357672',
21            'bug_id': 111,
22            'command': ('python src/tools/perf/run_benchmark -v '
23                        '--browser=release_x64 --output-format=chartjson '
24                        '--upload-results --also-run-disabled-tests '
25                        'blink_perf.bindings'),
26            'good_revision': '357643',
27            'gs_bucket': 'chrome-perf',
28            'max_time_minutes': '20',
29            'metric': 'create-element/create-element',
30            'recipe_tester_name': 'win_x64_perf_bisect',
31            'repeat_count': '10',
32            'test_type': 'perf'
33        },
34        'expected_results': {
35            'status': ['completed'],
36            'culprit_data': {'cl': ['2a1781d64d']},
37        }
38    },
39    'early_abort': {
40        'bisect_config': {
41            'bad_revision': '257672',
42            'bug_id': 222,
43            'command': ('python src/tools/perf/run_benchmark -v '
44                        '--browser=release_x64 --output-format=chartjson '
45                        '--upload-results --also-run-disabled-tests '
46                        'blink_perf.bindings'),
47            'good_revision': '257643',
48            'gs_bucket': 'chrome-perf',
49            'max_time_minutes': '20',
50            'metric': 'create-element/create-element',
51            'recipe_tester_name': 'win_x64_perf_bisect',
52            'repeat_count': '10',
53            'test_type': 'perf'
54        },
55        'expected_results': {
56            'status': ['aborted'],
57        }
58    },
59}
60
61
62@mock.patch('apiclient.discovery.build', mock.MagicMock())
63@mock.patch.object(utils, 'ServiceAccountHttp', mock.MagicMock())
64class BisectFYITest(testing_common.TestCase):
65
66  def setUp(self):
67    super(BisectFYITest, self).setUp()
68    app = webapp2.WSGIApplication(
69        [('/bisect_fyi', bisect_fyi.BisectFYIHandler)])
70    self.testapp = webtest.TestApp(app)
71    stored_object.Set(
72        bisect_fyi._BISECT_FYI_CONFIGS_KEY, TEST_FYI_CONFIGS)
73    testing_common.SetIsInternalUser('internal@chromium.org', True)
74    self.SetCurrentUser('internal@chromium.org')
75
76  @mock.patch.object(
77      issue_tracker_service.IssueTrackerService, 'AddBugComment')
78  @mock.patch.object(bisect_fyi.start_try_job, '_PerformBuildbucketBisect')
79  def testPost_FailedJobs_BisectFYI(self, mock_perform_bisect, _):
80    mock_perform_bisect.return_value = {'error': 'PerformBisect Failed'}
81    self.testapp.post('/bisect_fyi')
82    messages = self.mail_stub.get_sent_messages()
83    self.assertEqual(1, len(messages))
84
85  @mock.patch.object(
86      issue_tracker_service.IssueTrackerService, 'AddBugComment')
87  @mock.patch.object(bisect_fyi.start_try_job, '_PerformBuildbucketBisect')
88  def testPost_SuccessJobs_BisectFYI(self, mock_perform_bisect, mock_comment):
89    mock_perform_bisect.return_value = {'issue_id': 'http://fake'}
90    self.testapp.post('/bisect_fyi')
91    messages = self.mail_stub.get_sent_messages()
92    self.assertEqual(0, len(messages))
93    mock_comment.assert_called_with(222, mock.ANY, send_email=False)
94
95
96if __name__ == '__main__':
97  unittest.main()
98