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