blob: 0774dfb698ccd9cf7955176576006ce26f5f7e75 [file] [log] [blame]
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07001# Copyright 2017 The Chromium OS 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
5"""Unit tests for the chrome_chromeos_lkgm program."""
6
Ben Pastene6a8361a2021-08-19 19:52:05 -07007from unittest import mock
Ben Pasteneca7c71b2021-10-07 18:34:27 -07008import urllib.parse
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07009
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070010from chromite.lib import cros_test_lib
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070011from chromite.scripts import chrome_chromeos_lkgm
12
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080013
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070014# pylint: disable=protected-access
Benjamin Gordon121a2aa2018-05-04 16:24:45 -060015class ChromeLKGMCommitterTester(cros_test_lib.RunCommandTestCase,
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070016 cros_test_lib.MockTempDirTestCase):
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070017 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080018
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070019 def setUp(self):
20 """Common set up method for all tests."""
Jack Neusb5fd30a2022-07-21 17:23:00 +000021 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter('1001.0.0',
22 'main')
Achuith Bhandarkardc3a3512018-03-16 15:10:57 -070023 self.old_lkgm = None
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070024
Ben Pastene769b1212021-12-14 18:26:50 -080025 @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead')
26 def testCommitNewLKGM(self, mock_get_file):
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070027 """Tests that we can commit a new LKGM file."""
Ben Pastene769b1212021-12-14 18:26:50 -080028 mock_get_file.return_value = '999.0.0'
29 with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg:
30 cg.return_value = mock.MagicMock(gerrit_number=123456)
31 with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce:
Fergus Dall64a21b62022-01-07 13:50:26 +110032 with mock.patch.object(
33 self.committer._gerrit_helper, 'SetReview') as bc:
Ben Pastene5b1d1192022-01-27 18:09:34 -080034 with mock.patch.object(self.committer._gerrit_helper, 'SetHashtags'):
35 self.committer.UpdateLKGM()
36 ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM',
37 '1001.0.0')
38 bc.assert_called_once_with(123456, labels={'Bot-Commit': 1},
39 notify='NONE')
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070040
Ben Pastene769b1212021-12-14 18:26:50 -080041 @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead')
42 def testOlderLKGMFails(self, mock_get_file):
Steven Bennetts4bc322a2017-08-28 09:37:39 -070043 """Tests that trying to update to an older lkgm version fails."""
Ben Pastene769b1212021-12-14 18:26:50 -080044 mock_get_file.return_value = '1002.0.0'
45 with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg:
46 cg.return_value = mock.MagicMock(gerrit_number=123456)
47 with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce:
48 self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid,
49 self.committer.UpdateLKGM)
50 ce.assert_not_called()
Steven Bennetts4bc322a2017-08-28 09:37:39 -070051
Ben Pastene769b1212021-12-14 18:26:50 -080052 @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead')
53 def testVersionWithChromeBranch(self, mock_get_file):
Steven Bennetts4bc322a2017-08-28 09:37:39 -070054 """Tests passing a version with a chrome branch strips the branch."""
Jack Neusb5fd30a2022-07-21 17:23:00 +000055 branch = 'refs/branch-heads/5000'
56 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter('1003.0.0-rc2',
57 branch)
Ben Pastene769b1212021-12-14 18:26:50 -080058 mock_get_file.return_value = '1002.0.0'
Steven Bennetts4bc322a2017-08-28 09:37:39 -070059
Ben Pastene769b1212021-12-14 18:26:50 -080060 with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg:
61 cg.return_value = mock.MagicMock(gerrit_number=123456)
62 with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce:
Fergus Dall64a21b62022-01-07 13:50:26 +110063 with mock.patch.object(
64 self.committer._gerrit_helper, 'SetReview') as bc:
Ben Pastene5b1d1192022-01-27 18:09:34 -080065 with mock.patch.object(self.committer._gerrit_helper, 'SetHashtags'):
66 # Check the file was actually written out correctly.
67 self.committer.UpdateLKGM()
Jack Neusb5fd30a2022-07-21 17:23:00 +000068 cg.assert_called_once_with('chromium/src', branch, mock.ANY, False)
Ben Pastene5b1d1192022-01-27 18:09:34 -080069 ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM',
70 '1003.0.0')
71 bc.assert_called_once_with(123456, labels={'Bot-Commit': 1},
72 notify='NONE')
Ben Pastened3b93d42019-10-10 09:56:29 -070073
74 def testCommitMsg(self):
75 """Tests format of the commit message."""
Ben Pastened3b93d42019-10-10 09:56:29 -070076 self.committer._PRESUBMIT_BOTS = ['bot1', 'bot2']
Ben Pastenef0fb0622021-09-24 23:38:13 +000077 self.committer._buildbucket_id = 'some-build-id'
Ben Pastened3b93d42019-10-10 09:56:29 -070078 commit_msg_lines = self.committer.ComposeCommitMsg().splitlines()
Ben Pastene769b1212021-12-14 18:26:50 -080079 self.assertIn(
80 'Automated Commit: LKGM 1001.0.0 for chromeos.', commit_msg_lines)
Ben Pastenef0fb0622021-09-24 23:38:13 +000081 self.assertIn(
82 'Uploaded by https://ci.chromium.org/b/some-build-id', commit_msg_lines)
Ben Pastened3b93d42019-10-10 09:56:29 -070083 self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1', commit_msg_lines)
84 self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2', commit_msg_lines)
Ben Pastene6a8361a2021-08-19 19:52:05 -070085
86 def testFindAlreadyOpenLKGMRoll(self):
87 already_open_issues = [123456]
Ben Pasteneca7c71b2021-10-07 18:34:27 -070088 self.committer._commit_msg_header = 'A message with spaces'
Ben Pastene6a8361a2021-08-19 19:52:05 -070089 with mock.patch.object(
90 self.committer._gerrit_helper, 'Query',
Ben Pasteneca7c71b2021-10-07 18:34:27 -070091 return_value=already_open_issues) as mock_query:
Ben Pastene6a8361a2021-08-19 19:52:05 -070092 self.assertEqual(
93 self.committer.FindAlreadyOpenLKGMRoll(),
94 already_open_issues[0])
Ben Pasteneca7c71b2021-10-07 18:34:27 -070095 escaped_quotes = urllib.parse.quote('"')
96 message = mock_query.call_args.kwargs['message']
97 self.assertEqual(message.count(escaped_quotes), 2)
98 self.assertTrue(message.startswith(escaped_quotes))
99 self.assertTrue(message.endswith(escaped_quotes))
Ben Pastene6a8361a2021-08-19 19:52:05 -0700100 already_open_issues = [123456, 654321]
101 with mock.patch.object(
102 self.committer._gerrit_helper, 'Query',
103 return_value=already_open_issues):
104 self.assertRaises(
105 chrome_chromeos_lkgm.LKGMNotValid,
106 self.committer.FindAlreadyOpenLKGMRoll)
107
108 def testSubmitToCQ(self):
Ben Pastene33da0c22021-11-29 21:14:05 -0800109 self.committer._buildbucket_id = 'some-build-id'
Ben Pastene6a8361a2021-08-19 19:52:05 -0700110 already_open_issue = 123456
Ben Pastene33da0c22021-11-29 21:14:05 -0800111 with mock.patch.object(
112 self.committer._gerrit_helper, 'SetReview') as mock_review:
Ben Pastene6a8361a2021-08-19 19:52:05 -0700113 self.committer.SubmitToCQ(already_open_issue)
Ben Pastene33da0c22021-11-29 21:14:05 -0800114 self.assertIn(
115 self.committer._buildbucket_id, mock_review.call_args[1]['msg'])