Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 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 |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 15 | class ChromeLKGMCommitterTester(cros_test_lib.RunCommandTestCase, |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 16 | cros_test_lib.MockTempDirTestCase): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 17 | """Test cros_chromeos_lkgm.Committer.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 18 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 19 | def setUp(self): |
| 20 | """Common set up method for all tests.""" |
Ben Pastene | 3ae39bd | 2022-01-04 15:20:05 -0800 | [diff] [blame] | 21 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter('1001.0.0') |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 22 | self.old_lkgm = None |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 23 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 24 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 25 | def testCommitNewLKGM(self, mock_get_file): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 26 | """Tests that we can commit a new LKGM file.""" |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 27 | mock_get_file.return_value = '999.0.0' |
| 28 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 29 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 30 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
Fergus Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 31 | with mock.patch.object( |
| 32 | self.committer._gerrit_helper, 'SetReview') as bc: |
Ben Pastene | 5b1d119 | 2022-01-27 18:09:34 -0800 | [diff] [blame^] | 33 | with mock.patch.object(self.committer._gerrit_helper, 'SetHashtags'): |
| 34 | self.committer.UpdateLKGM() |
| 35 | ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM', |
| 36 | '1001.0.0') |
| 37 | bc.assert_called_once_with(123456, labels={'Bot-Commit': 1}, |
| 38 | notify='NONE') |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 39 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 40 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 41 | def testOlderLKGMFails(self, mock_get_file): |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 42 | """Tests that trying to update to an older lkgm version fails.""" |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 43 | mock_get_file.return_value = '1002.0.0' |
| 44 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 45 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 46 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
| 47 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid, |
| 48 | self.committer.UpdateLKGM) |
| 49 | ce.assert_not_called() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 50 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 51 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 52 | def testVersionWithChromeBranch(self, mock_get_file): |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 53 | """Tests passing a version with a chrome branch strips the branch.""" |
Ben Pastene | 3ae39bd | 2022-01-04 15:20:05 -0800 | [diff] [blame] | 54 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter('1003.0.0-rc2') |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 55 | mock_get_file.return_value = '1002.0.0' |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 56 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 57 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 58 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 59 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
Fergus Dall | 64a21b6 | 2022-01-07 13:50:26 +1100 | [diff] [blame] | 60 | with mock.patch.object( |
| 61 | self.committer._gerrit_helper, 'SetReview') as bc: |
Ben Pastene | 5b1d119 | 2022-01-27 18:09:34 -0800 | [diff] [blame^] | 62 | with mock.patch.object(self.committer._gerrit_helper, 'SetHashtags'): |
| 63 | # Check the file was actually written out correctly. |
| 64 | self.committer.UpdateLKGM() |
| 65 | ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM', |
| 66 | '1003.0.0') |
| 67 | bc.assert_called_once_with(123456, labels={'Bot-Commit': 1}, |
| 68 | notify='NONE') |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 69 | |
| 70 | def testCommitMsg(self): |
| 71 | """Tests format of the commit message.""" |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 72 | self.committer._PRESUBMIT_BOTS = ['bot1', 'bot2'] |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 73 | self.committer._buildbucket_id = 'some-build-id' |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 74 | commit_msg_lines = self.committer.ComposeCommitMsg().splitlines() |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame] | 75 | self.assertIn( |
| 76 | 'Automated Commit: LKGM 1001.0.0 for chromeos.', commit_msg_lines) |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 77 | self.assertIn( |
| 78 | 'Uploaded by https://ci.chromium.org/b/some-build-id', commit_msg_lines) |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 79 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1', commit_msg_lines) |
| 80 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2', commit_msg_lines) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 81 | |
| 82 | def testFindAlreadyOpenLKGMRoll(self): |
| 83 | already_open_issues = [123456] |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 84 | self.committer._commit_msg_header = 'A message with spaces' |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 85 | with mock.patch.object( |
| 86 | self.committer._gerrit_helper, 'Query', |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 87 | return_value=already_open_issues) as mock_query: |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 88 | self.assertEqual( |
| 89 | self.committer.FindAlreadyOpenLKGMRoll(), |
| 90 | already_open_issues[0]) |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 91 | escaped_quotes = urllib.parse.quote('"') |
| 92 | message = mock_query.call_args.kwargs['message'] |
| 93 | self.assertEqual(message.count(escaped_quotes), 2) |
| 94 | self.assertTrue(message.startswith(escaped_quotes)) |
| 95 | self.assertTrue(message.endswith(escaped_quotes)) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 96 | already_open_issues = [123456, 654321] |
| 97 | with mock.patch.object( |
| 98 | self.committer._gerrit_helper, 'Query', |
| 99 | return_value=already_open_issues): |
| 100 | self.assertRaises( |
| 101 | chrome_chromeos_lkgm.LKGMNotValid, |
| 102 | self.committer.FindAlreadyOpenLKGMRoll) |
| 103 | |
| 104 | def testSubmitToCQ(self): |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 105 | self.committer._buildbucket_id = 'some-build-id' |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 106 | already_open_issue = 123456 |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 107 | with mock.patch.object( |
| 108 | self.committer._gerrit_helper, 'SetReview') as mock_review: |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 109 | self.committer.SubmitToCQ(already_open_issue) |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 110 | self.assertIn( |
| 111 | self.committer._buildbucket_id, mock_review.call_args[1]['msg']) |