blob: c7c2271a30ddb0a96e5af0dbfc4a4e3ff4d4098d [file] [log] [blame]
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07001# 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
Ben Pastene6a8361a2021-08-19 19:52:05 -07007from unittest import mock
Ben Pasteneca7c71b2021-10-07 18:34:27 -07008import urllib.parse
Steven Bennettsddf9bcd2017-06-14 14:07:43 -07009
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070010from chromite.lib import cros_test_lib
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070011from chromite.scripts import chrome_chromeos_lkgm
12
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080013
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070014# pylint: disable=protected-access
Alex Klein1699fab2022-09-08 08:46:06 -060015class ChromeLKGMCommitterTester(
16 cros_test_lib.RunCommandTestCase, cros_test_lib.MockTempDirTestCase
17):
18 """Test cros_chromeos_lkgm.Committer."""
Achuith Bhandarkarec8d7a72018-03-01 16:56:22 -080019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def setUp(self):
21 """Common set up method for all tests."""
22 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
23 "1001.0.0", "main"
24 )
25 self.old_lkgm = None
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070026
Alex Klein1699fab2022-09-08 08:46:06 -060027 @mock.patch("chromite.lib.gob_util.GetFileContents")
28 def testCommitNewLKGM(self, mock_get_file):
29 """Tests that we can commit a new LKGM file."""
30 mock_get_file.return_value = "999.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110031 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060032 self.committer._gerrit_helper, "CreateChange"
33 ) as cg:
34 cg.return_value = mock.MagicMock(gerrit_number=123456)
35 with mock.patch.object(
36 self.committer._gerrit_helper, "ChangeEdit"
37 ) as ce:
38 with mock.patch.object(
39 self.committer._gerrit_helper, "SetReview"
40 ) as bc:
41 with mock.patch.object(
42 self.committer._gerrit_helper, "SetHashtags"
43 ):
44 self.committer.UpdateLKGM()
45 ce.assert_called_once_with(
46 123456, "chromeos/CHROMEOS_LKGM", "1001.0.0"
47 )
48 bc.assert_called_once_with(
49 123456, labels={"Bot-Commit": 1}, notify="NONE"
50 )
Steven Bennettsddf9bcd2017-06-14 14:07:43 -070051
Alex Klein1699fab2022-09-08 08:46:06 -060052 @mock.patch("chromite.lib.gob_util.GetFileContents")
53 def testOlderLKGMFails(self, mock_get_file):
54 """Tests that trying to update to an older lkgm version fails."""
55 mock_get_file.return_value = "1002.0.0"
Fergus Dall64a21b62022-01-07 13:50:26 +110056 with mock.patch.object(
Alex Klein1699fab2022-09-08 08:46:06 -060057 self.committer._gerrit_helper, "CreateChange"
58 ) as cg:
59 cg.return_value = mock.MagicMock(gerrit_number=123456)
60 with mock.patch.object(
61 self.committer._gerrit_helper, "ChangeEdit"
62 ) as ce:
63 self.assertRaises(
64 chrome_chromeos_lkgm.LKGMNotValid, self.committer.UpdateLKGM
65 )
66 ce.assert_not_called()
Ben Pastened3b93d42019-10-10 09:56:29 -070067
Alex Klein1699fab2022-09-08 08:46:06 -060068 @mock.patch("chromite.lib.gob_util.GetFileContents")
69 def testVersionWithChromeBranch(self, mock_get_file):
70 """Tests passing a version with a chrome branch strips the branch."""
71 branch = "refs/branch-heads/5000"
72 self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter(
73 "1003.0.0-rc2", branch
74 )
75 mock_get_file.return_value = "1002.0.0"
Ben Pastene6a8361a2021-08-19 19:52:05 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 with mock.patch.object(
78 self.committer._gerrit_helper, "CreateChange"
79 ) as cg:
80 cg.return_value = mock.MagicMock(gerrit_number=123456)
81 with mock.patch.object(
82 self.committer._gerrit_helper, "ChangeEdit"
83 ) as ce:
84 with mock.patch.object(
85 self.committer._gerrit_helper, "SetReview"
86 ) as bc:
87 with mock.patch.object(
88 self.committer._gerrit_helper, "SetHashtags"
89 ):
90 # Check the file was actually written out correctly.
91 self.committer.UpdateLKGM()
92 cg.assert_called_once_with(
93 "chromium/src", branch, mock.ANY, False
94 )
95 ce.assert_called_once_with(
96 123456, "chromeos/CHROMEOS_LKGM", "1003.0.0"
97 )
98 bc.assert_called_once_with(
99 123456, labels={"Bot-Commit": 1}, notify="NONE"
100 )
Ben Pastene6a8361a2021-08-19 19:52:05 -0700101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 def testCommitMsg(self):
103 """Tests format of the commit message."""
104 self.committer._PRESUBMIT_BOTS = ["bot1", "bot2"]
105 self.committer._buildbucket_id = "some-build-id"
106 commit_msg_lines = self.committer.ComposeCommitMsg().splitlines()
107 self.assertIn(
108 "Automated Commit: LKGM 1001.0.0 for chromeos.", commit_msg_lines
109 )
110 self.assertIn(
111 "Uploaded by https://ci.chromium.org/b/some-build-id",
112 commit_msg_lines,
113 )
114 self.assertIn(
115 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot1", commit_msg_lines
116 )
117 self.assertIn(
118 "CQ_INCLUDE_TRYBOTS=luci.chrome.try:bot2", commit_msg_lines
119 )
120
121 def testFindAlreadyOpenLKGMRoll(self):
122 already_open_issues = [123456]
123 self.committer._commit_msg_header = "A message with spaces"
124 with mock.patch.object(
125 self.committer._gerrit_helper,
126 "Query",
127 return_value=already_open_issues,
128 ) as mock_query:
129 self.assertEqual(
130 self.committer.FindAlreadyOpenLKGMRoll(), already_open_issues[0]
131 )
132 escaped_quotes = urllib.parse.quote('"')
133 message = mock_query.call_args.kwargs["message"]
134 self.assertEqual(message.count(escaped_quotes), 2)
135 self.assertTrue(message.startswith(escaped_quotes))
136 self.assertTrue(message.endswith(escaped_quotes))
137 already_open_issues = [123456, 654321]
138 with mock.patch.object(
139 self.committer._gerrit_helper,
140 "Query",
141 return_value=already_open_issues,
142 ):
143 self.assertRaises(
144 chrome_chromeos_lkgm.LKGMNotValid,
145 self.committer.FindAlreadyOpenLKGMRoll,
146 )
147
148 def testSubmitToCQ(self):
149 self.committer._buildbucket_id = "some-build-id"
150 already_open_issue = 123456
151 with mock.patch.object(
152 self.committer._gerrit_helper, "SetReview"
153 ) as mock_review:
154 self.committer.SubmitToCQ(already_open_issue)
155 self.assertIn(
156 self.committer._buildbucket_id, mock_review.call_args[1]["msg"]
157 )