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.""" |
| 21 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 22 | 'user@test.org', '1001.0.0') |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 23 | self.old_lkgm = None |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 24 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 25 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 26 | def testCommitNewLKGM(self, mock_get_file): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 27 | """Tests that we can commit a new LKGM file.""" |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 28 | mock_get_file.return_value = '999.0.0' |
| 29 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 30 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 31 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
| 32 | self.committer.UpdateLKGM() |
| 33 | ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM', '1001.0.0') |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 34 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 35 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 36 | def testOlderLKGMFails(self, mock_get_file): |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 37 | """Tests that trying to update to an older lkgm version fails.""" |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 38 | mock_get_file.return_value = '1002.0.0' |
| 39 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 40 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 41 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
| 42 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid, |
| 43 | self.committer.UpdateLKGM) |
| 44 | ce.assert_not_called() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 45 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 46 | @mock.patch('chromite.lib.gob_util.GetFileContentsOnHead') |
| 47 | def testVersionWithChromeBranch(self, mock_get_file): |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 48 | """Tests passing a version with a chrome branch strips the branch.""" |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 49 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 50 | 'user@test.org', '1003.0.0-rc2') |
| 51 | mock_get_file.return_value = '1002.0.0' |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 52 | |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 53 | with mock.patch.object(self.committer._gerrit_helper, 'CreateChange') as cg: |
| 54 | cg.return_value = mock.MagicMock(gerrit_number=123456) |
| 55 | with mock.patch.object(self.committer._gerrit_helper, 'ChangeEdit') as ce: |
| 56 | # Check the file was actually written out correctly. |
| 57 | self.committer.UpdateLKGM() |
| 58 | ce.assert_called_once_with(123456, 'chromeos/CHROMEOS_LKGM', '1003.0.0') |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 59 | |
| 60 | def testCommitMsg(self): |
| 61 | """Tests format of the commit message.""" |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 62 | self.committer._PRESUBMIT_BOTS = ['bot1', 'bot2'] |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 63 | self.committer._buildbucket_id = 'some-build-id' |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 64 | commit_msg_lines = self.committer.ComposeCommitMsg().splitlines() |
Ben Pastene | 769b121 | 2021-12-14 18:26:50 -0800 | [diff] [blame^] | 65 | self.assertIn( |
| 66 | 'Automated Commit: LKGM 1001.0.0 for chromeos.', commit_msg_lines) |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 67 | self.assertIn( |
| 68 | '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] | 69 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1', commit_msg_lines) |
| 70 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2', commit_msg_lines) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 71 | |
| 72 | def testFindAlreadyOpenLKGMRoll(self): |
| 73 | already_open_issues = [123456] |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 74 | self.committer._commit_msg_header = 'A message with spaces' |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 75 | with mock.patch.object( |
| 76 | self.committer._gerrit_helper, 'Query', |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 77 | return_value=already_open_issues) as mock_query: |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 78 | self.assertEqual( |
| 79 | self.committer.FindAlreadyOpenLKGMRoll(), |
| 80 | already_open_issues[0]) |
Ben Pastene | ca7c71b | 2021-10-07 18:34:27 -0700 | [diff] [blame] | 81 | escaped_quotes = urllib.parse.quote('"') |
| 82 | message = mock_query.call_args.kwargs['message'] |
| 83 | self.assertEqual(message.count(escaped_quotes), 2) |
| 84 | self.assertTrue(message.startswith(escaped_quotes)) |
| 85 | self.assertTrue(message.endswith(escaped_quotes)) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 86 | already_open_issues = [123456, 654321] |
| 87 | with mock.patch.object( |
| 88 | self.committer._gerrit_helper, 'Query', |
| 89 | return_value=already_open_issues): |
| 90 | self.assertRaises( |
| 91 | chrome_chromeos_lkgm.LKGMNotValid, |
| 92 | self.committer.FindAlreadyOpenLKGMRoll) |
| 93 | |
| 94 | def testSubmitToCQ(self): |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 95 | self.committer._buildbucket_id = 'some-build-id' |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 96 | already_open_issue = 123456 |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 97 | with mock.patch.object( |
| 98 | self.committer._gerrit_helper, 'SetReview') as mock_review: |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 99 | self.committer.SubmitToCQ(already_open_issue) |
Ben Pastene | 33da0c2 | 2021-11-29 21:14:05 -0800 | [diff] [blame] | 100 | self.assertIn( |
| 101 | self.committer._buildbucket_id, mock_review.call_args[1]['msg']) |