Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for the chrome_chromeos_lkgm program.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 11 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 12 | from chromite.lib import constants |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.lib import osutils |
| 15 | from chromite.lib import partial_mock |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 16 | from chromite.scripts import chrome_chromeos_lkgm |
| 17 | |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 18 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 19 | # pylint: disable=protected-access |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 20 | class ChromeLKGMCommitterTester(cros_test_lib.RunCommandTestCase, |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 21 | cros_test_lib.MockTempDirTestCase): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 22 | """Test cros_chromeos_lkgm.Committer.""" |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 23 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 24 | def setUp(self): |
| 25 | """Common set up method for all tests.""" |
| 26 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Achuith Bhandarkar | b6f4025 | 2019-12-09 16:31:34 -0800 | [diff] [blame^] | 27 | 'user@test.org', self.tempdir, '1001.0.0') |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 28 | self.lkgm_file = os.path.join(self.tempdir, constants.PATH_TO_CHROME_LKGM) |
| 29 | self.old_lkgm = None |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 30 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 31 | def _createOldLkgm(self, *args, **kwargs): # pylint: disable=unused-argument |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 32 | # 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] | 33 | osutils.SafeMakedirs(os.path.join(self.tempdir, '.git', 'info')) |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 34 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 35 | osutils.WriteFile(self.lkgm_file, self.old_lkgm) |
| 36 | |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 37 | def testCheckoutChromeLKGM(self): |
Mike Frysinger | 3ef6d97 | 2019-08-24 20:07:42 -0400 | [diff] [blame] | 38 | """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] | 39 | self.old_lkgm = '1234.0.0' |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 40 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 41 | side_effect=self._createOldLkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 42 | self.committer.CheckoutChrome() |
Achuith Bhandarkar | ec8d7a7 | 2018-03-01 16:56:22 -0800 | [diff] [blame] | 43 | |
| 44 | self.assertEqual(self.committer.lkgm_file, self.lkgm_file) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 45 | self.assertEqual(osutils.ReadFile(self.lkgm_file), self.old_lkgm) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 46 | |
| 47 | def testCommitNewLKGM(self): |
| 48 | """Tests that we can commit a new LKGM file.""" |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 49 | self.old_lkgm = '999.0.0' |
| 50 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
| 51 | side_effect=self._createOldLkgm) |
| 52 | self.committer.CheckoutChrome() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 53 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 54 | self.assertEqual(self.committer.lkgm_file, self.lkgm_file) |
| 55 | |
| 56 | self.committer.UpdateLKGM() |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 57 | self.committer.CommitNewLKGM() |
| 58 | |
| 59 | # Check the file was actually written out correctly. |
| 60 | self.assertEqual(osutils.ReadFile(self.lkgm_file), self.committer._lkgm) |
| 61 | self.assertCommandContains(['git', 'commit']) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 62 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 63 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 64 | def testOlderLKGMFails(self): |
| 65 | """Tests that trying to update to an older lkgm version fails.""" |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 66 | self.old_lkgm = '1002.0.0' |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 67 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
Steven Bennetts | d36fc78 | 2017-11-08 14:45:43 -0800 | [diff] [blame] | 68 | side_effect=self._createOldLkgm) |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 69 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 70 | self.committer.CheckoutChrome() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 71 | |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 72 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid, |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 73 | self.committer.UpdateLKGM) |
| 74 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
| 75 | self.assertEqual(self.committer._lkgm, '1001.0.0') |
| 76 | self.assertEqual(osutils.ReadFile(self.lkgm_file), '1002.0.0') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 77 | |
| 78 | def testVersionWithChromeBranch(self): |
| 79 | """Tests passing a version with a chrome branch strips the branch.""" |
Achuith Bhandarkar | 1b9180f | 2018-02-22 19:12:09 -0800 | [diff] [blame] | 80 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
Achuith Bhandarkar | b6f4025 | 2019-12-09 16:31:34 -0800 | [diff] [blame^] | 81 | 'user@test.org', self.tempdir, '1003.0.0-rc2') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 82 | |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 83 | self.old_lkgm = '1002.0.0' |
| 84 | self.rc.AddCmdResult(partial_mock.In('remote'), returncode=0, |
| 85 | side_effect=self._createOldLkgm) |
| 86 | |
| 87 | self.committer.CheckoutChrome() |
| 88 | self.committer.UpdateLKGM() |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 89 | self.committer.CommitNewLKGM() |
| 90 | |
| 91 | # Check the file was actually written out correctly. |
| 92 | stripped_lkgm = '1003.0.0' |
| 93 | self.assertEqual(osutils.ReadFile(self.lkgm_file), stripped_lkgm) |
Achuith Bhandarkar | dc3a351 | 2018-03-16 15:10:57 -0700 | [diff] [blame] | 94 | self.assertEqual(self.committer._old_lkgm, self.old_lkgm) |
Ben Pastene | d3b93d4 | 2019-10-10 09:56:29 -0700 | [diff] [blame] | 95 | |
| 96 | def testCommitMsg(self): |
| 97 | """Tests format of the commit message.""" |
| 98 | self.committer._lkgm = '12345.0.0' |
| 99 | self.committer._PRESUBMIT_BOTS = ['bot1', 'bot2'] |
| 100 | commit_msg_lines = self.committer.ComposeCommitMsg().splitlines() |
| 101 | self.assertIn('LKGM 12345.0.0 for chromeos.', commit_msg_lines) |
| 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) |