blob: 6cce6c531c2144a465331404e11ffd4cc91d7904 [file] [log] [blame]
Alex Klein2966e302019-01-17 13:29:38 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
Mike Frysinger6db648e2018-07-24 19:57:58 -040012import mock
13
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060015from chromite.api import controller
16from chromite.api.controller import image as image_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060017from chromite.api.gen.chromite.api import image_pb2
David Burger13e06be2019-05-13 20:33:16 -060018from chromite.api.gen.chromiumos import common_pb2
Alex Klein56355682019-02-07 10:36:54 -070019from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060020from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070021from chromite.lib import cros_test_lib
Michael Mortensenc83c9952019-08-05 12:15:12 -060022from chromite.lib import image_lib
Alex Klein2966e302019-01-17 13:29:38 -070023from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070024from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070025
26
Alex Klein231d2da2019-07-22 16:44:45 -060027class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin):
Alex Klein56355682019-02-07 10:36:54 -070028 """Create image tests."""
29
Alex Klein231d2da2019-07-22 16:44:45 -060030 def setUp(self):
31 self.response = image_pb2.CreateImageResult()
32
Alex Klein21b95022019-05-09 14:14:46 -060033 def _GetRequest(self, board=None, types=None, version=None, builder_path=None,
34 disable_rootfs_verification=False):
35 """Helper to build a request instance."""
36 return image_pb2.CreateImageRequest(
37 build_target={'name': board},
38 image_types=types,
39 disable_rootfs_verification=disable_rootfs_verification,
40 version=version,
41 builder_path=builder_path,
42 )
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 def testValidateOnly(self):
45 """Sanity check that a validate only call does not execute any logic."""
46 patch = self.PatchObject(image_service, 'Build')
Alex Klein21b95022019-05-09 14:14:46 -060047
Alex Klein231d2da2019-07-22 16:44:45 -060048 request = self._GetRequest(board='board')
49 image_controller.Create(request, self.response, self.validate_only_config)
50 patch.assert_not_called()
51
Michael Mortensen10146cf2019-11-19 19:59:22 -070052 def testMockCall(self):
53 """Test that mock call does not execute any logic, returns mocked value."""
54 patch = self.PatchObject(image_service, 'Build')
55
56 request = self._GetRequest(board='board')
57 image_controller.Create(request, self.response, self.mock_call_config)
58 patch.assert_not_called()
59 self.assertEqual(self.response.success, True)
60
Alex Klein231d2da2019-07-22 16:44:45 -060061 def testNoBoard(self):
62 """Test no board given fails."""
63 request = self._GetRequest()
Alex Klein56355682019-02-07 10:36:54 -070064
65 # No board should cause it to fail.
Alex Klein4f0eb432019-05-02 13:56:04 -060066 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060067 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070068
Alex Klein21b95022019-05-09 14:14:46 -060069 def testNoTypeSpecified(self):
70 """Test the image type default."""
71 request = self._GetRequest(board='board')
Alex Klein21b95022019-05-09 14:14:46 -060072
Alex Klein1bcd9882019-03-19 13:25:24 -060073 # Failed result to avoid the success handling logic.
74 result = image_service.BuildResult(1, [])
75 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
Alex Klein56355682019-02-07 10:36:54 -070076
Alex Klein231d2da2019-07-22 16:44:45 -060077 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070078 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE],
Alex Klein21b95022019-05-09 14:14:46 -060079 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070080
Alex Klein21b95022019-05-09 14:14:46 -060081 def testSingleTypeSpecified(self):
82 """Test it's properly using a specified type."""
83 request = self._GetRequest(board='board', types=[common_pb2.DEV])
Alex Klein21b95022019-05-09 14:14:46 -060084
85 # Failed result to avoid the success handling logic.
86 result = image_service.BuildResult(1, [])
87 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
88
Alex Klein231d2da2019-07-22 16:44:45 -060089 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070090 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
Alex Klein21b95022019-05-09 14:14:46 -060091 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070092
Alex Klein21b95022019-05-09 14:14:46 -060093 def testMultipleAndImpliedTypes(self):
94 """Test multiple types and implied type handling."""
95 # The TEST_VM type should force it to build the test image.
96 types = [common_pb2.BASE, common_pb2.TEST_VM]
97 expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST]
98
99 request = self._GetRequest(board='board', types=types)
Alex Klein21b95022019-05-09 14:14:46 -0600100
101 # Failed result to avoid the success handling logic.
102 result = image_service.BuildResult(1, [])
103 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
104
Alex Klein231d2da2019-07-22 16:44:45 -0600105 image_controller.Create(request, self.response, self.api_config)
Alex Klein21b95022019-05-09 14:14:46 -0600106 build_patch.assert_called_with(images=expected_images, board='board',
Alex Klein56355682019-02-07 10:36:54 -0700107 config=mock.ANY)
108
Alex Klein1bcd9882019-03-19 13:25:24 -0600109 def testFailedPackageHandling(self):
110 """Test failed packages are populated correctly."""
111 result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg'])
112 expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
113 self.PatchObject(image_service, 'Build', return_value=result)
114
Alex Klein231d2da2019-07-22 16:44:45 -0600115 input_proto = self._GetRequest(board='board')
Alex Klein1bcd9882019-03-19 13:25:24 -0600116
Alex Klein231d2da2019-07-22 16:44:45 -0600117 rc = image_controller.Create(input_proto, self.response, self.api_config)
118
Alex Klein8cb365a2019-05-15 16:24:53 -0600119 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600120 for package in self.response.failed_packages:
Alex Klein1bcd9882019-03-19 13:25:24 -0600121 self.assertIn((package.category, package.package_name), expected_packages)
122
Alex Klein2557b4f2019-07-11 14:34:00 -0600123 def testNoPackagesFailureHandling(self):
124 """Test failed packages are populated correctly."""
125 result = image_service.BuildResult(1, [])
126 self.PatchObject(image_service, 'Build', return_value=result)
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700127
Alex Klein2557b4f2019-07-11 14:34:00 -0600128 input_proto = image_pb2.CreateImageRequest()
129 input_proto.build_target.name = 'board'
Alex Klein2557b4f2019-07-11 14:34:00 -0600130
Alex Klein231d2da2019-07-22 16:44:45 -0600131 rc = image_controller.Create(input_proto, self.response, self.api_config)
Alex Klein2557b4f2019-07-11 14:34:00 -0600132 self.assertTrue(rc)
133 self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
134 rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600135 self.assertFalse(self.response.failed_packages)
Alex Klein2557b4f2019-07-11 14:34:00 -0600136
137
Alex Klein231d2da2019-07-22 16:44:45 -0600138class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase,
139 api_config.ApiConfigMixin):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600140 """Image signer test tests."""
141
142 def setUp(self):
143 self.image_path = os.path.join(self.tempdir, 'image.bin')
Michael Mortensenc83c9952019-08-05 12:15:12 -0600144 self.result_directory = os.path.join(self.tempdir, 'results')
145
146 osutils.SafeMakedirs(self.result_directory)
147 osutils.Touch(self.image_path)
148
Alex Klein231d2da2019-07-22 16:44:45 -0600149 def testValidateOnly(self):
150 """Sanity check that validate-only calls don't execute any logic."""
151 patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True)
152 input_proto = image_pb2.TestImageRequest()
153 input_proto.image.path = self.image_path
154 output_proto = image_pb2.TestImageResult()
155
156 image_controller.SignerTest(input_proto, output_proto,
157 self.validate_only_config)
158
159 patch.assert_not_called()
160
Michael Mortensen10146cf2019-11-19 19:59:22 -0700161 def testMockCall(self):
162 """Test that mock call does not execute any logic, returns mocked value."""
163 patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True)
164 input_proto = image_pb2.TestImageRequest()
165 input_proto.image.path = self.image_path
166 output_proto = image_pb2.TestImageResult()
167
168 image_controller.SignerTest(input_proto, output_proto,
169 self.mock_call_config)
170
171 patch.assert_not_called()
172 self.assertEqual(output_proto.success, True)
173
Alex Klein231d2da2019-07-22 16:44:45 -0600174 def testSignerTestNoImage(self):
175 """Test function argument validation."""
Michael Mortensenc83c9952019-08-05 12:15:12 -0600176 input_proto = image_pb2.TestImageRequest()
177 output_proto = image_pb2.TestImageResult()
178
179 # Nothing provided.
180 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600181 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600182
Alex Klein231d2da2019-07-22 16:44:45 -0600183 def testSignerTestSuccess(self):
184 """Test successful call handling."""
185 self.PatchObject(image_lib, 'SecurityTest', return_value=True)
186 input_proto = image_pb2.TestImageRequest()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600187 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600188 output_proto = image_pb2.TestImageResult()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600189
Alex Klein231d2da2019-07-22 16:44:45 -0600190 image_controller.SignerTest(input_proto, output_proto, self.api_config)
191
192 def testSignerTestFailure(self):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600193 """Test function output tests."""
194 input_proto = image_pb2.TestImageRequest()
195 input_proto.image.path = self.image_path
Michael Mortensenc83c9952019-08-05 12:15:12 -0600196 output_proto = image_pb2.TestImageResult()
197
Michael Mortensenc83c9952019-08-05 12:15:12 -0600198 self.PatchObject(image_lib, 'SecurityTest', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600199 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600200 self.assertFalse(output_proto.success)
201
Michael Mortensenc83c9952019-08-05 12:15:12 -0600202
Alex Klein231d2da2019-07-22 16:44:45 -0600203class ImageTestTest(cros_test_lib.MockTempDirTestCase,
204 api_config.ApiConfigMixin):
Alex Klein2557b4f2019-07-11 14:34:00 -0600205 """Image test tests."""
Alex Klein2966e302019-01-17 13:29:38 -0700206
207 def setUp(self):
208 self.image_path = os.path.join(self.tempdir, 'image.bin')
209 self.board = 'board'
210 self.result_directory = os.path.join(self.tempdir, 'results')
211
212 osutils.SafeMakedirs(self.result_directory)
213 osutils.Touch(self.image_path)
214
Alex Klein231d2da2019-07-22 16:44:45 -0600215 def testValidateOnly(self):
216 """Sanity check that a validate only call does not execute any logic."""
217 patch = self.PatchObject(image_service, 'Test')
218
219 input_proto = image_pb2.TestImageRequest()
220 input_proto.image.path = self.image_path
221 input_proto.build_target.name = self.board
222 input_proto.result.directory = self.result_directory
223 output_proto = image_pb2.TestImageResult()
224
225 image_controller.Test(input_proto, output_proto, self.validate_only_config)
226 patch.assert_not_called()
227
Michael Mortensen10146cf2019-11-19 19:59:22 -0700228 def testMockCall(self):
229 """Test that mock call does not execute any logic, returns mocked value."""
230 patch = self.PatchObject(image_service, 'Test')
231
232 input_proto = image_pb2.TestImageRequest()
233 input_proto.image.path = self.image_path
234 input_proto.build_target.name = self.board
235 input_proto.result.directory = self.result_directory
236 output_proto = image_pb2.TestImageResult()
237
238 image_controller.Test(input_proto, output_proto, self.mock_call_config)
239 patch.assert_not_called()
240 self.assertEqual(output_proto.success, True)
241
Alex Klein2966e302019-01-17 13:29:38 -0700242 def testTestArgumentValidation(self):
243 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700244 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700245 input_proto = image_pb2.TestImageRequest()
246 output_proto = image_pb2.TestImageResult()
247
248 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600249 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600250 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700251
252 # Just one argument.
253 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600254 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600255 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700256
257 # Two arguments provided.
258 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600259 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600260 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700261
262 # Invalid image path.
263 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600264 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600265 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700266
267 # All valid arguments.
268 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600269 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700270
271 def testTestOutputHandling(self):
272 """Test function output tests."""
273 input_proto = image_pb2.TestImageRequest()
274 input_proto.image.path = self.image_path
275 input_proto.build_target.name = self.board
276 input_proto.result.directory = self.result_directory
277 output_proto = image_pb2.TestImageResult()
278
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700279 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600280 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700281 self.assertTrue(output_proto.success)
282
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700283 self.PatchObject(image_service, 'Test', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600284 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700285 self.assertFalse(output_proto.success)