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