blob: ec7147665fe0ba5689eef14d99280a509348800d [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",
Yoshiki Iguchidad24252022-09-22 14:21:30 +090070 ready=True,
71 reviewers=[
72 "chrome-os-gardeners-reviews@google.com"
73 ],
Alex Klein1699fab2022-09-08 08:46:06 -060074 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 @mock.patch("chromite.lib.gob_util.GetFileContents")
77 def testOlderLKGMFails(self, mock_get_file):
78 """Tests that trying to update to an older lkgm version fails."""
79 mock_get_file.return_value = "1002.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110080 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060081 self.committer._gerrit_helper, "CreateChange"
82 ) as cg:
83 cg.return_value = mock.MagicMock(gerrit_number=123456)
84 with mock.patch.object(
85 self.committer._gerrit_helper, "ChangeEdit"
86 ) as ce:
87 self.assertRaises(
88 chrome_chromeos_lkgm.LKGMNotValid, self.committer.UpdateLKGM
89 )
90 ce.assert_not_called()
Ben Pastened3b93d42019-10-10 09:56:29 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 @mock.patch("chromite.lib.gob_util.GetFileContents")
Yoshiki Iguchif8ab0112022-09-15 16:57:26 +090093 def testAbandonObsoleteLKGMs(self, mock_get_file):
94 """Tests that trying to abandon the obsolete lkgm CLs."""
95 mock_get_file.return_value = "10002.0.0"
96
97 older_change = StubGerritChange(3876550, "10001.0.0", "10001.0.0")
98 newer_change = StubGerritChange(3876551, "10003.0.0", "10003.0.0")
99 open_issues = [older_change, newer_change]
100
101 with mock.patch.object(
102 self.committer._gerrit_helper, "Query", return_value=open_issues
103 ) as mock_query:
104 with mock.patch.object(
105 self.committer._gerrit_helper, "AbandonChange"
106 ) as ac:
107 self.committer.AbandonObsoleteLKGMRolls()
108 mock_query.assert_called_once()
109 ac.assert_called_once_with((older_change), msg=mock.ANY)
110
111 @mock.patch("chromite.lib.gob_util.GetFileContents")
Alex Klein1699fab2022-09-08 08:46:06 -0600112 def testVersionWithChromeBranch(self, mock_get_file):
113 """Tests passing a version with a chrome branch strips the branch."""
114 branch = "refs/branch-heads/5000"
115 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
116 "1003.0.0-rc2", branch
117 )
118 mock_get_file.return_value = "1002.0.0"
Ben Pastene6a8361a2021-08-19 19:52:05 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 with mock.patch.object(
121 self.committer._gerrit_helper, "CreateChange"
122 ) as cg:
123 cg.return_value = mock.MagicMock(gerrit_number=123456)
124 with mock.patch.object(
125 self.committer._gerrit_helper, "ChangeEdit"
126 ) as ce:
127 with mock.patch.object(
128 self.committer._gerrit_helper, "SetReview"
129 ) as bc:
130 with mock.patch.object(
131 self.committer._gerrit_helper, "SetHashtags"
132 ):
133 # Check the file was actually written out correctly.
134 self.committer.UpdateLKGM()
135 cg.assert_called_once_with(
136 "chromium/src", branch, mock.ANY, False
137 )
138 ce.assert_called_once_with(
139 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0"
140 )
141 bc.assert_called_once_with(
Jack Neus624c8a52022-09-13 17:33:03 +0000142 123456,
143 labels={"Bot-Commit": 1, "Commit-Queue": 2},
144 notify="NONE",
Yoshiki Iguchidad24252022-09-22 14:21:30 +0900145 ready=True,
146 reviewers=[
147 "chrome-os-gardeners-reviews@google.com"
148 ],
Alex Klein1699fab2022-09-08 08:46:06 -0600149 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 def testCommitMsg(self):
152 """Tests format of the commit message."""
153 self.committer._PRESUBMIT_BOTS = ["bot1", "bot2"]
154 self.committer._buildbucket_id = "some-build-id"
155 commit_msg_lines = self.committer.ComposeCommitMsg().splitlines()
156 self.assertIn(
157 "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines
158 )
159 self.assertIn(
160 "Uploaded by https://ci.chromium.org/b/some-build-id",
161 commit_msg_lines,
162 )
163 self.assertIn(
164 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines
165 )
166 self.assertIn(
167 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines
168 )