Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Image service tests.""" |
| 6 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 7 | import os |
| 8 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 9 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 10 | from chromite.api import controller |
| 11 | from chromite.api.controller import image as image_controller |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import image_pb2 |
David Burger | 13e06be | 2019-05-13 20:33:16 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromiumos import common_pb2 |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 14 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 15 | from chromite.lib import constants |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_test_lib |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 18 | from chromite.lib import image_lib |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 19 | from chromite.lib import osutils |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 20 | from chromite.scripts import pushimage |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 21 | from chromite.service import image as image_service |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 22 | from chromite.third_party import mock |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 23 | |
| 24 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 25 | class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin): |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 26 | """Create image tests.""" |
| 27 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 28 | def setUp(self): |
| 29 | self.response = image_pb2.CreateImageResult() |
| 30 | |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 31 | def _GetRequest(self, |
| 32 | board=None, |
| 33 | types=None, |
| 34 | version=None, |
| 35 | builder_path=None, |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 36 | disable_rootfs_verification=False): |
| 37 | """Helper to build a request instance.""" |
| 38 | return image_pb2.CreateImageRequest( |
| 39 | build_target={'name': board}, |
| 40 | image_types=types, |
| 41 | disable_rootfs_verification=disable_rootfs_verification, |
| 42 | version=version, |
| 43 | builder_path=builder_path, |
| 44 | ) |
| 45 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 46 | def testValidateOnly(self): |
| 47 | """Sanity check that a validate only call does not execute any logic.""" |
| 48 | patch = self.PatchObject(image_service, 'Build') |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 49 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 50 | request = self._GetRequest(board='board') |
| 51 | image_controller.Create(request, self.response, self.validate_only_config) |
| 52 | patch.assert_not_called() |
| 53 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 54 | def testMockCall(self): |
| 55 | """Test that mock call does not execute any logic, returns mocked value.""" |
| 56 | patch = self.PatchObject(image_service, 'Build') |
| 57 | |
| 58 | request = self._GetRequest(board='board') |
| 59 | image_controller.Create(request, self.response, self.mock_call_config) |
| 60 | patch.assert_not_called() |
| 61 | self.assertEqual(self.response.success, True) |
| 62 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 63 | def testMockError(self): |
| 64 | """Test that mock call does not execute any logic, returns error.""" |
| 65 | patch = self.PatchObject(image_service, 'Build') |
| 66 | |
| 67 | request = self._GetRequest(board='board') |
| 68 | rc = image_controller.Create(request, self.response, self.mock_error_config) |
| 69 | patch.assert_not_called() |
| 70 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 71 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 72 | def testNoBoard(self): |
| 73 | """Test no board given fails.""" |
| 74 | request = self._GetRequest() |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 75 | |
| 76 | # No board should cause it to fail. |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 77 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 78 | image_controller.Create(request, self.response, self.api_config) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 79 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 80 | def testNoTypeSpecified(self): |
| 81 | """Test the image type default.""" |
| 82 | request = self._GetRequest(board='board') |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 84 | # Failed result to avoid the success handling logic. |
| 85 | result = image_service.BuildResult(1, []) |
| 86 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 87 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 88 | image_controller.Create(request, self.response, self.api_config) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 89 | build_patch.assert_called_with( |
| 90 | images=[constants.IMAGE_TYPE_BASE], board='board', config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 91 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 92 | def testSingleTypeSpecified(self): |
| 93 | """Test it's properly using a specified type.""" |
George Engelbrecht | c55d631 | 2021-05-05 12:11:13 -0600 | [diff] [blame] | 94 | request = self._GetRequest(board='board', types=[common_pb2.IMAGE_TYPE_DEV]) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 95 | |
| 96 | # Failed result to avoid the success handling logic. |
| 97 | result = image_service.BuildResult(1, []) |
| 98 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
| 99 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 100 | image_controller.Create(request, self.response, self.api_config) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 101 | build_patch.assert_called_with( |
| 102 | images=[constants.IMAGE_TYPE_DEV], board='board', config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 103 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 104 | def testMultipleAndImpliedTypes(self): |
| 105 | """Test multiple types and implied type handling.""" |
| 106 | # The TEST_VM type should force it to build the test image. |
George Engelbrecht | c55d631 | 2021-05-05 12:11:13 -0600 | [diff] [blame] | 107 | types = [common_pb2.IMAGE_TYPE_BASE, common_pb2.IMAGE_TYPE_TEST_VM] |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 108 | expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST] |
| 109 | |
| 110 | request = self._GetRequest(board='board', types=types) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 111 | |
| 112 | # Failed result to avoid the success handling logic. |
| 113 | result = image_service.BuildResult(1, []) |
| 114 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
| 115 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 116 | image_controller.Create(request, self.response, self.api_config) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 117 | build_patch.assert_called_with( |
| 118 | images=expected_images, board='board', config=mock.ANY) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 119 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 120 | def testRecoveryImpliedTypes(self): |
| 121 | """Test implied type handling of recovery images.""" |
| 122 | # The TEST_VM type should force it to build the test image. |
| 123 | types = [common_pb2.IMAGE_TYPE_RECOVERY] |
| 124 | |
| 125 | request = self._GetRequest(board='board', types=types) |
| 126 | |
| 127 | # Failed result to avoid the success handling logic. |
| 128 | result = image_service.BuildResult(1, []) |
| 129 | build_patch = self.PatchObject(image_service, 'Build', return_value=result) |
| 130 | |
| 131 | image_controller.Create(request, self.response, self.api_config) |
| 132 | build_patch.assert_called_with( |
| 133 | images=[constants.IMAGE_TYPE_BASE], board='board', config=mock.ANY) |
| 134 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 135 | def testFailedPackageHandling(self): |
| 136 | """Test failed packages are populated correctly.""" |
| 137 | result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg']) |
| 138 | expected_packages = [('foo', 'bar'), ('cat', 'pkg')] |
| 139 | self.PatchObject(image_service, 'Build', return_value=result) |
| 140 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 141 | input_proto = self._GetRequest(board='board') |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 142 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 143 | rc = image_controller.Create(input_proto, self.response, self.api_config) |
| 144 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 145 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 146 | for package in self.response.failed_packages: |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 147 | self.assertIn((package.category, package.package_name), expected_packages) |
| 148 | |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 149 | def testNoPackagesFailureHandling(self): |
| 150 | """Test failed packages are populated correctly.""" |
| 151 | result = image_service.BuildResult(1, []) |
| 152 | self.PatchObject(image_service, 'Build', return_value=result) |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 153 | |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 154 | input_proto = image_pb2.CreateImageRequest() |
| 155 | input_proto.build_target.name = 'board' |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 156 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 157 | rc = image_controller.Create(input_proto, self.response, self.api_config) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 158 | self.assertTrue(rc) |
| 159 | self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, |
| 160 | rc) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 161 | self.assertFalse(self.response.failed_packages) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 162 | |
| 163 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 164 | class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase, |
| 165 | api_config.ApiConfigMixin): |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 166 | """Image signer test tests.""" |
| 167 | |
| 168 | def setUp(self): |
| 169 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 170 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 171 | |
| 172 | osutils.SafeMakedirs(self.result_directory) |
| 173 | osutils.Touch(self.image_path) |
| 174 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 175 | def testValidateOnly(self): |
| 176 | """Sanity check that validate-only calls don't execute any logic.""" |
| 177 | patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True) |
| 178 | input_proto = image_pb2.TestImageRequest() |
| 179 | input_proto.image.path = self.image_path |
| 180 | output_proto = image_pb2.TestImageResult() |
| 181 | |
| 182 | image_controller.SignerTest(input_proto, output_proto, |
| 183 | self.validate_only_config) |
| 184 | |
| 185 | patch.assert_not_called() |
| 186 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 187 | def testMockCall(self): |
| 188 | """Test that mock call does not execute any logic, returns mocked value.""" |
| 189 | patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True) |
| 190 | input_proto = image_pb2.TestImageRequest() |
| 191 | input_proto.image.path = self.image_path |
| 192 | output_proto = image_pb2.TestImageResult() |
| 193 | |
| 194 | image_controller.SignerTest(input_proto, output_proto, |
| 195 | self.mock_call_config) |
| 196 | |
| 197 | patch.assert_not_called() |
| 198 | self.assertEqual(output_proto.success, True) |
| 199 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 200 | def testMockError(self): |
| 201 | """Test that mock call does not execute any logic, returns error.""" |
| 202 | patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True) |
| 203 | input_proto = image_pb2.TestImageRequest() |
| 204 | input_proto.image.path = self.image_path |
| 205 | output_proto = image_pb2.TestImageResult() |
| 206 | |
| 207 | rc = image_controller.SignerTest(input_proto, output_proto, |
| 208 | self.mock_error_config) |
| 209 | |
| 210 | patch.assert_not_called() |
| 211 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 212 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 213 | def testSignerTestNoImage(self): |
| 214 | """Test function argument validation.""" |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 215 | input_proto = image_pb2.TestImageRequest() |
| 216 | output_proto = image_pb2.TestImageResult() |
| 217 | |
| 218 | # Nothing provided. |
| 219 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 220 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 221 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 222 | def testSignerTestSuccess(self): |
| 223 | """Test successful call handling.""" |
| 224 | self.PatchObject(image_lib, 'SecurityTest', return_value=True) |
| 225 | input_proto = image_pb2.TestImageRequest() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 226 | input_proto.image.path = self.image_path |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 227 | output_proto = image_pb2.TestImageResult() |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 228 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 229 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
| 230 | |
| 231 | def testSignerTestFailure(self): |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 232 | """Test function output tests.""" |
| 233 | input_proto = image_pb2.TestImageRequest() |
| 234 | input_proto.image.path = self.image_path |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 235 | output_proto = image_pb2.TestImageResult() |
| 236 | |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 237 | self.PatchObject(image_lib, 'SecurityTest', return_value=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 238 | image_controller.SignerTest(input_proto, output_proto, self.api_config) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 239 | self.assertFalse(output_proto.success) |
| 240 | |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 241 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 242 | class ImageTestTest(cros_test_lib.MockTempDirTestCase, |
| 243 | api_config.ApiConfigMixin): |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 244 | """Image test tests.""" |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 245 | |
| 246 | def setUp(self): |
| 247 | self.image_path = os.path.join(self.tempdir, 'image.bin') |
| 248 | self.board = 'board' |
| 249 | self.result_directory = os.path.join(self.tempdir, 'results') |
| 250 | |
| 251 | osutils.SafeMakedirs(self.result_directory) |
| 252 | osutils.Touch(self.image_path) |
| 253 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 254 | def testValidateOnly(self): |
| 255 | """Sanity check that a validate only call does not execute any logic.""" |
| 256 | patch = self.PatchObject(image_service, 'Test') |
| 257 | |
| 258 | input_proto = image_pb2.TestImageRequest() |
| 259 | input_proto.image.path = self.image_path |
| 260 | input_proto.build_target.name = self.board |
| 261 | input_proto.result.directory = self.result_directory |
| 262 | output_proto = image_pb2.TestImageResult() |
| 263 | |
| 264 | image_controller.Test(input_proto, output_proto, self.validate_only_config) |
| 265 | patch.assert_not_called() |
| 266 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 267 | def testMockCall(self): |
| 268 | """Test that mock call does not execute any logic, returns mocked value.""" |
| 269 | patch = self.PatchObject(image_service, 'Test') |
| 270 | |
| 271 | input_proto = image_pb2.TestImageRequest() |
| 272 | input_proto.image.path = self.image_path |
| 273 | input_proto.build_target.name = self.board |
| 274 | input_proto.result.directory = self.result_directory |
| 275 | output_proto = image_pb2.TestImageResult() |
| 276 | |
| 277 | image_controller.Test(input_proto, output_proto, self.mock_call_config) |
| 278 | patch.assert_not_called() |
| 279 | self.assertEqual(output_proto.success, True) |
| 280 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 281 | def testMockError(self): |
| 282 | """Test that mock call does not execute any logic, returns error.""" |
| 283 | patch = self.PatchObject(image_service, 'Test') |
| 284 | |
| 285 | input_proto = image_pb2.TestImageRequest() |
| 286 | input_proto.image.path = self.image_path |
| 287 | input_proto.build_target.name = self.board |
| 288 | input_proto.result.directory = self.result_directory |
| 289 | output_proto = image_pb2.TestImageResult() |
| 290 | |
| 291 | rc = image_controller.Test(input_proto, output_proto, |
| 292 | self.mock_error_config) |
| 293 | patch.assert_not_called() |
| 294 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 295 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 296 | def testTestArgumentValidation(self): |
| 297 | """Test function argument validation tests.""" |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 298 | self.PatchObject(image_service, 'Test', return_value=True) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 299 | input_proto = image_pb2.TestImageRequest() |
| 300 | output_proto = image_pb2.TestImageResult() |
| 301 | |
| 302 | # Nothing provided. |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 303 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 304 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 305 | |
| 306 | # Just one argument. |
| 307 | input_proto.build_target.name = self.board |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 308 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 309 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 310 | |
| 311 | # Two arguments provided. |
| 312 | input_proto.result.directory = self.result_directory |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 313 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 314 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 315 | |
| 316 | # Invalid image path. |
| 317 | input_proto.image.path = '/invalid/image/path' |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 318 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 319 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 320 | |
| 321 | # All valid arguments. |
| 322 | input_proto.image.path = self.image_path |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 323 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 324 | |
| 325 | def testTestOutputHandling(self): |
| 326 | """Test function output tests.""" |
| 327 | input_proto = image_pb2.TestImageRequest() |
| 328 | input_proto.image.path = self.image_path |
| 329 | input_proto.build_target.name = self.board |
| 330 | input_proto.result.directory = self.result_directory |
| 331 | output_proto = image_pb2.TestImageResult() |
| 332 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 333 | self.PatchObject(image_service, 'Test', return_value=True) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 334 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 335 | self.assertTrue(output_proto.success) |
| 336 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 337 | self.PatchObject(image_service, 'Test', return_value=False) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 338 | image_controller.Test(input_proto, output_proto, self.api_config) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 339 | self.assertFalse(output_proto.success) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | class PushImageTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
| 343 | """Push image test.""" |
| 344 | |
| 345 | def setUp(self): |
| 346 | self.response = image_pb2.PushImageResponse() |
| 347 | |
| 348 | def _GetRequest( |
| 349 | self, |
| 350 | gs_image_dir='gs://chromeos-image-archive/atlas-release/R89-13604.0.0', |
| 351 | build_target_name='atlas', |
| 352 | profile='foo', |
| 353 | sign_types=None, |
| 354 | dryrun=True): |
| 355 | return image_pb2.PushImageRequest( |
| 356 | gs_image_dir=gs_image_dir, |
| 357 | sysroot=sysroot_pb2.Sysroot( |
| 358 | build_target=common_pb2.BuildTarget(name=build_target_name)), |
| 359 | profile=common_pb2.Profile(name=profile), |
| 360 | sign_types=sign_types, |
| 361 | dryrun=dryrun) |
| 362 | |
| 363 | def testValidateOnly(self): |
| 364 | """Check that a validate only call does not execute any logic.""" |
| 365 | patch = self.PatchObject(pushimage, 'PushImage') |
| 366 | |
| 367 | req = self._GetRequest(sign_types=[ |
| 368 | common_pb2.IMAGE_TYPE_RECOVERY, common_pb2.IMAGE_TYPE_FACTORY, |
| 369 | common_pb2.IMAGE_TYPE_FIRMWARE, common_pb2.IMAGE_TYPE_ACCESSORY_USBPD, |
| 370 | common_pb2.IMAGE_TYPE_ACCESSORY_RWSIG, common_pb2.IMAGE_TYPE_BASE, |
| 371 | common_pb2.IMAGE_TYPE_GSC_FIRMWARE |
| 372 | ]) |
| 373 | res = image_controller.PushImage(req, self.response, |
| 374 | self.validate_only_config) |
| 375 | patch.assert_not_called() |
| 376 | self.assertEqual(res, controller.RETURN_CODE_VALID_INPUT) |
| 377 | |
| 378 | def testValidateOnlyInvalid(self): |
| 379 | """Check that validate call rejects invalid sign types.""" |
| 380 | patch = self.PatchObject(pushimage, 'PushImage') |
| 381 | |
| 382 | # Pass unsupported image type. |
| 383 | req = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_DLC]) |
| 384 | res = image_controller.PushImage(req, self.response, |
| 385 | self.validate_only_config) |
| 386 | patch.assert_not_called() |
| 387 | self.assertEqual(res, controller.RETURN_CODE_INVALID_INPUT) |
| 388 | |
| 389 | def testMockCall(self): |
| 390 | """Test that mock call does not execute any logic, returns mocked value.""" |
| 391 | patch = self.PatchObject(pushimage, 'PushImage') |
| 392 | |
| 393 | rc = image_controller.PushImage(self._GetRequest(), self.response, |
| 394 | self.mock_call_config) |
| 395 | patch.assert_not_called() |
| 396 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 397 | |
| 398 | def testMockError(self): |
| 399 | """Test that mock call does not execute any logic, returns error.""" |
| 400 | patch = self.PatchObject(pushimage, 'PushImage') |
| 401 | |
| 402 | rc = image_controller.PushImage(self._GetRequest(), self.response, |
| 403 | self.mock_error_config) |
| 404 | patch.assert_not_called() |
| 405 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 406 | |
| 407 | def testNoBuildTarget(self): |
| 408 | """Test no build target given fails.""" |
| 409 | request = self._GetRequest(build_target_name='') |
| 410 | |
| 411 | # No build target should cause it to fail. |
| 412 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 413 | image_controller.PushImage(request, self.response, self.api_config) |
| 414 | |
| 415 | def testNoGsImageDir(self): |
| 416 | """Test no image dir given fails.""" |
| 417 | request = self._GetRequest(gs_image_dir='') |
| 418 | |
| 419 | # No image dir should cause it to fail. |
| 420 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 421 | image_controller.PushImage(request, self.response, self.api_config) |
| 422 | |
| 423 | def testCallCorrect(self): |
| 424 | """Check that a call is called with the correct parameters.""" |
| 425 | patch = self.PatchObject(pushimage, 'PushImage') |
| 426 | |
| 427 | request = self._GetRequest( |
| 428 | dryrun=False, profile='', sign_types=[common_pb2.IMAGE_TYPE_RECOVERY]) |
Jack Neus | 485a9d2 | 2020-12-21 03:15:15 +0000 | [diff] [blame] | 429 | request.dest_bucket = 'gs://foo' |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 430 | image_controller.PushImage(request, self.response, self.api_config) |
| 431 | patch.assert_called_with( |
| 432 | request.gs_image_dir, |
| 433 | request.sysroot.build_target.name, |
| 434 | dry_run=request.dryrun, |
Jack Neus | 485a9d2 | 2020-12-21 03:15:15 +0000 | [diff] [blame] | 435 | sign_types=['recovery'], |
| 436 | dest_bucket=request.dest_bucket) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 437 | |
| 438 | def testCallSucceeds(self): |
| 439 | """Check that a (dry run) call is made successfully.""" |
| 440 | request = self._GetRequest(sign_types=[common_pb2.IMAGE_TYPE_RECOVERY]) |
| 441 | res = image_controller.PushImage(request, self.response, self.api_config) |
| 442 | self.assertEqual(res, controller.RETURN_CODE_SUCCESS) |
| 443 | |
| 444 | def testCallFailsWithBadImageDir(self): |
| 445 | """Check that a (dry run) call fails when given a bad gs_image_dir.""" |
| 446 | request = self._GetRequest(gs_image_dir='foo') |
| 447 | res = image_controller.PushImage(request, self.response, self.api_config) |
| 448 | self.assertEqual(res, controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY) |