blob: 97b01d8dee1c775d0ca78abb757ffa48a70ae1d1 [file] [log] [blame]
Alex Klein2966e302019-01-17 13:29:38 -07001# 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 Klein2966e302019-01-17 13:29:38 -07007import os
Mike Frysinger166fea02021-02-12 05:30:33 -05008from unittest import mock
Alex Klein2966e302019-01-17 13:29:38 -07009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060011from chromite.api import controller
12from chromite.api.controller import image as image_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060013from chromite.api.gen.chromite.api import image_pb2
David Burger13e06be2019-05-13 20:33:16 -060014from chromite.api.gen.chromiumos import common_pb2
Jack Neus761e1842020-12-01 18:20:11 +000015from chromite.api.gen.chromite.api import sysroot_pb2
Alex Klein56355682019-02-07 10:36:54 -070016from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060017from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070018from chromite.lib import cros_test_lib
Michael Mortensenc83c9952019-08-05 12:15:12 -060019from chromite.lib import image_lib
Alex Klein2966e302019-01-17 13:29:38 -070020from chromite.lib import osutils
Jack Neus761e1842020-12-01 18:20:11 +000021from chromite.scripts import pushimage
Alex Kleinb7cdbe62019-02-22 11:41:32 -070022from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070023
24
Alex Klein231d2da2019-07-22 16:44:45 -060025class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin):
Alex Klein56355682019-02-07 10:36:54 -070026 """Create image tests."""
27
Alex Klein231d2da2019-07-22 16:44:45 -060028 def setUp(self):
29 self.response = image_pb2.CreateImageResult()
30
Jack Neus761e1842020-12-01 18:20:11 +000031 def _GetRequest(self,
32 board=None,
33 types=None,
34 version=None,
35 builder_path=None,
Alex Klein21b95022019-05-09 14:14:46 -060036 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 Klein231d2da2019-07-22 16:44:45 -060046 def testValidateOnly(self):
47 """Sanity check that a validate only call does not execute any logic."""
48 patch = self.PatchObject(image_service, 'Build')
Alex Klein21b95022019-05-09 14:14:46 -060049
Alex Klein231d2da2019-07-22 16:44:45 -060050 request = self._GetRequest(board='board')
51 image_controller.Create(request, self.response, self.validate_only_config)
52 patch.assert_not_called()
53
Michael Mortensen10146cf2019-11-19 19:59:22 -070054 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 Mortensen85d38402019-12-12 09:50:29 -070063 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 Klein231d2da2019-07-22 16:44:45 -060072 def testNoBoard(self):
73 """Test no board given fails."""
74 request = self._GetRequest()
Alex Klein56355682019-02-07 10:36:54 -070075
76 # No board should cause it to fail.
Alex Klein4f0eb432019-05-02 13:56:04 -060077 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060078 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070079
Alex Klein21b95022019-05-09 14:14:46 -060080 def testNoTypeSpecified(self):
81 """Test the image type default."""
82 request = self._GetRequest(board='board')
Alex Klein21b95022019-05-09 14:14:46 -060083
Alex Klein1bcd9882019-03-19 13:25:24 -060084 # Failed result to avoid the success handling logic.
Alex Klein27978a42021-07-27 14:18:10 -060085 result = image_service.BuildResult([constants.IMAGE_TYPE_BASE])
86 result.return_code = 1
Alex Klein1bcd9882019-03-19 13:25:24 -060087 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
Alex Klein56355682019-02-07 10:36:54 -070088
Alex Klein231d2da2019-07-22 16:44:45 -060089 image_controller.Create(request, self.response, self.api_config)
Alex Klein27978a42021-07-27 14:18:10 -060090 build_patch.assert_any_call(
91 'board', [constants.IMAGE_TYPE_BASE], config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070092
Alex Klein21b95022019-05-09 14:14:46 -060093 def testSingleTypeSpecified(self):
94 """Test it's properly using a specified type."""
George Engelbrechtc55d6312021-05-05 12:11:13 -060095 request = self._GetRequest(board='board', types=[common_pb2.IMAGE_TYPE_DEV])
Alex Klein21b95022019-05-09 14:14:46 -060096
97 # Failed result to avoid the success handling logic.
Alex Klein27978a42021-07-27 14:18:10 -060098 result = image_service.BuildResult([constants.IMAGE_TYPE_DEV])
99 result.return_code = 1
Alex Klein21b95022019-05-09 14:14:46 -0600100 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
101
Alex Klein231d2da2019-07-22 16:44:45 -0600102 image_controller.Create(request, self.response, self.api_config)
Alex Klein27978a42021-07-27 14:18:10 -0600103 build_patch.assert_any_call(
104 'board', [constants.IMAGE_TYPE_DEV], config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -0700105
Alex Klein21b95022019-05-09 14:14:46 -0600106 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 Engelbrechtc55d6312021-05-05 12:11:13 -0600109 types = [common_pb2.IMAGE_TYPE_BASE, common_pb2.IMAGE_TYPE_TEST_VM]
Alex Klein21b95022019-05-09 14:14:46 -0600110 expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST]
111
112 request = self._GetRequest(board='board', types=types)
Alex Klein21b95022019-05-09 14:14:46 -0600113
114 # Failed result to avoid the success handling logic.
Alex Klein27978a42021-07-27 14:18:10 -0600115 result = image_service.BuildResult(expected_images)
116 result.return_code = 1
Alex Klein21b95022019-05-09 14:14:46 -0600117 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
118
Alex Klein231d2da2019-07-22 16:44:45 -0600119 image_controller.Create(request, self.response, self.api_config)
Alex Klein27978a42021-07-27 14:18:10 -0600120 build_patch.assert_any_call('board', expected_images, config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -0700121
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700122 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 Klein27978a42021-07-27 14:18:10 -0600130 result = image_service.BuildResult([])
131 result.return_code = 1
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700132 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
133
134 image_controller.Create(request, self.response, self.api_config)
Alex Klein27978a42021-07-27 14:18:10 -0600135 build_patch.assert_any_call(
136 'board', [constants.IMAGE_TYPE_BASE], config=mock.ANY)
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700137
Alex Klein1bcd9882019-03-19 13:25:24 -0600138 def testFailedPackageHandling(self):
139 """Test failed packages are populated correctly."""
Alex Klein27978a42021-07-27 14:18:10 -0600140 result = image_service.BuildResult([])
141 result.return_code = 1
142 result.failed_packages = ['foo/bar', 'cat/pkg']
Alex Klein1bcd9882019-03-19 13:25:24 -0600143 expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
144 self.PatchObject(image_service, 'Build', return_value=result)
145
Alex Klein231d2da2019-07-22 16:44:45 -0600146 input_proto = self._GetRequest(board='board')
Alex Klein1bcd9882019-03-19 13:25:24 -0600147
Alex Klein231d2da2019-07-22 16:44:45 -0600148 rc = image_controller.Create(input_proto, self.response, self.api_config)
149
Alex Klein8cb365a2019-05-15 16:24:53 -0600150 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600151 for package in self.response.failed_packages:
Alex Klein1bcd9882019-03-19 13:25:24 -0600152 self.assertIn((package.category, package.package_name), expected_packages)
153
Alex Klein2557b4f2019-07-11 14:34:00 -0600154 def testNoPackagesFailureHandling(self):
155 """Test failed packages are populated correctly."""
Alex Klein27978a42021-07-27 14:18:10 -0600156 result = image_service.BuildResult([])
157 result.return_code = 1
Alex Klein2557b4f2019-07-11 14:34:00 -0600158 self.PatchObject(image_service, 'Build', return_value=result)
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700159
Alex Klein2557b4f2019-07-11 14:34:00 -0600160 input_proto = image_pb2.CreateImageRequest()
161 input_proto.build_target.name = 'board'
Alex Klein2557b4f2019-07-11 14:34:00 -0600162
Alex Klein231d2da2019-07-22 16:44:45 -0600163 rc = image_controller.Create(input_proto, self.response, self.api_config)
Alex Klein2557b4f2019-07-11 14:34:00 -0600164 self.assertTrue(rc)
165 self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
166 rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600167 self.assertFalse(self.response.failed_packages)
Alex Klein2557b4f2019-07-11 14:34:00 -0600168
169
Alex Klein231d2da2019-07-22 16:44:45 -0600170class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase,
171 api_config.ApiConfigMixin):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600172 """Image signer test tests."""
173
174 def setUp(self):
175 self.image_path = os.path.join(self.tempdir, 'image.bin')
Michael Mortensenc83c9952019-08-05 12:15:12 -0600176 self.result_directory = os.path.join(self.tempdir, 'results')
177
178 osutils.SafeMakedirs(self.result_directory)
179 osutils.Touch(self.image_path)
180
Alex Klein231d2da2019-07-22 16:44:45 -0600181 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 Mortensen10146cf2019-11-19 19:59:22 -0700193 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 Mortensen85d38402019-12-12 09:50:29 -0700206 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 Klein231d2da2019-07-22 16:44:45 -0600219 def testSignerTestNoImage(self):
220 """Test function argument validation."""
Michael Mortensenc83c9952019-08-05 12:15:12 -0600221 input_proto = image_pb2.TestImageRequest()
222 output_proto = image_pb2.TestImageResult()
223
224 # Nothing provided.
225 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600226 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600227
Alex Klein231d2da2019-07-22 16:44:45 -0600228 def testSignerTestSuccess(self):
229 """Test successful call handling."""
230 self.PatchObject(image_lib, 'SecurityTest', return_value=True)
231 input_proto = image_pb2.TestImageRequest()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600232 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600233 output_proto = image_pb2.TestImageResult()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600234
Alex Klein231d2da2019-07-22 16:44:45 -0600235 image_controller.SignerTest(input_proto, output_proto, self.api_config)
236
237 def testSignerTestFailure(self):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600238 """Test function output tests."""
239 input_proto = image_pb2.TestImageRequest()
240 input_proto.image.path = self.image_path
Michael Mortensenc83c9952019-08-05 12:15:12 -0600241 output_proto = image_pb2.TestImageResult()
242
Michael Mortensenc83c9952019-08-05 12:15:12 -0600243 self.PatchObject(image_lib, 'SecurityTest', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600244 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600245 self.assertFalse(output_proto.success)
246
Michael Mortensenc83c9952019-08-05 12:15:12 -0600247
Alex Klein231d2da2019-07-22 16:44:45 -0600248class ImageTestTest(cros_test_lib.MockTempDirTestCase,
249 api_config.ApiConfigMixin):
Alex Klein2557b4f2019-07-11 14:34:00 -0600250 """Image test tests."""
Alex Klein2966e302019-01-17 13:29:38 -0700251
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 Klein231d2da2019-07-22 16:44:45 -0600260 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 Mortensen10146cf2019-11-19 19:59:22 -0700273 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 Mortensen85d38402019-12-12 09:50:29 -0700287 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 Klein2966e302019-01-17 13:29:38 -0700302 def testTestArgumentValidation(self):
303 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700304 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700305 input_proto = image_pb2.TestImageRequest()
306 output_proto = image_pb2.TestImageResult()
307
308 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600309 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600310 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700311
312 # Just one argument.
313 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600314 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600315 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700316
317 # Two arguments provided.
318 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600319 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600320 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700321
322 # Invalid image path.
323 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600324 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600325 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700326
327 # All valid arguments.
328 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600329 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700330
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 Kleinb7cdbe62019-02-22 11:41:32 -0700339 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600340 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700341 self.assertTrue(output_proto.success)
342
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700343 self.PatchObject(image_service, 'Test', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600344 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700345 self.assertFalse(output_proto.success)
Jack Neus761e1842020-12-01 18:20:11 +0000346
347
348class 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 Neuse77614d2021-10-11 14:10:27 +0000360 dryrun=True,
361 channels=None):
Jack Neus761e1842020-12-01 18:20:11 +0000362 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 Neuse77614d2021-10-11 14:10:27 +0000368 dryrun=dryrun,
369 channels=channels)
Jack Neus761e1842020-12-01 18:20:11 +0000370
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 Neuse77614d2021-10-11 14:10:27 +0000436 dryrun=False, profile='', sign_types=[common_pb2.IMAGE_TYPE_RECOVERY],
437 channels=[common_pb2.CHANNEL_DEV, common_pb2.CHANNEL_CANARY])
Jack Neus485a9d22020-12-21 03:15:15 +0000438 request.dest_bucket = 'gs://foo'
Jack Neus761e1842020-12-01 18:20:11 +0000439 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 Neus485a9d22020-12-21 03:15:15 +0000444 sign_types=['recovery'],
Jack Neuse77614d2021-10-11 14:10:27 +0000445 dest_bucket=request.dest_bucket,
446 force_channels=['dev', 'canary'])
Jack Neus761e1842020-12-01 18:20:11 +0000447
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)