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