blob: 853e99dcbde9ed2b71f7ca5ef5f9b3bd03dd516f [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2017 The ChromiumOS Authors
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07002# 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 Pastene6a8361a2021-08-19 19:52:05 -07007from unittest import mock
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07008
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07009from chromite.lib import cros_test_lib
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070010from chromite.scripts import chrome_chromeos_lkgm
11
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080012
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070013# pylint: disable=protected-access
Alex Klein1699fab2022-09-08 08:46:06 -060014class ChromeLKGMCommitterTester(
15 cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase
16):
17 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080018
Alex Klein1699fab2022-09-08 08:46:06 -060019 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 Bennettsddf9bcd2017-06-14 14:07:43 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 @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 Dall64a21b62022-01-07 13:50:26 +110030 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060031 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 Neus624c8a52022-09-13 17:33:03 +000048 123456,
49 labels={"Bot-Commit": 1, "Commit-Queue": 2},
50 notify="NONE",
Alex Klein1699fab2022-09-08 08:46:06 -060051 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070052
Alex Klein1699fab2022-09-08 08:46:06 -060053 @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 Dall64a21b62022-01-07 13:50:26 +110057 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060058 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 Pastened3b93d42019-10-10 09:56:29 -070068
Alex Klein1699fab2022-09-08 08:46:06 -060069 @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 Pastene6a8361a2021-08-19 19:52:05 -070077
Alex Klein1699fab2022-09-08 08:46:06 -060078 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 Neus624c8a52022-09-13 17:33:03 +0000100 123456,
101 labels={"Bot-Commit": 1, "Commit-Queue": 2},
102 notify="NONE",
Alex Klein1699fab2022-09-08 08:46:06 -0600103 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 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 )