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( |
| 252 | path=self.tempdir, chrome_root=self.tempdir |
| 253 | ) |
| 254 | board = "chell" |
| 255 | sysroot_path = "/build/%s" % board |
| 256 | self.sysroot_class = sysroot_lib.Sysroot(sysroot_path) |
| 257 | self.build_target = build_target_lib.BuildTarget(board) |
| 258 | |
| 259 | def _InputProto( |
| 260 | self, |
| 261 | artifact_types=_artifact_funcs.keys(), |
| 262 | ): |
| 263 | """Helper to build an input proto instance.""" |
| 264 | return common_pb2.ArtifactsByService.Image( |
| 265 | output_artifacts=[ |
| 266 | common_pb2.ArtifactsByService.Image.ArtifactInfo( |
| 267 | artifact_types=artifact_types |
| 268 | ) |
| 269 | ] |
| 270 | ) |
| 271 | |
| 272 | def testNoArtifacts(self): |
| 273 | """Test GetArtifacts with no artifact types.""" |
| 274 | in_proto = self._InputProto(artifact_types=[]) |
| 275 | image_controller.GetArtifacts( |
| 276 | in_proto, self.chroot, self.sysroot_class, self.build_target, "" |
| 277 | ) |
| 278 | |
| 279 | for _, patch in self._mocks.items(): |
| 280 | patch.assert_not_called() |
| 281 | |
| 282 | def testArtifactsSuccess(self): |
| 283 | """Test GetArtifacts with all artifact types.""" |
| 284 | image_controller.GetArtifacts( |
| 285 | self._InputProto(), |
| 286 | self.chroot, |
| 287 | self.sysroot_class, |
| 288 | self.build_target, |
| 289 | "", |
| 290 | ) |
| 291 | |
| 292 | for _, patch in self._mocks.items(): |
| 293 | patch.assert_called_once() |
| 294 | |
| 295 | def testArtifactsException(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 296 | """Test with all artifact types when one type throws an exception.""" |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 297 | |
| 298 | self._mocks[ |
| 299 | common_pb2.ArtifactsByService.Image.ArtifactType.STRIPPED_PACKAGES |
| 300 | ].side_effect = Exception("foo bar") |
| 301 | generated = image_controller.GetArtifacts( |
| 302 | self._InputProto(), |
| 303 | self.chroot, |
| 304 | self.sysroot_class, |
| 305 | self.build_target, |
| 306 | "", |
| 307 | ) |
| 308 | |
| 309 | for _, patch in self._mocks.items(): |
| 310 | patch.assert_called_once() |
| 311 | |
| 312 | found_artifact = False |
| 313 | for data in generated: |
| 314 | artifact_type = ( |
| 315 | common_pb2.ArtifactsByService.Image.ArtifactType.Name( |
| 316 | data["type"] |
| 317 | ) |
| 318 | ) |
| 319 | if artifact_type == "STRIPPED_PACKAGES": |
| 320 | found_artifact = True |
| 321 | self.assertTrue(data["failed"]) |
| 322 | self.assertEqual(data["failure_reason"], "foo bar") |
| 323 | self.assertTrue(found_artifact) |
| 324 | |
| 325 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | class RecoveryImageTest( |
| 327 | cros_test_lib.RunCommandTempDirTestCase, api_config.ApiConfigMixin |
| 328 | ): |
| 329 | """Recovery image tests.""" |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 330 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 331 | def setUp(self): |
| 332 | self.response = image_pb2.CreateImageResult() |
| 333 | self.types = [ |
| 334 | common_pb2.IMAGE_TYPE_BASE, |
| 335 | common_pb2.IMAGE_TYPE_RECOVERY, |
| 336 | ] |
| 337 | self.build_result = self._CreateMockBuildResult( |
| 338 | [common_pb2.IMAGE_TYPE_BASE] |
| 339 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 340 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | self.PatchObject( |
| 342 | image_service, |
| 343 | "Build", |
| 344 | side_effect=[ |
| 345 | self.build_result, |
| 346 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_FACTORY]), |
| 347 | ], |
| 348 | ) |
| 349 | self.copy_image_mock = self.PatchObject( |
| 350 | image_service, |
| 351 | "CopyBaseToRecovery", |
| 352 | side_effect=[ |
| 353 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_RECOVERY]), |
| 354 | ], |
| 355 | ) |
| 356 | self.recov_image_mock = self.PatchObject( |
| 357 | image_service, |
| 358 | "BuildRecoveryImage", |
| 359 | side_effect=[ |
| 360 | self._CreateMockBuildResult([common_pb2.IMAGE_TYPE_RECOVERY]), |
| 361 | ], |
| 362 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 363 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | def _GetRequest( |
| 365 | self, |
| 366 | board=None, |
| 367 | types=None, |
| 368 | version=None, |
| 369 | builder_path=None, |
| 370 | disable_rootfs_verification=False, |
| 371 | base_is_recovery=False, |
| 372 | ): |
| 373 | """Helper to build a request instance.""" |
| 374 | return image_pb2.CreateImageRequest( |
| 375 | build_target={"name": board}, |
| 376 | image_types=types, |
| 377 | disable_rootfs_verification=disable_rootfs_verification, |
| 378 | version=version, |
| 379 | builder_path=builder_path, |
| 380 | base_is_recovery=base_is_recovery, |
| 381 | ) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 382 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 383 | def _CreateMockBuildResult( |
| 384 | self, image_types: List[int] |
| 385 | ) -> Optional[image_service.BuildResult]: |
| 386 | """Helper to create Mock build_image results. |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | Args: |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 389 | image_types: A list of image types for which the mock BuildResult |
| 390 | has to be created. |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 391 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 392 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 393 | A list of mock BuildResult. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | """ |
| 395 | image_types_names = [ |
| 396 | image_controller.SUPPORTED_IMAGE_TYPES[x] |
| 397 | for x in image_types |
| 398 | if image_controller.SUPPORTED_IMAGE_TYPES[x] |
| 399 | in constants.IMAGE_TYPE_TO_NAME |
| 400 | ] |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 401 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | if not image_types_names: |
| 403 | if ( |
| 404 | common_pb2.IMAGE_TYPE_FACTORY in image_types |
| 405 | and len(image_types) == 1 |
| 406 | ): |
| 407 | image_types_names.append(constants.IMAGE_TYPE_FACTORY_SHIM) |
| 408 | else: |
| 409 | return None |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 410 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 411 | _build_result = image_service.BuildResult(image_types_names) |
| 412 | _build_result.return_code = 0 |
| 413 | for image_type in image_types_names: |
| 414 | test_image = ( |
| 415 | Path(self.tempdir) / constants.IMAGE_TYPE_TO_NAME[image_type] |
| 416 | ) |
| 417 | test_image.touch() |
| 418 | _build_result.add_image(image_type, test_image) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | return _build_result |
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 | def testBaseIsRecoveryTrue(self): |
| 423 | """Test that cp is called.""" |
| 424 | input_proto = self._GetRequest( |
| 425 | board="board", types=self.types, base_is_recovery=True |
| 426 | ) |
| 427 | image_controller.Create(input_proto, self.response, self.api_config) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 428 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | self.copy_image_mock.assert_called_with( |
| 430 | board="board", |
| 431 | image_path=self.build_result.images[constants.IMAGE_TYPE_BASE], |
| 432 | ) |
Joseph Sussman | 34c01be | 2022-06-03 14:04:41 +0000 | [diff] [blame] | 433 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | def testBaseIsRecoveryFalse(self): |
| 435 | """Test that mod_image_for_recovery.sh is called.""" |
| 436 | input_proto = self._GetRequest( |
| 437 | board="board", types=self.types, base_is_recovery=False |
| 438 | ) |
| 439 | image_controller.Create(input_proto, self.response, self.api_config) |
Ram Chandrasekar | 53c77cb | 2022-06-09 22:16:42 +0000 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | self.recov_image_mock.assert_called_with( |
| 442 | board="board", |
| 443 | image_path=self.build_result.images[constants.IMAGE_TYPE_BASE], |
| 444 | ) |
Joseph Sussman | 34c01be | 2022-06-03 14:04:41 +0000 | [diff] [blame] | 445 | |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 446 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 447 | class ImageSignerTestTest( |
| 448 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 449 | ): |
| 450 | """Image signer test tests.""" |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 451 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 452 | def setUp(self): |
| 453 | self.image_path = os.path.join(self.tempdir, "image.bin") |
| 454 | self.result_directory = os.path.join(self.tempdir, "results") |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 455 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 456 | osutils.SafeMakedirs(self.result_directory) |
| 457 | osutils.Touch(self.image_path) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 458 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 459 | def testValidateOnly(self): |
| 460 | """Sanity check that validate-only calls don't execute any logic.""" |
| 461 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 462 | input_proto = image_pb2.TestImageRequest() |
| 463 | input_proto.image.path = self.image_path |
| 464 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 465 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | image_controller.SignerTest( |
| 467 | input_proto, output_proto, self.validate_only_config |
| 468 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 469 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 470 | patch.assert_not_called() |
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 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 473 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 475 | input_proto = image_pb2.TestImageRequest() |
| 476 | input_proto.image.path = self.image_path |
| 477 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 478 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 479 | image_controller.SignerTest( |
| 480 | input_proto, output_proto, self.mock_call_config |
| 481 | ) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 482 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 483 | patch.assert_not_called() |
| 484 | self.assertEqual(output_proto.success, True) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | def testMockError(self): |
| 487 | """Test that mock call does not execute any logic, returns error.""" |
| 488 | patch = self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 489 | input_proto = image_pb2.TestImageRequest() |
| 490 | input_proto.image.path = self.image_path |
| 491 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 492 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 493 | rc = image_controller.SignerTest( |
| 494 | input_proto, output_proto, self.mock_error_config |
| 495 | ) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | patch.assert_not_called() |
| 498 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 499 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 500 | def testSignerTestNoImage(self): |
| 501 | """Test function argument validation.""" |
| 502 | input_proto = image_pb2.TestImageRequest() |
| 503 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 504 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 505 | # Nothing provided. |
| 506 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 507 | image_controller.SignerTest( |
| 508 | input_proto, output_proto, self.api_config |
| 509 | ) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 510 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 511 | def testSignerTestSuccess(self): |
| 512 | """Test successful call handling.""" |
| 513 | self.PatchObject(image_lib, "SecurityTest", return_value=True) |
| 514 | input_proto = image_pb2.TestImageRequest() |
| 515 | input_proto.image.path = self.image_path |
| 516 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 517 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 518 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 519 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | def testSignerTestFailure(self): |
| 521 | """Test function output tests.""" |
| 522 | input_proto = image_pb2.TestImageRequest() |
| 523 | input_proto.image.path = self.image_path |
| 524 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 525 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 526 | self.PatchObject(image_lib, "SecurityTest", return_value=False) |
| 527 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
| 528 | self.assertFalse(output_proto.success) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 529 | |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 530 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 531 | class ImageTestTest( |
| 532 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 533 | ): |
| 534 | """Image test tests.""" |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 535 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 536 | def setUp(self): |
| 537 | self.image_path = os.path.join(self.tempdir, "image.bin") |
| 538 | self.board = "board" |
| 539 | self.result_directory = os.path.join(self.tempdir, "results") |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 540 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 541 | osutils.SafeMakedirs(self.result_directory) |
| 542 | osutils.Touch(self.image_path) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 543 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 544 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 545 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | patch = self.PatchObject(image_service, "Test") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 547 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | input_proto = image_pb2.TestImageRequest() |
| 549 | input_proto.image.path = self.image_path |
| 550 | input_proto.build_target.name = self.board |
| 551 | input_proto.result.directory = self.result_directory |
| 552 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 553 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 554 | image_controller.Test( |
| 555 | input_proto, output_proto, self.validate_only_config |
| 556 | ) |
| 557 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 558 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 559 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 560 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | patch = self.PatchObject(image_service, "Test") |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 562 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 563 | input_proto = image_pb2.TestImageRequest() |
| 564 | input_proto.image.path = self.image_path |
| 565 | input_proto.build_target.name = self.board |
| 566 | input_proto.result.directory = self.result_directory |
| 567 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 568 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 569 | image_controller.Test(input_proto, output_proto, self.mock_call_config) |
| 570 | patch.assert_not_called() |
| 571 | self.assertEqual(output_proto.success, True) |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 572 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 573 | def testMockError(self): |
| 574 | """Test that mock call does not execute any logic, returns error.""" |
| 575 | patch = self.PatchObject(image_service, "Test") |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 576 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 577 | input_proto = image_pb2.TestImageRequest() |
| 578 | input_proto.image.path = self.image_path |
| 579 | input_proto.build_target.name = self.board |
| 580 | input_proto.result.directory = self.result_directory |
| 581 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 582 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 583 | rc = image_controller.Test( |
| 584 | input_proto, output_proto, self.mock_error_config |
| 585 | ) |
| 586 | patch.assert_not_called() |
| 587 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 588 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 589 | def testTestArgumentValidation(self): |
| 590 | """Test function argument validation tests.""" |
| 591 | self.PatchObject(image_service, "Test", return_value=True) |
| 592 | input_proto = image_pb2.TestImageRequest() |
| 593 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 594 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 595 | # Nothing provided. |
| 596 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 597 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 598 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 599 | # Just one argument. |
| 600 | input_proto.build_target.name = self.board |
| 601 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 602 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 603 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 604 | # Two arguments provided. |
| 605 | input_proto.result.directory = self.result_directory |
| 606 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 607 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 608 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 609 | # Invalid image path. |
| 610 | input_proto.image.path = "/invalid/image/path" |
| 611 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 612 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 613 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 614 | # All valid arguments. |
| 615 | input_proto.image.path = self.image_path |
| 616 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 617 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 618 | def testTestOutputHandling(self): |
| 619 | """Test function output tests.""" |
| 620 | input_proto = image_pb2.TestImageRequest() |
| 621 | input_proto.image.path = self.image_path |
| 622 | input_proto.build_target.name = self.board |
| 623 | input_proto.result.directory = self.result_directory |
| 624 | output_proto = image_pb2.TestImageResult() |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 625 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 626 | self.PatchObject(image_service, "Test", return_value=True) |
| 627 | image_controller.Test(input_proto, output_proto, self.api_config) |
| 628 | self.assertTrue(output_proto.success) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 629 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 630 | self.PatchObject(image_service, "Test", return_value=False) |
| 631 | image_controller.Test(input_proto, output_proto, self.api_config) |
| 632 | self.assertFalse(output_proto.success) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 633 | |
| 634 | |
Greg Edelston | 6902e3d | 2022-01-27 12:19:38 -0700 | [diff] [blame] | 635 | class PushImageTest(api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 636 | """Push image test.""" |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 637 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 638 | def _GetRequest( |
| 639 | self, |
| 640 | gs_image_dir="gs://chromeos-image-archive/atlas-release/R89-13604.0.0", |
| 641 | build_target_name="atlas", |
| 642 | profile="foo", |
| 643 | sign_types=None, |
| 644 | dryrun=True, |
| 645 | channels=None, |
| 646 | ): |
| 647 | return image_pb2.PushImageRequest( |
| 648 | gs_image_dir=gs_image_dir, |
| 649 | sysroot=sysroot_pb2.Sysroot( |
| 650 | build_target=common_pb2.BuildTarget(name=build_target_name) |
| 651 | ), |
| 652 | profile=common_pb2.Profile(name=profile), |
| 653 | sign_types=sign_types, |
| 654 | dryrun=dryrun, |
| 655 | channels=channels, |
| 656 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 657 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 658 | def _GetResponse(self): |
| 659 | return image_pb2.PushImageRequest() |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 660 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 661 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 662 | def testValidateOnly(self, MockPushImage): |
| 663 | """Check that a validate only call does not execute any logic.""" |
| 664 | req = self._GetRequest( |
| 665 | sign_types=[ |
| 666 | common_pb2.IMAGE_TYPE_RECOVERY, |
| 667 | common_pb2.IMAGE_TYPE_FACTORY, |
| 668 | common_pb2.IMAGE_TYPE_FIRMWARE, |
| 669 | common_pb2.IMAGE_TYPE_ACCESSORY_USBPD, |
| 670 | common_pb2.IMAGE_TYPE_ACCESSORY_RWSIG, |
| 671 | common_pb2.IMAGE_TYPE_BASE, |
| 672 | common_pb2.IMAGE_TYPE_GSC_FIRMWARE, |
| 673 | common_pb2.IMAGE_TYPE_HPS_FIRMWARE, |
| 674 | ] |
| 675 | ) |
| 676 | rc = image_controller.PushImage( |
| 677 | req, self.NewResponse(), self.validate_only_config |
| 678 | ) |
| 679 | MockPushImage.assert_not_called() |
| 680 | self.assertEqual(rc, controller.RETURN_CODE_VALID_INPUT) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 681 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 682 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 683 | def testValidateOnlyInvalid(self, MockPushImage): |
| 684 | """Check that validate call rejects invalid sign types.""" |
| 685 | # Pass unsupported image type. |
| 686 | req = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_DLC]) |
| 687 | rc = image_controller.PushImage( |
| 688 | req, self._GetResponse(), self.validate_only_config |
| 689 | ) |
| 690 | MockPushImage.assert_not_called() |
| 691 | self.assertEqual(rc, controller.RETURN_CODE_INVALID_INPUT) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 692 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 693 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 694 | def testMockCall(self, MockPushImage): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 695 | """Test mock call does not execute any logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 696 | rc = image_controller.PushImage( |
| 697 | self._GetRequest(), self._GetResponse(), self.mock_call_config |
| 698 | ) |
| 699 | MockPushImage.assert_not_called() |
| 700 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 701 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 702 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 703 | def testMockError(self, MockPushImage): |
| 704 | """Test that mock call does not execute any logic, returns error.""" |
| 705 | rc = image_controller.PushImage( |
| 706 | self._GetRequest(), self._GetResponse(), self.mock_error_config |
| 707 | ) |
| 708 | MockPushImage.assert_not_called() |
| 709 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 710 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 711 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 712 | def testNoBuildTarget(self, _): |
| 713 | """Test no build target given fails.""" |
| 714 | request = self._GetRequest(build_target_name="") |
| 715 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 716 | image_controller.PushImage( |
| 717 | request, self._GetResponse(), self.api_config |
| 718 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 719 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 720 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 721 | def testNoGsImageDir(self, _): |
| 722 | """Test no image dir given fails.""" |
| 723 | request = self._GetRequest(gs_image_dir="") |
| 724 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 725 | image_controller.PushImage( |
| 726 | request, self._GetResponse(), self.api_config |
| 727 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 728 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 729 | @mock.patch.object(pushimage, "PushImage", return_value={}) |
| 730 | def testCallCorrect(self, MockPushImage): |
| 731 | """Check that a call is called with the correct parameters.""" |
| 732 | request = self._GetRequest( |
| 733 | dryrun=False, |
| 734 | profile="", |
| 735 | sign_types=[common_pb2.IMAGE_TYPE_RECOVERY], |
| 736 | channels=[common_pb2.CHANNEL_DEV, common_pb2.CHANNEL_CANARY], |
| 737 | ) |
| 738 | request.dest_bucket = "gs://foo" |
| 739 | image_controller.PushImage( |
| 740 | request, self._GetResponse(), self.api_config |
| 741 | ) |
| 742 | MockPushImage.assert_called_with( |
| 743 | request.gs_image_dir, |
| 744 | request.sysroot.build_target.name, |
| 745 | dry_run=request.dryrun, |
| 746 | sign_types=["recovery"], |
| 747 | dest_bucket=request.dest_bucket, |
| 748 | force_channels=["dev", "canary"], |
| 749 | ) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 750 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 751 | @mock.patch.object( |
| 752 | pushimage, |
| 753 | "PushImage", |
| 754 | return_value={ |
| 755 | "dev": ["gs://dev/instr1", "gs://dev/instr2"], |
| 756 | "canary": ["gs://canary/instr1"], |
| 757 | }, |
| 758 | ) |
| 759 | def testOutput(self, _): |
| 760 | """Check that a call populates the response object.""" |
| 761 | request = self._GetRequest( |
| 762 | profile="", |
| 763 | sign_types=[common_pb2.IMAGE_TYPE_RECOVERY], |
| 764 | channels=[common_pb2.CHANNEL_DEV, common_pb2.CHANNEL_CANARY], |
| 765 | ) |
| 766 | request.dest_bucket = "gs://foo" |
| 767 | response = self._GetResponse() |
| 768 | image_controller.PushImage(request, response, self.api_config) |
| 769 | self.assertEqual( |
| 770 | sorted([i.instructions_file_path for i in response.instructions]), |
| 771 | sorted( |
| 772 | ["gs://dev/instr1", "gs://dev/instr2", "gs://canary/instr1"] |
| 773 | ), |
| 774 | ) |
Greg Edelston | 6902e3d | 2022-01-27 12:19:38 -0700 | [diff] [blame] | 775 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 776 | def testCallSucceeds(self, _): |
| 777 | """Check that a (dry run) call is made successfully.""" |
| 778 | request = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_RECOVERY]) |
| 779 | rc = image_controller.PushImage( |
| 780 | request, self._GetResponse(), self.api_config |
| 781 | ) |
| 782 | self.assertEqual(rc, controller.RETURN_CODE_SUCCESS) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 783 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 784 | def testCallFailsWithBadImageDir(self): |
| 785 | """Check that a (dry run) call fails when given a bad gs_image_dir.""" |
| 786 | request = self._GetRequest(gs_image_dir="foo") |
| 787 | rc = image_controller.PushImage( |
| 788 | request, self._GetResponse, self.api_config |
| 789 | ) |
| 790 | self.assertEqual(rc, controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY) |