blob: 1b16bd7305bcaaa516d1ada1be8d951e7bce52da [file] [log] [blame]
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -07001# Copyright 2021 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"""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"
42 build_minios.main(
43 [
44 # --board is a required argument.
45 "--board",
46 test_board,
47 # --version is a required argument.
48 "--version",
49 test_version,
50 # --image is a required argument.
51 "--image",
52 test_image,
53 ]
54 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070055
Alex Klein1699fab2022-09-08 08:46:06 -060056 self.assertEqual(
57 self.create_minios_mock.mock_calls,
58 [
59 mock.call(
60 test_board,
61 test_version,
62 self._tempdir,
63 constants.VBOOT_DEVKEYS_DIR,
64 constants.RECOVERY_PUBLIC_KEY,
65 constants.MINIOS_DATA_PRIVATE_KEY,
66 constants.MINIOS_KEYBLOCK,
67 None,
68 True,
69 False,
70 )
71 ],
72 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070073
Alex Klein1699fab2022-09-08 08:46:06 -060074 self.assertEqual(
75 self.insert_minios_mock.mock_calls,
76 [
77 mock.call(
78 test_image,
79 self.create_minios_mock_return,
80 )
81 ],
82 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -070083
Alex Klein1699fab2022-09-08 08:46:06 -060084 def testOverridenArguments(self):
85 """Test that overridden arguments of build_minios are formatted correct."""
86 test_board = "test-board"
87 test_version = "1.0.0.0"
88 test_image = "/some/image/path"
89 test_keys_dir = "/some/path/test-keys-dir"
90 test_public_key = "test-public-key"
91 test_private_key = "test-private-key"
92 test_keyblock = "test-keyblock"
93 test_serial = "test-serial"
94 build_minios.main(
95 [
96 # --board is a required argument.
97 "--board",
98 test_board,
99 # --version is a required argument.
100 "--version",
101 test_version,
102 # --image is a required argument.
103 "--image",
104 test_image,
105 "--keys-dir",
106 test_keys_dir,
107 "--public-key",
108 test_public_key,
109 "--private-key",
110 test_private_key,
111 "--keyblock",
112 test_keyblock,
113 "--serial",
114 test_serial,
115 "--force-build",
116 ]
117 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 self.assertEqual(
120 self.create_minios_mock.mock_calls,
121 [
122 mock.call(
123 test_board,
124 test_version,
125 self._tempdir,
126 test_keys_dir,
127 test_public_key,
128 test_private_key,
129 test_keyblock,
130 test_serial,
131 True,
132 False,
133 )
134 ],
135 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800136
Alex Klein1699fab2022-09-08 08:46:06 -0600137 self.assertEqual(
138 self.insert_minios_mock.mock_calls,
139 [
140 mock.call(
141 test_image,
142 self.create_minios_mock_return,
143 )
144 ],
145 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 def testModForDev(self):
148 """Test that default arguments of build_minios are formatted correct."""
149 test_board = "test-board"
150 test_version = "0.0.0.0"
151 test_image = "/some/image/path"
152 build_minios.main(
153 [
154 # --board is a required argument.
155 "--board",
156 test_board,
157 # --version is a required argument.
158 "--version",
159 test_version,
160 # --image is a required argument.
161 "--image",
162 test_image,
163 "--mod-for-dev",
164 ]
165 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 self.assertEqual(
168 self.create_minios_mock.mock_calls,
169 [
170 mock.call(
171 test_board,
172 test_version,
173 self._tempdir,
174 constants.VBOOT_DEVKEYS_DIR,
175 constants.RECOVERY_PUBLIC_KEY,
176 constants.MINIOS_DATA_PRIVATE_KEY,
177 constants.MINIOS_KEYBLOCK,
178 None,
179 False,
180 True,
181 )
182 ],
183 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 self.assertEqual(
186 self.insert_minios_mock.mock_calls,
187 [
188 mock.call(
189 test_image,
190 self.create_minios_mock_return,
191 )
192 ],
193 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 def testModForDevWithForceBuild(self):
196 """Test that default arguments of build_minios are formatted correct."""
197 test_board = "test-board"
198 test_version = "0.0.0.0"
199 test_image = "/some/image/path"
200 build_minios.main(
201 [
202 # --board is a required argument.
203 "--board",
204 test_board,
205 # --version is a required argument.
206 "--version",
207 test_version,
208 # --image is a required argument.
209 "--image",
210 test_image,
211 "--mod-for-dev",
212 "--force-build",
213 ]
214 )
Henry Barnore0dfd562022-02-16 10:01:54 -0800215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 self.assertEqual(
217 self.create_minios_mock.mock_calls,
218 [
219 mock.call(
220 test_board,
221 test_version,
222 self._tempdir,
223 constants.VBOOT_DEVKEYS_DIR,
224 constants.RECOVERY_PUBLIC_KEY,
225 constants.MINIOS_DATA_PRIVATE_KEY,
226 constants.MINIOS_KEYBLOCK,
227 None,
228 True,
229 True,
230 )
231 ],
232 )
Jae Hoon Kim2f8c62a2021-06-22 15:10:43 -0700233
Alex Klein1699fab2022-09-08 08:46:06 -0600234 self.assertEqual(
235 self.insert_minios_mock.mock_calls,
236 [
237 mock.call(
238 test_image,
239 self.create_minios_mock_return,
240 )
241 ],
242 )