blob: 75e71ec7827e25b5b9e82ea3a809c70edd949c3d [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
52 def testNoBoard(self):
53 """Test no board given fails."""
54 request = self._GetRequest()
Alex Klein56355682019-02-07 10:36:54 -070055
56 # No board should cause it to fail.
Alex Klein4f0eb432019-05-02 13:56:04 -060057 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060058 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070059
Alex Klein21b95022019-05-09 14:14:46 -060060 def testNoTypeSpecified(self):
61 """Test the image type default."""
62 request = self._GetRequest(board='board')
Alex Klein21b95022019-05-09 14:14:46 -060063
Alex Klein1bcd9882019-03-19 13:25:24 -060064 # Failed result to avoid the success handling logic.
65 result = image_service.BuildResult(1, [])
66 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
Alex Klein56355682019-02-07 10:36:54 -070067
Alex Klein231d2da2019-07-22 16:44:45 -060068 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070069 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE],
Alex Klein21b95022019-05-09 14:14:46 -060070 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070071
Alex Klein21b95022019-05-09 14:14:46 -060072 def testSingleTypeSpecified(self):
73 """Test it's properly using a specified type."""
74 request = self._GetRequest(board='board', types=[common_pb2.DEV])
Alex Klein21b95022019-05-09 14:14:46 -060075
76 # Failed result to avoid the success handling logic.
77 result = image_service.BuildResult(1, [])
78 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
79
Alex Klein231d2da2019-07-22 16:44:45 -060080 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070081 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
Alex Klein21b95022019-05-09 14:14:46 -060082 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070083
Alex Klein21b95022019-05-09 14:14:46 -060084 def testMultipleAndImpliedTypes(self):
85 """Test multiple types and implied type handling."""
86 # The TEST_VM type should force it to build the test image.
87 types = [common_pb2.BASE, common_pb2.TEST_VM]
88 expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST]
89
90 request = self._GetRequest(board='board', types=types)
Alex Klein21b95022019-05-09 14:14:46 -060091
92 # Failed result to avoid the success handling logic.
93 result = image_service.BuildResult(1, [])
94 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
95
Alex Klein231d2da2019-07-22 16:44:45 -060096 image_controller.Create(request, self.response, self.api_config)
Alex Klein21b95022019-05-09 14:14:46 -060097 build_patch.assert_called_with(images=expected_images, board='board',
Alex Klein56355682019-02-07 10:36:54 -070098 config=mock.ANY)
99
Alex Klein1bcd9882019-03-19 13:25:24 -0600100 def testFailedPackageHandling(self):
101 """Test failed packages are populated correctly."""
102 result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg'])
103 expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
104 self.PatchObject(image_service, 'Build', return_value=result)
105
Alex Klein231d2da2019-07-22 16:44:45 -0600106 input_proto = self._GetRequest(board='board')
Alex Klein1bcd9882019-03-19 13:25:24 -0600107
Alex Klein231d2da2019-07-22 16:44:45 -0600108 rc = image_controller.Create(input_proto, self.response, self.api_config)
109
Alex Klein8cb365a2019-05-15 16:24:53 -0600110 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600111 for package in self.response.failed_packages:
Alex Klein1bcd9882019-03-19 13:25:24 -0600112 self.assertIn((package.category, package.package_name), expected_packages)
113
Alex Klein2557b4f2019-07-11 14:34:00 -0600114 def testNoPackagesFailureHandling(self):
115 """Test failed packages are populated correctly."""
116 result = image_service.BuildResult(1, [])
117 self.PatchObject(image_service, 'Build', return_value=result)
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700118
Alex Klein2557b4f2019-07-11 14:34:00 -0600119 input_proto = image_pb2.CreateImageRequest()
120 input_proto.build_target.name = 'board'
Alex Klein2557b4f2019-07-11 14:34:00 -0600121
Alex Klein231d2da2019-07-22 16:44:45 -0600122 rc = image_controller.Create(input_proto, self.response, self.api_config)
Alex Klein2557b4f2019-07-11 14:34:00 -0600123 self.assertTrue(rc)
124 self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
125 rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600126 self.assertFalse(self.response.failed_packages)
Alex Klein2557b4f2019-07-11 14:34:00 -0600127
128
Alex Klein231d2da2019-07-22 16:44:45 -0600129class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase,
130 api_config.ApiConfigMixin):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600131 """Image signer test tests."""
132
133 def setUp(self):
134 self.image_path = os.path.join(self.tempdir, 'image.bin')
Michael Mortensenc83c9952019-08-05 12:15:12 -0600135 self.result_directory = os.path.join(self.tempdir, 'results')
136
137 osutils.SafeMakedirs(self.result_directory)
138 osutils.Touch(self.image_path)
139
Alex Klein231d2da2019-07-22 16:44:45 -0600140 def testValidateOnly(self):
141 """Sanity check that validate-only calls don't execute any logic."""
142 patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True)
143 input_proto = image_pb2.TestImageRequest()
144 input_proto.image.path = self.image_path
145 output_proto = image_pb2.TestImageResult()
146
147 image_controller.SignerTest(input_proto, output_proto,
148 self.validate_only_config)
149
150 patch.assert_not_called()
151
152 def testSignerTestNoImage(self):
153 """Test function argument validation."""
Michael Mortensenc83c9952019-08-05 12:15:12 -0600154 input_proto = image_pb2.TestImageRequest()
155 output_proto = image_pb2.TestImageResult()
156
157 # Nothing provided.
158 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600159 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600160
Alex Klein231d2da2019-07-22 16:44:45 -0600161 def testSignerTestSuccess(self):
162 """Test successful call handling."""
163 self.PatchObject(image_lib, 'SecurityTest', return_value=True)
164 input_proto = image_pb2.TestImageRequest()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600165 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600166 output_proto = image_pb2.TestImageResult()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600167
Alex Klein231d2da2019-07-22 16:44:45 -0600168 image_controller.SignerTest(input_proto, output_proto, self.api_config)
169
170 def testSignerTestFailure(self):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600171 """Test function output tests."""
172 input_proto = image_pb2.TestImageRequest()
173 input_proto.image.path = self.image_path
Michael Mortensenc83c9952019-08-05 12:15:12 -0600174 output_proto = image_pb2.TestImageResult()
175
Michael Mortensenc83c9952019-08-05 12:15:12 -0600176 self.PatchObject(image_lib, 'SecurityTest', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600177 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600178 self.assertFalse(output_proto.success)
179
Michael Mortensenc83c9952019-08-05 12:15:12 -0600180
Alex Klein231d2da2019-07-22 16:44:45 -0600181class ImageTestTest(cros_test_lib.MockTempDirTestCase,
182 api_config.ApiConfigMixin):
Alex Klein2557b4f2019-07-11 14:34:00 -0600183 """Image test tests."""
Alex Klein2966e302019-01-17 13:29:38 -0700184
185 def setUp(self):
186 self.image_path = os.path.join(self.tempdir, 'image.bin')
187 self.board = 'board'
188 self.result_directory = os.path.join(self.tempdir, 'results')
189
190 osutils.SafeMakedirs(self.result_directory)
191 osutils.Touch(self.image_path)
192
Alex Klein231d2da2019-07-22 16:44:45 -0600193 def testValidateOnly(self):
194 """Sanity check that a validate only call does not execute any logic."""
195 patch = self.PatchObject(image_service, 'Test')
196
197 input_proto = image_pb2.TestImageRequest()
198 input_proto.image.path = self.image_path
199 input_proto.build_target.name = self.board
200 input_proto.result.directory = self.result_directory
201 output_proto = image_pb2.TestImageResult()
202
203 image_controller.Test(input_proto, output_proto, self.validate_only_config)
204 patch.assert_not_called()
205
Alex Klein2966e302019-01-17 13:29:38 -0700206 def testTestArgumentValidation(self):
207 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700208 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700209 input_proto = image_pb2.TestImageRequest()
210 output_proto = image_pb2.TestImageResult()
211
212 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600213 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600214 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700215
216 # Just one argument.
217 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600218 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600219 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700220
221 # Two arguments provided.
222 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600223 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600224 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700225
226 # Invalid image path.
227 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600228 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600229 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700230
231 # All valid arguments.
232 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600233 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700234
235 def testTestOutputHandling(self):
236 """Test function output tests."""
237 input_proto = image_pb2.TestImageRequest()
238 input_proto.image.path = self.image_path
239 input_proto.build_target.name = self.board
240 input_proto.result.directory = self.result_directory
241 output_proto = image_pb2.TestImageResult()
242
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700243 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600244 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700245 self.assertTrue(output_proto.success)
246
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700247 self.PatchObject(image_service, 'Test', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600248 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700249 self.assertFalse(output_proto.success)