blob: 1bf8589c087ec89171a47caf961bf09059cdc694 [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
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090013class StubGerritChange:
14 """Stab class corresponding to cros_patch.GerritChange."""
15
16 def __init__(self, gerrit_number, file_content, subject):
17 self._gerrit_number = gerrit_number
18 self._subject = subject
19 self._file_content = file_content
20
21 @property
22 def subject(self):
23 return self._subject
24
25 @property
26 def gerrit_number(self):
27 return self._gerrit_number
28
29 def GetFileContents(self, _path: str):
30 return self._file_content
31
32
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070033# pylint: disable=protected-access
Alex Klein1699fab2022-09-08 08:46:06 -060034class ChromeLKGMCommitterTester(
35 cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase
36):
37 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080038
Alex Klein1699fab2022-09-08 08:46:06 -060039 def setUp(self):
40 """Common set up method for all tests."""
41 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
42 "1001.0.0", "main"
43 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 @mock.patch("chromite.lib.gob_util.GetFileContents")
46 def testCommitNewLKGM(self, mock_get_file):
47 """Tests that we can commit a new LKGM file."""
48 mock_get_file.return_value = "999.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110049 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060050 self.committer._gerrit_helper, "CreateChange"
51 ) as cg:
52 cg.return_value = mock.MagicMock(gerrit_number=123456)
53 with mock.patch.object(
54 self.committer._gerrit_helper, "ChangeEdit"
55 ) as ce:
56 with mock.patch.object(
57 self.committer._gerrit_helper, "SetReview"
58 ) as bc:
59 with mock.patch.object(
60 self.committer._gerrit_helper, "SetHashtags"
61 ):
62 self.committer.UpdateLKGM()
63 ce.assert_called_once_with(
64 123456, "chromeos/CHROMEOS_LKGM", "1001.0.0"
65 )
66 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +000067 123456,
68 labels={"Bot-Commit": 1, "Commit-Queue": 2},
69 notify="NONE",
Alex Klein1699fab2022-09-08 08:46:06 -060070 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070071
Alex Klein1699fab2022-09-08 08:46:06 -060072 @mock.patch("chromite.lib.gob_util.GetFileContents")
73 def testOlderLKGMFails(self, mock_get_file):
74 """Tests that trying to update to an older lkgm version fails."""
75 mock_get_file.return_value = "1002.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110076 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060077 self.committer._gerrit_helper, "CreateChange"
78 ) as cg:
79 cg.return_value = mock.MagicMock(gerrit_number=123456)
80 with mock.patch.object(
81 self.committer._gerrit_helper, "ChangeEdit"
82 ) as ce:
83 self.assertRaises(
84 chrome_chromeos_lkgm.LKGMNotValid, self.committer.UpdateLKGM
85 )
86 ce.assert_not_called()
Ben Pastened3b93d42019-10-10 09:56:29 -070087
Alex Klein1699fab2022-09-08 08:46:06 -060088 @mock.patch("chromite.lib.gob_util.GetFileContents")
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090089 def testAbandonObsoleteLKGMs(self, mock_get_file):
90 """Tests that trying to abandon the obsolete lkgm CLs."""
91 mock_get_file.return_value = "10002.0.0"
92
93 older_change = StubGerritChange(3876550, "10001.0.0", "10001.0.0")
94 newer_change = StubGerritChange(3876551, "10003.0.0", "10003.0.0")
95 open_issues = [older_change, newer_change]
96
97 with mock.patch.object(
98 self.committer._gerrit_helper, "Query", return_value=open_issues
99 ) as mock_query:
100 with mock.patch.object(
101 self.committer._gerrit_helper, "AbandonChange"
102 ) as ac:
103 self.committer.AbandonObsoleteLKGMRolls()
104 mock_query.assert_called_once()
105 ac.assert_called_once_with((older_change), msg=mock.ANY)
106
107 @mock.patch("chromite.lib.gob_util.GetFileContents")
Alex Klein1699fab2022-09-08 08:46:06 -0600108 def testVersionWithChromeBranch(self, mock_get_file):
109 """Tests passing a version with a chrome branch strips the branch."""
110 branch = "refs/branch-heads/5000"
111 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
112 "1003.0.0-rc2", branch
113 )
114 mock_get_file.return_value = "1002.0.0"
Ben Pastene6a8361a2021-08-19 19:52:05 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 with mock.patch.object(
117 self.committer._gerrit_helper, "CreateChange"
118 ) as cg:
119 cg.return_value = mock.MagicMock(gerrit_number=123456)
120 with mock.patch.object(
121 self.committer._gerrit_helper, "ChangeEdit"
122 ) as ce:
123 with mock.patch.object(
124 self.committer._gerrit_helper, "SetReview"
125 ) as bc:
126 with mock.patch.object(
127 self.committer._gerrit_helper, "SetHashtags"
128 ):
129 # Check the file was actually written out correctly.
130 self.committer.UpdateLKGM()
131 cg.assert_called_once_with(
132 "chromium/src", branch, mock.ANY, False
133 )
134 ce.assert_called_once_with(
135 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0"
136 )
137 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +0000138 123456,
139 labels={"Bot-Commit": 1, "Commit-Queue": 2},
140 notify="NONE",
Alex Klein1699fab2022-09-08 08:46:06 -0600141 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 def testCommitMsg(self):
144 """Tests format of the commit message."""
145 self.committer._PRESUBMIT_BOTS = ["bot1", "bot2"]
146 self.committer._buildbucket_id = "some-build-id"
147 commit_msg_lines = self.committer.ComposeCommitMsg().splitlines()
148 self.assertIn(
149 "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines
150 )
151 self.assertIn(
152 "Uploaded by https://ci.chromium.org/b/some-build-id",
153 commit_msg_lines,
154 )
155 self.assertIn(
156 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines
157 )
158 self.assertIn(
159 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines
160 )