blob: eae45d59b83d3c763bac9addff397de32f0b731c [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
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07009from chromite.lib import cros_test_lib
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070010from chromite.scripts import chrome_chromeos_lkgm
11
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080012
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090013class StubGerritChange:
14 """Stab class corresponding to cros_patch.GerritChange."""
15
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090016 def __init__(
17 self,
18 gerrit_number,
19 file_content,
20 subject,
21 mergeable=True,
22 original_file_content=None,
23 ):
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090024 self._gerrit_number = gerrit_number
25 self._subject = subject
26 self._file_content = file_content
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090027 self._mergeable = mergeable
28 self._original_file_content = original_file_content or file_content
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090029
30 @property
31 def subject(self):
32 return self._subject
33
34 @property
35 def gerrit_number(self):
36 return self._gerrit_number
37
38 def GetFileContents(self, _path: str):
39 return self._file_content
40
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +090041 def GetOriginalFileContents(self, _path: str):
42 return self._original_file_content
43
44 def IsMergeable(self):
45 return self._mergeable
46
47 def Rebase(self, allow_conflicts: bool = False):
48 pass
49
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090050
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070051# pylint: disable=protected-access
Alex Klein1699fab2022-09-08 08:46:06 -060052class ChromeLKGMCommitterTester(
53 cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase
54):
55 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080056
Alex Klein1699fab2022-09-08 08:46:06 -060057 def setUp(self):
58 """Common set up method for all tests."""
59 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
60 "1001.0.0", "main"
61 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070062
Alex Klein1699fab2022-09-08 08:46:06 -060063 @mock.patch("chromite.lib.gob_util.GetFileContents")
64 def testCommitNewLKGM(self, mock_get_file):
65 """Tests that we can commit a new LKGM file."""
66 mock_get_file.return_value = "999.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110067 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060068 self.committer._gerrit_helper, "CreateChange"
69 ) as cg:
70 cg.return_value = mock.MagicMock(gerrit_number=123456)
71 with mock.patch.object(
72 self.committer._gerrit_helper, "ChangeEdit"
73 ) as ce:
74 with mock.patch.object(
75 self.committer._gerrit_helper, "SetReview"
76 ) as bc:
77 with mock.patch.object(
78 self.committer._gerrit_helper, "SetHashtags"
79 ):
80 self.committer.UpdateLKGM()
81 ce.assert_called_once_with(
82 123456, "chromeos/CHROMEOS_LKGM", "1001.0.0"
83 )
84 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +000085 123456,
86 labels={"Bot-Commit": 1, "Commit-Queue": 2},
87 notify="NONE",
Yoshiki Iguchidad24252022-09-22 14:21:30 +090088 ready=True,
89 reviewers=[
90 "chrome-os-gardeners-reviews@google.com"
91 ],
Alex Klein1699fab2022-09-08 08:46:06 -060092 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 @mock.patch("chromite.lib.gob_util.GetFileContents")
95 def testOlderLKGMFails(self, mock_get_file):
96 """Tests that trying to update to an older lkgm version fails."""
97 mock_get_file.return_value = "1002.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110098 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060099 self.committer._gerrit_helper, "CreateChange"
100 ) as cg:
101 cg.return_value = mock.MagicMock(gerrit_number=123456)
102 with mock.patch.object(
103 self.committer._gerrit_helper, "ChangeEdit"
104 ) as ce:
105 self.assertRaises(
106 chrome_chromeos_lkgm.LKGMNotValid, self.committer.UpdateLKGM
107 )
108 ce.assert_not_called()
Ben Pastened3b93d42019-10-10 09:56:29 -0700109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 @mock.patch("chromite.lib.gob_util.GetFileContents")
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900111 def testAbandonObsoleteLKGMs(self, mock_get_file):
112 """Tests that trying to abandon the obsolete lkgm CLs."""
113 mock_get_file.return_value = "10002.0.0"
114
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900115 older_change = StubGerritChange(
116 3876550, "10001.0.0", "10001.0.0", mergeable=False
117 )
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900118 newer_change = StubGerritChange(3876551, "10003.0.0", "10003.0.0")
119 open_issues = [older_change, newer_change]
120
121 with mock.patch.object(
122 self.committer._gerrit_helper, "Query", return_value=open_issues
123 ) as mock_query:
124 with mock.patch.object(
125 self.committer._gerrit_helper, "AbandonChange"
126 ) as ac:
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900127 self.committer.ProcessObsoleteLKGMRolls()
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +0900128 mock_query.assert_called_once()
129 ac.assert_called_once_with((older_change), msg=mock.ANY)
130
131 @mock.patch("chromite.lib.gob_util.GetFileContents")
Yoshiki Iguchiab2c4c22022-09-22 18:43:21 +0900132 def testRebaseObsoleteLKGMs(self, mock_get_file):
133 """Tests that trying to abandon the obsolete lkgm CLs."""
134 mock_get_file.return_value = "10002.0.0"
135
136 # LKGM Roll CL from "10001.0.0" to "10003.0.0" should be in the
137 # merge-conflict state, since the current LKGM version is "10002.0.0".
138 ROLL_FROM = "10001.0.0"
139 ROLL_TO = "10003.0.0"
140 GERRIT_NUM = 3876551
141 roll = StubGerritChange(
142 GERRIT_NUM,
143 ROLL_TO,
144 ROLL_TO,
145 mergeable=False,
146 original_file_content=ROLL_FROM,
147 )
148
149 with mock.patch.object(
150 self.committer._gerrit_helper, "Query", return_value=[roll]
151 ) as mock_query:
152 with mock.patch.object(roll, "Rebase") as rebase:
153 with mock.patch.object(
154 self.committer._gerrit_helper, "ChangeEdit"
155 ) as ce:
156 self.committer.ProcessObsoleteLKGMRolls()
157 mock_query.assert_called_once()
158
159 # Confirm that it does rebasing.
160 rebase.assert_called_once_with(allow_conflicts=True)
161 ce.assert_called_once_with(GERRIT_NUM, mock.ANY, ROLL_TO)
162
163 @mock.patch("chromite.lib.gob_util.GetFileContents")
164 def testDoNothingObsoleteLKGMs(self, mock_get_file):
165 """Tests that trying to abandon the obsolete lkgm CLs."""
166 mock_get_file.return_value = "10002.0.0"
167
168 # LKGM Roll CL from "10002.0.0" to "10003.0.0" should NOT be in the
169 # merge-conflict state, since the current LKGM version is "10002.0.0".
170 ROLL_FROM = "10002.0.0"
171 ROLL_TO = "10003.0.0"
172 GERRIT_NUM = 3876551
173 # Even if mergeable=False, the logic should not do nothing.
174 roll = StubGerritChange(
175 GERRIT_NUM,
176 ROLL_TO,
177 ROLL_TO,
178 mergeable=False,
179 original_file_content=ROLL_FROM,
180 )
181
182 with mock.patch.object(
183 self.committer._gerrit_helper, "Query", return_value=[roll]
184 ) as mock_query:
185 with mock.patch.object(roll, "Rebase") as rebase:
186 with mock.patch.object(
187 self.committer._gerrit_helper, "ChangeEdit"
188 ) as ce:
189 self.committer.ProcessObsoleteLKGMRolls()
190 mock_query.assert_called_once()
191
192 # Confirm that it does nothing.
193 rebase.assert_not_called()
194 ce.assert_not_called()
195
196 @mock.patch("chromite.lib.gob_util.GetFileContents")
Alex Klein1699fab2022-09-08 08:46:06 -0600197 def testVersionWithChromeBranch(self, mock_get_file):
198 """Tests passing a version with a chrome branch strips the branch."""
199 branch = "refs/branch-heads/5000"
200 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
201 "1003.0.0-rc2", branch
202 )
203 mock_get_file.return_value = "1002.0.0"
Ben Pastene6a8361a2021-08-19 19:52:05 -0700204
Alex Klein1699fab2022-09-08 08:46:06 -0600205 with mock.patch.object(
206 self.committer._gerrit_helper, "CreateChange"
207 ) as cg:
208 cg.return_value = mock.MagicMock(gerrit_number=123456)
209 with mock.patch.object(
210 self.committer._gerrit_helper, "ChangeEdit"
211 ) as ce:
212 with mock.patch.object(
213 self.committer._gerrit_helper, "SetReview"
214 ) as bc:
215 with mock.patch.object(
216 self.committer._gerrit_helper, "SetHashtags"
217 ):
218 # Check the file was actually written out correctly.
219 self.committer.UpdateLKGM()
220 cg.assert_called_once_with(
221 "chromium/src", branch, mock.ANY, False
222 )
223 ce.assert_called_once_with(
224 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0"
225 )
226 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +0000227 123456,
228 labels={"Bot-Commit": 1, "Commit-Queue": 2},
229 notify="NONE",
Yoshiki Iguchidad24252022-09-22 14:21:30 +0900230 ready=True,
231 reviewers=[
232 "chrome-os-gardeners-reviews@google.com"
233 ],
Alex Klein1699fab2022-09-08 08:46:06 -0600234 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700235
Alex Klein1699fab2022-09-08 08:46:06 -0600236 def testCommitMsg(self):
237 """Tests format of the commit message."""
238 self.committer._PRESUBMIT_BOTS = ["bot1", "bot2"]
239 self.committer._buildbucket_id = "some-build-id"
240 commit_msg_lines = self.committer.ComposeCommitMsg().splitlines()
241 self.assertIn(
242 "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines
243 )
244 self.assertIn(
245 "Uploaded by https://ci.chromium.org/b/some-build-id",
246 commit_msg_lines,
247 )
248 self.assertIn(
249 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines
250 )
251 self.assertIn(
252 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines
253 )