blob: 10132442a514153f8d9ca4b67cfd8c9402277d2f [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
Alex Klein56355682019-02-07 10:36:54 -070010import mock
Alex Klein2966e302019-01-17 13:29:38 -070011import os
12
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060014from chromite.api import controller
15from chromite.api.controller import image as image_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060016from chromite.api.gen.chromite.api import image_pb2
David Burger13e06be2019-05-13 20:33:16 -060017from chromite.api.gen.chromiumos import common_pb2
Alex Klein56355682019-02-07 10:36:54 -070018from chromite.lib import constants
Alex Klein4f0eb432019-05-02 13:56:04 -060019from chromite.lib import cros_build_lib
Alex Klein2966e302019-01-17 13:29:38 -070020from chromite.lib import cros_test_lib
Michael Mortensenc83c9952019-08-05 12:15:12 -060021from chromite.lib import image_lib
Alex Klein2966e302019-01-17 13:29:38 -070022from chromite.lib import osutils
Alex Kleinb7cdbe62019-02-22 11:41:32 -070023from chromite.service import image as image_service
Alex Klein2966e302019-01-17 13:29:38 -070024
25
Alex Klein231d2da2019-07-22 16:44:45 -060026class CreateTest(cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin):
Alex Klein56355682019-02-07 10:36:54 -070027 """Create image tests."""
28
Alex Klein231d2da2019-07-22 16:44:45 -060029 def setUp(self):
30 self.response = image_pb2.CreateImageResult()
31
Alex Klein21b95022019-05-09 14:14:46 -060032 def _GetRequest(self, board=None, types=None, version=None, builder_path=None,
33 disable_rootfs_verification=False):
34 """Helper to build a request instance."""
35 return image_pb2.CreateImageRequest(
36 build_target={'name': board},
37 image_types=types,
38 disable_rootfs_verification=disable_rootfs_verification,
39 version=version,
40 builder_path=builder_path,
41 )
42
Alex Klein231d2da2019-07-22 16:44:45 -060043 def testValidateOnly(self):
44 """Sanity check that a validate only call does not execute any logic."""
45 patch = self.PatchObject(image_service, 'Build')
Alex Klein21b95022019-05-09 14:14:46 -060046
Alex Klein231d2da2019-07-22 16:44:45 -060047 request = self._GetRequest(board='board')
48 image_controller.Create(request, self.response, self.validate_only_config)
49 patch.assert_not_called()
50
51 def testNoBoard(self):
52 """Test no board given fails."""
53 request = self._GetRequest()
Alex Klein56355682019-02-07 10:36:54 -070054
55 # No board should cause it to fail.
Alex Klein4f0eb432019-05-02 13:56:04 -060056 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060057 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070058
Alex Klein21b95022019-05-09 14:14:46 -060059 def testNoTypeSpecified(self):
60 """Test the image type default."""
61 request = self._GetRequest(board='board')
Alex Klein21b95022019-05-09 14:14:46 -060062
Alex Klein1bcd9882019-03-19 13:25:24 -060063 # Failed result to avoid the success handling logic.
64 result = image_service.BuildResult(1, [])
65 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
Alex Klein56355682019-02-07 10:36:54 -070066
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 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_BASE],
Alex Klein21b95022019-05-09 14:14:46 -060069 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070070
Alex Klein21b95022019-05-09 14:14:46 -060071 def testSingleTypeSpecified(self):
72 """Test it's properly using a specified type."""
73 request = self._GetRequest(board='board', types=[common_pb2.DEV])
Alex Klein21b95022019-05-09 14:14:46 -060074
75 # Failed result to avoid the success handling logic.
76 result = image_service.BuildResult(1, [])
77 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
78
Alex Klein231d2da2019-07-22 16:44:45 -060079 image_controller.Create(request, self.response, self.api_config)
Alex Klein56355682019-02-07 10:36:54 -070080 build_patch.assert_called_with(images=[constants.IMAGE_TYPE_DEV],
Alex Klein21b95022019-05-09 14:14:46 -060081 board='board', config=mock.ANY)
Alex Klein56355682019-02-07 10:36:54 -070082
Alex Klein21b95022019-05-09 14:14:46 -060083 def testMultipleAndImpliedTypes(self):
84 """Test multiple types and implied type handling."""
85 # The TEST_VM type should force it to build the test image.
86 types = [common_pb2.BASE, common_pb2.TEST_VM]
87 expected_images = [constants.IMAGE_TYPE_BASE, constants.IMAGE_TYPE_TEST]
88
89 request = self._GetRequest(board='board', types=types)
Alex Klein21b95022019-05-09 14:14:46 -060090
91 # Failed result to avoid the success handling logic.
92 result = image_service.BuildResult(1, [])
93 build_patch = self.PatchObject(image_service, 'Build', return_value=result)
94
Alex Klein231d2da2019-07-22 16:44:45 -060095 image_controller.Create(request, self.response, self.api_config)
Alex Klein21b95022019-05-09 14:14:46 -060096 build_patch.assert_called_with(images=expected_images, board='board',
Alex Klein56355682019-02-07 10:36:54 -070097 config=mock.ANY)
98
Alex Klein1bcd9882019-03-19 13:25:24 -060099 def testFailedPackageHandling(self):
100 """Test failed packages are populated correctly."""
101 result = image_service.BuildResult(1, ['foo/bar', 'cat/pkg'])
102 expected_packages = [('foo', 'bar'), ('cat', 'pkg')]
103 self.PatchObject(image_service, 'Build', return_value=result)
104
Alex Klein231d2da2019-07-22 16:44:45 -0600105 input_proto = self._GetRequest(board='board')
Alex Klein1bcd9882019-03-19 13:25:24 -0600106
Alex Klein231d2da2019-07-22 16:44:45 -0600107 rc = image_controller.Create(input_proto, self.response, self.api_config)
108
Alex Klein8cb365a2019-05-15 16:24:53 -0600109 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600110 for package in self.response.failed_packages:
Alex Klein1bcd9882019-03-19 13:25:24 -0600111 self.assertIn((package.category, package.package_name), expected_packages)
112
Alex Klein2557b4f2019-07-11 14:34:00 -0600113 def testNoPackagesFailureHandling(self):
114 """Test failed packages are populated correctly."""
115 result = image_service.BuildResult(1, [])
116 self.PatchObject(image_service, 'Build', return_value=result)
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700117
Alex Klein2557b4f2019-07-11 14:34:00 -0600118 input_proto = image_pb2.CreateImageRequest()
119 input_proto.build_target.name = 'board'
Alex Klein2557b4f2019-07-11 14:34:00 -0600120
Alex Klein231d2da2019-07-22 16:44:45 -0600121 rc = image_controller.Create(input_proto, self.response, self.api_config)
Alex Klein2557b4f2019-07-11 14:34:00 -0600122 self.assertTrue(rc)
123 self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE,
124 rc)
Alex Klein231d2da2019-07-22 16:44:45 -0600125 self.assertFalse(self.response.failed_packages)
Alex Klein2557b4f2019-07-11 14:34:00 -0600126
127
Alex Klein231d2da2019-07-22 16:44:45 -0600128class ImageSignerTestTest(cros_test_lib.MockTempDirTestCase,
129 api_config.ApiConfigMixin):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600130 """Image signer test tests."""
131
132 def setUp(self):
133 self.image_path = os.path.join(self.tempdir, 'image.bin')
Michael Mortensenc83c9952019-08-05 12:15:12 -0600134 self.result_directory = os.path.join(self.tempdir, 'results')
135
136 osutils.SafeMakedirs(self.result_directory)
137 osutils.Touch(self.image_path)
138
Alex Klein231d2da2019-07-22 16:44:45 -0600139 def testValidateOnly(self):
140 """Sanity check that validate-only calls don't execute any logic."""
141 patch = self.PatchObject(image_lib, 'SecurityTest', return_value=True)
142 input_proto = image_pb2.TestImageRequest()
143 input_proto.image.path = self.image_path
144 output_proto = image_pb2.TestImageResult()
145
146 image_controller.SignerTest(input_proto, output_proto,
147 self.validate_only_config)
148
149 patch.assert_not_called()
150
151 def testSignerTestNoImage(self):
152 """Test function argument validation."""
Michael Mortensenc83c9952019-08-05 12:15:12 -0600153 input_proto = image_pb2.TestImageRequest()
154 output_proto = image_pb2.TestImageResult()
155
156 # Nothing provided.
157 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600158 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600159
Alex Klein231d2da2019-07-22 16:44:45 -0600160 def testSignerTestSuccess(self):
161 """Test successful call handling."""
162 self.PatchObject(image_lib, 'SecurityTest', return_value=True)
163 input_proto = image_pb2.TestImageRequest()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600164 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600165 output_proto = image_pb2.TestImageResult()
Michael Mortensenc83c9952019-08-05 12:15:12 -0600166
Alex Klein231d2da2019-07-22 16:44:45 -0600167 image_controller.SignerTest(input_proto, output_proto, self.api_config)
168
169 def testSignerTestFailure(self):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600170 """Test function output tests."""
171 input_proto = image_pb2.TestImageRequest()
172 input_proto.image.path = self.image_path
Michael Mortensenc83c9952019-08-05 12:15:12 -0600173 output_proto = image_pb2.TestImageResult()
174
Michael Mortensenc83c9952019-08-05 12:15:12 -0600175 self.PatchObject(image_lib, 'SecurityTest', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600176 image_controller.SignerTest(input_proto, output_proto, self.api_config)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600177 self.assertFalse(output_proto.success)
178
Michael Mortensenc83c9952019-08-05 12:15:12 -0600179
Alex Klein231d2da2019-07-22 16:44:45 -0600180class ImageTestTest(cros_test_lib.MockTempDirTestCase,
181 api_config.ApiConfigMixin):
Alex Klein2557b4f2019-07-11 14:34:00 -0600182 """Image test tests."""
Alex Klein2966e302019-01-17 13:29:38 -0700183
184 def setUp(self):
185 self.image_path = os.path.join(self.tempdir, 'image.bin')
186 self.board = 'board'
187 self.result_directory = os.path.join(self.tempdir, 'results')
188
189 osutils.SafeMakedirs(self.result_directory)
190 osutils.Touch(self.image_path)
191
Alex Klein231d2da2019-07-22 16:44:45 -0600192 def testValidateOnly(self):
193 """Sanity check that a validate only call does not execute any logic."""
194 patch = self.PatchObject(image_service, 'Test')
195
196 input_proto = image_pb2.TestImageRequest()
197 input_proto.image.path = self.image_path
198 input_proto.build_target.name = self.board
199 input_proto.result.directory = self.result_directory
200 output_proto = image_pb2.TestImageResult()
201
202 image_controller.Test(input_proto, output_proto, self.validate_only_config)
203 patch.assert_not_called()
204
Alex Klein2966e302019-01-17 13:29:38 -0700205 def testTestArgumentValidation(self):
206 """Test function argument validation tests."""
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700207 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein2966e302019-01-17 13:29:38 -0700208 input_proto = image_pb2.TestImageRequest()
209 output_proto = image_pb2.TestImageResult()
210
211 # Nothing provided.
Alex Klein4f0eb432019-05-02 13:56:04 -0600212 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600213 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700214
215 # Just one argument.
216 input_proto.build_target.name = self.board
Alex Klein4f0eb432019-05-02 13:56:04 -0600217 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600218 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700219
220 # Two arguments provided.
221 input_proto.result.directory = self.result_directory
Alex Klein4f0eb432019-05-02 13:56:04 -0600222 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600223 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700224
225 # Invalid image path.
226 input_proto.image.path = '/invalid/image/path'
Alex Klein4f0eb432019-05-02 13:56:04 -0600227 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600228 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700229
230 # All valid arguments.
231 input_proto.image.path = self.image_path
Alex Klein231d2da2019-07-22 16:44:45 -0600232 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700233
234 def testTestOutputHandling(self):
235 """Test function output tests."""
236 input_proto = image_pb2.TestImageRequest()
237 input_proto.image.path = self.image_path
238 input_proto.build_target.name = self.board
239 input_proto.result.directory = self.result_directory
240 output_proto = image_pb2.TestImageResult()
241
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700242 self.PatchObject(image_service, 'Test', return_value=True)
Alex Klein231d2da2019-07-22 16:44:45 -0600243 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700244 self.assertTrue(output_proto.success)
245
Alex Kleinb7cdbe62019-02-22 11:41:32 -0700246 self.PatchObject(image_service, 'Test', return_value=False)
Alex Klein231d2da2019-07-22 16:44:45 -0600247 image_controller.Test(input_proto, output_proto, self.api_config)
Alex Klein2966e302019-01-17 13:29:38 -0700248 self.assertFalse(output_proto.success)