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 | |
| 16 | def __init__(self, gerrit_number, file_content, subject): |
| 17 | self._gerrit_number = gerrit_number |
| 18 | self._subject = subject |
| 19 | self._file_content = file_content |
| 20 | |
| 21 | @property |
| 22 | def subject(self): |
| 23 | return self._subject |
| 24 | |
| 25 | @property |
| 26 | def gerrit_number(self): |
| 27 | return self._gerrit_number |
| 28 | |
| 29 | def GetFileContents(self, _path: str): |
| 30 | return self._file_content |
| 31 | |
| 32 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 33 | # pylint: disable=protected-access |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | class ChromeLKGMCommitterTester( |
| 35 | cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase |
| 36 | ): |
| 37 | """Test cros_chromeos_lkgm.Committer.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | def setUp(self): |
| 40 | """Common set up method for all tests.""" |
| 41 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 42 | "1001.0.0", "main" |
| 43 | ) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | @mock.patch("chromite.lib.gob_util.GetFileContents") |
| 46 | def testCommitNewLKGM(self, mock_get_file): |
| 47 | """Tests that we can commit a new LKGM file.""" |
| 48 | mock_get_file.return_value = "999.0.0" |
Fergus Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 49 | with mock.patch.object( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | self.committer._gerrit_helper, "CreateChange" |
| 51 | ) as cg: |
| 52 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 53 | with mock.patch.object( |
| 54 | self.committer._gerrit_helper, "ChangeEdit" |
| 55 | ) as ce: |
| 56 | with mock.patch.object( |
| 57 | self.committer._gerrit_helper, "SetReview" |
| 58 | ) as bc: |
| 59 | with mock.patch.object( |
| 60 | self.committer._gerrit_helper, "SetHashtags" |
| 61 | ): |
| 62 | self.committer.UpdateLKGM() |
| 63 | ce.assert_called_once_with( |
| 64 | 123456, "chromeos/CHROMEOS_LKGM", "1001.0.0" |
| 65 | ) |
| 66 | bc.assert_called_once_with( |
Jack Neus | 624c8a5 | 2022-09-13 17:33:03 +0000 | [diff] [blame] | 67 | 123456, |
| 68 | labels={"Bot-Commit": 1, "Commit-Queue": 2}, |
| 69 | notify="NONE", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | ) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | @mock.patch("chromite.lib.gob_util.GetFileContents") |
| 73 | def testOlderLKGMFails(self, mock_get_file): |
| 74 | """Tests that trying to update to an older lkgm version fails.""" |
| 75 | mock_get_file.return_value = "1002.0.0" |
Fergus Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 76 | with mock.patch.object( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | self.committer._gerrit_helper, "CreateChange" |
| 78 | ) as cg: |
| 79 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 80 | with mock.patch.object( |
| 81 | self.committer._gerrit_helper, "ChangeEdit" |
| 82 | ) as ce: |
| 83 | self.assertRaises( |
| 84 | chrome_chromeos_lkgm.LKGMNotValid, self.committer.UpdateLKGM |
| 85 | ) |
| 86 | ce.assert_not_called() |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | @mock.patch("chromite.lib.gob_util.GetFileContents") |
Yoshiki Iguchi | f8ab011 | 2022-09-15 16:57:26 +0900 | [diff] [blame] | 89 | def testAbandonObsoleteLKGMs(self, mock_get_file): |
| 90 | """Tests that trying to abandon the obsolete lkgm CLs.""" |
| 91 | mock_get_file.return_value = "10002.0.0" |
| 92 | |
| 93 | older_change = StubGerritChange(3876550, "10001.0.0", "10001.0.0") |
| 94 | newer_change = StubGerritChange(3876551, "10003.0.0", "10003.0.0") |
| 95 | open_issues = [older_change, newer_change] |
| 96 | |
| 97 | with mock.patch.object( |
| 98 | self.committer._gerrit_helper, "Query", return_value=open_issues |
| 99 | ) as mock_query: |
| 100 | with mock.patch.object( |
| 101 | self.committer._gerrit_helper, "AbandonChange" |
| 102 | ) as ac: |
| 103 | self.committer.AbandonObsoleteLKGMRolls() |
| 104 | mock_query.assert_called_once() |
| 105 | ac.assert_called_once_with((older_change), msg=mock.ANY) |
| 106 | |
| 107 | @mock.patch("chromite.lib.gob_util.GetFileContents") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | def testVersionWithChromeBranch(self, mock_get_file): |
| 109 | """Tests passing a version with a chrome branch strips the branch.""" |
| 110 | branch = "refs/branch-heads/5000" |
| 111 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 112 | "1003.0.0-rc2", branch |
| 113 | ) |
| 114 | mock_get_file.return_value = "1002.0.0" |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | with mock.patch.object( |
| 117 | self.committer._gerrit_helper, "CreateChange" |
| 118 | ) as cg: |
| 119 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 120 | with mock.patch.object( |
| 121 | self.committer._gerrit_helper, "ChangeEdit" |
| 122 | ) as ce: |
| 123 | with mock.patch.object( |
| 124 | self.committer._gerrit_helper, "SetReview" |
| 125 | ) as bc: |
| 126 | with mock.patch.object( |
| 127 | self.committer._gerrit_helper, "SetHashtags" |
| 128 | ): |
| 129 | # Check the file was actually written out correctly. |
| 130 | self.committer.UpdateLKGM() |
| 131 | cg.assert_called_once_with( |
| 132 | "chromium/src", branch, mock.ANY, False |
| 133 | ) |
| 134 | ce.assert_called_once_with( |
| 135 | 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0" |
| 136 | ) |
| 137 | bc.assert_called_once_with( |
Jack Neus | 624c8a5 | 2022-09-13 17:33:03 +0000 | [diff] [blame] | 138 | 123456, |
| 139 | labels={"Bot-Commit": 1, "Commit-Queue": 2}, |
| 140 | notify="NONE", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | ) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 142 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | def testCommitMsg(self): |
| 144 | """Tests format of the commit message.""" |
| 145 | self.committer._PRESUBMIT_BOTS = ["bot1", "bot2"] |
| 146 | self.committer._buildbucket_id = "some-build-id" |
| 147 | commit_msg_lines = self.committer.ComposeCommitMsg().splitlines() |
| 148 | self.assertIn( |
| 149 | "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines |
| 150 | ) |
| 151 | self.assertIn( |
| 152 | "Uploaded by https://ci.chromium.org/b/some-build-id", |
| 153 | commit_msg_lines, |
| 154 | ) |
| 155 | self.assertIn( |
| 156 | "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines |
| 157 | ) |
| 158 | self.assertIn( |
| 159 | "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines |
| 160 | ) |