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