blob: 244e62ec65038f69b0cc9a2242626dc2d986b24e [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07002# 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
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07008
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +09009from chromite.lib import chromeos_version
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
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090014class StubGerritChange:
15 """Stab class corresponding to cros_patch.GerritChange."""
16
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090017 def __init__(
18 self,
19 gerrit_number,
20 file_content,
21 subject,
22 mergeable=True,
23 original_file_content=None,
24 ):
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090025 self._gerrit_number = gerrit_number
26 self._subject = subject
27 self._file_content = file_content
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090028 self._mergeable = mergeable
29 self._original_file_content = original_file_content or file_content
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090030
31 @property
32 def subject(self):
33 return self._subject
34
35 @property
36 def gerrit_number(self):
37 return self._gerrit_number
38
39 def GetFileContents(self, _path: str):
40 return self._file_content
41
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090042 def GetOriginalFileContents(self, _path: str):
43 return self._original_file_content
44
45 def IsMergeable(self):
46 return self._mergeable
47
48 def Rebase(self, allow_conflicts: bool = False):
49 pass
50
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090051
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070052# pylint: disable=protected-access
Alex Klein1699fab2022-09-08 08:46:06 -060053class ChromeLKGMCommitterTester(
54 cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase
55):
56 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080057
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090058 def testCommitNewLKGM(self):
59 """Tests that we can commit a new LKGM file."""
60 committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
61 "1001.0.0",
62 "main",
63 chromeos_version.VersionInfo("999.0.0"),
64 False,
Alex Klein1699fab2022-09-08 08:46:06 -060065 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070066
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090067 with mock.patch.object(committer._gerrit_helper, "CreateChange") as cg:
Alex Klein1699fab2022-09-08 08:46:06 -060068 cg.return_value = mock.MagicMock(gerrit_number=123456)
69 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090070 committer._gerrit_helper, "ChangeEdit"
Alex Klein1699fab2022-09-08 08:46:06 -060071 ) as ce:
72 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090073 committer._gerrit_helper, "SetReview"
Alex Klein1699fab2022-09-08 08:46:06 -060074 ) as bc:
75 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090076 committer._gerrit_helper, "SetHashtags"
Alex Klein1699fab2022-09-08 08:46:06 -060077 ):
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090078 committer.UpdateLKGM()
Alex Klein1699fab2022-09-08 08:46:06 -060079 ce.assert_called_once_with(
80 123456, "chromeos/CHROMEOS_LKGM", "1001.0.0"
81 )
82 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +000083 123456,
84 labels={"Bot-Commit": 1, "Commit-Queue": 2},
85 notify="NONE",
Yoshiki Iguchidad24252022-09-22 14:21:30 +090086 ready=True,
87 reviewers=[
88 "chrome-os-gardeners-reviews@google.com"
89 ],
Alex Klein1699fab2022-09-08 08:46:06 -060090 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070091
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090092 def testOlderLKGMFails(self):
Alex Klein1699fab2022-09-08 08:46:06 -060093 """Tests that trying to update to an older lkgm version fails."""
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +090094 committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
95 "1001.0.0",
96 "main",
97 chromeos_version.VersionInfo("1002.0.0"),
98 False,
99 )
100 with mock.patch.object(committer._gerrit_helper, "CreateChange") as cg:
Alex Klein1699fab2022-09-08 08:46:06 -0600101 cg.return_value = mock.MagicMock(gerrit_number=123456)
102 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900103 committer._gerrit_helper, "ChangeEdit"
Alex Klein1699fab2022-09-08 08:46:06 -0600104 ) as ce:
105 self.assertRaises(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900106 chrome_chromeos_lkgm.LKGMNotValid, committer.UpdateLKGM
Alex Klein1699fab2022-09-08 08:46:06 -0600107 )
108 ce.assert_not_called()
Ben Pastened3b93d42019-10-10 09:56:29 -0700109
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900110 def testAbandonObsoleteLKGMs(self):
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900111 """Tests that trying to abandon the obsolete lkgm CLs."""
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900112 cleaner = chrome_chromeos_lkgm.ChromeLKGMCleaner(
113 "main", chromeos_version.VersionInfo("10002.0.0"), "USER_EMAIL"
114 )
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900115
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900116 older_change = StubGerritChange(
117 3876550, "10001.0.0", "10001.0.0", mergeable=False
118 )
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900119 newer_change = StubGerritChange(3876551, "10003.0.0", "10003.0.0")
120 open_issues = [older_change, newer_change]
121
122 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900123 cleaner._gerrit_helper, "Query", return_value=open_issues
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900124 ) as mock_query:
125 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900126 cleaner._gerrit_helper, "AbandonChange"
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900127 ) as ac:
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900128 cleaner.ProcessObsoleteLKGMRolls()
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900129 mock_query.assert_called_once()
130 ac.assert_called_once_with((older_change), msg=mock.ANY)
131
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900132 def testRebaseObsoleteLKGMs(self):
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900133 """Tests that trying to abandon the obsolete lkgm CLs."""
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900134 cleaner = chrome_chromeos_lkgm.ChromeLKGMCleaner(
135 "main", chromeos_version.VersionInfo("10002.0.0"), "USER_EMAIL"
136 )
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900137
138 # LKGM Roll CL from "10001.0.0" to "10003.0.0" should be in the
139 # merge-conflict state, since the current LKGM version is "10002.0.0".
140 ROLL_FROM = "10001.0.0"
141 ROLL_TO = "10003.0.0"
142 GERRIT_NUM = 3876551
143 roll = StubGerritChange(
144 GERRIT_NUM,
145 ROLL_TO,
146 ROLL_TO,
147 mergeable=False,
148 original_file_content=ROLL_FROM,
149 )
150
151 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900152 cleaner._gerrit_helper, "Query", return_value=[roll]
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900153 ) as mock_query:
154 with mock.patch.object(roll, "Rebase") as rebase:
155 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900156 cleaner._gerrit_helper, "ChangeEdit"
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900157 ) as ce:
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900158 cleaner.ProcessObsoleteLKGMRolls()
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900159 mock_query.assert_called_once()
160
161 # Confirm that it does rebasing.
162 rebase.assert_called_once_with(allow_conflicts=True)
163 ce.assert_called_once_with(GERRIT_NUM, mock.ANY, ROLL_TO)
164
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900165 def testDoNothingObsoleteLKGMs(self):
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900166 """Tests that trying to abandon the obsolete lkgm CLs."""
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900167 cleaner = chrome_chromeos_lkgm.ChromeLKGMCleaner(
168 "main", chromeos_version.VersionInfo("10002.0.0"), "USER_EMAIL"
169 )
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900170
171 # LKGM Roll CL from "10002.0.0" to "10003.0.0" should NOT be in the
172 # merge-conflict state, since the current LKGM version is "10002.0.0".
173 ROLL_FROM = "10002.0.0"
174 ROLL_TO = "10003.0.0"
175 GERRIT_NUM = 3876551
176 # Even if mergeable=False, the logic should not do nothing.
177 roll = StubGerritChange(
178 GERRIT_NUM,
179 ROLL_TO,
180 ROLL_TO,
181 mergeable=False,
182 original_file_content=ROLL_FROM,
183 )
184
185 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900186 cleaner._gerrit_helper, "Query", return_value=[roll]
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900187 ) as mock_query:
188 with mock.patch.object(roll, "Rebase") as rebase:
189 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900190 cleaner._gerrit_helper, "ChangeEdit"
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900191 ) as ce:
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900192 cleaner.ProcessObsoleteLKGMRolls()
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900193 mock_query.assert_called_once()
194
195 # Confirm that it does nothing.
196 rebase.assert_not_called()
197 ce.assert_not_called()
198
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900199 def testVersionWithChromeBranch(self):
Alex Klein1699fab2022-09-08 08:46:06 -0600200 """Tests passing a version with a chrome branch strips the branch."""
201 branch = "refs/branch-heads/5000"
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900202 committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
203 "1003.0.0-rc2",
204 branch,
205 chromeos_version.VersionInfo("1002.0.0"),
206 False,
Alex Klein1699fab2022-09-08 08:46:06 -0600207 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700208
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900209 with mock.patch.object(committer._gerrit_helper, "CreateChange") as cg:
Alex Klein1699fab2022-09-08 08:46:06 -0600210 cg.return_value = mock.MagicMock(gerrit_number=123456)
211 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900212 committer._gerrit_helper, "ChangeEdit"
Alex Klein1699fab2022-09-08 08:46:06 -0600213 ) as ce:
214 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900215 committer._gerrit_helper, "SetReview"
Alex Klein1699fab2022-09-08 08:46:06 -0600216 ) as bc:
217 with mock.patch.object(
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900218 committer._gerrit_helper, "SetHashtags"
Alex Klein1699fab2022-09-08 08:46:06 -0600219 ):
220 # Check the file was actually written out correctly.
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900221 committer.UpdateLKGM()
Alex Klein1699fab2022-09-08 08:46:06 -0600222 cg.assert_called_once_with(
223 "chromium/src", branch, mock.ANY, False
224 )
225 ce.assert_called_once_with(
226 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0"
227 )
228 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +0000229 123456,
230 labels={"Bot-Commit": 1, "Commit-Queue": 2},
231 notify="NONE",
Yoshiki Iguchidad24252022-09-22 14:21:30 +0900232 ready=True,
233 reviewers=[
234 "chrome-os-gardeners-reviews@google.com"
235 ],
Alex Klein1699fab2022-09-08 08:46:06 -0600236 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700237
Alex Klein1699fab2022-09-08 08:46:06 -0600238 def testCommitMsg(self):
239 """Tests format of the commit message."""
Yoshiki Iguchid5bc7922022-09-22 14:37:48 +0900240 committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
241 "1001.0.0",
242 "main",
243 chromeos_version.VersionInfo("999.0.0"),
244 False,
245 buildbucket_id="some-build-id",
246 )
247
248 committer._PRESUBMIT_BOTS = ["bot1", "bot2"]
249 commit_msg_lines = committer.ComposeCommitMsg().splitlines()
Alex Klein1699fab2022-09-08 08:46:06 -0600250 self.assertIn(
251 "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines
252 )
253 self.assertIn(
254 "Uploaded by https://ci.chromium.org/b/some-build-id",
255 commit_msg_lines,
256 )
257 self.assertIn(
258 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines
259 )
260 self.assertIn(
261 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines
262 )