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