blob: f05de4ad6f938d19daf736756e1ea4ed75f23964 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2021 The ChromiumOS Authors
Jae Hoon Kim2f8c62a2021-06-22 15:10: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"""Unittests for build_minios.py"""
6
7import os
8import tempfile
9from unittest import mock
10
11from chromite.lib import constants
12from chromite.lib import cros_test_lib
13from chromite.lib import minios
14from chromite.scripts import build_minios
15
16
17class BuildMiniosTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Unit tests for build_minios."""
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070019
Alex Klein1699fab2022-09-08 08:46:06 -060020 def setUp(self):
21 self.create_minios_mock_return = "/some/kernel/path"
22 self.create_minios_mock = self.PatchObject(
23 minios,
24 "CreateMiniOsKernelImage",
25 return_value=self.create_minios_mock_return,
26 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070027
Alex Klein1699fab2022-09-08 08:46:06 -060028 self.insert_minios_mock = self.PatchObject(
29 minios, "InsertMiniOsKernelImage"
30 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 # Patch to assert against the tempdir that's created under the anchored
33 # tempdir created by cros_test_lib.
34 self._tempdir = os.path.join(self.tempdir, "test-dir")
35 self.PatchObject(tempfile, "mkdtemp", return_value=self._tempdir)
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 def testDefaultArguments(self):
38 """Test that default arguments of build_minios are formatted correct."""
39 test_board = "test-board"
40 test_version = "0.0.0.0"
41 test_image = "/some/image/path"
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +000042 self.PatchObject(
43 os,
44 "cpu_count",
45 return_value=777,
46 )
Alex Klein1699fab2022-09-08 08:46:06 -060047 build_minios.main(
48 [
49 # --board is a required argument.
50 "--board",
51 test_board,
52 # --version is a required argument.
53 "--version",
54 test_version,
55 # --image is a required argument.
56 "--image",
57 test_image,
58 ]
59 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070060
Alex Klein1699fab2022-09-08 08:46:06 -060061 self.assertEqual(
62 self.create_minios_mock.mock_calls,
63 [
64 mock.call(
65 test_board,
66 test_version,
67 self._tempdir,
68 constants.VBOOT_DEVKEYS_DIR,
69 constants.RECOVERY_PUBLIC_KEY,
70 constants.MINIOS_DATA_PRIVATE_KEY,
71 constants.MINIOS_KEYBLOCK,
72 None,
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +000073 777,
Alex Klein1699fab2022-09-08 08:46:06 -060074 True,
75 False,
76 )
77 ],
78 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070079
Alex Klein1699fab2022-09-08 08:46:06 -060080 self.assertEqual(
81 self.insert_minios_mock.mock_calls,
82 [
83 mock.call(
84 test_image,
85 self.create_minios_mock_return,
86 )
87 ],
88 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070089
Alex Klein1699fab2022-09-08 08:46:06 -060090 def testOverridenArguments(self):
Alex Klein9e7b29e2023-04-11 16:10:31 -060091 """Test overridden arguments of build_minios are formatted correctly."""
Alex Klein1699fab2022-09-08 08:46:06 -060092 test_board = "test-board"
93 test_version = "1.0.0.0"
94 test_image = "/some/image/path"
95 test_keys_dir = "/some/path/test-keys-dir"
96 test_public_key = "test-public-key"
97 test_private_key = "test-private-key"
98 test_keyblock = "test-keyblock"
99 test_serial = "test-serial"
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000100 test_jobs = 777
Alex Klein1699fab2022-09-08 08:46:06 -0600101 build_minios.main(
102 [
103 # --board is a required argument.
104 "--board",
105 test_board,
106 # --version is a required argument.
107 "--version",
108 test_version,
109 # --image is a required argument.
110 "--image",
111 test_image,
112 "--keys-dir",
113 test_keys_dir,
114 "--public-key",
115 test_public_key,
116 "--private-key",
117 test_private_key,
118 "--keyblock",
119 test_keyblock,
120 "--serial",
121 test_serial,
122 "--force-build",
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000123 "--jobs",
124 str(test_jobs),
Alex Klein1699fab2022-09-08 08:46:06 -0600125 ]
126 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -0700127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 self.assertEqual(
129 self.create_minios_mock.mock_calls,
130 [
131 mock.call(
132 test_board,
133 test_version,
134 self._tempdir,
135 test_keys_dir,
136 test_public_key,
137 test_private_key,
138 test_keyblock,
139 test_serial,
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000140 test_jobs,
Alex Klein1699fab2022-09-08 08:46:06 -0600141 True,
142 False,
143 )
144 ],
145 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 self.assertEqual(
148 self.insert_minios_mock.mock_calls,
149 [
150 mock.call(
151 test_image,
152 self.create_minios_mock_return,
153 )
154 ],
155 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 def testModForDev(self):
158 """Test that default arguments of build_minios are formatted correct."""
159 test_board = "test-board"
160 test_version = "0.0.0.0"
161 test_image = "/some/image/path"
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000162 test_jobs = 777
163 self.PatchObject(
164 os,
165 "cpu_count",
166 return_value=str(test_jobs),
167 )
168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 build_minios.main(
170 [
171 # --board is a required argument.
172 "--board",
173 test_board,
174 # --version is a required argument.
175 "--version",
176 test_version,
177 # --image is a required argument.
178 "--image",
179 test_image,
180 "--mod-for-dev",
181 ]
182 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800183
Alex Klein1699fab2022-09-08 08:46:06 -0600184 self.assertEqual(
185 self.create_minios_mock.mock_calls,
186 [
187 mock.call(
188 test_board,
189 test_version,
190 self._tempdir,
191 constants.VBOOT_DEVKEYS_DIR,
192 constants.RECOVERY_PUBLIC_KEY,
193 constants.MINIOS_DATA_PRIVATE_KEY,
194 constants.MINIOS_KEYBLOCK,
195 None,
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000196 test_jobs,
Alex Klein1699fab2022-09-08 08:46:06 -0600197 False,
198 True,
199 )
200 ],
201 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 self.assertEqual(
204 self.insert_minios_mock.mock_calls,
205 [
206 mock.call(
207 test_image,
208 self.create_minios_mock_return,
209 )
210 ],
211 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800212
Alex Klein1699fab2022-09-08 08:46:06 -0600213 def testModForDevWithForceBuild(self):
214 """Test that default arguments of build_minios are formatted correct."""
215 test_board = "test-board"
216 test_version = "0.0.0.0"
217 test_image = "/some/image/path"
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000218 test_jobs = 777
Alex Klein1699fab2022-09-08 08:46:06 -0600219 build_minios.main(
220 [
221 # --board is a required argument.
222 "--board",
223 test_board,
224 # --version is a required argument.
225 "--version",
226 test_version,
227 # --image is a required argument.
228 "--image",
229 test_image,
230 "--mod-for-dev",
231 "--force-build",
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000232 "--jobs",
233 str(test_jobs),
Alex Klein1699fab2022-09-08 08:46:06 -0600234 ]
235 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800236
Alex Klein1699fab2022-09-08 08:46:06 -0600237 self.assertEqual(
238 self.create_minios_mock.mock_calls,
239 [
240 mock.call(
241 test_board,
242 test_version,
243 self._tempdir,
244 constants.VBOOT_DEVKEYS_DIR,
245 constants.RECOVERY_PUBLIC_KEY,
246 constants.MINIOS_DATA_PRIVATE_KEY,
247 constants.MINIOS_KEYBLOCK,
248 None,
Jae Hoon Kim9f0c9392023-02-08 06:27:48 +0000249 test_jobs,
Alex Klein1699fab2022-09-08 08:46:06 -0600250 True,
251 True,
252 )
253 ],
254 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -0700255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 self.assertEqual(
257 self.insert_minios_mock.mock_calls,
258 [
259 mock.call(
260 test_image,
261 self.create_minios_mock_return,
262 )
263 ],
264 )