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 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 7 | import os |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 8 | from unittest import mock |
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 constants |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 11 | from chromite.lib import cros_test_lib |
| 12 | from chromite.lib import osutils |
| 13 | from chromite.lib import partial_mock |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 14 | from chromite.scripts import chrome_chromeos_lkgm |
| 15 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 16 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 17 | # pylint: disable=protected-access |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 18 | class ChromeLKGMCommitterTester(cros_test_lib.RunCommandTestCase, |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 19 | cros_test_lib.MockTempDirTestCase): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 20 | """Test cros_chromeos_lkgm.Committer.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 21 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 22 | def setUp(self): |
| 23 | """Common set up method for all tests.""" |
| 24 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Achuith Bhandarkar | b6f4025 | 2019-12-09 16:31:34 -0800 | [diff] [blame] | 25 | 'user@test.org', self.tempdir, '1001.0.0') |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 26 | self.lkgm_file = os.path.join(self.tempdir, constants.PATH_TO_CHROME_LKGM) |
| 27 | self.old_lkgm = None |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 28 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 29 | def _createOldLkgm(self, *args, **kwargs): # pylint: disable=unused-argument |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 30 | # Write out an old lkgm file as if we got it from a git fetch. |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 31 | osutils.SafeMakedirs(os.path.join(self.tempdir, '.git', 'info')) |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 32 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 33 | osutils.WriteFile(self.lkgm_file, self.old_lkgm) |
| 34 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 35 | def testCheckoutChromeLKGM(self): |
Mike Frysinger | 3ef6d97 | 2019-08-24 20:07:42 -0400 | [diff] [blame] | 36 | """Tests that we can read/obtain the old LKGM from mocked out git.""" |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 37 | self.old_lkgm = '1234.0.0' |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 38 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 39 | side_effect=self._createOldLkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 40 | self.committer.CheckoutChrome() |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 41 | |
| 42 | self.assertEqual(self.committer.lkgm_file, self.lkgm_file) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 43 | self.assertEqual(osutils.ReadFile(self.lkgm_file), self.old_lkgm) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 44 | |
| 45 | def testCommitNewLKGM(self): |
| 46 | """Tests that we can commit a new LKGM file.""" |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 47 | self.old_lkgm = '999.0.0' |
| 48 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
| 49 | side_effect=self._createOldLkgm) |
| 50 | self.committer.CheckoutChrome() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 51 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 52 | self.assertEqual(self.committer.lkgm_file, self.lkgm_file) |
| 53 | |
| 54 | self.committer.UpdateLKGM() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 55 | self.committer.CommitNewLKGM() |
| 56 | |
| 57 | # Check the file was actually written out correctly. |
| 58 | self.assertEqual(osutils.ReadFile(self.lkgm_file), self.committer._lkgm) |
| 59 | self.assertCommandContains(['git', 'commit']) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 60 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 61 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 62 | def testOlderLKGMFails(self): |
| 63 | """Tests that trying to update to an older lkgm version fails.""" |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 64 | self.old_lkgm = '1002.0.0' |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 65 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 66 | side_effect=self._createOldLkgm) |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 67 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 68 | self.committer.CheckoutChrome() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 69 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 70 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid, |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 71 | self.committer.UpdateLKGM) |
| 72 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
| 73 | self.assertEqual(self.committer._lkgm, '1001.0.0') |
| 74 | self.assertEqual(osutils.ReadFile(self.lkgm_file), '1002.0.0') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 75 | |
| 76 | def testVersionWithChromeBranch(self): |
| 77 | """Tests passing a version with a chrome branch strips the branch.""" |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 78 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Achuith Bhandarkar | b6f4025 | 2019-12-09 16:31:34 -0800 | [diff] [blame] | 79 | 'user@test.org', self.tempdir, '1003.0.0-rc2') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 80 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 81 | self.old_lkgm = '1002.0.0' |
| 82 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
| 83 | side_effect=self._createOldLkgm) |
| 84 | |
| 85 | self.committer.CheckoutChrome() |
| 86 | self.committer.UpdateLKGM() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 87 | self.committer.CommitNewLKGM() |
| 88 | |
| 89 | # Check the file was actually written out correctly. |
| 90 | stripped_lkgm = '1003.0.0' |
| 91 | self.assertEqual(osutils.ReadFile(self.lkgm_file), stripped_lkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 92 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 93 | |
| 94 | def testCommitMsg(self): |
| 95 | """Tests format of the commit message.""" |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 96 | self.committer._PRESUBMIT_BOTS = ['bot1', 'bot2'] |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 97 | self.committer._buildbucket_id = 'some-build-id' |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 98 | commit_msg_lines = self.committer.ComposeCommitMsg().splitlines() |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 99 | self.assertIn('LKGM 1001.0.0 for chromeos.', commit_msg_lines) |
Ben Pastene | f0fb062 | 2021-09-24 23:38:13 +0000 | [diff] [blame] | 100 | self.assertIn( |
| 101 | '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] | 102 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1', commit_msg_lines) |
| 103 | self.assertIn('CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2', commit_msg_lines) |
Ben Pastene | 6a8361a | 2021-08-19 19:52:05 -0700 | [diff] [blame] | 104 | |
| 105 | def testFindAlreadyOpenLKGMRoll(self): |
| 106 | already_open_issues = [123456] |
| 107 | with mock.patch.object( |
| 108 | self.committer._gerrit_helper, 'Query', |
| 109 | return_value=already_open_issues): |
| 110 | self.assertEqual( |
| 111 | self.committer.FindAlreadyOpenLKGMRoll(), |
| 112 | already_open_issues[0]) |
| 113 | already_open_issues = [123456, 654321] |
| 114 | with mock.patch.object( |
| 115 | self.committer._gerrit_helper, 'Query', |
| 116 | return_value=already_open_issues): |
| 117 | self.assertRaises( |
| 118 | chrome_chromeos_lkgm.LKGMNotValid, |
| 119 | self.committer.FindAlreadyOpenLKGMRoll) |
| 120 | |
| 121 | def testSubmitToCQ(self): |
| 122 | already_open_issue = 123456 |
| 123 | with mock.patch.object(self.committer._gerrit_helper, 'SetReview'): |
| 124 | self.committer.SubmitToCQ(already_open_issue) |