Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2017 The ChromiumOS Authors |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 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 Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 7 | from unittest import mock |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 8 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 9 | from chromite.lib import cros_test_lib |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 10 | from chromite.scripts import chrome_chromeos_lkgm |
| 11 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 12 | |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 13 | class StubGerritChange: |
| 14 | """Stab class corresponding to cros_patch.GerritChange.""" |
| 15 | |
Yoshiki Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 16 | def __init__( |
| 17 | self, |
| 18 | gerrit_number, |
| 19 | file_content, |
| 20 | subject, |
| 21 | mergeable=True, |
| 22 | original_file_content=None, |
| 23 | ): |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 24 | self._gerrit_number = gerrit_number |
| 25 | self._subject = subject |
| 26 | self._file_content = file_content |
Yoshiki Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 27 | self._mergeable = mergeable |
| 28 | self._original_file_content = original_file_content or file_content |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 29 | |
| 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 Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 41 | 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 Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 50 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 51 | # pylint: disable=protected-access |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | class ChromeLKGMCommitterTester( |
| 53 | cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase |
| 54 | ): |
| 55 | """Test cros_chromeos_lkgm.Committer.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | 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 Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 62 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | @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 Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 67 | with mock.patch.object( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | 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 Neus | 624c8a5 | 2022-09-13 17:33:03 +0000 | [diff] [blame] | 85 | 123456, |
| 86 | labels={"Bot-Commit": 1, "Commit-Queue": 2}, |
| 87 | notify="NONE", |
Yoshiki Iguchi | dad2425 | 2022-09-22 14:21:30 +0900 | [diff] [blame] | 88 | ready=True, |
| 89 | reviewers=[ |
| 90 | "chrome-os-gardeners-reviews@google.com" |
| 91 | ], |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | ) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | @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 Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 98 | with mock.patch.object( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | 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 Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | @mock.patch("chromite.lib.gob_util.GetFileContents") |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 111 | 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 Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 115 | older_change = StubGerritChange( |
| 116 | 3876550, "10001.0.0", "10001.0.0", mergeable=False |
| 117 | ) |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 118 | 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 Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 127 | self.committer.ProcessObsoleteLKGMRolls() |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 128 | 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 Iguchi | ab2c4c2 | 2022-09-22 18:43:21 +0900 | [diff] [blame] | 132 | 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 Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | 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 Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | 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 Neus | 624c8a5 | 2022-09-13 17:33:03 +0000 | [diff] [blame] | 227 | 123456, |
| 228 | labels={"Bot-Commit": 1, "Commit-Queue": 2}, |
| 229 | notify="NONE", |
Yoshiki Iguchi | dad2425 | 2022-09-22 14:21:30 +0900 | [diff] [blame] | 230 | ready=True, |
| 231 | reviewers=[ |
| 232 | "chrome-os-gardeners-reviews@google.com" |
| 233 | ], |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 234 | ) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 235 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | 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 | ) |