Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Image service tests.""" |
| 6 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 7 | import os |
Joseph Sussman | 34c01be | 2022-06-03 14:04:41 +0000 | [diff] [blame] | 8 | from pathlib import Path |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 9 | from typing import List, Optional |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 10 | from unittest import mock |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 13 | from chromite.api import controller |
| 14 | from chromite.api.controller import image as image_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import image_pb2 |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 17 | from chromite.api.gen.chromiumos import common_pb2 |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 18 | from chromite.lib import build_target_lib |
| 19 | from chromite.lib import chroot_lib |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 20 | from chromite.lib import constants |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 21 | from chromite.lib import cros_build_lib |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 22 | from chromite.lib import cros_test_lib |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 23 | from chromite.lib import image_lib |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 24 | from chromite.lib import osutils |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 25 | from chromite.lib import sysroot_lib |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 26 | from chromite.scripts import pushimage |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 27 | from chromite.service import image as image_service |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 28 | |
| 29 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 30 | class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | """Create image tests.""" |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | def setUp(self): |
| 34 | self.response = image_pb2.CreateImageResult() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 35 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | def _GetRequest( |
| 37 | self, |
| 38 | board=None, |
| 39 | types=None, |
| 40 | version=None, |
| 41 | builder_path=None, |
| 42 | disable_rootfs_verification=False, |
| 43 | base_is_recovery=False, |
| 44 | ): |
| 45 | """Helper to build a request instance.""" |
| 46 | return image_pb2.CreateImageRequest( |
| 47 | build_target={"name": board}, |
| 48 | image_types=types, |
| 49 | disable_rootfs_verification=disable_rootfs_verification, |
| 50 | version=version, |
| 51 | builder_path=builder_path, |
| 52 | base_is_recovery=base_is_recovery, |
| 53 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 56 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | patch = self.PatchObject(image_service, "Build") |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | request = self._GetRequest(board="board") |
| 60 | image_controller.Create( |
| 61 | request, self.response, self.validate_only_config |
| 62 | ) |
| 63 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 66 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | patch = self.PatchObject(image_service, "Build") |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | request = self._GetRequest(board="board") |
| 70 | image_controller.Create(request, self.response, self.mock_call_config) |
| 71 | patch.assert_not_called() |
| 72 | self.assertEqual(self.response.success, True) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | def testMockError(self): |
| 75 | """Test that mock call does not execute any logic, returns error.""" |
| 76 | patch = self.PatchObject(image_service, "Build") |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | request = self._GetRequest(board="board") |
| 79 | rc = image_controller.Create( |
| 80 | request, self.response, self.mock_error_config |
| 81 | ) |
| 82 | patch.assert_not_called() |
| 83 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | def testNoBoard(self): |
| 86 | """Test no board given fails.""" |
| 87 | request = self._GetRequest() |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | # No board should cause it to fail. |
| 90 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 91 | image_controller.Create(request, self.response, self.api_config) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | def testNoTypeSpecified(self): |
| 94 | """Test the image type default.""" |
| 95 | request = self._GetRequest(board="board") |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | # Failed result to avoid the success handling logic. |
| 98 | result = image_service.BuildResult([constants.IMAGE_TYPE_BASE]) |
| 99 | result.return_code = 1 |
| 100 | build_patch = self.PatchObject( |
| 101 | image_service, "Build", return_value=result |
| 102 | ) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | image_controller.Create(request, self.response, self.api_config) |
| 105 | build_patch.assert_any_call( |
| 106 | "board", [constants.IMAGE_TYPE_BASE], config=mock.ANY |
| 107 | ) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | def testSingleTypeSpecified(self): |
| 110 | """Test it's properly using a specified type.""" |
| 111 | request = self._GetRequest( |
| 112 | board="board", types=[common_pb2.IMAGE_TYPE_DEV] |
| 113 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | # Failed result to avoid the success handling logic. |
| 116 | result = image_service.BuildResult([constants.IMAGE_TYPE_DEV]) |
| 117 | result.return_code = 1 |
| 118 | build_patch = self.PatchObject( |
| 119 | image_service, "Build", return_value=result |
| 120 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 121 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | image_controller.Create(request, self.response, self.api_config) |
| 123 | build_patch.assert_any_call( |
| 124 | "board", [constants.IMAGE_TYPE_DEV], config=mock.ANY |
| 125 | ) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | def testMultipleAndImpliedTypes(self): |
| 128 | """Test multiple types and implied type handling.""" |
| 129 | # The TEST_VM type should force it to build the test image. |
| 130 | types = [common_pb2.IMAGE_TYPE_BASE, common_pb2.IMAGE_TYPE_TEST_VM] |
| 131 | expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST] |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | request = self._GetRequest(board="board", types=types) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | # Failed result to avoid the success handling logic. |
| 136 | result = image_service.BuildResult(expected_images) |
| 137 | result.return_code = 1 |
| 138 | build_patch = self.PatchObject( |
| 139 | image_service, "Build", return_value=result |
| 140 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 141 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 142 | image_controller.Create(request, self.response, self.api_config) |
| 143 | build_patch.assert_any_call("board", expected_images, config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | def testRecoveryImpliedTypes(self): |
| 146 | """Test implied type handling of recovery images.""" |
| 147 | # The TEST_VM type should force it to build the test image. |
| 148 | types = [common_pb2.IMAGE_TYPE_RECOVERY] |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | request = self._GetRequest(board="board", types=types) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | # Failed result to avoid the success handling logic. |
| 153 | result = image_service.BuildResult([]) |
| 154 | result.return_code = 1 |
| 155 | build_patch = self.PatchObject( |
| 156 | image_service, "Build", return_value=result |
| 157 | ) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 158 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | image_controller.Create(request, self.response, self.api_config) |
| 160 | build_patch.assert_any_call( |
| 161 | "board", [constants.IMAGE_TYPE_BASE], config=mock.ANY |
| 162 | ) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | def testFailedPackageHandling(self): |
| 165 | """Test failed packages are populated correctly.""" |
| 166 | result = image_service.BuildResult([]) |
| 167 | result.return_code = 1 |
| 168 | result.failed_packages = ["foo/bar", "cat/pkg"] |
| 169 | expected_packages = [("foo", "bar"), ("cat", "pkg")] |
| 170 | self.PatchObject(image_service, "Build", return_value=result) |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | input_proto = self._GetRequest(board="board") |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | rc = image_controller.Create( |
| 175 | input_proto, self.response, self.api_config |
| 176 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | self.assertEqual( |
| 179 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 180 | ) |
| 181 | for package in self.response.failed_packages: |
| 182 | self.assertIn( |
| 183 | (package.category, package.package_name), expected_packages |
| 184 | ) |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | def testNoPackagesFailureHandling(self): |
| 187 | """Test failed packages are populated correctly.""" |
| 188 | result = image_service.BuildResult([]) |
| 189 | result.return_code = 1 |
| 190 | self.PatchObject(image_service, "Build", return_value=result) |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 191 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 192 | input_proto = image_pb2.CreateImageRequest() |
| 193 | input_proto.build_target.name = "board" |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | rc = image_controller.Create( |
| 196 | input_proto, self.response, self.api_config |
| 197 | ) |
| 198 | self.assertTrue(rc) |
| 199 | self.assertNotEqual( |
| 200 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 201 | ) |
| 202 | self.assertFalse(self.response.failed_packages) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 203 | |
Jack Neus | 70490b5 | 2022-09-12 14:41:45 +0000 | [diff] [blame] | 204 | def testFactory(self): |
| 205 | """Test it's properly building factory.""" |
| 206 | request = self._GetRequest( |
| 207 | board="board", |
| 208 | types=[ |
| 209 | common_pb2.IMAGE_TYPE_FACTORY, |
| 210 | common_pb2.IMAGE_TYPE_NETBOOT, |
| 211 | ], |
| 212 | ) |
| 213 | factory_path = self.tempdir / "factory-shim" |
| 214 | factory_path.touch() |
| 215 | result = image_service.BuildResult([constants.IMAGE_TYPE_FACTORY_SHIM]) |
| 216 | result.add_image(constants.IMAGE_TYPE_FACTORY_SHIM, factory_path) |
| 217 | result.return_code = 0 |
| 218 | build_patch = self.PatchObject( |
| 219 | image_service, "Build", return_value=result |
| 220 | ) |
| 221 | netboot_patch = self.PatchObject(image_service, "create_netboot_kernel") |
| 222 | |
| 223 | image_controller.Create(request, self.response, self.api_config) |
| 224 | build_patch.assert_any_call( |
| 225 | "board", [constants.IMAGE_TYPE_FACTORY_SHIM], config=mock.ANY |
| 226 | ) |
Madeleine Hardt | f0a92fc | 2022-09-19 18:41:12 +0000 | [diff] [blame] | 227 | netboot_patch.assert_any_call("board", os.path.dirname(factory_path)) |
Jack Neus | 70490b5 | 2022-09-12 14:41:45 +0000 | [diff] [blame] | 228 | |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 229 | |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 230 | class GetArtifactsTest( |
| 231 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 232 | ): |
| 233 | """GetArtifacts function tests.""" |
| 234 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 235 | # pylint: disable=line-too-long |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 236 | _artifact_funcs = { |
| 237 | common_pb2.ArtifactsByService.Image.ArtifactType.DLC_IMAGE: image_service.copy_dlc_image, |
| 238 | common_pb2.ArtifactsByService.Image.ArtifactType.LICENSE_CREDITS: image_service.copy_license_credits, |
| 239 | common_pb2.ArtifactsByService.Image.ArtifactType.FACTORY_IMAGE: image_service.create_factory_image_zip, |
| 240 | common_pb2.ArtifactsByService.Image.ArtifactType.STRIPPED_PACKAGES: image_service.create_stripped_packages_tar, |
Jack Neus | 9184600 | 2022-12-01 23:49:22 +0000 | [diff] [blame] | 241 | common_pb2.ArtifactsByService.Image.ArtifactType.IMAGE_SCRIPTS: image_service.create_image_scripts_archive, |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 242 | } |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 243 | # pylint: enable=line-too-long |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 244 | |
| 245 | def setUp(self): |
| 246 | self._mocks = {} |
| 247 | for artifact, func in self._artifact_funcs.items(): |
| 248 | self._mocks[artifact] = self.PatchObject( |
| 249 | image_service, func.__name__ |
| 250 | ) |
| 251 | self.chroot = chroot_lib.Chroot( |
Brian Norris | 4f251e4 | 2023-03-09 15:53:26 -0800 | [diff] [blame] | 252 | path=self.tempdir / "chroot", |
| 253 | chrome_root=self.tempdir, |
| 254 | out_path=self.tempdir / "out", |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 255 | ) |
| 256 | board = "chell" |
| 257 | sysroot_path = "/build/%s" % board |
| 258 | self.sysroot_class = sysroot_lib.Sysroot(sysroot_path) |
| 259 | self.build_target = build_target_lib.BuildTarget(board) |
| 260 | |
| 261 | def _InputProto( |
| 262 | self, |
| 263 | artifact_types=_artifact_funcs.keys(), |
| 264 | ): |
| 265 | """Helper to build an input proto instance.""" |
| 266 | return common_pb2.ArtifactsByService.Image( |
| 267 | output_artifacts=[ |
| 268 | common_pb2.ArtifactsByService.Image.ArtifactInfo( |
| 269 | artifact_types=artifact_types |
| 270 | ) |
| 271 | ] |
| 272 | ) |
| 273 | |
| 274 | def testNoArtifacts(self): |
| 275 | """Test GetArtifacts with no artifact types.""" |
| 276 | in_proto = self._InputProto(artifact_types=[]) |
| 277 | image_controller.GetArtifacts( |
| 278 | in_proto, self.chroot, self.sysroot_class, self.build_target, "" |
| 279 | ) |
| 280 | |
| 281 | for _, patch in self._mocks.items(): |
| 282 | patch.assert_not_called() |
| 283 | |
| 284 | def testArtifactsSuccess(self): |
| 285 | """Test GetArtifacts with all artifact types.""" |
| 286 | image_controller.GetArtifacts( |
| 287 | self._InputProto(), |
| 288 | self.chroot, |
| 289 | self.sysroot_class, |
| 290 | self.build_target, |
| 291 | "", |
| 292 | ) |
| 293 | |
| 294 | for _, patch in self._mocks.items(): |
| 295 | patch.assert_called_once() |
| 296 | |
| 297 | def testArtifactsException(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 298 | """Test with all artifact types when one type throws an exception.""" |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 299 | |
| 300 | self._mocks[ |
| 301 | common_pb2.ArtifactsByService.Image.ArtifactType.STRIPPED_PACKAGES |
| 302 | ].side_effect = Exception("foo bar") |
| 303 | generated = image_controller.GetArtifacts( |
| 304 | self._InputProto(), |
| 305 | self.chroot, |
| 306 | self.sysroot_class, |
| 307 | self.build_target, |
| 308 | "", |
| 309 | ) |
| 310 | |
| 311 | for _, patch in self._mocks.items(): |
| 312 | patch.assert_called_once() |
| 313 | |
| 314 | found_artifact = False |
| 315 | for data in generated: |
| 316 | artifact_type = ( |
| 317 | common_pb2.ArtifactsByService.Image.ArtifactType.Name( |
| 318 | data["type"] |
| 319 | ) |
| 320 | ) |
| 321 | if artifact_type == "STRIPPED_PACKAGES": |
| 322 | found_artifact = True |
| 323 | self.assertTrue(data["failed"]) |
| 324 | self.assertEqual(data["failure_reason"], "foo bar") |
| 325 | self.assertTrue(found_artifact) |
| 326 | |
| 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | class RecoveryImageTest( |
| 329 | cros_test_lib.RunCommandTempDirTestCase, api_config.ApiConfigMixin |
| 330 | ): |
| 331 | """Recovery image tests.""" |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 332 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 333 | def setUp(self): |
| 334 | self.response = image_pb2.CreateImageResult() |
| 335 | self.types = [ |
| 336 | common_pb2.IMAGE_TYPE_BASE, |
| 337 | common_pb2.IMAGE_TYPE_RECOVERY, |
| 338 | ] |
| 339 | self.build_result = self._CreateMockBuildResult( |
| 340 | [common_pb2.IMAGE_TYPE_BASE] |
| 341 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 342 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | self.PatchObject( |
| 344 | image_service, |
| 345 | "Build", |
| 346 | side_effect=[ |
| 347 | self.build_result, |
| 348 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_FACTORY]), |
| 349 | ], |
| 350 | ) |
| 351 | self.copy_image_mock = self.PatchObject( |
| 352 | image_service, |
| 353 | "CopyBaseToRecovery", |
| 354 | side_effect=[ |
| 355 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_RECOVERY]), |
| 356 | ], |
| 357 | ) |
| 358 | self.recov_image_mock = self.PatchObject( |
| 359 | image_service, |
| 360 | "BuildRecoveryImage", |
| 361 | side_effect=[ |
| 362 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_RECOVERY]), |
| 363 | ], |
| 364 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 365 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | def _GetRequest( |
| 367 | self, |
| 368 | board=None, |
| 369 | types=None, |
| 370 | version=None, |
| 371 | builder_path=None, |
| 372 | disable_rootfs_verification=False, |
| 373 | base_is_recovery=False, |
| 374 | ): |
| 375 | """Helper to build a request instance.""" |
| 376 | return image_pb2.CreateImageRequest( |
| 377 | build_target={"name": board}, |
| 378 | image_types=types, |
| 379 | disable_rootfs_verification=disable_rootfs_verification, |
| 380 | version=version, |
| 381 | builder_path=builder_path, |
| 382 | base_is_recovery=base_is_recovery, |
| 383 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 384 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 385 | def _CreateMockBuildResult( |
| 386 | self, image_types: List[int] |
| 387 | ) -> Optional[image_service.BuildResult]: |
Jack Rosenthal | 8d3fc83 | 2023-06-16 20:04:37 -0600 | [diff] [blame] | 388 | """Helper to create Mock `cros build-image` results. |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 389 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 390 | Args: |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 391 | image_types: A list of image types for which the mock BuildResult |
| 392 | has to be created. |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 395 | A list of mock BuildResult. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 396 | """ |
| 397 | image_types_names = [ |
| 398 | image_controller.SUPPORTED_IMAGE_TYPES[x] |
| 399 | for x in image_types |
| 400 | if image_controller.SUPPORTED_IMAGE_TYPES[x] |
| 401 | in constants.IMAGE_TYPE_TO_NAME |
| 402 | ] |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 403 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 404 | if not image_types_names: |
| 405 | if ( |
| 406 | common_pb2.IMAGE_TYPE_FACTORY in image_types |
| 407 | and len(image_types) == 1 |
| 408 | ): |
| 409 | image_types_names.append(constants.IMAGE_TYPE_FACTORY_SHIM) |
| 410 | else: |
| 411 | return None |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 412 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 413 | _build_result = image_service.BuildResult(image_types_names) |
| 414 | _build_result.return_code = 0 |
| 415 | for image_type in image_types_names: |
| 416 | test_image = ( |
| 417 | Path(self.tempdir) / constants.IMAGE_TYPE_TO_NAME[image_type] |
| 418 | ) |
| 419 | test_image.touch() |
| 420 | _build_result.add_image(image_type, test_image) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 421 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 422 | return _build_result |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 423 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | def testBaseIsRecoveryTrue(self): |
| 425 | """Test that cp is called.""" |
| 426 | input_proto = self._GetRequest( |
| 427 | board="board", types=self.types, base_is_recovery=True |
| 428 | ) |
| 429 | image_controller.Create(input_proto, self.response, self.api_config) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | self.copy_image_mock.assert_called_with( |
| 432 | board="board", |
| 433 | image_path=self.build_result.images[constants.IMAGE_TYPE_BASE], |
| 434 | ) |
Joseph Sussman | 34c01be | 2022-06-03 14:04:41 +0000 | [diff] [blame] | 435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | def testBaseIsRecoveryFalse(self): |
| 437 | """Test that mod_image_for_recovery.sh is called.""" |
| 438 | input_proto = self._GetRequest( |
| 439 | board="board", types=self.types, base_is_recovery=False |
| 440 | ) |
| 441 | image_controller.Create(input_proto, self.response, self.api_config) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 442 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 443 | self.recov_image_mock.assert_called_with( |
| 444 | board="board", |
| 445 | image_path=self.build_result.images[constants.IMAGE_TYPE_BASE], |
| 446 | ) |
Joseph Sussman | 34c01be | 2022-06-03 14:04:41 +0000 | [diff] [blame] | 447 | |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 448 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 449 | class ImageSignerTestTest( |
| 450 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 451 | ): |
| 452 | """Image signer test tests.""" |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 453 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | def setUp(self): |
| 455 | self.image_path = os.path.join(self.tempdir, "image.bin") |
| 456 | self.result_directory = os.path.join(self.tempdir, "results") |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 457 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 458 | osutils.SafeMakedirs(self.result_directory) |
| 459 | osutils.Touch(self.image_path) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 460 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 461 | def testValidateOnly(self): |
| 462 | """Sanity check that validate-only calls don't execute any logic.""" |
| 463 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 464 | input_proto = image_pb2.TestImageRequest() |
| 465 | input_proto.image.path = self.image_path |
| 466 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | image_controller.SignerTest( |
| 469 | input_proto, output_proto, self.validate_only_config |
| 470 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 471 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 472 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 473 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 475 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 476 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 477 | input_proto = image_pb2.TestImageRequest() |
| 478 | input_proto.image.path = self.image_path |
| 479 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 480 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 481 | image_controller.SignerTest( |
| 482 | input_proto, output_proto, self.mock_call_config |
| 483 | ) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 484 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 485 | patch.assert_not_called() |
| 486 | self.assertEqual(output_proto.success, True) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 487 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 488 | def testMockError(self): |
| 489 | """Test that mock call does not execute any logic, returns error.""" |
| 490 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 491 | input_proto = image_pb2.TestImageRequest() |
| 492 | input_proto.image.path = self.image_path |
| 493 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 494 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 495 | rc = image_controller.SignerTest( |
| 496 | input_proto, output_proto, self.mock_error_config |
| 497 | ) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 498 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 499 | patch.assert_not_called() |
| 500 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 501 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 502 | def testSignerTestNoImage(self): |
| 503 | """Test function argument validation.""" |
| 504 | input_proto = image_pb2.TestImageRequest() |
| 505 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 506 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | # Nothing provided. |
| 508 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 509 | image_controller.SignerTest( |
| 510 | input_proto, output_proto, self.api_config |
| 511 | ) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 512 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 513 | def testSignerTestSuccess(self): |
| 514 | """Test successful call handling.""" |
| 515 | self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 516 | input_proto = image_pb2.TestImageRequest() |
| 517 | input_proto.image.path = self.image_path |
| 518 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 519 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 521 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 522 | def testSignerTestFailure(self): |
| 523 | """Test function output tests.""" |
| 524 | input_proto = image_pb2.TestImageRequest() |
| 525 | input_proto.image.path = self.image_path |
| 526 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 527 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 528 | self.PatchObject(image_lib, "SecurityTest", return_value=False) |
| 529 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
| 530 | self.assertFalse(output_proto.success) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 531 | |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | class ImageTestTest( |
| 534 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 535 | ): |
| 536 | """Image test tests.""" |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 537 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 538 | def setUp(self): |
| 539 | self.image_path = os.path.join(self.tempdir, "image.bin") |
| 540 | self.board = "board" |
| 541 | self.result_directory = os.path.join(self.tempdir, "results") |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 542 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 543 | osutils.SafeMakedirs(self.result_directory) |
| 544 | osutils.Touch(self.image_path) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 545 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 547 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | patch = self.PatchObject(image_service, "Test") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 549 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 550 | input_proto = image_pb2.TestImageRequest() |
| 551 | input_proto.image.path = self.image_path |
| 552 | input_proto.build_target.name = self.board |
| 553 | input_proto.result.directory = self.result_directory |
| 554 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 555 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 556 | image_controller.Test( |
| 557 | input_proto, output_proto, self.validate_only_config |
| 558 | ) |
| 559 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 560 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 562 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 563 | patch = self.PatchObject(image_service, "Test") |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 564 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 565 | input_proto = image_pb2.TestImageRequest() |
| 566 | input_proto.image.path = self.image_path |
| 567 | input_proto.build_target.name = self.board |
| 568 | input_proto.result.directory = self.result_directory |
| 569 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 570 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 571 | image_controller.Test(input_proto, output_proto, self.mock_call_config) |
| 572 | patch.assert_not_called() |
| 573 | self.assertEqual(output_proto.success, True) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 574 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 575 | def testMockError(self): |
| 576 | """Test that mock call does not execute any logic, returns error.""" |
| 577 | patch = self.PatchObject(image_service, "Test") |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 578 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 579 | input_proto = image_pb2.TestImageRequest() |
| 580 | input_proto.image.path = self.image_path |
| 581 | input_proto.build_target.name = self.board |
| 582 | input_proto.result.directory = self.result_directory |
| 583 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 584 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 585 | rc = image_controller.Test( |
| 586 | input_proto, output_proto, self.mock_error_config |
| 587 | ) |
| 588 | patch.assert_not_called() |
| 589 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 590 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 591 | def testTestArgumentValidation(self): |
| 592 | """Test function argument validation tests.""" |
| 593 | self.PatchObject(image_service, "Test", return_value=True) |
| 594 | input_proto = image_pb2.TestImageRequest() |
| 595 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 596 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 597 | # Nothing provided. |
| 598 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 599 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 600 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 601 | # Just one argument. |
| 602 | input_proto.build_target.name = self.board |
| 603 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 604 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 605 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 606 | # Two arguments provided. |
| 607 | input_proto.result.directory = self.result_directory |
| 608 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 609 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 610 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 611 | # Invalid image path. |
| 612 | input_proto.image.path = "/invalid/image/path" |
| 613 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 614 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 615 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 616 | # All valid arguments. |
| 617 | input_proto.image.path = self.image_path |
| 618 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 619 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 620 | def testTestOutputHandling(self): |
| 621 | """Test function output tests.""" |
| 622 | input_proto = image_pb2.TestImageRequest() |
| 623 | input_proto.image.path = self.image_path |
| 624 | input_proto.build_target.name = self.board |
| 625 | input_proto.result.directory = self.result_directory |
| 626 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 627 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 628 | self.PatchObject(image_service, "Test", return_value=True) |
| 629 | image_controller.Test(input_proto, output_proto, self.api_config) |
| 630 | self.assertTrue(output_proto.success) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 631 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 632 | self.PatchObject(image_service, "Test", return_value=False) |
| 633 | image_controller.Test(input_proto, output_proto, self.api_config) |
| 634 | self.assertFalse(output_proto.success) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 635 | |
| 636 | |
Jack Neus | accffec | 2023-08-31 15:30:49 +0000 | [diff] [blame] | 637 | class PushImageTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 638 | """Push image test.""" |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 639 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 640 | def _GetRequest( |
| 641 | self, |
| 642 | gs_image_dir="gs://chromeos-image-archive/atlas-release/R89-13604.0.0", |
| 643 | build_target_name="atlas", |
| 644 | profile="foo", |
| 645 | sign_types=None, |
| 646 | dryrun=True, |
| 647 | channels=None, |
| 648 | ): |
| 649 | return image_pb2.PushImageRequest( |
| 650 | gs_image_dir=gs_image_dir, |
| 651 | sysroot=sysroot_pb2.Sysroot( |
| 652 | build_target=common_pb2.BuildTarget(name=build_target_name) |
| 653 | ), |
| 654 | profile=common_pb2.Profile(name=profile), |
| 655 | sign_types=sign_types, |
| 656 | dryrun=dryrun, |
| 657 | channels=channels, |
| 658 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 659 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 660 | def _GetResponse(self): |
Jack Neus | accffec | 2023-08-31 15:30:49 +0000 | [diff] [blame] | 661 | return image_pb2.PushImageResponse() |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 662 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 663 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 664 | def testValidateOnly(self, MockPushImage): |
| 665 | """Check that a validate only call does not execute any logic.""" |
| 666 | req = self._GetRequest( |
| 667 | sign_types=[ |
| 668 | common_pb2.IMAGE_TYPE_RECOVERY, |
| 669 | common_pb2.IMAGE_TYPE_FACTORY, |
| 670 | common_pb2.IMAGE_TYPE_FIRMWARE, |
| 671 | common_pb2.IMAGE_TYPE_ACCESSORY_USBPD, |
| 672 | common_pb2.IMAGE_TYPE_ACCESSORY_RWSIG, |
| 673 | common_pb2.IMAGE_TYPE_BASE, |
| 674 | common_pb2.IMAGE_TYPE_GSC_FIRMWARE, |
| 675 | common_pb2.IMAGE_TYPE_HPS_FIRMWARE, |
| 676 | ] |
| 677 | ) |
| 678 | rc = image_controller.PushImage( |
Jack Neus | accffec | 2023-08-31 15:30:49 +0000 | [diff] [blame] | 679 | req, self._GetResponse(), self.validate_only_config |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 680 | ) |
| 681 | MockPushImage.assert_not_called() |
| 682 | self.assertEqual(rc, controller.RETURN_CODE_VALID_INPUT) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 683 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 684 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 685 | def testValidateOnlyInvalid(self, MockPushImage): |
| 686 | """Check that validate call rejects invalid sign types.""" |
| 687 | # Pass unsupported image type. |
| 688 | req = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_DLC]) |
| 689 | rc = image_controller.PushImage( |
| 690 | req, self._GetResponse(), self.validate_only_config |
| 691 | ) |
| 692 | MockPushImage.assert_not_called() |
| 693 | self.assertEqual(rc, controller.RETURN_CODE_INVALID_INPUT) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 694 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 695 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 696 | def testMockCall(self, MockPushImage): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 697 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 698 | rc = image_controller.PushImage( |
| 699 | self._GetRequest(), self._GetResponse(), self.mock_call_config |
| 700 | ) |
| 701 | MockPushImage.assert_not_called() |
| 702 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 703 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 704 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 705 | def testMockError(self, MockPushImage): |
| 706 | """Test that mock call does not execute any logic, returns error.""" |
| 707 | rc = image_controller.PushImage( |
| 708 | self._GetRequest(), self._GetResponse(), self.mock_error_config |
| 709 | ) |
| 710 | MockPushImage.assert_not_called() |
| 711 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 712 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 713 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 714 | def testNoBuildTarget(self, _): |
| 715 | """Test no build target given fails.""" |
| 716 | request = self._GetRequest(build_target_name="") |
| 717 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 718 | image_controller.PushImage( |
| 719 | request, self._GetResponse(), self.api_config |
| 720 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 721 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 722 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 723 | def testNoGsImageDir(self, _): |
| 724 | """Test no image dir given fails.""" |
| 725 | request = self._GetRequest(gs_image_dir="") |
| 726 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 727 | image_controller.PushImage( |
| 728 | request, self._GetResponse(), self.api_config |
| 729 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 730 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 731 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 732 | def testCallCorrect(self, MockPushImage): |
| 733 | """Check that a call is called with the correct parameters.""" |
| 734 | request = self._GetRequest( |
| 735 | dryrun=False, |
| 736 | profile="", |
| 737 | sign_types=[common_pb2.IMAGE_TYPE_RECOVERY], |
| 738 | channels=[common_pb2.CHANNEL_DEV, common_pb2.CHANNEL_CANARY], |
| 739 | ) |
| 740 | request.dest_bucket = "gs://foo" |
| 741 | image_controller.PushImage( |
| 742 | request, self._GetResponse(), self.api_config |
| 743 | ) |
| 744 | MockPushImage.assert_called_with( |
| 745 | request.gs_image_dir, |
| 746 | request.sysroot.build_target.name, |
Mike Frysinger | 9d1253c | 2023-02-02 09:00:07 -0500 | [diff] [blame] | 747 | dryrun=request.dryrun, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 748 | sign_types=["recovery"], |
| 749 | dest_bucket=request.dest_bucket, |
| 750 | force_channels=["dev", "canary"], |
| 751 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 752 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 753 | @mock.patch.object( |
| 754 | pushimage, |
| 755 | "PushImage", |
| 756 | return_value={ |
| 757 | "dev": ["gs://dev/instr1", "gs://dev/instr2"], |
| 758 | "canary": ["gs://canary/instr1"], |
| 759 | }, |
| 760 | ) |
| 761 | def testOutput(self, _): |
| 762 | """Check that a call populates the response object.""" |
| 763 | request = self._GetRequest( |
| 764 | profile="", |
| 765 | sign_types=[common_pb2.IMAGE_TYPE_RECOVERY], |
| 766 | channels=[common_pb2.CHANNEL_DEV, common_pb2.CHANNEL_CANARY], |
| 767 | ) |
| 768 | request.dest_bucket = "gs://foo" |
| 769 | response = self._GetResponse() |
| 770 | image_controller.PushImage(request, response, self.api_config) |
| 771 | self.assertEqual( |
| 772 | sorted([i.instructions_file_path for i in response.instructions]), |
| 773 | sorted( |
| 774 | ["gs://dev/instr1", "gs://dev/instr2", "gs://canary/instr1"] |
| 775 | ), |
| 776 | ) |
Greg Edelston | 6902e3d | 2022-01-27 12:19:38 -0700 | [diff] [blame] | 777 | |
Jack Neus | accffec | 2023-08-31 15:30:49 +0000 | [diff] [blame] | 778 | def testCallSucceeds(self): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 779 | """Check that a (dry run) call is made successfully.""" |
| 780 | request = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_RECOVERY]) |
| 781 | rc = image_controller.PushImage( |
| 782 | request, self._GetResponse(), self.api_config |
| 783 | ) |
| 784 | self.assertEqual(rc, controller.RETURN_CODE_SUCCESS) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 785 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 786 | def testCallFailsWithBadImageDir(self): |
| 787 | """Check that a (dry run) call fails when given a bad gs_image_dir.""" |
| 788 | request = self._GetRequest(gs_image_dir="foo") |
| 789 | rc = image_controller.PushImage( |
| 790 | request, self._GetResponse, self.api_config |
| 791 | ) |
| 792 | self.assertEqual(rc, controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY) |
Jack Neus | fa4eca1 | 2023-08-30 20:10:08 +0000 | [diff] [blame] | 793 | |
| 794 | |
Jack Neus | 8b28d46 | 2023-09-27 18:02:25 +0000 | [diff] [blame] | 795 | class SignImageTest( |
| 796 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 797 | ): |
Jack Neus | fa4eca1 | 2023-08-30 20:10:08 +0000 | [diff] [blame] | 798 | """Sign image test.""" |
| 799 | |
| 800 | def testValidateOnly(self): |
| 801 | """Check that a validate only call does not execute any logic.""" |
Benjamin Shai | 30b5e95 | 2023-09-19 22:28:19 +0000 | [diff] [blame] | 802 | req = image_pb2.SignImageRequest( |
Jack Neus | 8b28d46 | 2023-09-27 18:02:25 +0000 | [diff] [blame] | 803 | archive_dir=str(self.tempdir), |
Benjamin Shai | 30b5e95 | 2023-09-19 22:28:19 +0000 | [diff] [blame] | 804 | result_path=common_pb2.ResultPath( |
| 805 | path=common_pb2.Path( |
| 806 | path="/path/to/outside", |
| 807 | location=common_pb2.Path.OUTSIDE, |
| 808 | ) |
Jack Neus | 8b28d46 | 2023-09-27 18:02:25 +0000 | [diff] [blame] | 809 | ), |
Benjamin Shai | 30b5e95 | 2023-09-19 22:28:19 +0000 | [diff] [blame] | 810 | ) |
Jack Neus | fa4eca1 | 2023-08-30 20:10:08 +0000 | [diff] [blame] | 811 | resp = image_pb2.SignImageResponse() |
| 812 | rc = image_controller.SignImage(req, resp, self.validate_only_config) |
| 813 | self.assertEqual(rc, controller.RETURN_CODE_VALID_INPUT) |
| 814 | |
| 815 | def testSuccess(self): |
| 816 | """Check that the endpoint finishes successfully.""" |
| 817 | req = image_pb2.SignImageRequest() |
| 818 | resp = image_pb2.SignImageResponse() |
| 819 | rc = image_controller.SignImage(req, resp, self.mock_call_config) |
| 820 | self.assertEqual(rc, controller.RETURN_CODE_SUCCESS) |